3 == Ports ===========================================================
9 == TeX ==============================================================
11 The Carry-Save Adder computes the carry-save sum of three input
12 values, provided sequentially at {\tt in}, and produces.
15 == Fleeterpreter ====================================================
26 private long maj(long a, long b, long c) {
28 for(int i=0; i<64; i++) {
29 boolean a_ = (a&(1L<<i))!=0L;
30 boolean b_ = (b&(1L<<i))!=0L;
31 boolean c_ = (c&(1L<<i))!=0L;
32 if ( (a_ && b_) || (b_ && c_) || (a_ && c_) )
37 public void service() {
38 if (!box_out.readyForDataFromShip()) return;
39 if (state!=3 && !box_in.dataReadyForShip()) return;
41 case 0: out = box_in.removeDataForShip(); break;
42 case 1: temp = box_in.removeDataForShip(); break;
44 long in = box_in.removeDataForShip();
45 long mm = maj(temp, out, in);
46 box_out.addDataFromShip(mm << 1, ((mm >> (getFleet().getWordWidth()-1)) & 1L)!=0);
47 temp = (temp ^ out) ^ in;
49 case 3: box_out.addDataFromShip(temp, false); break;
51 state = (state+1) % 4;
55 == FPGA ==============================================================
57 reg [(`WORDWIDTH-1):0] temp;
58 reg [(`WORDWIDTH):0] out_d;
61 assign out_d_ = out_d;
63 wire [(`WORDWIDTH-1):0] majority;
64 wire [(`WORDWIDTH-1):0] xors;
67 for(i=0; i<`WORDWIDTH; i=i+1) begin : OUT
68 assign majority[i] = (temp[i] & out_d[i]) | (in_d[i] & out_d[i]) | (temp[i] & in_d[i]);
69 assign xors[i] = temp[i] ^ out_d[i] ^ in_d[i];
73 always @(posedge clk) begin
79 if (`out_empty && state==3) begin
80 out_d <= { 1'b0, temp };
83 end else if (`in_full && `out_empty) begin
85 out_d <= { 1'b0, in_d };
86 end else if (state == 1) begin
88 end else if (state == 2) begin
89 out_d <= { majority[`WORDWIDTH-1:0], 1'b0 };
99 == Test ==============================================================
106 // ships required in order to run this code
108 #ship csa : CarrySaveAdder
113 set word=1018217; deliver;
114 set word=771820; deliver;
115 set word=2161521; deliver;
117 collect, send to csa.in; send to alu.in1;
118 collect, send to csa.in; send to alu.in2;
119 collect, send to csa.in; send to alu.in2;
121 alu.in1: set ilc=4; recv, deliver;
122 alu.in2: set ilc=4; recv, deliver;
124 set word=Alu.inOp[ADD];
127 collect, send to alu.in1;
128 collect; send to debug.in;
130 collect; send to debug.in;
138 collect, send to debug.in;
140 collect, send to debug.in;
145 send token to csa.out;
148 send token to alu.out;
152 == Contributors =========================================================
153 Adam Megacz <megacz@cs.berkeley.edu>