You are on page 1of 3

module carry_save_mult(x_in, y_in, p);

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

module basic_base2_mul_seq(x_in, y_in, clk, ini, p_out, done);


parameter
N = 8,
M = 4;
input
input
input
output
reg
output
reg

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

module basic_base2_mul(x_in, y_in, p_out);


parameter
N = 8,
M = 4;
input [N-1:0] x_in;
input [M-1:0] y_in;
output [N+M-1:0] p_out;
reg
[N+M-1:0] p_out;
integer i;
reg [M:0] wires[N:0];
always @(x_in or y_in)begin
for(i=0; i<N-1; i=i+1)begin
wires[i+1] = mult_by_1(x_in[i], wires[i][M:1], y_in);
p_out[i] = wires[i+1][0];
end
p_out[N+M-1:N] = wires[N][M:1];
end
function [M:0] mult_by_1;
input
input [M-1:0] A, B;

xi;

if(xi) mult_by_1 = A + B;
else
mult_by_1 = A;
endfunction
endmodule

module ripple_carry_mult(x_in, y_in, p);


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
for(i=0; i<=M-1; i=i+1)
cin[i][0] = 'b0;
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][j+1] = 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] = cin[i][M];
end
for(i=0; i<=M-1; i=i+1)
p[i+N] = pin[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

You might also like