Modulo-N, Up/Down and Loadable Counters
Give a counter a clear, a load and an enable in a fixed order. Make it wrap at any N, count both ways and chain into wider counters.
You Will Be Able To
- Write a counter with clear, load and enable in a stated priority.
- Make a counter wrap at N and drive a terminal count.
- Add up/down control with the right terminal count for each direction.
- Cascade counters with a ripple carry out or with lookahead enables.
- Lesson
- G6.5
- Time
- 25 min
- Runs on
- Your browser
- Check
- 5 questions
A clock face shows 1 to 12, not 0 to 15. Real counters stop short, count backwards, jump to a loaded value and pass a carry to the next digit. Each feature is one more line in a fixed order.
- Cycle 1
After the clear, load 9 wins over counting.
- Cycle 4
q is 11 going up, so tc is 1: the edge wraps to 0.
- Cycle 5
en is 0: the counter holds 0, and tc stays 0.
- Cycle 6
Going down from 0, tc is 1 again: this edge wraps to 11.
- Cycle 8
clear and load together: clear wins.
Measured: the lab prints these values.
Clear, Load and Enable: Their Priority
A counter usually has three controls. A clear forces 0. A load copies an input value. An enable lets it count. When two arrive together, the order of the if chain decides.
| Priority | Control at the edge | q becomes |
|---|---|---|
| 1 | clr = 1 | 0 |
| 2 | load = 1 | din, even when en = 0 |
| 3 | en = 1 | the next count |
| none | q (hold) |
The order of the 74x163 counter chip: clear, then load, then count. A spec can choose another order; the code must match it.
Terminal Count and Modulo-N
A modulo-N counter counts 0 to N - 1 and wraps to 0. The last value, N - 1, is the terminal count. A tc output marks the cycle in which the next enabled edge wraps.
wire last = (q == N - 1);
assign tc = en & last; // the next edge wraps
always_ff @(posedge clk)
if (en) q <= last ? 0 : q + 1;Use ceil(log2 N) bits at least. A modulo-12 counter needs 4 bits. The values 12 to 15 are never reached.
Up/Down Control
An up input picks the direction. Each direction has its own last value: N - 1 going up, 0 going down. The terminal count must follow the direction.
Going up
last = (q == 11);
q <= last ? 0 : q + 1;
Wraps from 11 to 0. tc is 1 at 11.
Going down
last = (q == 0);
q <= last ? 11 : q - 1;
Wraps from 0 to 11. tc is 1 at 0.
The lab does both in one block: last is chosen by up.
Cascading Counters
Two counters make a wider one. The low counter's terminal count enables the high counter, so the high digit moves once per wrap of the low digit.
- Control / FSM
- Storage
- Control
- Ripple carry out
Each stage ANDs the tc of the stage below with its own last value. The enable of stage k passes through k AND gates: a long path for many stages.
- Lookahead enables
Instead, en2 = en · last0 · last1 in one wide AND. Every stage gets its enable after one gate, whatever its position.
Read the diagram as text
- en: count enable
- Stage 0: tc0 = en · last0
- Stage 1: tc1 = tc0 · last1
- Stage 2: en2 = tc1
- en → Stage 0
- Stage 0 → Stage 1: tc0
- Stage 1 → Stage 2: tc1
The 74x163 has two enables for this reason: one rippled from the stage below, one shared by all stages.
Asked in interviews
How do you make a counter that counts 0 to 9?
Use a 4-bit counter that goes back to 0 on the edge that leaves 9. It never shows 10. Its terminal count, high while it is enabled and at its last value, can enable the next digit.
Why does a counter need a terminal count output?
To cascade: the next stage counts only in the cycle where this stage wraps. It also marks the end of a period for timers and dividers.
What is the difference between a synchronous and an asynchronous clear on a counter?
A synchronous clear acts at the clock edge. An asynchronous clear acts at once; used to make a modulo-N counter, it shows the value N for a moment, a glitch.
Try It
Each line is one edge: the controls, q and tc before the edge, then q after it.
Going up, tc is 1 at 11 and the edge wraps to 0. Going down, tc is 1 at 0 and the edge wraps to 11. With en at 0, tc stays 0.
Swap the clear and load lines and run again: the last edge then loads 5. Press Synthesize to see the counter's cells.
Synthesize the design to measure its cells and inspect the mapped gates.
clear q=x tc=0 -> q=0 load 9 q=0 tc=0 -> q=9 up q=9 tc=0 -> q=10 up q=10 tc=0 -> q=11 up (wrap) q=11 tc=1 -> q=0 hold q=0 tc=0 -> q=0 down (wrap) q=0 tc=1 -> q=11 down q=11 tc=0 -> q=10 clear+load q=10 tc=0 -> q=0Original design · 34 cells · 4 flip-flops · 0 latches · 7 logic levels. Synthesis uses Yosys; counts depend on the toolchain.
Read included file: tb.sv
// tb.sv: one rising edge per line; the inputs, q and tc before the edge, then q after it (read-only).
`timescale 1ns/1ns
module tb;
logic clk = 0, clr = 0, load = 0, en = 0, up = 1;
logic [3:0] din = 0;
wire [3:0] q;
wire tc;
hours12 dut (.clk(clk), .clr(clr), .load(load), .en(en), .up(up), .din(din), .q(q), .tc(tc));
task automatic tick(input logic c, l, e, u, input logic [3:0] d, input string what);
clr = c; load = l; en = e; up = u; din = d;
#4 $write("%-11s q=%0d tc=%b", what, q, tc);
#1 clk = 1; #5 clk = 0;
$display(" -> q=%0d", q);
endtask
initial begin
tick(1, 0, 0, 1, 0, "clear");
tick(0, 1, 1, 1, 9, "load 9");
tick(0, 0, 1, 1, 0, "up");
tick(0, 0, 1, 1, 0, "up");
tick(0, 0, 1, 1, 0, "up (wrap)");
tick(0, 0, 0, 1, 0, "hold");
tick(0, 0, 1, 0, 0, "down (wrap)");
tick(0, 0, 1, 0, 0, "down");
tick(1, 1, 1, 1, 5, "clear+load");
end
endmodule
Check Yourself
Common Mistakes
Wrong Compare with N to wrap a modulo-N counter.
The count then reaches N for one cycle before it wraps. A modulo-12 counter would show 12.
Fix Wrap when the count is N - 1: the edge that leaves N - 1 loads 0.
Wrong Register the terminal count, then use the registered copy to wrap.
It arrives one cycle late, so the counter shows N for one cycle before it wraps.
Fix Make tc combinational from the count and the enable, as in the lab.
Wrong Leave the enable out of the terminal count.
A stalled counter sitting at N - 1 then keeps tc high, and the next stage counts every cycle.
Fix tc = en AND (count = N - 1).
Wrong Use a counter too narrow for N - 1.
A modulo-20 counter needs 19, which takes 5 bits. In 4 bits the literal 19 becomes 3.
Fix Use at least ceil(log2 N) bits, and size the literals to match.
Remember
- The order of the if chain is the priority: clear, then load, then count, in the 74x163 style.
- A modulo-N counter wraps on N - 1; tc = en AND (count = N - 1).
- Up/down counters need a last value for each direction: N - 1 going up, 0 going down.
- Cascade by enabling the next stage with tc; lookahead enables keep wide counters fast.