replace DATAWIDTH with WORDWIDTH
[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   wire [7:0]              lut;
66
67   genvar i;
68   generate
69     for(i=0; i<`WORDWIDTH; i=i+1) begin : OUT
70       assign out_d_[i] = lut[{in3_d[i], in2_d[i], in1_d[i]}];
71     end
72   endgenerate
73
74   assign lut = inLut_d[7:0];
75
76   always @(posedge clk) begin
77     if (!rst) begin
78       `reset
79     end else begin
80       `flush
81       `cleanup
82       if (`out_draining) begin
83          `drain_in1
84          `drain_in2
85          `drain_in3
86          `drain_inLut
87       end
88       if (`in1_full && `in2_full && `in3_full && `inLut_full && `out_empty) begin
89          `fill_out
90       end
91     end
92   end
93
94
95 == Test =================================================================
96 #expect  0
97 #expect  -128
98 #expect  64
99 #expect  -64
100 #expect  32
101 #expect  -96
102 #expect  96
103 #expect  -32
104 #expect  16
105 #expect  -112
106 #expect  80
107 #expect  -48
108 #expect  48
109 #expect  -80
110 #expect  112
111 #expect  -16
112 #expect  8
113 #expect  -120
114 #expect  72
115 #expect  -56
116 #expect  40
117 #expect  -88
118 #expect  104
119 #expect  -24
120 #expect  24
121 #expect  -104
122 #expect  88
123 #expect  -40
124 #expect  56
125 #expect  -72
126 #expect  120
127 #expect  -8
128 #expect  4
129 #expect  -124
130 #expect  68
131 #expect  -60
132 #expect  36
133 #expect  -92
134 #expect  100
135 #expect  -28
136 #expect  20
137 #expect  -108
138 #expect  84
139 #expect  -44
140 #expect  52
141 #expect  -76
142 #expect  116
143 #expect  -12
144 #expect  12
145 #expect  -116
146 #expect  76
147 #expect  -52
148 #expect  44
149 #expect  -84
150 #expect  108
151 #expect  -20
152 #expect  28
153 #expect  -100
154 #expect  92
155 #expect  -36
156 #expect  60
157 #expect  -68
158 #expect  124
159 #expect  -4
160 #expect  2
161 #expect  -126
162 #expect  66
163 #expect  -62
164 #expect  34
165 #expect  -94
166 #expect  98
167 #expect  -30
168 #expect  18
169 #expect  -110
170 #expect  82
171 #expect  -46
172 #expect  50
173 #expect  -78
174 #expect  114
175 #expect  -14
176 #expect  10
177 #expect  -118
178 #expect  74
179 #expect  -54
180 #expect  42
181 #expect  -86
182 #expect  106
183 #expect  -22
184 #expect  26
185 #expect  -102
186 #expect  90
187 #expect  -38
188 #expect  58
189 #expect  -70
190 #expect  122
191 #expect  -6
192 #expect  6
193 #expect  -122
194 #expect  70
195 #expect  -58
196 #expect  38
197 #expect  -90
198 #expect  102
199 #expect  -26
200 #expect  22
201 #expect  -106
202 #expect  86
203 #expect  -42
204 #expect  54
205 #expect  -74
206 #expect  118
207 #expect  -10
208 #expect  14
209 #expect  -114
210 #expect  78
211 #expect  -50
212 #expect  46
213 #expect  -82
214 #expect  110
215 #expect  -18
216 #expect  30
217 #expect  -98
218 #expect  94
219 #expect  -34
220 #expect  62
221 #expect  -66
222 #expect  126
223 #expect  -2
224 #expect  1
225 #expect  -127
226 #expect  65
227 #expect  -63
228 #expect  33
229 #expect  -95
230 #expect  97
231 #expect  -31
232 #expect  17
233 #expect  -111
234 #expect  81
235 #expect  -47
236 #expect  49
237 #expect  -79
238 #expect  113
239 #expect  -15
240 #expect  9
241 #expect  -119
242 #expect  73
243 #expect  -55
244 #expect  41
245 #expect  -87
246 #expect  105
247 #expect  -23
248 #expect  25
249 #expect  -103
250 #expect  89
251 #expect  -39
252 #expect  57
253 #expect  -71
254 #expect  121
255 #expect  -7
256 #expect  5
257 #expect  -123
258 #expect  69
259 #expect  -59
260 #expect  37
261 #expect  -91
262 #expect  101
263 #expect  -27
264 #expect  21
265 #expect  -107
266 #expect  85
267 #expect  -43
268 #expect  53
269 #expect  -75
270 #expect  117
271 #expect  -11
272 #expect  13
273 #expect  -115
274 #expect  77
275 #expect  -51
276 #expect  45
277 #expect  -83
278 #expect  109
279 #expect  -19
280 #expect  29
281 #expect  -99
282 #expect  93
283 #expect  -35
284 #expect  61
285 #expect  -67
286 #expect  125
287 #expect  -3
288 #expect  3
289 #expect  -125
290 #expect  67
291 #expect  -61
292 #expect  35
293 #expect  -93
294 #expect  99
295 #expect  -29
296 #expect  19
297 #expect  -109
298 #expect  83
299 #expect  -45
300 #expect  51
301 #expect  -77
302 #expect  115
303 #expect  -13
304 #expect  11
305 #expect  -117
306 #expect  75
307 #expect  -53
308 #expect  43
309 #expect  -85
310 #expect  107
311 #expect  -21
312 #expect  27
313 #expect  -101
314 #expect  91
315 #expect  -37
316 #expect  59
317 #expect  -69
318 #expect  123
319 #expect  -5
320 #expect  7
321 #expect  -121
322 #expect  71
323 #expect  -57
324 #expect  39
325 #expect  -89
326 #expect  103
327 #expect  -25
328 #expect  23
329 #expect  -105
330 #expect  87
331 #expect  -41
332 #expect  55
333 #expect  -73
334 #expect  119
335 #expect  -9
336 #expect  15
337 #expect  -113
338 #expect  79
339 #expect  -49
340 #expect  47
341 #expect  -81
342 #expect  111
343 #expect  -17
344 #expect  31
345 #expect  -97
346 #expect  95
347 #expect  -33
348 #expect  63
349 #expect  -65
350 #expect  127
351 #expect  -1
352
353 #ship debug        : Debug
354 #ship lut          : Lut3
355 #ship alu          : Alu
356
357 lut.in1:   set word= 85; set ilc=*;  deliver;
358 lut.in2:   set word= 51; set ilc=*;  deliver;
359 lut.in3:   set word= 15; set ilc=*;  deliver;
360 lut.out:   set ilc=*;  collect, send to debug.in;
361
362 // cycle through truth tables using alu as INC
363 alu.in2:
364    set word= 1;
365    set ilc=*;  deliver;
366 alu.inOp:
367    set word= Alu.inOp[ADD];
368    set ilc=*;  deliver;
369 alu.in1:
370    set word= 0;
371    deliver;
372    set ilc=*;  recv, deliver;
373 alu.out:
374   set olc=2;
375   [Rq] recv token, collect, send to lut.inLut;
376   [Rq] send to alu.in1;
377   tail;
378
379 lut.inLut:
380   set word= 0;
381   deliver;
382   set ilc=*;  recv, deliver;
383
384 // acks from debug ship trigger new truth tables
385 debug.in:
386   set ilc=63;
387   recv, deliver, send token to alu.out;
388   set ilc=63;
389   recv, deliver, send token to alu.out;
390   set ilc=63;
391   recv, deliver, send token to alu.out;
392   set ilc=63;
393   recv, deliver, send token to alu.out;
394   set ilc=4;
395   recv, deliver, send token to alu.out;
396
397
398
399 == Contributors =========================================================
400 Adam Megacz <megacz@cs.berkeley.edu>