6502 routines

From BeebWiki
Jump to: navigation, search

Some useful 6502 routines.

Generate error from sideways ROM

When running code in a service ROM you can't generate an error with BRK as the current language will be in another ROM and won't be able to read it. The standard method is to copy the error to the bottom of the stack and execute it there.

 \ Generate inline error
 \ =====================
 PLA:STA &FD:PLA:STA &FE      :\ Pop address after JSR
 LDY #0                       :\ Index into the error block
 INY:LDA (&FD),Y:STA &100,Y   :\ Copy error block to stack
 BNE MkErrorLp                :\ Loop until terminating &00 byte
 STA &100:JMP &100            :\ Store &00 as BRK and jump to it

It is called with:

 JSR MkError
 EQUB errornumber:EQUS "error message":EQUB 0

The error number must not be zero as that will be matched as the terminator byte. &FD/E are allowed to be used as they are the error pointer anyway and will be overwritten immediately after the JMP &100 when the BRK handler sets them up.

Printing text

 \ Print inline text
 \ =================
 \ Corrupts A,Y
 LDY #1
 LDA (&F6),Y:BEQ PrTextEnd

This is called with:

 JSR PrText
 EQUS "text message"
 \ execution continues here

&F6/7 are allowed to be used as they are only otherwise used when calling OSRDRM and when ROMFS makes sideways ROM calls. As you can't *SPOOL text to ROMFS as it is a read-only filing system printing text won't result in a ROMFS filing system call that uses &F6/7.