-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwr-rd.s
More file actions
345 lines (264 loc) · 6.03 KB
/
wr-rd.s
File metadata and controls
345 lines (264 loc) · 6.03 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
;
; Simple example of using M4 direct file I/O
; to write a file, read it back and display the contents.
;
; Duke 2022 - spinpoint.org
;
; Assembled with RASM (www.roudoudou.com/rasm)
; commands
C_OPEN equ 0x4301
C_READ equ 0x4302
C_WRITE equ 0x4303
C_WRITE2 equ 0x431B
C_CLOSE equ 0x4304
C_SEEK equ 0x4305
C_ROMLOW equ 0x433D
; fat defs
FA_READ equ 1
FA_WRITE equ 2
FA_CREATE_NEW equ 4
FA_CREATE_ALWAYS equ 8
FA_OPEN_ALWAYS equ 16
FA_REALMODE equ 128
DATAPORT equ 0xFE00
ACKPORT equ 0xFC00
km_wait_key equ 0xBB18
txt_output equ 0xBB5A
txt_set_column equ 0xBB6F
scr_reset equ 0xBC0E
scr_set_ink equ 0xBC32
scr_set_border equ 0xBC38
org 0x8000
rom_addr:
; re-init M4rom
ld de,0x40
ld hl,0xB0FF
call 0xBCCB
; setup screen
ld a,1
call scr_reset ; set mode 2
xor a
ld b,a
call scr_set_border
xor a
ld b,a
ld c,b
call scr_set_ink
ld a,1
ld b,26
ld c,b
call scr_set_ink
di
call m4rom_enable ; detect and enable m4 rom at 0xC000, functions below assume its paged in.
cp 255
jr nz, is_ok
ld hl,txt_error
call wrt
jp error
is_ok:
; ----------------------------------------------------------------
; write a filename "test.asc" with contents "Hello World!",0
; ----------------------------------------------------------------
; open file
ld hl,fname
ld a, FA_REALMODE | FA_CREATE_ALWAYS | FA_WRITE
call fopen
cp 255
jr z, error
; write the file
push af ; save filehandle
ld hl,data
ld bc, 13
call fwrite
; close the file
pop af
call fclose
; ----------------------------------------------------------------
; read the file "test.asc" and display contents
; ----------------------------------------------------------------
; open file
ld hl,fname
ld a,FA_REALMODE | FA_READ
call fopen
cp 255
jr z, error
ld hl,buffer
ld bc,13 ; size
push af ; fd
call fread
cp 0
jr nz, error
pop af ;fd
call fclose
; display the contents
call m4rom_disable
ld hl,buffer
call wrt
endl: jp endl
error: ld a,7
call 0xbb5a
jp endl
data: db "Hello world!",0
; ------------------------- fopen
; -- parameters:
; -- HL = filename zero terminated
; -- A = mode
; -- return:
; -- A = file fd (255 if error!)
fopen:
ld bc,DATAPORT
out (c),c ; ignore first byte
ld de,C_OPEN
out (c),e
out (c),d
out (c),a ; mode
; filename
send_fn:
ld a,(hl)
inc hl
out (c),a
or a
jr nz, send_fn
ld b,ACKPORT>>8 ; kick command
out (c),c
ld hl,(0xFF02)
inc hl
inc hl
inc hl
inc hl
ld a,(hl) ; check if open was OK?
cp 0
jr nz, fd_not_ok
dec hl
ld a,(hl)
fd_not_ok:
ret
; ------------------------- fread
; -- parameters:
; -- A = fd
; -- HL = addr
; -- BC = size (max. 2048 at a time)
; -- return:
; -- A = 0 if OK
fread: push bc
ld bc,DATAPORT
out (c),c ; ignore first byte
ld de,C_READ
out (c),e
out (c),d
out (c),a ; fd
pop de
push de
out (c),e ; size
out (c),d ; size
ld b,ACKPORT>>8 ; kick command
out (c),c
ex de,hl ; address in ram
ld hl,(0xFF02)
inc hl
inc hl
inc hl
ld a,(hl) ; response
inc hl ; point at data in rombuffer
pop bc ; size
ldir
ret
; ------------------------- fwrite
; -- parameters:
; -- A = fd
; -- HL = addr
; -- BC = size (max. 65536 at a time)
; -- return:
; -- A = 0 if OK
fwrite: push bc
ld bc, DATAPORT ; data out port
out (c),c ;
ld de, C_WRITE2 ; command (only for REAL MODE, but takes 16 bit size at once)
out (c),e ; command lo (C_WRITE2)
out (c),d ; command hi (C_WRITE2)
out (c),a ; file handle
pop de
out (c),e ; size low byte
out (c),d ; size high byte
wr_loop:
inc b
outi
dec de
xor a
cp d
jr nz, wr_loop
cp e
jr nz, wr_loop
ld bc,ACKPORT ; kick the command
out (c),c
ret
; ------------------------- fclose
; -- parameters:
; -- A = file fd
; -- return
fclose:
ld bc,DATAPORT
out (c),c ; ignore first byte
ld de,C_CLOSE
out (c),e
out (c),d
out (c),a ; fd
ld b,ACKPORT>>8 ; kick command
out (c),c
ret
m4rom_enable:
; M4 detection via C_ROMLOW command (only from FW v2.0.7 )
ld bc,DATAPORT
out (c),c ; ignore first byte
ld de,C_ROMLOW
out (c),e
out (c),d
ld a,2 ; enable lowerrom from HACK MENU
out (c),a
ld b,ACKPORT>>8 ; kick command
out (c),c
; page in lowerrom
ld bc,0x7F89
out (c),c
ld a,(0x100)
cp 0x4D ; detect 'M' from "MV - SNA" string, to determine if M4 present
jr z, m4_found
ld a, 255
ret
m4_found:
ld a,(0x0) ; get M4 rom number
push af
ld bc,DATAPORT
out (c),c ; ignore first byte
ld de,C_ROMLOW
out (c),e
out (c),d
xor a ; re-enable system lowerrom
out (c),a
ld b,ACKPORT>>8 ; kick command
out (c),c
; select M4 upperrom
pop af
ld bc,0xDF00
out (c),a
; enable upperrom & disable lower
ld bc,0x7F85
out (c),c
ret
m4rom_disable:
; diable upperrom (and lower)
ld bc,0x7F8D
out (c),c
ret
wrt:
ld a,(hl)
or a
ret z
call txt_output
inc hl
jr wrt
txt_error:
db "M4 Board not found !",7,0
fname:
db "/TEST.ASC",0
buffer: ds 32