A down counter counts numbers backward, from a higher number down to zero. It helps track time or events in reverse order.
Down counter design in Verilog
module down_counter(
input wire clk,
input wire reset,
output reg [3:0] count
);
always @(posedge clk or posedge reset) begin
if (reset) begin
count <= 4'b1111; // start from 15
end else if (count > 0) begin
count <= count - 1;
end
end
endmoduleclk is the clock signal that triggers counting.
reset sets the counter back to the start value.
count <= 4'b1111; // start from 15
if (count > 0) count <= count - 1;
always @(posedge clk) begin
count <= count - 1;
endThis program defines a 4-bit down counter starting at 15. The testbench toggles the clock and releases reset after 10 time units. The count decreases every clock cycle until it reaches zero.
module down_counter(
input wire clk,
input wire reset,
output reg [3:0] count
);
always @(posedge clk or posedge reset) begin
if (reset) begin
count <= 4'b1111; // start from 15
end else if (count > 0) begin
count <= count - 1;
end
end
endmodule
// Testbench
module testbench();
reg clk = 0;
reg reset = 1;
wire [3:0] count;
down_counter dc(.clk(clk), .reset(reset), .count(count));
always #5 clk = ~clk; // clock toggles every 5 time units
initial begin
$monitor($time, " Count = %d", count);
#10 reset = 0; // release reset after 10 time units
#150 $finish;
end
endmoduleThe counter stops at zero and does not go negative.
Reset is asynchronous and sets the count immediately.
Clock signal controls when the count changes.
A down counter counts backward from a start number to zero.
Use a clock and reset to control counting.
Check count value to stop counting at zero.
Practice
What is the main function of a down counter in Verilog?
Solution
Step 1: Understand the counting direction
A down counter decreases its count value each clock cycle, moving backward.Step 2: Compare with other options
Counting forward is for up counters, clock generation and reset are different functions.Final Answer:
Counts backward from a start value to zero -> Option AQuick Check:
Down counter = counts backward [OK]
- Confusing down counter with up counter
- Thinking it generates clock signals
- Assuming it resets the system
Which of the following is the correct Verilog syntax to declare a 4-bit down counter register?
?Solution
Step 1: Identify correct register declaration
In Verilog, a 4-bit register is declared as reg [3:0] name; where 3:0 means 4 bits.Step 2: Check other options
wire is for connections, reg counter[3:0] is an array of 4 registers, integer is 32-bit signed.Final Answer:
reg [3:0] counter; -> Option BQuick Check:
4-bit reg = reg [3:0] name [OK]
- Using wire instead of reg for storage
- Declaring array instead of vector
- Using integer for fixed bit width
Given this Verilog code snippet for a 3-bit down counter starting at 5, what is the value of count after 3 clock cycles?
reg [2:0] count = 3'd5;
always @(posedge clk) begin
if (count > 0)
count <= count - 1;
endSolution
Step 1: Trace the count value each clock
Start at 5, after 1st clock: 4, 2nd clock: 3, 3rd clock: 2.Step 2: Confirm counting stops only at zero
Since count > 0 condition is true, it decrements each cycle until zero.Final Answer:
2 -> Option DQuick Check:
5 - 3 = 2 after 3 clocks [OK]
- Not decrementing count on each clock
- Stopping count too early
- Confusing initial value with current
Identify the error in this down counter code and select the fix:
reg [3:0] count = 4'd10;
always @(posedge clk or posedge reset) begin
if (reset)
count <= 4'd0;
else if (count >= 0)
count <= count - 1;
endSolution
Step 1: Analyze the condition for decrement
Conditioncount >= 0is always true for unsigned, causing underflow.Step 2: Correct condition to prevent underflow
Usecount > 0to stop decrement at zero and avoid wrap-around.Final Answer:
Changecount >= 0tocount > 0-> Option CQuick Check:
Prevent underflow by checking count > 0 [OK]
- Using >= 0 causes counter to wrap
- Ignoring reset sensitivity
- Incrementing instead of decrementing
You want to design a 4-bit down counter that stops at zero and asserts an output done signal when zero is reached. Which Verilog code snippet correctly implements this behavior?
?Solution
Step 1: Check initialization and reset behavior
Count starts at 10, done is 0, reset sets count and done correctly.Step 2: Verify counting and done signal logic
Count decrements only if > 0, done set to 1 when count reaches zero, stopping count.Step 3: Compare with other options
reg [3:0] count = 4'd10; reg done = 0; always @(posedge clk) begin if (count >= 0) begin count <= count - 1; done <= (count == 0); end end allows underflow. Other options either start count at 0 or use >= 0 causing wrap-around.Final Answer:
A -> Option AQuick Check:
Stop at zero and assert done = 1 [OK]
- Allowing count to underflow below zero
- Incorrect done signal timing
- Wrong initial count value
Ready to prove it?
Pass the test and this pattern moves unknown
