Bankswitching, etc….

Author(s): Dr. Watson
Publication: LISTing Newsletter
Date: March - April 1987
Pages: 15-22

13. FUNCTION DISPATCHER

Located at location 6200H (or FA50H if D/FILE/2 is open), is a function dispatcher which will, when passed two arguments on the machine stack, perform various functions for applications and systems programmers.

The arguments passed to the function dispatcher are contained in a single word. Bits 0-14 contain the service code for the function you are requesting, and bit 15 is a jump/call selection flag. If bit 15 is set, the dispatcher will jump to the function you have requested. If bit 15 is reset, the dispatcher will call the routine and then return to its (the dispatcher’s) caller.

Table 5 is a list of functions currently provided by the dispatcher.

                  Table 5
            T/S 2000 FUNCTIONS

FUNCTION      SERVICE CODE  ACTION

W_TAPE             0        Write a block to tape.
R_TAPE             1        Read a block from tape.
RD_BIT             2        Read a bit from tape.
R_EDGE             3        Read an edge from tape.
SLVM               4        General tape routine.
LOAD               5        Load.
MERGE              6        Merge.
SAVE               7        Save.
CHNG_VID           8        Change video mode.
W_BORD             9        Write border color.
                  10        Reserved.
                  11        Reserved.
                  12        Reserved.
                  13        Reserved.
GET_STATUS        14
GET_NUMBER        15
BANK_ENABLE       16        Refer to paragraph 12 .
GOTO_BANK         17
CALL_BANK         18
XFER_BANK         19
                  20        Reserved.
                  21        Reserved.
                  22        Reserved.
                  23        Reserved.
                  24        Reserved.
UPD_K             25        Scan keyboard.
PARP              26        Sound routine.
BEEP              27        BEEP command.
K_DUMP            28        COPY command.
SENDTV            29        Send char. to screen.
SETAT             30        Set print position.
STTBYT            31        Fix attribute byte.

                Table 5 (CON'T)

FUNCTION      SERVICE CODE  ACTION

R_ATTS            32        Temp. Atts., Perm. Atts.
CLLHS             33        Clear lower half of screen.
CLS               34        Clear entire screen.
DUMPPR            35        Printer buffer sent to print.
PRSCAN            36        Send scan to printer.
DESLUG            37        Remove slugs from line buffer.
K_NEW             38        NEW command.
INIT              39        Initialize.
INCH              40        Input character.
SELECT            41        Select current stream.
INSERT            42        Insert bytes.
RESET             43        Reset calculator stack.
CLOSE             44        CLOSE command.
CLCHAN            45        Close channel.
OPEN              46        OPEN command.
OPCHAN            47        Open channel.
CAT               48        CAT command.
DELETE            49        DELETE command.
FORMAT            50        FORMAT command.
MOVE              51        MOVE command.
FLASHA            52        Flash char. in A to screen.
FINDL             53        Find BASIC line.
SUBLIN            54        Find sub-line.
RECLEN            55        Record length.
DELREC            56        Delete record.
PUT_LN            57        Send line to output.
SYNTAX            58        Check syntax.
EXECUTE           59        Execute line.
FOR               60        FOR command.
STOP              61        STOP command.
NEXT              62        NEXT command.
READ              63        READ command.
DATA              64        DATA command.
RESTBC            65        RESTORE bc.
RAND              66        RAND command.
CON'T             67        CON'T command.
JUMP              68        Jump to line.
FIX_U1            69        Fix 1 byte # from calc.
                            stack.
FIX_U             70        Fix 2 byte # from calc.
                            stack.
CLEAR             71        CLEAR command.
CLR_BC            72        CLEAR bc.
GO_SUB            73        GOSUB command.
CHKUSR            74        Free space left.
RETURN            75        RETURN command.
PAUSE             76        PAUSE command.
BREAK?            77        Break key pressed?
DEF               78        DEF command.
K_LPR             79        LPRINT command.
K_PRIN            80        PRINT command.
P_SEQ             81        Print sequence.
INPUT             82        INPUT command.

                Table 5 (CON'T)

FUNCTION      SERVICE CODE  ACTION

I_SEQ             83        Input sequence.
NOTKB?            84        Test CURCHL=KB.
COLOR             85        Adjust attributes sysvars.
HIFLSH            86        Adjust attributes sysvars.
SCRMBL            87        Screen address calculator.
PLOT              88        PLOT command.
PLOTBC            89        Plot c, b.
GET_XY            90        Get x and y.
CIRCLE            91        CIRCLE command.
DRAW              92        DRAW command.
DRAW_L            93        Draw line.
EXPRN             94        Expression Evaluator.
F_SCRN            95        Run time action for SCREEN $
F_ATTR            96        Run time action for ATTR.
RND               97        RND action.
F_PI              98        PI action.
F_INKY            99        INKEY action.
FIND_N           100        Find variable.
PSHSTR           101        Push string.
PAEDCB           102        Push A, E, D, C, B.
LET              103        LET command.
POPSTR           104        Pop string.
DIM              105        DIM command.
STKUSN           106        Stack unsigned number.
STK_A            107        Stack A.
STK_BC           108        Stack BC.
ININT            109        Read/Stack Integer.
FP2BC            110        Get 16 bit #.
FP2A             111        Get 8 bit #.
OUTPUT           112        Output number on stack.
SUB              113        Subtract.
ADD              114        Add.
MULT             115        Multiple (int).
TIMES            116        Multiply (F.P.).
DIVIDE           117        Divide.
TRUNC            118        Truncate.
FLOAT            119        Force Int F.P.
INTDIV           120        Integer Divide.
INT              121        INT.
EXP              122        EXP.
LN               123        LN.
ANGLE            124        Angle calculator.
COS              125        COS.
SIN              126        SIN.
TAN              127        TAN.
ATN              128        ATN.
ASN              129        ASN.
ACS              130        ACS.
ROOT             131        SQR calculator.
TO_THE           132        Exponentiation.
RDCH             133        Read character.
SENDCH           134        Send character.

                Table 5 (CON'T)

FUNCTION      SERVICE CODE  ACTION

WRCH             135        Write character
K_SCAN           136        Keyboard scan
P_LFT            137        Print cursor left
P_RT             138        Print cursor right
P_NL             139        Print newline
PUTMES           140        Print message
K_CLS            141        CLS command
SCRL             142        Scrolling routine
F_PNT            143        Point action.

Editor’s Note:

As an example of the use of the Function Dispatcher, consider the use of PLOTBC routine. This routine is on page 215 of Jeff Mazur‘s book “Timex Sinclair 2068- Intermediate/Advanced Guide”. The original example has an error in both the assembly as well as in the Basic Data statement, which causes the computer to crash. I will now quote Jeff Mazur:

” The routine performs the equivalent of the BASIC statement PLOT C,B. The command has four variations depending on the value stored in the system variable PFLAG(23697). Bit 0 of this variable is called XOR-CH; bit 2 is called INV-CH. Table 7 outlines the significance of these bits to the PLOTBC function. Normally this routine will cause a pixel to be set.

In the following listing a machine language routine that uses the Function Dispatcher and PLOTBC to draw a vertical line on the screen. This involves plotting 174 seperate points from within a loop. Since the B register holds the vertical coordinate for the PLOTBC operation, we use the DJNZ instruction to create the loop. We,therefore, start by initializing the B register to 175 and loading the C register with the desired X-coordinate (in this acse 127). We can load both registers simultaneously using the LD BC instruction. Next we begin our loop by saving the BC register on the stack. This is because the contents of this register will get changed after calling the Function Dispatcher. We then need to PUSH two sets of 2-byte zeros to set up the stack for the Dispatcher. We PUSH the service code onto the stack and we’re ready to CALL the Function Dispatcher. After plotting the point indicated by register B and C, the routine returns to our POP BC instruction. This recovers the coordinates of the point just plotted. The DJNZ instruction lowers the Y-coordinate by one and then jumps back to plot the next point. After 174 points have been plotted, the routine returns to BASIC. By using DJNZ instruction, we do not plot a point at the zero Y-coordinate point.

        LD BC,7FAF    ;Start at (127,175)
LOOP PUSH BC ;Save coordinates
LD DE,0000 ;Set up
PUSH DE ;Function
PUSH DE ;Dispatcher
LD DE,89 ; Get Service Code
PUSH DE ;and push it
CALL 6200H ;Go to Fcn. Disp.
POP BC ;Restore coordinates
DJNZ LOOP ; Do another point
RET

BASIC Listing

10 CLEAR 65000
20 FOR a=65001 TO 65020
30 READ d: POKE a,d
40 NEXT a
50 DATA 1,127,175,197,17,0,0,213,213,17,89,0213,205,0,98,193,16,239,201

The equivalent BASIC prog. is a one liner:

10 FOR i=175 TO 1 STEP-1:PLOT 127,[illegible]:NEXT i

The following paragraph describes some of the more useful functions provided by the dispatcher.

13.1 W_TAPE.

Write a block of data from memory to cassette tape.

This routine will write the number of bytes specified in the DE register from memory to TAPE. The IX register points to the first byte to save. This byte should be preceded by the byte in A which is used to identify the type of block:

Usually 0 for data
-1 for header

The format of data on tape is a leader at 806.5-Hz, one cycle of 2040-Hz, and then data. The data is MS bit first. A zero bit is 1-cycle of 2040-Hz. A one bit is 1-cycle of 1020 Hz. Lastly comes the parity byte. This is the XOR of all data bytes — formatted as a data byte. All frequencies are nominal.

This routine aborts with an Report Code D if the BREAK key is pressed.

13.2 R_TAPE

Read a block of data to memory from cassette tape.

This routine reads in the number of bytes specified in the DE register from tape. The routine should be entered with the A register equal to the block type:

0 = data
-1 = header

The IX register should point to the memory location into which the first data byte should be loaded.

The routine returns carry set if there were no errors. It returns carry reset if the checksums did not match, or the block consisted of fewer than DE bytes or the verify failed or the block type was wrong.

The routine will abort with an Report Code D if the BREAK key is pressed.

13.3 CHNG_VID

Change video mode.

This function is used to “open” and “close” the second display file (and its corresponding attributes file – D_FILE_2 and A_FILE_2). If the second display file is closed, it should not be used by the applications programmer because data structures used by the Operating System currently overlay the needed memory area.

To access this routine, you merely pass it a value in the accumulator. Valid values are shown in Table 6.

                 Table 6
     CHANGE VIDEO MODE ACCESS CODES

VALUE     FUNCTION

0         Normal video mode (D/FILE/1)
1         Second display page (D_FILE_2)
2         Ultra-high-resolution color mode (D_FILE_1 and
          D_FILE_2)
6         64-column mode - paper black.
8+6       64-column mode - paper blue.
16+6      64-column mode - paper red.
24+6      64-column mode - paper magenta.
32+6      64-column mode - paper green.
40+6      64-column mode - paper cyan.
48+6      64-column mode - paper yellow
56+6      64-column mode - paper white.

A system variable VIDMOD, keeps track of the current display mode (see Table 6 above).

Note that using the second display file means that the function dispatcher, and system stack will move to the top of memory. This means that you should only open the second display file if you are operating a 48K machine.

13.4 BREAK?

Read the BREAK key.

This routine looks directly at the keyboard to determine if both CAPS SHIFT and SPACE are being depressed (i.e.; BREAK). It returns carry reset if the BREAK key is depressed.

13.5 FIND_N

Parse and find a specified variable.

This routine will skip CHADD over the identifier (variable name) specified. It will adjust bit 6 of FLAGS for the type of the variable as follows:

Set = Numeric
Reset = String

It will return with NC if the system is checking syntax, or if the variable was found when FIND_N searched the variables area (VARS) for the specified variable.

It will return with Z if array. (i.e.; ‘(‘ present if variable not found or checking syntax and array or string if running and variable found).

If running (i.e., not checking Syntax), Then HL–> :

Last character of identifier in record if found (NC),
First character of name in text if not found (C),

C := First character of name with bits 5 and 6 right, and bit 7 OFF.

If checking syntax, C gets bits 0 – 4 OFF, 5 and 6 correct (as above), and bit 7 OFF.

13.6 RDCH

Wait for a character from current channel.

This routine waits for a character from the current input stream. It places the character in A.

If the current device is a finite device, and an end of file arrives, then RDCH aborts with an Report Code 8.

13.7 INCH

Get character from current channel without waiting.

This routine reads a character from the current input stream without waiting. It returns C if the character is OK. It returns NC, NZ if end of file (for a finite device; i.e.: one like a disk file for which it can always be told if there are any more characters to come).

It will return NC, Z if no character yet (for an infinite device, like the keyboard).

13.8 SELECT

Select the current input stream.

The routine sets the current channel to be that for stream A.

Specified Streams

STREAM     ID NO.    CONNECTION

HID-K        -3      Keyboard
HID-S        -2      Screen
HID-R        -1      RAM insertion
COM-ST        0      Stream for commands
INP-ST        1      Stream for input data
PR-ST         2      Stream for PRINT
LPR-ST        3      Stream for LPRINT

Streams whose ID numbers are less than zero, are referred to as hidden. They are tied unalterably to specific channels.

13.9 SCRMBL

Screen address calculator.

This routine will set HL pointing at a byte in the primary display file, given BC holding PLOT-type coordinates. B should be holding the y-coordinate, and C should be holding the x-coordinate. The accumulator will hold the bit number in (HL) where the pixel is stored. (Zero for left-hand = MS end, one for right-hand = LS end.)

The routine preserves DE.

13.10 PLOTBC

Plot a point on the screen.

This routine performs the equivalent of PLOT c,b. It sets COORDS to the coordinates specified.

There are four options depending on two bits of PFLAG, as listed by Table 7. Bit 0 is XOR-CH. Bit 2 is INV-CH.

          Table 7
      PLOTBC OPTIONS

XOR-CH   INV-CH   FUNCTION

  0        0      Set bit.
  0        1      Reset bit.
  1        0      Invert bit.
  1        1      Leave bit.

This routine adjusts the proper attributes byte for the primary display file.

END.