run omegaCounter inverter study with Electric LE code fixed
[fleet.git] / misc / obsolete-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
53   always @(posedge clk) begin
54     if (!rst) begin
55       `reset
56     end else begin
57       if (out_r    && out_a)    out_r    <= 0;
58       if (!in_r    && in_a)     in_a     <= 0;
59       if (!inOp_r  && inOp_a)   inOp_a   <= 0;
60       if (!out_r && !out_a && in_r && !in_a && inOp_r && !inOp_a) begin
61         out_r <= 1;
62         in_a <= 1;
63         inOp_a <= 1;
64         case (inOp_d)
65           0: out_d <= -in_d;
66           1: out_d <= in_d+1;
67           2: out_d <= in_d-1;
68           3: out_d <= (in_d<0) ? (-in_d) : in_d;
69           default: out_d <= 0;
70         endcase        
71       end
72     end
73   end
74
75 == Test ==============================================================================
76 // expected output
77
78 #expect 10
79 #expect 8
80 #expect 9
81 #expect -9
82 #expect 9
83
84 #ship debug        : Debug
85 #ship alu1         : Alu1
86 #ship fifo         : Fifo
87
88 debug.in:
89   [*] take, deliver;
90
91 alu1.in:
92   literal 9;
93   load repeat counter with 4;
94   deliver;
95   take, deliver;
96
97 alu1.out:
98   load repeat counter with 4;
99   take, sendto debug.in;
100   sendto alu1.in;
101   take, sendto debug.in;
102
103 alu1.inOp:
104   notify fifo.out;
105   [*] take, deliver, notify fifo.out;
106
107 fifo.out:
108   [*] wait, take, sendto alu1.inOp;
109
110 fifo.in:
111    literal 1; deliver;
112    literal 2; deliver;
113    literal 3; deliver;
114    literal 0; deliver;
115    literal 0; deliver;
116
117
118 == Contributors =========================================================
119 Adam Megacz <megacz@cs.berkeley.edu>