PIC18F46K20: Where a 64K Copy Diverges From the Original
At sixty-four kilobytes the array is large enough that a copy is a serious operation and small enough that every byte is meaningful. Reading it and writing it elsewhere is mechanical. Making the second device behave like the first is not, and the divergences are all in places that a file comparison will show as differences with no obvious cause.
Four of them account for most failed replications.
One: padding between sections
Linkers align sections, and the space between them is filled with the erased value or with a defined padding byte depending on the toolchain’s settings. A copy that reproduces the code but not the padding differs in the gaps.
Does the padding matter? Sometimes. A routine that reads past the end of its table, or a checksum that covers an aligned range, will see the difference. On a part where a verification routine checksums a range rather than a section, padding is part of the verified content.
The rule: copy the array as read, gaps included, rather than reconstructing it from sections.
Two: regions that were never written
Not every address in a 64K array holds content. A device using 40K has 24K of erased flash, and a copy that fills those regions with anything else differs.
This is usually harmless, and it becomes visible when something reads the erased region deliberately — a version scan, a memory dump command, a self-test. A device that reports “unused space is zeros” on the original and “unused space is FF” on the copy is a device whose copy is detectable.
| Region | Original value | Copy risk |
|---|---|---|
| Programmed sections | Content | None if copied exactly |
| Section padding | Toolchain-defined fill | Differs if reconstructed |
| Unused flash | Erased value | Differs if filled |
| Configuration | Set at production | Differs if not copied |
| Data array | Per-unit | Differs by design |
Three: table encoding
Constant tables are where a copy is most likely to be subtly wrong, because the representation is a convention rather than a specification. A 16-bit constant in a table might appear as two words, as one word per byte, or as a single word if the values fit.
A copy taken byte for byte is correct by construction. A copy reconstructed from a disassembly is not, and the resulting errors are small — a filter coefficient that is slightly wrong produces a device that works and sounds different.
The rule is the same as for padding: copy the array, do not rebuild it.
Four: configuration
Configuration is not in the array and decides the oscillator, watchdog, brown-out and protection. It is discussed in detail elsewhere; here it belongs in the list because it is the divergence that produces the largest behavioural difference with the smallest file difference — often zero bytes of code difference.
The verification that catches all four
Compare four things, in this order:
- The array, byte for byte, including gaps and unused regions.
- The configuration, byte for byte, against the recorded source values.
- The data array, by decision — copied, or deliberately blank.
- Behaviour, on a bench, against recorded behaviour of the original.
Steps one and two catch everything mechanical. Step four catches what the files cannot: timing, start-up order, response to faults.
What a copy cannot reproduce
- Analog trim, which belongs to the die.
- Silicon revision, which decides errata.
- The unique identifier on parts that carry one.
- The device’s history, if the firmware records usage in the data array.
Each of those produces small, consistent differences that are properties of the device rather than defects in the copy. Recognising them prevents a long search for a bug that is not there.
The larger point
Replication is a matching problem with four dimensions, and the easiest one — the code — is the one that a file comparison addresses. The other three are configuration, data and silicon, and a replication that succeeds at the first and ignores the rest produces a device that runs and is not the same device.
Related reading
- PIC18F45K20 and distinguishing blocked reads — what has to succeed before replication begins.
- PIC18F44K20 and the order of operations — how the copy is written correctly.
- PIC18F43K22 and the four dimensions of a duplicate — the same argument at smaller scale.
FAQ
Does padding matter in a copy? Sometimes. If anything checksums an aligned range, it is part of the verified content.
Should unused flash be filled? No. Copy it as read, or leave it untouched.
Why do tables cause trouble? Because their encoding is a convention. A byte copy is correct; a reconstruction may not be.
What is the costliest divergence? Configuration, because it changes behaviour with no difference in the code.
Can a copy be exact? Only at the array level. Trim, revision and identifier belong to the die.
Conclusion
A 64K copy diverges in four places: padding, unused regions, table encoding and configuration. Three are solved by copying the array rather than rebuilding it, and the fourth by treating configuration as content. The remainder — trim, revision, identifier — cannot be copied, and knowing that saves the search for a bug that is a property of silicon.