added comments to ship files
[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 MUL:
14 DIV:
15 MOD:
16 REM:
17 MAX:
18 MIN:
19 SORT: output min(in1,in2) followed by max(in1,in2) (FIXME: redundant?)
20
21 == TeX ==============================================================
22 This ship is a two-input arithmetic unit.  It features several
23 opcodes, such as {\tt ADD} and {\tt SUB}.  In my opinion, it is
24 niftycool.
25
26 FIXME: implement all the link bit stuff
27
28 Use carry-in bit to create a selector?  Perhaps a waste of an ALU.
29
30 Carry-save / carry completion stuff.
31
32 Flags: zero, negative, overflow, ?
33
34 == Fleeterpreter ====================================================
35 public void service() {
36   if (box_in1.dataReadyForShip() &&
37       box_in2.dataReadyForShip() &&
38       box_inOp.dataReadyForShip() &&
39       box_out.readyForItemFromShip()) {
40       int a      = box_in1.removeDataForShip();
41       int b      = box_in2.removeDataForShip();
42       int op     = box_inOp.removeDataForShip();
43       switch(op) {
44           case 0: box_out.addDataFromShip(a+b); // ADD
45               break;
46           case 1: box_out.addDataFromShip(a-b); // SUB
47               break;
48           case 2: box_out.addDataFromShip(a*b); // MUL
49               break;
50           case 3: box_out.addDataFromShip(a/b); // DIV
51               break;
52           case 4: box_out.addDataFromShip(a%b); // REM
53               break;
54           default: box_out.addDataFromShip(0);
55               break;
56       }
57   }
58 }
59
60 == FleetSim ==============================================================
61
62 == FPGA ==============================================================
63 `include "macros.v"
64
65 module alu2 (clk, 
66              a_r,    a_a_,  a_d,
67              b_r,    b_a_,  b_d,
68              op_r,   op_a_, op_d,
69              out_r_, out_a, out_d_);
70
71   input  clk;
72   `input(a_r,    a_a,    a_a_,  [(`DATAWIDTH-1):0], a_d)
73   `input(b_r,    b_a,    b_a_,  [(`DATAWIDTH-1):0], b_d)
74   `input(op_r,   op_a,   op_a_, [(`DATAWIDTH-1):0], op_d)
75   `output(out_r, out_r_, out_a, [(`DATAWIDTH-1):0], out_d_)
76   `defreg(out_d_, [(`DATAWIDTH-1):0], out_d)
77
78   reg                    have_a;
79   reg [(`DATAWIDTH-1):0] reg_a;
80   reg                    have_b;
81   reg [(`DATAWIDTH-1):0] reg_b;
82   reg                    have_op;
83   reg [(`DATAWIDTH-1):0] reg_op;
84
85   always @(posedge clk) begin
86     if (!have_a) begin
87       `onread(a_r, a_a) have_a = 1; reg_a = a_d; end
88       end
89     if (!have_b) begin
90       `onread(b_r, b_a) have_b = 1; reg_b = b_d; end
91       end
92     if (!have_op) begin
93       `onread(op_r, op_a) have_op = 1; reg_op = op_d; end
94       end
95   
96     if (have_a && have_b && have_op) begin
97       case (reg_op)
98         0: out_d = reg_a + reg_b;
99         1: out_d = reg_a - reg_b;
100         //2: out_d = reg_a * reg_b; // will not synthesize --AM
101         //3: out_d = reg_a / reg_b; // will not synthesize --AM
102         //4: out_d = reg_a % reg_b; // will not synthesize --AM
103         default: out_d = 0;
104       endcase        
105       `onwrite(out_r, out_a)
106         have_a  = 0;
107         have_b  = 0;
108         have_op = 0;
109       end
110     end
111   end
112
113 endmodule
114
115
116
117 == Contributors =========================================================
118 Adam Megacz <megacz@cs.berkeley.edu>