cleanup on Alu2
[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 REM:
14 MAX:
15 MIN:
16 SORT: output min(in1,in2) followed by max(in1,in2) (FIXME: redundant?)
17
18 == TeX ==============================================================
19 This ship is a two-input arithmetic unit.  It features several
20 opcodes, such as {\tt ADD} and {\tt SUB}.  In my opinion, it is
21 niftycool.
22
23 FIXME: implement all the link bit stuff
24
25 Use carry-in bit to create a selector?  Perhaps a waste of an ALU.
26
27 Carry-save / carry completion stuff.
28
29 Flags: zero, negative, overflow, ?
30
31 move elsewhere:
32 //MUL:
33 //DIV:
34 //MOD:
35
36 == Fleeterpreter ====================================================
37 public void service() {
38   if (box_in1.dataReadyForShip() &&
39       box_in2.dataReadyForShip() &&
40       box_inOp.dataReadyForShip() &&
41       box_out.readyForItemFromShip()) {
42       int a      = box_in1.removeDataForShip();
43       int b      = box_in2.removeDataForShip();
44       int op     = box_inOp.removeDataForShip();
45       switch(op) {
46           case 0: box_out.addDataFromShip(a+b); // ADD
47               break;
48           case 1: box_out.addDataFromShip(a-b); // SUB
49               break;
50           default: box_out.addDataFromShip(0);
51               break;
52       }
53   }
54 }
55
56 == FleetSim ==============================================================
57
58 == FPGA ==============================================================
59
60   input  clk;
61   `input(in1_r,    in1_a,    in1_a_,  [(`DATAWIDTH-1):0], in1_d)
62   `input(in2_r,    in2_a,    in2_a_,  [(`DATAWIDTH-1):0], in2_d)
63   `input(inOp_r,   inOp_a,   inOp_a_, [(`DATAWIDTH-1):0], inOp_d)
64   `output(out_r, out_r_, out_a, [(`DATAWIDTH-1):0], out_d_)
65
66   `defreg(out_d_, [(`DATAWIDTH-1):0], out_d)
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; // will not synthesize --AM
91         //3: out_d = reg_a / reg_b; // will not synthesize --AM
92         //4: out_d = reg_a % reg_b; // will not synthesize --AM
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 endmodule
104
105
106
107 == Contributors =========================================================
108 Adam Megacz <megacz@cs.berkeley.edu>