Home / Learn Chip Programming / Sequential Logic: Flip-Flops, Registers, Counters

Sequential Logic: Flip-Flops, Registers, Counters

Clocked circuits: always @(posedge clk) with non-blocking assignments.

In shortClocked circuits: always @(posedge clk) with non-blocking assignments.

Theory in plain words

Registerclk rising edgerst? q <= 0else q <= doutput q holds until next edge
Register

What it is

Registers update on a clock edge. In always @(posedge clk) use non-blocking (<=) assignments. Add synchronous reset. Counters, shift registers and state registers are all variations.

Why we use it

All memory, timing and CPUs are built from flip-flops. Mixing = and <= is the classic mistake.

How it works

Template: always @(posedge clk) if (rst) q <= 0; else if (en) q <= d; Counters add one each enable; clock dividers use a counter and compare.

Where it is used

  • Counters, clock dividers, PWM
  • Shift registers, UART
  • Any CPU register

Counter and clock divider

Count clock edgesTry it Yourself

Bit n of a free-running counter toggles at clk / 2^(n+1): the simplest clock divider.

Blocking vs non-blocking
always @(posedge clk) begin          // CORRECT for registers
    a <= b;
    b <= a;                          // swap works: right-hand sides use OLD values
end
always @(*) begin                    // CORRECT for combinational logic
    y = a & b;                       // blocking, evaluated in order
end
RuleWhy
Use <= in clocked always blocksall registers update together
Use = in combinational always @(*)order inside the block matters
Never mix both in one blockcauses simulation/synthesis mismatch
One always block drives each regmultiple drivers are a design error
Prefer synchronous reset on FPGAmaps to flip-flop set/reset cleanly

Quick quiz

1. Assignment type in always @(posedge clk)?

2. Which bit of a counter divides the clock by 8?

Common questions

Assignment type in always @(posedge clk)?

<= non-blocking. Registers update together.

Which bit of a counter divides the clock by 8?

bit 2. bit n gives clk / 2^(n+1).