add rst wire (but do not do anything with it)
[fleet.git] / ships / Alu1.ship
1 ship: Alu1
2
3 == Ports ===========================================================
4 data  in:   in
5 data  in:   inOp
6   constant NEG: 0
7   constant INC: 1
8   constant DEC: 2
9   constant ABS: 3
10
11 data  out:  out
12
13 == TeX ==============================================================
14
15 {\tt Alu1} is a ``one-input'' arithmetic logic unit.  It includes
16 logic for performing arithmetic operations on a single argument.
17 Currently this includes
18 negate ({\sc neg}),
19 increment ({\sc inc}),
20 decrement ({\sc dec}), and
21 absolute value ({\sc abs}).
22
23 \subsection*{Semantics}
24
25 When a value is present at each of {\tt in} and {\tt inOp}, these two
26 values are consumed.  Based on the value consumed at {\tt inOp}, the
27 requested operation is performed on the value consumed from {\tt in}.
28 The result of this operation is then made available at {\tt out}.
29
30 == Fleeterpreter ====================================================
31     public void service() {
32         if (box_in.dataReadyForShip() && box_inOp.dataReadyForShip() && box_out.readyForDataFromShip()) {
33             long data   = box_in.removeDataForShip();
34             long opcode = box_inOp.removeDataForShip();
35             switch((int)opcode) {
36                 case 0: box_out.addDataFromShip(-1 * data);      // NEG
37                     break;
38                 case 1: box_out.addDataFromShip(data+1);         // INC
39                     break;
40                 case 2: box_out.addDataFromShip(data-1);         // DEC
41                     break;
42                 case 3: box_out.addDataFromShip(Math.abs(data)); // ABS
43                     break;
44                 default: box_out.addDataFromShip(0);
45                     break;
46             }
47         }
48     }
49
50 == FleetSim ==============================================================
51 == FPGA ==============================================================
52   reg                       have_a;
53   reg [(`PACKET_WIDTH-1):0] reg_a;
54   reg                       have_op;
55   reg [(`PACKET_WIDTH-1):0] reg_op;
56   reg [(`PACKET_WIDTH-1):0] extrabits;
57
58   always @(posedge clk) begin
59     if (!have_a) begin
60       `onread(in_r, in_a) have_a = 1; reg_a = in_d; end
61       end
62     if (!have_op) begin
63       `onread(inOp_r, inOp_a)
64          have_op   = 1;
65          reg_op    = inOp_d[(`DATAWIDTH-1):0];
66          extrabits = inOp_d[(`PACKET_WIDTH-1):`DATAWIDTH];
67       end
68     end
69   
70     if (have_a && have_op) begin
71       case (reg_op)
72         0: out_d = -reg_a;
73         1: out_d = reg_a+1;
74         2: out_d = reg_a-1;
75         3: out_d = (reg_a<0) ? (-reg_a) : reg_a;
76         4: out_d = 37'b1111111111111111111111111111111111111;
77         5: out_d = extrabits;
78         default: out_d = 0;
79       endcase        
80       `onwrite(out_r, out_a)
81         have_a  = 0;
82         have_op = 0;
83       end
84     end
85   end
86
87 == Test ==============================================================================
88 // expected output
89 #expect 10
90 #expect 8
91 #expect 9
92 #expect -9
93 #expect 9
94
95 #ship debug        : Debug
96 #ship alu1         : Alu1
97
98 debug.in:   [*] take, deliver;
99 alu1.in:
100   literal 9;
101   load repeat counter with 4; deliver;
102   [*] take, deliver;
103
104 alu1.out:
105   load repeat counter with 4; take, sendto debug.in;
106   sendto alu1.in;
107   [*] take, sendto debug.in;
108
109 alu1.inOp:
110    literal 1; deliver;
111    literal 2; deliver;
112    literal 3; deliver;
113    literal 0; load repeat counter with 2; deliver;
114
115
116 == Contributors =========================================================
117 Adam Megacz <megacz@cs.berkeley.edu>