06Implementation
Wrap a counter after nine
Problem
Intended behavior. decade_counter is a clocked counter with a synchronous,
active-high reset. On every rising edge with reset low it advances one step
through 0, 1, ..., 9, and after 9 it returns to 0. While reset is high at a
rising edge the count becomes 0.
Current behavior. The starter is the plain 4-bit counter from the Testbench chapter. It compiles and its reset works, but after 9 it keeps going to 10, 11, and 12, as the public output shows from cycle 10 on.
Your task. Edit the always block at the TODO so that the counter wraps
after 9. Keep the ports, the reset behavior, and the public testbench unchanged.
Read the testbench first
The testbench below the marker generates a 10-unit clock, holds reset high
until time 12, and prints count on each later rising edge. Because the print
happens at the edge, each line shows the value the register held just before
that edge updates it. This is the same EDGE-line view as in the counter-trace example
of the Testbench chapter. Before editing, predict cycles 0 through 12 for a
correct decade counter, and mark the cycle where the wrap must first appear.
Make your change at the TODO, run, and compare the printed trace with your
prediction line by line. Then explain why the trace shows the wrap one line
after the edge that performed it.
Completion check
- Starting from reset, the sequence is 0 through 9 and then 0 again, repeating for as many periods as the clock keeps running.
- The value never reaches 10 or above.
- Raising
resetin the middle of a period returns the count to 0 on the next rising edge, not earlier, and counting resumes from 0 after reset is released.
Progressive hints
Hint 1
A 4-bit register naturally wraps after 15. Stopping at 9 is a decision the design must make on the edge where the current value is 9.
Hint 2
The if/else chain already chooses between reset and increment. A third
choice belongs between those two, and reset must stay first.
Hint 3
Compare count with the last value you want to see, not with the first value
you want to avoid. Comparing with 10 lets 10 appear for one cycle.
Transfer experiment
Predict the public output if the wrap point were 5 instead of 9. Then list which cycles of a 13-line trace would show 0, and explain why the count of printed zeros grows as the wrap point shrinks.