PostHeaderIcon PIC16F76: The 8K-Word Page Boundary That Breaks Naive Disassembly

The PIC16F76 is the 28-pin, 8K-word member of Microchip’s mid-range Flash family: 8192 × 14 bits of program memory, 368 bytes of RAM, 256 bytes of data EEPROM, five 10-bit ADC channels, two CCP modules and 22 I/O pins. It is the larger sibling of the PIC16F73 and PIC16F74, and it exists because 4K words was not enough for designs that needed a serial stack plus an ADC front end plus a control loop.

The 8K figure is where the interesting engineering is. It is exactly twice the 4K that the mid-range core was originally laid out for, and the extra bit of address does not fit in the instruction.

What the PIC16F76 actually contains

Item PIC16F76
Core 14-bit instruction word, 35 instructions
Program memory 8192 × 14-bit Flash (14 KB)
Data RAM 368 bytes (4 banks)
Data EEPROM 256 bytes
Hardware stack 8 levels
I/O pins 22 (PORTA 5, PORTB 8, PORTC 8, plus RE bits)
A/D converter 5 channels, 10-bit
PWM CCP1 and CCP2, up to 2 outputs
Serial USART, SSP (SPI/I²C)
Timers Timer0, Timer1 (16-bit), Timer2
Packages 28-pin PDIP, SOIC, SSOP
Programming ICSP, high voltage on MCLR

Four RAM banks of 96 bytes each, minus the mapped SFRs at the top of each bank, is what 368 bytes looks like in practice. The bank select bits RP1:RP0 in STATUS are set and cleared around almost every register access, and that pattern is one of the clearest fingerprints in a recovered listing.

CALL and GOTO carry 11 bits

On the mid-range core, CALL and GOTO encode an 11-bit destination. That addresses 2048 words. The PIC16F76 has 8192.

The remaining two bits come from PCLATH<4:3>, which the firmware has to load before the jump and which the jump itself does not change. The hardware behaviour is specific and worth stating precisely: a GOTO or CALL loads PCLATH<4:3> into PCL<12:11> for the duration of the jump, and then PCL<12:11> reverts to whatever PCLATH<4:3> held before the instruction completed. In other words the page bits apply to the destination only, not to the return, and not to subsequent instruction fetches.

Two consequences follow for anyone reading a recovered image.

First, a linear disassembly from address 0000h that ignores PCLATH will mislabel every cross-page destination. The 11-bit field is correct; the missing two bits are in a register write that may be several instructions earlier. Every call graph built without tracking PCLATH<4:3> on this part is wrong above 7FFh.

Second, the reverse is also true: because PCLATH<4:3> is set by ordinary MOVLW/MOVWF sequences, a static analyser can recover the page bits reliably. They are not hidden in a computed jump — they are in the instruction stream, just not in the instruction. This is why the 16F76 recovers more cleanly than chips where jump targets are computed at runtime.

The computed jump, which is the real problem

The one place the page bits are not statically visible is the RETLW table idiom, and the PIC16F76 uses it heavily because 256 bytes of data EEPROM is not much room for a segmented sensor linearisation.

The idiom is: compute an offset, add it to PCL, and let the program counter walk into a run of RETLW instructions. On an 8K device the addition can carry out of the low byte into PCLATH, and the firmware must handle that carry explicitly. Get it wrong and the routine jumps into the wrong page; get it right and the table is a sequence of literal values sitting in the listing.

For recovery this is good news. A RETLW table is data with no obfuscation — the values appear as they were written. The difficulty is knowing where the table ends, since RETLW is also a legitimate way to return a constant from an ordinary routine.

The configuration word at 2007h

Bit Field Meaning
13:7 — Unimplemented, read as 1
6 CP Program memory protection; 1 = disabled, 0 = enabled
5 CPD Data EEPROM protection; 1 = disabled, 0 = enabled
4 BOREN Brown-out reset; 1 = enabled
3 PWRTEN Power-up timer; 1 = disabled, 0 = enabled
2 WDTEN Watchdog; 1 = enabled
1:0 FOSC1:FOSC0 11 RC, 10 HS, 01 XT, 00 LP

Two things are absent that appear on the following generation. There is no LVP bit — this family is programmed by applying high voltage to MCLR, and there is no single-supply path to consider. There is also no partial protection: CP covers all 8192 words as one unit, with no equivalent of the CP1:CP0 field that the PIC16F87X parts use to protect the upper half separately.

The four-value oscillator field is the other generational marker. Where the 16F87X offers eight oscillator modes including two internal RC configurations, the 16F7X offers four, and all four require external components or an external clock. On a recovered image, that means the clock frequency is determined by the board, not by the configuration word — a simpler situation than on parts where INTOSC is an option, but one that makes the board itself part of the record.

Protection, and the clause that decides it

CP = 0 blocks external reads over ICSP. It does not alter execution. Microchip’s programming specification for the Flash mid-range parts attaches the standard note to the CP bit:

The entire program memory will be erased when code protection is turned off.

So clearing CP performs a bulk erase, and a subsequent read returns erased words. The same note applies to CPD and the 256-byte data EEPROM: turning data protection off erases the array. Both barriers are of the same kind, and neither can be opened without destroying what is behind it.

What a complete image contains

  • 0000h–1FFFh — 8192 × 14-bit program Flash, spanning four code pages
  • 2000h–2003h — four user ID locations
  • 2006h — device ID
  • 2007h — the configuration word
  • 256 bytes of data EEPROM in a separate address space

The user ID locations are worth noting on this family. They are four 14-bit words that the programmer can write but the firmware cannot read at runtime, and in production they frequently hold a board revision, a serial number or a build date. When present they date the firmware and identify which variant of a product a given binary came from — useful when a product line shipped several incompatible revisions under one model number.

Related notes

The 4K-word members of the same family use an identical configuration word with half the address space: the PIC16F74 and the PIC16F72. The later PIC16F767 replaces this part with enhanced peripherals, and the smaller PIC16F716 covers the 18-pin end of the range.

FAQ

Why is the page boundary a problem for disassembly? CALL and GOTO encode only 11 address bits. The upper two bits of the destination come from PCLATH<4:3>, set by an earlier instruction, so a linear disassembler that ignores it mislabels every cross-page target above 7FFh.

Does PCLATH affect the return address? No. The page bits apply to the jump destination only, and PCL<12:11> reverts afterwards. Return instructions use the hardware stack, which holds the full 13-bit address.

Can CP be cleared without erasing the program? No. The programming specification states that the entire program memory is erased when code protection is turned off.

Does this part support low-voltage programming? No. The configuration word has no LVP bit; programming requires high voltage on MCLR.

Is there partial protection like the PIC16F87X? No. CP covers all 8192 words.

Conclusion

The PIC16F76 is a case where the interesting limitation is arithmetic rather than physical. Two bits of address do not fit in a 14-bit instruction, so they live in a register, so the jump structure of the firmware is only reconstructable by reading the instructions around each jump. The protection mechanism is the ordinary Flash mid-range arrangement, and the single fact that governs it is the one about erasing on unprotect. Everything else is a question about reading the image correctly.

Comments are closed.