PostHeaderIcon Recover Microcontroller PIC16F71 Binary: Two Banks, Two Boundaries

Two independent address boundaries run through this architecture, and confusing them is a common source of confusion when reading a recovered image. One governs where instructions live. The other governs which registers an instruction can reach. They have different sizes, different rules and different visible signatures in the code.

Learning to tell them apart turns a listing from a stream of instructions into a readable description of a design, because every boundary crossing was a decision the engineer had to make and could not avoid.

The two boundaries

Boundary Granularity Crossed by Visible signature
program page 2K instruction words call, goto, computed jump high address bits preset before the transfer
register bank 128 bytes of register file any direct access to another bank status bits rewritten before the access
program page, far case where a page register exists indexed access a write to a page-select register

The two are easy to distinguish once named. A program boundary appears before a control transfer. A register boundary appears before an instruction that touches a file register — a load, a store, a bit set.

On a 28-pin device with a large complement of peripherals spread across two banks, the register boundary is crossed constantly. That frequency is what makes the image readable: the crossings form a regular rhythm, and departures from that rhythm mark the interesting parts of the code.

What the register boundary tells you

Peripheral registers on this generation are laid out in the first bank and continued in the second, with core registers repeated in each bank at fixed offsets so that the status word and the indirect pointers are always reachable.

The practical consequence is a pattern. Code that touches only the first bank’s peripherals writes the status bits once at startup and rarely again. Code that reaches into the second bank writes them before every access, and the sequence — save status, set bank, access, restore status — is a recognisable idiom.

Reading that idiom gives you an inventory. Each time the bank changes, note which register is then accessed, and after a hundred lines you have a list of exactly which peripherals the product exercises. On a device whose datasheet describes more peripherals than the product could plausibly use, that list is the fastest way to find out what the design is for.

What the program boundary tells you

Program memory crosses its 2K boundary with a different idiom. Before a call or jump that leaves the current page, the high bits of the program counter must be loaded, and that load is emitted immediately before the transfer.

Two readings follow. The first is alignment confirmation: the pairs appear at consistent offsets relative to control transfers, so a listing where they appear at inconsistent offsets is misaligned by a word. The second is call-graph structure: every crossing is a boundary between compilation units or between hand-written modules, and the density of crossings tells you whether the code was written as one file or assembled from several.

On a device of this density, a code base with very few crossings was written as a single unit or placed by hand to keep hot paths on one page. A code base with many was written by a compiler without regard to placement. Neither is a defect, and the difference tells a customer a good deal about who wrote the product and when.

Reading the array in an order that builds understanding

The sequence below produces a usable picture faster than a linear pass.

  1. Read the whole array and the configuration word. Note the position of the erased tail; that is the end of the code.
  2. Extract every control transfer to a target outside the current page. These are the module boundaries.
  3. Extract every status-bit rewrite that precedes a file-register access. These are the peripheral accesses.
  4. Build the peripheral inventory from step three, and cross-check it against the pin count of the package.
  5. Only then read the instruction stream in order, with the map from steps two and three as a guide.

Step four is the sanity check that catches bad reads. A 28-pin part has a fixed number of I/O pins; a peripheral inventory that implies more pins than the package provides means the disassembly has drifted somewhere.

Where the configuration word sits and why it matters here

The configuration word at the top of the address space selects the oscillator, the watchdog, the low-voltage programming option and the code protection. On a device with an internal oscillator variant, the same region may carry a factory calibration byte.

Reading it first, and decoding it explicitly, answers two questions at once: how the board is meant to start, and whether the array was protected when the device was last programmed. That second fact is worth recording even on a device that is currently unlocked, because a device that was once locked and later erased has a different history from one that was never locked.

Protection and the salvage question

When protection is set, the interface returns nothing from the array. On a device of this size the loss is total — there is no partial code to reason from — and the job moves to the behavioural side.

What remains useful is the pin-level behaviour. A 28-pin product with a defined set of outputs can be exercised, and its responses over time describe its control logic even when the instructions are invisible. That is a different deliverable, and it is worth being explicit that it is a description rather than a file.

The delivery

  • the program image, with the erased tail’s position stated as the end of code
  • the configuration word decoded
  • a peripheral inventory derived from the bank-crossing pattern
  • a list of module boundaries derived from the page-crossing pattern
  • the data EEPROM image, where the part carries one
  • the identifier and revision words

The third and fourth items are what a customer cannot produce for themselves from the raw file, and they are the reason a recovered listing is worth more than a hexadecimal dump.

For the 18-pin relatives where the peripheral count is smaller and the bank crossings are rarer, PIC16F884 code copying covers the scope decision. Where the array is stored as charge and the read itself needs validation, PIC16C73A program recovery treats the same paging structure on an older process. And where the image belongs to a smaller device whose entire program fits inside a single page, PIC16F715 firmware recovery shows how much of this analysis disappears.

A note on naming

Ordering codes in this line are terse, and the code printed on a package does not always match the marketing name in a catalogue. The safe procedure is to read the identifier word from the device and confirm the density and the peripheral set from that, rather than from the label. It costs one read and prevents a whole category of misdirected work — particularly where a customer is working from a parts list compiled years ago and the labels have drifted from what is actually fitted.

Comments are closed.