wokwi / avr8js

Arduino (8-bit AVR) simulator, written in JavaScript and runs in the browser / Node.js
https://blog.wokwi.com/avr8js-simulate-arduino-in-javascript/
MIT License
463 stars 73 forks source link

Improve interrupt handling #38

Closed urish closed 3 years ago

urish commented 4 years ago

Currently each peripheral implements part of the interrupt handling logic, such as checking for interrupt condition, and clearing the corresponding interrupt flag after the. This creates some code duplication, does not respect interrupt priority, and also allows multiple interrupt handler to run sequentially.

Section 7.7 of the datasheet describes how interrupts should work:

  1. The list also determines the priority levels of the different interrupts. The lower the address the higher is the priority level. RESET has the highest priority, and next is INT0 – the External Interrupt Request 0.
  2. When the AVR exits from an interrupt, it will always return to the main program and execute one more instruction before any pending interrupt is served.

In other words, RETI should disable interrupts for one CPU instruction.