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.
Theory in plain words
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
Bit n of a free-running counter toggles at clk / 2^(n+1): the simplest clock divider.
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| Rule | Why |
|---|---|
| Use <= in clocked always blocks | all registers update together |
| Use = in combinational always @(*) | order inside the block matters |
| Never mix both in one block | causes simulation/synthesis mismatch |
| One always block drives each reg | multiple drivers are a design error |
| Prefer synchronous reset on FPGA | maps to flip-flop set/reset cleanly |
Quick quiz
1. Assignment type in always @(posedge clk)?
Registers update together.
2. Which bit of a counter divides the clock by 8?
bit n gives clk / 2^(n+1).
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).