10
20
30
40
50 :
60
70
80 :
90 OSARGS=&FFDA:OSFILE=&FFDD:GSINIT=&FFC2:GSREAD=&FFC5
100 OSBYTE=&FFF4:OSRDCH=&FFE0:OSWRCH=&FFEE:OSNEWL=&FFE7
110 DIM mcode% &200:load%=&FFFF0900
120 src=&A8:dst=&AA:rom=&AC:flg=&AD
130
140 :
150 FOR P=0 TO 1
160 P%=load%:O%=mcode%
170 [OPT P*2+4
180 .text
190 EQUS "Press SPACE to save"
200 .errSyntax
210 BRK:EQUB 220:EQUS "Syntax: SRSave <afsp> <rom> (P)":BRK
220 :
230 .exec%
240 LDA #0:LDX #1:JSR OSBYTE :\ Find machine type
250 TXA:BNE CmdLine :\ If not Electron, X<>&00
260 LDA #&05:STA ROMSEL+1 :\ Change to Electron ROMSEL
270 .CmdLine
280 STY rom:STY flg :\ ROM=0, clear flags
290 LDA #1:LDX #src:JSR OSARGS :\ Find command line (Y=0 from above)
300 LDA src+0:STA &F2:LDA src+1:STA &F3 :\ GSTEXT=>command line
310 CLC:JSR GSINIT:BEQ errSyntax
320 .loop1
330 JSR GSREAD:BCC loop1 :\ Step past filename
340 .loop2
350 LDA (&F2),Y:INY:CMP #32:BEQ loop2 :\ Skip spaces
360 CMP #13:BEQ save :\ End of line, save the file
370 CMP #ASC"+":BEQ loop2 :\ Skip '+'
380 CMP #ASC"0":BCC errSyntax :\ <'0', error
390 CMP #ASC"9"+1:BCC digit :\ '0'-'9', set ROM number
400 CMP #ASC"A":BCC errSyntax :\ '9'-'A', error
410 AND #&DF
420 CMP #ASC"G":BCC digithex :\ 'A'-'F', set ROM number
430 CMP #ASC"P":BNE loop2 :\ 'P'ause -> 0 -> 1
440 \CMP #ASC"Q":\BEQ option :\ 'Q'uick -> 1 -> 2
450 \CMP #ASC"R":\BEQ option :\ 'R'un -> 2 -> 4
460 \CMP #ASC"I":\BNE jmperror :\ 'I'nsert
470 \ADC #1 :\ Convert 'I' to 3 -> 8
480 .option
490 \AND #3:\TAX:\LDA BitMap,X
500 LDA #1
510 .option2
520 ORA flg:STA flg:BNE loop2
530 .digithex
540 SBC #6
550 .digit
560 AND #15:LDX #4
570 .digitlp
580 ASL rom:DEX:BNE digitlp
590 ORA rom:STA rom :\ Move digit into ROM number
600 LDA #&80:BNE option2 :\ ROM number specified
610 \.BitMap
620 \EQUB 1:\EQUB 2:\EQUB 4:\EQUB 8
630 .jmperror
640 JMP errSyntax
650 :
660 .save
670 LDA &F4:PHA
680 :
690 LDA rom:CMP #&10:BCC presave :\ rom<&10 - use as hex
700 SBC #6:STA rom :\ rom=&10-&15 - use as decimal
710 CMP #16:BCS jmperror :\ rom>15
720 :
730 .presave
740 LDA src+0:STA blk+0 :\ Point to filename
750 LDA src+1:STA blk+1
760 LDA #&84:JSR OSBYTE :\ Read top of memory
770 CPY #&70:BCS savefile1 :\ Enough free memory
780 LDA #22:JSR OSWRCH
790 LDA #&87:JSR OSWRCH :\ Change to MODE 7
800 LDA #&84:JSR OSBYTE :\ Read top of memory again
810 .savefile1
820 STY blk+15 :\ end=HIMEM
830 TYA:SEC:SBC #&40:STA blk+11 :\ start=HIMEM-&4000
840 STA dst+1 :\ dst=HIMEM-&4000
850 :
860 LSR flg:PHP:BCC savefile :\ No 'P'ause, flag is now flg/2
870 LDY #0:BEQ prmsg
880 .prmsglp
890 JSR OSWRCH:INY
900 .prmsg
910 LDA text,Y:BNE prmsglp
920 :
930 .savefile
940 LDX rom:JSR SelectRom :\ Select ROM
950 LDY #0:STY src+0:STY dst+0 :\ Also set Y=0 for loop
960 LDA #&80:STA src+1 :\ src=&8000
970 .copylp
980 LDA (src),Y:STA (dst),Y :\ Copy data
990 INY:BNE copylp
1000 INC src+1:INC dst+1
1010 BIT src+1:BVC copylp
1020 :
1030 LDA &8006 :\ Check Lang+Tube
1040 ASL A:BPL noreloc :\ Not a language, use &FFFF8000
1050 SEC:LDY #2 :\ If no reloc, start with Y=2, CS
1060 AND #&40:BEQ savelp2 :\ No relocation address, use &00008000
1070 LDX &8007 :\ Offset to (C)
1080 .savelp1
1090 INX:LDA &8000,X:BNE savelp1 :\ Step past (C) message
1100 CLC:LDY #0
1110 .savelp2
1120 BCS P%+5:LDA &8001,X :\ Get Tube address
1130 STA blk+2,Y:STA blk+6,Y :\ Copy to control block
1140 INX:INY:TYA:AND #4:BEQ savelp2
1150 .noreloc
1160 :
1170 PLP:PHP:BCC savego
1180 JSR OSRDCH :\ Wait for a keypress
1190 \ We displayed the message before copying the data so we don't
1200 \ overwrite the data with the prompt message
1210 :
1220 .savego
1230 LDX #blk AND 255:LDY #blk DIV 256
1240 LDA #0:JSR OSFILE :\ Save file
1250 PLP:BCC done:JSR OSNEWL :\ Newline after saving
1260 .done
1270 PLA:TAX :\ Restore ROM
1280 .SelectRom
1290 LDA #12:JSR SelectRom2:TXA
1300 .SelectRom2
1310 STA &F4
1320 .ROMSEL
1330 STA &FE30:RTS :\ Select ROM to read
1340 :
1350 .blk
1360 EQUW 0:EQUD &FFFF8000:EQUD &FFFF8000
1370 EQUD &FFFF3000:EQUD &FFFF7000
1380 EQUS "v1.05"
1390 ]NEXT
1400 IFP%>&B00:PRINT"Code overrun":END
1410 PRINT" *SAVE SRSave ";~mcode%;" ";~O%;" ";~exec%OR&FFFF0000;" ";~load%