forked from jbrandwood/teos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mb128.s
1612 lines (1268 loc) · 36.5 KB
/
mb128.s
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; ***************************************************************************
; ***************************************************************************
;
; mb128.s
;
; Functions for using an "MB128" or "Save Kun" backup-memory device.
;
; Copyright John Brandwood 2019.
;
; Distributed under the Boost Software License, Version 1.0.
; (See accompanying file LICENSE_1_0.txt or copy at
; http://www.boost.org/LICENSE_1_0.txt)
;
; ***************************************************************************
; ***************************************************************************
;
; The MB128 is built around an MSM6389 1Mbit (128KByte) serial memory chip.
;
; That memory chip is accessed by the PC Engine one bit at a time, through
; the joypad port.
;
; The MB128 normally allows the joypad port's signals to directly pass
; through to any attached joypad/mouse/multitap devices.
;
; When it detects a specific "wakeup" sequence on the joypad port, it takes
; control of the port and locks-out the other devices.
;
; You then send the MB128 a command, and when the command is finished, the
; MB128 releases the joypad port, and waits for the next "wakeup" sequence.
;
;
; The MB128 only responds to two commands, READ BITS and WRITE BITS.
;
; READ BITS
;
; 1 <addr> <size> ... MB128 then sends <data> ... PCE sends 3 zero-bits.
;
; WRITE BITS
;
; 0 <addr> <size> ... MB128 then receives <data> ... PCE sends 5 zero-bits.
;
; <addr> is a 10-bit value, in 128-byte increments within the 1Mbit range.
;
; The code normally uses an 8-bit value of nominal 512-byte "sectors".
;
; <size> is a 20-bit value, as the # of bits to transfer.
;
; The code normally uses an 8-bit value of nominal 512-byte "sectors".
;
; <addr>, <size>, and <data> are all little-endian values, sent and received
; low-bit first.
;
; The final bits of each command that the PCE sends, seem designed to flush
; out the MB128's internal shift-registers, and to prepare the MB128's
; sequence-detector for the next "wakeup" sequence.
;
; ***************************************************************************
; ***************************************************************************
; Compilation options.
MB1_TRY_UNJAM = 0 ; Try to unjam a stuck MB128.
MB1_UNJAM_SIZE = 4 ; Max # of sectors to flush.
MB1_NUM_RETRIES = 3 ; Retry count for operations.
; Error Codes.
MB1_OK = $00
MB1_ERR_INIT = $A0
MB1_ERR_IDENT = $A1
MB1_ERR_CSUM = $A2
MB1_ERR_INVALID = $A3
MB1_ERR_READ = $A4
MB1_ERR_WRITE = $A5
; 1st entry in MB128's 1024-byte directory.
;
; N.B. Some later MB128 games did not count the 2 directory sectors in the
; total number of sectors used, while earlier MB128 games do.
; But some early games only check the lo-byte of MB1_HEAD_USED, which
; makes them think that $0000 is actually $0100 (i.e. full), and then
; they think that a freshly-formatted MB128 has no free space on it!
;
; These library rountines count the 2 directory sectors in the total,
; but they do not trust MB1_HEAD_USED, and always recalculate it.
MB1_HEAD_CSUM = $00 ; ( 2) Checksum of last 510 bytes.
MB1_HEAD_USED = $02 ; ( 2) # of sectors used.
MB1_HEAD_IDENT = $04 ; (12) Identification Signature.
; Other 63 entries in MB128's 1024-byte directory.
;
; N.B. The file's checksum is only of the bytes used, not of the sectors used.
MB1_FILE_ADDR = $00 ; ( 1) Sector # (or 0 if final).
MB1_FILE_SIZE = $01 ; ( 1) # of sectors used.
MB1_FILE_TAIL = $02 ; ( 2) # of bytes in last sector.
MB1_FILE_CSUM = $04 ; ( 2) Checksum of file data.
MB1_FILE_UNKN = $06 ; ( 2) Unknown, maybe company ID.
MB1_FILE_NAME = $08 ; ( 8) Filename.
;
bss
mb1_detected: ds 1 ; NZ if an MB128 has been found.
mb1_retry_left: ds 1
mb1_base_bank: ds 1 ; BRAM_BANK or SLOT_BANK.
mb1_sector_num: ds 1 ; For mb1_load_image & mb1_save_image.
mb1_file_count: ds 1 ; For mb1_check_dir ...
mb1_frag_count: ds 1 ; 0=full, 1=ok, 2+=fragmented
mb1_directory = $6000 ; 1024-bytes.
code
; ****************************************************************************
; ****************************************************************************
;
; Messages.
;
mb1_format: dw $0632 ; MB1_HEAD_CSUM
dw $0002 ; MB1_HEAD_USED
mb1_signature: db $D2,$D3,$D8,$CD,$DE,$B0,$BD,$31,$32,$38,$00,$00
mb1_msg_init: db $0C
db "%>%p5"
db " MBASE128 SD: Initializing ..."
db "%<%p0",$0A,$0A,$0A,0
mb1_detect_ok: db " MB128 detected.",$0A,0
mb1_detect_fail:db " MB128 not found!",$0A,0
mb1_msg_rdd_now:db " Loading MB128 directory.",0
mb1_msg_wrd_now:db " Saving MB128 directory.",0
mb1_msg_rdd_ok: db $0D
db " MB128 directory loaded. ",$0A,0
mb1_msg_wrd_ok: db $0D
db " MB128 directory saved. ",$0A,0
mb1_msg_rdf_now:db $0D," Loading MB128 file ",$22,0
mb1_msg_wrf_now:db $0D," Saving MB128 file ",$22,0
mb1_msg_name: db " ",0
mb1_msg_rdf_ok: db $0D
db " MB128 files loaded. ",$0A,0
mb1_msg_wrf_ok: db $0D
db " MB128 files saved. ",$0A,0
mb1_msg_rd_fail:db $0A
db " Load failed!",$0A,0
mb1_msg_wr_fail:db $0A
db " Save failed!",$0A,0
mb1_msg_dot: db ".",0
mb1_msg_lfsp: db $0A," ",0
mb1_msg_lf: db $0A,0
; ****************************************************************************
; ****************************************************************************
;
; mb1_load_image - Load up the entire contents of the MB128 into memory.
;
; Args: A = bank # of image (uses 16 contiguous banks)
; Uses: mb1_directory address in mb1_base_bank.
; Uses: __bp = Directory ptr.
;
; Returns: X = MB1_OK (and Z flag) or an error code.
;
; N.B. This loads the raw image contents with no attempt to fix bad file data!
;
mb1_load_image: sta mb1_base_bank ; Set the image base bank.
; PUTS cls_m128_save
; PUTS mb1_msg_rdd_now
jsr mb1_load_dir ; Load the directory & verify
beq .got_dir ; signature and checksum.
cpx #MB1_ERR_IDENT ; Is the MB128 unformatted?
bne .show_load_err
PUTS mb1_msg_lf
PUTS mb1_msg_lf
lda mb1_base_bank ; Create a blank image if the
jmp mb1_new_image ; MB128 is unformatted.
.got_dir:
; PUTS mb1_msg_rdd_ok
cla ; Write an invalid directory
sta mb1_sector_num ; to the image to mark the
jsr mb1_image_addr ; file contents as trashed.
tai tos_zero, mb1_directory + MB1_HEAD_IDENT, 12
lda #2 ; Sector #.
.block_loop: pha
lda #MB1_NUM_RETRIES ; Each block gets the full
sta mb1_retry_left ; number of retries.
.block_retry: pla ; Sector #.
pha
sta mb1_sector_num
jsr mb1_image_addr ; Calculate address in image.
ldx #2 ; Sector Count.
jsr mb1_read_data ; Load the file's data.
bne .block_load_err
pla ; Sector #.
pha
jsr mb1_image_addr ; Calculate address in image.
ldx #2 ; Sector Count.
jsr mb1_check_data ; Read again to confirm data.
beq .block_load_ok
.block_load_err:dec mb1_retry_left ; Timeout?
bne .block_retry ; Timeout!
ldx #MB1_ERR_READ
.show_load_err: phx
PUTS mb1_msg_rd_fail
plx
rts
.block_load_ok: pla ; Sector #.
pha
dec a
dec a
and #$3F
bne .next_block
PUTS mb1_msg_lfsp
.next_block: PUTS mb1_msg_dot
pla ; Sector #.
inc a
inc a
cmp #2 ; Wrap and load directory.
bne .block_loop
PUTS mb1_msg_lf
PUTS mb1_msg_lf
PUTS mb1_msg_rdd_ok
PUTS mb1_msg_rdf_ok
PUTS mb1_msg_lf
ldx #MB1_OK ; Return MB128_OK.
.finished: txa ; Set the N & Z result flags.
rts
; ****************************************************************************
; ****************************************************************************
;
; mb1_save_image - Save the entire contents of memory to the MB128.
;
; Args: mb1_base_bank = bank # of image (uses 16 contiguous banks)
; Uses: mb1_directory
; Uses: __bp = Directory ptr.
;
; Returns: X = MB1_OK (and Z flag) or an error code.
;
; N.B. This saves the raw image contents with no attempt to fix bad file data!
;
mb1_save_image: sta mb1_base_bank ; Set the image base bank.
tam3
; PUTS cls_m128_load
ldy #12-1 ; Compare the signature.
.ident: lda mb1_directory + MB1_HEAD_IDENT,y
cmp mb1_signature,y
bne .bad_ident
dey
bpl .ident
bra .got_dir
.bad_ident: lda mb1_base_bank ; Create a blank image if the
jsr mb1_new_image ; image is unformatted.
.got_dir: cla ; Write an invalid directory
sta mb1_sector_num ; to the MB128 to mark the
jsr mb1_image_addr ; file contents as trashed.
tai tos_zero, mb1_directory + MB1_HEAD_IDENT, 12
ldx #2 ; Sector Count.
jsr mb1_write_data ; Wipe out old directory data.
tii mb1_signature, mb1_directory + MB1_HEAD_IDENT, 12
lda #2 ; Sector #.
.block_loop: pha
lda #MB1_NUM_RETRIES ; Each file gets the full
sta mb1_retry_left ; number of retries.
.block_retry: pla ; Sector #.
pha
sta mb1_sector_num ; Use sector # in directory.
jsr mb1_image_addr ; Calculate address in image.
ldx #2 ; Sector Count.
jsr mb1_write_data ; Save the block's data.
bne .block_save_err
pla ; Sector #.
pha
jsr mb1_image_addr ; Calculate address in image.
ldx #2 ; Sector Count.
jsr mb1_check_data ; Check to confirm the write.
beq .block_save_ok
.block_save_err:dec mb1_retry_left ; Timeout?
bne .block_retry ; Timeout!
ldx #MB1_ERR_WRITE
.show_save_err: phx
PUTS mb1_msg_wr_fail
plx
rts
.block_save_ok: pla ; Sector #.
pha
dec a
dec a
and #$3F
bne .next_block
PUTS mb1_msg_lfsp
.next_block: PUTS mb1_msg_dot
pla ; Sector #.
inc a
inc a
cmp #2 ; Wrap and save directory.
bne .block_loop
PUTS mb1_msg_lf
PUTS mb1_msg_lf
PUTS mb1_msg_wrf_ok
PUTS mb1_msg_wrd_ok
PUTS mb1_msg_lf
ldx #MB1_OK ; Return MB128_OK.
.finished: txa ; Set the N & Z result flags.
rts
; ****************************************************************************
; ****************************************************************************
;
; mb1_new_image - Format a new MB128 image in memory.
;
; Args: A = bank # of image (uses 16 contiguous banks)
; Uses: mb1_directory address in mb1_base_bank.
;
; Returns: X = MB1_OK (and Z flag) or an error code.
;
mb1_new_image: sta mb1_base_bank ; Set the image base bank.
tma3
tai tos_zero, mb1_directory, 1024
tii mb1_format, mb1_directory, 16
lda #>(mb1_directory + 1024)
sta __ax + 1
stz __ax + 0
lda #$FF
ldx #254
jsr mb1_fill_memory
lda mb1_base_bank ; Map in the directory bank.
tam3
ldx #MB1_OK
rts
; ****************************************************************************
; ****************************************************************************
;
; mb1_load_files - Load up the entire contents of the MB128 into memory.
;
; Args: A = bank # of image (uses 16 contiguous banks)
; Uses: mb1_directory address in mb1_base_bank.
; Uses: __bp = Directory ptr.
;
; Returns: X = MB1_OK (and Z flag) or an error code.
;
; N.B. This verifies both the file and directory integrity!
;
mb1_load_files: sta mb1_base_bank ; Set the image base bank.
; PUTS cls_m128_save
PUTS mb1_msg_rdd_now
jsr mb1_load_dir ; Load the directory & verify
bne .show_load_err ; that it makes sense.
jsr mb1_csum_dir ; Verify the file information
bne .show_load_err ; integrity.
.got_dir: PUTS mb1_msg_rdd_ok
lda #2 ; Starting sector for image.
sta mb1_sector_num
lda #<(mb1_directory + 16) ; Start with the 1st file.
sta <__bp + 0
lda #>(mb1_directory + 16)
sta <__bp + 1
.file_loop: lda #MB1_NUM_RETRIES ; Each file gets the full
sta mb1_retry_left ; number of retries.
.file_retry: lda mb1_base_bank ; Map in the directory bank.
tam3
lda [__bp] ; Get file's MB1_FILE_ADDR.
bne .file_load
jmp .all_loaded ; Found END-OF-DIRECTORY!
.file_load: jsr mb1_copy_name ; Report to the user.
PUTS mb1_msg_rdf_now
PUTS mb1_msg_name
ldy #MB1_FILE_SIZE ; Get file's MB1_FILE_SIZE.
lda [__bp],y
tax
lda [__bp] ; Get file's MB1_FILE_ADDR.
jsr mb1_image_addr ; Calculate address in image.
jsr mb1_read_data ; Load the file's data.
bne .file_load_err
lda mb1_base_bank ; Map in the directory bank.
tam3
ldy #MB1_FILE_SIZE ; Get file's MB1_FILE_SIZE.
lda [__bp],y
tax
lda [__bp] ; Get file's MB1_FILE_ADDR.
jsr mb1_image_addr ; Calculate address in image.
jsr mb1_check_data ; Read again to confirm data.
beq .file_load_ok
.file_load_err: dec mb1_retry_left ; Timeout?
bne .file_retry ; Timeout!
ldx #MB1_ERR_READ
.show_load_err: phx
PUTS mb1_msg_rd_fail
plx
rts
.file_load_ok: lda mb1_base_bank ; Map in the directory bank.
tam3
if 1
ldy #MB1_FILE_SIZE ; Get file's MB1_FILE_SIZE.
lda [__bp],y
dec a ; Exclude last sector.
jsr mb1_xvert_size
tya ; Add last sector size to csum
sax ; byte count.
ldy #MB1_FILE_TAIL + 1 ; Hi-Byte of # in last sector.
clc
adc [__bp],y
bcc .skip
inx ; bit17 of byte count.
.skip: phx
tax
ldy #MB1_FILE_TAIL + 0 ; Lo-Byte of # in last sector.
lda [__bp],y
ply
jsr mb1_image_addr ; Calculate address in image.
jsr mb1_csum_memory ; Calculate the file checksum.
lda mb1_base_bank ; Map in the directory bank.
tam3
ldy #MB1_FILE_CSUM ; Compare the checksum with
lda [__bp],y ; the one in the directory.
cmp <__ax + 0
bne .csum_mismatch
iny
lda [__bp],y
cmp <__ax + 1
beq .next_file
.csum_mismatch: bra .file_load_err
endif
.next_file: lda mb1_sector_num ; Set file's MB1_FILE_ADDR.
sta [__bp]
ldy #MB1_FILE_SIZE ; Add file's MB1_FILE_SIZE.
clc
adc [__bp],y
sta mb1_sector_num ; Next sector in image.
bcs .all_loaded ; Check for overflow.
lda <__bp + 0 ; Goto next file entry.
clc
adc #$10
sta <__bp + 0
lda <__bp + 1
adc #$00
sta <__bp + 1
cmp #>(mb1_directory + 1024)
beq .all_loaded
jmp .file_loop
.all_loaded: jsr mb1_image_addr ; Blank out the rest of the
cla ; memory in the image.
sec
sbc mb1_sector_num
beq .fix_csum
tax ; # of sectors to clear.
lda #$FF ; Value to write.
jsr mb1_fill_memory
.fix_csum: jsr mb1_csum_dir ; Update directory checksum.
bne .show_load_err ; This should never happen!
PUTS mb1_msg_rdf_ok
PUTS mb1_msg_lf
ldx #MB1_OK ; Return MB128_OK.
.finished: txa ; Set the N & Z result flags.
rts
;
;
;
mb1_copy_name: ldy #$08
.name_loop: lda [__bp],y
beq .name_tail
sta mb1_msg_name - 8,y
iny
cpy #$10
bne .name_loop
.name_tail: lda #'"'
sta mb1_msg_name - 8,y
iny
lda #'.'
sta mb1_msg_name - 8,y
.tail_loop: iny
lda #' '
sta mb1_msg_name - 8,y
cpy #$12
bne .tail_loop
rts
; ****************************************************************************
; ****************************************************************************
;
; mb1_save_files - Save the entire contents of memory to the MB128.
;
; Args: mb1_base_bank = bank # of image (uses 16 contiguous banks)
; Uses: mb1_directory
; Uses: __bp = Directory ptr.
;
; Returns: X = MB1_OK (and Z flag) or an error code.
;
; N.B. This verifies both the file and directory integrity!
;
mb1_save_files: sta mb1_base_bank ; Set the image base bank.
; PUTS cls_m128_load
jsr mb1_csum_dir ; Sanity Check the directory
beq .got_dir ; contents *before* saving.
jmp .finished
.got_dir: lda #0 ; Write an invalid directory
sta mb1_sector_num ; to the MB128 to mark the
jsr mb1_image_addr ; file contents as trashed.
tai tos_zero, mb1_directory + MB1_HEAD_IDENT, 12
cla ; Sector #.
ldx #2 ; Sector Count.
jsr mb1_write_data ; Wipe out old directory data.
tii mb1_signature, mb1_directory + MB1_HEAD_IDENT, 12
; bne .finished
lda #<(mb1_directory + 16) ; Start with the 1st file.
sta <__bp + 0
lda #>(mb1_directory + 16)
sta <__bp + 1
.file_loop: lda #MB1_NUM_RETRIES ; Each file gets the full
sta mb1_retry_left ; number of retries.
.file_retry: lda mb1_base_bank ; Map in the directory bank.
tam3
lda [__bp] ; Get file's MB1_FILE_ADDR.
bne .file_save
jmp .all_saved ; Found END-OF-DIRECTORY!
.file_save: jsr mb1_copy_name ; Report to the user.
PUTS mb1_msg_wrf_now
PUTS mb1_msg_name
ldy #MB1_FILE_SIZE ; Get file's MB1_FILE_SIZE.
lda [__bp],y
tax
lda [__bp] ; Get file's MB1_FILE_ADDR.
sta mb1_sector_num ; Use sector # in directory.
jsr mb1_image_addr ; Calculate address in image.
jsr mb1_write_data ; Save the file's data.
bne .file_save_err
lda mb1_base_bank ; Map in the directory bank.
tam3
ldy #MB1_FILE_SIZE ; Get file's MB1_FILE_SIZE.
lda [__bp],y
tax
lda [__bp] ; Get file's MB1_FILE_ADDR.
jsr mb1_image_addr ; Calculate address in image.
jsr mb1_check_data ; Check to confirm the write.
beq .file_save_ok
.file_save_err: dec mb1_retry_left ; Timeout?
bne .file_retry ; Timeout!
ldx #MB1_ERR_WRITE
.show_save_err: phx
PUTS mb1_msg_wr_fail
plx
rts
.file_save_ok: lda mb1_base_bank ; Map in the directory bank.
tam3
.next_file: lda [__bp] ; Get file's MB1_FILE_ADDR.
ldy #MB1_FILE_SIZE ; Add file's MB1_FILE_SIZE.
clc
adc [__bp],y
bcs .all_saved ; Check for overflow.
lda <__bp + 0 ; Goto next file entry.
clc
adc #$10
sta <__bp + 0
lda <__bp + 1
adc #$00
sta <__bp + 1
cmp #>(mb1_directory + 1024)
beq .all_saved
jmp .file_loop
.all_saved: PUTS mb1_msg_wrf_ok
PUTS mb1_msg_wrd_now
jsr mb1_save_dir ; Save the directory.
bne .show_save_err
PUTS mb1_msg_wrd_ok
PUTS mb1_msg_lf
ldx #MB1_OK ; Return MB128_OK.
.finished: txa ; Set the N & Z result flags.
rts
; ****************************************************************************
; ****************************************************************************
;
; mb1_image_addr - Calculate a sector's address within the memory image.
;
; Args: mb1_sector_num = Sector #.
; Uses: __ax = Address.
; Uses: MPR3 = Address.
;
; Preserves A, X, Y.
;
mb1_image_addr: pha
lda mb1_sector_num ; Xvert sector # to address
and #$0F ; in the memory-image.
asl a
adc #>mb1_directory
stz <__ax + 0
sta <__ax + 1
lda mb1_sector_num
lsr a
lsr a
lsr a
lsr a
clc
adc mb1_base_bank
tam3
pla
rts
; ****************************************************************************
; ****************************************************************************
;
; mb1_load_dir - Load up the directory from the MB128.
;
; Args: mb1_base_bank = bank # of image (uses 16 contiguous banks)
;
; Returns: X = MB1_OK (and Z flag) or an error code.
;
mb1_load_dir: lda mb1_base_bank ; Map in the directory bank.
tam3
lda #MB1_NUM_RETRIES ; Retry count.
.retry: pha
stz <__ax + 0 ; Load the 2 directory sectors.
lda #>mb1_directory
sta <__ax + 1
cla ; Sector address.
ldx #$02 ; Sector count.
jsr mb1_read_data
bne .failed
stz <__ax + 0 ; Read again to confirm data.
lda #>mb1_directory
sta <__ax + 1
cla ; Sector address.
ldx #$02 ; Sector count.
jsr mb1_check_data
bne .failed
jsr mb1_test_dir ; Test signature and checksum.
bne .failed
pla ; Throw away retry count.
.finished: txa ; Set the N & Z result flags.
rts
.failed: pla ; Restore retry count.
dec a ; Timeout?
bne .retry ; Timeout!
bra .finished
; ****************************************************************************
; ****************************************************************************
;
; mb1_test_dir - Test the directory signature and checksum.
;
; Args: mb1_base_bank = bank # of image (uses 16 contiguous banks)
;
; Returns: X = MB1_OK (and Z flag) or an error code.
;
mb1_test_dir: lda mb1_base_bank ; Map in the directory bank.
tam3
ldx #MB1_ERR_IDENT
ldy #12-1 ; Compare the signature.
.ident: lda mb1_directory + MB1_HEAD_IDENT,y
cmp mb1_signature,y
bne .failed
dey
bpl .ident
lda #<(mb1_directory + 2) ; Generate the checksum.
sta <__al
lda #>(mb1_directory + 2)
sta <__ah
lda #<$03FE
ldx #>$03FE
cly
jsr mb1_csum_memory
ldx #MB1_ERR_CSUM
lda <__al ; Confirm the checksum.
cmp mb1_directory + MB1_HEAD_CSUM + 0
bne .failed
lda <__ah
cmp mb1_directory + MB1_HEAD_CSUM + 1
bne .failed
ldx #MB1_OK
.failed: txa ; Set the N & Z result flags.
rts
; ****************************************************************************
; ****************************************************************************
;
; mb1_save_dir - Save the directory to the MB128.
;
; Args: mb1_base_bank = bank # of image (uses 16 contiguous banks)
; Args: __ax = ptr to memory (page-aligned in MPR3).
; Args: A = Value.
; Args: X = Sector count.
;
; Returns: X = MB1_OK (and Z flag) or an error code.
;
mb1_save_dir: lda mb1_base_bank ; Map in the directory bank.
tam3
jsr mb1_csum_dir ; Fix HEAD_USED & HEAD_CSUM.
bne .finished
lda #MB1_NUM_RETRIES ; Retry count.
.retry: pha
stz <__ax + 0 ; Save the 2 directory sectors.
lda #>mb1_directory
sta <__ax + 1
cla ; Sector address.
ldx #$02 ; Sector count.
jsr mb1_write_data
bne .failed
stz <__ax + 0 ; Confirm they were written OK.
lda #>mb1_directory
sta <__ax + 1
cla ; Sector address.
ldx #$02 ; Sector count.
jsr mb1_check_data
bne .failed
pla ; Throw away retry count.
ldx #MB1_OK
.finished: txa ; Set the N & Z result flags.
rts
.failed: pla ; Restore retry count.
dec a ; Timeout?
bne .retry ; Timeout!
bra .finished
; ****************************************************************************
; ****************************************************************************
;
; mb1_csum_dir - Update the USED and CSUM values, scan for directory errors.
;
; Args: mb1_base_bank = bank # of image (uses 16 contiguous banks)
; Args: none
;
; Returns: X = MB1_OK (and Z flag) or an error code.
;
mb1_csum_dir: lda mb1_base_bank ; Map in the directory bank.
tam3
stz mb1_file_count ; #files.
stz mb1_frag_count ; #free-space fragments.
ldy #12-1 ; Compare the signature.
.ident: lda mb1_directory + MB1_HEAD_IDENT,y
cmp mb1_signature,y
bne .invalid_file
dey
bpl .ident
stz <__ax + 0 ; Calculate the # of sectors
lda #>mb1_directory ; used, incl the directory.
sta <__ax + 1 ; (2..256).
lda #2 ; Current file's minimum valid
sta <__bl ; sector address.
sta mb1_directory + MB1_HEAD_USED + 0
stz <__bh
stz mb1_directory + MB1_HEAD_USED + 1
lda #1024 / 256 ; # of pages in directory.
sta <__cl
ldx #MB1_OK ; Start by assuming success.
ldy #$10 ; Skip the header.
.used_loop: lda [__ax],y ; Check MB1_FILE_ADDR.
beq .used_done ; Found END-OF-DIRECTORY?
iny
cmp <__bl ; Current minimum sector.
beq .contiguous
inc mb1_frag_count ; #free-space fragments.
.contiguous: bcc .invalid_file
bbs0 <__bh, .invalid_file ; Current minimum == $0100?
clc
adc [__ax],y ; Add MB1_FILE_SIZE to ADDR.
sta <__bl ; Next file's minimum.
bcc .file_ok
smb0 <__bh ; Next file's minimum = $0100.
bne .invalid_file ; Sector beyond end of 128KB?
.file_ok: lda [__ax],y ; Zero length file?
beq .invalid_file
clc ; Maximum used is 254, so CC.
adc mb1_directory + MB1_HEAD_USED + 0
sta mb1_directory + MB1_HEAD_USED + 0
bcc .used_ok
inc mb1_directory + MB1_HEAD_USED + 1
.used_ok: inc mb1_file_count ; #files.
tya ; Goto next file entry.
clc
adc #$10-1
tay
bne .used_loop
inc <__ax + 1
dec <__cl
bne .used_loop
bra .used_done ; All directory entries used!
.invalid_file: tya ; Go back to the start of the
and #$F0 ; invalid file entry.
tay
ldx #MB1_ERR_INVALID ; Signal directory has errors.
.used_done: sec ; Clear out the rest of the
tya ; directory.
sta .self_mod + 3 ; Destination-lo.
eor #$FF
adc #<(mb1_directory + 1024)
sta .self_mod + 5 ; Length-lo.
lda <__ax + 1
sta .self_mod + 4 ; Destination-hi.
eor #$FF
adc #>(mb1_directory + 1024)
sta .self_mod + 6 ; Length-hi.
ora .self_mod + 5
beq .dir_full
.self_mod: tai tos_zero, mb1_directory, $0400
.dir_full: lda <__bh ; Is there free space at the
bne .checksum ; end of the MB128?
inc mb1_frag_count ; #free-space fragments.
.checksum: phx ; Preserve status code.
lda #<(mb1_directory + 2) ; Generate the checksum.
sta <__ax + 0