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.

The modulo-12 Counter of the LabEach cycle ends at a rising edge. q is the value before the edge; tc is 1 when that edge will wrap.
012345678clkclrloadenupqx09101101110tc
Scroll horizontally ↔
  1. Cycle 1

    After the clear, load 9 wins over counting.

  2. Cycle 4

    q is 11 going up, so tc is 1: the edge wraps to 0.

  3. Cycle 5

    en is 0: the counter holds 0, and tc stays 0.

  4. Cycle 6

    Going down from 0, tc is 1 again: this edge wraps to 11.

  5. 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.

PriorityControl at the edgeq becomes
1clr = 10
2load = 1din, even when en = 0
3en = 1the next count
noneq (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.

Wrap on N - 1, never on N. Gate tc with the enable.
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.

Ripple Carry Out Against Lookahead EnablesThree counter stages. Arrows show the enable path.
  • Control / FSM
  • Storage
  • Control
Ripple carry out against lookahead enablesThree counter stages. Arrows show the enable path. control en count enable storage Stage 0 tc0 = en · last0 storage Stage 1 tc1 = tc0 · last1 storage Stage 2 en2 = tc1 tc0tc1
  1. control encount enable
    • Stage 0
  2. storage Stage 0tc0 = en · last0
    • tc0 → Stage 1
  3. storage Stage 1tc1 = tc0 · last1
    • tc1 → Stage 2
  4. storage Stage 2en2 = tc1
  1. 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.

  2. 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
  1. 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.

  2. 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.

  3. 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.

RTLIcarus Verilog · in your browser · Lint: Verilator service · Synthesize: Yosys service
Edit the design, then Run, Lint or Synthesize
Expected: 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=0

Original 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

Spot the bugEach option is a 4-bit counter. The spec: clear first, then load (even when en = 0), then count up. The bench checks all three. Which block fails?
ConceptHow many flip-flops does a modulo-100 counter need, at least?
ConceptA modulo-10 counter wraps with q <= (q == 10) ? 0 : q + 1;. What does it show?
Predict the outputThe lab counter is cleared, loaded with 3, then counts down for five enabled edges. Predict the exact line.
module tb;
  logic       clk = 0, clr = 0, load = 0, en = 0, up = 0;
  logic [3:0] din = 4'd3;
  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); clr = c; load = l; en = e; #1 clk = 1; #1 clk = 0; endtask
  initial begin
    tick(1, 0, 0); tick(0, 1, 0);
    repeat (5) tick(0, 0, 1);
    $display("q=%0d", q);
  end
endmodule
// hours12: a modulo-12 up/down counter with a synchronous clear, a load and a terminal count.
module hours12 (
  input  logic       clk, clr, load, en, up,
  input  logic [3:0] din,
  output logic [3:0] q,
  output logic       tc     // 1 when the next enabled edge wraps: at 11 going up, at 0 going down
);
  wire last = up ? (q == 4'd11) : (q == 4'd0);
  assign tc = en & last;
  always_ff @(posedge clk)
    if (clr)       q <= 4'd0;                  // 1: clear
    else if (load) q <= din;                   // 2: load
    else if (en)                               // 3: count
      if (up) q <= last ? 4'd0  : q + 4'd1;
      else    q <= last ? 4'd11 : q - 4'd1;
endmodule
ConceptA 32-bit counter is built from eight 4-bit stages with a rippled carry. Why might lookahead enables be needed?

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.

Practice

Report a problem with this page