more bugfixes to Alu3
[fleet.git] / ships / Alu2.ship
1 ship: Alu2
2
3 == Ports ===========================================================
4 data  in:   in1
5 data  in:   in2
6 data  in:   inOp
7
8 data  out:  out
9
10 == Constants ========================================================
11 ADD: add the two arguments; treat link as carry
12 SUB: subtract the two arguments; treat link as carry
13 MAX:
14 MIN:
15
16 == TeX ==============================================================
17 This ship is a two-input arithmetic unit.  It features several
18 opcodes, such as {\tt ADD} and {\tt SUB}.  In my opinion, it is
19 niftycool.
20
21 FIXME: implement all the link bit stuff
22
23 Use carry-in bit to create a selector?  Perhaps a waste of an ALU.
24
25 Flags: zero, negative, overflow, ?
26
27 \begin{verbatim}
28 move elsewhere:
29 //MUL:
30 //DIV:
31 //MOD:
32 \end{verbatim}
33
34 == Fleeterpreter ====================================================
35 public long resolveLiteral(String literal) {
36   if (literal.equals("ADD")) return 0;
37   if (literal.equals("SUB")) return 1;
38   if (literal.equals("MAX")) return 2;
39   if (literal.equals("MIN")) return 3;
40   return super.resolveLiteral(literal);
41 }
42 public void service() {
43   if (box_in1.dataReadyForShip() &&
44       box_in2.dataReadyForShip() &&
45       box_inOp.dataReadyForShip() &&
46       box_out.readyForDataFromShip()) {
47       long a      = box_in1.removeDataForShip();
48       long b      = box_in2.removeDataForShip();
49       long op     = box_inOp.removeDataForShip();
50       switch((int)op) {
51           case 0: box_out.addDataFromShip(a+b); // ADD
52               break;
53           case 1: box_out.addDataFromShip(a-b); // SUB
54               break;
55           case 2: box_out.addDataFromShip(Math.max(a,b)); // MAX
56               break;
57           case 3: box_out.addDataFromShip(Math.min(a,b)); // MIN
58               break;
59           default: box_out.addDataFromShip(0);
60               break;
61       }
62   }
63 }
64
65 == FleetSim ==============================================================
66
67 == FPGA ==============================================================
68
69   reg                    have_a;
70   reg [(`DATAWIDTH-1):0] reg_a;
71   reg                    have_b;
72   reg [(`DATAWIDTH-1):0] reg_b;
73   reg                    have_op;
74   reg [(`DATAWIDTH-1):0] reg_op;
75
76   always @(posedge clk) begin
77     if (!have_a) begin
78       `onread(in1_r, in1_a) have_a = 1; reg_a = in1_d; end
79       end
80     if (!have_b) begin
81       `onread(in2_r, in2_a) have_b = 1; reg_b = in2_d; end
82       end
83     if (!have_op) begin
84       `onread(inOp_r, inOp_a) have_op = 1; reg_op = inOp_d; end
85       end
86   
87     if (have_a && have_b && have_op) begin
88       case (reg_op)
89         0: out_d = reg_a + reg_b;
90         1: out_d = reg_a - reg_b;
91         2: out_d = reg_a > reg_b ? reg_a : reg_b;
92         3: out_d = reg_a > reg_b ? reg_b : reg_a;
93         default: out_d = 0;
94       endcase        
95       `onwrite(out_r, out_a)
96         have_a  = 0;
97         have_b  = 0;
98         have_op = 0;
99       end
100     end
101   end
102
103
104
105
106 == Contributors =========================================================
107 Adam Megacz <megacz@cs.berkeley.edu>