Digital Circuits Cheatsheet
Sequential Circuits and FSMs
Use this Digital Circuits reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Overview
A sequential circuit has outputs that depend on both current inputs and past history (stored state). The state is held in flip-flops. The most general sequential circuit model is a Finite State Machine (FSM), the same state-machine idea you will see in parsers, UI flows, network protocols, and other software engineering systems.
Sequential Circuit Model
Inputs ─►┌──────────────────┐
│ Next-State │
│ Logic (comb.) │──► Next State
State ──►└──────────────────┘ │
▲ │
│ ┌──────┐ │
└──────────────┤ FFs │◄──────────┘
└──────┘
│
┌────────────────────┐
│ Output Logic │──► Outputs
│ (comb.) │
└────────────────────┘- State register: holds current state (flip-flops)
- Next-state logic: combinational; computes next state from current state + inputs
- Output logic: combinational; computes outputs
Mealy vs Moore Machines
| Property | Mealy | Moore |
|---|---|---|
| Outputs depend on | State + Inputs | State only |
| Output timing | Can change mid-cycle | Changes only at clock edge |
| Number of states (typical) | Fewer | More |
| Output circuit | Next-state logic feeds output | Output block from state only |
| Response speed | One cycle earlier | One cycle later |
| Hazard risk | Higher (async. output glitches) | Lower |
Mealy output equation: Z = f(Q, X) Moore output equation: Z = g(Q)
FSM Design Procedure
- Understand the specification — describe behavior as a state diagram.
- State diagram → state table — enumerate all states, inputs, and transitions.
- State encoding — assign binary codes to states.
- Next-state logic — K-map or Boolean minimization for each FF input.
- Output logic — derive output equations.
- Implementation — select FF type, draw circuit.
State Diagram Notation
input/output (Mealy) State A ────────────────────► State B input (Moore: output labeled in state circle)
- Circle: state (Moore: output written inside or below)
- Arrow: transition
- Label: input condition / output (Mealy) or just input (Moore)
- Arrow-in (from nowhere): initial/reset state
- Double circle: accepting/final state (automata-theory convention; hardware FSMs rarely use it)
State Table Format
Mealy Machine State Table
| Current State | Input | Next State | Output |
|---|---|---|---|
| S₀ | 0 | S₀ | 0 |
| S₀ | 1 | S₁ | 0 |
| S₁ | 0 | S₀ | 0 |
| S₁ | 1 | S₂ | 1 |
| S₂ | 0 | S₀ | 0 |
| S₂ | 1 | S₂ | 1 |
Moore Machine State Table
| Current State | Output | Input=0 → Next | Input=1 → Next |
|---|---|---|---|
| S₀ | 0 | S₀ | S₁ |
| S₁ | 0 | S₀ | S₂ |
| S₂ | 1 | S₀ | S₂ |
State Encoding
| Encoding | Description | States needed | Pros | Cons |
|---|---|---|---|---|
| Binary | Compact binary codes | ⌈log₂ n⌉ FFs | Fewest FFs | More complex next-state logic |
| One-hot | One FF per state, exactly one=1 | n FFs | Simplest logic, fast | Many FFs (fine in FPGAs) |
| Gray code | Adjacent states differ 1 bit | ⌈log₂ n⌉ FFs | Fewer glitches | Moderate logic |
FPGAs typically use one-hot encoding because flip-flops are plentiful and LUT-based logic favors it.
Worked Example: Sequence Detector (101)
Detect the pattern 1→0→1 in a serial input stream. Overlapping allowed.
State Diagram (Mealy)
0/0 1/0 ┌───┐ ┌───┐ ▼ │ ▼ │ ──► S0 ──┘ ────1/0────► S1 ──┘ ▲ ▲ │ │ 1/1│ │0/0 │ 0/0 │ ▼ └────────────────── S2
On input 1 in S2 the pattern is complete: output 1 and go to S1, because the trailing 1 already starts the next match (overlapping detection).
States: - S0 — reset / waiting for first 1 - S1 — received 1 - S2 — received 10
Transitions: | State | Input | Next | Output | |-------|-------|------|--------| | S0 | 0 | S0 | 0 | | S0 | 1 | S1 | 0 | | S1 | 0 | S2 | 0 | | S1 | 1 | S1 | 0 | | S2 | 0 | S0 | 0 | | S2 | 1 | S1 | 1 |
Binary encoding: S0=00, S1=01, S2=10 (need 2 FFs: Q₁Q₀). The unused state 11 is a don't-care in every K-map below.
Next-state logic (D FF): D₁ = Q₁′·Q₀·X′ → with the 11 don't-care: D₁ = Q₀·X′ (in S1, input 0 → S2) D₀ = Q₁′·Q₀′·X + Q₁′·Q₀·X + Q₁·Q₀′·X = X·(Q₁′ + Q₀′) → with the 11 don't-care: D₀ = X (every input 1 leads to a state with Q₀=1)
Output: Z = Q₁·Q₀′·X → with the 11 don't-care: Z = Q₁·X (in state S2, input 1)
State Minimization
Equivalent states: two states are equivalent if for every input sequence, they produce the same output sequence. Equivalent states can be merged.
Implication table method: 1. List all pairs of states. 2. Mark pairs with different outputs as distinguishable. 3. For each pair, check if their transitions lead to distinguishable pairs → mark as distinguishable. 4. Repeat until no new markings. 5. Unmarked pairs are equivalent → merge.
Timing in Sequential Circuits
Clock period T ≥ t_cq + t_logic + t_setup
| Symbol | Meaning |
|---|---|
| t_cq | Clock-to-Q delay of flip-flop |
| t_logic | Propagation delay of combinational next-state logic |
| t_setup | Setup time of flip-flop |
| t_hold | Hold time (must be satisfied too) |
Hold time constraint: t_cq + t_logic_min ≥ t_hold
Violation causes hold-time failure (data changes too soon after clock edge) — a wiring/delay problem, cannot be fixed by slowing the clock.
Synchronous vs Asynchronous Sequential Circuits
| Property | Synchronous | Asynchronous |
|---|---|---|
| State changes | Only on clock edge | Any time input changes |
| Analysis | Straightforward (clock-by-clock) | Complex (hazards, races) |
| Speed | Limited by clock | Can be faster |
| Design difficulty | Easier | Much harder |
| Hazard risk | Low (clock isolates transitions) | High (critical races, glitches) |
Virtually all modern digital systems are synchronous. Asynchronous design is specialized (handshake circuits, clock-domain crossing).
Hazards in Sequential Circuits
Critical race: two state variables change simultaneously; the order of changes affects which final state is reached.
Non-critical race: multiple variables change but all orderings lead to the same final state.
Cycle: the circuit cycles through a series of unstable states before settling (hazard).
Elimination: use one-hot encoding or add delays/feedback to resolve races.
Common FSM Patterns
| Pattern | States needed | Use |
|---|---|---|
| Edge detector | 2 | Detect 0→1 or 1→0 transition |
| Sequence recognizer | Length of pattern | Match bit sequences |
| Timing controller | n | Generate n-cycle signals |
| Traffic light | 4–6 | Multi-phase timer |
| Vending machine | Number of coin combos | Accumulate credit |
FSM in Hardware Description
// Mealy 101 detector — the worked example above module seq101 ( input wire clk, input wire rst, // synchronous, active-high input wire x, output reg z ); localparam [1:0] S0 = 2'b00, S1 = 2'b01, S2 = 2'b10; reg [1:0] state, next_state; always @(posedge clk) begin // state register if (rst) state <= S0; else state <= next_state; end always @(*) begin // next-state + output logic next_state = S0; z = 1'b0; case (state) S0: next_state = x ? S1 : S0; S1: next_state = x ? S1 : S2; S2: begin next_state = x ? S1 : S0; z = x; // 101 detected (Mealy output) end default: next_state = S0; // recover from illegal state endcase end endmodule
Separate the state register (clocked) from combinational logic (always @(*), with defaults assigned before the case) for clean synthesis and no inferred latches.