1、Build a counter that counts from 0 to 999, inclusive, with a period of 1000 cycles. The reset input is synchronous, and should reset the counter to 0.
构建一个计数范围为0到999(含0到999)的计数器,周期为1000个周期。重置输入是同步的,应将计数器重置为0。
思路:
分别设置个位、十位、百位的三个变量one\ten\hun进行计数,到9进1。
如果直接q+1来计周期,不会符合十进制的进位标准。
最终q= one + ten*10 +hun*100;
module top_module (
input clk,
input reset,
output [9:0] q);
reg [3:0] one,ten,hun;
always @(posedge clk)
begin
if(reset) begin one<=0;ten<=0;hun<=0; end
else
begin
if(hun==4'd9&&ten==4'd9&&one==4'd9) begin hun<=0; ten<=0;one<=0; end
else if(ten==4'd9&&one==4'd9) begin hun<=hun+1;ten<=0;one<=0; end
else if(one==4'd9) begin one<=0; ten<=ten+1; end
else one<=one+1;
end
end
assign q = one + ten*10 +hun*100;
endmodule
理理思路还是比较简单的
冲冲冲