基于verilog的微复杂电路
Ripple cary adder:
module eight_bit_adder (
a,
b,
cin,
cout,
sum,
clk,
rst
);
input [7:0]a,b;
input cin,clk,rst;
output reg cout;
output reg [7:0]sum;
reg [6:0]bit_carry;
always@(posedge clk or negedge rst) begin
if (~rst) begin
cout<=0;
sum<=0;
end
else begin
{bit_carry[0],sum[0]}<=a[0]+b[0]+cin;
{bit_carry[1],sum[1]}<=a[1]+b[1]+bit_carry[0];
{bit_carry[2],sum[2]}<=a[2]+b[2]+bit_carry[1];
{bit_carry[3],sum[3]}<=a[3]+b[3]+bit_carry[2];
{bit_carry[4],sum[4]}<=a[4]+b[4]+bit_carry[3];
{bit_carry[5],sum[5]}<=a[5]+b[5]+bit_carry[4];
{bit_carry[6],sum[6]}<=a[6]+b[6]+bit_carry[5];
{cout,sum[7]}<=a[7]+b[7]+bit_carry[6];
end
end
endmodule //8_bit_adder
testbench:
module tb_rca_bitadder ();
reg [7:0] a,b;
output [7:0] sum;
output cout;
reg rst,clk;
reg cin;
parameter delay=140;
eight_bit_adder m1(a,b,cin,cout,sum,clk,rst);
initial begin
$dumpfile("dump.vcd");
$dumpvars;
end
initial begin
clk=0;
forever #10 clk = ~clk;
end
initial begin
#10a=32;b=32;cin=1'b0; rst=1;
// #delay a=1;b=1;
// #delay a=20;b=20;cin=0;
// #delay a=75;b=75;cin=0;
// #delay a=128;b=128;cin=0;
// #delay a=200;b=200;cin=0;
end
// initial begin
// clk=0;
// forever #10 clk = ~clk;
// end
initial begin
#1000 $finish;
end
endmodule
两级pipeline adder:
module pipeline_8bit_adder (enable,rst,a,b,cin,cout,sum);
input [7:0] a,b;
output reg[7:0] sum;
output reg cout;
input cin,rst,enable;
reg[3:0] temp_a;
reg[3:0] temp_b;
reg temp_cin;
reg[3:0]low_bit_sum;
reg[3:0]high_bit_sum;
always @(posedge enable or negedge rst) begin
if (~rst) begin
sum=0;
cout=0;
end
else begin
{temp_cin,low_bit_sum}<=a[3:0]+b[3:0]+cin;
temp_a <= a[7:4];
temp_b <= b[7:4];
// {cout,sum[7:4]}<=temp_a+temp_b+temp_cin;
// {cout,high_bit_sum}<=temp_a+temp_b+temp_cin;
// sum[3:0]<=low_bit_sum;
end
end
always @( posedge enable or negedge rst) begin
if (~rst) begin
sum=0;
cout=0;
end
else begin
{cout,sum[7:4]}<=temp_a+temp_b+temp_cin;
{cout,high_bit_sum}<=temp_a+temp_b+temp_cin;
sum[3:0]<=low_bit_sum;
end
end
endmodule
testbench:
module tb_8_bitadder ();
reg [7:0] a,b;
output [7:0] sum;
output cout;
reg cin;
reg rst,enable;
parameter delay=80;
pipeline_8bit_adder m1(enable,rst,a,b,cin,cout,sum);
initial begin
$dumpfile("dump.vcd");
$dumpvars;
end
initial begin
enable=0;
forever #10 enable = ~enable;
end
initial begin
#10 a=8'b0;b=8'b0;cin=1'b0; rst=1;
#20 a=1;b=1;
#20 a=20;b=20;cin=1;
#20 a=75;b=75;cin=1;
#20 a=128;b=128;cin=0;
#20 a=200;b=200;cin=0;
end
// initial begin
// clk=0;
// forever #10 clk = ~clk;
// end
initial begin
#200 $finish;
end
endmodule
Comments
Post a Comment