Checking calls have been actioned

From BeebWiki
Revision as of 19:12, 8 March 2015 by WikiSysop (talk | contribs) (1 revision)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Rather than building assumptions into a program as to what calls may or may not be available to the running program, the program must just do the call anyway and test the return values to check that it has been actioned. For instance, a program must never say "this is not a Master, therefore I will refuse to look for a Real-Time Clock". It must actually ask for the Real-Time clock and check that it has actually been given an answer.

 !X%=1:A%=14:CALL OSWORD
 IF !X%=1 THEN "No RTC" ELSE PRINT "RTC=";~!X%

OSBYTE

When an OSBYTE call is made, if it is not implemented by the Operating System it is passed around the sideways ROMs to see if any will service it. A result of this is that if no ROM claims it then the X register is left set to the &FF after stepping through the ROMs. This can be used to test if an OSBYTE call has been actioned or not.

   IF FNbyte(25,0,0)=255 THEN PRINT "Font not reset"

With some calls a more useful test is to just check bit 7 as then the OSBYTE code can return other information in the other 6 bits of X. This is particularly useful as the only unused OSBYTEs are <128 and OSBYTEs <128 only pass and receive information in the X register.

 IF FNbyte(25,0,0) AND &80 THEN PRINT "Font not reset"
 IF FNbyte(108,1,0)=255 THEN PRINT "No shadow RAM"
 
 .vramSelect            :\ See Paging in video memory
 TYA:PHA:AND #1:PHA:TAX :\ A=0 main RAM, A=1 video RAM
 LDA #108:JSR OSBYTE    :\ Attempt to select Master video RAM
 PLA:INX:BNE vramOk     :\ X<>255, successful
 EOR #1:TAX             :\ A=1 main RAM, A=0 video RAM
 LDA #111:JSR OSBYTE    :\ Attempt to select Aries/Watford RAM
 .vramOk
 PLA:TAY:RTS

When writing sideways ROMs that implement OSBYTEs it is useful to use b7 and b6 of the returned X value as 'present' and 'enabled' flags in the following way:

 Returned X=%11xxxxxx - ROM absent (will be default value of &FF)
 Returned X=%10xxxxxx - ROM present but disabled
 Returned X=%0xxxxxxx - ROM present and enabled

This allows code to test bit 7 on the return from a call to see if it has been actioned. Inaction because the ROM is absent and inaction because the ROM is present but disabled are functionally the same as far as the calling code is concerned. This protocol is used in the OSBYTE 90 sideways ROM status call.

OSWORD

When an OSWORD call is made if it is not actioned then the control block will be left unaltered, as in the Real-Time Clock example above. This is how you detect if an OSWORD call has been actioned or not. You can also pre-load any 'result' byte in the control block with a default result to return if the OSWORD call does not get actioned.

 X%?10=&1E:A%=127:CALL OSWORD :REM If no OSWORD &7F present, result is &1E

Other calls

Preloading a control block with a default value is the way to make other calls take a default value if the call is not implemented. For example:

 used%=FNargs(4,0,0)  :REM Get disk space used as &00000000 if OSARGS 4 not supported
 used%=FNargs(4,0,-1) :REM Get disk space used as &FFFFFFFF if OSARGS 4 not supported
 free%=FNargs(5,0,0)  :REM Get disk free space as &00000000 if OSARGS 5 not supported

Jgharston 02:57, 10 September 2012 (UTC)