more bugfixes to Alu3
[fleet.git] / ships / Alu2b.ship
1 ship: Alu2b
2
3 == Ports ===========================================================
4 data  in:   in1.add
5 data  in:   in1.sub
6 data  in:   in1.max
7 data  in:   in1.min
8
9 data  in:   in2
10
11 data  out:  out
12
13 == Constants ========================================================
14
15 == TeX ==============================================================
16 This ship is a two-input arithmetic unit.  The first input is split
17 into multiple virtual destinations that control the operation to be
18 performed.
19
20 == Fleeterpreter ====================================================
21 public void service() {
22   if (!box_out.readyForDataFromShip() ||
23       !box_in1.dataReadyForShip() ||
24       !box_in2.dataReadyForShip()) return;
25
26   Packet selector = box_in1.removePacketForShip();
27   String port = selector.destination.getDestinationName();
28   long a      = selector.value;
29   long b      = box_in2.removeDataForShip();
30
31   if (port.equals("add")) {
32     box_out.addDataFromShip(a+b); // ADD
33   } else if (port.equals("sub")) {
34     box_out.addDataFromShip(a-b); // SUB
35   } else if (port.equals("max")) {
36     box_out.addDataFromShip(Math.max(a,b)); // MAX
37   } else if (port.equals("min")) {
38     box_out.addDataFromShip(Math.min(a,b)); // MIN
39   } else {
40     box_out.addDataFromShip(0);
41   }
42 }
43
44 == FleetSim ==============================================================
45
46 == FPGA ==============================================================
47
48   reg                    have_a;
49   reg [(`PACKET_WIDTH-1):0] reg_a;
50   reg                    have_b;
51   reg [(`DATAWIDTH-1):0] reg_b;
52
53   reg a_val;
54
55   always @(posedge clk) begin
56     if (!have_a) begin
57       `onread(in1_r, in1_a) have_a = 1; reg_a = in1_d; end
58       end
59     if (!have_b) begin
60       `onread(in2_r, in2_a) have_b = 1; reg_b = in2_d; end
61       end
62   
63     if (have_a && have_b) begin
64       a_val = reg_a[`DATAWIDTH-1:0];
65       case (reg_a[`PACKET_WIDTH-1:`DATAWIDTH])
66         0: out_d = a_val + reg_b;
67         1: out_d = a_val - reg_b;
68         2: out_d = a_val > reg_b ? a_val : reg_b;
69         3: out_d = a_val > reg_b ? reg_b : a_val;
70         default: out_d = 0;
71       endcase        
72       `onwrite(out_r, out_a)
73         have_a  = 0;
74         have_b  = 0;
75       end
76     end
77   end
78
79
80
81
82 == Contributors =========================================================
83 Adam Megacz <megacz@cs.berkeley.edu>
84 Amir Kamil  <kamil@cs.berkeley.edu>