more reset code
[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 (!rst) begin
60       have_a = 0;
61       have_op = 0;
62       `reset
63     end else begin
64     if (!have_a) begin
65       `onread(in_r, in_a) have_a = 1; reg_a = in_d; end
66       end
67     if (!have_op) begin
68       `onread(inOp_r, inOp_a)
69          have_op   = 1;
70          reg_op    = inOp_d[(`DATAWIDTH-1):0];
71          extrabits = inOp_d[(`PACKET_WIDTH-1):`DATAWIDTH];
72       end
73     end
74   
75     if (have_a && have_op) begin
76       case (reg_op)
77         0: out_d = -reg_a;
78         1: out_d = reg_a+1;
79         2: out_d = reg_a-1;
80         3: out_d = (reg_a<0) ? (-reg_a) : reg_a;
81         4: out_d = 37'b1111111111111111111111111111111111111;
82         5: out_d = extrabits;
83         default: out_d = 0;
84       endcase        
85       `onwrite(out_r, out_a)
86         have_a  = 0;
87         have_op = 0;
88       end
89     end
90     end
91   end
92
93 == Test ==============================================================================
94 // expected output
95 #expect 10
96 #expect 8
97 #expect 9
98 #expect -9
99 #expect 9
100
101 #ship debug        : Debug
102 #ship alu1         : Alu1
103
104 debug.in:   [*] take, deliver;
105 alu1.in:
106   literal 9;
107   load repeat counter with 4; deliver;
108   [*] take, deliver;
109
110 alu1.out:
111   load repeat counter with 4; take, sendto debug.in;
112   sendto alu1.in;
113   [*] take, sendto debug.in;
114
115 alu1.inOp:
116    literal 1; deliver;
117    literal 2; deliver;
118    literal 3; deliver;
119    literal 0; load repeat counter with 2; deliver;
120
121
122 == Contributors =========================================================
123 Adam Megacz <megacz@cs.berkeley.edu>