ATmega128P: Binary Layout Past the Sixty-Four Kilobyte Line
A binary file is a sequence of bytes with an implied start address and nothing else. For an image smaller than sixty-four kilobytes that is sufficient, because a single sixteen-bit address covers it. Past that point the file has a region the format cannot describe, and every tool resolves it by convention.
The conventions differ, and a file produced under one and read under another is not obviously wrong — it is wrong in a way that only shows up when the upper half is examined.
What the line means
Sixty-four kilobytes is where a sixteen-bit offset wraps. A file that describes more than that must either record addresses (as hex does) or append the excess at a known offset with an agreed meaning.
Three arrangements are common:
| Arrangement | Upper half position | Risk |
|---|---|---|
| Contiguous append | immediately after 64 KB of bytes | misread if the flash is smaller than the file |
| Fixed offset block | at a round boundary well above | misinterpreted as blank padding |
| Separate file | not in this file at all | the upper half silently missing |
The third is the dangerous one, because the resulting file looks complete at sixty-four kilobytes and is half a program.
Why a truncated image can still run
Here is the uncomfortable part: a device whose upper half was not captured may still disassemble and even make sense, because the lower half contains the vectors, the initialisation and the main loop. The functions that live in the upper half appear as unresolved call targets, which looks like missing library code rather than a missing region.
That misreading persists until someone tries to follow a call to an address beyond sixty-four kilobytes and finds the file ends at that point.
The check that reveals it
Two questions, both cheap:
What is the file length? Compare it against the array size reported by the signature. A file substantially shorter than the die’s flash is suspect.
Does the code call addresses beyond the file? Decode a few calls and note their targets. A target that exceeds the file length is a reference to the missing region.
Both are arithmetic and neither requires understanding the program. Doing them first prevents an afternoon of analysis on half an image.
Reading a full 128 KB image
With both halves present, three features need care:
Address arithmetic in the code. The upper half can only be reached through an instruction that carries a high byte, so the code contains visible register loads before jumps and table accesses. Those loads are the map of where the image crosses the line.
Constant tables. Tables that straddle the line are the most fragile part of a relocation, because a corrupted high byte turns a valid pointer into an invalid one without changing the low bytes.
Vector table. Unchanged, and still the anchor. Its targets are the only fixed reference in either half.
The revision and the low-voltage grade
Neither suffix changes the layout. The P marks the picoPower die and the V the extended supply range; both affect the electrical envelope of a read of this size.
A 128 KB read takes long enough that drift matters. Reading in passes with per-page verification, and re-reading the first page at the end, is the practical guard against a file that is correct at the start and wrong at the end.
Related notes
- ATmega128A and EEPROM wear — the other array on this die.
- ATmega1280PV and the three-byte counter — why the counter widens at this size.
- ATmega640PV and binary offsets — the same format below the line.
FAQ
Is a binary ever a complete description of a 128 KB image? Only with a stated convention for the upper half and a file length that matches.
How can I tell if the upper half is missing? By file length and by call targets that exceed it.
Does a hex file avoid this problem? Yes. It records addresses explicitly, so the crossing is visible.
Why keep using binary at all? It is smaller and it is what some tools require. The convention just has to be recorded.
Conclusion
The sixty-four kilobyte line is where a binary stops describing itself. Length and call targets are the two checks that reveal a truncated image, and they cost nothing. Past the line, the work is arithmetic on high bytes — and the vector table remains the only anchor that does not move.