INTERRUPTIONS and SYSTEM
VARIABLES
Before starting to talk to you about interrupt-driven programs, let's study:
*** SYSTEM VARIABLES ***
- SYSTEM VARIABLES are various pieces of information that are available to us for reading and writing, located at the beginning of the second KB of our ST's RAM. (The location in RAM varies if you have a MEGA ST or an ST with the operating system (the TOS) not in ROM, but it does not vary between different versions of ROM)
- To access these variables, you must first go into SUPERVISOR MODE, otherwise beware of crashing... (Bus error: 2 bombs!)
System variables are either BYTES or WORDS or LONG VECTORS (L-M).
- We will use some of these SYSTEM VARIABLES, they greatly influence the functioning of the ST:
Here are the addresses where these different variables are located as well as their names, sizes, and uses:
The names below are the standard names given by ATARI to these variables, the addresses assigned to them are only valid for STs with TOS in RAM.
ADDRESS | NAME + SIZE | USE
. $400 evt_timer (.L): This is a vector that points to the address $FCA648 (I give the addresses valid for ST with TOS in ROM) and allows the execution of periodic functions of the GEM.
. $404 evt_critic (.W): This is a vector that points to the address $2A156 and is used when the operating system must handle an error from the floppy drive. (Drive not responding...)
. $408 evt_term (.L): This is a vector that is used by _TERM type functions of Gemdos which are used to end the execution of a program.
. $40C evt_xtra 5*(.L): 5 L-M are reserved for future applications...
. $420 memvalid (.L): Semaphore for a valid memory controller configuration.
. $424 memctrl (.B): If memctrl=4 the computer is a 520 ST
If memctrl=5 the computer is a 1040 ST
If memctrl=10 it's a MEGA ST
If memctrl=0 it's a 128 ST
. $426 resvalid (.L): If it is worth $31415926 (that is, PI without the comma), the address pointed by resvector will be used as a vector during a RESET.
. $42A resvector (.L): The vector used by resvalid during a RESET.
. $42E phystop (.L): Address of the end of the physical video memory.
. $432 _membot (.L): Address of the start of the memory area reserved for the programmer. (see organization of memory further on)
. $436 _memtop (.L): Address of the end of this area ($78000 for a 520 ST or $F8000 for a 1040 ST)
. $43A memval2 (.L): If memval2=$237698AA, the L-M confirms memvalid
. $43E flock (.L): If flock is different from 0, the VBL is stopped (See further for VBL)
. $440 seekrate (.W): Allows selecting the speed of movement of the floppy drive head from one track: 0=6 ms,1=12 ms,2=2 ms,3=3 ms
. $442 _timer_ms (.W): Elapsed time between 2 calls from the timer, i.e., the speed of the beats of the internal clock. (in milliseconds) You will always find 20 ms or 50 Hz there (See further for TIMERS)
. $444 _fverify (.W): If _fverify is different from 0, there is a writing test that is performed by the floppy drive: With each writing, it reads the written byte and compares it to the byte to be written.
In case of error, we get an error code
. $446 _bootdev (.W): Contains the number of the floppy drive that loaded the operating system.
. $448 palmode (.W): Flag indicating either that the system is in 50 Hz video mode (PAL) if palmode is different from 0 or that we are in 60 Hz video mode (Standard NTSC)
. $44A defshiftmd (.W): Contains the screen resolution in which we are if we switch from a High-resolution screen to a Color screen. (0=low,1=medium)
. $44C sshiftmd (.W): Contains the screen resolution in which we are: 0=LOW,1=MEDIUM, 2=HIGH
. $44E _v_bas_ad (.L): Contains the address of the beginning of the logical video memory. It is necessarily a multiple of 256
. $452 vblsem (.W): If this value is zero, the VBL is no longer executed: See further for the VBL
. $454 nvbls (.W): Contains the number of routines executed during the VBL (Logically 8)
. $456 _vblqueue (.L): Vector that points to the addresses of the routines executed during the VBL
. $45A colorptr (.L): If this L-M is different from 0, it is interpreted as the address of a new color palette.
. $45E screenpt (.L): If this L-M is not zero, it will be interpreted as the address of the new physical video memory.
. $462 _vbclock (.L): This is the VBL interruption counter (See the following chapter)
. $466 _frclock (.L): Contains the number of VBL routines that are not blocked by vblsem.
This number is also used for the RANDOM function of the XBIOS to obtain a random number.
. $46A hdv_init (.L): Vector that points to the hard drive initialization routine in $FC0D60
. $46E swv_vec (.L): Vector that points to the routine executed when a monochrome monitor is disconnected or connected (in $FC0020).
. $472 hdv_bpb (.L): See function code $7 in the BIOS (Getbpb)
. $476 hdv_rw (.L): Vector that points to the data writing and reading routines on a hard drive.
. $47A hdv_boot (.L): Vector that points to the startup (BOOT) routine of the hard drive
. $47E hdv_mediach (.L): Vector for the MEDIACH routine of the hard drive (To test if you change the disk: see MEDIACH code $4 in the BIOS)
. $482 _cmdload (.W): If this variable is different from 0, the program named 'COMMAND.PRG' will be loaded as soon as the operating system is in place. This variable must be modified on the BOOT SECTOR.
. $484 conterm (.B): This is a bit vector whose first 4 bits are used. If one of these bits is active, the function is used, otherwise it is disconnected.
Bit nr° 0: BEEP with each pressed key
Bit nr° 1: Keyboard repetition
Bit nr° 2: Ctrl+G gives a BEEP
Bit nr° 3: Allows you to adjust the BCONIN function of the BIOS.
. $492 themd2 (.L): Address of the beginning of the available memory
. $496 themd3 (.L): Address of the end of the available memory
. $4A2 savptr (.L): Address ($90C) that points to a memory area used to save the registers by the PROCESSOR after calling a BIOS or XBIOS function
. $4A6 _nflops (.W): Contains the number of connected floppy drives.
. $4A8 con_state (.L): Vector pointing to the code routines ('ESC' + 'value') for the effects of graphical text editing.
. $4AC save_row (.W): Temporary memory for the text cursor position for the ESC + 'Y' function
. $4AE sav_context (.L): Address pointing to a temporary memory used in case of an exception procedure.
. $4BA _hz_200 (.L): The system timer that is incremented 200 times per second.
. $4BC _the_env (.L): The default character string, positioned on 4 null bytes...
. $4C2 _drvbits (.L): Bit vector representing the drives connected according to the active bit number.
. $4C6 _dskbufp (.L): A 1 KB buffer used by disk operations.
. $4CE _vbl_list 8*(.L): Vector that points to the addresses of the 8 routines that are executed in the VBL.
. $4EE _dumpflg 8*(.L): Semaphore used by the Hardcopy function activated by pressing Alternate and Help.
. $4F2 _sysbase (.L): Address of the beginning of the operating system ($FC0000 on STs with TOS in ROM)
. $4FA _end_tos (.L): Address of the end of the operating system ($A100 on STs with TOS in ROM)
. $4FE exec_os (.L): Address of the beginning of the AES ($FD91D0)
. $502 dump_vec (.L): Vector that points to the hardcopy routine of function 20 of the XBIOS.
. $506 prt_stat (.L): Semaphore for the printer status
. $50A prt_vec (.L): Vector for printing functions on the printer
. $50E aux_sta (.L): State lookup vector used by a Hardcopy function
. $512 aux_vec (.L): Output vector used by a Hardcopy function
That is the list of these system variables, to modify them, you will just need to go into SUPERVISOR MODE and deposit the value you want. (Of course, you can also just content yourself to read the data that is deposited there...)
Therefore, if you want to know the resolution:
You will write:
* ;READING a SYSTEM VARIABLE:
SUPER ;MACRO to switch to SUPERVISOR MODE
move.w $44C,d0 ;$44C=sshiftmd
and You get the screen resolution in d0.W!!!
If you want to change the color palette, you will write:
* ;WRITING on a SYSTEM VARIABLE:
SUPER ;MACRO to switch to SUPERVISEUR MODE
move.l #PAL, $45A ;'PAL' address in $45A=colorptr
DATA
PAL DC.W $777, $007, $700, $070, $777, $777, $777, $777 ; the 16 colors
DC.W $777, $777, $777, $000, $000, $123, $456, $789
and You change the color palette!!!
This method applies to all system variables...
These variables are very interesting for us because thanks to them, it is no longer necessary to call certain BIOS, XBIOS, or GEMDOS functions to obtain basic graphic effects such as changing the palette, getting the resolution... :
This will be really very useful to us because these system variables are very easily modified or read. But the greatest interest we can draw from them is that modifying a system variable does not require the use of registers (SP or RETURN values), unlike BIOS, XBIOS, or GEMDOS functions...
This will be really very useful when we make our first interrupt-driven programs...
*** INTERRUPT-DRIVEN PROGRAMS ***
-----------------------------------
- An interrupt-driven program is a program that must be executed periodically at regular intervals.
Such a program will be the source of an EXCEPTION PROCESSING:
It will be executed if a certain condition is met (in practice: after a certain amount of time has elapsed) and interrupted (Upon encountering instructions provided for this purpose) to be called again only when the condition is met again.
An interrupt-driven program does not stop any function of the computer:
It is managed entirely separately.
It is quite possible that your main program performs a loop and AT THE SAME TIME, an interrupt-driven program executes without interrupting the main program!!
It is this property that makes interrupt-driven programs so useful...
(Not to say indispensable, because there are always interrupt-driven routines that are permanently activated for vital reasons...)
We will study all this in detail very soon, don't panic!
- There are EXCEPTION ROUTINES that are executed by the operating system if a certain specific condition is met.
They are programs that are executed exceptionally (hence their name!), i.e., if the condition that activates them is met.
These exception programs are pointed to by a series of VECTORS located in the first KB of memory that point to the address of the relevant EXCEPTION ROUTINE.
They are responsible for a number of vital functions of our MICROPROCESSOR.
We can consider that interrupt-driven programs are exception programs.
In practice:
You have probably already seen a number of BOMBS display on the screen when you created a program that did not work correctly. These bombs serve to identify the source of the error for the programmer (or the user) and come from exception processing.
There are 255 EXCEPTION VECTORS, and the number of bombs that display informs you of the number of the exception that has been solicited.
Here are the various EXCEPTION VECTORS and their memory locations.
They are LONG VECTORS (L-M) because they are ADDRESSES that point to the specific routines for the relevant VECTOR.
VECTOR NUMBER: ADDRESS OF: ROUTINE and USE
THE VECTOR
0 : $000 :SP after a RESET (.L)
1 : $004 :PC after a RESET (.L)
2 : $008 :BUS ERROR (2 BOMBS)
3 : $00C :ADDRESS ERROR (3 BOMBS)
4 : $010 :ILLEGAL INSTRUCTION (4 BOMBS)
5 : $014 :ERROR because a DIVISION BY ZERO has been detected. (The routine is actually just an RTE!)
6 : $018 :Used by CHK
7 : $01C :Used by TRAPV
8 : $020 :PRIVILEGE VIOLATION: An attempt has been made to touch data only accessible in SUPERVISOR MODE.
9 : $024 :Address of the routine executed after each instruction in TRACE mode
10 : $028 :LINEA emulation ERROR
11 : $02C :LINEF emulation ERROR
12 to 14 : $030 :Reserved for future applications
15 : $03C :UNINITIATED INTERRUPTION
16 to 23 : $040 :Reserved for future applications
24 : $060 :Secondary INTERRUPTION
25 : $064 :IPL 1
26 : $068 :IPL 2
27 : $06C :IPL 3
28 : $070 :IPL 4
29 : $074 :IPL 5
30 : $078 :IPL 6
31 : $07C :IPL 7
32 : $080 :Used by TRAP #0
33 : $084 :Used by TRAP #1 (GEMDOS)
34 : $088 :Used by TRAP #2 (AES/VDI)
35 : $08C :Used by TRAP #3
36 : $090 :Used by TRAP #4
37 : $094 :Used by TRAP #5
38 : $098 :Used by TRAP #6
39 : $09C :Used by TRAP #7
40 : $0A0 :Used by TRAP #8
41 : $0A4 :Used by TRAP #9
42 : $0A8 :Used by TRAP #10
43 : $0AC :Used by TRAP #11
44 : $0B0 :Used by TRAP #12
45 : $0B4 :Used by TRAP #13 (BIOS)
46 : $0B8 :Used by TRAP #14 (XBIOS)
47 : $0BC :Used by TRAP #15
48 to 63 : $0C0 :Reserved
64 to 255 : $100 :Available to the user, including:
68 : $110 :TIMER D Interruption
69 : $114 :TIMER C Interruption
72 : $120 :TIMER B Interruption
77 : $134 :TIMER A Interruption
The details will come later.
In practice, if your program displays 3 BOMBS before crashing, it means an address error has been detected, if it displays 4, it means an illegal instruction has been detected...
Here is how the computer proceeds to execute an EXCEPTION ROUTINE:
.Backup of SR in an internal register.
.Switch to SUPERVISOR MODE by activating the S bit of SR.
.Turn off TRACE MODE by extinguishing the T bit of the SR (Explanations on TRACE MODE later).
.Search for the exception vector to use.
.Stacking of PC and SR in the system stack.
.Loading into the PC of the address contained in the chosen EXCEPTION VECTOR: Jump to the corresponding routine...
.Execution of the exception program and return upon encountering an RTE (Return From Exception)
.Restoration of SR and PC registers.
It is quite possible to modify these vectors, just deposit the address (L-M) of the new routine to be executed at the address of the vector to be modified. (MOVE.L #NEW,$vector)
This will have the effect of diverting the execution of the EXCEPTION ROUTINE towards your own routine...
We will see this in detail when I talk about TRACE mode.
But let's get back to our interrupt-driven programs.
To manage several interrupt-driven programs at the same time, we will have to assign a PRIORITY LEVEL to our interrupt-driven program.
The higher this PRIORITY LEVEL, the greater the importance given to our program in the hierarchy.
The PRIORITY LEVEL of our program will be defined by the state of BITS I1, I2, I3 of the 'SR' STATUS REGISTER (Available ONLY in SUPERVISOR MODE!!)
These 3 BITS allow to define 8 levels of priority.
BITS I1 I2 I0 = % LEVEL (or Interrupt Priority Level)
% 1 1 1 7
% 1 1 0 6
% 1 0 1 5
% 1 0 0 4
% 0 1 1 3
% 0 1 0 2
% 0 0 1 1
% 0 0 0 0
An INTERRUPT LEVEL (or Interrupt Priority Level :'IPL') 0 program will therefore be interrupted by any other program with an IPL ò 1, a
program with IPL 1 will only be interrupted by a program with IPL ò 2
but will remain indifferent to a program under interruption with IPL 0, and
it will not be executed because its IPL is < 1 etc...
LEVEL 0 is theoretically that of your program.
(Lowest level)
LEVEL 7 is theoretically the IPL level that will allow the interrupted program to be unaffected by any other interrupted program.
(It could, for example, be the RESET, which has ABSOLUTE priority)
. In practice, only levels 2,4, and 6 are used.
IPL 2 is used by HBL
IPL 4 is used by VBL routines
IPL 6 is used by the MFP 68901 interruptions
HBL: Horizontal BLank
-------
It is actually a routine executed after the SHIFTER
displays 1 horizontal line on the screen.
Depending on the type of connected monitor, the routine is called every
50 microseconds (50Hz) or every 64 microseconds (64 Hz)
A color monitor has 200 horizontal lines, the screen is 'refreshed' 50 times per second (50 Hz), so the HBL routine is
called 10000 times per second in some conditions!
This routine would greatly slow down the computer, which is why the HBL routine is not executed. (fortunately for us!)
Indeed, from its first call, the routine will automatically set
the IPL of our program to an IPL level 3: the routine will no longer be
called because it has an IPL level 2...
NB: It is possible to hijack this routine:
--
Just deposit the address of your routine in the 26th
EXCEPTION VECTOR that points to the HBL routine (.L) and
reset the IPL of the program to a level < 2 ( By modifying
the I1,I2,I3 bits of the status register )
Your routine should also end with the RTE instruction
(Return From Exception), but we will talk about this soon...
(All in SUPERVISOR mode, don't forget!)
VBL routines: Vertical BLank
-------------------
They have an interrupt level 4, so they have priority
over the HBL interrupt.
These routines are executed after a graphic screen (200 or 400
horizontal lines depending on the resolution) has been fully
displayed on the monitor.
They run 50 times per second (50 Hz).
The VBL routines handle the change of the color palette ( Only after a complete image
has been drawn to avoid disturbing the image ), they test the change of
disks ...
There are 8 routines in total, the number of available routines is
contained in the system variable NVBLS and there is a vector
pointing to the 8 addresses of these routines, in VBLQUEUE.
In reality, only 1 routine is executed: The 7 others are
therefore available to us...
Interruptions of the MFP 68901 (Multi Function Peripheral)
------------------------------
The MFP 68901 manages 16 interruptions, they have a priority level 6,
so they have priority over the VBL and HBL interruptions.
The 16 interruptions of the MFP 68901 also have different levels of priority
among themselves!
Thus, an MFP interruption of level 6 can only be interrupted by another interruption of level > 6, etc...
Here are the different interruptions of the MFP 68901, classified by
priority level (noted x on the ILP 6 of the MFP).
LEVEL (x/6): The interruption
15/6 : Detector of connection or disconnection of the high-resolution monitor
14/6 : Ring indicator of the RS232 interface
13/6 : TIMER A of the internal clock
12/6 : Reception buffer of 1 character from RS232
intended for the user
11/6 : Reception error of 1 character from
the RS232 interface
10/6 : Temporary transmission buffer of 1 character to
the RS232
9/6 : Transmission error of 1 character from the RS232
8/6 : TIMER B and Line return counter
7/6 : Routine control of floppy disk drives and DMA
6/6 : Keyboard control routines (ACIAs) and MIDI port
(Input and output)
5/6 : TIMER C used by the YM-2149 (Sound), the keyboard and the
system synchronization counter (at 200 Hz)
4/6 : TIMER D used for RS232 transmission and reception operations
3/6 : Unused
2/6 : Routine control of the CTS of the RS232
1/6 : Routine control of the DCD of the RS232
0/6 : Used by the BUSY of the CENTRONICS interface to create
a printer spooler.
The above terms will not be explained, as you will not need to use them, so why complicate what is not
already simple?
We will still detail and explain the operating modes of the TIMERS, as it is their programming that will allow us
to create our interruption programs.
The TIMERS:
As you have seen, there are 4 TIMERS:
TIMER A, TIMER B, TIMER C, TIMER D.
. TIMER A is entirely available to the programmer and has a priority
level of 13 within the MFP (high!)
. TIMER B is used as a line return counter but you can easily hijack it. It has a priority level of 8 within
the MFP (IPL < TIMER A's IPL)
. TIMERS C and D are responsible for a number of important functions
(sound management, keyboard...), hijacking them can therefore
cause many problems...
Moreover, they have a LOW priority level within the MFP, which is
why we will not use them to install our interruption programs.
The programming of the TIMERS is done in a very particular
manner:
It will be necessary to program directly the registers of the CO-PROCESSOR MFP
68901!!