migrate verilog into ship files
[fleet.git] / ships / Alu1.ship
1 ship: Alu1
2
3 == Ports ===========================================================
4 data  in:   in
5 data  in:   inOp
6
7 data  out:  out
8
9 == Constants ========================================================
10 == TeX ==============================================================
11 == Fleeterpreter ====================================================
12     public void service() {
13 /*
14         if (in.dataReadyForShip() && op.dataReadyForShip()) {
15             int data   = in.removeDataForShip();
16             int opcode = in.removeDataForShip();
17             switch(opcode) {
18                 case 0: out.addDataFromShip(-1 * data);      // NEG
19                     break;
20                 case 1: out.addDataFromShip(data+1);         // INC
21                     break;
22                 case 2: out.addDataFromShip(data-1);         // DEC
23                     break;
24                 case 3: out.addDataFromShip(Math.abs(data)); // ABS
25                     break;
26                 default: out.addDataFromShip(0);
27                     break;
28             }
29         }
30 */
31     }
32
33 == ArchSim ==============================================================
34 == FPGA ==============================================================
35 `include "macros.v"
36
37 module alu1 (clk, 
38              a_r,    a_a_,  a_d,
39              op_r,   op_a_, op_d,
40              out_r_, out_a, out_d_);
41
42   input  clk;
43   `input(a_r,    a_a,    a_a_,  [(`DATAWIDTH-1):0], a_d)
44   `input(op_r,   op_a,   op_a_, [(`DATAWIDTH-1):0], op_d)
45   `output(out_r, out_r_, out_a, [(`DATAWIDTH-1):0], out_d_)
46   `defreg(out_d_, [(`DATAWIDTH-1):0], out_d)
47
48   reg                    have_a;
49   reg [(`DATAWIDTH-1):0] reg_a;
50   reg                    have_op;
51   reg [(`DATAWIDTH-1):0] reg_op;
52
53   always @(posedge clk) begin
54     if (!have_a) begin
55       `onread(a_r, a_a) have_a = 1; reg_a = a_d; end
56       end
57     if (!have_op) begin
58       `onread(op_r, op_a) have_op = 1; reg_op = op_d; end
59       end
60   
61     if (have_a && have_op) begin
62       case (reg_op)
63         0: out_d = -reg_a;
64         1: out_d = reg_a+1;
65         2: out_d = reg_a-1;
66         3: out_d = (reg_a<0) ? (-reg_a) : reg_a;
67         default: out_d = 0;
68       endcase        
69       `onwrite(out_r, out_a)
70         have_a  = 0;
71         have_op = 0;
72       end
73     end
74   end
75
76 endmodule
77
78
79 == Contributors =========================================================
80 Adam Megacz <megacz@cs.berkeley.edu>