Copy IC PIC16F884 Code: Drawing the Line Between Code and Everything Else
A scope statement is the cheapest part of an engineering job and the one most often left implicit. When the request says copy the code, four different regions could be meant, and the difference between them is the difference between a replacement that works and one that starts and misbehaves.
This part makes the question concrete because it carries all four regions at once. Program memory, a data EEPROM of a couple of hundred bytes, a configuration block, and a manufacturer identity area. Each has its own protection bit, its own access method, and its own claim on the phrase that was used.
The four regions and who owns each
| Region | Owner at design time | Protection | What happens if it is left out of the copy |
|---|---|---|---|
| Program memory | the firmware engineer | code protection bit | nothing runs |
| Data EEPROM | the production line and the field | data protection bit, often unset | runs, with wrong calibration and identity |
| Configuration block | the firmware engineer, once | its own bits | may not start at all, or starts in the wrong clock mode |
| Identity area | the manufacturer | read-only, not erasable | nothing, but the silicon revision becomes unknown |
The asymmetry is what makes the discussion worth having. Leaving out program memory is a job that visibly failed. Leaving out data EEPROM is a job that appears to have succeeded and fails weeks later in the field, which is a considerably worse outcome for everyone involved.
What the program array actually contains
Reading the program array of a mid-range device is straightforward: a linear sequence of instruction words covering the implemented address space, with the reset vector at address zero, the interrupt vector a few words above it, and the bulk of the code following.
Two structural features are worth naming because they affect how the image is read rather than what it holds. The first is paging. The instruction set addresses program memory in pages, and a call or goto that crosses a page boundary needs a bit set in the program counter’s high byte beforehand. This leaves a visible signature in the disassembly — pairs of instructions where a page-select precedes a call — and it is a reliable way to confirm that a disassembled listing is aligned correctly rather than shifted by one word.
The second is the literal pool. Values too large for an instruction’s operand field are stored as words in the code stream and loaded through a table-read, and those words look like nonsense instructions to a disassembler. Finding them and marking them as data rather than code is part of producing a listing a human can read.
Where the data array earns its place
The data EEPROM on this device is byte-writable, retains across power cycles, and is addressed separately from the program array. That independence is what makes it the natural home for anything that changes after the firmware is fixed — a sensor trim measured on the fixture, a serial number, a run-hour counter, the position of a mechanical part at last shutdown.
It is also why the data array and the program array have to be treated as a pair in a copy. The program reads the data at startup, and the read has a defined behaviour when the data is blank. That behaviour is usually a fallback rather than a refusal: the firmware loads a default gain, a default offset, a default identity, and carries on. The product works, and every measurement it produces is wrong by a small percentage that nobody notices until a customer compares two units.
There is also a startup flag convention worth knowing. Many products keep a magic value in the first bytes of the data array and treat anything else as a virgin device, running a first-time initialisation path that overwrites the array. Copy a program onto a device with a blank data array and the first power-up may quietly replace the calibration you were trying to preserve.
Configuration is not a setting, it is a decision about the silicon
The configuration block decides things that happen before the first instruction: which oscillator the core runs from, whether that oscillator is divided, when the brown-out reset trips, whether the watchdog is armed, whether the programming pin remains available as an input, and whether the arrays are protected.
It is worth reading it before anything else on a device of unknown history, because it answers two operational questions at once. It says how the board is meant to start, and it says whether the arrays you are about to read are protected. On this family the configuration block sits at a defined address near the top of the map and the identity word sits just below it.
The trap on the write side is that copying configuration verbatim copies the protection bits. A device programmed with the original’s configuration is immediately as locked as the original was, which is fine if the copy is finished and a problem if it is not. The working order is: read everything, verify, program program and data, verify again, program configuration last.
Identity and revision
The manufacturer identity area is small and read-only. It carries a device identifier and a revision code, and it cannot be written, so it cannot be copied. What it can do is explain a behaviour.
Errata attach to revision. A product designed around a workaround for a specific silicon bug will behave differently on a later revision where the workaround is unnecessary or where the bug was fixed differently. Recording the identifier and revision in the delivery costs one read and answers the question that arrives six months later, when two supposedly identical units behave differently and nobody can say why.
Deciding the scope with the customer
Four questions settle it in a short conversation, and they are worth asking in this order.
- Is the goal to reproduce the product, to repair one unit, or to understand the algorithm? A repair needs the data array and often not the code; understanding needs the code and often not the data.
- Does the product have per-unit calibration? If the answer is unknown, assume yes and read the data array anyway.
- Are there field-updated settings? If so, the data array is part of the deliverable by definition.
- Will the copy be programmed onto a fresh device, or into a simulation? A simulation needs the disassembly with literal pools marked; a fresh device needs the raw images.
The answers usually move the scope outward rather than inward. Almost every job described as copy the code ends up delivering three regions and a note about the fourth.
What to hand over
A complete delivery for this class of work is a small set of files with an unambiguous description of each.
- the program image with start and end addresses
- the data EEPROM image, blank regions marked as blank rather than omitted
- the configuration block, decoded into named fields
- the identity and revision words
- a disassembly with data regions marked
- a statement of which regions were protected and therefore inferred
The last line is the one that keeps a delivery honest. An inferred region should be labelled as inferred, never silently filled with a plausible pattern.
For the equivalent scope decision on a part without a data array at all, PIC12C671 image copying shows how the job simplifies. For a device where the data array is the whole point, PIC16F88 data recovery is the closer comparison. Where a vendor puts the factory calibration into a separate information segment instead of the last program word, the region split becomes a fourth and fifth region and the scope question grows — MSP430F4361 flash recovery covers that layout. And for the larger sibling where the program array is several times this size and paging becomes a structural feature rather than an occasional detail, PIC16F877 heximal recovery covers the reading strategy.