adjust ships to use fill/drain/full/empty macros
[fleet.git] / ships / Alu.ship
1 ship: Alu
2
3 == Ports ===========================================================
4 data  in:   in1
5 data  in:   in2
6 data  in:   inOp
7   constant IN1: 0
8   constant IN2: 1
9   constant ADD: 2
10   constant SUB: 3
11   constant MAX: 4
12   constant MIN: 5
13   constant CMP: 6
14   constant DROP1: 7
15   constant DROP2: 8
16   constant MAXMERGE: 9
17
18 data  out:  out
19
20
21 == TeX ==============================================================
22
23 {\tt Alu} is a ``two-input'' arithmetic logic unit.  It includes
24 logic for performing arithmetic operations on a pair of arguments.
25 Currently this includes
26 addition ({\sc add}),
27 subtraction ({\sc sub}),
28 maximum ({\sc max}), and
29 minimum ({\sc min}).
30
31 \subsection*{Semantics}
32
33 When a value is present at each of {\tt in1}, {\tt in2} and {\tt
34 inOp}, these three values are consumed.  Based on the value consumed
35 at {\tt inOp}, the requested operation is performed on the values
36 consumed from {\tt in1} and {\tt in2}.  The result of this operation
37 is then made available at {\tt out}.
38
39 \subsection*{C-Flag}
40
41 \begin{verbatim}
42 IN1      - undefined; drain in1 only
43 IN2      - undefined; drain in2 only
44 ADD      - carry-out
45 SUB      - undefined
46 MAX      - if in1>in2 cflag=0 and drain in1, else cflag=1 and drain in2
47 MIN      - if in1>in2 cflag=1 and drain in2, else cflag=0 and drain in1
48 CMP      - if in1==in2 cflag=1, else cflag=0
49 DROP1    - consume in1, produce no output
50 DROP2    - consume in2, produce no output
51 MAXMERGE - if (in1<0 && in2<0) consume both, emit either, cflag=undef else act as MAX
52 \end{verbatim}
53
54 \subsection*{To Do}
55
56 The {\it link bit} and other features of \cite{ies31} are not yet
57 implemented.
58
59 The carry-in, carry-out, zero-test, negative-test, and overflow-test
60 flags typically present in a conventional processor ALU are also not
61 yet implemented.
62
63 == Fleeterpreter ====================================================
64 public void service() {
65   if (box_inOp.dataReadyForShip() &&
66       box_in1.dataReadyForShip() &&
67       box_in2.dataReadyForShip() &&
68       box_out.readyForDataFromShip()) {
69       long a;
70       long b;
71       long op     = box_inOp.removeDataForShip();
72       switch((int)op) {
73           case 0:
74               a = box_in1.removeDataForShip();
75               box_out.addDataFromShip(a); // IN1
76               break;
77           case 1:
78               b = box_in2.removeDataForShip();
79               box_out.addDataFromShip(b); // IN2
80               break;
81           case 2:
82               a = box_in1.removeDataForShip();
83               b = box_in2.removeDataForShip();
84               box_out.addDataFromShip(a+b); // ADD
85               break;
86           case 3:
87               a = box_in1.removeDataForShip();
88               b = box_in2.removeDataForShip();
89               box_out.addDataFromShip(a-b); // SUB
90               break;
91           case 4:
92               a = box_in1.peekDataForShip();
93               b = box_in2.peekDataForShip();
94               box_out.addDataFromShip(Math.max(a,b)); // MAX
95               box_out.flag_c = !(a>b);
96               if (a<b) box_in1.removeDataForShip(); else box_in2.removeDataForShip();
97               break;
98           case 5:
99               a = box_in1.peekDataForShip();
100               b = box_in2.peekDataForShip();
101               box_out.addDataFromShip(Math.min(a,b)); // MIN
102               box_out.flag_c = a>b;
103               if (a>b) box_in1.removeDataForShip(); else box_in2.removeDataForShip();
104               break;
105           case 6:
106               a = box_in1.removeDataForShip();
107               b = box_in2.removeDataForShip();
108               box_out.addDataFromShip(0); // CMP
109               box_out.flag_c = a==b;
110               break;
111           default:
112               a = box_in1.removeDataForShip();
113               b = box_in2.removeDataForShip();
114               box_out.addDataFromShip(0);
115               break;
116       }
117   }
118 }
119
120 == FleetSim ==============================================================
121
122 == FPGA ==============================================================
123
124   wire [`DATAWIDTH:0]     sum;
125   wire                    cin;
126   wire [(`DATAWIDTH-1):0] in2_inverted;
127
128   wire [(`DATAWIDTH-1):0] res;
129   wire                    isplus;
130   wire                    eq;
131   wire                    cout;
132
133   assign isplus        = inOp_d[2:0]==2;
134   assign cin           = isplus ? 0 : 1;
135   assign in2_inverted  = isplus ? in2_d : ~in2_d;
136   assign sum           = {in1_d,cin} + {in2_inverted,cin};
137   assign res           = sum[`DATAWIDTH:1];
138   assign greater       = !res[`DATAWIDTH-1];
139   assign both_negative = in1_d[`DATAWIDTH-1] && in2_d[`DATAWIDTH-1];
140   assign eq            = in1_d == in2_d;
141   assign cout          = sum[`DATAWIDTH];
142
143   assign out_d_[`DATAWIDTH] =
144           (inOp_d==0) ? 1'b0 :
145           (inOp_d==1) ? 1'b0 :
146           (inOp_d==2) ? cout :
147           (inOp_d==3) ? cout :
148           (inOp_d==4) ? ~greater :
149           (inOp_d==5) ?  greater :
150           (inOp_d==6) ?  eq :
151           (inOp_d==9) ? ~greater :
152           0;
153
154   assign out_d_[(`DATAWIDTH-1):0] =
155           (inOp_d==0) ? (in1_d)  :
156           (inOp_d==1) ? (in2_d)  :
157           (inOp_d==2) ? (res)  :
158           (inOp_d==3) ? (res)  :
159           (inOp_d==4) ? (greater ? in1_d : in2_d)  :
160           (inOp_d==5) ? (greater ? in2_d : in1_d)  :
161           (inOp_d==6) ? {{ (`DATAWIDTH-1) {1'b0 }}, eq  } :
162           (inOp_d==9) ? (both_negative ? in1_d : (greater ? in1_d : in2_d)) :
163           0;
164
165   always @(posedge clk) begin
166     if (!rst) begin
167       `reset
168     end else begin
169       `flush
170       `cleanup
171       if (`out_draining) begin
172         `drain_inOp
173         if      (inOp_d==0) `drain_in1
174         else if (inOp_d==1) `drain_in2
175         else if (inOp_d==9 &&  both_negative) begin `drain_in1 `drain_in2 end
176         else if (inOp_d==4 &&  greater) `drain_in1
177         else if (inOp_d==5 &&  greater) `drain_in2
178         else if (inOp_d==9 &&  greater) `drain_in1
179         else if (inOp_d==4 && !greater) `drain_in2
180         else if (inOp_d==5 && !greater) `drain_in1
181         else if (inOp_d==9 && !greater) `drain_in2
182         else begin
183           `drain_in1
184           `drain_in2
185         end
186       end
187       if (`out_empty && `in1_full && `in2_full && `inOp_full) begin
188         `fill_out
189       end
190     end
191   end
192
193 == Test ==============================================================================
194
195 // FIXME: need test for ADD carry-out c-flag
196
197 #ship debug : Debug
198 #ship alu   : Alu
199
200 #expect 17
201 #expect 1
202 #expect 9
203 #expect 8
204 #expect 8
205 #expect 1
206 #expect 9
207 #expect 0
208 #expect 0
209 #expect 1
210
211 debug.in:   set ilc=*;  recv, deliver;
212 alu.in1:
213   set word= 9;
214   set ilc=5;
215   deliver;
216   set word= 9;
217   deliver;
218
219 alu.in2:
220   set word= 8;
221   set ilc=5;
222   deliver;
223   set word= 9;
224   deliver;
225
226 alu.out:
227   set ilc=4;
228   collect, send to debug.in;
229 alu.inOp:
230  set word= Alu.inOp[ADD]; deliver;
231  set word= Alu.inOp[SUB]; deliver;
232  set word= Alu.inOp[IN1]; deliver;
233  set word= Alu.inOp[IN2]; deliver;
234  set word= Alu.inOp[MIN]; deliver;
235  set word= Alu.inOp[MAX]; deliver;
236  set word= Alu.inOp[CMP]; deliver;
237  set word= Alu.inOp[CMP]; deliver;
238
239 alu.out:
240   collect, send to debug.in;        // MIN
241   set flags a=c, b=b;
242   [a]  set word= 1;
243   [!a] set word= 0;
244   send to debug.in;
245
246   collect, send to debug.in;        // MAX
247   set flags a=c, b=b;
248   [a]  set word= 1;
249   [!a] set word= 0;
250   send to debug.in;
251
252   collect;        // CMP
253   set flags a=c, b=b;
254   [a]  set word= 1;
255   [!a] set word= 0;
256   send to debug.in;
257
258   collect;        // CMP
259   set flags a=c, b=b;
260   [a]  set word= 1;
261   [!a] set word= 0;
262   send to debug.in;
263
264 == Contributors =========================================================
265 Adam Megacz <megacz@cs.berkeley.edu>