Interrupts - TypeDefinition/NautiBuoy GitHub Wiki

Interrupts

How interrupts works

  • Interrupts are like an eventsystem, throughout our program, there will be certain 'events' or conditions that will call an interrupt and the CPU will temporarily stop the execution of the current codes, go to specific addresses and run the codes there.
  • Before every instruction, the CPU checks if an interrupt needs to be handled. If there is an interrupt, the CPU will stop the current execution of the code, and call the instruction at certain predetermined memory addresses.
  • When an interrupt is called, it puts a return address at the stack so we can return back to the original location of the program before the interrupt occurred using the instruction ret.

Interrupts on the Gameboy

  • VBlank (Handler Address: $0040, Highest Priority)
  • STAT (Handler Address: $0048)
  • Timer (Handler Address: $0050)
  • Serial (Handler Address: $0058)
  • Joypad (Handler Address: $0060, Lowest Priority)
  • For example, there is a Timer interrupt, the CPU would essentially do "call $0050" and run the instructions there.

Enabling interrupts

  • The instruction ei enable interrupts, while the instruction di disable them.
  • We also need to write to $FFFF, which is the interrupt register, the flags on which interrupts we want to enable.
ld a, %00000001 | %00000100 ; flags for V-blank interrupt and timer interupt
ld [$FFFF], a ; enable V-blank interrupt and timer interupt

More info on interrupts: