Mercurial > pygar
comparison modules/bluespec/Pygar/core/SFIFO.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 |
comparison
equal
deleted
inserted
replaced
7:7393cd19371e | 8:74716e9a81cc |
---|---|
1 | |
2 import FIFO::*; | |
3 import ConfigReg::*; | |
4 import RWire::*; | |
5 | |
6 import List::*; | |
7 import Monad::*; | |
8 | |
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); | |
16 | |
17 endinterface | |
18 | |
19 module mkSFIFO#(function Bool searchfunc(search_T s, alpha_T x)) (SFIFO#(alpha_T, search_T)) | |
20 provisos | |
21 (Bits#(alpha_T,asz)); | |
22 | |
23 Reg#(alpha_T) f0 <- mkConfigRegU(); | |
24 Reg#(alpha_T) f1 <- mkConfigRegU(); | |
25 | |
26 Reg#(Bool) vf0 <- mkConfigReg(False); | |
27 Reg#(Bool) vf1 <- mkConfigReg(False); | |
28 | |
29 PulseWire edge1 <- mkPulseWire(); | |
30 | |
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 | |
44 | |
45 method Action deq() if (vf0); | |
46 edge1.send(); | |
47 vf0 <= vf1; | |
48 f0 <= f1; | |
49 vf1 <= False; | |
50 endmethod | |
51 | |
52 method alpha_T first() if(vf0); | |
53 return (f0); | |
54 endmethod | |
55 | |
56 method Action clear(); | |
57 vf0 <= False; | |
58 vf1 <= False; | |
59 endmethod | |
60 | |
61 method Bool find(search_T sv); | |
62 Bool nvf0 = edge1 ? False: vf0; | |
63 Bool nvf1 = vf1; | |
64 | |
65 return (nvf0 && searchfunc(sv, f0) || | |
66 nvf1 && searchfunc(sv, f1)); | |
67 endmethod | |
68 | |
69 method Bool find2(search_T sv); | |
70 Bool nvf0 = edge1 ? False: vf0; | |
71 Bool nvf1 = vf1; | |
72 | |
73 return (nvf0 && searchfunc(sv, f0) || | |
74 nvf1 && searchfunc(sv, f1)); | |
75 endmethod | |
76 | |
77 endmodule | |
78 | |
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)); | |
82 | |
83 Reg#(alpha_T) f0 <- mkConfigRegU; | |
84 | |
85 Reg#(Bool) vf0 <- mkConfigReg(False); | |
86 | |
87 PulseWire edge1 <- mkPulseWire(); | |
88 | |
89 method Action enq(alpha_T x) if (!vf0); | |
90 vf0 <= True; //True | |
91 f0 <= x; | |
92 endmethod | |
93 | |
94 method Action deq() if (vf0); | |
95 edge1.send(); | |
96 vf0 <= False; | |
97 endmethod | |
98 | |
99 method alpha_T first() if(vf0); | |
100 return (f0); | |
101 endmethod | |
102 | |
103 method Action clear(); | |
104 vf0 <= False; | |
105 endmethod | |
106 | |
107 method Bool find(search_T sv); | |
108 Bool nvf0 = edge1 ? False: vf0; | |
109 | |
110 return (nvf0 && searchfunc(sv, f0)); | |
111 endmethod | |
112 | |
113 method Bool find2(search_T sv); | |
114 Bool nvf0 = edge1 ? False: vf0; | |
115 return (nvf0 && searchfunc(sv, f0)); | |
116 endmethod | |
117 | |
118 endmodule | |
119 | |
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)) | |
123 | |
124 provisos ( Bits#(alpha_T,alpha_SZ) ); | |
125 | |
126 List#(Reg#(alpha_T)) registers <- replicateM(n, mkRegU); | |
127 List#(Reg#(Bool)) valids <- replicateM(n, mkReg(False)); | |
128 | |
129 function Nat getNextFree (List#(Reg#(Bool)) vs); | |
130 | |
131 Nat res = fromInteger(n - 1); | |
132 | |
133 for (Integer x = n - 1; x > -1; x = x - 1) | |
134 res = !vs[x]._read() ? fromInteger(x) : res; | |
135 | |
136 return res; | |
137 | |
138 endfunction | |
139 | |
140 function Bool notFull(); | |
141 | |
142 Bool full = True; | |
143 | |
144 for (Integer x = 0; x < n; x = x + 1) | |
145 full = full && valids[x]._read(); | |
146 | |
147 return !full; | |
148 | |
149 endfunction | |
150 | |
151 method Action enq( alpha_T item ) if ( notFull() ); | |
152 | |
153 Nat k = getNextFree(valids); | |
154 select(valids, k)._write(True); | |
155 select(registers, k)._write(item); | |
156 | |
157 endmethod | |
158 | |
159 method Action deq() if ( valids[0]._read() ); | |
160 | |
161 for (Integer x = 0; x < (n-1); x = x + 1) | |
162 begin | |
163 | |
164 (registers[x]) <= registers[x + 1]._read(); | |
165 (valids[x]) <= valids[x + 1]._read(); | |
166 | |
167 end | |
168 (valids[n-1]) <= False; | |
169 endmethod | |
170 | |
171 method alpha_T first() if ( valids[0]._read() ); | |
172 return registers[0]._read(); | |
173 endmethod | |
174 | |
175 method Bool find(search_T sv); | |
176 Bool res = False; | |
177 | |
178 for (Integer x = 0; x < n; x = x + 1) | |
179 if ( valids[x]._read() && searchfunc1(sv, registers[x]._read()) ) | |
180 res = True; | |
181 | |
182 return res; | |
183 | |
184 endmethod | |
185 | |
186 method Bool find2(search_T sv); | |
187 Bool res = False; | |
188 | |
189 for (Integer x = 0; x < n; x = x + 1) | |
190 if ( valids[x]._read() && searchfunc2(sv, registers[x]._read()) ) | |
191 res = True; | |
192 | |
193 return res; | |
194 | |
195 endmethod | |
196 | |
197 method Action clear(); | |
198 | |
199 for (Integer x = 0; x < n; x = x + 1) | |
200 (valids[x]) <= False; | |
201 | |
202 endmethod | |
203 | |
204 endmodule | |
205 | |
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)); | |
209 | |
210 let foo <- mkSizedSFIFOInternal(n, searchfunc, searchfunc); | |
211 return foo; | |
212 | |
213 endmodule |