ATmega2560: RAMPZ, ELPM, and the Tables That Live Above 64 KB
A 256 KB flash array sounds like one memory, and for instruction fetch it largely behaves like one. For constant data it does not. The ordinary load-from-program-memory instruction takes a sixteen-bit pointer, which reaches sixty-five thousand five hundred and thirty-six bytes — a quarter of the array. Everything above that is reachable, but only through a different instruction and only if a particular register holds the right value.
That asymmetry is the single most common reason a recovered image appears to be missing its tables. The tables are there. The code that reads them is using an address form that a quick disassembly has rendered as something else.
Two limits that are not the same limit
Instruction fetch and constant data access are separate paths with separate widths, and on this part they diverge:
| path | pointer width | reach | limitation |
|---|---|---|---|
| instruction fetch | program counter, extended to 17 bits | the whole 128K words | indirect jumps need an extension register |
| ordinary constant load | 16-bit register pair | 64 KB of bytes, a quarter of the array | cannot see the upper three quarters |
| extended constant load | register pair plus a high-byte register | the whole array | the high-byte register must be correct |
The middle row is the trap. A design with a font, a waveform table or a large string set placed in the upper part of the array reads that table with the extended instruction, and a disassembly that does not recognise the idiom will show the table as unreferenced and the reader will conclude it was never used.
The high-byte register is global state
The register that supplies the upper address bits for the extended load is not part of the pointer. It is a single global register that stays where it was last written. That produces a code pattern with no equivalent on smaller parts: before every extended load, the register is set, and after the load, if the code is careful, it is restored.
Careful is the operative word. A routine that performs an extended load and returns without restoring the register will corrupt the next extended load performed by its caller. The bug is real, it is common in hand-written assembly, and it appears in recovered images as a routine that sets the register at entry and never restores it.
For a reader the practical consequence is that the register’s value at any given instruction cannot be determined locally. It has to be tracked along each path, which is exactly the kind of analysis a disassembler does not do by default.
Interrupts make it worse, and the code knows
An interrupt can occur between setting the register and using it. If the service routine also performs an extended load, it overwrites the value and the interrupted code resumes with the wrong high byte — reading a table address in the wrong bank, which usually means reading plausible-looking bytes from somewhere else entirely.
The fix is to save and restore the register in the service routine, and that save appears in the image as a pair of instructions around the handler’s prologue that look redundant until the mechanism is understood. Seeing them is a good sign: it means the firmware was written by somebody who understood the part.
| symptom in the field | mechanism | what to look for in the image |
|---|---|---|
| occasional wrong lookup values under interrupt load | high-byte register clobbered by a handler | handlers that omit the save and restore |
| a table that reads correctly in a test loop and wrongly in production | same, with the test loop not enabling interrupts | the enable instruction’s position relative to the table read |
| data that looks shifted by a fixed offset | wrong bank selected, reading a neighbouring region | the constant written to the register before the load |
Three-byte pointers in the data structures
Constant pointers on this part are three bytes wide, not two, because the address space needs eighteen bits. That shows up in structures stored in flash or in SRAM: arrays of pointers are three bytes per entry, and a structure containing a program-memory pointer has different padding from the same structure on a 128 KB part.
Reading such a table by hand is straightforward once the width is known and hopeless if it is assumed to be two. The characteristic error is an array that decodes as alternating sensible and nonsensical values, which is what happens when a two-byte stride is applied to three-byte records: every third entry lands correctly.
The routine that reads the whole array
A bootloader that verifies the application before jumping to it has to read all 256 KB, and it cannot do that with the ordinary load. Such a routine is often the largest single user of the extended instruction in the image, and it is a useful landmark because it walks the address space linearly with the high-byte register incrementing at each 64 KB boundary.
Finding it answers a question that matters for a recovery: whether the device would have refused to run a modified image. A design with a checksum over the whole array will not boot after a partial write, which is why some «dead» units are not dead at all.
What the deliverable states
The return set is the flash image at the correct length, the EEPROM separately, the fuse and lock state as read, a disassembly in which the extended load is annotated with the bank it selects, and a map of where the large tables sit relative to the 64 KB byte boundary.
That map is the part of the work that prevents a wrong conclusion downstream. An engineer who knows that the font lives at an address requiring the extended load will not spend a day looking for where it is read.
The control-flow side of the same part — the extension register for indirect jumps and the three-byte return address — is covered in ATmega2560V firmware recovery. The part that stops one size below this boundary is in ATmega1280V program recovery, and the same table-versus-code distinction on a smaller array in ATtiny261 code recovery.
FAQ
Why can the ordinary load only reach a quarter of the array? Because it takes a sixteen-bit byte address, and the array is 256 KB. The extension register supplies the bits above sixteen.
Do the tables disappear in a bad read? No. They are present in the image. What fails is the disassembly’s recognition of how they are addressed.
Is the high-byte register saved by the compiler? Compilers that support this part do save and restore it around functions that use the extended load. Hand-written assembly often does not.
Conclusion
On a quarter-megabyte array the interesting problems are on the data side. One register holds the high address bits, it is global state, interrupts can clobber it, and pointers that reach the upper part of the array are three bytes wide. Read those four facts into the disassembly and the missing tables reappear where they always were.