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
 \ =====================
 .MkError
 PLA:STA &FD:PLA:STA &FE      :\ Pop address after JSR
 LDY #0                       :\ Index into the error block
 .MkErrorLp
 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
 .PrText
 PLA:STA &F6:PLA:STA &F7
 LDY #1
 .PrTextLp
 LDA (&F6),Y:BEQ PrTextEnd
 JSR OSASCI:INY:BNE PrTextLp
 .PrTextEnd
 CLC:TYA:ADC &F6:TAY
 LDA #0:ADC &F7:PHA
 TYA:PHA:RTS

This is called with:

 JSR PrText
 EQUS "text message"
 EQUB 0
 \ 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.