Repository navigation
Description
While fixing a ChibiOS bug on the ATmega8, I noticed that simavr happily runs instructions the part doesn't have. The port used call chThdExit, which doesn't exist on avr4. avr-as rejected it, but simavr would have run it without complaint.
The decoder handles the full instruction set on every core. The only instructions gated per core are EIJMP/EICALL, which need EIND (sim_core.c#L1124), and ELPM, which needs RAMPZ (#L1160, #L1205). So JMP/CALL (#L1416-L1438) runs on the ATmega8/48/88 and the ATtiny2313/85. MUL and friends (#L848, #L859, #L1522) run on those tinies and on the ATmega8U2/16U2/32U2 and AT90USB82 cores that @teknoman117 added in #603. None of these parts have the instructions, according to the Instruction Set Summary in the ATmega8 datasheet (page 311, no JMP/CALL) and the ATmega8U2/16U2/32U2 datasheet (page 292, no MUL).
avr-gcc never emits them for these parts, so this only bites hand-written assembly. Easy to reproduce, though:
#include <avr/interrupt.h>
#include <avr/sleep.h>
void target(void) __attribute__((used, noinline));
void target(void) {
__asm__ volatile ("nop");
}
int main(void) {
/* call target, hand-encoded since avr-as rejects CALL here */
__asm__ volatile (".word 0x940e\n\t.word pm(target)");
cli();
sleep_mode();
}avr-gcc -mmcu=atmega8 -Os -o call.elf call.c
run_avr -v -v -v -m atmega8 -f 8000000 call.elf
It ends with "sleeping with interrupts off, quitting gracefully", so the CALL ran and returned. .word 0x9f86 (mul r24, r22) on atmega16u2 behaves the same way.
The two existing gates don't quite work either. There's no break after _avr_invalid_opcode(), so EICALL and ELPM still execute, using data[0] (r0) in place of the missing EIND/RAMPZ. The error message is also off by one instruction, because _avr_invalid_opcode() steps over the opcode before printing (#L490). With an EICALL at 003c on atmega8 and Z = 0, it reports 003e ... O=94f8 (the following CLI), jumps to 0 and loops through reset.
Flash size isn't a reliable proxy here. The 8 KiB ATmega8U2 and AT90USB82 have CALL while the 8 KiB ATmega8 and ATmega88 don't, and avr35 has CALL but no MUL. Explicit per-core flags for JMP/CALL and MUL seem like the cleanest fix, rejected through _avr_invalid_opcode(), plus a break after the existing gates. Happy to put together a PR if that sounds reasonable.
Activity
teknoman117 commentedon Nov 15, 2026
One more data point on the gate fall-through. Since EICALL uses r0 as the EIND byte, a non-zero r0 sends the call far outside flash, and run_avr crashes instead of just reporting the bad opcode. On atmega8:
It logs
004a: Invalid Opcode ... O=cfff(therjmp .-2after the EICALL at 0048), then calls(r0 << 16 | Z) << 1= 0x20038, well past the 8 KiB flash, and run_avr segfaults (exit 139). Without theinc r0, the same code callstargetand quits gracefully, which also shows the flagged EICALL still executes.felias-fogg commentedon Nov 15, 2026
And a data point from the real world: https://arduino-craft-corner.de/index.php/2021/12/29/surprise-surprise/#half-legal-opcodes
Although the official documentation says that CALL and JMP are not implemented on MCUs with 8KB or less, they still work. So, the question is: Should the simulator implement what's in the documentation or what the MCUs really do? In the end, it probably does not matter because compilers and assemblers will not generate such opcodes.
teknoman117 commentedon Nov 15, 2026
@felias-fogg oh that's interesting 😆
Any idea if that applies to all ATmega's with 8 KiB or less of flash? I do have a couple of early 00's mask ATmega8's I could test if I could find them...
felias-fogg commentedon Nov 16, 2026
I checked at that time for ATtiny85, ATtiny13, ATtiny24, ATtiny84, ATtiny2313, and ATtiny861.
I would bet a considerable amount of money on the fact thatit is also true for ATmega8, ATmega88, and ATmega48. Probably for the remaining parts as well.The test program I used was
A Makefile to compile that could be:
Note that I fool the assembler into believing that it compiles for an avr3 architecture. If flashed on an avr25 (small ATtinys) or avr4 (small ATmegas) architecture chip where JMP is nevertheless a legal instruction on the MCU, it will light up the connected LED. Otherwise, it will probably act as a NOP or do crazy things, but not jump to SETOUT, I assume. I actually single-stepped it in order to see whether it really jumps---and it does.
An interesting question is whether this is still true on the modern parts.
AVR: call chThdExit() with RCALL on the devices without CALL (#614)
gatk555 commentedon Nov 21, 2026
@felias-fogg: I think we should definitely prefer tested behaviour in the real parts over the data-sheet. That might be a trap though: it would be quite legitimate for Microchip to remove the instructions in a future die shrink. The observed behaviour might be expected: in a well-run engineering organisation the test team will rightly insist that all design variants are fully validated. Cheaper to waste a few gates than do that! MUL is a different matter, as a fast multiplier uses a lot of gates and silicon. It would be interesting to know if that works anywhere it "should not".
A parallel issue that may be more important in practice: the timing variations are not done either. If someone turns to simulation to sweat the timing, that could matter. In case anyone thinks to tackle this: hold off. A change is in queue to make the core agnostic about whether the 32 CPU registers are in data space: Xmegas, Megaxx0y, AVRxxYYzz (if I recall the part names correctly). And recent tinies ...
ccrause commentedon Nov 22, 2026
This topic is quite interesting, so I did some testing on hardware. I confirmed that call/jmp works on an attiny85, but appears to be unimplemented on attiny10 and attiny104. My guess is that the AVRe family may all share a common core design and therefore one could expect that call/jmp should be implemented in hardware (although there are still no guarantees). AVRrc family appears to have a different core design, which would then suggest that the call/jmp instructions are missing on all the members of this family.
felias-fogg commentedon Nov 23, 2026
And a few more data points. I tested a few chips that possess the original AVR core architecture (ATtiny12, 15, and 26), expecting -- after the results by @ccrause on AVRrc MCUs -- that on those chips JMP is definitely not implemented. And I got mixed results. ATtiny12 and 15 indeed treat JMP as a NOP, but ATtiny26 jumps.
I agree with @gatk555 that the reason one sees these instructions implemented on the chips is most probably that it is more expensive to remove the gates from an existing design than to leave them there and ignore them. Whether one should account for that in a simulator, I have mixed feelings. As a user, I think it is very helpful when one gets feedback from the system when opcodes specified to be illegal are going to be executed, stopping the simulation at this point. There is simply no reason why I would rely on such 'illegal' instructions. If one wants to understand the behavior of programs that run havoc, it might be a good idea to simulate what real hardware does. So, ideally, one would like to have both options.
ccrause commentedon Nov 24, 2026
Some more testing suggest that on the attiny104 the first word of the jmp opcode is skipped, the second word is interpreted as a regular opcode.
Edit: same behaviour also confirmed for attiny10.