`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 19:15:48 09/19/2019
// Design Name:
// Module Name: bcd
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module bcd(clk,cin,rst,cout,q
);
input clk;//计数器时钟输入ujs-lili
input cin;//计数器进位输入,就是一个使能端
input rst;//计数器复位
output reg cout;//计数器进位输出
output [3:0]q;//计数器值
reg [3:0] cnt;//计数值,对时钟计数
//加到9就结束
always @(posedge clk or negedge rst)
if(rst == 1'b0)
cnt<=4'd0;
else if(cin == 1'b1) begin//在rst=1且,cin为1的时候又有两种情况,若cnt为9,清零,若不为9,加一
if(cnt == 4'd9)
cnt<=4'd0;
else
cnt<=cnt+1'b1;
end
else//如果不满足上述情况的话,和第一个else是一组的
cnt<=cnt;
//产生进位信号
always @(posedge clk or negedge rst)
if(rst == 1'b0)
cout<=4'd0;
else if (cin==1'b1 && cnt ==4'd9)
cout<=1'b1;//产生进位
else
cout<=1'b0;
//把计数值输出
assign q= cnt;
仿真:
module bcd_tb;
// Inputs
reg clk;
reg cin;
reg rst;
// Outputs
wire cout;
wire [3:0] q;
// Instantiate the Unit Under Test (UUT)
bcd uut (
.clk(clk),
.cin(cin),
.rst(rst),
.cout(cout),
.q(q)
);
always#(`clock) clk=~clk;
initial begin
// Initialize Inputs
clk = 0;
cin = 0;
rst = 0;
// Wait 100 ns for global reset to finish
#10;
rst =1;
repeat(30)begin
cin =1;
#`clock;
cin=0;
#(`clock*5);
end
// Add stimulus here
end
endmodule```
![bcd](https://img-blog.csdnimg.cn/20190919194037809.PNG)
限制于ise的仿真时间...