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