-
Notifications
You must be signed in to change notification settings - Fork 22
/
diskmenu.a65
931 lines (806 loc) · 14.6 KB
/
diskmenu.a65
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
;
; Display list of disk images on SD card on MEGA65, and allow user to pick which one
; to mount.
;
; Display configuration will (for now) be 40x25 screen with 16 character filenames
; in columns. The on-screen order is to allow moving sideways to skip 21 disks
; at a time. Only two columns will be shown on screen at a time.
;
; To save memory, we will only cache the names of the disk images. When one is selected
; then we repeat the directory scan to find the matching disk image name, and then mount
; it from there. Hopefully this won't be too slow. It would also be nice to sort the list
; alphabetically when it is displayed.
;
; List of disk names begins at $1000 - $9FFF
; 16 bytes each, so 36KB can hold 2,304 disks
.alias disknamearray $1000
; Where to get Hypervisor to copy dirent structures
.alias dirent $0f00
; temporary filename structure (must also be <32KB)
.alias scratch_filename $0e00
; 16-bit pointers to current disk and number of
; disks.
.alias displaydiskpointer $40
.alias diskpointer $fe
.alias sortpointer $f7
; pointer for where we are drawing on screen
.alias screenpointer $f9
.scope
; enable enhanced registers
lda #$47
sta $d02f
lda #$53
sta $d02f
; Clear screen
lda #$0e
sta $d020
lda #$06
sta $d021
; Lower-case
lda #$0e
jsr $ffd2
ldx #0
ll9: lda #$20
sta $0400,x
sta $0500,x
sta $0600,x
sta $0700,x
lda #$e
sta $d800,x
sta $d900,x
sta $da00,x
sta $db00,x
inx
bne ll9
; top and bottom text
ldy #39
ll13: lda topbanner,y
jsr asciitocharrom
ora #$80
sta $0400,y
lda bottombanner,y
jsr asciitocharrom
ora #$80
sta $07c0,y
dey
bpl ll13
; reset state
lda #$00
sta currentitem
sta currentitem+1
sta diskcount
sta diskcount+1
sta screenitem
sta screenitem+1
; enquire hypervisor version
; (protect NMI handler from non-zero Z register value which
; can upset some 6502 instructions).
sei
lda #$00
sta $d640
; XXX - work around bug in hypervisor traps, where the byte
; after a trap may or may not get executed
nop
ldz #$00
cli
cmp #$00
bne getdisklist
; no hypervisor. not on a mega65?
ldx #39
ll15: lda nohypermsg,x
jsr asciitocharrom
sta $05e0,x
dex
bpl ll15
ll16: jmp ll16
nohypermsg:
.byte "No reply from Hypervisor - not a MEGA65?"
getdisklist:
; make sure Z = $00 after OS & DOS version enquiry
lda #$00
taz
; Now that we know we are on a real MEGA65, ask for fast CPU
lda #65
sta 0
; Open current directory via Hypervisor
lda #$12
sta $d640
; XXX - Work around hypervisor trap bug
nop
bcs opendirok
; Display error message
ldx #39
ll17: lda noopendirmsg,x
jsr asciitocharrom
sta $05e0,x
dex
bpl ll17
jmp ll16
noopendirmsg:
.byte " Couldn't open directory for reading "
opendirok:
; Remember file descriptor for calls to readdir
sta filedescriptor
; get pointer ready for writing disk names from DIRENT structure
lda #<disknamearray
sta diskpointer
lda #>disknamearray
sta diskpointer+1
readnextentry:
; ask for next directory entry
lda #$14
; Ask for entry to be written to screen RAM for debugging
ldx filedescriptor
ldy #>dirent
; XXX - NOP after trap to hypervisor to work around CPU bug
sta $d640
nop
bcc endofdir
; Check file type: we only want regular files
; Skip "hidden" files
lda dirent
cmp #$2e
beq readnextentry
; Check if filename ends in .D81
; Begin by checking length >4 (i.e, something.d81)
lda dirent+64
cmp #5
bcc readnextentry
tay
dey
lda dirent,y
cmp #$31
bne readnextentry
dey
lda dirent,y
cmp #$38
bne readnextentry
dey
lda dirent,y
cmp #$44
beq isd81file
cmp #$64
bne readnextentry
isd81file:
; Chop extension from filename
lda dirent+64
sec
sbc #$04
sta dirent+64
; Got an entry - copy name
ldy #$00
ll19: lda dirent,y
sta (diskpointer),y
iny
cpy dirent+64
beq ll19b
cpy #$10
bne ll19
ll19b:
; blank out beyond end of filename
; get length byte from dirent structure
cpy #$10
bcs ll20
lda #$00
ll21: cpy #$10
bcs ll20
sta (diskpointer),y
iny
jmp ll21
ll20:
; Bump directory count
lda diskcount
clc
adc #$01
sta diskcount
lda diskcount+1
adc #$00
sta diskcount+1
; Limit directory to fit between $1000-$8FFF
; (= 2,304 entries)
cmp #$90
bcs endofdir
; update pointer address for dirent
lda diskpointer
clc
adc #$10
sta diskpointer
lda diskpointer+1
adc #$00
sta diskpointer+1
; jsr updatescreen
; check if entry is the only / first one and don't sort if so
lda diskcount+1
bne readnextentry
lda diskcount
cmp #$01
bcc readnextentry
jsr sortentry
jmp readnextentry
endofdir:
ldz #0
mainloop:
; display current screen of disks
jsr updatescreen
waitforkey:
; check for keyboard input
jsr $ffe4
bne havekey
jsr scanjoystick
havekey:
cmp #$11
bne notdownkey
ldx #1
ldy #0
jsr adjustcurrentitem
jsr adjustcurrentscreen
jmp mainloop
notdownkey:
cmp #$91
bne notupkey
ldx #$ff
ldy #$ff
jsr adjustcurrentitem
jsr adjustcurrentscreen
jmp mainloop
notupkey:
cmp #$1D
bne notrightkey
ldx #21
ldy #0
jsr adjustcurrentitem
jsr adjustcurrentscreen
jmp mainloop
notrightkey:
cmp #$9D
bne notleftkey
ldx #235
ldy #$ff
jsr adjustcurrentitem
jsr adjustcurrentscreen
jmp mainloop
notleftkey:
cmp #$30
bne notzerokey
jsr jumptofirstitem
jsr adjustcurrentscreen
jmp mainloop
notzerokey:
cmp #$39
bne notninekey
jsr jumptolastitem
jsr adjustcurrentscreen
jmp mainloop
notninekey:
cmp #$03
bne notrunstopkey
; Exit nicely to basic.
ldz #$00
jmp ($a002)
notrunstopkey:
cmp #$0d
bne notreturnkey
ldx #39
me12: lda msg_mounttrying,x
jsr asciitocharrom
ora #$80
sta $07c0,x
dex
bpl me12
; RETURN pressed, so mount image and exit
; To do this, we just need the name of the .D81 file (with extension)
; and call the DOS setname trap, and then the d81attach trap
; So first step is to find any copy the disk image name
lda currentitem
asl
asl
asl
asl
sta diskpointer
lda currentitem
lsr
lsr
lsr
lsr
sta diskpointer+1
lda currentitem+1
asl
asl
asl
asl
ora diskpointer+1
clc
adc #>disknamearray
sta diskpointer+1
ldy #$00
r1: lda (diskpointer),y
beq eoname
sta scratch_filename,y
iny
cpy #$10
bne r1
eoname: ldx #0
eoname1:
lda dotd81,x
sta scratch_filename,y
iny
inx
cpx #5
bne eoname1
; call DOS setname function
dey
tya
taz
lda #$2e
ldx #<scratch_filename
ldy #>scratch_filename
sta $d640
nop
ldz #$00
bcc mounterror
; call DOS disk image mount function
lda #$40
sta $d640
nop
bcc mounterror
ldx #39
me11: lda msg_mountsuccess,x
jsr asciitocharrom
ora #$80
sta $07c0,x
dex
bpl me11
jmp mainloop
msg_mountsuccess:
.byte "Disk image mounted. RUN/STOP to exit. "
msg_mounttrying:
.byte "Attempting to mount disk image... "
mounterror:
; Get DOS error code from hypervisor
lda #$38
sta $d640
nop
; Put dos error code into mount error message
tax
jsr checkpoint_bytetohex
sty msg_mounterror+34
stx msg_mounterror+35
ldx #39
me1: lda msg_mounterror,x
jsr asciitocharrom
ora #$80
sta $07c0,x
dex
bpl me1
jmp mainloop
msg_mounterror:
.byte "Error mounting disk image (errno=$nn) "
dotd81:
.byte ".d81",0
notreturnkey:
jmp waitforkey
rts
nybltohex:
cmp #$a
bcs nth1
ora #$30
rts
nth1: sec
sbc #9
rts
checkpoint_bytetohex:
txa
and #$f0
lsr
lsr
lsr
lsr
jsr checkpoint_nybltohex
tay
txa
and #$0f
jsr checkpoint_nybltohex
tax
rts
checkpoint_nybltohex:
and #$0f
ora #$30
cmp #$3a
bcs cpnth1
rts
cpnth1: adc #$06
rts
adjustcurrentscreen:
; Check if current item is before what we are displaying now,
; if so, move display to the left a column
lda currentitem
sec
sbc screenitem
lda currentitem+1
sbc screenitem+1
bcs notbeforehere
lda screenitem
sec
sbc #21
sta screenitem
lda screenitem+1
sbc #0
sta screenitem+1
jmp adjustcurrentscreen
notbeforehere:
; Now check the select item would be off to the right of the
; edge of the screen.
; work out where edge of the screen would be
lda screenitem
clc
adc #41
sta temp16
lda screenitem+1
adc #0
sta temp16+1
; now do the comparison
lda temp16
sec
sbc currentitem
lda temp16+1
sbc currentitem+1
bcs notafterhere
lda screenitem
clc
adc #21
sta screenitem
lda screenitem+1
adc #0
sta screenitem+1
jmp adjustcurrentscreen
notafterhere:
rts
adjustcurrentitem:
txa
clc
adc currentitem
sta currentitem
tya
adc currentitem+1
sta currentitem+1
bmi itemnegative
sec
sbc diskcount+1
bpl itemnumbermaybetoolarge
; Item number is ok
rts
itemnumbermaybetoolarge:
lda currentitem
cmp diskcount
bcs itemnumbertoolarge
; Item number is ok
rts
itemnumbertoolarge:
; item negative, so limit to end of list
; ... or better, wrap around for convenience
jumptofirstitem:
lda #0
sta currentitem
sta currentitem+1
rts
itemnegative:
; if trying to go before start of list, then limit to start of
; list.
; ... or better, wrap around for convenience
jumptolastitem:
lda diskcount
sec
sbc #1
sta currentitem
lda diskcount+1
sbc #0
sta currentitem+1
rts
sortentry:
; char offset for comparing the entries (max 16)
ldy #00
; flag if proper position has been found AND entry must be moved
ldx #00
lda diskpointer
sta sortpointer
lda diskpointer+1
sta sortpointer+1
sortloop2:
sec
lda sortpointer+1
sbc #$10
sta sortpointer+1
lda sortpointer
sbc #$00
sta sortpointer
sortloop1:
lda (diskpointer),y
cmp (sortpointer),y
bcs sortgreatervalue
bne sortlesservalue
iny
; advance to next char
jmp sortloop1
sortlesservalue:
ldx #$01
jmp sortloop2
sortgreatervalue:
txa
; if X flag is not set sorting is not necessary (because last entry stays last)
beq exitsort
; Prepare to do DMA copy to make space for this entry
; First, we add $0f to diskpointer to get the last byte to
; get the copy source address.
clc
lda diskpointer
adc #15
sta sortdmasrc
lda diskpointer+1
adc #$00
sta sortdmasrc+1
; the destination address is $10 more, so that we make 16 bytes of space
; for the sorted entry to be copied into
clc
lda diskpointer
adc #31
sta sortdmadst
lda diskpointer+1
adc #$00
sta sortdmadst+1
; We need to copy diskpointer-sortpointer bytes
sec
lda diskpointer
sbc sortpointer
sta sortdmacnt
lda diskpointer+1
sbc sortpointer+1
sta sortdmacnt+1
; Set DMAgic list address and trigger job
; (Since we are on a MEGA65, also clear the high bytes of list and src/dst address)
lda #$00
sta $d702
sta $d704
sta $d705
sta $d706
lda #>sortdmalist
sta $d701
lda #<sortdmalist
sta $d700
;now copy the new entry to the space we just freed
ldx #15
sortcopy:
lda diskpointer+15,x
sta sortpointer,x
dex
bne sortcopy
exitsort:
rts
updatescreen:
; Draw 2 columns x 25 disk image names
; Current selection is displayed reversed
; get pointer to start of array
lda #<disknamearray
sta displaydiskpointer
lda #>disknamearray
sta displaydiskpointer+1
; add on for starting item on the screen
lda screenitem
sta temp16
lda screenitem+1
sta temp16+1
ldx #$04
ll1: clc
rol temp16
rol temp16+1
dex
bne ll1
lda displaydiskpointer
clc
adc temp16
sta displaydiskpointer
lda displaydiskpointer+1
adc temp16+1
sta displaydiskpointer+1
; Now display 2 columns of 21 names each
lda #<$0452
sta screenpointer
lda #>$0452
sta screenpointer+1
; Keep track of which item is current, so that we can
; highlight it when drawing
lda screenitem
sta temp16
lda screenitem+1
sta temp16+1
ldx #$00
drawnextdiskname:
; Is the item we are drawing past the end of the list?
lda temp16
cmp diskcount
bcc ll10
lda temp16+1
cmp diskcount+1
bcc ll10
; Item is past end of list, so don't draw
ldy #$0f
lda #$20
ll12: sta (screenpointer),y
dey
bpl ll12
jmp ll11
ll10:
ldy #$0f
ll2: lda (displaydiskpointer),y
jsr asciitocharrom
sta (screenpointer),y
dey
bpl ll2
ll11:
; Work out drawing attributes for this item
lda #$0e
sta colour
lda #$00
sta reverse
; is this the current item?
lda currentitem
cmp temp16
bne ll5
lda currentitem+1
cmp temp16+1
bne ll5
; it's the current item, so draw in reverse white
; instead
lda #$01
sta colour
lda #$80
sta reverse
ll5:
; set reverse video as required
ldy #$0f
ll6: lda (screenpointer),y
ora reverse
sta (screenpointer),y
dey
bpl ll6
; jump to colour ram
lda screenpointer+1
clc
and #$03
ora #$d8
sta screenpointer+1
ldy #$0f
lda colour
ll7: sta (screenpointer),y
dey
bpl ll7
; switch back to screen RAM
lda screenpointer+1
and #$03
ora #$04
sta screenpointer+1
; advance screen item number for comparison with current
; selected item.
inc temp16
lda temp16+1
adc #0
sta temp16+1
; advance screen pointer ready for drawing next item
lda screenpointer
clc
adc #$28
sta screenpointer
lda screenpointer+1
adc #0
sta screenpointer+1
; advance disk name pointer
lda displaydiskpointer
clc
adc #$10
sta displaydiskpointer
lda displaydiskpointer+1
adc #0
sta displaydiskpointer+1
; XXX - Reverse video for current item
inx
cpx #21
bne ll4
; Disk 22 is start of 2nd column, so update screen pointer
lda #<$0466
sta screenpointer
lda #>$0466
sta screenpointer+1
ll4:
cpx #42
beq alldonedrawing
jmp drawnextdiskname
alldonedrawing:
rts
waitoneframe:
lda $d012
wof1:
cmp $d012
beq wof1
wof2:
cmp $d012
bne wof2
rts
waitonesecond:
pha
phx
phy
phz
ldx #60
wos1: jsr waitoneframe
dex
bpl wos1
plz
ply
plx
pla
rts
asciitocharrom:
; NUL ($00) becomes a space
cmp #0
bne atc0
lda #$20
atc0:
; @ becomes $00
cmp #$40
bne atc1
lda #0
rts
atc1:
cmp #$5b
bcs atc2
; A to Z -> leave unchanged
rts
atc2:
cmp #$5b
bcc atc3
cmp #$60
bcs atc3
; [ \ ] ^ _ -> subtract $40
and #$1f
rts
atc3:
cmp #$61
bcc atc4
cmp #$7b
bcs atc4
; a - z -> subtract $60
and #$1f
rts
atc4: rts
scanjoystick:
lda $dc00
and $dc01
and #$1f
tay
lda joysticktable,y
rts
joysticktable:
; UDLRF $91,$11,$9d,$1d,$0d
.byte $00,$91,$11,$00,$9d,$91,$11,$00
.byte $1d,$91,$10,$00,$00,$00,$00,$00
.byte $0d,$0d,$0d,$0d,$0d,$0d,$0d,$0d
.byte $0d,$0d,$0d,$0d,$0d,$0d,$0d,$0d
.scend
iter1: .byte 0
diskcount: .byte 0,0
sortdmalist: .byte $00 ; copy non-chained job
sortdmacnt: .word $0001 ; number of bytes to copy
sortdmasrc: .word $0000 ; source address
.byte $40 ; source bank number / direction
sortdmadst: .word $0000 ; destination address
.byte $40 ; destination bank number / direction
.word $0000 ; modulo
screenitem: .byte 0,0
currentitem: .byte 0,0
temp16: .byte 0,0
colour: .byte 14 ; light blue
reverse: .byte 0
filedescriptor: .byte 0
topbanner:
.byte "MEGA65 Disk Image Chooser V00.01 "
bottombanner:
.byte ">> Arrows to navigate, ENTER to mount <<"