forked from gardners/c65gs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kickstart.a65
2657 lines (2356 loc) · 54.9 KB
/
kickstart.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
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
; MEGA65 Kick Start ROM.
; Paul Gardner-Stephen, 2014-2016.
;
; Purpose:
; 1. Verify checksum of ROM area of slow RAM.
; 1. If checksum fails, load complete ROM from SD card.
; 2. Select default disk image for F011 emulation.
;
; The kickstart ROM is 16KB in length, and maps at $8000-$BFFF
; in hypervisor mode.
;
; Kickstart modifies RAM from $0000-$07FFF (ZP, stack, 40-column
; screen, 16-bit text mode) during normal boot.
;
; If Kickstart needs to load the ROM from SD card, then it may
; modify the first 64KB of fast ram.
;
; We will use the convention of C=0 means failure, and C=1 means
; success.
;
.include "kickstart_machine.a65"
; scratch space in ZP space usually used by kernel
; we try to use address space not normally used by C64 kernel, so that
; it is possible to make calls to kickstart after boot. Eventually
; the desire is to have an SYS call that brings up a menu that lets
; you choose a disk image from a list.
.data
.org $ce00
.space romslab 1
.space screenrow 1
.space checksum 4
.space file_pagesread 2
; variables for testing of D81 boot image
.space d81_clusternumber 4
.space d81_clustersneeded 2
.space d81_clustercount 2
; make sure that we don't go past the 256 byte page reserved for hypervisor scratch space
.checkpc $CEFF
.text
.org $8000
;;; ----------------------------------------------------------------------------
;;; CPU Hypervisor Trap entry points.
;;; 64 x 4 byte entries for user-land traps.
;;; some more x 4 byte entries for system traps (reset, page fault etc)
;;; ----------------------------------------------------------------------------
trap_entry_points:
; Traps $00-$07 (user callable)
jmp dos_and_process_trap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
; Traps $08-$0F (user callable)
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
; Traps $10-$17 (user callable)
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
; Traps $18-$1F (user callable)
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
; Traps $20-$27 (user callable)
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
; Traps $28-$2F (user callable)
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
; Traps $30-$37
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
; Traps $38-$3F (user callable)
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
; Traps $40-$4F (reset, page fault and other system-generated traps)
jmp reset_entry_allow_etherkick ; Trap #$40 (power on / reset)
nop
jmp page_fault ; Trap #$41 (page fault)
nop
jmp double_restore_trap ; Trap #$42 (double-tap RESTORE key)
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
jmp nosuchtrap
nop
; Leave room for traps to $7E
.checkpc $81F8
.advance $81F8
; Then we have relocated CPU vectors
.word reset_entry_allow_etherkick ; unused vector
.word hypervisor_nmi ; NMI
.word reset_entry_allow_etherkick ; RESET
.word hypervisor_irq ; IRQ
.advance $8200
;;; ----------------------------------------------------------------------------
;;; Hypervisor traps
;;; ----------------------------------------------------------------------------
;;; ----------------------------------------------------------------------------
;;; Illegal trap / trap sub-function handlers
;;; ----------------------------------------------------------------------------
; Traps are triggered by writing to $D640-$D67F
; and trap to $8000+((address & $3F)*4) in the hypervisor
; Routine for unimplemented/reserved traps
; (Consider replacing with trap to hypervisor error screen with option
; to return?)
nosuchtrap:
; Clear C flag for caller to indicate failure
lda hypervisor_flags
and #$FE ; C flag is bit 0
sta hypervisor_flags
; set A to $FF
lda #$ff
sta hypervisor_a
; return from hypervisor
sta hypervisor_enterexit_trigger
; Return from trap with C flag clear to indicate success
return_from_trap_with_success:
jsr sd_unmap_sectorbuffer
; set C flag for caller to indicate success
lda hypervisor_flags
ora #$01 ; C flag is bit 0
sta hypervisor_flags
jsr checkpoint
.byte 0,"return_from_trap_with_success",0
; return from hypervisor
sta hypervisor_enterexit_trigger
return_from_trap_with_failure:
jsr sd_unmap_sectorbuffer
; report error in A
sta hypervisor_a
lda hypervisor_flags
and #$fe ; C flag is bit 0
sta hypervisor_flags
jsr checkpoint
.byte 0,"return_from_trap_with_failure",0
; return from hypervisor
sta hypervisor_enterexit_trigger
invalid_subfunction:
jmp nosuchtrap
;;; ----------------------------------------------------------------------------
;;; DOS, process control and related functions trap
;;; ----------------------------------------------------------------------------
.include "kickstart_dos.a65"
;;; ----------------------------------------------------------------------------
;;; Virtual memory and memory management
;;; ----------------------------------------------------------------------------
.include "kickstart_mem.a65"
;;; ----------------------------------------------------------------------------
;;; Task (process) management
;;; ----------------------------------------------------------------------------
.include "kickstart_task.a65"
;;; ----------------------------------------------------------------------------
;;; CPU Hypervisor Entry Point on reset
;;; ----------------------------------------------------------------------------
reset_machine_state:
; get CPU state sensible
sei
cld
see
; disable IRQ/NMI sources
lda #$7f
sta $DC0D
sta $DD0D
LDA #$00
STA $D019
sec
jsr enhanced_io
lda $d60d
; switch to fast mode
; 1. C65 fast-mode enable
lda $d031
ora #$40
sta $d031
; 2. MEGA65 48MHz enable (requires C65 or C128 fast mode to truly enable, hence the above)
lda $d054
ora #$40
sta $d054
; sprites off
lda #$00
sta $d017
lda #$7f
sta $DC0D
sta $DD0D
lda #$00
sta $D01A
; We DO NOT need to mess with $01, because
; the 4510 starts up with kickstart mapped at $8000-$BFFF
; enhanced ($FFD3xxx) IO page mapped at $D000,
; and fast RAM elsewhere.
; Disable reset watchdog, enable flat 32-bit addressing, don't engage
; ROM write protect (yet), do make ASC/DIN / CAPS LOCK control CPU speed.
; also force 4502 CPU personality (6502 personality is still incomplete)
; (The watchdog was added to catch reset problems where the machine
; would run off somewhere odd instead of resetting properly. Now it
; will auto-reset after 65535 cycles if the watchdog is not cleared).
lda #$2a
sta $d67d
jsr resetdisplay
jsr erasescreen
jsr resetpalette
jsr checkpoint
.byte 0,"reset_machine_state",0
rts
;;; ----------------------------------------------------------------------------
;;; CPU Hypervisor reset/trap routines
;;; ----------------------------------------------------------------------------
reset_entry_allow_etherkick:
sei
lda #$00
sta kickstart_boot_flags
jmp reset_entry_common
reset_entry_no_etherkick:
sei
lda #$80
sta kickstart_boot_flags
jmp reset_entry_common
reset_entry_common:
jsr reset_machine_state
; display welcome screen
ldx #<msg_kickstart
ldy #>msg_kickstart
jsr printbanner
; leave a blank line below kickstart banner
ldx #<msg_blankline
ldy #>msg_blankline
jsr printmessage
; Display GIT commit
ldx #<msg_gitcommit
ldy #>msg_gitcommit
jsr printmessage
; check keyboard for 0-9 down to select alternate rom
jsr keyboardread
; If switch 8 and 9 are set, and etherkick mode is not disabled
; then wait forever for ethernet packets
lda kickstart_boot_flags
bmi normalboot
lda $d6f1
and #$02
beq normalboot
etherboot:
ldx #<msg_etherkick
ldy #>msg_etherkick
jsr printmessage
jsr setupethernet
etherloop:
jsr checkethernet
.byte 0,"etherloop",0
jmp etherloop
normalboot:
; Try to read the MBR from the SD card to ensure SD card is
; happy
ldx #<msg_tryingsdcard
ldy #>msg_tryingsdcard
jsr printmessage
tryreadmbr:
jsr readmbr
bcs gotmbr
jmp tryreadmbr
gotmbr:
; Scan SD card for partitions and mount them.
jsr dos_clearall
jsr dos_read_partitiontable
ldx #<msg_diskcount
ldy #>msg_diskcount
jsr printmessage
ldy #$00
ldz dos_disk_count
jsr printhex
; Go to root directory on default disk
ldx dos_default_disk
jsr dos_cdroot
bcs mountsystemdiskok
ldx #<msg_cdrootfailed
ldy #>msg_cdrootfailed
jsr printmessage
ldy #$00
ldz dos_error_code
jsr printhex
mountsystemdiskok:
; Load and display boot logo
; Prepare 32-bit pointer for loading boot logo @ $0004000
lda #$00
sta <dos_file_loadaddress+0
lda #$40
sta <dos_file_loadaddress+1
lda #$00
sta <dos_file_loadaddress+2
lda #$00
sta <dos_file_loadaddress+3
ldx #<txt_bootlogo
ldy #>txt_bootlogo
jsr dos_setname
jsr dos_readfileintomemory
bcs logook
ldx #<msg_nologo
ldy #>msg_nologo
jsr printmessage
ldy #$00
ldz dos_error_code
jsr printhex
logook:
; iterate through directory entries looking for ordinary file
; KICKUP.M65 to load into hypervisor memory ...
; ... but only if we are not running a kick-up'd kickstart now.
lda $d67e
beq nextdirectoryentry3
; already kicked
ldx #<msg_alreadykicked
ldy #>msg_alreadykicked
jsr printmessage
jmp postkickup
nextdirectoryentry3:
; Prepare 32-bit pointer for loading boot logo @ $0004000
lda #$00
sta <dos_file_loadaddress+0
lda #$40
sta <dos_file_loadaddress+1
lda #$00
sta <dos_file_loadaddress+2
lda #$00
sta <dos_file_loadaddress+3
ldx #<txt_kickupfile
ldy #>txt_kickupfile
jsr dos_setname
jsr dos_readfileintomemory
bcc nokickup
; We have loaded kickup file, so jump into it.
ldx #<msg_kickuploaded
ldy #>msg_kickuploaded
jsr printmessage
ldy #$00
ldz <zptempv32+3
jsr printhex
ldz <zptempv32+2
jsr printhex
ldz <zptempv32+1
jsr printhex
ldz <zptempv32+0
jsr printhex
; Use DMAgic to copy $0004000-$0007FFF to $FFF8000-$FFFBFFF
; (We have to copy the routine to do this to RAM, since we will
; be replacing ourselves)
ldx #$00
krc: lda kickuproutine,x
sta $3000,x
inx
bne krc
jmp $3000
kickuproutine:
; The following routine gets copied as-is to $3000 and run from there.
; The DMA list is still available in the kickstart ROM when it gets
; called, so we can just use it there, instead of working out where
; it gets copied to
; Set bottom 22 bits of DMA list address as for C65
; (8MB address range). Kickstart ROM is at $FFF8000, so $FF goes
; in high-byte area
lda #$ff
sta $d702
lda #$ff
sta $d704 ; dma list is in top MB of address space
sta $d706 ; similarly destination of copy is top MB of address space
lda #$00
sta $d705 ; source of copy is bottom MB of address space
lda #>kickupdmalist
sta $d701
lda #<kickupdmalist
sta $d700
; clear source/destination MB so that C65 ROM doesn't go bananas
lda #$00
sta $d706
; copy complete, so mark ourselves upgraded, and jump into hypervisor
; as though we were just reset.
sta $d67e ; mark ourselves as having kicked up
jmp $8100
kickupdmalist:
; copy $0004000-$0007FFF to $FFF8000-$FFFBFFF
; (MB offsets get set in routine)
.byte $00 ; copy + last request in chain
.word $4000 ; size of copy is 16KB
.word $4000 ; starting at $4000
.byte $00 ; of bank $0
.word $8000 ; destination address is $8000
.byte $0F ; of bank $F
.word $0000 ; modulo (unused)
couldntopenkickup:
nokickup:
ldx #<msg_nokickup
ldy #>msg_nokickup
jsr printmessage
postkickup:
; MILESTONE: Have file system properties.
; Look for MEGA65.D81 to mount for F011 emulation
; for now indicate that there is no disk in drive
lda #$00
sta $d68b
jsr dos_cdroot
bcc sdcarderror
; set name of file we are looking for
ldx #<txt_c65gsd81
ldy #>txt_c65gsd81
jsr dos_setname
jsr dos_findfile
bcc nod81
jsr dos_closefile
jsr dos_d81attach
bcc d81attachfail
ldx #<msg_d81mounted
ldy #>msg_d81mounted
jsr printmessage
; all done, move on to loading the ROM
jmp findrom
d81attachfail:
; we couldn't find the D81 file, so tell the user
ldx #<msg_nod81
ldy #>msg_nod81
jsr printmessage
ldy #$00
ldz dos_error_code
jsr printhex
findrom:
; Check state of current ROM
jsr checkromok
bcc loadrom
; ROM is loaded and ready, so transfer control
; to it.
ldx #<msg_romok
ldy #>msg_romok
jsr printmessage
jmp go64
loadrom:
; ROM is not loaded, so try to load it, or prompt
; for user to insert SD card
ldx #<msg_rombad
ldy #>msg_rombad
jsr printmessage
; Load CHARROM.M65 into character ROM
ldx #<txt_c65gscharrom
ldy #>txt_c65gscharrom
jsr dos_setname
; Prepare 32-bit pointer for loading whole ROM ($FF7E000)
lda #$00
sta <dos_file_loadaddress+0
lda #$E0
sta <dos_file_loadaddress+1
lda #$F7
sta <dos_file_loadaddress+2
lda #$0F
sta <dos_file_loadaddress+3
jsr dos_readfileintomemory
bcs loadedcharromok
ldx #<msg_charrombad
ldy #>msg_charrombad
jsr printmessage
jmp loadc65rom
loadedcharromok:
ldx dos_current_file_descriptor_offset
lda dos_file_descriptors+dos_filedescriptor_offset_fileoffset+0,x
sta file_pagesread
lda dos_file_descriptors+dos_filedescriptor_offset_fileoffset+1,x
sta file_pagesread+1
ldx #<msg_charromloaded
ldy #>msg_charromloaded
jsr printmessage
ldy #$00
ldz file_pagesread+1
jsr printhex
ldz file_pagesread
jsr printhex
loadc65rom:
ldx #<txt_c65gsrom
ldy #>txt_c65gsrom
jsr dos_setname
; Prepare 32-bit pointer for loading whole ROM ($0020000)
lda #$00
sta <dos_file_loadaddress+0
sta <dos_file_loadaddress+1
sta <dos_file_loadaddress+3
lda #$02
sta <dos_file_loadaddress+2
jsr dos_readfileintomemory
bcs loadedok
; ROM not found: indicate which ROM we were looking for
ldx #$0b
l17d: lda txt_c65gsrom,x
sta msg_romnotfound+19,x
dex
bne l17d
ldx #<msg_romnotfound
ldy #>msg_romnotfound
jsr printmessage
jsr sdwaitawhile
jsr sdwaitawhile
jsr sdwaitawhile
jsr sdwaitawhile
jmp sdcarderror
loadedok:
ldx dos_current_file_descriptor_offset
lda dos_file_descriptors+dos_filedescriptor_offset_fileoffset+0,x
sta file_pagesread
lda dos_file_descriptors+dos_filedescriptor_offset_fileoffset+1,x
sta file_pagesread+1
lda file_pagesread
bne romfiletooshort
lda file_pagesread+1
cmp #$00
beq romfiletooshort
cmp #$02
bne romfiletoolong
; need to store checksum
jsr storeromsum
; ROM file loaded, transfer control
ldx #<msg_romok
ldy #>msg_romok
jsr printmessage
jmp go64
romfiletoolong:
ldx #<msg_romfilelongerror
ldy #>msg_romfilelongerror
jsr printmessage
ldz file_pagesread+1
jsr printhex
ldz file_pagesread
jsr printhex
jsr sdwaitawhile
jmp reset_entry_allow_etherkick
romfiletooshort:
ldx #<msg_romfileshorterror
ldy #>msg_romfileshorterror
jsr printmessage
ldz file_pagesread+1
jsr printhex
ldz file_pagesread
jsr printhex
jsr sdwaitawhile
jmp reset_entry_allow_etherkick
fileopenerror:
ldx #<msg_fileopenerror
ldy #>msg_fileopenerror
jsr printmessage
sdcarderror: ldx #<msg_sdcarderror
ldy #>msg_sdcarderror
jsr printmessage
jsr sdwaitawhile
jmp reset_entry_allow_etherkick
badfs: ldx #<msg_badformat
ldy #>msg_badformat
jsr printmessage
jsr sdwaitawhile
jmp reset_entry_allow_etherkick
;;; ----------------------------------------------------------------------------
;;; FAT file system routines
;;; ----------------------------------------------------------------------------
toupper: ; convert ASCII character to upper case
cmp #$60
bcc tu1
cmp #$7a
bcs tu1
and #$5f
tu1: rts
;;; ----------------------------------------------------------------------------
;;; MBP / partition routines
;;; ----------------------------------------------------------------------------
; read master boot record. Does not sanity check anything.
readmbr:
; begin by resetting SD card
jsr sdreset
bcs l7
rts
l7: ldx #<msg_foundsdcard
ldy #>msg_foundsdcard
jsr printmessage
; MBR is sector 0
lda #$00
sta $D681
sta $d682
sta $d683
sta $d684
jmp sd_readsector
;;; ----------------------------------------------------------------------------
;;; SD Card access routines
;;; ----------------------------------------------------------------------------
sdreset:
; try SD-HC mode first
; lda #$41
; sta $D680
; jsr sd_resetsequence
; bcc sdr1
; rts
sdr1: ; fall back to SD mode
lda #$42
sta $d680
; fall through
sd_resetsequence:
; write $00 to $D680 to start reset
lda #$00
sta $D680
jsr sdtimeoutreset
re1: jsr sdreadytest
bcs re1done ; success, so return
bne re1 ; not timed out, so keep trying
rts ; timeout, so return
re1done:
; write $01 to $D680 to complete reset
lda #$01
sta $D680
jsr sdtimeoutreset
re2: jsr sdreadytest
bcs re2done ; success, so return
bne re2 ; not timed out, so keep trying
rts ; timeout, so return
re2done:
; give card some time to settle after reset, otherwise
; reading sectors will fail
jsr sdwaitawhile
jsr sd_map_sectorbuffer
; write $02 to $D680 to read MBR as a test of reset
lda #$02
sta $D680
jsr sdtimeoutreset
re3: ; allow time for read to happen
lda $d680
jsr sdreadytest
bcs redone ; success, so return
bne re3 ; not timed out, so keep trying
; timeout -- so call reset again
rts
redone:
sec
rts
; Watch for ethernet packets while waiting for the SD card.
; this allows loading of code into the hypervisor for testing and
; bare-metal operation.
sdwaitawhile: jsr sdtimeoutreset
sw1:
inc $d020
jsr checkethernet
dec $d020
inc $0300
bne sw1
inc $0301
bne sw1
inc $0302
bne sw1
rts
; count to timeout value when trying to read from SD card
; (if it is too short, the SD card won't reset)
sdtimeoutreset:
lda #$00
sta $0300
sta $0301
lda #$f7
sta $0302
rts
sdreadytest: ; check if SD card is ready, or if timeout has occurred
; C is set if ready.
; Z is set if timeout has occurred.
lda $d680
and #$03
beq sdisready
inc $0300
bne sr1
inc $0301
bne sr1
inc $0302
bne sr1
; timeout
lda #$00 ; set Z
sr1: clc
rts
sdisready: sec
rts
sd_map_sectorbuffer:
lda #$81
sta $D680
sec
rts
sd_unmap_sectorbuffer:
pha
lda #$82
sta $D680
pla
sec
rts
sd_readsector:
; Assumes fixed sector number (or byte address in case of SD cards)
; is loaded into $D681 - $D684
; check if sd card is busy
lda $d680
and #$01
bne rsbusyfail
; try reading sector fast the first time
jmp rs4
redoread:
; when retrying, introduce a delay. This seems to be needed often
; when reading the first sector after SD card reset.
ldx #$f0
ldy #$00
ldz #$00
r1: inz
bne r1
iny
bne r1
inx
bne r1
rs4:
; ask for sector to be read
lda #$02
sta $d680
; wait for sector to be read
jsr sdtimeoutreset
rs3:
jsr sdreadytest
bcs rsread
bne rs3
jmp rereadsector
rsread:
lda $d680
and #$01
bne rs3
; check that we read 512 bytes
lda $d688
lda $d689
cmp #$02
bne redoread
sec
rts
; reset sd card and try again
rereadsector:
jsr sdreset
jmp rs4
rsbusyfail: ; fail
lda #dos_errorcode_read_timeout
sta dos_error_code
clc
rts
printsectoraddress:
ldx #<msg_sectoraddress