Count15
Build a 4-bit binary counter that counts from 0 through 15, inclusive, with a period of 16. The reset input is synchronous, and should reset the counter to 0.
module top_module (
input clk,
input reset, // Synchronous active-high reset
output [3:0] q);
always @(posedge clk) begin
if(reset) q <= 4'h0;
else begin
if(q == 4'hf ) q <= 0;
else q <= q + 4'h1;
end
/*
q <= q+1; // Because q is 4 bits, it rolls over from 15 -> 0.
// If you want a counter that counts a range different from 0 to (2^n)-1,
// then you need to add another rule to reset q to 0 when roll-over should occur.
*/
end
endmodule
Count10
Build a decade counter that counts from 0 through 9, inclusive, with a period of 10. The reset input is synchronous, and should reset the counter to 0.
module top_module (
input clk,
input reset, // Synchronous active-high reset
output [3:0] q);
always @(posedge clk) begin
if(reset | q == 4'h9) q <= 4'h0;
else q <= q + 4'h1;
end
endmodule
Count1to10
Make a decade counter that counts 1 through 10, inclusive. The reset input is synchronous, and should reset the counter to 1.
module top_module (
input clk,
input reset,
output [3:0] q);
always @(posedge clk) begin
if(reset | q == 4'ha) q <= 1;
else q <= q + 4'h1;
end
endmodule