migrate verilog into ship files
[fleet.git] / ships / Alu2.ship
1 ship: Alu2
2
3 == Ports ===========================================================
4 data  in:   a
5 data  in:   b
6 data  in:   op
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
14 == TeX ==============================================================
15 This ship is a two-input arithmetic unit.  It features several
16 opcodes, such as {\tt ADD} and {\tt SUB}.  In my opinion, it is
17 niftycool.
18
19 == Fleeterpreter ====================================================
20 public void service() {
21   if (box_a.dataReadyForShip() &&
22       box_b.dataReadyForShip() &&
23       box_op.dataReadyForShip() &&
24       box_out.readyForItemFromShip()) {
25       int a      = box_a.removeDataForShip();
26       int b      = box_b.removeDataForShip();
27       int op     = box_op.removeDataForShip();
28       switch(op) {
29           case 0: box_out.addDataFromShip(a+b); // ADD
30               break;
31           case 1: box_out.addDataFromShip(a-b); // SUB
32               break;
33           case 2: box_out.addDataFromShip(a*b); // MUL
34               break;
35           case 3: box_out.addDataFromShip(a/b); // DIV
36               break;
37           case 4: box_out.addDataFromShip(a%b); // REM
38               break;
39           default: box_out.addDataFromShip(0);
40               break;
41       }
42   }
43 }
44
45 == ArchSim ==============================================================
46
47 == FPGA ==============================================================
48 `include "macros.v"
49
50 module alu2 (clk, 
51              a_r,    a_a_,  a_d,
52              b_r,    b_a_,  b_d,
53              op_r,   op_a_, op_d,
54              out_r_, out_a, out_d_);
55
56   input  clk;
57   `input(a_r,    a_a,    a_a_,  [(`DATAWIDTH-1):0], a_d)
58   `input(b_r,    b_a,    b_a_,  [(`DATAWIDTH-1):0], b_d)
59   `input(op_r,   op_a,   op_a_, [(`DATAWIDTH-1):0], op_d)
60   `output(out_r, out_r_, out_a, [(`DATAWIDTH-1):0], out_d_)
61   `defreg(out_d_, [(`DATAWIDTH-1):0], out_d)
62
63   reg                    have_a;
64   reg [(`DATAWIDTH-1):0] reg_a;
65   reg                    have_b;
66   reg [(`DATAWIDTH-1):0] reg_b;
67   reg                    have_op;
68   reg [(`DATAWIDTH-1):0] reg_op;
69
70   always @(posedge clk) begin
71     if (!have_a) begin
72       `onread(a_r, a_a) have_a = 1; reg_a = a_d; end
73       end
74     if (!have_b) begin
75       `onread(b_r, b_a) have_b = 1; reg_b = b_d; end
76       end
77     if (!have_op) begin
78       `onread(op_r, op_a) have_op = 1; reg_op = op_d; end
79       end
80   
81     if (have_a && have_b && have_op) begin
82       case (reg_op)
83         0: out_d = reg_a + reg_b;
84         1: out_d = reg_a - reg_b;
85         //2: out_d = reg_a * reg_b; // will not synthesize --AM
86         //3: out_d = reg_a / reg_b; // will not synthesize --AM
87         //4: out_d = reg_a % reg_b; // will not synthesize --AM
88         default: out_d = 0;
89       endcase        
90       `onwrite(out_r, out_a)
91         have_a  = 0;
92         have_b  = 0;
93         have_op = 0;
94       end
95     end
96   end
97
98 endmodule
99
100
101
102 == Contributors =========================================================
103 Adam Megacz <megacz@cs.berkeley.edu>