alter Alu1 test to work around fpga anomoly
[fleet.git] / ships / Alu2.ship
1 ship: Alu2
2
3 == Ports ===========================================================
4 data  in:   in1
5 data  in:   in2
6 data  in:   inOp
7   constant ADD: 0
8   constant SUB: 1
9   constant MAX: 2
10   constant MIN: 3
11
12 data  out:  out
13
14
15 == TeX ==============================================================
16
17 {\tt Alu2} is a ``two-input'' arithmetic logic unit.  It includes
18 logic for performing arithmetic operations on a pair of arguments.
19 Currently this includes
20 addition ({\sc add}),
21 subtraction ({\sc sub}),
22 maximum ({\sc max}), and
23 minimum ({\sc min}).
24
25 \subsection*{Semantics}
26
27 When a value is present at each of {\tt in1}, {\tt in2} and {\tt
28 inOp}, these three values are consumed.  Based on the value consumed
29 at {\tt inOp}, the requested operation is performed on the values
30 consumed from {\tt in1} and {\tt in2}.  The result of this operation
31 is then made available at {\tt out}.
32
33 \subsection*{To Do}
34
35 The {\it link bit} and other features of \cite{ies31} are not yet
36 implemented.
37
38 The carry-in, carry-out, zero-test, negative-test, and overflow-test
39 flags typically present in a conventional processor ALU are also not
40 yet implemented.
41
42 == Fleeterpreter ====================================================
43 public long resolveLiteral(String literal) {
44   if (literal.equals("ADD")) return 0;
45   if (literal.equals("SUB")) return 1;
46   if (literal.equals("MAX")) return 2;
47   if (literal.equals("MIN")) return 3;
48   return super.resolveLiteral(literal);
49 }
50 public void service() {
51   if (box_in1.dataReadyForShip() &&
52       box_in2.dataReadyForShip() &&
53       box_inOp.dataReadyForShip() &&
54       box_out.readyForDataFromShip()) {
55       long a      = box_in1.removeDataForShip();
56       long b      = box_in2.removeDataForShip();
57       long op     = box_inOp.removeDataForShip();
58       switch((int)op) {
59           case 0: box_out.addDataFromShip(a+b); // ADD
60               break;
61           case 1: box_out.addDataFromShip(a-b); // SUB
62               break;
63           case 2: box_out.addDataFromShip(Math.max(a,b)); // MAX
64               break;
65           case 3: box_out.addDataFromShip(Math.min(a,b)); // MIN
66               break;
67           default: box_out.addDataFromShip(0);
68               break;
69       }
70   }
71 }
72
73 == FleetSim ==============================================================
74
75 == FPGA ==============================================================
76
77   reg                    have_a;
78   reg [(`DATAWIDTH-1):0] reg_a;
79   reg                    have_b;
80   reg [(`DATAWIDTH-1):0] reg_b;
81   reg                    have_op;
82   reg [(`DATAWIDTH-1):0] reg_op;
83
84   always @(posedge clk) begin
85     if (!rst) begin
86       have_a = 0;
87       have_b = 0;
88       have_op = 0;
89     end else begin
90     if (!have_a) begin
91       `onread(in1_r, in1_a) have_a = 1; reg_a = in1_d; end
92       end
93     if (!have_b) begin
94       `onread(in2_r, in2_a) have_b = 1; reg_b = in2_d; end
95       end
96     if (!have_op) begin
97       `onread(inOp_r, inOp_a) have_op = 1; reg_op = inOp_d; end
98       end
99   
100     if (have_a && have_b && have_op) begin
101       case (reg_op)
102         0: out_d = reg_a + reg_b;
103         1: out_d = reg_a - reg_b;
104         2: out_d = reg_a > reg_b ? reg_a : reg_b;
105         3: out_d = reg_a > reg_b ? reg_b : reg_a;
106         default: out_d = 0;
107       endcase        
108       `onwrite(out_r, out_a)
109         have_a  = 0;
110         have_b  = 0;
111         have_op = 0;
112       end
113     end
114   end
115   end
116
117 == Test ==============================================================================
118 // expected output
119 #ship debug : Debug
120 #ship alu   : Alu2
121
122 #expect 17
123 #expect 1
124 #expect 8
125 #expect 9
126
127 debug.in:   [*] take, deliver;
128 alu.in1:
129   literal 9; load repeat counter with 4; deliver;
130
131 alu.in2:
132   literal 8; load repeat counter with 4; deliver;
133
134 alu.in1:    [*] take, deliver;
135 alu.in2:    [*] take, deliver;
136 alu.out:    [*] take, sendto debug.in;
137
138 alu.inOp:
139  literal Alu2.inOp[ADD]; deliver;
140  literal Alu2.inOp[SUB]; deliver;
141  literal Alu2.inOp[MIN]; deliver;
142  literal Alu2.inOp[MAX]; deliver;
143
144
145
146 == Contributors =========================================================
147 Adam Megacz <megacz@cs.berkeley.edu>