题解 | #加减计数器#

加减计数器

https://www.nowcoder.com/practice/9d50eb2addaf4a37b7cd5a5ee7b297f6

`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_tmp;
always @ (posedge clk or negedge rst_n) 
begin
    if(~rst_n) begin
        num_tmp <= 4'b0;
    end 
    else begin
        if(mode == 1'b1) begin
            if(num_tmp == 4'd9)
                num_tmp <= 4'd0;
            else 
                num_tmp <= num_tmp + 4'd1;
        end 
        else begin
            if(num_tmp == 4'd0)
                num_tmp <= 4'd9;
            else
                num_tmp <= num_tmp - 4'd1;
        end 
    end 
end 

always @ (posedge clk or negedge rst_n) 
begin
    if(~rst_n) begin
        number <= 4'b0;
    end 
    else begin
        number <= num_tmp;
    end 
end 

always @ (posedge clk or negedge rst_n) 
begin
    if(~rst_n) begin
        zero <= 1'b0;
    end 
    else begin
        zero <= (num_tmp == 4'b0);
    end 
end 
endmodule

全部评论

相关推荐

11-27 12:36
已编辑
门头沟学院 前端工程师
Apries:这个阶段来说,很厉害很厉害了,不过写的简历确实不是很行,优势删掉吧,其他的还行
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务