bugfix to Alu3
[fleet.git] / ships / Alu3.ship
1 ship: Alu3
2
3 == Ports ===========================================================
4 data  in:   in1
5 data  in:   in2
6 data  in:   in3
7
8 data  out:  out1
9   shortcut to: in1
10 data  out:  out2
11   shortcut to: in2
12 data  out:  out3
13   shortcut to: in3
14 data  out:  outBits
15
16 == Constants ========================================================
17 == TeX ==============================================================
18
19 == Fleeterpreter ====================================================
20 boolean mode = false;
21 BitFifo.BitStorage outBits = new BitFifo.BitStorage(74);
22 public void service() {
23   if (box_in1.dataReadyForShip() &&
24       box_in2.dataReadyForShip() &&
25       box_in3.dataReadyForShip() &&
26       outBits.hasSpace(1) &&
27       box_out1.readyForDataFromShip() &&
28       box_out2.readyForDataFromShip() &&
29       box_out3.readyForDataFromShip()) {
30       long v1 = box_in1.removeDataForShip();
31       long v2 = box_in2.removeDataForShip();
32       long v3 = box_in3.removeDataForShip();
33       long o1, o2, o3;
34       if (!mode) {
35         o1 = v1;
36         o2 = v2 >>> 1;
37         o3 = ((v2 & 0x1L) == 0) ? 0 : v1;
38       } else {
39         o1 = ((v1 & v2) | (v2 & v3) | (v1 & v3))/* << 1*/;
40         o2 = (v1 ^ v2 ^ v3) >> 1;
41         o3 = 0;
42         outBits.add((v1 ^ v2 ^ v3) & 0x1L, 1);
43       }
44       box_out1.addDataFromShip(o1);
45       box_out2.addDataFromShip(o2);
46       box_out3.addDataFromShip(o3);
47       mode = !mode;
48   }
49   if (box_outBits.readyForDataFromShip() &&
50       outBits.size() >= 37) {
51       box_outBits.addDataFromShip(outBits.get(37));
52   }
53 }
54
55 == FleetSim ==============================================================
56
57 == FPGA ==============================================================
58
59   reg                    mode;         initial mode = 0;
60   reg                    have_in1;     initial have_in1 = 0;
61   reg                    have_in2;     initial have_in2 = 0;
62   reg                    have_in3;     initial have_in3 = 0;
63   reg [(`DATAWIDTH-1):0] keep_in1;     initial keep_in1 = 0;
64   reg [(`DATAWIDTH-1):0] keep_in2;     initial keep_in2 = 0;
65   reg [(`DATAWIDTH-1):0] keep_in3;     initial keep_in3 = 0;
66   reg                    have_out1;    initial have_out1 = 0;
67   reg                    have_out2;    initial have_out2 = 0;
68   reg                    have_out3;    initial have_out3 = 0;
69   reg [73:0] bitstorage;
70   reg [7:0] bitstorage_count;          initial bitstorage_count = 0;
71   reg wrote;                           initial wrote = 0;
72
73   always @(posedge clk) begin
74     wrote = 0;
75     if (have_out1) begin
76       `onwrite(out1_r, out1_a) have_out1 <= 0; end
77     end else if (have_out2) begin
78       `onwrite(out2_r, out2_a) have_out2 <= 0; end
79     end else if (have_out3) begin
80       `onwrite(out3_r, out3_a) have_out3 <= 0; end
81     end else if (!have_in1) begin
82       `onread(in1_r, in1_a) have_in1 <= 1; keep_in1 <= in1_d; end
83     end else if (!have_in2) begin
84       `onread(in2_r, in2_a) have_in2 <= 1; keep_in2 <= in2_d; end
85     end else if (!have_in3) begin
86       `onread(in3_r, in3_a) have_in3 <= 1; keep_in3 <= in3_d; end
87     end else begin
88         if (mode == 0) begin
89           out1_d    <= keep_in1;
90           out2_d    <= { 1'b0, keep_in2[(`DATAWIDTH-1):1] };
91           out3_d    <= (keep_in2[0]==0) ? 0 : keep_in1;
92         end else begin
93           out1_d           <= { ((keep_in1 & keep_in2) | (keep_in2 & keep_in3) | (keep_in1 & keep_in3)) };
94           out2_d                       <= (keep_in1 ^ keep_in2 ^ keep_in3) >> 1;
95           out3_d                       <= 0;
96           bitstorage_count             <= bitstorage_count+1;
97           bitstorage[bitstorage_count] = (keep_in1[0] ^ keep_in2[0] ^ keep_in3[0]);
98           outBits_d                    = bitstorage[(`DATAWIDTH-1):0];
99           wrote = 1;
100         end
101         have_out1 <= 1;
102         have_out2 <= 1;
103         have_out3 <= 1;
104         have_in1  <= 0;
105         have_in2  <= 0;
106         have_in3  <= 0;
107         mode <= ~mode;
108     end
109
110     if (!wrote && bitstorage_count >= `DATAWIDTH) begin
111       `onwrite(outBits_r, outBits_a) begin
112         bitstorage_count <= bitstorage_count - `DATAWIDTH;
113         bitstorage       = bitstorage >> `DATAWIDTH;
114       end
115     end    
116   end
117 end
118
119
120 == Test ========================================================================
121 #expect 100488372224
122 #expect 8
123
124
125 #ship alu3  : Alu3
126 #ship debug : Debug
127
128 1000000:       sendto alu3.in1;
129 1200000:       sendto alu3.in2;
130 0:             sendto alu3.in3;
131 0:             sendto alu3.in1;
132 0:             sendto alu3.in2;
133 alu3.in1:      [*] take, deliver;
134 alu3.in2:      [*] take, deliver;
135 alu3.in3:      [*] take, deliver;
136 alu3.out1:     [74] take, sendto alu3.in1;
137                [74] take, sendto alu3.in1;
138 alu3.out2:     [74] take, sendto alu3.in2;
139                [74] take, sendto alu3.in2;
140 alu3.out3:     [74] take, sendto alu3.in3;
141                [74] take, sendto alu3.in3;
142 alu3.outBits:  [2] take, sendto debug.in;
143 debug.in:      [*] take, deliver;
144
145
146 == Contributors =========================================================
147 Amir Kamil <kamil@cs.berkeley.edu>
148 Adam Megacz <megacz@cs.berkeley.edu>