Stack bugfixes
[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 MAX:
14 MIN:
15
16 == TeX ==============================================================
17 This ship is a two-input arithmetic unit.  It features several
18 opcodes, such as {\tt ADD} and {\tt SUB}.  In my opinion, it is
19 niftycool.
20
21 FIXME: implement all the link bit stuff
22
23 Use carry-in bit to create a selector?  Perhaps a waste of an ALU.
24
25 Flags: zero, negative, overflow, ?
26
27 move elsewhere:
28 //MUL:
29 //DIV:
30 //MOD:
31
32 == Fleeterpreter ====================================================
33 public long resolveLiteral(String literal) {
34   if (literal.equals("ADD")) return 0;
35   if (literal.equals("SUB")) return 1;
36   if (literal.equals("MAX")) return 2;
37   if (literal.equals("MIN")) return 3;
38   return super.resolveLiteral(literal);
39 }
40 public void service() {
41   if (box_in1.dataReadyForShip() &&
42       box_in2.dataReadyForShip() &&
43       box_inOp.dataReadyForShip() &&
44       box_out.readyForDataFromShip()) {
45       long a      = box_in1.removeDataForShip();
46       long b      = box_in2.removeDataForShip();
47       long op     = box_inOp.removeDataForShip();
48       switch((int)op) {
49           case 0: box_out.addDataFromShip(a+b); // ADD
50               break;
51           case 1: box_out.addDataFromShip(a-b); // SUB
52               break;
53           case 2: box_out.addDataFromShip(Math.max(a,b)); // MAX
54               break;
55           case 3: box_out.addDataFromShip(Math.min(a,b)); // MIN
56               break;
57           default: box_out.addDataFromShip(0);
58               break;
59       }
60   }
61 }
62
63 == FleetSim ==============================================================
64
65 == FPGA ==============================================================
66
67   reg                    have_a;
68   reg [(`DATAWIDTH-1):0] reg_a;
69   reg                    have_b;
70   reg [(`DATAWIDTH-1):0] reg_b;
71   reg                    have_op;
72   reg [(`DATAWIDTH-1):0] reg_op;
73
74   always @(posedge clk) begin
75     if (!have_a) begin
76       `onread(in1_r, in1_a) have_a = 1; reg_a = in1_d; end
77       end
78     if (!have_b) begin
79       `onread(in2_r, in2_a) have_b = 1; reg_b = in2_d; end
80       end
81     if (!have_op) begin
82       `onread(inOp_r, inOp_a) have_op = 1; reg_op = inOp_d; end
83       end
84   
85     if (have_a && have_b && have_op) begin
86       case (reg_op)
87         0: out_d = reg_a + reg_b;
88         1: out_d = reg_a - reg_b;
89         2: out_d = reg_a > reg_b ? reg_a : reg_b;
90         3: out_d = reg_a > reg_b ? reg_b : reg_a;
91         default: out_d = 0;
92       endcase        
93       `onwrite(out_r, out_a)
94         have_a  = 0;
95         have_b  = 0;
96         have_op = 0;
97       end
98     end
99   end
100
101
102
103
104 == Contributors =========================================================
105 Adam Megacz <megacz@cs.berkeley.edu>