adjust ships to use fill/drain/full/empty macros
[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       `cleanup
48       if (`out_empty && state==3) begin
49         out_d <= { 1'b0, temp };
50         `fill_out
51         state <= state + 1;
52       end else if (`in_full && `out_empty) begin
53         if (state == 0) begin
54           out_d <= { 1'b0, in_d };
55         end else if (state == 1) begin
56           temp <= in_d;
57         end else if (state == 2) begin
58           out_d <= { majority[`DATAWIDTH-1:0], 1'b0 };
59           temp  <= xors;
60           `fill_out
61         end
62         state <= state + 1;
63         `drain_in
64       end
65     end
66   end
67
68 == Test ==============================================================
69 // expected output
70 #expect 0x3c4bc6
71 #expect 0x1796d2
72 #expect 0x24b4f4
73 #expect 0x3c4bc6
74
75 // ships required in order to run this code
76 #ship debug        : Debug
77 #ship csa          : CarrySaveAdder
78 #ship alu          : Alu
79 #ship fifo         : Fifo
80
81 fifo.in:
82    set word=1018217;  deliver;
83    set word=771820;   deliver;
84    set word=2161521;  deliver;
85 fifo.out:
86    collect, send to csa.in; send to alu.in1;
87    collect, send to csa.in; send to alu.in2;
88    collect, send to csa.in; send to alu.in2;
89
90 alu.in1: set ilc=4; recv, deliver;
91 alu.in2: set ilc=4; recv, deliver;
92 alu.inOp:
93    set word=Alu.inOp[ADD];
94    set ilc=4; deliver;   
95 alu.out:
96    collect, send to alu.in1;
97    collect; send to debug.in;
98    recv token;
99    collect; send to debug.in;
100
101 csa.in:
102    set ilc=*;
103    recv, deliver;
104
105 csa.out:
106    recv token;
107    collect, send to debug.in;
108    send to alu.in1;
109    collect, send to debug.in;
110    send to alu.in2;
111
112 debug.in:
113    recv, deliver;
114    send token to csa.out;
115    set ilc=2;
116    recv, deliver;
117    send token to alu.out;
118    recv, deliver;
119
120
121 == Contributors =========================================================
122 Adam Megacz <megacz@cs.berkeley.edu>