将多个一位BCD计数器级联即可得到多位BCD计数器
一位BCD计数器模块代码:
module BCD_Count(Clk,Rst_n,Cin,Cout,q);
input Clk;
input Rst_n;
input Cin;
output reg Cout;
output [3:0] q;
reg [3:0] cnt;
always@( posedge Clk or negedge Rst_n )
begin
if( Rst_n==0 )
cnt<=0;
else if( Cin==1 )
begin
cnt=cnt+1;
if( cnt==10 )
begin
cnt<=0;
Cout<=1;
end
end
else if( Cin==0 )
Cout<=0;
end
assign q=cnt;
endmodule
module BCD_Count_top(Clk,Rst_n,Cin,Cout,q);
input Clk;
input Rst_n;
input Cin;
output Cout;
output [11:0] q;
wire cout0,cout1;
wire [3:0] q0,q1,q2;
assign q={q2,q1,q0};
BCD_Count BCD_Count0
(
.Clk(Clk),
.Rst_n(Rst_n),
.Cin(Cin),
.Cout(cout0),
.q(q0)
);
BCD_Count BCD_Count1
(
.Clk(Clk),
.Rst_n(Rst_n),
.Cin(cout0),
.Cout(cout1),
.q(q1)
);
BCD_Count BCD_Count2
(
.Clk(Clk),
.Rst_n(Rst_n),
.Cin(cout1),
.Cout(Cout),
.q(q2)
);
endmodule
testbench代码:
`timescale 1ns/1ns
`define Time_Periord 20
module BCD_Count_top_tb;
reg clk;
reg rst;
reg cin;
wire cout;
wire [11:0] q;
BCD_Count_top BCD_Count_top0
(
.Clk(clk),
.Rst_n(rst),
.Cin(cin),
.Cout(cout),
.q(q)
);
initial
clk=1;
always
#(`Time_Periord/2) clk=~clk;
initial
begin
rst=0;
#100;
rst=1;
repeat( 3000 )
begin
cin=0;
#(`Time_Periord*10);
cin=1;
#(`Time_Periord*1);
end
$stop;
end
endmodule
RTL仿真结果:
还是存在一些小瑕疵,以后有熟练了再改完善