4371afd3650ac38a6f3e4ac3e6eeca85debafc32
[fleet.git] / ships / CarrySaveAdder.ship
1 ship: CarrySaveAdder
2
3 == Ports ===========================================================
4 in:   in
5
6 out:  out
7
8   
9 == TeX ==============================================================
10
11 The Carry-Save Adder computes the carry-save sum of three input
12 values, provided sequentially at {\tt in}, and produces.
13
14
15 == Fleeterpreter ====================================================
16
17 public void service() {
18   if (box_in.dataReadyForShip() && box_out.readyForDataFromShip()) {
19   }
20 }
21
22
23 == FPGA ==============================================================
24
25   reg [(`DATAWIDTH-1):0] temp;
26   reg [(`DATAWIDTH):0]   out_d;
27   reg [1:0] state;
28   initial state = 0;
29   assign out_d_ = out_d;
30
31   wire [(`DATAWIDTH-1):0] majority;
32   wire [(`DATAWIDTH-1):0] xors;
33   genvar i;
34   generate
35     for(i=0; i<`DATAWIDTH; i=i+1) begin : OUT
36       assign majority[i] = (temp[i] & out_d[i]) | (in_d[i] & out_d[i]) | (temp[i] & in_d[i]);
37       assign xors[i]     = temp[i] ^ out_d[i] ^ in_d[i];
38     end
39   endgenerate
40
41   always @(posedge clk) begin
42     if (!rst) begin
43       `reset
44       state <= 0;
45     end else begin
46       `flush
47       if (!in_r_       && in_a)       in_a        <= 0;
48       if (out_r        && out_a)      out_r       <= 0;
49       if (!out_r && !out_a && state==3) begin
50         out_d <= { 1'b0, temp };
51         out_r <= 1;
52         state <= state + 1;
53       end else if (in_r && !in_a && !out_r && !out_a) begin
54         if (state == 0) begin
55           out_d <= { 1'b0, in_d };
56         end else if (state == 1) begin
57           temp <= in_d;
58         end else if (state == 2) begin
59           out_d <= { majority[`DATAWIDTH-1:0], 1'b0 };
60           temp  <= xors;
61           out_r <= 1;
62         end
63         state <= state + 1;
64         in_a  <= 1;
65       end
66     end
67   end
68
69 == Test ==============================================================
70 // expected output
71 #expect 0x3c4bc6
72 #expect 0x1796d2
73 #expect 0x24b4f4
74 #expect 0x3c4bc6
75
76 // ships required in order to run this code
77 #ship debug        : Debug
78 #ship csa          : CarrySaveAdder
79 #ship alu          : Alu
80 #ship fifo         : Fifo
81
82 fifo.in:
83    set word=1018217;  deliver;
84    set word=771820;   deliver;
85    set word=2161521;  deliver;
86 fifo.out:
87    collect, send to csa.in; send to alu.in1;
88    collect, send to csa.in; send to alu.in2;
89    collect, send to csa.in; send to alu.in2;
90
91 alu.in1: set ilc=4; recv, deliver;
92 alu.in2: set ilc=4; recv, deliver;
93 alu.inOp:
94    set word=Alu.inOp[ADD];
95    set ilc=4; deliver;   
96 alu.out:
97    collect, send to alu.in1;
98    collect; send to debug.in;
99    recv token;
100    collect; send to debug.in;
101
102 csa.in:
103    set ilc=*;
104    recv, deliver;
105
106 csa.out:
107    recv token;
108    collect, send to debug.in;
109    send to alu.in1;
110    collect, send to debug.in;
111    send to alu.in2;
112
113 debug.in:
114    recv, deliver;
115    send token to csa.out;
116    set ilc=2;
117    recv, deliver;
118    send token to alu.out;
119    recv, deliver;
120
121
122 == Contributors =========================================================
123 Adam Megacz <megacz@cs.berkeley.edu>