PostHeaderIcon Attack CPLD Source Code: Recovering a State Machine From a Fuse Map

Many CPLDs on real boards do not implement glue logic. They implement a controller: a state machine that sequences a converter, drives a motor, manages a power rail, or handles a protocol the main processor does not want to service. When such a device is the only copy of a design, the question is not “what logic is in here” but “what sequence does this thing run”, and that question has a good answer.

The answer comes from the fuse map, because a CPLD state machine is fully described by two things: which macrocells are registered and what feeds them. The registered macrocells hold the state vector. The product terms feeding them define the next-state function. The combinational macrocells define the outputs. All of it is in the map.

Why a CPLD is a good target for this

A microcontroller state machine is compiled into instructions, and the state vector lives in RAM or registers whose location the compiler chose. Recovering it requires tracking data flow through the whole program. A CPLD state machine has no such indirection: the state vector is the set of flip-flops, and the flip-flops are physical structures at known locations.

Property Consequence for recovery
State vector is the register set No data-flow analysis needed
Next-state function is combinational logic Expandable into a truth table
No interrupts or stack Every behaviour is a transition
Small state space Exhaustive enumeration is feasible
Fixed clock domain, if synchronous Transitions are unambiguous

The fourth row is the one that makes this tractable rather than merely possible. A controller with twelve state bits has at most four thousand states, and in practice uses a few dozen. Enumerating them exhaustively and checking the transition table for consistency is a mechanical process.

Separating state from logic

Not every registered macrocell is a state bit. Some are pipeline registers, some are synchronisers, some are counters that are not part of the control flow. Telling them apart is the first analytical step, and the distinction is behavioural rather than structural.

Register behaviour Likely role
Changes only in response to a small set of input combinations State bit
Increments monotonically Counter, not state
Copies an input with one cycle delay Synchroniser
Holds a value for many cycles then changes with one input Flag or latch
Changes every cycle regardless of inputs Free-running divider

The second row is a common false positive. A counter that sequences a conversion has a regular structure that looks like a state machine, and treating it as one produces a transition table with dozens of states that are really one state and a count. Recognising the increment pattern in the equations settles it.

Building the transition table

With the state bits identified, the next-state equations can be evaluated for every combination of state and input. The result is a table, and the table is the design.

State (bits) Inputs Next state Outputs
0000 reset asserted 0000 all inactive
0000 start pulse 0001 busy high
0001 conversion complete 0010 busy high, latch low
0010 always 0000 data valid

A table like that is genuinely useful even though the state names are invented. It lets a firmware engineer answer the questions that matter: what happens if the input arrives during state two, what happens if the clock stops in state one, and what is the longest path through the machine. Those are the questions a replacement design has to answer.

The encoding problem, restated

State encoding was chosen by the synthesis tool, not by the designer, so the reconstructed bit patterns do not correspond to anything the original engineer wrote. A design with states named IDLE, ARM, CONVERT and DONE becomes 0000, 0001, 0011 and 0010, or any of a hundred other assignments.

This is a real loss and it is not fatal, because the encoding carries no behaviour. What matters is the graph: which states connect to which, under what conditions, with what outputs. Relabelling the states is a cosmetic step and should be done to make the table readable, not to pretend the names were recovered.

Aspect Recoverable Note
State graph topology Yes, exactly Determined by the equations
Transition conditions Yes, exactly Product terms are explicit
Output values per state Yes, exactly Combinational terms are explicit
Original state names No Invented labels are the honest option
Original encoding No, and irrelevant Any consistent encoding works

When the machine has asynchronous paths

Some CPLD controllers use inputs as asynchronous set or reset on state registers, bypassing the clock. This is common in designs that must respond to a fault faster than a clock cycle, and it produces behaviour that a purely synchronous model cannot represent.

Pattern in the equations Interpretation
Register set term independent of clock Asynchronous set, likely a fault response
Register clear term independent of clock Asynchronous reset
Product term including a register output and an input, no clock Combinational path, potential race
Same term appearing in several next-state equations Shared condition, often a mode select

The third row is where recovered designs earn their value. A combinational path from an input to a state register’s asynchronous control is a race waiting to happen, and it is exactly the kind of thing that a redesign will accidentally fix or accidentally break.

Verification against the real device

A transition table inferred from equations can be checked against the device without any knowledge of the original design. Drive the inputs, observe the outputs, and compare against the table’s prediction. Discrepancies point at either a wrong state-bit assignment or a missing asynchronous path, and both are correctable.

This is the practical answer to the question of whether the reconstruction is right: it predicts behaviour, and the prediction can be tested. Reversing Lattice CPLD source code covers the equation-level reconstruction that this page builds on, and extracting Lattice CPLD source code covers the acquisition step before it. Where the controller works alongside a processor, reading PLD code in an MCU system covers the related question of memory mapping.

Services at this boundary

Work covers state-bit identification, next-state equation extraction, transition table construction, asynchronous path detection, and behavioural verification on the bench. It does not cover recovering original state names or the original encoding, and it does not cover proving that the reconstructed table is the only design consistent with the equations.

The deliverable worth asking for is a transition table plus a list of the assumptions behind it, because the table is checkable and the assumptions are what a redesign needs to know.

Comments are closed.