-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpm_bios.asm
1227 lines (1151 loc) · 60.9 KB
/
cpm_bios.asm
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
;**************************************************************
;*
;* CP/M BIOS version 2.2
;*
;* Reconstructed from Sanco CP/M Floppy Image
;*
;* by RetrOfficina GLG
;*
;**************************************************************
org 0xf200
PUBLIC _main
_main:
jp $f8ae ;[f200] BOOT
jp $f2de ;[f203] WBOOT
jp $f368 ;[f206] CONST
jp $f372 ;[f209] CONIN
jp $f3ab ;[f20c] CONOUT
jp $f3e6 ;[f20f] LIST
jp $f3c7 ;[f212] PUNCH
jp $f3d1 ;[f215] READER
jp $f474 ;[f218] HOME
jp $f47d ;[f21b] SELDSK
jp $f4b3 ;[f21e] SETTRK
jp $f4b9 ;[f221] SETSEC
jp $f4be ;[f224] SETDMA
jp $f4c8 ;[f227] READ
jp $f4df ;[f22a] WRITE
jp $f3da ;[f22d] LISTST
jp $f4c4 ;[f230] SECTRAN
; TODO
; This is some kind of control code used to know which CP/M BIOS is running
; (i.e. which machine revision hides under the operating system?)
DB "0037" ;[f233]
; CP/M Disc Parameter Header
; https://www.seasip.info/Cpm/dph.html
; In CP/M, the DPH is a BIOS structure containing information about a disk drive.
; The actual format of the DPH is version-dependent.
; TODO DPH for disk 0
DW 0 ;[f237] Address of sector translation table
DW 0, 0, 0 ; Used as workspace by CP/M
DW $fd7c ; Address of a 128-byte sector buffer
; this is the same for all DPHs in the system.
DW $f267 ; Address of the DPB
; giving the format of this drive.
DW $fe15 ; Address of the directory checksum vector
; for this drive.
DW $fdfc ; Address of the allocation vector
; for this drive.
; TODO DPH for disk 1
DW 0 ;[f247] Address of sector translation table
DW 0, 0, 0 ; Used as workspace by CP/M
DW $fd7c ; Address of a 128-byte sector buffer
; this is the same for all DPHs in the system.
DW $f284 ; Address of the DPB
; giving the format of this drive.
DW $fe4e ; Address of the directory checksum vector
; for this drive.
DW $fe35 ; Address of the allocation vector
; for this drive.
; TODO DPH for disk 2 (what is disk 2??)
DW 0 ;[f257] Address of sector translation table
DW 0, 0, 0 ; Used as workspace by CP/M
DW $fd7c ; Address of a 128-byte sector buffer
; this is the same for all DPHs in the system.
DW $f2ac ; Address of the DPB
; giving the format of this drive.
DW $fe94 ; Address of the directory checksum vector
; for this drive.
DW $fe6e ; Address of the allocation vector
; for this drive.
; TODO DPB for disk 0
DW 80 ;[f267] Number of 128-byte records per track
DB 5 ;[f269] Block shift. 3 => 1k, 4 => 2k, 5 => 4k
DB 0x1f ;[f26a] Block mask. 7 => 1k, 0Fh => 2k, 1Fh => 4k...
DB 03 ;[f26b] Extent mask
DW 196 ;[f26c] (no. of blocks on the disk)-1
DW 0x7f ;[f26e] (no. of directory entries)-1
DB 0x80 ;[f270] Directory allocation bitmap, first byte
DB 0 ;[f271] Directory allocation bitmap, second byte
DW 0x0020 ;[f272] Checksum vector size, 0 for a fixed disk
; No. directory entries/4, rounded up.
DB 1 ;[f274] Offset, number of reserved tracks
DB 0x00 ; TODO
DB 0x20
DB 0x50 ;[f277]
DB 0x07 ;[f278]
DB 0x03 ;[f279]
DB 0x00 ;[f27a]
DB 0x03 ;[f27b]
DB 0x05 ;[f27c]
DB 0x00 ;[f27d]
DB 0x00 ;[f27e]
DB 0x00 ;[f27f]
DB 0x02 ;[f280]
DB 0x04 ;[f281]
DB 1 ;[f282]
DB 3
; TODO DPB for disk 1
DW 80 ;[f284] Number of 128-byte records per track
DB 5 ;[f286] Block shift. 3 => 1k, 4 => 2k, 5 => 4k
DB 0x1f ;[f287] Block mask. 7 => 1k, 0Fh => 2k, 1Fh => 4k...
DB 03 ;[f288] Extent mask
DW 196 ;[f289] (no. of blocks on the disk)-1
DW 0x7f ;[f28b] (no. of directory entries)-1
DB 0x80 ;[f28d] Directory allocation bitmap, first byte
DB 0 ;[f28e] Directory allocation bitmap, second byte
DW 0x0020 ;[f28f] Checksum vector size, 0 for a fixed disk
; No. directory entries/4, rounded up.
DB 1 ;[f291] Offset, number of reserved tracks
DB 00 ;[f292] TODO
DB 0x20 ;[f293]
DB 0x50 ;[f294]
DB 0x07 ;[f295]
DB 0x03 ;[f296]
DB 0x01, 0x03, 0x05 ;[f297]
DB 0x00 ;[f29a]
DB 0x00 ;[f29b]
DB 0x00 ;[f29c]
DB 0x02 ;[f29d]
DB 0x04 ;[f29e]
DB 0x01, 0x03, 0x00 ;[f29f]
DB 0x00 ;[f2a2]
DB 0x00 ;[f2a3]
DB 0x00 ;[f2a4]
DB 0x00 ;[f2a5]
DB 0x00 ;[f2a6]
DB 0x00 ;[f2a7]
DB 0x00 ;[f2a8]
DB 0x00 ;[f2a9]
DB 0x00 ;[f2aa]
DB 0x00 ;[f2ab]
; TODO DPB for disk 2
DW 128 ;[f2ac] Number of 128-byte records per track
DB 5 ;[f2ae] Block shift. 3 => 1k, 4 => 2k, 5 => 4k
DB 0x1f ;[f2af] Block mask. 7 => 1k, 0Fh => 2k, 1Fh => 4k...
DB 00 ;[f2b0] Extent mask
DW 299 ;[f2b1] (no. of blocks on the disk)-1
DW 0x7f ;[f2b3] (no. of directory entries)-1
DB 0x80 ;[f2b5] Directory allocation bitmap, first byte
DB 0 ;[f2b6] Directory allocation bitmap, second byte
DW 0x0020 ;[f2b7] Checksum vector size, 0 for a fixed disk
; No. directory entries/4, rounded up.
DB 2 ;[f2b9] Offset, number of reserved tracks
DB 0 ;[f2ba] 00
DB 0x20
DB 0x80
DB 0x07 ;[f2bd]
DB 0x03 ;[f2be]
DB 0x82 ;[f2bf]
DB 0x03 ;[f2c0]
DB 0x08 ;[f2c1]
DB 0x00 ;[f2c2]
DB 0x00 ;[f2c3]
DB 0x00 ;[f2c4]
DB 0x03 ;[f2c5]
DB 0x06, 0x01 ;[f2c6]
DB 0x04 ;[f2c8]
DB 0x07 ;[f2c9]
DB 0x02 ;[f2ca]
DB 0x05 ;[f2cb]
DB 0x00 ;[f2cc]
DB 0x00 ;[f2cd]
DB 0x00 ;[f2ce]
DB 0x00 ;[f2cf]
DB 0x00 ;[f2d0]
DB 0x00 ;[f2d1]
DB 0x00 ;[f2d2]
DB 0x00 ;[f2d3]
DB 0x00 ;[f2d4]
DB 0x00 ;[f2d5]
DB 0x00 ;[f2d6]
DB 0x00 ;[f2d7]
DB 0x00 ;[f2d8]
DB 0x00 ;[f2d9]
DB 0x00 ;[f2da]
DB 0x00 ;[f2db]
DB 0x00 ;[f2dc]
DB 0x00 ;[f2dd]
; Warm boot - reload command processor
; Reloads the command processor and (on some systems) the BDOS as well.
; How it does this is implementation-dependent; it may use the reserved tracks
; of a floppy disk or extra memory.
wboot:
ld sp,$0100 ;[f2de] initialize stack pointer
ld hl,$e000 ;[f2e1] buffer area: $e000
ld d,$00 ;[f2e4] track = 0
ld e,$00 ;[f2e6] sector = 0, no burst
ld c,$04 ;[f2e8] head = 1
ld b,$40 ;[f2ea] read operation
ld a,$03 ;[f2ec] ssf = 3
call $f631 ;[f2ee]
cp $ff ;[f2f1]
jp z,$f34e ;[f2f3]
ld hl,$e000 ;[f2f6] Now, move the just loaded block
ld de,$dc00 ;[f2f9] in the proper area ($dc00)
ld bc,$0400 ;[f2fc]
ldir ;[f2ff]
ld hl,$e000 ;[f301] buffer area: $e000
ld d,$00 ;[f304] track = 0
ld e,$01 ;[f306] sector = 1
set 7,e ;[f308] sector burst enabled
ld c,$04 ;[f30a] head = 1
ld b,$43 ;[f30c] read operation, burst = 3
ld a,$03 ;[f30e] ssf = 3
call $f631 ;[f310] load CCP and part of BDOS
cp $ff ;[f313]
jr z,label_f34e ;[f315] if read fails, die
call bank_switch_on ;[f317]
jr label_f31c ;[f31a] TODO it's right there, why jump?
label_f31c:
di ;[f31c]
xor a ;[f31d] TODO zero some other memories
ld ($fd24),a ;[f31e]
ld ($fd26),a ;[f321]
ld bc,($0080) ;[f324] TODO load 0080 in $fd32
call $f4be ;[f328]
ld a,$c3 ;[f32b]
ld ($0000),a ;[f32d] load jp $f203 in $0000 (c3 03 f2)
ld hl,$f203 ;[f330]
ld ($0001),hl ;[f333]
ld ($0005),a ;[f336] load jp $e406 in $0005 (c3 06 e4)
ld hl,$e406 ;[f339] this is the BDOS entry point
ld ($0006),hl ;[f33c]
xor a ;[f33f]
ld hl,$0080 ;[f340]
ld ($fd32),hl ;[f343] TODO reload 0080 in $fd32, why?
ld a,($0004) ;[f346]
ld c,a ;[f349]
ei ;[f34a]
jp $dc00 ;[f34b] invoke CCP
label_f34e:
call bank_switch_off ;[f34e] must access to ROM
call $c01b ;[f351] call putstr
BYTE $0d
BYTE $0a
BYTE "BOOT ERROR"
BYTE $0d
BYTE $0a
BYTE $00
call bank_switch_on ;[f363]
jr label_f34e ;[f366] infinite loop
; Console status
; Returns its status in A; 0 if no character is ready, FF if one is.
const:
ld a,($fcf4) ;[f368] TODO fcf4 may be populated by the SIO ISR
or a ;[f36b]
ld a,$00 ;[f36c]
ret z ;[f36e]
ld a,$ff ;[f36f]
ret ;[f371]
; Console input
; Wait until the keyboard is ready to provide a character, and return it in A.
conin:
in a,($b1) ;[f372] check if data is available from serial port
and $01 ;[f374]
jr z,label_f37d ;[f376]
in a,($b0) ;[f378] if data is available, fetch and return it
and $7f ;[f37a] but make it a valid ASCII character
ret ;[f37c]
label_f37d:
call const ;[f37d] get console status
or a ;[f380]
jr z,conin ;[f381] busy wait until valid data
di ;[f383] begin interrupt guard
ld hl,$fcb2 ;[f384] base pointer of keyboard circular buffer (TODO)
ld a,($fcf3) ;[f387] read offset
ld e,a ;[f38a]
ld d,$00 ;[f38b]
add hl,de ;[f38d] compute read pointer
ld a,(hl) ;[f38e] get keyboard byte
push af ;[f38f]
ld a,$00 ;[f390] TODO zero something
ld ($fcf5),a ;[f392] TODO
ld ($fcf6),a ;[f395] TODO
ld a,($fcf3) ;[f398] update current read pointer
inc a ;[f39b] (increase by one and wrap at 63)
and $3f ;[f39c]
ld ($fcf3),a ;[f39e]
ld a,($fcf4) ;[f3a1] update number of bytes in buffer
dec a ;[f3a4] (decrease by one)
ld ($fcf4),a ;[f3a5]
ei ;[f3a8] end interrupt guard
pop af ;[f3a9]
ret ;[f3aa]
; Console output
; Write the character in C to the screen.
conout:
ld a,c ;[f3ab]
or a ;[f3ac]
ret z ;[f3ad] Null is not printable
ld a,($0003) ;[f3ae]
cp $83 ;[f3b1]
jp z,$f3e6 ;[f3b3] TODO
call $f6b0 ;[f3b6] TODO
call bank_switch_off ;[f3b9]
call $c009 ;[f3bc] BIOS ROM putchar
call bank_switch_on ;[f3bf]
ret ;[f3c2]
ld hl,($bff8) ;[f3c3] 2a f8 bf
jp (hl) ;[f3c6] e9
; Paper tape punch output
; Write the character in C to the "paper tape punch" - or whatever the current
; auxiliary device is. If the device isn't ready, wait until it is.
punch:
in a,($b1) ;[f3c7] Just write C on main serial
and $04 ;[f3c9] polling status register
jr z,punch ;[f3cb] to avoid overrun
ld a,c ;[f3cd]
out ($b0),a ;[f3ce]
ret ;[f3d0]
; Paper tape reader input
; Read a character from the "paper tape reader" - or whatever the current
; auxiliary device is. If the device isn't ready, wait until it is. The
; character will be returned in A. If this device isn't implemented,
; return character 26 (^Z).
reader:
in a,($b1) ;[f3d1] Just read from main serial
and $01 ;[f3d3] polling status register
jr z,reader ;[f3d5] (busy wait)
in a,($b0) ;[f3d7]
ret ;[f3d9]
; Status of list device
; Return status of current printer device.
; Returns A=0 (not ready) or A=FF (ready).
listst:
in a,($82) ;[f3da] TODO
bit 7,a ;[f3dc]
jr z,label_f3e2 ;[f3de]
xor a ;[f3e0]
ret ;[f3e1]
label_f3e2:
ld a,($00ff) ;[f3e2]
ret ;[f3e5]
; Printer output
; Write the character in C to the printer. If the printer isn't ready, wait
; until it is.
list:
ld hl,$0000 ;[f3e6] big TODO, which printer??
ld ($f471),hl ;[f3e9]
ld a,($0003) ;[f3ec]
cp $81 ;[f3ef]
call z,$f3ab ;[f3f1]
label_f3f4:
ld a,($0003) ;[f3f4]
rlc a ;[f3f7]
rlc a ;[f3f9]
and $03 ;[f3fb]
or a ;[f3fd]
jr z,label_f408 ;[f3fe]
dec a ;[f400]
jr z,label_f40b ;[f401]
dec a ;[f403]
jr z,label_f40c ;[f404]
jr label_f432 ;[f406]
label_f408:
jp $f3ab ;[f408]
label_f40b:
ret ;[f40b]
label_f40c:
in a,($82) ;[f40c]
bit 7,a ;[f40e]
jr z,label_f432 ;[f410]
ld hl,($f471) ;[f412]
dec hl ;[f415]
ld ($f471),hl ;[f416]
ld a,h ;[f419]
or l ;[f41a]
jr nz,label_f3f4 ;[f41b]
xor a ;[f41d]
out ($da),a ;[f41e]
call $f368 ;[f420]
or a ;[f423]
jr z,label_f3f4 ;[f424]
call $f372 ;[f426]
cp $1a ;[f429]
jr nz,label_f3f4 ;[f42b]
xor a ;[f42d]
ld ($0003),a ;[f42e]
ret ;[f431]
label_f432:
ld a,($f473) ;[f432]
or a ;[f435]
jr nz,label_f44e ;[f436]
call $f6b0 ;[f438]
label_f43b:
call bank_switch_off ;[f43b]
call $f3c3 ;[f43e]
call bank_switch_on ;[f441]
jr z,label_f44e ;[f444]
push bc ;[f446]
ld c,a ;[f447]
call $f44e ;[f448]
pop bc ;[f44b]
jr label_f43b ;[f44c]
label_f44e:
ld a,($0003) ;[f44e]
and $c0 ;[f451]
cp $c0 ;[f453]
jp z,$f3c7 ;[f455]
ld a,c ;[f458]
out ($80),a ;[f459]
out ($d0),a ;[f45b]
out ($d2),a ;[f45d]
ret ;[f45f]
ld a,$01 ;[f460]
ld ($f473),a ;[f462]
push hl ;[f465]
push de ;[f466]
call $f3e6 ;[f467]
pop de ;[f46a]
pop hl ;[f46b]
xor a ;[f46c]
ld ($f473),a ;[f46d]
ret ;[f470]
nop ;[f471]
nop ;[f472]
nop ;[f473]
; Move disk head to track 0
; Move the current drive to track 0.
home:
ld a,($fd25) ;[f474] TODO
or a ;[f477]
ret nz ;[f478]
ld ($fd24),a ;[f479]
ret ;[f47c]
; Select the disk drive in register C (0=A:, 1=B: ...). Called with E=0 or 0FFFFh.
; If bit 0 of E is 0, then the disk is logged in as if new; if the format has
; to be determined from the boot sector, for example, this will be done.
; If bit 0 if E is 1, then the disk has been logged in before. The disk is not
; accessed; the DPH address (or zero) is returned immediately.
; SELDSK returns the address of a Disc Parameter Header in HL. The exact format
; of a DPH varies between CP/M versions; note that under CP/M 3, the DPH is in
; memory bank 0 and probably not visible to programs. If the disk could not be
; selected it returns HL=0.
seldsk:
ld hl,$fd25 ;[f47d] TODO
xor a ;[f480]
cp (hl) ;[f481]
ld (hl),a ;[f482]
call nz,$f4ac ;[f483]
ld hl,$0000 ;[f486]
ld a,c ;[f489]
ld ($fd1b),a ;[f48a]
cp $04 ;[f48d]
ret nc ;[f48f]
ld l,a ;[f490]
ld h,$00 ;[f491]
add hl,hl ;[f493]
add hl,hl ;[f494]
add hl,hl ;[f495]
add hl,hl ;[f496]
ld de,$f237 ;[f497]
add hl,de ;[f49a]
push hl ;[f49b]
ld de,$000a ;[f49c]
add hl,de ;[f49f]
ld e,(hl) ;[f4a0]
inc hl ;[f4a1]
ld d,(hl) ;[f4a2]
ld hl,$000f ;[f4a3]
add hl,de ;[f4a6]
ld ($fd34),hl ;[f4a7]
pop hl ;[f4aa]
ret ;[f4ab]
call $f6b0 ;[f4ac]
call $f625 ;[f4af]
ret ;[f4b2]
; Set track number
; Set the track in BC - 0 based.
settrk:
ld h,b ;[f4b3]
ld l,c ;[f4b4]
ld ($fd1c),hl ;[f4b5]
ret ;[f4b8]
; Set sector number
; Set the sector in BC. Under CP/M 1 and 2 a sector is 128 bytes.
setsec:
ld a,c ;[f4b9]
ld ($fd1e),a ;[f4ba]
ret ;[f4bd]
; Set DMA address
; The next disk operation will read its data from (or write its data to) the
; address given in BC.
setdma:
ld h,b ;[f4be]
ld l,c ;[f4bf]
ld ($fd32),hl ;[f4c0]
ret ;[f4c3]
; Sector translation for skewing
; Translate sector numbers to take account of skewing.
; On entry, BC=logical sector number (zero based) and DE=address of translation
; table. On exit, HL contains physical sector number. On a system with hardware
; skewing, this would normally ignore DE and return either BC or BC+1.
sectran:
ld l,c ;[f4c4]
ld h,$00 ;[f4c5] Just return bc, with MSB zeroed
ret ;[f4c7]
; Read a sector
; Read the currently set track and sector at the current DMA address.
; Returns A=0 for OK, 1 for unrecoverable error, FF if media changed.
read:
call $f6b0 ;[f4c8]
xor a ;[f4cb]
ld ($fd26),a ;[f4cc]
ld a,$01 ;[f4cf]
ld ($fd30),a ;[f4d1]
ld ($fd2c),a ;[f4d4]
ld a,$02 ;[f4d7]
ld ($fd31),a ;[f4d9]
jp $f557 ;[f4dc]
; Write a sector
; Write the currently set track and sector. C contains a deblocking code:
; C=0 - Write can be deferred
; C=1 - Write must be immediate
; C=2 - Write can be deferred, no pre-read is necessary.
; Returns A=0 for OK, 1 for unrecoverable error, 2 if disk is readonly, FF if
; media changed.
write:
call $f6b0 ;[f4df]
xor a ;[f4e2]
ld ($fd30),a ;[f4e3]
ld a,c ;[f4e6]
ld ($fd31),a ;[f4e7]
cp $02 ;[f4ea]
jp nz,$f50b ;[f4ec]
ld ix,($fd34) ;[f4ef]
ld a,(ix+$00) ;[f4f3]
ld ($fd26),a ;[f4f6]
ld a,($fd1b) ;[f4f9]
ld ($fd27),a ;[f4fc]
ld hl,($fd1c) ;[f4ff]
ld ($fd28),hl ;[f502]
ld a,($fd1e) ;[f505]
ld ($fd2a),a ;[f508]
ld a,($fd26) ;[f50b]
or a ;[f50e]
jp z,$f54f ;[f50f]
dec a ;[f512]
ld ($fd26),a ;[f513]
ld a,($fd1b) ;[f516]
ld hl,$fd27 ;[f519]
cp (hl) ;[f51c]
jp nz,$f54f ;[f51d]
ld hl,$fd28 ;[f520]
call $f60d ;[f523]
jp nz,$f54f ;[f526]
ld a,($fd1e) ;[f529]
ld hl,$fd2a ;[f52c]
cp (hl) ;[f52f]
jp nz,$f54f ;[f530]
inc (hl) ;[f533]
ld a,(hl) ;[f534]
ld ix,($fd34) ;[f535]
cp (ix+$01) ;[f539]
jp c,$f548 ;[f53c]
ld (hl),$00 ;[f53f]
ld hl,($fd28) ;[f541]
inc hl ;[f544]
ld ($fd28),hl ;[f545]
xor a ;[f548]
ld ($fd2c),a ;[f549]
jp $f557 ;[f54c]
xor a ;[f54f]
ld ($fd26),a ;[f550]
inc a ;[f553]
ld ($fd2c),a ;[f554]
xor a ;[f557]
ld ($fd2b),a ;[f558]
push bc ;[f55b]
ld ix,($fd34) ;[f55c]
ld a,(ix+$03) ;[f560]
and $03 ;[f563]
ld c,a ;[f565]
inc c ;[f566]
ld a,($fd1e) ;[f567]
dec c ;[f56a]
jp z,$f573 ;[f56b]
or a ;[f56e]
rra ;[f56f]
jp $f56a ;[f570]
pop bc ;[f573]
ld ($fd23),a ;[f574]
ld hl,$fd24 ;[f577]
ld a,(hl) ;[f57a]
ld (hl),$01 ;[f57b]
or a ;[f57d]
jp z,$f5a5 ;[f57e]
ld a,($fd1b) ;[f581]
ld hl,$fd1f ;[f584]
cp (hl) ;[f587]
jp nz,$f59e ;[f588]
ld hl,$fd20 ;[f58b]
call $f60d ;[f58e]
jp nz,$f59e ;[f591]
ld a,($fd23) ;[f594]
ld hl,$fd22 ;[f597]
cp (hl) ;[f59a]
jp z,$f5c2 ;[f59b]
ld a,($fd25) ;[f59e]
or a ;[f5a1]
call nz,$f625 ;[f5a2]
ld a,($fd1b) ;[f5a5]
ld ($fd1f),a ;[f5a8]
ld hl,($fd1c) ;[f5ab]
ld ($fd20),hl ;[f5ae]
ld a,($fd23) ;[f5b1]
ld ($fd22),a ;[f5b4]
ld a,($fd2c) ;[f5b7]
or a ;[f5ba]
call nz,$f619 ;[f5bb]
xor a ;[f5be]
ld ($fd25),a ;[f5bf]
ld a,($fd1e) ;[f5c2]
ld ix,($fd34) ;[f5c5]
and (ix+$02) ;[f5c9]
ld l,a ;[f5cc]
ld h,$00 ;[f5cd]
add hl,hl ;[f5cf]
add hl,hl ;[f5d0]
add hl,hl ;[f5d1]
add hl,hl ;[f5d2]
add hl,hl ;[f5d3]
add hl,hl ;[f5d4]
add hl,hl ;[f5d5]
ld de,$f8ae ;[f5d6]
add hl,de ;[f5d9]
ex de,hl ;[f5da]
ld hl,($fd32) ;[f5db]
ld a,h ;[f5de]
jp $f5ee ;[f5df]
ld a,($fd30) ;[f5e2]
or a ;[f5e5]
ret nz ;[f5e6]
ld a,$01 ;[f5e7]
ld ($fd25),a ;[f5e9]
ex de,hl ;[f5ec]
ret ;[f5ed]
ld bc,$0080 ;[f5ee]
call $f5e2 ;[f5f1]
ex de,hl ;[f5f4]
ldir ;[f5f5]
ld a,($fd31) ;[f5f7]
cp $01 ;[f5fa]
ld a,($fd2b) ;[f5fc]
ret nz ;[f5ff]
or a ;[f600]
ret nz ;[f601]
xor a ;[f602]
ld ($fd25),a ;[f603]
call $f625 ;[f606]
ld a,($fd2b) ;[f609]
ret ;[f60c]
ex de,hl ;[f60d]
ld hl,$fd1c ;[f60e]
ld a,(de) ;[f611]
cp (hl) ;[f612]
ret nz ;[f613]
inc de ;[f614]
inc hl ;[f615]
ld a,(de) ;[f616]
cp (hl) ;[f617]
ret ;[f618]
call $f64a ;[f619]
set 6,b ;[f61c]
call $f631 ;[f61e]
ld ($fd2b),a ;[f621]
ret ;[f624]
call $f64a ;[f625]
set 7,b ;[f628]
call $f631 ;[f62a]
ld ($fd2b),a ;[f62d]
ret ;[f630]
call bank_switch_off ;[f631]
ld ($fd2e),a ;[f634]
call $f692 ;[f637]
call $b818 ;[f63a]
or a ;[f63d]
jr z,label_f646 ;[f63e]
ld a,($fd2e) ;[f640]
call $b818 ;[f643]
label_f646:
call bank_switch_on ;[f646]
ret ;[f649]
ld ix,($fd34) ;[f64a]
push ix ;[f64e]
ld hl,$fd1f ;[f650]
ld a,(ix+$04) ;[f653]
and $80 ;[f656]
or (hl) ;[f658]
ld c,a ;[f659]
inc hl ;[f65a]
ld d,(hl) ;[f65b]
ld a,d ;[f65c]
cp $00 ;[f65d]
jr z,label_f66b ;[f65f]
cp (ix+$07) ;[f661]
jr nc,label_f66b ;[f664]
ld a,(ix+$08) ;[f666]
sub d ;[f669]
ld d,a ;[f66a]
label_f66b:
inc hl ;[f66b]
inc hl ;[f66c]
ld a,(hl) ;[f66d]
cp (ix+$06) ;[f66e]
jr c,label_f67c ;[f671]
sub (ix+$06) ;[f673]
push af ;[f676]
ld a,c ;[f677]
or $04 ;[f678]
ld c,a ;[f67a]
pop af ;[f67b]
label_f67c:
push bc ;[f67c]
ld c,a ;[f67d]
ld b,$00 ;[f67e]
add ix,bc ;[f680]
ld e,(ix+$09) ;[f682]
pop bc ;[f685]
ld hl,$f8ae ;[f686]
pop ix ;[f689]
ld b,(ix+$05) ;[f68b]
ld a,(ix+$03) ;[f68e]
ret ;[f691]
push af ;[f692]
push bc ;[f693]
ld a,c ;[f694]
bit 7,a ;[f695]
jr nz,label_f6a4 ;[f697]
call $f6f9 ;[f699]
in a,($82) ;[f69c]
set 5,a ;[f69e]
out ($82),a ;[f6a0]
jr label_f6ad ;[f6a2]
label_f6a4:
call $f6f2 ;[f6a4]
in a,($82) ;[f6a7]
res 5,a ;[f6a9]
out ($82),a ;[f6ab]
label_f6ad:
pop bc ;[f6ad]
pop af ;[f6ae]
ret ;[f6af]
; Set up a dedicated stack - prologue
; When this routine is called, sp is moved to $fd7c and all registers are saved
; there.
; Old stack pointer and hl values are saved in reserved locations for later
; restore.
; Onto the new stack, two addresses are pushed, in following order:
; - epilogue function ($f6d0)
; - return address to caller
; This way, the return from f6b0 is correctly done to caller. And when a
; "same level" ret is executed from the caller, the epilogue is called instead
ld ($fd36),hl ;[f6b0] save hl content
ex (sp),hl ;[f6b3] fetch value from stack in hl
ld ($fd38),hl ;[f6b4] save the return address to caller
ld ($fd3a),sp ;[f6b7] save stack pointer
ld sp,$fd7c ;[f6bb] change stack pointer value
push de ;[f6be] push there the registers
push bc ;[f6bf]
push ix ;[f6c0]
push iy ;[f6c2]
ld hl,$f6d0 ;[f6c4] push epilogue address for
push hl ;[f6c7] later execution
ld hl,($fd38) ;[f6c8] push return address to
push hl ;[f6cb] caller
ld hl,($fd36) ;[f6cc] restore hl content
ret ;[f6cf]
; Set up a dedicated stack - epilogue
; Restore the originary stack, saved in the prologue. Then return
; to ordinary code execution.
pop iy ;[f6d0] Restore the registers
pop ix ;[f6d2]
pop bc ;[f6d4]
pop de ;[f6d5]
ld sp,($fd3a) ;[f6d6] move back stack pointer
pop hl ;[f6da] restore original value of hl (saved in stack with ex)
ret ;[f6db]
bank_switch_off:
di ;[f6dc]
push af ;[f6dd]
in a,($81) ;[f6de]
res 0,a ;[f6e0]
out ($81),a ;[f6e2]
pop af ;[f6e4]
ei ;[f6e5]
ret ;[f6e6]
bank_switch_on:
push af ;[f6e7]
di ;[f6e8]
in a,($81) ;[f6e9]
set 0,a ;[f6eb]
out ($81),a ;[f6ed]
ei ;[f6ef]
pop af ;[f6f0]
ret ;[f6f1]
push af ;[f6f2]
push hl ;[f6f3]
ld hl,$f717 ;[f6f4]
jr label_f6ff ;[f6f7]
ret ;[f6f9]
push af ;[f6fa]
push hl ;[f6fb]
ld hl,$f71b ;[f6fc]
label_f6ff:
ld a,(hl) ;[f6ff]
or a ;[f700]
jr z,label_f709 ;[f701]
call $f70c ;[f703]
inc hl ;[f706]
jr label_f6ff ;[f707]
label_f709:
pop hl ;[f709]
pop af ;[f70a]
ret ;[f70b]
out ($c1),a ;[f70c]
label_f70e:
in a,($c0) ;[f70e]
rla ;[f710]
jr nc,label_f70e ;[f711]
rla ;[f713]
jr c,label_f70e ;[f714]
ret ;[f716]
inc bc ;[f717] 03
rst $18 ;[f718] df
ld ($0300),a ;[f719] 32 00 03
rst $28 ;[f71c] ef
jr nz,label_f71f ;[f71d] 20 00
label_f71f:
ld ($fcf9),sp ;[f71f] ed 73 f9 fc
ld sp,$fd1b ;[f723] 31 1b fd
push af ;[f726] f5
push bc ;[f727] c5
push de ;[f728] d5
push hl ;[f729] e5
in a,($81) ;[f72a] db 81
push af ;[f72c] f5
res 0,a ;[f72d] cb 87
out ($81),a ;[f72f] d3 81
in a,($82) ;[f731] db 82
bit 0,a ;[f733] cb 47
jr z,label_f750 ;[f735] 28 19
call $c01b ;[f737] cd 1b c0
BYTE $0d
BYTE $0a
BYTE "* PARITY ERROR *"
BYTE $07
BYTE $00
label_f74e:
jr label_f74e ;[f74e]
label_f750: ; ISR epilogue
out ($d8),a ;[f750] TODO
pop af ;[f752]
out ($81),a ;[f753]
pop hl ;[f755]
pop de ;[f756]
pop bc ;[f757]
pop af ;[f758]
ld sp,($fcf9) ;[f759]
ei ;[f75d] classic return from interrupt
reti ;[f75e]
; keyboard interrupt routine
ld ($fcf9),sp ;[f760] prologue
ld sp,$fd1b ;[f764] dedicated stack for ISR
push af ;[f767]
push bc ;[f768]
push de ;[f769]
push hl ;[f76a]
in a,($81) ;[f76b] save bank switching status
push af ;[f76d]
res 0,a ;[f76e] reset memory bank switching
out ($81),a ;[f770]
in a,($b3) ;[f772] read from SIO port 2 status
bit 0,a ;[f774] (data available)
label_f776:
jp z,$f750 ;[f776] no data, just ISR epilogue
call $f77f ;[f779] get byte and put in software buffer (TODO)
jp $f750 ;[f77c] ISR eilogue
ld hl,$fcf7 ;[f77f] temporary location for read byte (TODO)
call $f88a ;[f782] read from SIO, flags byte is returned
ret z ;[f785] flags byte can't be zero, no data to process in this case
ld b,a ;[f786] put the flags in b
dec hl ;[f787]
ld a,(hl) ;[f788]
ld c,a ;[f789] put the data in c
bit 2,b ;[f78a] ALT
jr z,label_f797 ;[f78c]
bit 3,b ;[f78e] CTRL
jr z,label_f797 ;[f790]
cp $4d ;[f792] BOOT
jp z,$f8a5 ;[f794] CTRL + ALT + BOOT (TODO)
; any keypress, except for CTRL + ALT + BOOT
label_f797:
cp $5c ;[f797] F15
jr nz,label_f7af ;[f799] F15 key (TODO)
bit 3,b ;[f79b] CTRL + F15
jr z,label_f7af ;[f79d]
; CTRL + F15
ld a,b ;[f79f] TODO
rrca ;[f7a0]
rrca ;[f7a1] put C and S flags in MSb
and $c0 ;[f7a2] and mask them
ld e,a ;[f7a4]
ld a,($0003) ;[f7a5] TODO in 0003 are kept keyboard status bits
and $3f ;[f7a8] clear C and S old flags
or e ;[f7aa] update C and S flags
ld ($0003),a ;[f7ab]
ret ;[f7ae]
; any keypress, except CTRL + ALT + BOOT and CTRL + F15
label_f7af:
call $f7b3 ;[f7af]
ret ;[f7b2]
ld a,($fcf4) ;[f7b3]
cp $36 ;[f7b6]
jr c,label_f7bf ;[f7b8] no more than 54 bytes are
ld a,$01 ;[f7ba] accepted in circular buffer.
out ($da),a ;[f7bc] If limit reached, beep and
ret ;[f7be] discard data.
; TODO keymap conversion and push in buffer?
label_f7bf:
ld hl,$f826 ;[f7bf] TODO keymap table?
ld a,c ;[f7c2] take the data byte
cp (hl) ;[f7c3]
jr z,label_f7cd ;[f7c4]
call $f80c ;[f7c6] TODO, return in carry
jr nc,label_f7fa ;[f7c9] no match
jr label_f7da ;[f7cb] TODO match
; TODO, jump here if data == 0x4b (double zero?)
; "Double zero" key macro
label_f7cd:
ld a,($fcb1) ;[f7cd] TODO e = number of zeroes the
ld e,a ;[f7d0] "double zero" key must insert
label_f7d1:
ld c,$30 ;[f7d1]
call $f85c ;[f7d3] put "0" in keyboard buffer
dec e ;[f7d6]
jr nz,label_f7d1 ;[f7d7] while e > 0
ret ;[f7d9]
; TODO $f80c has returned carry=1