PostHeaderIcon PIC18F45K80: Reading an Image Backwards, From Behaviour to Code

There are two directions to work in. Forwards is the familiar one: an image exists, and the question is what it does. Backwards is the one that appears when the array is closed but the device is alive: behaviour exists, and the question is what the code must be to produce it.

The backwards direction is not guesswork, and it is not less rigorous. It is constraint-solving, and the constraints come from measurement. On a 32K device with a defined job, they usually narrow the answer to something a rebuild can satisfy.

The first constraint is the clock

Everything else depends on it. Instruction timing, peripheral timing, communication rates and control loop periods all scale with the clock, and the clock is measurable at a pin if the oscillator output is enabled, or inferable from any timed output.

Once the clock is known, every timing measurement converts into a cycle count, and cycle counts are the currency of this kind of reconstruction. A loop that runs every 400 microseconds at 16 MHz is 6,400 cycles, and 6,400 cycles is a quantity that constrains what the loop can contain.

The second constraint is the pin assignment

A 44-pin device with fixed peripheral functions means the pin map is a statement about the firmware. Which pins are outputs, which are analog, which carry a serial function, and at what rate — all measurable, all constraining.

Observation Constrains
Pin driven at a fixed frequency A timer, and its period
Pin toggling on an external event An interrupt path and its latency
Analog pin with a known reference An ADC configuration and its resolution
Serial line at a known rate A UART or CAN configuration and its clock source

The fourth row is the most informative on this family, because serial rates are exact. A bus running at a specific rate fixes the oscillator and the divider, and the oscillator is the input to every other calculation.

The third constraint is the response

Step the input. The output’s shape tells you the control law’s structure, independently of its parameters:

  • Immediate proportion with no overshoot: proportional only.
  • Slow approach with no steady-state error: integral action.
  • Overshoot and ringing: derivative action, or a second-order plant.
  • Saturation with a defined recovery: limiting logic, which is firmware behaviour and not plant behaviour.

Each of those is a structural statement, and structure is what a rebuild needs. Parameters follow from measurement once the structure is fixed.

Where a partial image helps

If some blocks are readable, they constrain the reconstruction directly. A readable boot region tells you the image format and the entry address. A readable application block tells you the instruction mix, which tells you what the compiler was, which tells you what the source probably looked like.

Readable region What it gives
Boot region Entry address, update format, verification method
Lower application block Vector table, initialisation, peripheral setup
Upper application block Tables and constants, often the domain knowledge
Data array Calibration values and settings layout

The last row is the one that converts a reconstruction from approximate to precise. Calibration constants in the data array are the numbers the code uses, and their magnitudes and ordering reveal the scaling and the sequence of operations.

Assembling it

The reconstruction procedure that produces something testable:

  1. Measure the clock and derive the cycle budget for each observable period.
  2. Map the pins and identify each peripheral function and its configuration.
  3. Characterise the response to step, ramp and fault inputs.
  4. Read whatever regions are open, and extract the frame: vectors, initialisation, tables.
  5. Write an implementation to the constraints.
  6. Compare against the original at every observable point, and iterate on the differences.

Step six is where the work is. A first implementation meets the coarse constraints and misses the fine ones — an off-by-one in a scaling factor, a filter coefficient slightly wrong, a timing margin that differs. Each difference is information about the original.

Honest limits

  • A reconstruction is an equivalent, not a copy. It will differ in details that are not observable.
  • Behaviour that the device never exercises cannot be inferred, and a rebuild may therefore fail in conditions the original handled.
  • On this family, protection is configuration bits and there is no password; once reads are gated, they stay gated until an erase, and an erase ends the reconstruction’s reference device.

The third point is a warning about method. A working reference device is the most valuable object in a backwards project, and it is destroyed by exactly the operation people reach for when a read fails.

Related reading

FAQ

Can a rebuild be byte-identical? No. Reconstruction produces equivalent behaviour, not the original instruction stream.

What measurement matters most? The clock, because every other timing measurement depends on it.

Does a readable block help if the rest is closed? Yes. It fixes the instruction set in use, the calling conventions, and often the compiler.

What destroys a backwards project? Erasing the reference device to attempt a read. The reference is the source of every constraint.

How close is close enough? When the rebuilt device is indistinguishable at every point the application can observe.

Conclusion

Working from behaviour to code is constraint-solving: clock, pins, response, and whatever the array will still show. It produces an equivalent rather than a copy, and on a part whose reads are gated it is often the only direction available — which makes the reference device worth protecting more carefully than the file it might have produced.

Comments are closed.