/
val.mac
3301 lines (3036 loc) · 79.1 KB
/
val.mac
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
.z80
name('val')
TITLE MSX-DOS 2 KERNEL Copyright (1986) IS Systems Ltd.
SUBTTL Validate Disk and Interface to Disk Drivers
;******************************************************************************
;
INCLUDE ../macros.inc
INCLUDE ../const.inc
RAMMOD
MAXCLUS_FAT12 equ 4084
MAXCLUS_12BITS equ 4095
;
;-----------------------------------------------------------------------------
;
PROC DSK_CHOICE
;
; This routine calls the "choice" routine of the disk driver defined by the
; unit descriptor in HL. It does a special test for a pointer returned value
; of 0FFFFh since this indicates that the drive does not support formatting
; and so an error is returned.
;
; Entry: HL = Address of unit descriptor
; Returns: A = Error code (flags not set)
; DE = Address of choice string for this unit
; Corrupts: AF,BC,DE
;
;
ld a,CH_CMD ;Do the choice command and
call CALL_UNIT ; give up if any error
ret nz ; (shouldn't be!)
;
ld a,e ;Null pointer is OK.
or d
ret z
;
push hl
ex (sp),ix
ex de,hl ;HL := address of string
ld a,(ix+UD_SLOT##) ; A := slot address of driver
call RDSLT##
ex de,hl
ex (sp),ix
pop hl ;If returned string is
or a ; null then return an
ld a,.IFORM## ; error.
ret z
xor a
ret
;
;
;-----------------------------------------------------------------------------
;
PROC DSK_FORMAT
;
; This routine calls the "format" routine of the disk driver defined by
; the unit descriptor in HL. It shuffles the parameters around a bit and
; calls the disk driver to do the actual format. If this is successful then
; it reads the boot sector in and puts on a new boot routine (currently null)
; and also puts on a random volume id. This new boot sector is then written
; back out to the disk.
;
; If the choice parameter is FFh then this function does a "new boot
; sector" operation. This determines the disk format from the FATid byte and
; generates a new boot sector containing the correct parameter block and
; volume-id. This can be useful for modifying disks which were formatted
; under MSX-DOS 1 and is used by the "FIXDISK" command.
;
; The choice FEh is the same as FFh, but only the disk parameters are updated
; correctly and the volume id does not overwrite the boot program.
;
; The choice FDh will assume that disk parameters are correct, and will
; put a manufacturer name "MSXDOS2 " at the beginning of the boot sector,
; plus a extended block (29h, plus a volume name "NO NAME", plus a "FAT12" or
; "FAT16" mark, plus a volume id) after the disk parameters, to make up
; a standard boot disk. If there is already an extended block, only the
; manufacturer name will be set.
;
; The choice FCh is the same as FCh, except that if the disk is FAT12,
; a MSX-DOS 2.20 style boot sector (with the "VOL_ID" string and DOS1 boot code)
; will be built.
;
; The choice FBh will do a "quick format", that is, it will clear the FAT
; and root directory, thus effectively making the disk as if it were
; just formatted.
;
; Disk errors will be reported as a return error code. DISK_ERR
; will not be invoked.
;
; Entry: A = Choice number (1...9, or FCh..FFh)
; D = Segment number for buffer in page-2
; IX -> Buffer in page-2
; BC = Size of buffer in page-2
; HL = Address of unit descriptor
; Returns: A = Error code (flags not set)
; Corrupts: AF,BC,DE,IX,AF'
;
;
cp 0FEh
jr nc,new_boot_sec
cp 0FBh
jp nc,new_choices
;
ld e,c
ld c,d ; C := page-2 segment
ld d,b ;DE := buffer size
ld b,a ; B := choice number
ld a,FMT_CMD
call CALL_UNIT ;Do the format command
ret nz ; and give up if error.
inc a ;At the moment
;
;
new_boot_sec: push af
ld ix,UPB_BUF##
ld d,a ; Try to read the boot sector
ld e,a ; from the disk and if there
ld bc,(DATA_SEG##) ; was any error then give up
ld b,1 ; immediately.
call READ_UNIT
pop bc
ret nz
;
push bc ;Save choice again.
call FIND_UPB
ld de,1 ;Try to read the first FAT
jr nz,upb_no_valid
ld e,(ix+UPB_RESERVED_SECTORS##)
ld d,(ix+UPB_RESERVED_SECTORS##+1)
upb_no_valid:
ld bc,(DATA_SEG##) ; sector and if there was
ld b,1 ; any error then give up
; immediately.
call READ_UNIT
pop bc ;Get back choice value
ret nz
;
ld a,(ix+1) ;Error if second and third
and (ix+2) ; bytes of the FAT are not
inc a ; both FFh.
jr nz,not_dos1_disk
ld a,(ix) ;Also error if FATid byte
cp 0F8h ; is not F8h...FFh.
jr c,not_dos1_disk
;
ld c,a ;C := FATid byte
push bc
ld de,0 ;Try to read the boot sector
ld bc,(DATA_SEG##) ; from the disk and if there
ld b,1 ; was any error then give up
; immediately.
call READ_UNIT
pop bc ;Restore choice and FATid byte.
ret nz
;
push bc
ld a,c
;
call MAKE_UPB ;Create the disk parameter
; block in the boot sector.
pop bc
ret nz ;Give up if there was an error.
;
bit 0,b ;If choice code is "upb only"
jr z,put_boot_sec ; skip to write out boot.
;
got_boot_sec: push hl
xor a
call SET_BOOT_EX
call SET_RANDOM_VOLID ; Write new volume id even if there is already one,
; for compatibility with previous DOS version
ld hl,UPB_BUF##+NEW_BOOT_SZ+1Eh
ld de,512-NEW_BOOT_SZ-1Eh ;UPB_BOOT_CODE##+NEW_BOOT_SZ-256
ld b,0
zero_old_upb: ld (hl),b ;Zero the rest of the boot
inc hl ; sector to keep things
dec de ; neat and tidy.
ld a,d
or e
jr nz,zero_old_upb
already_got_volid:
pop hl
;
put_boot_sec: ld ix,UPB_BUF##
ld de,0 ;Now try to write this new
ld bc,(DATA_SEG##) ; boot sector out to the
ld b,1 ; disk and return with
; any error which may have
ld a,WR_CMD ; been generated.
call CALL_UNIT
ret
;
;
not_dos1_disk: ld a,.NDOS## ;Error if was no valid
ret ; FATid byte.
;
;
NEW_BOOT_NAME: db 0EBh,0FEh,090h ;Dummy 8086 jump instruction
db "NEXTOR20"
;
; ------------------------------------
;Jump here if choice is FBh, FCh or FDh
;(make a standard or a DOS 2.20 boot sector, or quick format)
new_choices: push af
call BUILR_UPB
jr nz,std_boot_err
call NEW_UPB
pop af
cp 0FBh
jr z,do_quick_format
cp 0FDh
jr z,do_boot_sec
;Choice is FCh:
;Check if drive is FAT12, if so put DOS 2.20 boot sector,
;otherwise put standard boot sector
push hl
pop ix
bit UF_F12,(ix+UD_FLAGS##)
ld a,1
jr z,do_boot_sec
xor a
do_boot_sec: push hl
call SET_BOOT_EX
call nz,SET_RANDOM_VOLID
pop hl
;Write the boot sector back to disk.
;Note that we do NOT clear the rest of the boot sector,
;since there may be a partition table here.
jr put_boot_sec
std_boot_err: pop bc ;Throw away pushed AF (choice)
ret
; ------------------------------------
;Quick format (choice FBh).
;Note that at this point, HL points that unit descriptor
;and we know already that the disk is valid.
do_quick_format:
push hl
pop ix
ld hl,UPB_BUF## ;Clear sector buffer only once,
ld de,UPB_BUF##+1 ;as we will be writing
ld (hl),0 ;mostly zeros to disk.
ld bc,512-1
ldir
ld e,(ix+UD_RESERVED_SECTORS##)
ld d,(ix+UD_RESERVED_SECTORS##+1) ;DE = First FAT sector
xor a ;Assume we will work exclusively
ld (DIO_EX_SECTOR##),a ; on the first 32MB of the disk.
ld b,(ix+UD_NUMBER_OF_FATS##) ;B = Number of FATs
;--- Loop for all FATs
qf_fats_loop: push bc
;* Clear the first FAT sector
ld hl,UPB_BUF##
ld a,(ix+UD_MEDIA_DESC_BYTE##)
ld (hl),a
inc hl
ld (hl),0FFh
inc hl
ld (hl),0FFh
bit UF_F16,(ix+UD_FLAGS##)
jr z,qf_is_notfat16
inc hl
ld (hl),0FFh
qf_is_notfat16:
ld b,1
call qf_write_sec
jr nz,qf_error1
ld hl,0
ld (UPB_BUF##),hl ;Clear again the sector buffer
ld (UPB_BUF##+2),hl
;* Clear other FAT sectors, if any
ld a,(ix+UD_SECTORS_PER_FAT##)
dec a
jr z,qf_okfat
ld b,a
call qf_write_sec
jr nz,qf_error1
;* Go for next FAT if any
qf_okfat: pop bc
djnz qf_fats_loop
;--- Now clear the root directory
ld e,(ix+UD_FIRST_ROOT_DIR_SECTOR##) ;DE = first directory sector
ld d,(ix+UD_FIRST_ROOT_DIR_SECTOR##+1)
ld b,(ix+UD_WHOLE_DIR_SECTORS##) ;B = whole sectors for root directory
ld a,(ix+UD_ODD_DIR_ENTRIES##)
or a
jr z,qf_no_ode
inc b
qf_no_ode:
jr qf_write_sec
qf_error1: pop bc
ret
;This routine writes the sector data from UPB_BUF
;repeatedly on consecutive sectors.
;Input: IX = Unit descriptor
; DE = First sector number
; B = Number of sectors to write
;Output: A = Error code (Z set appropriately)
; DE = DE + B (except on error)
;Preserves IX
qf_write_sec: push ix
push ix
pop hl
ld ix,UPB_BUF##
ld a,(DATA_SEG##)
ld c,a
qf_wrsec_loop:
push de
push bc
ld b,1
call WRITE_UNIT
pop bc
pop de
inc de
or a
jr nz,qf_wrsec_end
djnz qf_wrsec_loop ;Sets Z flag when B=0
qf_wrsec_end:
pop ix
ret
; ------------------------------------
;This routine generates the extended block or the DOS 2.20 specific boot sector data.
;Input: A=0 for DOS 2.20 boot sector, A<>0 for standard boot sector.
; HL=Address of unit descriptor (used for standard boot sector only)
;Output: Z if the disk had already the data of the type requested, NZ otherwise
;Corrupts: AF, HL, DE, BC, IX
SET_BOOT_EX:
;Put manufacturer name
push hl
ld hl,NEW_BOOT_NAME
ld de,UPB_BUF##
ld bc,11
ldir
pop hl
ld ix,UPB_BUF##
or a
jr nz,set_boot_ex_std
;DOS 2 boot sector
push hl
call GET_VOLID
ld a,b
dec a
ld hl,UPB_BUF##+UPB_DIRTY_FLAG##
jr z,set_boot_ex_end
ld hl,NEW_BOOT
ld de,UPB_BUF##+UPB_BOOT_CODE##
ld bc,NEW_BOOT_SZ
ldir
pop hl
ld a,1
or a
ret
;Standard boot sector
set_boot_ex_std:
push hl
call GET_VOLID
ld a,b
cp 2
ld hl,UPB_BUF##+UPB_RESERVED##
jr z,set_boot_ex_end
ld hl,NEW_BOOT_STD
ld de,UPB_BUF##+UPB_PHYSICAL_DRIVE##
ld bc,NEW_STDBOOT_SZ
ldir
ld ix,UPB_BUF## ;Set big sector count to zero
ld a,(ix+UPB_TOTAL_SECTORS##) ;if small sector count is not zero
or (ix+UPB_TOTAL_SECTORS##+1)
jr z,set_boot_ex_bigs
ld (ix+UPB_BIG_TOTAL_SECTORS##),0
ld (ix+UPB_BIG_TOTAL_SECTORS##+1),0
ld (ix+UPB_BIG_TOTAL_SECTORS##+2),0
ld (ix+UPB_BIG_TOTAL_SECTORS##+3),0
set_boot_ex_bigs:
ld (ix+UPB_BOOT_CODE##),0 ;Clear old DOS1 jump, if any
ld (ix+UPB_BOOT_CODE##+1),0
pop hl
ld bc,UD_FLAGS##
add hl,bc
bit UF_F16,(hl)
ld a,"2"
jr nz,fatmark_ok
ld (UPB_BUF##+UPB_FILESYSTEM##+4),a ;Set "FAT12" mark if necessary
fatmark_ok:
or a
ret
set_boot_ex_end:
ld (hl),0 ;Clear dirty disk flag
pop hl
ret
;This routine generates a random volume ID in the boot sector at UPB_BUF.
;Corrupts AF, BC, HL
SET_RANDOM_VOLID:
ld hl,(RANDOM##)
ld bc,(RANDOM##+2-1) ;Get the current 23-bit random
; number value and shift
; the top bit of each byte
xor a
srl l ; into A to make up a 4th
rla ; byte. This ensures that
srl h ; all 23 bits are used and
rla ; that each byte is 00h...7Fh.
srl b
rla
ld c,a
ld (UPB_BUF##+UPB_RANDOM_ID##),hl ;Store this as the volume-id
ld (UPB_BUF##+UPB_RANDOM_ID##+2),bc ; in the boot sector.
ret
; ------------------------------------
;* Standard boot sector after disk parameters and big sector count
NEW_BOOT_STD:
db 0 ;Physical drive num
db 0 ;Reserved (used as dirty disk flag)
db 29h ;Extended block signature
db 0FFh,0FFh,0FFh,0FFh ;Volume id
db "NO NAME " ;Volume name
db "FAT16 " ;FAT12 or FAT16 mark
NEW_STDBOOT_SZ equ $-NEW_BOOT_STD
;* DOS 2.20 boot sector after disk parameters
NEW_BOOT:
if PHASED
.dephase
endif
.phase 0C01Eh
;
jr BOOT_BODY ;Skip over VOL_ID's etc.
;
defb "VOL_ID" ;Validity checking string
defb 0,0FFh,0FFh,0FFh,0FFh ;Dirty flag and vol_id
defs 5,0 ;Reserved for future expansion
;
BOOT_BODY: ret nc ;If not called to invoke
; MSXDOS, just return.
ld (DOSON+1),de
ld (NOTFIRST+1),a
ld de,BOOTERRVECT
ld (hl),e
inc hl
ld (hl),d
;
retry_boot: ld sp,KBUF##+100h
ld de,SYSFCB
ld c,_FOPEN##
call BDOS##
inc a
jr z,NOFILE ;'MSXDOS.SYS' does not exist
;
ld de,100h
ld c,_SETDTA##
call BDOS##
ld hl,1 ;Set record size to 1
ld (SYSFCB+14),hl
ld hl,3F00h ;Read all into 100h..3FFFh
ld de,SYSFCB
ld c,_RDBLK##
call BDOS##
jp 100h ;Jump to MSXDOS.SYS
;
BOOTERRVECT: defw BOOTERR
;
BOOTERR:
DOSON: call ??????
ld a,c
and 0FEh ;Drive not ready is OK
sub 2 ; nothing else is.
NOFILE:
NOTFIRST: or ?????? ;If we are at the first boot
jp z,4022h ; start up Disk BASIC.
;
ld de,BOOTERR_MSG ;Prompt 'Boot error ...'
ld c,_STROUT## ; message and wait for a
call BDOS## ; user key in.
ld c,_DIRIN##
call BDOS##
jr retry_boot
;
BOOTERR_MSG: defb 'Boot error',CR,LF
defb 'Press any key for retry',CR,LF
defb '$'
;
SYSFCB:
defb 0,'MSXDOS SYS'
;
; Rest of the FCB (25 bytes) will be initialized to zero.
;
.dephase
if PHASED
.phase $-OFFSET
endif
NEW_BOOT_SZ equ $-NEW_BOOT
;
;
;==============================================================================
;
PROC VAL_FIB
;
; This routine is called whenever a KBDOS call is made to access a
; particular logical unit number. It does the translation from logical to
; physical unit number, and does all the necessary processing to ensure that
; the disk in the drive is correct. If B=0 (new FIB) then the volume-id for
; the current disk will be copied into the FIB, otherwise (B=1) the volume-id
; in the FIB will be used to check that it is the correct disk and will be
; modified if the user ignores a ".WFILE" error.
;
; If B=2 then the logical->physical unit number translation and checking is
; done, and any dirty buffers for the disk are flushed. If there is no UPB
; for this drive ("UD_TIME" < 2) then a normal validation (as if B=0) will be
; done, otherwise the timeout will just be reset to the maximum value. This
; is used in the absolute sector read and write functions to suppress most of
; the disk checking.
;
; Entry: C = Logical unit number (0=default, 1=A: etc)
; B = 0 => New FIB, so any disk will do.
; 1 => Old FIB, so volume-id must match
; the volume-id in the FIB.
; 2 => New FIB, but only validate if no current UPB.
; IX -> Fileinfo block (FIB)
; Returns: A = Error code, zero if successful. Z-flag
; will NOT be set appropriately on return.
; HL -> Unit descriptor for this unit (if A=0)
; Corrupts: AF,BC,DE,HL,PAGE1
;
; Assumes: UD_VOLUME_ID > UD_TIME
;
; Note: the flag returned by BUILD_UPB is not checked when B=1,
; since this flag only makes sense when invoked from F_RDDRV/F_WRDRV,
; and these routines always invoke VAL_FIB with B=2.
ld a,c ;Convert logical unit number
call LG_TO_PHYS ; to physical unit number.
ld (PHYS_UNIT##),a ;Record physical unit number
ld e,(hl)
inc hl
ld d,(hl) ;Get pointer to unit
ld a,d ; descriptor into DE.
or e
ld a,c ;Give an error return if
ret z ; this unit does not exist.
;
push de ;Save unit descriptor pointer
;
; ---------------------------------
;
pop hl
push hl
push hl
ld bc,UD_TIME## ; unit descriptor and skip if
add hl,bc ; it is zero since this means
ld a,(hl) ; that there is no current
pop hl
or a ; UPB for this unit.
jr z,valfib2
call IS_FIXED_OR_LOCKED ;Skip all if disk is fixed or locked
ld a,0
pop hl
ret c
push hl
valfib2:
dec b ;Skip if B<>1
jr nz,not_opened
;
pop hl
push hl
wfile_loop: push ix
pop hl ;DE -> required volume-id
ld bc,FIB_VOLUME_ID## ; in the FIB.
add hl,bc
ex de,hl
;
pop hl
if 1
call UD_TICK ;Make UD_TIME up to date.
endif
push ix
push hl ;Look at UD_TIME field of
ld c,UD_TIME## ; unit descriptor and skip if
add hl,bc ; it is zero since this means
ld a,(hl) ; that there is no current
or a ; UPB for this unit.
jr z,no_cur_upb
;
ld c,UD_VOLUME_ID##-UD_TIME## ;HL -> current volume-id in
add hl,bc ; unit descriptor.
ld c,a ;C := media check timeout
;
push de
call CP_VOLID ;Compare volume-id in FIB with
pop de ; one in unit descriptor and
jr z,same_volid ; skip if they are the same.
;
ld a,(PHYS_UNIT##) ;If volume-ids are different,
pcall FL_PHYS ; flush buffers for this unit.
;
no_cur_upb: pop hl ;Get the UPB for the disk in
call BUILD_UPB ; this drive and jump to
push hl ; test volume-id.
ld c,1 ;C=1 => not current volume-id
jr check_volid
;
same_volid: pop hl ;SAME VOLUME REQUIRED
push hl
dec c ;If timeout is 1 then always
jr z,get_a_new_upb ; get a new UPB.
dec c ;If timeout >2 then assume
jr nz,cur_volid_2 ; same UPB, otherwise (=2)
call MEDIA_CHECK ; do a media check and assume
jr z,cur_volid_2 ; same UPB if "not changed".
;call DO_MEDIA_CHANGE
;jr get_a_new_upb_done
get_a_new_upb: call DO_MEDIA_CHANGE ;BUILD_UPB ;Get a new UPB for the disk
get_a_new_upb_done:
ld c,0 ; and set C=0 => current volid
;
check_volid: call SET_TIMEOUT ;Reset UD_TIME to maximum.
push de
call GET_VOLID ;Point HL at volume-id from
pop de ; new UPB and compare it with
call CP_VOLID ; the one we want.
pop hl
push hl
jr nz,wrong_volid ;Jump if it is the wrong vol-id
dec c ;CORRECT VOLUME ID. If it is
jr nz,cur_volid_2 ; the same as before then jump.
call NEW_UPB ;Otherwise setup the new UPB
pcall INV_UD ; and invalidate any old
; buffers for the drive.
cur_volid_2: jr current_volid
;
;
;
wrong_volid: ld a,.WFILE## ;Tell the user that he is
ld de,0FFFFh ; trying to access the file on
;===== start add DOS2.50 (add 23bit sector address support)
ld b,d
;===== end add DOS2.50
pcall C_DISK_ERR ; the wrong disk and get his
; response (no IGNORE)
pop hl ;If IGNORE jump to alter FIB
jr nz,ignore_wfile ; and use new disk.
pop ix
push hl ;If RETRY then loop back to
jr wfile_loop ; try a new disk.
;
;
; ---------------------------------
;
not_opened: pop hl ;Flush all buffers for this
pcall FL_UD ; physical unit.
;
if 1
call UD_TICK ;Make UD_TIME up to date.
endif
push ix
push hl ;Look at UD_TIME field of
ld de,UD_TIME## ; unit descriptor and if it is
add hl,de ; 0 or 1 then jump to get a new
ld a,(hl) ; UPB for this unit.
if 0
sub 2
ld d,a ;Remember UD_TIME for later
pop hl
jr c,no_curr_upb
else
pop hl
if 0 ;900725 ;Was "if 1" in DOS 2.3x
dec b ;If B=2 on entry then skip
jr z,got_new_volid ; to assume current disk.
endif
or a
jr z,no_curr_upb
endif
;
if 1 ;900725 ;Was "if 0" in DOS 2.3x
dec b ;If B=2 on entry then skip
jr z,got_new_volid ; to assume current disk.
endif
;
if 1
dec a ;If timeout is 1 then always
jr z,no_curr_upb ; get a new UPB.
dec a ;If timeout >2 then assume
jr nz,got_old_volid ; same UPB. Otherwise 2.
endif
call IS_FIXED_OR_LOCKED ;Assume disk unchanged
jr c,got_old_volid ;if drive is locked
call CHECK_MNT ;or has a file mounted
jr nz,got_old_volid
call MEDIA_CHECK ;Do a media check call.
jr c,not_sure ;If "not sure" or "changed"
jr z,got_old_volid ; then get new UPB else
not_sure: call DO_MEDIA_CHANGE ; assume same one. !!!
jr no_curr_upd_done
no_curr_upb:
;ld a,(MFLAGS##)
;and 2
;jr nz,no_curr_upb2
call MEDIA_CHECK ;!!!
call nz,DO_MEDIA_CHANGE ;!!!
no_curr_upb2:
call BUILD_UPB ;Get the new UPB.
no_curr_upd_done:
jr z,ignore_wfile
call SET_TIMEOUT ;Don't try to parse FAT boot sector
jr no_dos_disk ;if the disk is not FAT at all
;
ignore_wfile: call NEW_UPB ;Set up the unit descriptor.
pcall INV_UD ;Invalidate any old buffers
; for the drive.
got_new_volid:
call SET_TIMEOUT ;Reset UD_TIME to its maximum
got_old_volid:
pop de
push de ;DE -> FIB
push hl
;
ld bc,UD_VOLUME_ID##
add hl,bc
ex de,hl ;Copy the four byte volume-id
ld c,FIB_VOLUME_ID## ; from the unit descriptor
add hl,bc ; into the FIB.
ex de,hl
ld c,4
ldir
;
current_volid: pop hl
no_dos_disk: pop ix ;Restore FIB pointer
xor a ;Zero error code
ret
; This subroutine will check if a device is either fixed, or removable but locked.
;
; Entry: HL = Unit descriptor
; Returns: C it drive is fixed or locked, NC otherwise
; Corrupts: none
IS_FIXED_OR_LOCKED:
push hl
ex (sp),ix
push bc
ld b,a
ld a,(ix+UD_DFLAGS##)
and UFM_DB+UFM_RM
cp UFM_DB ;Device-based + not removable?
scf
jr z,CHKLCK2
bit UF_LCK,(ix+UD_FLAGS##)
scf
jr nz,CHKLCK2
or a
CHKLCK2:
ld a,b
pop bc
ex (sp),ix
pop hl
ret
CHECK_MNT:
push hl
ex (sp),ix
bit UF_MNT,(ix+UD_FLAGS##)
ex (sp),ix
pop hl
ret
;
;
;------------------------------------------------------------------------------
;
PROC RW_UNIT
;
; This routine is used do any sector read or write operations, except those
; done internally by the "BUILD_UPB" routine. It first calls VAL_SAME to
; ensure that the correct disk is in the drive and then does the operation.
; Any error from the operation will be returned to the caller. The UD_TIME
; field will be set to UD_MEDIA_CHECK_TIMEOUT to start the MEDIA CHECK timeout going again.
;
; Entry: HL = Address of unit descriptor
; A = Command code (RD_CMD or WR_CMD)
; BC,DE,IX = Parameters for CALL_UNIT
; Returns: A = Return code from unit handler
; BC,DE = Results from CALL_UNIT
; Corrupts: AF,BC,DE
;
;
call VAL_SAME ;Validate disk in this unit
;
call CALL_UNIT ;Do the read/write operation
;
SET_TIMEOUT: push hl ;Set media check timeout to
if 1
call UD_TICK ;Make UD_TIME up to date.
endif
ex (sp),ix ; maximum value since unit
ld (ix+UD_TIME##),UD_MEDIA_CHECK_TIMEOUT##; has just been accessed. If
ex (sp),ix ; DISK_ERR is called then this
pop hl ; will be set back to 1.
;
or a ;Return with flags set
ret
;
;
;=============================================================================
;
VAL_SAME:
;
; This routine is called to make sure that the disk in a drive is the same
; as the current one defined in the unit descriptor. It is called before
; flushing a buffer for this unit. A MEDIA CHECK call is done to the disk
; driver if the timeout has expired. If this call returns "not sure" then it
; assumes that the disk has changed if the system variable "DSK_CHK" is zero,
; and assumes that it has not otherwise. If MEDIA CHECK returns "disk
; changed" then a BUILD UPB call will be done to see whether it is in fact the
; same disk, by comparing volume id's. If they are different then the user
; will be given a "Wrong disk" message and asked to retry. If the disks do
; not have volume-id's on then it will be assumed to be the correct disk,
; unless the media byte is different.
;
; Entry: HL = Address of unit descriptor
; Returns: None
; Corrupts: None
;
; Assumes: UD_MEDIA_DESC_BYTE=UD_VOLUME_ID+4
;
;
push af
push de
push hl
ld de,UD_TIME## ;HL -> UD_TIME byte of unit
add hl,de ; descriptor.
ld a,(hl)
cp 2
pop hl
push bc
jr c,chk_same_do ;If UD_TIME=0 or 1 there's no UPB so build one
call IS_FIXED_OR_LOCKED ;If fixed or locked media do nothing more
jr c,not_changed
call UD_TICK ;Make UD_TIME up to date.
cp 3 ;If UD_TIME>2 then the timeout
jr nc,not_changed ; has not expired so return without further checking.
cp 2 ;If UD_TIME=2 then do a media
call z,MEDIA_CHECK ; check call, otherwise assume
jr z,not_changed ; that disk has changed.
;
chk_same_do:
push ix ;Get a new UPB for this unit
chk_same_loop: call DO_MEDIA_CHANGE ; from the unit handler.
ld ix,UPB_BUF##
call NEW_UPB
call TEST_SAME_DISK ;Test whether volume-id's are
jr z,same_disk ; the same and jump if so.
;
ld a,.WDISK## ;WRONG DISK IN DRIVE. Report
ld de,0FFFFh ; to user, not allowing
;===== start add DOS2.50 (add 23bit sector address support)
ld b,d
;===== end add DOS2.50
pcall C_DISK_ERR ; an ignore response and
jr chk_same_loop ; with invalid sector number.
;
;
same_disk: pop ix ;Restore caller's registers
not_changed: pop bc
pop_deaf_ret: pop de
pop_af_ret: pop af
ret
;
;
;------------------------------------------------------------------------------
;
MEDIA_CHECK:
;
; This routine does a media check call to a unit handler, and if the result
; is "not sure" it translates it into "changed" or "not changed" depending on
; whether the system variable DSK_CHK is zero or 0FFh. If it is zero then it
; assumes "changed", if it is 0FFh then it assumes "not changed". Any errors
; from the disk driver except for "not ready" are ignored and "disk changed"
; is returned. This avoids confusion from badly behaved old disk drivers and
; "BUILD_UPB" will detect any errors itself. A "not ready" error is reported
; to the user so that the delay will only occur once, and "disk changed"
; returned so the retry will be done in "BUILD_UPB".
;
; Entry: HL = Address of appropriate unit descriptor
; Returns: NC & Z => Disk has not changed
; NC & NZ => Disk has changed
; C & Z => Not sure, interpret as not changed
; C & NZ => Not sure, interpret as changed
; Corrupts: AF
;
;
;First check if partition assignment is pending, if so return "changed"
push ix
push hl
pop ix
bit UF_PAP,(ix+UD_DFLAGS##)
pop ix
jr z,MCHK0
ld a,1
or a ;NC, NZ
ret
MCHK0:
call _MCHECK
ret z
ret nc ;If not "not sure interpret as changed", return
push hl
ex (sp),ix
ld a,(ix+UD_DFLAGS##)
ex (sp),ix
pop hl
bit UF_DBD,a
jr nz,MCHECK2
ld a,1 ;Return C & NZ (not sure, assume changed) if not a device-based driver
or a
scf
ret