Verilog入门5bcd10进制

`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的仿真时间...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值