1、设计内容
四位计数器,实现0---9的计数。
2、代码
module BCD_counter(Clk,Cin,Rst_n,Cout,q);
input Clk;//计数基准时钟
input Cin;//计数器进位输入
input Rst_n;//系统复位
output reg Cout;//计数进位输出
output q;//计数值输出
reg [3:0]cnt;//定义计数器寄存器
//执行计数过程
always @(posedge Clk or negedge Rst_n)
begin
if(Rst_n == 1'b0)
cnt <= 4'd0;
else if (Cin == 1'b1)
begin
if(cnt == 4'd9)
cnt <= 4'd0;
else
cnt <= cnt + 1'b1;
end
else
cnt <= cnt;
end
//产生进位输出信号
always @(posedge Clk or negedge Rst_n)
begin
if(Rst_n == 1'b0)
Cout <= 1'b0;
else if(Cin == 1'b1 && cnt == 4'd9)
Cout <= 1'b1;
else
Cout <= 1'b0;
end
assign q = cnt;
endmodule
`timescale 1ns/1ns
`define clock_period 20
module BCD_counter_tb;
reg Clk;
reg Cin;
reg Rst_n;
wire Cout;
wire [3:0]q;
BCD_counter BCD_counter0(
.Clk(Clk),
.Cin(Cin),
.Rst_n(Rst_n),
.Cout(Cout),
.q(q)
);
initial Clk = 1'b1;
always #(`clock_period/2) Clk = ~Clk;
initial begin
Rst_n = 1'b0;
Cin = 1'b0;
#(`clock_period*200);
Rst_n = 1'b1;
#(`clock_period*20);
repeat(30)
begin
Cin = 1'b1;
#(`clock_period);
Cin = 1'b0;
#(`clock_period*5);
end
#(`clock_period*20)
$stop;
end
endmodule
3、RTL电路图
4、RTL仿真