remove macros include from Debug.ship
[fleet.git] / ships / Alu2.ship
1 ship: Alu2
2
3 == Ports ===========================================================
4 data  in:   in1
5 data  in:   in2
6 data  in:   inOp
7   constant ADD: 0
8   constant SUB: 1
9   constant MAX: 2
10   constant MIN: 3
11
12 data  out:  out
13
14
15 == TeX ==============================================================
16
17 {\tt Alu2} is a ``two-input'' arithmetic logic unit.  It includes
18 logic for performing arithmetic operations on a pair of arguments.
19 Currently this includes
20 addition ({\sc add}),
21 subtraction ({\sc sub}),
22 maximum ({\sc max}), and
23 minimum ({\sc min}).
24
25 \subsection*{Semantics}
26
27 When a value is present at each of {\tt in1}, {\tt in2} and {\tt
28 inOp}, these three values are consumed.  Based on the value consumed
29 at {\tt inOp}, the requested operation is performed on the values
30 consumed from {\tt in1} and {\tt in2}.  The result of this operation
31 is then made available at {\tt out}.
32
33 \subsection*{To Do}
34
35 The {\it link bit} and other features of \cite{ies31} are not yet
36 implemented.
37
38 The carry-in, carry-out, zero-test, negative-test, and overflow-test
39 flags typically present in a conventional processor ALU are also not
40 yet implemented.
41
42 == Fleeterpreter ====================================================
43 public void service() {
44   if (box_in1.dataReadyForShip() &&
45       box_in2.dataReadyForShip() &&
46       box_inOp.dataReadyForShip() &&
47       box_out.readyForDataFromShip()) {
48       long a      = box_in1.removeDataForShip();
49       long b      = box_in2.removeDataForShip();
50       long op     = box_inOp.removeDataForShip();
51       switch((int)op) {
52           case 0: box_out.addDataFromShip(a+b); // ADD
53               break;
54           case 1: box_out.addDataFromShip(a-b); // SUB
55               break;
56           case 2: box_out.addDataFromShip(Math.max(a,b)); // MAX
57               break;
58           case 3: box_out.addDataFromShip(Math.min(a,b)); // MIN
59               break;
60           default: box_out.addDataFromShip(0);
61               break;
62       }
63   }
64 }
65
66 == FleetSim ==============================================================
67
68 == FPGA ==============================================================
69
70   always @(posedge clk) begin
71     if (!rst) begin
72       `reset
73     end else begin
74       if (out_r    && out_a)    out_r    <= 0;
75       if (!in1_r   && in1_a)    in1_a    <= 0;
76       if (!in2_r   && in2_a)    in2_a    <= 0;
77       if (!inOp_r  && inOp_a)   inOp_a   <= 0;
78       if (!out_r && !out_a && in1_r && !in1_a && in2_r && !in2_a && inOp_r && !inOp_a) begin
79         out_r <= 1;
80         in1_a <= 1;
81         in2_a <= 1;
82         inOp_a <= 1;
83         case (inOp_d)
84           0: out_d <= in1_d + in2_d;
85           1: out_d <= in1_d - in2_d;
86           2: out_d <= in1_d > in2_d ? in1_d : in2_d;
87           3: out_d <= in1_d > in2_d ? in2_d : in1_d;
88           default: out_d <= 0;
89         endcase        
90       end
91     end
92   end
93
94 == Test ==============================================================================
95 // expected output
96 #ship debug : Debug
97 #ship alu   : Alu2
98
99 #expect 17
100 #expect 1
101 #expect 8
102 #expect 9
103
104 debug.in:   [*] take, deliver;
105 alu.in1:
106   literal 9; load repeat counter with 4; deliver;
107
108 alu.in2:
109   literal 8; load repeat counter with 4; deliver;
110
111 alu.inOp:
112  literal Alu2.inOp[ADD]; deliver;
113  literal Alu2.inOp[SUB]; deliver;
114  literal Alu2.inOp[MIN]; deliver;
115  literal Alu2.inOp[MAX]; deliver;
116
117 alu.in1:    [*] take, deliver;
118 alu.in2:    [*] take, deliver;
119 alu.out:    [*] take, sendto debug.in;
120
121
122
123 == Contributors =========================================================
124 Adam Megacz <megacz@cs.berkeley.edu>