Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutContactPrivacyTerms
Skip to lesson

Course contents

  • RTL Foundations36
  • Combinational Design25
  • Sequential Design27
    • Flip-Flops and Registers
    • Counters and Timers
      • Counting as Controlled State Evolution
      • Up/Down Counter with Reload
      • A Prescaler Feeding a Timer Tick
      • Watchdog Timer Example
      • A Gray-Code Counter
      • Mini Project: a Programmable Timer
    • Shift Registers and LFSRs
    • Pipelining
    • Stalls, Bubbles and Hazards
    • Retiming
  • FSM & Control27
  • Timing & Clocking39
  • Reset & CDC34
  • Memory & Buses57
  • Advanced RTL28
  • Senior & Interview76
  • Glossary and Notation
Loading lesson

‹Counters and Timers
  1. Topics
  2. RTL Design
  3. Sequential Design
  4. Counters and Timers
  5. A Prescaler Feeding a Timer Tick

Part 3 · Sequential · Intermediate

A Prescaler Feeding a Timer Tick

Slow timing from a fast clock with a tick enable, plus a timer built on top of it.

On this page

    Why this matters

    A divided clock needs generated-clock constraints and clock-domain checks. A one-cycle tick enable on the system clock gives the same slow rate with none of that.

    After this lesson you can: build a prescaler and timer from a tick enable, predict which edge produces and which consumes a tick, and size the counter for a given interval.

    Slow events, one fast clock

    Slow functions run far below the system clock, such as a millisecond timebase from megahertz clocks. A tick enable marks one cycle in every N, and slow logic acts only then, all on the same clock.

    The bottom row, tick, pulses high once per DIV cycles.

    • Slots 1–2: tick is low.

    • Slot 3: tick is high for one cycle.

    • Slots 4–6: tick is low.

    • Slot 7: tick is high for one cycle.

    • Slots 8–10: tick is low.

    • Slot 11: tick is high for one cycle.

    • Slots 12–14: tick is low.

    Prescaler and timer

    The prescaler counts DIV cycles and registers its tick so the pulse is glitch-free. The timer counts N such ticks after start, then pulses expired.

    DIV must be at least 2 and N at least 1: a one-cycle prescaler needs no counter, and a zero-tick timer has nothing to count. Both modules report a time-zero simulation error otherwise (an initial check at time zero, not an elaboration-time diagnostic).

    The full code sits under Testbench below.

    Producing a tick and consuming it are different edges

    Predict when the timer first counts a tick. Number the first rising edge after reset release as edge 0. For the code below, DIV=4 and the comparison is c == DIV-2: a tick is produced after edge 2 and consumed at edge 3. Both sequential blocks read old values before NBA updates. The waveform above shows the periodic pulse shape; its slots are not these numbered post-reset edges.

    Edge

    Old c -> new c

    Tick before -> after

    Timer action (start=0, running)

    0

    0 -> 1

    0 -> 0

    No accepted tick

    1

    1 -> 2

    0 -> 0

    No accepted tick

    2

    2 -> 3

    0 -> 1

    No accepted tick; new tick is not yet visible to timer

    3

    3 -> 0

    1 -> 0

    Accept tick 1

    6

    2 -> 3

    0 -> 1

    Produce next tick

    7

    3 -> 0

    1 -> 0

    Accept tick 2

    start wins over a tick on the same edge, including a restart while running: it clears the interval and that coincident tick does not count. With N=3, starts at edges 0, 1, 2, 3 expire after 11, 10, 9, 12 further rising edges respectively. Each expiry follows exactly three accepted ticks; elapsed duration depends on start phase. expired is high for one fast-clock cycle, then clears even if the timer is idle.

    Why a tick enable beats a divided clock

    Tick enable (one clock)

    Divided clock

    Clock domains

    One

    A new clock: needs clock routing and constraints; related clocks can be timed together

    Timing analysis

    Ordinary single-clock paths

    Generated clock, extra checks

    Glitches

    Registered pulse, none

    A divider output used as a clock must be glitch-free

    Test/scan

    Normal

    Divided clock needs special handling

    Flexibility

    Change DIV without touching the clock network

    Re-plan clocking

    Common mistakes

    Warning

    A divided clock needs suitable clock routing and a generated-clock constraint describing its relationship to the source. If that relationship is known and bounded, crossings between the two clocks can be checked by setup/hold timing analysis without asynchronous synchronizers. If the clocks are unrelated, their phase relationship is not guaranteed, or timing checks are intentionally cut, use an appropriate CDC protocol; independently synchronizing bus bits does not preserve a word. Even between related clocks, the transfer protocol must account for different rates so items are not lost or duplicated. A tick enable keeps these timing functions on one clock.

    Interview

    “Divide the clock by 3” starts with a question: clock output or slower enable. A true clock output, for an external pin, comes from a counter with a registered, glitch-free output.

    Check yourself A 100 MHz clock must produce a 1 µs tick. What is DIV, and how many bits does the prescaler counter need?

    Period of the clock = 10 ns, so DIV = 1 µs / 10 ns = 100. $clog2(100) = 7 bits.

    Testbench

    The bounded test uses DIV=4, N=3. It checks 40 cycles of tick production/consumption, every start phase, start coincident with tick, and restart while running on a tick. An independent cycle-index reference checks spacing and pulse width every edge; a separate accepted-tick model predicts exact expiry. Inputs change in the low phase, the old tick is sampled at the rising edge, and outputs are checked 1 ns later after NBA updates. These are functional checks, not hardware timing measurements.

    tick_gen.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    module tick_gen #(parameter int DIV = 4) (   // DIV must be at least 2
      input  logic clk, rst_n,
      output logic tick                       // one-cycle pulse every DIV cycles
    );
      logic [$clog2(DIV)-1:0] c;
      initial begin
        if (DIV < 2) $error("tick_gen: DIV must be at least 2");
      end
      always_ff @(posedge clk or negedge rst_n)
        if (!rst_n) begin c <= '0; tick <= 1'b0; end
        else begin
          tick <= (c == DIV - 2);             // registered, so tick is glitch-free
          c    <= (c == DIV - 1) ? '0 : c + 1'b1;
        end
    endmodule
    
    module timer #(parameter int N = 3) (   // N must be at least 1
      input  logic clk, rst_n, tick, start,
      output logic expired                    // pulses after N ticks
    );
      initial begin
        if (N < 1) $error("timer: N must be at least 1");
      end
      logic [$clog2(N+1)-1:0] n;
      logic running;
      always_ff @(posedge clk or negedge rst_n)
        if (!rst_n) begin n <= '0; running <= 1'b0; expired <= 1'b0; end
        else begin
          expired <= 1'b0;
          if (start) begin n <= '0; running <= 1'b1; end
          else if (running && tick) begin
            if (n == N - 1) begin running <= 1'b0; expired <= 1'b1; end
            else            n <= n + 1'b1;
          end
        end
    endmodule
    

    Remember

    • Slow logic acts only when tick is high, all on the same clock.

    • A tick is produced after one edge and consumed at the next, because both blocks read old values.

    • start wins over a coincident tick; the elapsed time to expiry depends on the start phase.

    Rule of thumb: prefer a tick enable to a divided clock unless a real clock output is needed.

    Keep going

    • Code lab: Timer with prescaler

    • Interview: run a mock interview on this topic

    Related topics

    • Watchdog timer

    • Clock gating

    • Mini project: a programmable timer

    Previous

    69 / 350

    Next: Watchdog Timer Example

    Up next

    Up nextWatchdog Timer Example

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Counting as Controlled State Evolution
    • A Gray-Code Counter
    • Mini Project: a Programmable Timer
    Tick every 4 cycles (DIV = 4)clktick
    A one-cycle-wide tick once per DIV clock cycles. Logic in the slow domain updates only when tick is high.