当前位置:网站首页>Verilog 每日一题 (VL28 加减计数器)
Verilog 每日一题 (VL28 加减计数器)
2022-07-28 16:23:00 【别再出error了】
题目描述
请编写一个十进制计数器模块,当mode信号为1,计数器输出信号递增,当mode信号为0,计数器输出信号递减。每次到达0,给出指示信号zero。
模块的接口信号图如下:


这里的波形图有问题,明明第一个上升沿来时,已经开始加一了,这里的num是中间计数器,number被赋值会慢一拍,但这也看不出来啊。。。牛客网的题真是,还是HDLbits的做的开心。
考虑了这个之后,就很简单了。具体解题过程如下:
`timescale 1ns/1ns
module count_module(
input clk,
input rst_n,
input mode,
output reg [3:0]number,
output reg zero
);
reg [3:0] num; //声明一个reg变量作为中间计数
always @(posedge clk or negedge rst_n) begin
if(!rst_n) num <= 0 ;
else if(num==9&&mode==1) num<=0; //要考虑num=9继续增加时变0的情况
else if(num==0&&mode==0) num<=9; //要考虑num=0继续减少时变9的情况
else if(mode==1) num<=num+1; //mode控制加减
else if(mode==0) num<=num-1;
end
always @(posedge clk or negedge rst_n) begin
if(!rst_n) number <= 0; //将num赋值给number
else number <= num;
end
always @(posedge clk or negedge rst_n) begin
if(!rst_n) zero = 0;
else if(num==0) zero <= 1;
else zero <= 0;
end
endmodule
边栏推荐
- Atcoder beginer contest 240 g.reporting Takahashi (classical problems of Combinatorial Mathematics)
- Unity shader cartoon style rendering
- The longest substring of sword finger offer without repeated characters
- 线性代数及矩阵论(九)
- 一文了解 Kubernetes 中的服务发现
- Unity shader texture animation
- Goweb开发之Beego框架实战:第三节 程序执行流程分析
- 微服务架构-服务注册中心和服务网关(6.8) (转载)
- Algorithm learning: leetcode interview question 09. implement queue with two stacks
- Goweb开发之Beego框架实战:第二节 项目初始化配置
猜你喜欢

Shopee code League 2022 - qualification round p3.connecting the numbers (segment tree / bipartite graph determination, to be discussed)

22年多校第三场(F的证明

高速电路中电容的选型和应用——详解

mysql 最大建议行数2000w,靠谱吗?

配置V530交换机步骤

Why do I choose to use go language?

如何在构建阶段保护镜像安全

利用SQL Server代理作业对数据库进行定时还原

Steps to configure V530 switch

数据库故障容错之系统时钟故障
随机推荐
在android开发过程中遇到.sqlite文件处理
MySQL数据库增删改查(基础操作命令详解)
System clock failure of database fault tolerance
net框架
Goweb开发之Beego框架实战:第三节 程序执行流程分析
C # traversal set
Make full use of English
Goweb开发之Beego框架实战:第四节 数据库配置及连接
Ugui learning notes (IV) ugui event system overview and Usage Summary
Function接口之andThen
Some notes on how unity objects move
How do we do full link grayscale on the database?
Ugui learning notes (VI) get the information of the clicked UI
PostgreSQL weekly news - July 20, 2022
Modeling Semantics with Gated Graph Neural Networks for KBQA
线性代数及矩阵论(八)
Unity shader realizes mirror effect with rendered texture
Goweb开发之Beego框架实战:第五节 项目搭建及注册用户
Verilog 每日一题 (VL5 信号发生器)
A total of 13billion flash and 400million MCU were shipped! In depth analysis of the three product lines of Zhaoyi innovation