view modules/bluespec/Pygar/lab1/FIRFilterDefault.bsv @ 8:74716e9a81cc pygar svn.9

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