Build sequential circuit

From pjt222/agent-almanac · View interactive version

Build sequential circuit is an AI agent skill from the GitHub repository pjt222/agent-almanac. Build sequential (stateful) logic circuits including latches, flip-flops, registers, counters, and finite state machines. Covers SR latch, D and JK flip-flops, binary/BCD/ring counters, and Mealy/Moore FSM design with clock signal and timing analysis. Use when a circuit must remember past inputs, co

How to use this skill

Skill file

Build Sequential Circuit

Sequential logic circuit design → ID memory + state type, construct state diagram + transition table, derive excitation equations for flip-flop type, impl at gate level w/ flip-flops + combinational logic, verify via timing diagram + state sequence sim.

Use When

In

Do

Step 1: ID Memory + State Reqs

What circuit remembers + how many states:

  1. State enumeration: List all distinct states. Sequence detector: each state = progress through target. Counter: each state = count val.
  2. State encoding: Binary encoding for states.
  1. In/out classification: ID primary ins (external), primary outs, internal state vars (flip-flop outs). Mealy: outs depend on state + in. Moore: outs depend only on state.
  2. Flip-flop type selection:
## State Requirements
- **Number of states**: [N]
- **State encoding**: [binary / one-hot / Gray]
- **Flip-flops needed**: [count and type]
- **Machine type**: [Mealy / Moore]
- **Inputs**: [list with descriptions]
- **Outputs**: [list with descriptions]
- **Reset behavior**: [synchronous / asynchronous / none]

→ Complete state inventory w/ encoding, flip-flop type, machine classified as Mealy or Moore.

If err: State count unclear → enumerate by tracing all possible in sequences up to memory depth. Exceeds practical (>16 states manual) → decompose into smaller interacting FSMs.

Step 2: State Diagram + Transition Table

Formalize behavior:

  1. State diagram: Directed graph:
  1. Transition table: Convert diagram to table w/ cols: present state, in(s), next state, out(s).
  2. Reachability check: From initial/reset, verify all states reachable via some in sequence. Unreachable = design err or treat as don't-cares.
  3. State minimization (optional): Check equivalent states — same out for every in + transition to equivalent next. Merge equivalent → reduce flip-flop count.
## State Transition Table
| Present State | Input | Next State | Output |
|--------------|-------|------------|--------|
| S0           | 0     | S0         | 0      |
| S0           | 1     | S1         | 0      |
| S1           | 0     | S0         | 0      |
| S1           | 1     | S2         | 0      |
| ...          | ...   | ...        | ...    |

- **Unreachable states**: [list, or "none"]
- **Equivalent state pairs**: [list, or "none"]

→ Complete transition table covering every present-state/in combo, all states reachable from initial.

If err: Missing entries → spec incomplete. Return to reqs, resolve ambiguity. Unreachable states → add transitions to reach or remove + reduce encoding.

Step 3: Derive Excitation Equations

Flip-flop in equations from transition table:

  1. Encode states: Replace names w/ binary encoding. Each bit pos = one flip-flop.
  2. Per-flip-flop truth table: Each flip-flop → truth table w/ present-state bits + ins as in cols, required flip-flop in as out col.
  1. Minimize each eq: Use evaluate-boolean-expression (K-map or algebraic simplify) on each flip-flop in fn. Don't-cares from unreachable states + JK X-entries reduce significantly.
  2. Derive out eqs: Moore: each out = fn of present state bits only. Mealy: each out = fn of present state bits + ins.
## Excitation Equations
- **Flip-flop type**: [D / JK / T]
- **State encoding**: [binary assignment table]

| Flip-Flop | Excitation Equation          |
|-----------|------------------------------|
| Q1        | D1 = [minimized expression]  |
| Q0        | D0 = [minimized expression]  |

## Output Equations
| Output | Equation                     |
|--------|------------------------------|
| Y      | [minimized expression]       |

→ Minimized excitation eqs per flip-flop + out eqs per primary out, all don't-cares exploited.

If err: Eqs overly complex → reconsider encoding. Diff encoding (binary → one-hot, or reassigning codes) can dramatically simplify. Try ≥2 encodings, compare literal counts.

Step 4: Impl at Gate Level

Build circuit from flip-flops + combinational gates:

  1. Place flip-flops: One per state bit. Connect all clock ins to system clock. Connect reset ins if spec'd (async reset → directly to CLR/PRE pin; sync reset part of excitation logic).
  2. Build excitation logic: Impl each eq as combinational circuit via design-logic-circuit. Ins = present-state flip-flop outs (Q, Q') + primary ins.
  3. Build out logic: Impl each out eq as combinational. Moore: only state bits. Mealy: state bits + primary ins.
  4. Connect: Wire excitation outs → flip-flop D/JK/T ins. Wire out logic → primary outs.
  5. Init: Circuit reaches known initial state on power-up. Typically async reset forcing all flip-flops to 0 (or encoded initial).
## Circuit Implementation
- **Flip-flops**: [count] x [type], [edge type]-triggered
- **Combinational gates for excitation**: [count and types]
- **Combinational gates for output**: [count and types]
- **Total gate count**: [flip-flops + combinational gates]
- **Reset mechanism**: [asynchronous CLR / synchronous mux / none]

→ Complete gate-level netlist w/ flip-flops, excitation logic, out logic, clock distribution, reset. Every signal has exactly 1 driver.

If err: Feedback outside flip-flops → combinational loop introduced. All feedback in sync circuit must pass through flip-flop. Trace offending path, reroute through register.

Step 5: Verify via Timing + State Sim

Confirm circuit correct across clock cycles:

  1. Test sequence: In sequence exercising every transition ≥1. Sequence detectors: target, partial matches, overlapping, non-matching.
  2. Timing diagram: Each cycle record:
  1. Trace state sequence: Verify matches state diagram Step 2. Every transition follows edge in diagram.
  2. Timing constraints:
  1. Reset verify: Reset drives circuit to initial regardless of current.
## Timing Verification
| Cycle | Clock | Input | Present State | Next State | Output |
|-------|-------|-------|---------------|------------|--------|
| 0     | rst   | -     | -             | S0         | 0      |
| 1     | rise  | 1     | S0            | S1         | 0      |
| 2     | rise  | 1     | S1            | S2         | 0      |
| ...   | ...   | ...   | ...           | ...        | ...    |

- **All transitions match state diagram**: [Yes / No]
- **Setup/hold violations**: [None / list]
- **Reset verified**: [Yes / No]

→ Every cycle matches transition table, outs correct every cycle, no timing violations.

If err: Transition wrong → trace excitation logic for that present-state + in combo. Outs wrong but transitions correct → err in out logic. Circuit enters unintended state → check incomplete reset or missing transitions from unused codes.

Check

Traps

→

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.