Posts Tagged ‘extract avr microcontroller encrypt program’

PostHeaderIcon Reverse DSP and CPLD Programs: Two Reverse Problems, Not One

A board carries a DSP and a CPLD. Both need to be reversed. The request is reasonable and the plan it implies usually is not, because the two devices require different disciplines that share only their first step.

Acquisition is common to both: get the bits out. Everything after that diverges. A DSP program is a sequence of instructions with data, and reversing it means recovering semantics: what does this block compute, what protocol does that handler speak, where does this coefficient come from. A CPLD configuration is a structure of logic, and reversing it means recovering connectivity and function: what does this term implement, what is this register for, what does this state machine sequence.

The two problems, side by side

Aspect DSP program CPLD configuration
Stored form Instructions and data Connection and truth-table bits
Unit of meaning Operation Product term
Control flow Explicit, in the instruction stream Implicit, in register feedback
Order matters Yes; execution is sequential Partly; logic is concurrent
Typical deliverable Annotated disassembly, C Equations, state machine
Failure of the reverse Misread instruction semantics Misread macrocell mapping

The fourth row is the fundamental difference and the one that trips people up. A DSP program has a program counter, and the order of execution is a property of the code. A CPLD has no program counter; every enabled path evaluates continuously, and “order” exists only in the sense that a clock edge samples a settled state.

Why DSP reverse is harder than it looks

Modern DSP architectures are not clean RISC machines. They have features that exist to make filtering fast and that make disassembly ambiguous.

Feature Effect on reversal
Delayed branches Instructions after a branch execute before it takes effect
Parallel instruction packets Several operations issued in one cycle, with resource constraints
Circular addressing An address wraps by hardware; linear reading of the code misleads
Specialised addressing modes Post-increment and bit-reversed modes hide the actual index
Multiple accumulators Data flow is wider than a single destination suggests

Circular addressing deserves specific mention because it breaks the most basic assumption of reading code: that an address refers to a fixed location. In a filter loop using a circular buffer, the same instruction touches different memory on each iteration, and the buffer length is a register value that may itself be computed. Reading the code without modelling the buffer produces conclusions that are wrong in a way that looks right.

Why CPLD reverse is harder than it looks

The CPLD side has the opposite problem: it is easy to decode and hard to interpret. Decoding is mechanical once the architecture is known. Interpretation requires deciding what a structure is for.

Structure Possible meanings
AND term combining address bits Address decode, or a range check, or a pattern match
Register with feedback and an enable Counter, shift register, or state bit
Term combining a clock and a data signal Gated clock, or a synchronous enable
Output that depends on many inputs Arbitration, or a priority encoder, or a lookup

Every row has several plausible readings, and the correct one is usually determined by context: what the other terms do, what the board connects to the pin, what the DSP expects. That is why CPLD reversal is iterative and why it benefits from knowing the system, whereas DSP reversal benefits from knowing the architecture.

The shared artefact nobody produces

There is one deliverable that both halves need and neither produces on its own: an interface specification. What signals cross between the two devices, in which direction, with what timing, and what each side assumes about the other. On most boards this exists nowhere, and reconstructing it is often more valuable than either individual reverse.

Interface element Source on the DSP side Source on the CPLD side
Address range Constants in code Decode terms
Data width Load and store instructions Bus width configuration
Strobe polarity Access macros Output polarity
Wait states Timing loop constants Acknowledge logic
Handshake protocol Polling loops Status register bits

The fifth row is where the two sides most often disagree, and the disagreement is diagnostic. A DSP that polls a status bit the CPLD never drives is a firmware bug that survived because the poll happened to succeed for another reason.

Doing both in the right order

The order is not arbitrary. Reversing the CPLD first usually makes the DSP work easier, because the decode terms tell you the memory map before you start reading code that uses it.

Order Benefit
CPLD then DSP Memory map known before code reading
DSP then CPLD Access patterns known before decode interpretation
Both in parallel Fastest on small systems, needs two specialists
Neither, measurement first Best when the board still runs

The fourth row is underused. On a working board, capturing bus activity for a few seconds with a logic analyser often establishes the interface faster than either reverse, and it provides ground truth against which both reconstructions can be checked.

The realistic output

A combined reverse produces three documents: a DSP reconstruction, a CPLD reconstruction, and the interface specification that reconciles them. The third is the one that makes the other two usable by somebody who was not involved.

Reading DSP embedded source code covers the segment-level work that precedes instruction reading, and attacking CPLD source code covers the state machine reconstruction on the logic side. Where the CPLD’s role is purely decoding, reading PLD code in an MCU system is the closer analogue.

Services at this boundary

Work covers acquisition for both devices, DSP disassembly with pipeline and addressing-mode modelling, CPLD equation and state machine reconstruction, interface specification, and reconciliation against measured bus activity. It does not cover producing a single unified source tree, because the two device types do not share a programming model.

The expectation worth setting is that a mixed reverse is two projects with a shared appendix. Quoting it as one project is how schedules get lost.