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
80 if (`out_empty && state==3) begin
81 out_d <= { 1'b0, temp };
84 end else if (`in_full && `out_empty) begin
86 out_d <= { 1'b0, in_d };
87 end else if (state == 1) begin
89 end else if (state == 2) begin
90 out_d <= { majority[`WORDWIDTH-1:0], 1'b0 };
100 == Test ==============================================================
107 // ships required in order to run this code
109 #ship csa : CarrySaveAdder
114 set word=1018217; deliver;
115 set word=771820; deliver;
116 set word=2161521; deliver;
118 collect, send to csa.in; send to alu.in1;
119 collect, send to csa.in; send to alu.in2;
120 collect, send to csa.in; send to alu.in2;
122 alu.in1: set ilc=4; recv, deliver;
123 alu.in2: set ilc=4; recv, deliver;
125 set word=Alu.inOp[ADD];
128 collect, send to alu.in1;
129 collect; send to debug.in;
131 collect; send to debug.in;
139 collect, send to debug.in;
141 collect, send to debug.in;
146 send token to csa.out;
149 send token to alu.out;
153 == Contributors =========================================================
154 Adam Megacz <megacz@cs.berkeley.edu>