PostHeaderIcon PIC16F876 Binary Recovery: Reading an 8K Image Through an 11-Bit Instruction Field

The PIC16F876 is the 28-pin half of the 8K PIC16F87X pair. It carries 8K × 14-bit Flash program memory, 368 bytes of RAM and 256 bytes of data EEPROM, with 22 I/O pins and a five-channel 10-bit A/D converter. Its 40-pin twin, the 877, adds the Parallel Slave Port and five more A/D channels on the same die family.

Recovering the binary from this part is not simply a matter of reading 8192 words out. The architecture creates a specific trap: the program counter is wide enough to address the whole space, but the CALL and GOTO instructions that move it are not. A recovered dump that is byte-perfect can still produce a wrong disassembly, because the instruction stream alone does not contain the full target address. This is why the 876 is a good case study in what a binary image really is.

What the PIC16F876 actually contains

Item PIC16F876
Package 28-pin PDIP, SOIC, SSOP
Program memory 8K × 14-bit Flash (0000h–1FFFh)
Program counter 13-bit
Data RAM 368 bytes
Data EEPROM 256 bytes
I/O pins 22
A/D converter 5 channels, 10-bit
CCP modules 2
Serial MSSP (SPI / I²C) + USART
Parallel Slave Port Not implemented
Hardware stack 8 levels × 13-bit, circular

The 8K figure is the reason this part behaves differently from the 873 and 874. Those have 4K, and the configuration word’s protect ranges are expressed in different addresses for the two sizes. It also means the top half of memory is a separate 2K page, which is where the addressing problem starts.

The paging problem

All PIC16F87X devices can address a continuous 8K word block, but the CALL and GOTO instructions each carry only 11 bits of address — enough for a 2K page. The two remaining bits of the destination come from PCLATH<4:3>, a register the program has to set before the branch.

This has three consequences for anyone working with a recovered image:

A raw hex file does not fully describe control flow. Reading the image gives you every 14-bit instruction, but a CALL to 0x1234 encodes only 0x234. Whether it lands at 0x0234, 0x0834, 0x1234 or 0x1A34 depends on PCLATH at the moment of execution. Disassemblers handle this by tracking PCLATH writes, which works only if the init sequence and the page-switching code are both present and intact.

Partial protection breaks the chain. With CP1:CP0 set to 10, only 1F00h–1FFFh is protected; the rest reads back normally. A dump taken in that state has a hole at the top of the last page. If a page-switch routine or an interrupt vector table lives in the missing 256 words, every cross-page call in the recovered image becomes unresolvable, even though 94% of the binary is present.

The stack is not in the image. The 8-level stack is 13 bits wide and physically separate from both program and data space, and the stack pointer is not readable or writable. It operates as a circular buffer: after eight pushes, the next one overwrites the oldest entry, with no overflow flag and no exception. Nothing about the runtime stack state survives a power cycle, so no recovery method returns it. What you get is code, not context.

The configuration word at 2007h

The protect field on this generation appears twice in the same register, at bits 13:12 and again at bits 5:4, and both copies must hold the same value for the documented scheme to apply.

Bits Field Meaning
13:12 and 5:4 CP1:CP0 Flash code protection, duplicated
11 DEBUG In-circuit debugger enable
9 WRT Flash write enable via EECON
8 CPD Data EEPROM code protection
7 LVP Low-voltage ICSP enable
6 BODEN Brown-out reset
3 PWRTE Power-up timer (inverted)
2 WDTE Watchdog enable
1:0 FOSC1:FOSC0 Oscillator selection

For the 8K devices the four CP1:CP0 values map to:

CP1:CP0 Range protected
11 none
10 1F00h–1FFFh
01 1000h–1FFFh
00 0000h–1FFFh

The erased value of the whole word is 3FFFh, which corresponds to protection off, debugger off, watchdog on and RC oscillator — not a setting anyone would choose deliberately, but it is what a blank or partially erased device reports.

Data memory and the bank model

The 368 bytes of RAM are split into four banks of up to 128 bytes. RP1 (STATUS<6>) and RP0 (STATUS<5>) select the bank for direct addressing, and the lower addresses of each bank hold the Special Function Registers. Several frequently used registers are mirrored across banks so the program does not have to switch banks to reach them.

This matters during reconstruction because a binary that assumes a particular bank state at entry to a routine may misbehave if the reconstructed project changes any register allocation. The instructions are position-independent in Flash but not bank-independent at runtime.

Why the distinction between “binary” and “hex” matters

A dump from this part is 8192 fourteen-bit words. Microchip’s programming format stores each 14-bit word across two bytes, with the upper two bits unused, and Intel HEX wraps that in address-tagged records. The configuration word at 2007h and the four user ID locations at 2000h–2003h are outside the user program space, so they are either carried in separate records or omitted depending on the tool.

A file described as a binary is therefore ambiguous until you know whether it includes those regions. An image that contains only 0000h–1FFFh will programme a replacement part with its protection state determined by whatever default the programmer applies — which is how a rebuilt board ends up re-readable when the original was not.

Why this recovery is requested

The 8K parts were used heavily in motor control, power supplies and instrumentation where a 4K part was too tight. Devices from that era are still in service, and the failure pattern is predictable: a working board exists, the toolchain and source do not, and maintenance requires the image. Related notes on the same architecture cover PIC16F876A write protection and the 40-pin PIC16F877 hex image. The smallest member of the family, the PIC16F870, uses the same core with a different memory size.

FAQ

Why does a complete hex file still disassemble with wrong call targets? Because CALL and GOTO carry only 11 address bits. Correct disassembly requires tracking PCLATH<4:3> writes, which depends on code that may sit outside the readable region.

Can a partial dump be rebuilt into a working project? Only if nothing in the missing range is referenced. On an 8K part with CP1:CP0 = 10, the missing 256 words are exactly where an interrupt vector or boot routine often lives.

Does the 876 have a Parallel Slave Port? No. PSP is implemented only on the 40/44-pin 874 and 877. A design that depends on it cannot be retargeted to a 28-pin part regardless of the recovered code.

Is the stack recoverable? No. It is 8 levels deep, 13 bits wide, separate from both program and data memory, and not readable at runtime. It is a circular buffer with no overflow indication.

What does the device report when blank? The configuration word reads 3FFFh when erased — protection off, watchdog enabled, RC oscillator selected.

Conclusion

The PIC16F876 makes the boundary of “recovery” visible. You can obtain every stored instruction word, and still not have the program, because control flow on this core is split between the instruction and a register the program maintains. An 8K image through an 11-bit call field is readable, reconstructable, and easy to misread — in that order.

Related reading

Comments are closed.