Professional Documents
Culture Documents
Carry Save Mul
Carry Save Mul
parameter
N = 4,
M = 4;
input [N-1:0] x_in;
input [M-1:0] y_in;
output [N+M-1:0] p;
reg
[N+M-1:0] p;
reg [M:0] cin[N:0], pin[N:0], cout[N:0], pout[N:0];
integer i, j;
always @(x_in or y_in)begin
cin[0] = 0;
pin[0] = 0;
for(i=0; i<=N-1; i=i+1)begin
for(j=0; j<=M-1; j=j+1)begin
basic_mul_cell(x_in[i], y_in[j], cin[i][j],
pin[i][j],
cout[i][j],
pout[i][j]);
cin[i+1][j] = cout[i][j];
if(j==0) p[i] = pout[i][j];
if(j>0) pin[i+1][j-1] = pout[i][j];
end
pin[i+1][M-1] = 'b0;
end
for(i=0; i<=M-1; i=i+1)
p[i+N] = pin[N][i] + cin[N][i];
end
//
task basic_mul_cell(input x_i, y_j, cin, pin, output cout, pout);
reg
int_p;
begin
int_p = x_i & y_j;
cout = (cin & pin)|(cin & int_p)|(pin & int_p);
pout = cin ^ int_p ^ pin;
end
endtask
endmodule
clk, ini;
[N-1:0] x_in;
[M-1:0] y_in;
done;
done;
[N+M-1:0] p_out;
[N+M-1:0] p_out;
integer counter;
reg [M:0] temp_p;
reg [N-1:0] x_reg;
reg [M-1:0] y_reg, p_reg;
reg [N+M-1:0] p_out;
always @(posedge clk)begin
if(ini)begin
x_reg <= x_in;
y_reg <= y_in;
p_reg <= 0;
counter <= 0;
done <= 0;
end
else if(counter < N)begin
if(x_reg[0])
temp_p = p_reg + y_reg;
else
temp_p = p_reg;
p_reg <= temp_p[M:1];
x_reg <= {temp_p[0], x_reg[N-1:1]};
counter <= counter + 1;
end
else if(counter == N)begin
p_out = {p_reg[M-1:0], x_reg[N-1:0]};
done <= 1;
end
end
endmodule
xi;
if(xi) mult_by_1 = A + B;
else
mult_by_1 = A;
endfunction
endmodule