「Verilog学习笔记」加减计数器

专栏前言

本专栏的内容主要是记录本人学习Verilog过程中的一些知识点,刷题网站用的是牛客网

「Verilog学习笔记」加减计数器_第1张图片

`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 ; 

	always @ (posedge clk or negedge rst_n) begin 
		if (~rst_n) num <= 0 ; 
		else 
			if (mode) num <= num == 9 ? 0 : num + 1 ; 
			else num <= num == 0 ? 9 : num - 1 ; 
	end

	always @ (posedge clk or negedge rst_n) begin 
		if (~rst_n) number <= 0 ; 
		else number <= num ; // 题目有误 根据答案波形图 将输出打一拍后输出
	end

	always @ (posedge clk or negedge rst_n) begin 
		if (~rst_n) zero <= 0 ; 
		else zero <= num == 0 ; // 同理 当num == 0时 下一拍number才会等于0 根据非阻塞赋值的特性 将会在下一拍输出zero = 1
	end 

endmodule

你可能感兴趣的:(Verilog学习笔记,学习,笔记,Verilog)