PostHeaderIcon PIC16F876A Binary Extraction: Write Protection Is a Different Lock From Read Protection

The PIC16F876A is the 28-pin, 8K-word member of the enhanced PIC16F87XA family: 8K × 14-bit Flash, 368 bytes of RAM, 256 bytes of data EEPROM, 22 I/O pins and a five-channel 10-bit A/D converter. Its 40-pin twin adds the Parallel Slave Port and three more A/D channels on the same core.

When people talk about protection on this part, they almost always mean the CP bit — the single bit at position 13 of the configuration word that blocks external reads. That bit matters, but it is only one of two writeable locks in the same word, and the second one is routinely ignored: WRT, which controls whether running code can overwrite Flash through the EECON sequence. Confusing the two produces a rebuilt device that is secure in the wrong way.

What the PIC16F876A actually contains

Item PIC16F876A
Package 28-pin PDIP, SOIC, SSOP
Program memory 8K × 14-bit Flash (0000h–1FFFh)
Program counter 13-bit
Data RAM 368 bytes, four banks
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

Reset vector is at 0000h, interrupt vector at 0004h. Accessing an address above the physically implemented range wraps around rather than faulting, which is a behaviour worth remembering when an image is longer than the part it is being written to.

Two independent locks

The configuration word at 2007h carries both:

Bit Field Blocked by this bit
13 CP External read of all program memory
10:9 WRT1:WRT0 Internal write to selected Flash blocks

They answer different questions. CP asks: can an external programmer read this memory? WRT asks: can the program running on the device rewrite this memory?

WRT1:WRT0 Range write-protected (8K device)
11 none — all memory writable by EECON control
10 0000h–00FFh
01 0000h–07FFh
00 0000h–0FFFh

On the 4K devices (873A and 874A) those same field values cover 0000h–00FFh, 0000h–03FFh and 0000h–07FFh. The field is identical; the ranges are not. Using the 8K table on a 4K part overstates how much of the device resists self-modification.

Why WRT shows up in field failures

Write protection exists so a bootloader can sit at the bottom of Flash and be immune to the application it loads. A design with WRT1:WRT0 = 10 puts the boot block at 0000h–00FFh beyond the reach of EECON writes from application code, which is what prevents a bad firmware update from destroying the update mechanism itself.

That is a runtime property, not a security property — and it does not travel with the code. A recovered image written to a replacement part with WRT1:WRT0 = 11 (protection off) will run correctly and then, on the first self-reprogramming sequence, overwrite the region the original design deliberately protected. The device appears fixed until the first field update, and then it stops booting.

The reverse also matters. If the original had WRT engaged and the rebuilt part does not, a diagnostic path or an internal flash-logging routine that relied on being blocked now behaves differently, and a failure that was previously impossible becomes reachable.

CPD: a third lock, also independent

Data EEPROM protection sits at bit 8 and applies only to external reads of the 256-byte EEPROM block. Internal reads and writes through EEADR and EEDATA are unaffected regardless of how CP and CPD are set.

This asymmetry is the reason a program with calibration constants stored in EEPROM continues to function normally with code protection enabled — it never needs external access to that block. It is also why an image that contains only the Flash returns a device that boots, reads plausible values, and measures slightly wrong: the constants it expects are in a region nobody copied.

The configuration word in full

Bit Field Meaning
13 CP 1 = protection off, 0 = all program memory protected
12 — Unimplemented, reads as 1
11 DEBUG In-circuit debugger; 1 = disabled
10:9 WRT1:WRT0 Write-protect range
8 CPD Data EEPROM protection; 1 = off
7 LVP Low-voltage ICSP; 1 = PGM on RB3
6 BOREN Brown-out reset; 1 = enabled
5:4 — Unimplemented, read as 1
3 PWRTEN Power-up timer; 0 = enabled
2 WDTEN Watchdog; 1 = enabled
1:0 FOSC1:FOSC0 11 = RC, 10 = HS, 01 = XT, 00 = LP

Erased value is 3FFFh.

The LVP bit deserves separate attention because it determines the hardware path into the device. With LVP = 1 the part accepts programming from a single supply on RB3/PGM; with LVP = 0 that pin is ordinary I/O and high voltage must be applied to MCLR. High-voltage entry works either way, but low-voltage entry does not, so the fixture design has to follow the bit rather than assume a convention.

Reading the image

A complete 876A image covers 0000h–1FFFh of program Flash plus the four user ID locations at 2000h–2003h, the device ID at 2006h and the configuration word at 2007h. The device ID is the check worth doing first on any secondhand part: the 873A, 874A, 876A and 877A each report a distinct pattern, and the address ranges that CP and WRT refer to depend on whether the die is a 4K or 8K device.

Related notes cover the PIC16F876 binary recovery path on the non-A part, which uses the older duplicated protect field, and the PIC16F877A 40-pin version of this same scheme.

FAQ

Does CP protect data EEPROM? No. CPD at bit 8 does, and only against external reads. Internal access is always allowed.

Does WRT prevent an external programmer from writing the device? No. WRT governs writes issued by running code through EECON. External programming, including bulk erase, follows different rules.

What happens if the rebuilt part has WRT off but the original had it on? The device runs, then loses a region the original design protected on the first self-reprogramming sequence — typically a bootloader.

Are the WRT address ranges the same on a 4K part? No. 4K devices use 0000h–00FFh, 0000h–03FFh and 0000h–07FFh for the three protected levels.

What if the part is longer than the address space? Accesses above the implemented range wrap around silently instead of raising an error.

Conclusion

The PIC16F876A holds three separate locks in one 14-bit word: CP for external reads, WRT for internal writes, CPD for the EEPROM. Only one of them is normally discussed. A rebuild that reproduces the instruction stream but not the WRT state produces a device that passes every bench test and fails on its first field update — which is the most expensive kind of failure to discover late.

Related reading

Comments are closed.