题解 | #可置位计数器#
可置位计数器
http://www.nowcoder.com/practice/b96def986e29475e8100c213178b73a8
从题目的波形图可以看到,计数器输出number延迟一个时钟周期才输出,因此加一个中间计数器num。
完整代码
`timescale 1ns/1ns
module count_module(
input clk,
input rst_n,
input set,
input [3:0] set_num,
output reg [3:0]number,
output reg zero
);
reg[3:0] num;
always@(negedge rst_n or posedge clk)begin
if(~rst_n)
num <= 0;
else
num <= set==1? set_num: num==15? 0: num+1;
end
always@(negedge rst_n or posedge clk)begin
if(~rst_n)
zero <= 0;
else
zero <= num==0? 1: 0;
end
always@(negedge rst_n or posedge clk)begin
if(~rst_n)
number <= 0;
else
number <= num;
end
endmodule