view core/src/SFIFO.bsv @ 1:91a1f76ddd62 pygar svn.2

[svn r2] Adding initial lab 5 source
author punk
date Tue, 13 Apr 2010 17:34:33 -0400
parents
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 endinterface
19 module mkSFIFO#(function Bool searchfunc(search_T s, alpha_T x)) (SFIFO#(alpha_T, search_T))
20 provisos
21 (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 dequeueing
33 begin
34 vf0 <= True; //True
35 vf1 <= False;
36 f0 <= x;
37 end
38 else // !vf1
39 begin
40 vf1 <= True;
41 f1 <= x;
42 end
43 endmethod
45 method Action deq() if (vf0);
46 edge1.send();
47 vf0 <= vf1;
48 f0 <= f1;
49 vf1 <= False;
50 endmethod
52 method alpha_T first() if(vf0);
53 return (f0);
54 endmethod
56 method Action clear();
57 vf0 <= False;
58 vf1 <= False;
59 endmethod
61 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 endmethod
69 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 endmethod
77 endmodule
79 module mkSFIFO1#(function Bool searchfunc(search_T s, alpha_T x)) (SFIFO#(alpha_T, search_T))
80 provisos
81 (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; //True
91 f0 <= x;
92 endmethod
94 method Action deq() if (vf0);
95 edge1.send();
96 vf0 <= False;
97 endmethod
99 method alpha_T first() if(vf0);
100 return (f0);
101 endmethod
103 method Action clear();
104 vf0 <= False;
105 endmethod
107 method Bool find(search_T sv);
108 Bool nvf0 = edge1 ? False: vf0;
110 return (nvf0 && searchfunc(sv, f0));
111 endmethod
113 method Bool find2(search_T sv);
114 Bool nvf0 = edge1 ? False: vf0;
115 return (nvf0 && searchfunc(sv, f0));
116 endmethod
118 endmodule
120 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 endfunction
140 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 endfunction
151 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 endmethod
159 method Action deq() if ( valids[0]._read() );
161 for (Integer x = 0; x < (n-1); x = x + 1)
162 begin
164 (registers[x]) <= registers[x + 1]._read();
165 (valids[x]) <= valids[x + 1]._read();
167 end
168 (valids[n-1]) <= False;
169 endmethod
171 method alpha_T first() if ( valids[0]._read() );
172 return registers[0]._read();
173 endmethod
175 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 endmethod
186 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 endmethod
197 method Action clear();
199 for (Integer x = 0; x < n; x = x + 1)
200 (valids[x]) <= False;
202 endmethod
204 endmodule
206 module mkSizedSFIFO#(Integer n, function Bool searchfunc(search_T s, alpha_T x)) (SFIFO#(alpha_T, search_T))
207 provisos
208 (Bits#(alpha_T,asz));
210 let foo <- mkSizedSFIFOInternal(n, searchfunc, searchfunc);
211 return foo;
213 endmodule