Recovering a PIC16F877 Hex Image: The Registers the Code Assumes You Will Preserve
The PIC16F877 is the 40-pin, 8K member of Microchip’s original PIC16F87X generation: 8K × 14-bit Flash, 368 bytes of RAM, 256 bytes of data EEPROM, 33 I/O pins, eight 10-bit A/D channels and a Parallel Slave Port. It was one of the most widely designed-in 8-bit microcontrollers of its era, which is why boards built around it are still in production and still failing.
When the question is what a recovered hex image contains, the answer is narrower than most people expect. It contains instructions. It does not contain the runtime state those instructions depend on. Three registers in particular — STATUS, OPTION_REG and INTCON — define the contract the program expects, and understanding that contract is what separates an image that rebuilds into a working device from one that rebuilds into a device that boots and then misbehaves.
What the PIC16F877 actually contains
| Item | PIC16F877 |
|---|---|
| Package | 40-pin PDIP / 44-pin PQFP, TQFP |
| Program memory | 8K × 14-bit Flash (0000h–1FFFh) |
| Data RAM | 368 bytes, four banks |
| Data EEPROM | 256 bytes |
| I/O pins | 33 |
| A/D converter | 8 channels, 10-bit |
| CCP modules | 2 |
| Serial | MSSP (SPI / I²C) + USART |
| Parallel Slave Port | Yes, 8-bit |
| Hardware stack | 8 levels × 13-bit, circular |
The reset vector is at 0000h and the interrupt vector at 0004h, four words apart. That gap is smaller than an interrupt handler, which is why every real program on this part puts a GOTO at 0004h rather than code.
STATUS: the register that is not fully writable
STATUS holds the ALU result flags and the bank select bits. The detail that matters most during reconstruction is that not all of it can be written. If an instruction targets STATUS and would affect Z, DC or C, those three bits are left alone — the hardware sets them from the result instead. TO and PD are read-only entirely, reflecting whether the last reset came from a watchdog timeout or a power cycle.
| Bits | Field | Behaviour |
|---|---|---|
| 7:6 | IRP, RP1 | Indirect and direct bank select |
| 5 | RP0 | Bank select, with RP1 |
| 4 | TO | Read-only, 0 after watchdog timeout |
| 3 | PD | Read-only, 0 after SLEEP |
| 2:0 | Z, DC, C | Set by hardware on arithmetic results |
This is the reason a disassembler cannot simply treat every MOVWF STATUS as a full register write. Code that appears to load a specific value into STATUS is in practice setting bank bits only, and the low three bits come from whatever the previous computation produced. A reconstruction that “corrects” these writes will change program behaviour.
OPTION_REG: the prescaler sharing problem
OPTION_REG configures the Timer0 clock source and, critically, decides whether the prescaler is attached to Timer0 or to the watchdog.
| Bit | Field | Function |
|---|---|---|
| 7 | RBPU | PORTB weak pull-up enable (active low) |
| 6 | INTEDG | RB0/INT edge select |
| 5 | T0CS | Timer0 clock source |
| 4 | T0SE | Timer0 edge select |
| 3 | PSA | Prescaler assignment: 1 = WDT, 0 = Timer0 |
| 2:0 | PS2:PS0 | Prescaler rate |
The single prescaler is shared between Timer0 and the watchdog — it cannot serve both. This is the most common source of silent divergence after a rebuild. If the recovered image’s initialisation sets PSA = 0 (prescaler to Timer0) and the watchdog is enabled in the configuration word, the watchdog then runs with no prescaler and times out at its base period. On the original board that may have been fine because WDTE was cleared; on a replacement part where the configuration word was not carried across, the watchdog may be running and resetting the device every few milliseconds.
INTCON: the interrupt contract
INTCON holds the enable and flag bits for Timer0 overflow, PORTB change and the external RB0/INT interrupt. Because the peripheral interrupts on this generation are individually enabled at the source and globally at INTCON, an image carries no record of which combination was intended for a given board. The code sets it, so the code has to be intact for the setting to survive.
Paging: the 11-bit limit again
The PIC16F877 addresses a continuous 8K block, but CALL and GOTO carry only 11 bits — enough for a 2K page. The upper two bits of the destination are supplied by PCLATH<4:3>, which the program must have set beforehand. When a CALL or an interrupt return executes, the entire 13-bit program counter is pushed or popped from the stack, so the stack itself is full-width even though the branch instructions are not.
There is a second, subtler gap: the FOSC1:FOSC0 field in the configuration word selects the RC, HS, XT or LP oscillator. A hex image carries one of those four values, and while it is a two-bit field, the parser is easy to get wrong because the rest of the word carries the protection and watchdog settings alongside it. A single misread bit in that word is enough to change the watchdog state and the protect state simultaneously.
What the configuration word holds
The word at 2007h is 14 bits. Four rows are documented for the protect field, and on the 8K devices the address ranges are 1F00h–1FFFh, 1000h–1FFFh and 0000h–1FFFh for CP1:CP0 values of 10, 01 and 00. CPD at bit 8 is a separate lock for the 256-byte EEPROM. LVP at bit 7 decides whether the part can be entered in programming mode from a low-voltage supply on RB3/PGM. A blank device reports 3FFFh.
The four user ID locations at 2000h–2003h sit alongside this and commonly carry build codes or serial numbers that a cloned device will duplicate unless they are deliberately changed.
Why this recovery is requested
Ten thousand products were built around this part. A working board, a failed line and no source archive is the standard situation, and the hex image is the last surviving copy of the design. Related analysis covers the 28-pin PIC16F876 binary that shares this 8K layout, the later PIC16F877A whose protection scheme was redesigned, and the 4K PIC16F873 software variant.
FAQ
Does the hex image include EEPROM contents? Not reliably. The 256 bytes of data EEPROM are a separate region — accessed through the EEADR and EEDATA registers at runtime, and addressed separately during programming. Its protection is controlled by CPD, independently of Flash. An image without it leaves calibration constants behind.
Why did the rebuilt device reset in a loop? Most often a watchdog mismatch. WDTE in the configuration word and PSA in OPTION_REG interact. If the prescaler was assigned to Timer0, the watchdog uses its base period.
Is STATUS writable in full? No. Z, DC and C are set by the ALU when STATUS is the destination, and TO and PD are read-only. Only IRP and RP1:RP0 behave as ordinary writable bits.
Can the full 8K be used by CALL and GOTO directly? No. Those instructions carry 11 bits. The remaining bits come from PCLATH<4:3>, set by the program.
What does 3FFFh read back as? A blank configuration word: protection off, debugger off, watchdog enabled and RC oscillator selected.
Conclusion
A PIC16F877 hex image is a record of instructions, and instructions on this core are only half the program. STATUS, OPTION_REG, INTCON and the configuration word define what the instructions assume — about banking, about timing, about which reset sources are armed. Recovery that returns the Flash and stops there returns a program that cannot be reasoned about. The registers are the rest of the contract.
Related reading
- PIC16F876 binary recovery — the 28-pin 8K part and its paging model
- PIC16F877A heximal extraction — the A revision and its single-bit code protection
- PIC16F873 software protection — the 4K variant with a different address map
- Chip recovery reference — the wider set of memory recovery notes