-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaverom.6502
150 lines (123 loc) · 3.62 KB
/
saverom.6502
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
\ SaveROM: A simple tool to save Sideways ROM contents to a file.
INCLUDE "tools/common.6502"
ORG &1D00
GUARD &6000
.begin
\ Check that we have enough free memory for the buffer
\ We need up to &5FFF
OSCALL_A osbyte, &84 \ OSBYTE &84: read start of screen memory into XY
CPY #&60:BCS ram_ok \ Check high byte to make sure screen memory starts at >=&6000
LDA #&16:JSR osasci:LDA #&06:JSR osasci \ Switch into a suitable mode
.ram_ok
\ Make sure we're not running on a second processor
OSCALL_AXY osbyte, &EA, &00, &FF \ OSBYTE &EA - get/set tube presence byte
TXA:BEQ tube_ok
BRK:EQUS 0, "Tube not supported", 0
.tube_ok
\ Check if we're running on an Electron, and if so, patch the address of the
\ ROM selection register
OSCALL_AX osbyte, &00, &01 \ OSBYTE &00,&01: return host type in X
CPX #0:BNE not_elk \ Are we running on an Electron?
LDA #LO(elk_romsel):STA romsel_sta+1 \ Yes, patch ROMSEL address
.not_elk
LDX #(str_header - strings)
JSR print_string
JSR print_roms
\ Prompt for ROM number to save
.rom_prompt
LDX #(str_rom_prompt - strings):JSR print_string
\ OSWORD &00 - read line from console
OSCALL_WORD osword, &00, ctl_rom_prompt
BCS exit \ Escape pressed? get outta here
LDA strbuf:CMP #&0D:BEQ rom_prompt \ Blank line? try again
\ Convert a single hex digit
\ Constraints (set in control block):
\ - only 1 character
\ - >= '0'
\ - <= 'f'
CMP #'9'+1:BPL char
SEC:SBC #'0':BCS convert_done
.char
AND #&DF:SEC:SBC #'A'-&0A
CMP #&0A:BMI rom_prompt \ exclude chars < A
CMP #&10:BCS rom_prompt \ exclude chars > F
.convert_done
PHA
\ Prompt for filename
.filename_prompt
LDX #(str_file_prompt - strings):JSR print_string
\ OSWORD &00 - read line from console
OSCALL_WORD osword, &00, ctl_filename_prompt
BCS exit \ Escape pressed? get outta here
LDA strbuf
CMP #&0D
BEQ filename_prompt
PLA:TAX
\ Copy ROM to buffer
LDA #LO(buffer):STA bufptr
LDA #HI(buffer):STA bufptr+1
LDA #&00:STA romptr
LDA #&80:STA romptr+1
LDY #0
{
.loop
JSR read_byte
STA (bufptr), Y
INY
BNE loop
INC romptr+1:INC bufptr+1
LDA romptr+1:CMP #&C0:BNE loop
}
\ Save buffer to file
OSCALL_WORD osfile, &00, ctl_file_save
JSR osfile
.finished
LDX #(str_done - strings)
\ Fall through to print_string and exit
\ Print a null-terminated string
\ Arguments: X - offset of string to print from 'strings' label
\ Destroys: A, X
\ Preserves: Y
.print_string
{
LDA strings, X
BEQ done
JSR osasci
INX
JMP print_string
.done
}
.exit
RTS
ENABLE_PRINT_ROMS=TRUE
ENABLE_READ_BYTE=TRUE
INCLUDE "tools/flashio.6502"
.strings
.str_header
EQUS "Sideways ROM saving tool", &0D, 0
.str_rom_prompt
EQUS "ROM # to save (0-F,Esc)? ", 0
.str_file_prompt
EQUS "Filename? ", 0
.str_done
EQUS "Done.", &0D, 0
.ctl_rom_prompt
EQUW strbuf \ Read string to strbuf
EQUB 1 \ Maximum length 1 char
EQUB '0', 'f' \ Accept only characters from '0' to 'f'
.ctl_filename_prompt
EQUW strbuf \ Read string to strbuf
EQUB 255 \ Max length 255 chars
EQUB &20, &7E \ Accept any printable character
.ctl_file_save
EQUW strbuf \ Get filename from strbuf
EQUD &8000 \ Set load address attribute to &8000
EQUD &8000 \ Set exec address attribute to &8000
EQUD buffer, buffer+&4000 \ Save area = buffer
EQUD &0000
.buffer
PRINT "Buffer starts at ", ~buffer
PRINT &2000-buffer, "bytes free."
.end
SKIP 16384 \ Room for buffer, not saved
SAVE begin, end, begin