Mercurial > pygar
view modules/bluespec/Pygar/core/SFIFO.bsv @ 12:394aa40fd812 pygar svn.13
[svn r13] more stuff
author | punk |
---|---|
date | Tue, 27 Apr 2010 09:02:23 -0400 |
parents | 74716e9a81cc |
children |
line wrap: on
line source
2 import FIFO::*;3 import ConfigReg::*;4 import RWire::*;6 import List::*;7 import Monad::*;9 interface SFIFO#(type alpha_T, type search_T);10 method Action enq(alpha_T x);11 method Action deq();12 method alpha_T first();13 method Action clear();14 method Bool find(search_T x);15 method Bool find2(search_T x);17 endinterface19 module mkSFIFO#(function Bool searchfunc(search_T s, alpha_T x)) (SFIFO#(alpha_T, search_T))20 provisos21 (Bits#(alpha_T,asz));23 Reg#(alpha_T) f0 <- mkConfigRegU();24 Reg#(alpha_T) f1 <- mkConfigRegU();26 Reg#(Bool) vf0 <- mkConfigReg(False);27 Reg#(Bool) vf1 <- mkConfigReg(False);29 PulseWire edge1 <- mkPulseWire();31 method Action enq(alpha_T x) if (!(vf0 && vf1));32 if (edge1 || !vf0)//empty or we're dequeueing33 begin34 vf0 <= True; //True35 vf1 <= False;36 f0 <= x;37 end38 else // !vf139 begin40 vf1 <= True;41 f1 <= x;42 end43 endmethod45 method Action deq() if (vf0);46 edge1.send();47 vf0 <= vf1;48 f0 <= f1;49 vf1 <= False;50 endmethod52 method alpha_T first() if(vf0);53 return (f0);54 endmethod56 method Action clear();57 vf0 <= False;58 vf1 <= False;59 endmethod61 method Bool find(search_T sv);62 Bool nvf0 = edge1 ? False: vf0;63 Bool nvf1 = vf1;65 return (nvf0 && searchfunc(sv, f0) ||66 nvf1 && searchfunc(sv, f1));67 endmethod69 method Bool find2(search_T sv);70 Bool nvf0 = edge1 ? False: vf0;71 Bool nvf1 = vf1;73 return (nvf0 && searchfunc(sv, f0) ||74 nvf1 && searchfunc(sv, f1));75 endmethod77 endmodule79 module mkSFIFO1#(function Bool searchfunc(search_T s, alpha_T x)) (SFIFO#(alpha_T, search_T))80 provisos81 (Bits#(alpha_T,asz), Eq#(alpha_T));83 Reg#(alpha_T) f0 <- mkConfigRegU;85 Reg#(Bool) vf0 <- mkConfigReg(False);87 PulseWire edge1 <- mkPulseWire();89 method Action enq(alpha_T x) if (!vf0);90 vf0 <= True; //True91 f0 <= x;92 endmethod94 method Action deq() if (vf0);95 edge1.send();96 vf0 <= False;97 endmethod99 method alpha_T first() if(vf0);100 return (f0);101 endmethod103 method Action clear();104 vf0 <= False;105 endmethod107 method Bool find(search_T sv);108 Bool nvf0 = edge1 ? False: vf0;110 return (nvf0 && searchfunc(sv, f0));111 endmethod113 method Bool find2(search_T sv);114 Bool nvf0 = edge1 ? False: vf0;115 return (nvf0 && searchfunc(sv, f0));116 endmethod118 endmodule120 module mkSizedSFIFOInternal#(Integer n,121 function Bool searchfunc1(search_T s, alpha_T x),122 function Bool searchfunc2(search_T s, alpha_T x)) (SFIFO#(alpha_T, search_T))124 provisos ( Bits#(alpha_T,alpha_SZ) );126 List#(Reg#(alpha_T)) registers <- replicateM(n, mkRegU);127 List#(Reg#(Bool)) valids <- replicateM(n, mkReg(False));129 function Nat getNextFree (List#(Reg#(Bool)) vs);131 Nat res = fromInteger(n - 1);133 for (Integer x = n - 1; x > -1; x = x - 1)134 res = !vs[x]._read() ? fromInteger(x) : res;136 return res;138 endfunction140 function Bool notFull();142 Bool full = True;144 for (Integer x = 0; x < n; x = x + 1)145 full = full && valids[x]._read();147 return !full;149 endfunction151 method Action enq( alpha_T item ) if ( notFull() );153 Nat k = getNextFree(valids);154 select(valids, k)._write(True);155 select(registers, k)._write(item);157 endmethod159 method Action deq() if ( valids[0]._read() );161 for (Integer x = 0; x < (n-1); x = x + 1)162 begin164 (registers[x]) <= registers[x + 1]._read();165 (valids[x]) <= valids[x + 1]._read();167 end168 (valids[n-1]) <= False;169 endmethod171 method alpha_T first() if ( valids[0]._read() );172 return registers[0]._read();173 endmethod175 method Bool find(search_T sv);176 Bool res = False;178 for (Integer x = 0; x < n; x = x + 1)179 if ( valids[x]._read() && searchfunc1(sv, registers[x]._read()) )180 res = True;182 return res;184 endmethod186 method Bool find2(search_T sv);187 Bool res = False;189 for (Integer x = 0; x < n; x = x + 1)190 if ( valids[x]._read() && searchfunc2(sv, registers[x]._read()) )191 res = True;193 return res;195 endmethod197 method Action clear();199 for (Integer x = 0; x < n; x = x + 1)200 (valids[x]) <= False;202 endmethod204 endmodule206 module mkSizedSFIFO#(Integer n, function Bool searchfunc(search_T s, alpha_T x)) (SFIFO#(alpha_T, search_T))207 provisos208 (Bits#(alpha_T,asz));210 let foo <- mkSizedSFIFOInternal(n, searchfunc, searchfunc);211 return foo;213 endmodule