-
Notifications
You must be signed in to change notification settings - Fork 1
/
loader_plus4_c16stm.inc
200 lines (178 loc) · 2.84 KB
/
loader_plus4_c16stm.inc
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
.print ". plus/4 C16STM loader"
.namespace iolib {
.namespace plus4c16stm {
.pseudopc io_base {
#if TEST_C16STM
.print "!!! TEST BUILD !!!"
#endif
jmp startload
jmp readbyte
jmp writebyte
jmp hardsync
jmp sync
hardsync:
sync:
rts
readbyte:
#if need_exodecrunch
ldy #0
lda (iolib.io_loadptr),y
inc iolib.io_loadptr
bne !+
inc iolib.io_loadptr+1
!: clc
#endif
rts
writebyte:
#if need_exodecrunch
ldx filenameptr
sta filename,x
inx
cpx #2
bne !+
ldx #0
!: stx filenameptr
beq startload2
#endif
rts
startload:
stx filename
sty filename+1
startload2:
lda #$52 // fopen
jsr sendrecievebyte
ldx #0
!: lda filename,x // send filename
jsr sendrecievebyte
inx
cpx #7
bne !-
jsr sendrecievebyte
cmp #$7f
bne !+
// signal load error
loaderror:
sec
rts
!:
sta channel // logical file channel
jsr sendrecievebyte
sta length // file size
jsr sendrecievebyte
// increase length by $100 so when it's high byte for better finished check
sta length+1
jsr sendandreceive
jsr sendrecievebyte
cmp #$80
bne loaderror
!:
lda #$63 // read word from file: load address
jsr sendandreceive
#if need_loadflag
bit iolib.io_loadflag
bmi !+
#endif
sta iolib.io_loadptr
#if need_exodecrunch
pha
#endif
!:
jsr sendrecievebyte
#if need_loadflag
bit iolib.io_loadflag
bmi !+
#endif
sta iolib.io_loadptr+1
#if need_exodecrunch
pha
#endif
!:
jsr declength
jsr declength
lda #$65 // read block
jsr sendrecievebyte
lda length
jsr sendrecievebyte
lda length+1
jsr sendrecievebyte
inc length+1
ldy #0
!: jsr sendrecievebyte
sta (iolib.io_loadptr),y
iny
bne !+
inc io_loadptr+1
!: jsr declength
bne !--
jsr sendrecievebyte
lda #$54 // fclose
jsr sendrecievebyte
.label channel=*+1
lda #0
jsr sendandreceive
#if need_exodecrunch
pla
sta iolib.io_loadptr+1
pla
sta iolib.io_loadptr
#endif
ldx filename
ldy filename+1
clc
rts
sendandreceive:
jsr sendrecievebyte
sendrecievebyte:
#if TEST_C16STM
lda #$80
rts
#else
sta c16stm.data
!:
lda c16stm.status
bmi !-
lda c16stm.data
rts
#endif
declength:
dec length
bne !+
dec length+1
!: rts
length:
.word 0
filename:
.text "00.prg"
filenameptr:
.byte 0
.if(*>io_base + $100) {
.error "Build too long: $" + toHexString(*-io_base-$100, 4)
} else {
.print ". . space remaining: $" + toHexString($100-(* - io_base), 4)
}
}
}
}
/*
Transfer protocol:
- write byte to send/trigger recieve to data register
- wait for status register bit 7 to became 0
- read response byte from data register
Load protocol (ROM):
- fopen
- - $52
- - filename
- - $00
- read response: $7f: file open error, otherwise channel(?)
- read file length low
- read file length high
- read 2 dummy bytes
- read fopen status: $80 - OK
- read file with one of these:
- - $63: read word
- - $64: read byte
- - $65 <length-low> <length high>: read block with length then dummy read
- fclose:
- - $54
- - channel#
*/