-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathromup.s
More file actions
543 lines (443 loc) · 10.6 KB
/
romup.s
File metadata and controls
543 lines (443 loc) · 10.6 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
;
; Example of using M4 direct file I/O
; to upload a new rom
;
; Duke 2018 - spinpoint.org
;
; Assembled with RASM (www.roudoudou.com/rasm)
; commands
C_OPEN equ 0x4301
C_READ equ 0x4302
C_WRITE equ 0x4303
C_CLOSE equ 0x4304
C_SEEK equ 0x4305
C_ROMSUPDATE equ 0x432B
; 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
org 0x4000
rom_addr:
incbin "protext.rom" ; use binary (no amsdos header) rom file here
org 0x8000
; find rom M4 rom number
ld a,(m4_rom_num)
cp 0xFF ; if not 0xFF its already initilized, no need to waste time....
call z,find_m4_rom
cp 0xFF
jr nz,found_m4 ; M4 rom not detected, crash and burn :/
ld a,7
call 0xbb5a ; beep
jp 0xbb18
found_m4:
di
ld iy, cmd_buf ; buffer used for all file functions
call m4rom_enable ; enable m4 rom at 0xC000, functions below assume its paged in.
; get rom_response address from rom_table at 0xFF02
ld hl,0xFF02
ld e,(hl)
inc hl
ld d,(hl)
push de
pop ix
; ix points to rom response, used by functions below
ld a,2 ; rom slot to be flashed
ld hl, rom_addr ; address of rom data
call write_rom
; more roms could be written here
; and finally tell m4 to update the flash internally
call update_roms
; the end
call m4rom_disable
ei
call 0xbb18
; done..
jp 0
; -- a = rom slot
; -- hl = address of rom to be flashed
write_rom:
; open m4/romslots.bin
ld (iy-2),a ; roms slot
ld (rom_addr_ptr),hl
ld hl, romslots_fn
ld c,0x80 | FA_READ|FA_WRITE
ld a,17
ld de, C_OPEN
call send_command2 ; will do cmd(2, DE), size(1, A), mode (1, C) followed by data in HL with A size.
ld b,(ix+3) ; fd
ld a,(ix+4) ; res
cp 0
jp nz, romupload_fail
ld (iy-1),b ; file handle
; calculate seek offset
ld bc,0x4000 ; rom size
ld e, (iy-2) ; rom slot
ld d,0
call mul16 ; performs DEHL = BC*DE
; perform fseek (32 bit) offset into m4/romslots.bin
ld (iy+0),7 ; size.. cmd(2) + fd (1) + offset (4)
ld (iy+1),C_SEEK ; cmd seek
ld (iy+2),C_SEEK>>8 ; cmd seek
ld a,(iy-1) ; file handle
ld (iy+3),a ;
ld (iy+4),l ; offset
ld (iy+5),h ; offset
ld (iy+6),e ; offset
ld (iy+7),d ; offset
call send_command_iy
ld a,(ix+3) ; check if seek was OK?
or a
jp nz, romupload_close_fail
; write the ROM in 252 byte chunks to m4/romslots.bin
ld hl, (rom_addr_ptr)
ld b, 65 ; 65 * 252 = 16380
; write chunk size to outputfile (romslots.bin)
rom_write_loop:
push bc
ld bc, DATAPORT ; data out port
ld de, C_WRITE ; command
ld a,255
out (c),a ; size
out (c),e ; command lo (C_WRITE)
out (c),d ; command hi (C_WRITE)
ld e,(iy-1) ; file handle
out (c),e
sub 3 ; size - 3
rom_data_loop:
inc b
outi
dec a
jr nz, rom_data_loop
ld bc,ACKPORT ; kick the command
out (c),c
pop bc
djnz rom_write_loop
; and write the remaining 16384-16380= 4 bytes
ld bc, DATAPORT ; data out port
ld de, C_WRITE ; command
ld a,7
out (c),a ; size
out (c),e ; command lo (C_WRITE)
out (c),d ; command hi (C_WRITE)
ld e,(iy-1) ; file handle
out (c),e
inc b
outi
inc b
outi
inc b
outi
inc b
outi
ld bc,ACKPORT ; kick the command
out (c),c
; close the file
ld a,(iy-1) ; file handle for "m4/romslots.bin"
call fclose
; now update m4/romconfig.bin
ld hl, romconfig_fn
ld c,0x80 | FA_READ|FA_WRITE
ld a,18
ld de, C_OPEN
call send_command2 ; will do cmd(2, DE), size(1, A), mode (1, C) followed by data in HL with A size.
ld b,(ix+3) ; fd
ld a,(ix+4) ; res
cp 0xFF
jp z, romupload_fail
ld (iy-1),b ; file handle
ld bc,33 ; rom name (32) + updateflag (1)
ld e, (iy-2) ; rom slot
ld d,0
call mul16 ; performs DEHL = BC*DE
ld de,32 ; skip header (8*4)
add hl,de ; size is less than 16 bit, no worries..
ld a, (iy-1)
call fseek ; we are now pointing at the updateflag for current rom slot
; set update flag to 2 and name to "rom".
ld de, C_WRITE
ld hl, update_rom_slot ; data read earlier
ld c, (iy-1)
ld a, 5
call send_command2 ; will do cmd(2, DE), size(1, A), fd (1, C) followed by data in HL with A size.
ld a, (iy-1)
call fclose
xor a ; success.
ret
romupload_close_fail:
ld a,(iy-1) ; file handle "m4/romslots.bin"
call fclose
romupload_fail:
ld a,255 ; fail
ret
update_roms:
ld (iy+1),C_ROMSUPDATE
ld (iy+2),C_ROMSUPDATE>>8
ld (iy+0),2 ; packet size, cmd (2)
call send_command_iy
ret
; fopen
; ------------------------- fopen
; -- parameters:
; -- IY = buffer to store command
; -- HL = filename
; -- B = filename length
; -- A = mode
; -- return:
; -- A = file fd (255 if error!)
; -- B = error code
fopen:
push bc
push de
push hl
ld (iy+1),C_OPEN
ld (iy+2),C_OPEN>>8
ld (iy+3),a ; mode
add 3 ; cmd (2) + mode (1) + filename
ld (iy),a ; packet length
call send_command_iy
ld a,(ix+4) ; check if open was OK?
cp 0
jr nz, fd_not_ok
ld a,(ix+3) ; file descriptor
fd_not_ok:
pop hl
pop de
pop bc
ret
; ------------------------- fread
; -- parameters:
; -- A = fd
; -- IY = buffer to store command
; -- HL = size
; -- DE = addr
; -- return:
; -- A = 0 if OK
fread:
push hl
push de
push bc
ld (iy+1),C_READ
ld (iy+2),C_READ>>8
ld (iy+3),a ; fd
ld (iy+0),5 ; packet size, cmd (2), fd (1), size (2)
read_loop:
; get chunk size (<=0x800)
push hl
ld bc,-0x800
add hl,bc ; and substract chunksize
jp c, full_chunk
pop hl
ld (iy+4),l ; chunk size low
ld (iy+5),h ; chunk size high
jr fread_cont
full_chunk:
pop hl
ld (iy+4),0x0 ; chunk size low
ld (iy+5),0x8 ; chunk size high
fread_cont:
xor a
cp (iy+4)
jr nz,not_done ; size is 0?
cp (iy+5)
jr nz,not_done ; size is 0?
pop bc
pop de
pop hl
xor a
ret
not_done:
push hl
call send_command_iy ; send read command packet
ld c,(iy+4) ; chunk size low
ld b,(iy+5) ; chunk size high
ld a,(ix+3) ; check result
cp 0
jr nz,read_error
inc hl
push bc ; store chunk size
ldir ; copy data in place
pop bc ; restore chunk size
pop hl ; restore remaining size
or a ; clear carry
sbc hl,bc ; and substract chunksize
jr read_loop
read_error:
pop hl
pop bc
pop de
pop hl
ret
; ------------------------- fseek
; -- parameters:
; -- IY = buffer to store command
; -- A = fd
; -- HL = offset
fseek:
push bc
push hl
push de
ld (iy+0),7 ; size.. cmd(2) + offset (4)
ld (iy+1),C_SEEK ; cmd seek
ld (iy+2),C_SEEK>>8 ; cmd seek
ld (iy+3),a ; fd
ld (iy+4),l ; offset lo
ld (iy+5),h ; offset hi
ld (iy+6),0 ; 0
ld (iy+7),0 ; 0
call send_command_iy
pop de
pop hl
pop bc
ret
; ------------------------- fclose
; -- parameters:
; -- IY = buffer to store command
; -- A = file fd
; -- return
; -- A = 0, good. A = 0xFF bad.
fclose:
push bc
push hl
ld (iy+1),C_CLOSE ; close cmd
ld (iy+2),C_CLOSE>>8 ; close cmd
ld (iy+3),a ; fd
ld (iy+0),3 ; size - cmd(2) + fd(1)
call send_command_iy
ld a,(ix+3)
cp 0
jr z,fclose_ok
ld a,255
fclose_ok:
pop hl
pop bc
ret
find_m4_rom:
ld d,127 ; start looking for from (counting downwards)
romloop: push de
ld c,d
call 0xB90F ; system/interrupt friendly
ld a,(0xC000)
cp 1
jr nz, not_this_rom
ld hl,(0xC004) ; get rsxcommand_table
ld de,m4_rom_name ; rom identification line
cmp_loop:
ld a,(de)
xor (hl) ; hl points at rom name
jr z, match_char
not_this_rom:
pop de
dec d
jr nz, romloop
ld a,255 ; not found!
ret
match_char:
ld a,(de)
inc hl
inc de
and 0x80
jr z,cmp_loop
; rom found, store the rom number
pop de ; rom number
ld a,d
ld (m4_rom_num),a
ret
mul16: ld hl,0
ld a,16
mul16Loop: add hl,hl
rl e
rl d
jp nc,nomul16
add hl,bc
jp nc,nomul16
inc de
nomul16:
dec a
jp nz,mul16Loop
ret
; DE = COMMAND
; A = size
; HL = DATA
; C = fd
send_command2:
push af
push hl
push de
push bc
add 3
ld bc,DATAPORT ; data out port
out (c),a ; size
out (c),e ; command lo
out (c),d ; command hi
pop de
push de
out (c),e ; fd
sub 3
; send actual data
sendloop2:
ld d,(hl)
out (c),d
inc hl
dec a
jr nz, sendloop2
; tell M4 that command has been send
ld bc,ACKPORT
out (c),c
pop bc
pop de
pop hl
pop af
ret
send_command_iy:
push iy
pop hl
ld bc,DATAPORT ;0xFE00 ; FE data out port
ld a,(hl) ; size
inc a
sendloop_iy:
inc b
outi
dec a
jr nz, sendloop_iy
; tell M4 that command has been send
ld bc,ACKPORT
out (c),c
ret
m4rom_enable:
; rom select
ld a,(m4_rom_num)
ld bc,0xDF00
out (c),a
; enable upperrom
ld bc,0x7F86
out (c),c
ret
m4rom_disable:
; diable upperrom (and lower)
ld bc,0x7F8E
out (c),c
ret
update_rom_slot:
db 2
db "ROM" ; change to any name you want, this will be displayed in the webinterface.
db 0
romslots_fn:
db "/m4/romslots.bin"
db 0
romconfig_fn:
db "/m4/romconfig.bin"
db 0
m4_rom_name: db "M4 BOAR",0xC4 ; D | 0x80
m4_rom_num: db 0xFF
rom_addr_ptr:
dw 0
rom_slot:
db 0
file_handle:
db 0 ; iy - 1
cmd_buf: ds 256 ; iy