Skip to content

sim: boot section and reset vector from the fuses and IVSEL - #604

Merged
gatk555 merged 1 commit into
buserror:masterfrom
teknoman117:boot-ivsel
Nov 19, 2026
Merged

gatk555 merged 1 commit into
buserror:masterfrom
teknoman117:boot-ivsel

Conversation

@teknoman117

Copy link
Copy Markdown
Contributor

Bootloaders that use interrupts (Caterina, LUFA CDC/DFU, ...) set MCUCR.IVSEL (GICR.IVSEL on the older megas) to move the vector table to the start of the boot section, and clear it before starting the application. simavr always used the vectors at 0, so such a bootloader ended up in the application's handlers.

The bit is declared by DEFAULT_CORE for every core whose avr-libc header defines IVSEL. While it is set, vectors are taken relative to reset_pc, which is where a bootloader is started from (BOOTRST). The IVCE timed sequence is not modeled.

@gatk555

gatk555 commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

I see nothing that sets avr->reset_pcm so it would seem to do nothing. Setting it should depend on the fuses, at least for Mega328 and 32U4. It looks possible to set the fuses in the firmware, but I never saw it used.

@teknoman117
teknoman117 force-pushed the boot-ivsel branch 2 times, most recently from 19405fd to c16b237 Compare November 14, 2026 07:27
@teknoman117
teknoman117 marked this pull request as draft November 14, 2026 07:33
@teknoman117
teknoman117 force-pushed the boot-ivsel branch 4 times, most recently from 3022be5 to be16f5c Compare November 14, 2026 08:22
@teknoman117 teknoman117 changed the title sim: support IVSEL, interrupt vectors in the boot section sim: boot section and reset vector from the fuses, IVSEL Nov 14, 2026
@teknoman117
teknoman117 marked this pull request as ready for review November 14, 2026 08:26
@teknoman117 teknoman117 changed the title sim: boot section and reset vector from the fuses, IVSEL sim: boot section and reset vector from the fuses, support IVSEL Nov 14, 2026
@teknoman117 teknoman117 changed the title sim: boot section and reset vector from the fuses, support IVSEL sim: boot section and reset vector from the fuses. support IVSEL. Nov 14, 2026
@teknoman117

teknoman117 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor Author

@gatk555 Sorry, I forgot to pull the relevant changes in from my usbip branch, I've now folded in those changes.

This PR has now grown somewhat into sourcing the default boot address of the AVR from the fuse bits rather than relying on the machine implementation to define the boot address directly. IVSEL then allows deriving the interrupt vector table from the boot sector or the start of flash.

@teknoman117
teknoman117 force-pushed the boot-ivsel branch 2 times, most recently from c2b6446 to 0678c6c Compare November 16, 2026 05:01
@teknoman117 teknoman117 changed the title sim: boot section and reset vector from the fuses. support IVSEL. sim: boot section and reset vector from the fuses and IVSEL Nov 16, 2026
@gatk555

gatk555 commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Looks good, but two quibbles:

sim_avr.c:
The calculation of "doublings" seems somewhat obfuscated.
Why not (~f & AVR_FUSE_BOOTSZ) >> AVR_FUSE_BOOTSZ0, as it seems
this is simply extracting a bitfield?

sim_elf.c:
Add a comment to explain call to avr_reset().

Thanks, G.

@teknoman117
teknoman117 force-pushed the boot-ivsel branch 2 times, most recently from 7ad8eb8 to da0ddfb Compare November 18, 2026 21:43
Bootloaders that use interrupts set IVSEL to move the vectors to the
boot section. simavr always used the vectors at 0.

- Each mega core gives its smallest boot section (BOOTSZ = 11) and the
  fuse byte that holds BOOTRST and BOOTSZ. The values come from the
  datasheets.
- avr_update_reset_vector() computes boot_start from BOOTSZ. When
  BOOTRST is programmed it also sets reset_pc and the PC to boot_start.
  Otherwise reset_pc is left as the caller set it.
- avr_reset() calls it and so does avr_load_firmware() when the
  firmware carries fuses.
- Interrupts vector to boot_start while IVSEL is set.
- _BV is now the real macro. The identity _BV made every default fuse
  value wrong.
- The watchdog no longer hooks the missing vector of the ATmega8/16/32.
  A second reset crashed these parts.
- board_simduino sets the board BOOTRST and BOOTSZ bits instead of the
  PC. Its ATmega2560 detection now triggers.

test_boot_section checks 26 parts against the datasheet tables and
that a reset keeps reset_pc when BOOTRST is unprogrammed.
test_atmega328p_ivsel runs a bootloader and an application that each
take an interrupt through their own vectors. AVR_LDFLAGS gives
per-firmware link flags.
@teknoman117

teknoman117 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor Author

@gatk555 Thanks for taking another look! I've addressed both.

The fuse bits are now plain bit numbers as avr-libc has them with a mask for BOOTSZ next to them. The boot section size is now a couple of named steps that read the BOOTSZ field with the shift you suggested.

I realized a second full reset was heavier than it needed to be. I replaced it with a small helper utility avr_update_reset_vector(). It sets the boot section from BOOTSZ and only moves reset_pc and the PC when BOOTRST is programmed. avr_reset() now calls it and so does the loader when the firmware carries fuses. With the default fuses nothing changes so code that manually sets reset_pc itself still works.

@gatk555
gatk555 merged commit 7054785 into buserror:master Nov 19, 2026
8 checks passed
@teknoman117
teknoman117 deleted the boot-ivsel branch November 19, 2026 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants