Personal Computer News


Feeling Free To Interrupt

 
Published in Personal Computer News #092

Feeling Free To Interrupt

Prevent NEW from being entered from the keyboard, and demonstrate the use of keyboard interrupts, with the program below.

When run, it asks for a number between 110 and 255 and the machine code is located in memory at this number multiplied by 256.

In normal operation, i.e. when the Spectrum is in interrupt mode 1 (set by mnemonic IM1), every 1/50th of a second a jump is made to address 38 hex to temporarily scan the keyboard. To change this address the interrupt mode 2 (IM2) can be used. Then every 1/50th of a second, the Z80 creates a vector with the I register as the high byte and another byte (usually 255) that it receives from another part of the hardware as the low byte. At the location pointed to by this vector is the low byte followed by the high byte of the interrupts subroutine address. A CALL to this address is then made.

In the example program, lines 10 to 40 can be used to set up any interrupt routine. To put in your own routine, simply change the data in line 50. As the program stands, it protects the user from accidentally NEWing a program. To return to normal, enter GOTO 9999.

If you set up your own interrupt using this method you must first push all the registers used in your routine off the stack at the end. Next disable interrupts at the beginning of the routine using D1 and enable them at the end using E1. Lastly, if you still want the keyboard scanned, RST 38 (hex) somewhere in the routine.

In this way interrupts could be set to handle sprites, display a trace of a program, single step, and so on.

  10 INPUT "Please enter m/c address divided by 256";a
  20 LET a=a-1
  30 LET f=a*256+248 : REM f=start address of initialisation
  35 READ s : IF s<>999 THEN POKE f,s : LET f=f+1: GOTO 35 : REM poke in data for init and actual interrupt
  40 DATA 62,a,237,71,237,94,201 : REM set up interrupt
  45 DATA 1,a+1 : REM holds address of interrupt routine
  50 DATA 243,255,245,58,8,92,222,230,32,3,50,8,92,241,251,201 : REM data of actual interrupt routine
  60 DATA 999
  70 RANDOMIZE USR a*256+248 : STOP
9999 POKE f+1,237 : POKE f+2,86 : POKE f+3,201 : RANDOMIZE USR (f+1) : REM return to normal

Callum Gibson
Blairgowrie, Perthshire

Callum Gibson