Skip to content

CALL/JMP and MUL execute on cores that don't have them #607

Open

Description

@teknoman117

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

  1. teknoman117 commented on Nov 15, 2026

    @teknoman117
    ContributorAuthor

    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:

    #include <avr/io.h>
    
    void target(void) __attribute__((used, noinline, naked));
    void target(void) {
      /* set SE, then cli + sleep: run_avr quits gracefully */
      __asm__ volatile ("ldi r24, 0x80\n\tout 0x35, r24\n\tcli\n\tsleep");
    }
    
    int main(void) {
      /* r0 = 1, Z = target, eicall */
      __asm__ volatile ("clr r0\n\tinc r0\n\tldi r30, pm_lo8(target)\n\tldi r31, pm_hi8(target)\n\t.word 0x9519" ::: "r0", "r30", "r31");
      for (;;)
        ;
    }
    avr-gcc -mmcu=atmega8 -Os -o eicall.elf eicall.c
    run_avr -v -v -v -m atmega8 -f 8000000 eicall.elf
    

    It logs 004a: Invalid Opcode ... O=cfff (the rjmp .-2 after the EICALL at 0048), then calls (r0 << 16 | Z) << 1 = 0x20038, well past the 8 KiB flash, and run_avr segfaults (exit 139). Without the inc r0, the same code calls target and quits gracefully, which also shows the flagged EICALL still executes.

  2. felias-fogg commented on Nov 15, 2026

    @felias-fogg
    Contributor

    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.

  3. teknoman117 commented on Nov 15, 2026

    @teknoman117
    ContributorAuthor

    @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...

  4. felias-fogg commented on Nov 16, 2026

    @felias-fogg
    Contributor

    I checked at that time for ATtiny85, ATtiny13, ATtiny24, ATtiny84, ATtiny2313, and ATtiny861. I would bet a considerable amount of money on the fact that it is also true for ATmega8, ATmega88, and ATmega48. Probably for the remaining parts as well.

    The test program I used was

    .org 0x0000
     
            rjmp MAIN     ; reset vector
     
    SETOUT: ldi r16, 0xFF
            out 0x17, r16 ;   -- needs to be adapted to where a LED is (0x04 for ATmegaX8)
            out 0x18, r16 ;  -- needs to be adapted to where a LED is (0x05 for ATmegaX8)
    MAIN:   jmp  SETOUT
    STOP:   rjmp STOP
    

    A Makefile to compile that could be:

    all:
    	avr-gcc jmp.S -mmcu=avr3 -c -o jmp.o
    	avr-gcc -mmcu=avr3 jmp.o --output jmp.elf -Wl,-Map=call.map,--cref
    	avr-objcopy -O ihex -R .eeprom jmp.elf jmp.hex
    

    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.

  5. gatk555 commented on Nov 21, 2026

    @gatk555
    Collaborator

    @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 ...

  6. ccrause commented on Nov 22, 2026

    @ccrause

    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.

  7. felias-fogg commented on Nov 23, 2026

    @felias-fogg
    Contributor

    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.

  8. ccrause commented on Nov 24, 2026

    @ccrause

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions