convert many more test cases
[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 This ship is a two-input arithmetic unit.  It features several
17 opcodes, such as {\tt ADD} and {\tt SUB}.  In my opinion, it is
18 niftycool.
19
20 FIXME: implement all the link bit stuff
21
22 Use carry-in bit to create a selector?  Perhaps a waste of an ALU.
23
24 Flags: zero, negative, overflow, ?
25
26 \begin{verbatim}
27 move elsewhere:
28 //MUL:
29 //DIV:
30 //MOD:
31 \end{verbatim}
32
33 == Fleeterpreter ====================================================
34 public long resolveLiteral(String literal) {
35   if (literal.equals("ADD")) return 0;
36   if (literal.equals("SUB")) return 1;
37   if (literal.equals("MAX")) return 2;
38   if (literal.equals("MIN")) return 3;
39   return super.resolveLiteral(literal);
40 }
41 public void service() {
42   if (box_in1.dataReadyForShip() &&
43       box_in2.dataReadyForShip() &&
44       box_inOp.dataReadyForShip() &&
45       box_out.readyForDataFromShip()) {
46       long a      = box_in1.removeDataForShip();
47       long b      = box_in2.removeDataForShip();
48       long op     = box_inOp.removeDataForShip();
49       switch((int)op) {
50           case 0: box_out.addDataFromShip(a+b); // ADD
51               break;
52           case 1: box_out.addDataFromShip(a-b); // SUB
53               break;
54           case 2: box_out.addDataFromShip(Math.max(a,b)); // MAX
55               break;
56           case 3: box_out.addDataFromShip(Math.min(a,b)); // MIN
57               break;
58           default: box_out.addDataFromShip(0);
59               break;
60       }
61   }
62 }
63
64 == FleetSim ==============================================================
65
66 == FPGA ==============================================================
67
68   reg                    have_a;
69   reg [(`DATAWIDTH-1):0] reg_a;
70   reg                    have_b;
71   reg [(`DATAWIDTH-1):0] reg_b;
72   reg                    have_op;
73   reg [(`DATAWIDTH-1):0] reg_op;
74
75   always @(posedge clk) begin
76     if (!have_a) begin
77       `onread(in1_r, in1_a) have_a = 1; reg_a = in1_d; end
78       end
79     if (!have_b) begin
80       `onread(in2_r, in2_a) have_b = 1; reg_b = in2_d; end
81       end
82     if (!have_op) begin
83       `onread(inOp_r, inOp_a) have_op = 1; reg_op = inOp_d; end
84       end
85   
86     if (have_a && have_b && have_op) begin
87       case (reg_op)
88         0: out_d = reg_a + reg_b;
89         1: out_d = reg_a - reg_b;
90         2: out_d = reg_a > reg_b ? reg_a : reg_b;
91         3: out_d = reg_a > reg_b ? reg_b : reg_a;
92         default: out_d = 0;
93       endcase        
94       `onwrite(out_r, out_a)
95         have_a  = 0;
96         have_b  = 0;
97         have_op = 0;
98       end
99     end
100   end
101
102 == Test ==============================================================================
103 // expected output
104 #ship debug : Debug
105 #ship alu   : Alu2
106
107 #expect 17
108 #expect 1
109 #expect 8
110 #expect 9
111
112 debug.in:   [*] take, deliver;
113 alu.in1:
114   literal 9; [4] deliver;
115
116 alu.in2:
117   literal 8; [4] deliver;
118
119 alu.in1:    [*] take, deliver;
120 alu.in2:    [*] take, deliver;
121 alu.out:    [*] take, sendto debug.in;
122
123 alu.inOp:
124  literal Alu2.inOp[ADD]; deliver;
125  literal Alu2.inOp[SUB]; deliver;
126  literal Alu2.inOp[MIN]; deliver;
127  literal Alu2.inOp[MAX]; deliver;
128
129
130
131 == Contributors =========================================================
132 Adam Megacz <megacz@cs.berkeley.edu>