Mercurial > pygar
view modules/bluespec/Pygar/lab1/FIRFilterDefault.bsv @ 35:14f7a7ace3f5 pygar svn.36
[svn r36] created test wav files
author | rlm |
---|---|
date | Tue, 04 May 2010 12:14:24 -0400 |
parents | 74716e9a81cc |
children |
line wrap: on
line source
1 // The MIT License3 // Copyright (c) 2009 Massachusetts Institute of Technology5 // Permission is hereby granted, free of charge, to any person obtaining a copy6 // of this software and associated documentation files (the "Software"), to deal7 // in the Software without restriction, including without limitation the rights8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell9 // copies of the Software, and to permit persons to whom the Software is10 // furnished to do so, subject to the following conditions:12 // The above copyright notice and this permission notice shall be included in13 // all copies or substantial portions of the Software.15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19 // 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 IN21 // THE SOFTWARE.23 // Author: Kermin Fleming kfleming@mit.edu25 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 communicate33 // with the outside world, and are part of the AWB library code35 `include "asim/provides/soft_connections.bsh"36 `include "asim/provides/common_services.bsh"38 // Local includes. Look for the correspondingly named .awb files39 // workspace/labs/src/mit-6.375/modules/bluespec/mit-6.375/common/40 // to find the actual Bluespec files which are used to generate41 // these includes. These files are specific to this audio processing42 // pipeline44 `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 FIFO53 // mkFIFO returns a fifo of length 2 (by default)54 // AudioProcessorUnit is the name given to the packets55 // of DATA processed by our audio pipeline. For their56 // definition, look in the file57 // workspace/labs/src/mit-6.375/modules/bluespec/mit-6.375/common/AudioProcessorTypes.bsv59 FIFO#(AudioProcessorUnit) infifo <- mkFIFO;60 FIFO#(AudioProcessorUnit) outfifo <- mkFIFO;63 // an alternate syntax for instantiating the samples vector64 // 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 how69 // vectors can be instantiated during the static elaboration70 // 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 FixedPoint80 // representation. The compiler is smart enough to infer the81 // type (bit width) of the fixed point (in this case, we have 1682 // 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 in96 // 16.16 fixed point. This preserves the magnitude of the input97 // pcm. This code was implemented using for loops so as to be more98 // clear. Using the functions map, fold, readVReg, and writeVReg99 // would have been more concise.101 rule process (infifo.first matches tagged Sample .sample);103 // Advance the fir filter, by shifting all the elements104 // 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 begin109 samples[i+1] <= samples[i];110 end112 // Filter the values, using an inefficient adder chain. You will113 // 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 begin118 accumulate = accumulate + firCoefs[1+i] * fromInt(samples[i]);119 end121 outfifo.enq(tagged Sample fxptGetInt(accumulate));123 infifo.deq;124 endrule126 // Handle the end of stream condition. Look at the two rule guards,127 // these are obviously mutually exclusive. The definition of128 // AudioProcessorUnit shows that it can be tagged only as a Sample, or129 // EndOfFile; nothing else!131 rule endOfFile (infifo.first matches tagged EndOfFile);133 $display("FIR got end of file");135 // Reset state for next invocation136 for(Integer i = 0; i < valueof(Taps); i = i + 1)137 begin138 samples[i] <= 0;139 pr[i] <= 0;140 end142 // pass the end-of-file token down the pipeline, eventually this will143 // make it back to the software side, to notify it that the stream144 // has been processed completely146 outfifo.enq(infifo.first);147 infifo.deq;148 endrule151 // this section connects the fifos instantiated internally to the152 // externally visible interface154 interface sampleInput = fifoToPut(infifo);155 interface sampleOutput = fifoToGet(outfifo);157 endmodule