ATtiny4313: Moving Code Between the 2 KB and 4 KB Parts
Two part numbers, one package, one pinout, one peripheral set, and a factor of two between their flash arrays. The relationship is common enough in this family that images get moved between the members informally: a build is compiled for the larger part because the larger part is what the developer had, and a production run is fitted with the smaller one because that is what purchasing could get.
Sometimes that works. Sometimes it produces a unit that boots and misbehaves in a way nobody can attribute to anything, because the code that was moved is not the code that was tested. The difference is rarely the total size of the file. It is three specific addresses.
The three addresses
| what | why it decides the port | how to find it |
|---|---|---|
| highest code address used | if it exceeds the smaller array, the code does not fit at all | the largest target of any branch or call in the disassembly |
| end of the constant tables | tables placed above the smaller array’s ceiling are simply absent | the highest address loaded through the program-memory pointer |
| the stack pointer initial value | a 256-byte stack frame in a 128-byte part overflows silently | the immediate loaded into the stack pointer at reset |
The first is obvious and the second is often missed. A build can have compact code and a large table — a font, a lookup curve, a protocol string set — and the linker places that table after the code. On the larger part the table sits above the smaller part’s ceiling even though the code itself would fit. The result is a program that runs correctly until it first needs the table, then reads whatever the program-memory access returns outside the array, which on this hardware is not a fault but a value.
The third is the one that produces the least explicable symptoms. A stack that overflows its SRAM does not stop the processor. It corrupts whatever the stack pointer walks into, which is usually the region allocated to variables, and the corruption depends on call depth and interrupt timing. A unit that fails only under load, or only when a particular interrupt coincides with a particular function call, is a stack that is too deep for the part.
Why the jump instruction does not save you
A reasonable assumption is that the assembler would refuse to emit a relative jump that cannot reach its target. On a 4 KB array that assumption is true in a way that is unhelpful: a relative jump encodes a signed offset of twelve bits, which spans two thousand and forty-eight words, and 4 KB is exactly two thousand and forty-eight words. Every address in the array is reachable from every other.
So the toolchain has no reason to complain, the image links cleanly, and the fact that the code is destined for a 2 KB part changes nothing at build time. The constraint only appears when the image is executed on the smaller device, where the addresses above the ceiling do not exist.
This is also why the reverse direction is safe. An image built for the 2 KB part runs on the 4 KB part unchanged, because every address it uses is present in both. Downgrading is the risky direction; upgrading is not.
Reading the image to answer the question
The question «will this image run on the smaller part» is answerable from a disassembly in a short time, without recompiling anything:
- Decode the vector table at address zero. Its length is set by the peripheral set and is identical on both parts.
- Walk every branch and call target, and record the maximum. If it exceeds the smaller array’s word count, the answer is no.
- Walk every program-memory load, note the base addresses handed to the pointer pair, and record the highest one. If any table base is above the ceiling, the answer is no even when the code fits.
- Read the stack pointer immediate from the reset code, and compare the implied stack size against the SRAM of the smaller part.
- Check the initialisation for a RAM test or a memory-clear loop, whose end address is a second, independent statement of how much SRAM the build expects.
Step 5 is the cross-check that catches a build compiled against the wrong device definition. A clear loop that stops at the top of a 256-byte SRAM is a build that was configured for the larger part, regardless of what the makefile said.
The EEPROM doubles too, and that truncates records
The capacity change is not confined to the flash array. This pair carries 128 bytes of EEPROM in the smaller part and 256 in the larger, so a configuration block that fits the smaller device may occupy only the first half of the larger array, and the reverse move silently cuts it in half.
That matters because EEPROM contents are rarely a flat parameter list. Where the structure is a rotating log — records written in sequence, the oldest overwritten when the array is full — the record at the top of the larger array is newer than everything in the smaller one. Copying the first 128 bytes of a 256-byte log therefore copies the oldest records and discards the current state, which is the exact opposite of what a migration is supposed to preserve.
The safe procedure is to identify the record structure before moving anything: fixed length, a sequence field or a timestamp, and a rule for which record is authoritative. Where the structure cannot be identified, the last written record is usually the one with a sequence number that is one higher than its neighbours, or the only one whose checksum validates against contents that are not all erased.
What this means for a recovery report
When the image is being recovered rather than ported, the same three addresses belong in the report for a different reason: they describe the memory the design occupies, and they tell the customer whether a replacement can be built on either member of the family. That is a purchasing question as much as an engineering one, and it is answered by the numbers the disassembly already produced.
The report should also state which member the image was built for, using the stack pointer constant as the evidence. It is a single number and it removes the ambiguity that the shared package creates.
The self-programming behaviour of the smaller member is covered in ATtiny2313 code recovery, and the revised silicon in ATtiny2313A heximal recovery. Where the same capacity question appears one family up, ATtiny261 code recovery covers the members that share an analogue front end.
FAQ
Can I tell the two parts apart from the image? Yes, from the stack pointer constant and from the highest address the code touches. Neither requires knowing the marking.
Does a larger part’s image always fail on the smaller one? No. It fails when the code or a table crosses the smaller ceiling, or when the stack outgrows the smaller SRAM. Small images port without change.
Is the pinout really identical? Yes within this pair, which is why the substitution happens in the first place and why the resulting faults are so hard to attribute.
Conclusion
Capacity-only siblings invite informal substitution, and the substitution is safe in one direction and conditional in the other. Three addresses settle it: the highest code target, the end of the constant tables, and the stack pointer initial value. None of them appear in a file size, and all three are visible in a disassembly that takes minutes to produce.