10
20
30
40 IFPAGE>=&8000THENPRINT"6502 Machines only!":END
50 ?&284=&FF:$&404=STRING$(26*4-1,CHR$0):@%=&90A:Z%=0
60 DEFFNlo(A%)=A%AND&FF
70 DEFFNhi(A%)=(A%AND&FF00)DIV&100
80 DEFFNhex(X%,Y%)=RIGHT$("0000000"+STR$~X%,Y%)
90 FORP%=&900TO&AFCSTEP4:!P%=0:NEXT:!&A04=&20030000:?79=3:!80=&900:!84=&4B03:!88=&22:A%=&7F:X%=79:Y%=0:CALL&FFF1
100 FORP%=&900TO&CFCSTEP4:!P%=&E5E5E5E5:NEXT:FORQ%=0TO2STEP2:P%=&B00:[OPTQ%
110 EQUS"(C)JR":.gcfs%SEC:JMP&FFC5:.code%CLD:LDA#1:LDX#&A8:LDY#0:JSR&FFDA:STY&AB:LDA&A8:STA&F2:LDA&A9:STA&F3
120 LDX#&FF:SEC:JSR&FFC2:JSR gcfs%:BCS cde1%:EOR#&30:CMP#4:EQUW&490:EOR#&30:BCS cde0%+5:TAX:JSR gcfs%:BCS cde1%:CMP#&20:BNE err0%
130 .cde0%JSR gcfs%:BCS cde1%:AND#&DF:CMP#&50:EQUW&3D0:ASLA:EQUW&4D0:CMP#&53:BNE err0%:BIT&AB:BNE err0%:ORA&AB:STA&AB:BNE cde0%
140 .cde1%STX osbk%:ASL&AB:BCC cde2%:LDX#FNlo(&FF-err0%+chms%):LDA chms%-FNlo(&FF-err0%+chms%),X:JSR&FFEE:INX:BNE cde1%+9:JSR&FFE0:PHP:JSR&FFE7:PLP:BCC cde2%:LDA#&7D:JMP&FFF4
150 .chms%EQUS"Change disc and press a key":.err0%EQUW&DC00:EQUS"Syntax: SWAP (<drive>) (P)(S)"
160 .err1%EQUW&B400:EQUS"Dir not empty":.err2%EQUW&FF00:EQUS"Bad SWAP disc":BRK
170 .cde2%JSR rdsc%-6:LDA#&F8:JSR cknm%:BCC ctsw%:JSR swsc%:LDA#1:JSR swsc%:BPL cde3%
180 .ctsw%BIT&AB:BPL err2%:LDA&A05:BNE err1%:LDY#&F0:JSR mkct%:LDA&A06:AND#7:LSRA:PHA:TAX:LDA&A07:RORA:PHA
190 SEC:SBC#6:EQUW&1B0:DEX:STA&A0D:TXA:ASLA:ASLA:ASLA:ASLA:STA&A0E:TYA:LDY#&22:JSR wtsc%-4
200 LDY#&F8:JSR mkct%:PLA:STA&A07:PLA:STA&A06:LDA#2:LDY#&24:JSR wtsc%-2
210 .cde3%JSR rdsc%-6:JSR cknm%-2:PHP:TAY:LDA cat0%+8,Y:JSR&FFEE:INY:EOR#&20:BNE cde3%+8:PLP:ROLA:EOR#&31:JSR&FFEE:LDA#&12:JSR&FFF4:JMP&FFE7
220 LDA#&F0:.cknm%TAY:CLC:ADC&A05:BCC cknm%+20:TAX:LDA cat0%-&F0,Y:EOR&908,X:EQUW&1F0:CLC:INX:INY:TYA:AND#7:BNE cknm%+8:RTS
230 .mkct%LDX#0:LDA cat0%-&F0,Y:STA&908,X:LDA cat1%-&F0,Y:STA&A08,X:INX:INY:BNE mkct%+2:STX&A05:RTS
240 .swsc%LDY#&21:JSR rdsc%-2:INX:EOR#2:JSR rdsc%:JSR swsc%+15:DEX:EOR#2:BPL wtsc%
250 LDY#&22:LDX#9:.wtsc%PHA:LDA#&4B:BNE rdsc%+3:LDA#0:LDY#&22:LDX#9:.rdsc%PHA:LDA#&53:STA osbk%+6:PLA
260 .oswd%STA osbk%+8:STX osbk%+2:STY osbk%+9:LDA#&7F:LDX#FNlo(osbk%):LDY#FNhi(osbk%):JSR&FFF1
270 LDY osbk%+9:LDX osbk%+2:LDA osbk%+10:EQUW&4D0:LDA osbk%+8:RTS:EQUW&C700:EQUS"Disc error":BRK:.osbk%BRK:EQUD&FFFF0900:EQUD3:BRK
280 .cat1%EQUD0:EQUD&6000000:EQUW&900:EQUWcode%:EQUD&2CC0400:.cat0%EQUD&212121A1:EQUD&A1212121:EQUD&50415753:EQUD&A4202020
290 ]:NEXT:PRINT"Ended @:";~P%:IF&D00<P%THENPRINT"Code too long!":END
300 OSCLI"SAVE :1.SWAP FFFF0900+400 "+STR$~code%+" FFFF0900"
310 PRINT"Double cat system saved"