This is an old revision of the document!


UPI Conventions

Bally Arcade system subroutines are called by executing a RST 38H instruction, followed by one or more parameters. The first parameter is a byte defining which subroutine to execute, whether or not additional parameters for that subroutine follow this parameter, and an indicator bit used for calling user-defined subroutines.

Example

RST     38H     ; call subroutine
DB      81      ; type, subroutine #, option
DB      30      ; passed parameter

The first byte executes the subroutine call. The second byte is the first parameter. It breaks into the following segments (bits read right-to-left):

Bit(s) Function Options
0 Option 0 = no parameters follow; 1 = parameters follow in subsequent bytes
1–6 Subroutine number (always even) to transfer to
7 Subroutine type 0 = standard subroutine; 1 = user-defined subroutine

The option bit (0) specifies how arguments are passed to the system routine. If option = 0, the arguments are presumed to exist in CPU registers; if option = 1, the arguments are taken to follow in subsequent bytes. These arguments are loaded into the CPU registers automatically before the called routine is entered. (The argument(s) required by each system routine are given in the system routine description sections.)

In the example above, the first parameter byte 81 expands to 01010001B. Bit 0 is 1, indicating that parameters will follow this byte. Bits 1–6, 101000B, indicate subroutine $50 (80D). Bit 7 is set to 0, indicating that this is a standard subroutine call.

According to the onboard subroutines list, subroutine 80 is PAWS, which waits for a user-defined number of interrupts, then returns. It requires one parameter, the number of interrupts, to be passed in register B. So the third byte in the example above is passing 30 to B, requesting a 30-interrupt pause.

For standard subroutines, programmers do not need to list the verbose RST call as illustrated in the example above. Instead, HVGLIB provides a list of symbolic identifiers for each system subroutine. Those identifiers are then invoked using the SYSTEM and SYSSUK calls.

SYSTEM and SYSSUK

The Bally Arcade uses two labels, SYSTEM and SYSSUK, to call system routines. Internally, both labels are represented by $FF, the “system sentinel” byte that readies the Arcade for a system routine number in the following byte. Their difference lies in how arguments are passed to the requested system routine.

The SYSTEM macro generates the system routine call with option = 0. Option 0 indicates that any system routine arguments will be loaded in their appropriate registers immediately prior to the SYSTEM call. For instance, returning to the example above, a user could execute the same interrupt wait routine using the following syntax:

LD         B,30
SYSTEM     PAWS

The SYSSUK macro generates the system routine call with option = 1. Option 1 indicates that any system routine arguments will be loaded in their appropriate registers directly after the SYSSUK call. In other words, the system 'sucks' them into the routine. The prior example would now look like this:

SYSSUK     PAWS
DB         30

Both methods have advantages and disadvantages. As Andy Guevara wrote in the Astrocade Machine Language Programming Tutorial:

The advantage of the SYSSUK structure is more compact code. The disadvantage is the inflexibility. It is better used for absolute cases than for iterative loops. Like for borders that will always be in the same place, rather than objects that will move across the screen.

In short, the choice of SYSTEM/SYSSUK depends on your specific programming task.

The Interpreter

Frequently it is desirable to string several system routine calls together. If four or more calls follow in sequence, it is more efficient to use the interpreter, because it voids the overhead of the RST 56 instruction. Once the interpreter is invoked, SYSSUK/SYSTEM calls are replaced with DO/DONT calls, respectively.

Special call indexes INTPC and EXIT are used to enter and exit interpretive mode.

Example: Interpreter Call and Exit

     SYSTEM     INTPC          ;BEGIN INTERPRETING
     DO         FILL           ;DO FILL ROUTINE
     DW         NORMEM         ;START AT TOP OF SCREEN
     DW         92*BYTEPL      ;CONTINUE FOR 92 LINES
     DB         0              ;FILL WITH ZEROES
     DO         CHRDIS         ;DO CHARACTER DISPLAY ROUTINE
     DB         0              ;CHARACTER Y-COORDINATE
     DB         10             ;CHARACTER X-COORDINATE
     DB         8              ;OPTIONS-PLOP, 10-ON, 00-OFF
     DB         "A"            ;CHARACTER TO DISPLAY
     EXIT                      ;EXIT INTERPRETER

System Routine Format

A system routine is coded like a conventional machine language subroutine, except that output parameters are not passed through registers but through the context block.

The context block is created by the RST 56 call. The user's register set (AF, BC, DE, HL, IX, IY) is pushed onto the stack. Register IY is set to point at this stack frame. Thus a copy of the input arguments exists in RAM, which the system routine may refer to as needed. These arguments are also present in the registers when the system routine is entered, so it is only necessary to refer to the context block when one has clobbered an input argument.

An output argument is returned to the caller by setting it in the context block. If a register was changed, but the associated cell in the context block was not, then the register will have its old value on return. Thus a system routine is free to use any of the registers it needs without worrying about saving and restoring. The user can also assume that no registers will change except those defined as returning an output argument.

The Context Block

The following illustration describes the context block and equates provided in HVGLIB for each field.

The UPI uses four tables in the control transfer process. The first two tables give the routine's starting address indexed via call number. The systems table is named SYSDPT. The user may extend this table by storing the address of their extended table into USERTB, USERTB+1. This address should point 128 bytes before the first entry.

The remaining two tables describe, if specified, the inline arguments a call expects. This table gives a one-byte bitstring, also indexed via call number. The systems name is MRARGT. The user's address is in UMARGT. UMARGT must point 64 bytes ahead. Arguments must follow the call in a specified order.

Note that the context contains additional information not shown. This information exists both above and below the context. User programs should never use this information or even assume that it exists. The user should only address this area by using IY.

Inline Argument Mask Table Entry

If optioned (i.e., bit 0 of call index is set), the Macro Routine Argument Table (MRARGT) is indexed giving a mask which specifies how to transfer inline arguments into the context block. The mask is formatted as follows:

Arguments must follow the call index in the following order (if an argument isn't specified, it is omitted):

(INDEX), IXL, IXH, E, D, C, B, A, L, H

Registers that are not defined as containing output parameters will not change.

All UPI routines are reentrant.

User-Defined Subroutines

Section needs clarification.

The UPI has been extended to support user-provided system routines. If the interpreter encounters a negative call index (i.e., bit 7 of the subroutine number is set), and “suck inline” is optioned, the user's macro routine address table (USERMT) and argument table (UMARGT) are used. The User Macro Routine Argument Table (UMARGT) is indexed for a parameter mask. The address of this table is assumed to be in (UMARGT),(UMARGT+1). This pointer should point 64 bytes before the first real entry.

Example

     LD        HL,USERMT-64     ; WHERE USERMT POINTS AT
     LD        (UMARGT),HL

The user must set up the subroutine as follows:

Location Function
4FFB/4FFC (Base address of register load specifications) - 40H
4FFD/4FFE (Base address of subroutine jump table) - 80H

The user is responsible for storing the addresses of these tables into dedicated system RAM cells.


From HVGLIB:

; User UPI Routines, even numbers from $80 to $FE ( + 1 for SUCK):
UMARGT  EQU     $4FFB   ; User Mask ARGument Table + (routine / 2)
USERTB  EQU     $4FFD   ; USER Table Base + routine = JumP address

Example: Space Fortress User Subroutine

;  User's Macro Subroutine $80
;  ---------------------------
;    JumP address          = $2027 ($1FA7 + $80)
;    Mask ARGument address = $2029 ($1FE9 + ($80/2))
;  User Macro $80 has two arguments based on a mask of 0000 0011B
;  Registers D and E are loaded when the User Macro is SYSSUKed. 
;  See BIOS listing for more details.
L2019:  EXX     
        LD      HL,L2025
        PUSH    HL
        LD      HL,($010D)
        PUSH    HL
        EXX     
        DI      
        RET     
 
L2025:  EI      
        RET
 
;  
L2027:  DW      L2019           ; User's Macro Subroutine $80, JumP address
L2029:  DB      00000011B       ; User's Macro Subroutine $80, Mask ARGument 
 
; [...]
 
;  Call User's Macro Subroutine $80
;  Requires two inline arguments.
;  Loads E and D (in that order)  
L2FF3:  SYSSUK  $80             ; User's MACRO routine $80
        DB      $14             ; Inline argument for register E 
        DB      $02             ; Inline argument for register D

[NM:2-7, source listing p. 20]