annotate modules/bluespec/Pygar/lab1/FIRFilterDefault.bsv @ 41:16ba43f0a7c3 pygar svn.42

[svn r42] got channels working
author rlm
date Tue, 04 May 2010 22:32:52 -0400
parents 74716e9a81cc
children
rev   line source
rlm@8 1 // The MIT License
rlm@8 2
rlm@8 3 // Copyright (c) 2009 Massachusetts Institute of Technology
rlm@8 4
rlm@8 5 // Permission is hereby granted, free of charge, to any person obtaining a copy
rlm@8 6 // of this software and associated documentation files (the "Software"), to deal
rlm@8 7 // in the Software without restriction, including without limitation the rights
rlm@8 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
rlm@8 9 // copies of the Software, and to permit persons to whom the Software is
rlm@8 10 // furnished to do so, subject to the following conditions:
rlm@8 11
rlm@8 12 // The above copyright notice and this permission notice shall be included in
rlm@8 13 // all copies or substantial portions of the Software.
rlm@8 14
rlm@8 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
rlm@8 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
rlm@8 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
rlm@8 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
rlm@8 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
rlm@8 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
rlm@8 21 // THE SOFTWARE.
rlm@8 22
rlm@8 23 // Author: Kermin Fleming kfleming@mit.edu
rlm@8 24
rlm@8 25 import Connectable::*;
rlm@8 26 import GetPut::*;
rlm@8 27 import ClientServer::*;
rlm@8 28 import FIFO::*;
rlm@8 29 import FixedPoint::*;
rlm@8 30 import Vector::*;
rlm@8 31
rlm@8 32 //AWB includes. These import the structure whcih allow us to communicate
rlm@8 33 // with the outside world, and are part of the AWB library code
rlm@8 34
rlm@8 35 `include "asim/provides/soft_connections.bsh"
rlm@8 36 `include "asim/provides/common_services.bsh"
rlm@8 37
rlm@8 38 // Local includes. Look for the correspondingly named .awb files
rlm@8 39 // workspace/labs/src/mit-6.375/modules/bluespec/mit-6.375/common/
rlm@8 40 // to find the actual Bluespec files which are used to generate
rlm@8 41 // these includes. These files are specific to this audio processing
rlm@8 42 // pipeline
rlm@8 43
rlm@8 44 `include "asim/provides/audio_pipeline_types.bsh"
rlm@8 45 `include "asim/provides/audio_processor_types.bsh"
rlm@8 46
rlm@8 47 typedef 8 Taps;
rlm@8 48
rlm@8 49 module [Connected_Module] mkFIRFilter (FIRFilter);
rlm@8 50
rlm@8 51
rlm@8 52 // instantiate an input FIFO and an Output FIFO
rlm@8 53 // mkFIFO returns a fifo of length 2 (by default)
rlm@8 54 // AudioProcessorUnit is the name given to the packets
rlm@8 55 // of DATA processed by our audio pipeline. For their
rlm@8 56 // definition, look in the file
rlm@8 57 // workspace/labs/src/mit-6.375/modules/bluespec/mit-6.375/common/AudioProcessorTypes.bsv
rlm@8 58
rlm@8 59 FIFO#(AudioProcessorUnit) infifo <- mkFIFO;
rlm@8 60 FIFO#(AudioProcessorUnit) outfifo <- mkFIFO;
rlm@8 61
rlm@8 62
rlm@8 63 // an alternate syntax for instantiating the samples vector
rlm@8 64 // would have been as follows:
rlm@8 65 //
rlm@8 66 // Vector#(Taps,Reg#(Sample)) samples <- replicateM(mkReg(0));
rlm@8 67 //
rlm@8 68 // we have used an explicit loop here, to demonstrate how
rlm@8 69 // vectors can be instantiated during the static elaboration
rlm@8 70 // phase, even though replicateM is far more concise.
rlm@8 71
rlm@8 72 Vector#(Taps,Reg#(Sample)) samples = newVector();
rlm@8 73 for(Integer i = 0; i < valueof(Taps); i=i+1)
rlm@8 74 samples[i] <- mkReg(0);
rlm@8 75
rlm@8 76 Vector#(9,Reg#(FixedPoint#(16,16))) pr <- replicateM(mkReg(0));
rlm@8 77
rlm@8 78
rlm@8 79 // fromReal takes a Real number and converts it to a FixedPoint
rlm@8 80 // representation. The compiler is smart enough to infer the
rlm@8 81 // type (bit width) of the fixed point (in this case, we have 16
rlm@8 82 // bits of integer, and 16 bits of fraction.
rlm@8 83
rlm@8 84 FixedPoint#(16,16) firCoefs [9] = {fromReal(-0.0124),
rlm@8 85 fromReal(0.0),
rlm@8 86 fromReal(-0.0133),
rlm@8 87 fromReal(0.0),
rlm@8 88 fromReal(0.8181),
rlm@8 89 fromReal(0.0),
rlm@8 90 fromReal(-0.0133),
rlm@8 91 fromReal(0.0),
rlm@8 92 fromReal(-0.0124)};
rlm@8 93
rlm@8 94
rlm@8 95 // This rule implements a fir filter. We do the fir computations in
rlm@8 96 // 16.16 fixed point. This preserves the magnitude of the input
rlm@8 97 // pcm. This code was implemented using for loops so as to be more
rlm@8 98 // clear. Using the functions map, fold, readVReg, and writeVReg
rlm@8 99 // would have been more concise.
rlm@8 100
rlm@8 101 rule process (infifo.first matches tagged Sample .sample);
rlm@8 102
rlm@8 103 // Advance the fir filter, by shifting all the elements
rlm@8 104 // down the Vector of registers (like a shift register)
rlm@8 105
rlm@8 106 samples[0] <= sample;
rlm@8 107 for(Integer i = 0; i < valueof(Taps) - 1; i = i + 1)
rlm@8 108 begin
rlm@8 109 samples[i+1] <= samples[i];
rlm@8 110 end
rlm@8 111
rlm@8 112 // Filter the values, using an inefficient adder chain. You will
rlm@8 113 // need to shorten the combinatorial path, by pipelining this logic.
rlm@8 114
rlm@8 115 FixedPoint#(16,16) accumulate= firCoefs[0] * fromInt(sample);
rlm@8 116 for(Integer i = 0; i < valueof(Taps); i = i + 1)
rlm@8 117 begin
rlm@8 118 accumulate = accumulate + firCoefs[1+i] * fromInt(samples[i]);
rlm@8 119 end
rlm@8 120
rlm@8 121 outfifo.enq(tagged Sample fxptGetInt(accumulate));
rlm@8 122
rlm@8 123 infifo.deq;
rlm@8 124 endrule
rlm@8 125
rlm@8 126 // Handle the end of stream condition. Look at the two rule guards,
rlm@8 127 // these are obviously mutually exclusive. The definition of
rlm@8 128 // AudioProcessorUnit shows that it can be tagged only as a Sample, or
rlm@8 129 // EndOfFile; nothing else!
rlm@8 130
rlm@8 131 rule endOfFile (infifo.first matches tagged EndOfFile);
rlm@8 132
rlm@8 133 $display("FIR got end of file");
rlm@8 134
rlm@8 135 // Reset state for next invocation
rlm@8 136 for(Integer i = 0; i < valueof(Taps); i = i + 1)
rlm@8 137 begin
rlm@8 138 samples[i] <= 0;
rlm@8 139 pr[i] <= 0;
rlm@8 140 end
rlm@8 141
rlm@8 142 // pass the end-of-file token down the pipeline, eventually this will
rlm@8 143 // make it back to the software side, to notify it that the stream
rlm@8 144 // has been processed completely
rlm@8 145
rlm@8 146 outfifo.enq(infifo.first);
rlm@8 147 infifo.deq;
rlm@8 148 endrule
rlm@8 149
rlm@8 150
rlm@8 151 // this section connects the fifos instantiated internally to the
rlm@8 152 // externally visible interface
rlm@8 153
rlm@8 154 interface sampleInput = fifoToPut(infifo);
rlm@8 155 interface sampleOutput = fifoToGet(outfifo);
rlm@8 156
rlm@8 157 endmodule