Posts Tagged ‘extract fpga ic locked archive’
Reverse Lattice CPLD Source Code: The Ceiling Is a Netlist
There is a specific disappointment that follows a successful CPLD readout. The fuse map is complete, every bit is where it should be, the checksum closes — and the customer opens the reconstructed file expecting to see their design in HDL and finds a list of product terms instead. The design is not in there. It went away during synthesis.
This is not a limitation of a tool or a matter of effort. Synthesis maps a behavioural description onto a fixed fabric, and the mapping is many-to-one. A hundred different HDL descriptions with the same timing-independent behaviour produce the same fuse map. Recovering “the” source from that map is recovering one of a hundred equally valid answers, and there is no information in the map that says which one was written.
What can be recovered is the netlist: the equations that describe the programmed connections. That is a real and useful deliverable. It is simply not source code, and calling it source code is the source of most disputes in this area.
What synthesis destroys
A synthesis tool performs several transformations, and each one removes information that no reverse step can restore.
| Transformation | Information lost |
|---|---|
| Boolean minimisation | The original expression form, including intentional redundancy |
| Common subexpression extraction | Shared terms that were written separately |
| State machine encoding | The original state names and the chosen encoding |
| Register retiming | The position of pipeline stages |
| Technology mapping | Which logic went into which macrocell |
| Signal renaming | Every identifier |
Two of these deserve emphasis. State encoding is the more damaging: a design with eight named states becomes a three-bit register with arbitrary assignments, and the assignment is an implementation detail that the tool chose. Reporting that the design has states 0 through 7 is accurate and almost useless, because the names carried the meaning.
The recoverable object
What comes back is a set of equations in terms of device pins and internal nodes. Each equation is a sum of products, because that is what the fabric implements. Each macrocell contributes one equation plus its mode and polarity.
“ out1 = (in1 & !in2 & fb1) # (in3 & fb2) # (!in4 & !fb1 & fb2) “
An equation like that is complete and unambiguous. It is also not what the designer wrote, because the designer wrote something at a higher level and the tool decided that this product-term structure was the efficient implementation.
| Level | Recoverable | Useful for |
|---|---|---|
| Fuse map | Yes, exactly | Reproducing the part |
| Equations per macrocell | Yes, with architecture knowledge | Understanding and modifying the logic |
| State machine with named states | No | Nothing, unless inferred separately |
| Original HDL | No | Nothing; a rewrite is needed anyway |
The third row is where optimism usually sits. A state machine can be inferred from the equations — the feedback terms define the next-state function, and the register bits define the state vector. What cannot be recovered is which state the designer called IDLE.
Why recompilation is the acceptance test
Given that the recovered equations are one of many equivalent descriptions, the acceptance criterion cannot be file comparison. It has to be behavioural equivalence, and the practical form of that is: compile the recovered equations back to the same device, and compare the resulting fuse map.
| Test | What it proves | Limitation |
|---|---|---|
| Fuse map identical after recompilation | The reconstruction is exactly equivalent | Depends on using the same tool version and settings |
| Fuse map functionally equivalent | Behaviour is preserved | Requires a formal equivalence check or exhaustive simulation |
| Simulation matches captured vectors | Behaviour matches on tested inputs | Only covers tested cases |
| Board-level test passes | The whole system works | Slow, and confounded by other components |
The first row is achievable more often than people expect, because a synthesis tool given the same equations and the same target tends to produce the same mapping. It is also fragile: a tool version change can alter the mapping while preserving behaviour, and the resulting mismatch is not a failure of the reconstruction.
The second row is the rigorous one. Formal equivalence between two fuse maps on a CPLD is tractable because the state space is small, and it gives a much stronger statement than simulation.
Reconstructing intent, not just logic
An equation-level netlist is enough to reproduce a part and not enough to maintain a design. The gap is intent: why this term exists, what it protects against, which input is a clock and which is an asynchronous override. Building that back requires reading the equations as a description of behaviour rather than as a description of logic.
Some of it can be inferred confidently. A term that combines a chip-select with an address range is a decoder. A term that combines a clock with an enable and feeds a register is a clock gating structure. A term that appears in every output is a global enable. These readings are reliable because they are the only sensible reasons to build the structure.
Where the device is implementing a controller rather than glue, the equations resolve into a state machine, and that reconstruction is a different exercise with its own method: the state vector is the registered macrocell feedback, and the transition conditions are the product terms feeding it. Attacking CPLD source code covers that path, and it is usually the more valuable one when the device is the only copy of a controller design.
Other parts cannot be inferred at all, and the reconstruction should say so rather than invent a story. A redundant term that the minimiser would have removed but is present in the map was put there deliberately, usually to avoid a hazard, and the specific hazard cannot be recovered from the map alone.
Timing is not in the equations
One thing the reconstruction cannot carry is the timing the design was closed against. The equations describe logic; the propagation delay through that logic depends on which macrocells were used, how the product terms were grouped, and how the signal is routed across the interconnect. Re-fitting the same equations with a different tool version can produce a functionally identical map with a different worst-case path.
That matters when the reconstruction is used as the basis for a replacement. A design that met a setup time in the original may fail it after a re-fit even though the logic is identical, and the failure appears only at temperature or supply extremes. The practical mitigation is to record the original timing report if it exists, and to test the re-fitted design at the corners the application actually sees.
Where the input comes from
None of this starts without the configuration pattern, and the access question is treated in reading Lattice CPLD embedded firmware. The distinction between extraction and interpretation — where one stops and the other begins — is set out in extracting Lattice CPLD source code, which is the boundary this page sits on the far side of.
Services at this boundary
Work covers fuse map validation, equation extraction per macrocell, state machine inference with explicit uncertainty, timing reconstruction from the mapped structure, and equivalence checking of a recompiled result. It does not cover producing original HDL, and it does not cover recovering original signal names, state names or comments.
The honest framing is that the ceiling is a netlist, that a netlist is genuinely useful, and that a client who needs HDL needs a rewrite informed by the netlist rather than a recovery of the original.