more reset code
[fleet.git] / ships / Lut3.ship
1 ship: Lut3
2
3 == Ports ===========================================================
4 data  in:   in1
5 data  in:   in2
6 data  in:   in3
7 data  in:   inLut
8
9 data  out:  out
10
11 == Constants ========================================================
12
13 AND
14 OR
15 XOR
16 NAND
17 NOR
18
19 == TeX ==============================================================
20
21 This ship implements a bitwise 3-input {\bf L}ook {\bf U}p {\bf
22 T}able.  The least significant eight bits of the {\tt inLut} value
23 form a truth table with three inputs and one output.
24
25 When values are available at all four inputs they are consumed and a
26 value is produced at {\tt out}.  Each bit of {\tt out} is produced by
27 looking up the corresponding bits of {\tt in1}, {\tt in2}, and {\tt
28 in3} in the {\tt inLut} truth table.
29
30 In particular, the bits {\tt in1}, {\tt in2}, {\tt in3} are
31 concatenated to form a three-bit number, with {\tt in3} as the {\it
32 most significant} bit and {\tt in1} as the {\it least significant
33 bit}.  This three-bit number, ranging from 0 to 7 (decimal), is used
34 as a bit index into {\tt inLut}'s value (whose least significant bit
35 is considered ``bit zero'').
36
37
38 == Fleeterpreter ====================================================
39   public void service() {
40       if (box_in1.dataReadyForShip() &&
41           box_in2.dataReadyForShip() &&
42           box_in3.dataReadyForShip() &&
43           box_inLut.dataReadyForShip() &&
44           box_out.readyForDataFromShip()) {
45           long a      = box_in1.removeDataForShip();
46           long b      = box_in2.removeDataForShip();
47           long c      = box_in3.removeDataForShip();
48           long lut    = box_inLut.removeDataForShip();
49           long ret = 0;
50           ret |= ((lut & (1<<0))==0) ? 0 : (~a) & (~b) & (~c);
51           ret |= ((lut & (1<<1))==0) ? 0 : ( a) & (~b) & (~c);
52           ret |= ((lut & (1<<2))==0) ? 0 : (~a) & ( b) & (~c);
53           ret |= ((lut & (1<<3))==0) ? 0 : ( a) & ( b) & (~c);
54           ret |= ((lut & (1<<4))==0) ? 0 : (~a) & (~b) & ( c);
55           ret |= ((lut & (1<<5))==0) ? 0 : ( a) & (~b) & ( c);
56           ret |= ((lut & (1<<6))==0) ? 0 : (~a) & ( b) & ( c);
57           ret |= ((lut & (1<<7))==0) ? 0 : ( a) & ( b) & ( c);
58           box_out.addDataFromShip(ret);
59       }
60   }
61
62 == FleetSim ==============================================================
63 == FPGA ==============================================================
64
65   reg                    have_in1;
66   reg [(`DATAWIDTH-1):0] reg_in1;
67   reg                    have_in2;
68   reg [(`DATAWIDTH-1):0] reg_in2;
69   reg                    have_in3;
70   reg [(`DATAWIDTH-1):0] reg_in3;
71   reg                    have_inLut;
72   reg [(`DATAWIDTH-1):0] reg_inLut;
73
74   wire [(`DATAWIDTH-1):0] out;
75   genvar i;
76   generate
77     for(i=0; i<`DATAWIDTH; i=i+1) begin : OUT
78       assign out[i] = reg_inLut[{reg_in3[i], reg_in2[i], reg_in1[i]}];
79     end
80   endgenerate
81
82   always @(posedge clk) begin
83     if (!rst) begin
84       have_in1 = 0;
85       have_in2 = 0;
86       have_in3 = 0;
87       have_inLut = 0;
88       `reset
89     end else begin
90     if (!have_in1) begin
91       `onread(in1_r, in1_a) have_in1 = 1; reg_in1 = in1_d; end
92       end else
93     if (!have_in2) begin
94       `onread(in2_r, in2_a) have_in2 = 1; reg_in2 = in2_d; end
95       end else
96     if (!have_in3) begin
97       `onread(in3_r, in3_a) have_in3 = 1; reg_in3 = in3_d; end
98       end else
99     if (!have_inLut) begin
100       `onread(inLut_r, inLut_a) have_inLut = 1; reg_inLut = inLut_d; end
101       end else
102   
103     if (have_in1 && have_in2 && have_in3 && have_inLut) begin
104       out_d = out;
105       `onwrite(out_r, out_a)
106         have_in1  = 0;
107         have_in2  = 0;
108         have_in3  = 0;
109         have_inLut = 0;
110       end
111     end
112     end
113   end
114
115
116 == Test =================================================================
117 #expect  0
118 #expect  -128
119 #expect  64
120 #expect  -64
121 #expect  32
122 #expect  -96
123 #expect  96
124 #expect  -32
125 #expect  16
126 #expect  -112
127 #expect  80
128 #expect  -48
129 #expect  48
130 #expect  -80
131 #expect  112
132 #expect  -16
133 #expect  8
134 #expect  -120
135 #expect  72
136 #expect  -56
137 #expect  40
138 #expect  -88
139 #expect  104
140 #expect  -24
141 #expect  24
142 #expect  -104
143 #expect  88
144 #expect  -40
145 #expect  56
146 #expect  -72
147 #expect  120
148 #expect  -8
149 #expect  4
150 #expect  -124
151 #expect  68
152 #expect  -60
153 #expect  36
154 #expect  -92
155 #expect  100
156 #expect  -28
157 #expect  20
158 #expect  -108
159 #expect  84
160 #expect  -44
161 #expect  52
162 #expect  -76
163 #expect  116
164 #expect  -12
165 #expect  12
166 #expect  -116
167 #expect  76
168 #expect  -52
169 #expect  44
170 #expect  -84
171 #expect  108
172 #expect  -20
173 #expect  28
174 #expect  -100
175 #expect  92
176 #expect  -36
177 #expect  60
178 #expect  -68
179 #expect  124
180 #expect  -4
181 #expect  2
182 #expect  -126
183 #expect  66
184 #expect  -62
185 #expect  34
186 #expect  -94
187 #expect  98
188 #expect  -30
189 #expect  18
190 #expect  -110
191 #expect  82
192 #expect  -46
193 #expect  50
194 #expect  -78
195 #expect  114
196 #expect  -14
197 #expect  10
198 #expect  -118
199 #expect  74
200 #expect  -54
201 #expect  42
202 #expect  -86
203 #expect  106
204 #expect  -22
205 #expect  26
206 #expect  -102
207 #expect  90
208 #expect  -38
209 #expect  58
210 #expect  -70
211 #expect  122
212 #expect  -6
213 #expect  6
214 #expect  -122
215 #expect  70
216 #expect  -58
217 #expect  38
218 #expect  -90
219 #expect  102
220 #expect  -26
221 #expect  22
222 #expect  -106
223 #expect  86
224 #expect  -42
225 #expect  54
226 #expect  -74
227 #expect  118
228 #expect  -10
229 #expect  14
230 #expect  -114
231 #expect  78
232 #expect  -50
233 #expect  46
234 #expect  -82
235 #expect  110
236 #expect  -18
237 #expect  30
238 #expect  -98
239 #expect  94
240 #expect  -34
241 #expect  62
242 #expect  -66
243 #expect  126
244 #expect  -2
245 #expect  1
246 #expect  -127
247 #expect  65
248 #expect  -63
249 #expect  33
250 #expect  -95
251 #expect  97
252 #expect  -31
253 #expect  17
254 #expect  -111
255 #expect  81
256 #expect  -47
257 #expect  49
258 #expect  -79
259 #expect  113
260 #expect  -15
261 #expect  9
262 #expect  -119
263 #expect  73
264 #expect  -55
265 #expect  41
266 #expect  -87
267 #expect  105
268 #expect  -23
269 #expect  25
270 #expect  -103
271 #expect  89
272 #expect  -39
273 #expect  57
274 #expect  -71
275 #expect  121
276 #expect  -7
277 #expect  5
278 #expect  -123
279 #expect  69
280 #expect  -59
281 #expect  37
282 #expect  -91
283 #expect  101
284 #expect  -27
285 #expect  21
286 #expect  -107
287 #expect  85
288 #expect  -43
289 #expect  53
290 #expect  -75
291 #expect  117
292 #expect  -11
293 #expect  13
294 #expect  -115
295 #expect  77
296 #expect  -51
297 #expect  45
298 #expect  -83
299 #expect  109
300 #expect  -19
301 #expect  29
302 #expect  -99
303 #expect  93
304 #expect  -35
305 #expect  61
306 #expect  -67
307 #expect  125
308 #expect  -3
309 #expect  3
310 #expect  -125
311 #expect  67
312 #expect  -61
313 #expect  35
314 #expect  -93
315 #expect  99
316 #expect  -29
317 #expect  19
318 #expect  -109
319 #expect  83
320 #expect  -45
321 #expect  51
322 #expect  -77
323 #expect  115
324 #expect  -13
325 #expect  11
326 #expect  -117
327 #expect  75
328 #expect  -53
329 #expect  43
330 #expect  -85
331 #expect  107
332 #expect  -21
333 #expect  27
334 #expect  -101
335 #expect  91
336 #expect  -37
337 #expect  59
338 #expect  -69
339 #expect  123
340 #expect  -5
341 #expect  7
342 #expect  -121
343 #expect  71
344 #expect  -57
345 #expect  39
346 #expect  -89
347 #expect  103
348 #expect  -25
349 #expect  23
350 #expect  -105
351 #expect  87
352 #expect  -41
353 #expect  55
354 #expect  -73
355 #expect  119
356 #expect  -9
357 #expect  15
358 #expect  -113
359 #expect  79
360 #expect  -49
361 #expect  47
362 #expect  -81
363 #expect  111
364 #expect  -17
365 #expect  31
366 #expect  -97
367 #expect  95
368 #expect  -33
369 #expect  63
370 #expect  -65
371 #expect  127
372 #expect  -1
373
374 #ship debug        : Debug
375 #ship lut          : Lut3
376 #ship alu          : Alu1
377
378 lut.in1:   literal 85; [*] deliver;
379 lut.in2:   literal 51; [*] deliver;
380 lut.in3:   literal 15; [*] deliver;
381 lut.out:   [*] take, sendto debug.in;
382
383 // cycle through truth tables using alu as INC
384 alu.inOp:
385    literal 1;
386    load repeat counter with 63;
387    deliver;
388    load repeat counter with 63;
389    deliver;
390    load repeat counter with 63;
391    deliver;
392    load repeat counter with 63;
393    deliver;
394    load repeat counter with 3;
395    deliver;
396 alu.in:
397    literal 0;
398    deliver;
399    [*] take, deliver;
400 alu.out:
401   clog;
402   load loop counter with 2;
403   wait, take, sendto lut.inLut;
404   sendto alu.in;
405   unclog;
406
407 // acks from debug ship trigger new truth tables
408 debug.in:
409   [*] take, deliver, notify alu.out;
410
411 lut.inLut:
412   literal 0;
413   deliver;
414   [*] take, deliver;
415
416
417
418
419 == Contributors =========================================================
420 Adam Megacz <megacz@cs.berkeley.edu>