Difference between revisions of "Interrupt handler"

From MSX Game Library

(Resources)
 
(7 intermediate revisions by the same user not shown)
Line 6: Line 6:
 
On an MSX, it is the Z80, the central processor, that manages this mechanism. It has a specific input connected to the internal BUS that allows it to communicate with all internal (such as the VDP) and external (via the cartridge port) peripherals.
 
On an MSX, it is the Z80, the central processor, that manages this mechanism. It has a specific input connected to the internal BUS that allows it to communicate with all internal (such as the VDP) and external (via the cartridge port) peripherals.
  
When the Z80 receives an interrupt signal from a peripheral device, it calls a routine located at a fixed address in memory page 0 (address 0x0038). <sup>[[#Notes|#1]]</sup>
+
When the Z80 receives an interrupt signal from a peripheral device, it calls a routine located at a fixed address in memory page 0 (address <tt>0x0038</tt>). <sup>[[#Notes|#1]]</sup>
  
 
The code that is called during an interrupt is called an ISR (interrupt service routine).
 
The code that is called during an interrupt is called an ISR (interrupt service routine).
 +
 +
Those with knowledge of assembly language should be concerned that the code executed during the interrupt may modify the CPU registers and that when the Z80 returns control to the main program, it will no longer function correctly. Well, you are right to be concerned because there is no automatic mechanism to prevent this!
 +
 +
As a result, before modifying the value of any register, ISRs have to save its value in the stack so that it can be restored before returning control to the main program.
 +
 +
==== Well known interrupt ====
 +
Interrupt can be triggered by any device plugged to the MSX. Here is a non-exhaustive list of well known devices that generate interrupt:
 +
* TMS9918 (VDP of the MSX1):
 +
** V-blank (when screen display ends)
 +
* V9938/V9958 (VDP of the MSX2/2+)
 +
** V-blank (when screen display ends)
 +
** H-blank (when display of a given line ends)
 +
* V9990 (external VDP)
 +
** V-blank (when screen display ends)
 +
** H-blank (when display of a given line ends)
 +
** Command end (when a blitter command ends).
 +
 +
=== BIOS ===
 +
As by default, the main-ROM slot is selected on page 0, it is a piece of code provided by the BIOS that is executed at address <tt>0x0038</tt> each time an interrupt occurs.
 +
 +
The video processor sends an interrupt signal at constant intervals after displaying each frame (at 50 or 60 Hz depending on the region) and the BIOS’s ISR uses this interrupt to update some of its variables (frame counter, joystick or keyboard input state…).
 +
 +
==== Hook ====
 +
For the main program to be acknowledged when an interrupt occurs, the BIOS provides hooks in memory that are called in given circumstances. The main program can redirect those hooks to its own function to handle interrupts on its own.
 +
 +
Interrupt related hooks:
 +
* <tt>H.KEYI</tt>: called when any interrupt occurs.
 +
* <tt>H.TIMI</tt>: called when v-blank interrupt occurs.
 +
 +
=== Custom ISR ===
 +
When using "ROM_48K_ISR", "ROM_64K_ISR", NEO mapper targets, or any ROM with InstallRAMISR equal to "RAM0_ISR" or "RAM0_SEGMENT", the MSXgl boot program will switch memory page 0 to point to a ROM segment or RAM where an custom ISR is located.
 +
 +
This ISR is optimized for game and only respond
  
 
== Notes ==
 
== Notes ==
* <sup>#1</sup> In the default interrupt mode (IM 1)
+
* <sup>#1</sup> In the default interrupt mode (IM 1). The Z80 has two other  interrupt modes (IM 0 and IM 2) but only IM 1 is used usually.
 +
 
 +
== Resources ==
 +
* ''[https://map.grauw.nl/articles/interrupts.php Interrupts]'' on the MSX Assembly Page.

Latest revision as of 23:06, 17 October 2025

The MSX interrupt mechanism is essential for gameplay synchronization, but also for using certain peripheral features (such as the graphics processor).

What is an interrupt?

As its name suggests, an interrupt is a mechanism that temporarily interrupts the execution of the current main program to execute another piece of code, before returning control back to the main code.

On an MSX, it is the Z80, the central processor, that manages this mechanism. It has a specific input connected to the internal BUS that allows it to communicate with all internal (such as the VDP) and external (via the cartridge port) peripherals.

When the Z80 receives an interrupt signal from a peripheral device, it calls a routine located at a fixed address in memory page 0 (address 0x0038). #1

The code that is called during an interrupt is called an ISR (interrupt service routine).

Those with knowledge of assembly language should be concerned that the code executed during the interrupt may modify the CPU registers and that when the Z80 returns control to the main program, it will no longer function correctly. Well, you are right to be concerned because there is no automatic mechanism to prevent this!

As a result, before modifying the value of any register, ISRs have to save its value in the stack so that it can be restored before returning control to the main program.

Well known interrupt

Interrupt can be triggered by any device plugged to the MSX. Here is a non-exhaustive list of well known devices that generate interrupt:

  • TMS9918 (VDP of the MSX1):
    • V-blank (when screen display ends)
  • V9938/V9958 (VDP of the MSX2/2+)
    • V-blank (when screen display ends)
    • H-blank (when display of a given line ends)
  • V9990 (external VDP)
    • V-blank (when screen display ends)
    • H-blank (when display of a given line ends)
    • Command end (when a blitter command ends).

BIOS

As by default, the main-ROM slot is selected on page 0, it is a piece of code provided by the BIOS that is executed at address 0x0038 each time an interrupt occurs.

The video processor sends an interrupt signal at constant intervals after displaying each frame (at 50 or 60 Hz depending on the region) and the BIOS’s ISR uses this interrupt to update some of its variables (frame counter, joystick or keyboard input state…).

Hook

For the main program to be acknowledged when an interrupt occurs, the BIOS provides hooks in memory that are called in given circumstances. The main program can redirect those hooks to its own function to handle interrupts on its own.

Interrupt related hooks:

  • H.KEYI: called when any interrupt occurs.
  • H.TIMI: called when v-blank interrupt occurs.

Custom ISR

When using "ROM_48K_ISR", "ROM_64K_ISR", NEO mapper targets, or any ROM with InstallRAMISR equal to "RAM0_ISR" or "RAM0_SEGMENT", the MSXgl boot program will switch memory page 0 to point to a ROM segment or RAM where an custom ISR is located.

This ISR is optimized for game and only respond

Notes

  • #1 In the default interrupt mode (IM 1). The Z80 has two other interrupt modes (IM 0 and IM 2) but only IM 1 is used usually.

Resources