PostHeaderIcon PIC18F24K20: Program and Data Are Two Arrays With Two Rules

“Program and data” in a recovery request usually means two things: the code, and whatever constants the code depends on. On this family those two things live in different arrays with different access rules and different protection bits, and the difference is not academic — one of them is routinely readable when the other is not.

For an instrumentation product, that asymmetry is decisive. The code describes what the device does; the data array describes how it is calibrated. A readable data array on a locked device can therefore yield the more commercially sensitive half of the design.

The two arrays

Program memory holds instructions and, in practice, also constant tables. Data EEPROM is a separate array, byte-writable, addressed through a small register window rather than through the program counter.

Property Program array Data EEPROM
Addressed by Program counter, table reads Address registers
Granularity Words Bytes
Protection bit Per block One bit, separate
Erased by Bulk or block erase Bulk erase, or byte write
Typical contents Code, tables Calibration, settings, counters

The separate protection bit is the whole point of this article. Set program protection and leave data protection clear, and the device will hand over its constants while refusing its code. The reverse is unusual but possible.

Why a designer would do that

The configuration is not a mistake. Consider a product whose calibration is performed at end of line and written into the data array, and whose code is the vendor’s intellectual property. Protecting the code and leaving the data readable lets a service technician verify or re-enter calibration without exposing the firmware.

That is a reasonable policy and it has a side effect: any technician, or anyone with a programmer, can read the calibration. For a sensor product, calibration curves frequently encode as much design effort as the firmware.

Reading the data array

Where the data array is unprotected, reading it is straightforward and does not depend on the program array at all:

  1. Enter programming mode.
  2. Read the data EEPROM address range through the programmer’s data-array access.
  3. Record it as bytes, not words — the array is byte-organised even though the core is word-organised.

The third step matters for the same reason it matters for program memory. A file that represents bytes as words doubles every value and produces constants that are wrong by a factor of 256, which then look like plausible fixed-point values in a disassembly.

What the data array tells you about the code

A data array is a description of the program’s assumptions. Three patterns are worth extracting:

Calibration constants grouped in a fixed order. The order matches the order the code reads them, which reveals the sequence of measurements the device performs.

Stored settings with defaults. A block of bytes that looks like a configuration record with a version byte at the front identifies the settings structure the code parses.

Counters and timestamps. Uniform-looking runs with occasional increments are usage counters, and they reveal how the device records its own history — useful for dating a build.

None of that is code, and all of it constrains the code. A rebuild that reads the same constants in the same order is far more likely to reproduce the original behaviour than one written from first principles.

When the program array is readable and the data is not

The opposite configuration appears when data protection was enabled and later forgotten, or when a bootloader set it. The practical effect is unusual: the code can be read, and its constants cannot.

The recovery then depends on the code being self-sufficient. Some firmware copies its constants into RAM at startup from the data array; the copy routine reveals the layout, and the values themselves have to be measured from the device’s behaviour — an instrument that reads slightly wrong tells you its offset and gain by how wrong it is.

Bulk erase and the order of loss

A complication specific to this family: a bulk erase clears both arrays. There is no way to erase the program array and keep the data array, or the reverse.

That makes the sequence of operations a decision rather than a procedure. Read both arrays, in whatever order the protection allows, before erasing anything — because after an erase there is nothing left to read and no partial outcome to salvage.

The rule is worth stating as a rule because it is violated constantly: on a device whose program is protected and whose data is not, an erase performed to remove program protection destroys the data first, and it is the data that was readable.

Practical checklist

  • Read the configuration; establish the state of both protection bits.
  • Read the data array first if it is open, and record it byte-exact.
  • Read the program array if it is open, and record the block map.
  • Only then consider any operation that modifies the device.
  • When comparing against a known build, compare the data array separately — it differs between units by design.

The last item prevents a common false conclusion: two units with identical firmware will have different data arrays, and a comparison that expects them to match will report a difference that means nothing.

Related reading

FAQ

Does program protection cover the data EEPROM? Not on this family. The data array has its own protection bit.

Can the data array be read while the program array is locked? Yes, if data protection is clear. That is a common production configuration.

Does a bulk erase clear both arrays? Yes. Neither array survives it.

Are data arrays identical across units of the same product? No. Calibration differs by design, so comparisons must treat the data array separately.

Is the data array byte-addressed? Yes, and recording it as words corrupts every value.

Conclusion

Two arrays, two protection bits, one erase that clears both. On this part the data array is frequently the readable half and the informative one, which makes the order of operations a decision with consequences rather than a formality — and makes “program and data” a request that needs to specify which is which.

Comments are closed.