题解 | #加减计数器#
加减计数器
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; always@(posedge clk or negedge rst_n) begin if(!rst_n) begin num <= 'd0; end else begin if(mode) begin num <= (num=='d9)?'d0:(num + 'd1); end else begin num <= (num=='d0)?'d9:(num - 'd1); end end end always@(posedge clk or negedge rst_n) begin if(!rst_n) begin number <= 'd0; end else begin number <= num; end end always@(posedge clk or negedge rst_n) begin if(!rst_n) begin zero <= 1'b0; end else begin zero <= (num=='d0); end end endmodule