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