autogenerate parts of Alu1, Alu2, Lut3 verilog
[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 ABS:
11 NEG:
12 INV:
13 INC:
14 DEC:
15
16 == TeX ==============================================================
17 == Fleeterpreter ====================================================
18     public void service() {
19         if (box_in.dataReadyForShip() && box_inOp.dataReadyForShip()) {
20             int data   = box_in.removeDataForShip();
21             int opcode = box_inOp.removeDataForShip();
22             switch(opcode) {
23                 case 0: box_out.addDataFromShip(-1 * data);      // NEG
24                     break;
25                 case 1: box_out.addDataFromShip(data+1);         // INC
26                     break;
27                 case 2: box_out.addDataFromShip(data-1);         // DEC
28                     break;
29                 case 3: box_out.addDataFromShip(Math.abs(data)); // ABS
30                     break;
31                 default: box_out.addDataFromShip(0);
32                     break;
33             }
34         }
35     }
36
37 == FleetSim ==============================================================
38 == FPGA ==============================================================
39   reg                    have_a;
40   reg [(`DATAWIDTH-1):0] reg_a;
41   reg                    have_op;
42   reg [(`DATAWIDTH-1):0] reg_op;
43
44   always @(posedge clk) begin
45     if (!have_a) begin
46       `onread(in_r, in_a) have_a = 1; reg_a = in_d; end
47       end
48     if (!have_op) begin
49       `onread(inOp_r, inOp_a) have_op = 1; reg_op = inOp_d; end
50       end
51   
52     if (have_a && have_op) begin
53       case (reg_op)
54         0: out_d = -reg_a;
55         1: out_d = reg_a+1;
56         2: out_d = reg_a-1;
57         3: out_d = (reg_a<0) ? (-reg_a) : reg_a;
58         4: out_d = 37'b1111111111111111111111111111111111111;
59         default: out_d = 0;
60       endcase        
61       `onwrite(out_r, out_a)
62         have_a  = 0;
63         have_op = 0;
64       end
65     end
66   end
67
68
69 == Contributors =========================================================
70 Adam Megacz <megacz@cs.berkeley.edu>