-
Notifications
You must be signed in to change notification settings - Fork 2
/
pwd.z80
803 lines (803 loc) · 18.8 KB
/
pwd.z80
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
; -*- tab-width: 8; -*-
title 'CP/M HFS+ print working dir utility'
;
;13-Jul-2021 George Warner initial version
;##############################################################################
.z80
maclib MACRO$.LIB
;==============================================================================
aseg
org 100h ;base of tpa
;
start: ld sp,stack ;setup local stack
;; PRINT$ 1,"Welcome to the HFS+ for CPM+ change dir utility... by geowar!",1
;
call setup
;
ld a,(seldsk) ;get selected disk
add a,'A' ;add offset to ASCII
call conout
ld a,':'
call conout
ld a,'/'
call conout
;
if 0
call puttab
;
ld hl,(selPart) ;get select drive's partition
call dec_w ;output hex word (in hl) to console
;
call crlf ;output cr/lf to console
endif
;
ld hl,path$ ;address of path$
ld (hl),NULL ;NULL terminate it
;
ld hl,(selPart) ;get select drive's partition #
;
sk0010: ld (dotPart),hl ;save as this partition #
;
call findSLASH ;find root dir entry
jp z,sk0080 ;(found)
;
call findDDOT ;find parent dir entry
jr z,sk0020 ;(not found)
;
PRINT$ 1,"Error: No root or parent dir found.",0
jp exit
;
sk0020: ld de,14 ;add offset to partition
add hl,de
;
ld a,(hl) ;get partition from dir entry
inc hl
ld h,(hl)
ld l,a
;
ld a,(seldsk) ;get selected disk
call setDrivePartition
;
ld de,(dotPart) ;get select drive's partition
call findPartEntry ;find dir entry with this partition #
jr z,sk0030 ;(found)
;
PRINT$ 1,"Error: can't find dir entry for this partition: ",0
ld hl,(dotPart) ;get select drive's partition
call dec_w
jr exit
;
sk0030: ld de,name$ ;get address of name$
call getFileName ;copy filename out of fcb
;
ex de,hl ;move name$ pointer to hl
call scanNULL ;scan to end of string (null terminator)
;
ld (hl),'/' ;add delimiter
inc hl ;bump pointer
ld (hl),NULL ;terminate it
;
ld hl,path$ ;get address of path$
ld de,name$ ;get address of name$
call strIns ;insert name$ at front of path$
;
ld hl,(partNum) ;get partition #
jp sk0010
;
sk0080: ld hl,path$ ;output path$ to console
call print2Null
;
sk0100: call crlf ;output cr/lf to console
;; jr exit
;
exit: ld a,(seldsk) ;get selected disk
ld hl,(selPart) ;get select drive's partition #
call setDrivePartition
;
jp wboot ;return to CP/M+
;
;##############################################################################
;copy filename out of fcb/dir entry @hl to #de
;==============================================================================
;
getFileName:
call pushpair ;push all register pairs (and poppair return address)
;
ld b,8 ;copy 8 characters max from fcb name
;
gFF040: inc hl ;bump pointer
ld a,(hl) ;get character
cp ' ' ;blank?
jr z,gFF050 ;(yes)
;
ex de,hl ;swap pointers
ld (hl),a ;store dir character in name$
inc hl ;bump pointer
ex de,hl ;swap pointers
;
gFF050: djnz gFF040 ;(loop till --b is zero)
;
ld a,(hl) ;get first character of extension
cp ' ' ;blank?
jr z,gFF080 ;(yes)
;
dec hl ;unbump extension pointer
ld b,4 ;copy 4 characters max
ld a,'.' ;copy period
jr gFF070 ;jump into copy loop
;
gFF060: inc hl ;bump pointer
ld a,(hl) ;get character
cp ' ' ;blank?
jr z,gFF080 ;(yes)
;
gFF070: ex de,hl ;swap pointers
ld (hl),a ;store dir character in name$
inc hl ;bump pointer
ex de,hl ;swap pointers
;
djnz gFF060 ;(loop till --b is zero)
;
gFF080: ex de,hl ;move output pointer to hl
ld (hl),0 ;terminate it!
;
ret
;
;##############################################################################
;fix fcb - convert all '-' to '.'
;==============================================================================
;
fixFCB: ld hl,fcb+1 ;get address of fcb
ld b,11 ;do 11 characters
ld c,'.' ;period character
ff0010: ld a,(hl) ;get fcb character
cp '-' ;minus?
jr nz,ff0020 ;(no)
ld (hl),c ;change to period
ff0020: inc hl ;bump fcb pointer
djnz ff0010 ;(loop till --b is zero)
ret
;
;##############################################################################
;set partition (in hl) for drive (in a)
;==============================================================================
;
setDrivePartition:
call pushpair ;push all register pairs (and poppair return address)
;
if 0
PRINT$ 1,"Switching drive ",0
;
push af ;save disk
add a,'A' ;convert to ASCII
call putchr
pop af
;
PRINT$ 0,": to partition # ",0
call dec_w
endif
;
push hl ;save partition #
;
call getDiskInfo ;get disk info for drive in acc
jr nz,mhd08 ;(is HFS+ drive)
;
PRINT$ 1,"Not an HFS+ drive.",1
jp wboot
;
mhd08: ld hl,(DPB) ;get address of DPB
ld de,21 ;add offset to partition # address
add hl,de
;
pop de ;restore partition #
;
ld (partNum),de ;save new partition #
;
ld (hl),e ;store partition # after DPB
inc hl
ld (hl),d
;
call BDOS$flushBuffers ;flush drive buffers
call setMediaFlags ;set door open flags
;
ld a,(seldsk) ;get selected disk
ld b,a ;to b
ld hl,1 ;initialize drive vector to 1
mhd010: add hl,hl ;shift up drive vector
djnz mhd010 ;(loop till --b is zero)
;
ld c,37 ;BDOS:reset drive
ex de,hl ;move vector to de
call bdos
;
ld c,14 ;BDOS:Select disk
ld a,(seldsk) ;disk to e
ld e,a
jp bdos
;
;##############################################################################
;find this (DOT) dir entry
;==============================================================================
findDOT:
ld de,dot$ ;address of dot string
ld bc,12 ;12 bytes
jp findEntry ;go find it
;
dot$: db '.. ' ;dot dir string
;##############################################################################
;find parent (DOT-DOT) dir entry
;==============================================================================
findDDOT:
ld de,dotdot$ ;address of dotdot string
ld bc,12 ;12 bytes
jp findEntry ;go find it
;
dotdot$:db '... ' ;dotdot dir string
;##############################################################################
;find root (SLASH) dir entry
;==============================================================================
findSlash:
ld de,slash$ ;address of slash dir string
ld bc,12 ;12 bytes
jp findEntry ;go find it
;
slash$: db './ ' ;slash dir string
;##############################################################################
;find empty dir entry
;==============================================================================
findEmpty:
ld de,empty$ ;address of empty string
ld bc,1 ;one byte
jr findEntry ;go find it
;
;empty dir string
empty$: defb 0E5h
;
;##############################################################################
;find dir entry
;bc=length, de=entry to find
;==============================================================================
findEntry:
call fnd1st ;find first entry
fE0010:
if 0
push bc
push hl
PRINT$ 1,"findEntry[",0
ld hl,(index)
call dec_w
PRINT$ 0,"]:",0
pop hl
ld bc,32 ;dump it
call dump
pop bc
endif
;
call memcmp ;compare (hl++) to (de++) for (bc--) bytes
ret z ;(equal!)
;
call fndnxt ;find next entry
jr nz,fE0010 ;(not end-of-dir)
;
xor a ;return -1 [entry not found]
dec a
ret
;
;##############################################################################
;find dir partition entry
;de=partition to find
;==============================================================================
findPartEntry:
call fnd1st ;find first entry
fP0010:
if 0
push bc
push hl
PRINT$ 1,"findPartEntry[",0
ld hl,(index)
call dec_w
PRINT$ 0,"]:",0
pop hl
ld bc,32 ;dump it
call dump
pop bc
endif
;
push hl ;save dir entry pointer
;
ld bc,14 ;add offset to partition # in dir entry
add hl,bc
;
ld a,(hl) ;get partition # from directory entry
inc hl
ld h,(hl)
ld l,a
;
cphlde ;cpw hl,de
pop hl ;restore dir entry pointer
ret z ;(found it)
;
call fndnxt ;find next entry
jr nz,fP0010 ;(not end-of-dir)
;
xor a ;return -1 [entry not found]
dec a
ret
;
;##############################################################################
;do initial setup
;==============================================================================
;
setup: ld e,0FFh ;set error mode to return error mode
call BDOS$setErrorMode
;
ld c,25 ;get current drive
call bdos
ld (curdsk),a ;store it
;
if 0
PRINT$ 1,"FCB: ",0
ld hl,fcb ;dump fcb
call dump_fcb
ld bc,32
call dump
endif
;
ld a,(fcb) ;get drive byte
or a ;default drive?
jr nz,notzero ;(no)
;
ld a,(curdsk) ;get current drive
inc a
;
notzero:ld (fcb),a ;make it implicit
dec a ;drive A?
jp nz,dskok ;(no)
;
dskerr: PRINT$ 1,"CP/M HFS+ not supported on boot/system drive A.",0
jp exit
;
dskok: call getDiskInfo ;get disk info (duh!)
ld (selPart),hl ;save the select partition #
ret nz ;(good)
;
PRINT$ 1,"Not HFS+ drive.",0
jp reboot
;
;##############################################################################
;get disk (in a) info; return partition # in hl (-1 & zero flag if invalid)
;==============================================================================
getDiskInfo:
call selectDisk ;select drive (in a)
jp nz,gdi020 ;(invalid)
;
call BDOS$GETDPB ;get DPB
ld (DPB),hl ;store it
;
push hl ;copy to IX
pop ix
;
ld l,(ix+0) ;get SPT
ld h,(ix+1)
ld (spt),hl ;store it
;
ld l,(ix+7) ;get DRM
ld h,(ix+8)
ld (drm),hl ;store it
;
inc hl ;bump to count
ld (count),hl ;store it
;
ld l,(ix+13) ;get offset
ld h,(ix+14)
ld (offset),hl ;store it
;
ld a,(ix+15) ;get PSH
ld (psh),a ;store it
;
ld b,a ;compute sector size
inc b ;do one extra (so we don't have to test for zero)
ld hl,64 ;start with half sector (because of extra)
gdi010: add hl,hl ;double sector size
djnz gdi010 ;(loop until --b is zero)
;
ld (secsiz),hl ;store it
;
push ix ;DPB to hl
pop hl
;
ld de,17 ;add offset to HFS+ signature
add hl,de
;
ld de,hfs$ ;address of HFS+ string
ld bc,4 ;four bytes
call memcmp ;compare (hl++) to (de++) for (bc--) bytes
jr nz,gdi020 ;(not HFS+ partition)
;
add hl,bc ;add offset to partition #
ldhlhl ;ldw hl,(hl)
jr gdi030
;
gdi020:
if 0
ld bc,4
call z,dump
endif
ld hl,-1
;
gdi030: ld (partNum),hl ;store it
ld de,-1
cphlde ;cpw hl,de
ret
;
hfs$: db 'hfs+' ;HFS+ partition signature
;
;##############################################################################
;find first dir entry
;==============================================================================
;
fnd1st: or a ;clear carry
sbc hl,hl ;zero hl
dec hl ;bump hl to -1
ld (sector),hl ;set sector to -1 (to force 1st read)
ld (index),hl ;start index at -1
;
;##############################################################################
;find next dir entry
;==============================================================================
;
fndnxt: call pushpair ;push all register pairs (and poppair return address)
;
ld hl,(index) ;bump index
inc hl
ld (index),hl
;
if 0
PRINT$ 1,"index: ",0
call dec_w
PRINT$ 0," (",0
call hex_w
endif
;
ex de,hl ;move to de
;
ld hl,(count) ;end-of-dir?
if 0
PRINT$ 0,"), count: ",0
call dec_w
PRINT$ 0," (",0
call hex_w
PRINT$ 0,")",0
endif
or a ;clear carry
sbc hl,de
ret z ;(yes)
;
ld hl,2 ;dir entries per (128 byte) sector (half for extra shift)
ld a,(psh) ;get physical shift factor
inc a ;do extra shift (so we don't have to test for zero)
ld b,a
fnd005: add hl,hl
djnz fnd005 ;(loop until --b is zero)
;
if 0
PRINT$ 0,", div by: ",0
call dec_w
endif
ex de,hl ;move to de
;
ld bc,(index)
call Div16 ;bc=bc/de, hl=bc%de
push hl ;store remainder
if 0
PRINT$ 0,", mod;",0
call dec_w
call crlf
endif
;
ld de,(spt) ;divide by sectors per track
call Div16 ;bc=bc/de, hl=bc%de
ld de,(sector) ;get last sector
ld (sector),hl ;store new sector
sbc hl,de ;same?
jr z,$+3 ;(yes)
dec a
ld (readflg),a ;store read flag
;
ld hl,(offset) ;add offset to track
add hl,bc
ld (track),hl ;store track
;
pop de ;restore remainder
ld bc,32 ;32 bytes per dir entry
call Mul16 ;dehl = bc * de
ld de,DirBuf ;add dir buffer
add hl,de ;[hl now had the dir entry address]
;
ld a,(readflg) ;need to do read?
or a
jp z,skipread ;(no)
;
push hl ;store dir entry address
;
call readDirSector
;
if 0
PRINT$ 1,"Disk: ",0
ld a,(seldsk)
call dec_b
;
PRINT$ 0,", Track: ",0
ld hl,(track)
call dec_w
PRINT$ 0," (0x",0
call hex_w
;
PRINT$ 0,"), sector: ",0
ld hl,(sector)
call dec_w
PRINT$ 0," (0x",0
call hex_w
PRINT$ 0,")",0
;
if 0
call crlf
ld hl,DirBuf ;in this sector
ld bc,(secsiz)
call dump
call crlf
endif
endif
;
pop hl ;restore dir entry address
;
skipread:
ex de,hl ;save dir entry address in de
ld hl,6
add hl,sp
ld (hl),e ;ldw (hl),de
inc hl
ld (hl),d
;
xor a ;return not zero (not end-of-dir)
dec a
ret
;
;##############################################################################
;select disk
;==============================================================================
selectDisk:
ld (seldsk),a ;store select disk
;
ld c,14 ;select disk
ld e,a
call bdos
or a ;success?
ret z ;(yes)
;
ld a,(quiet) ;quiet?
or a
ret nz ;(yes)
;
ld a,h ;get error code
cp 1 ;disk I/O error?
jr nz,ds_10 ;(no)
;
PRINT$ 1,'Select Disk: Disk I/O error',0
jr selerr9
;
ds_10: cp 4 ;invalid drive?
jr nz,ds_20 ;(no)
;
PRINT$ 1,'Select Disk: Invalid drive',0
jr selerr9
;
ds_20: PRINT$ 1,'Select Disk: Unknown error;',0
call dec_b ;output acc as decimal
;
selerr9:jp exit
;
;##############################################################################
;read dir sector
;==============================================================================
readDirSector:
call selectDir ;select dir (disk, track, sector & DMA)
jp BIOS$READ ;read sector
;
;##############################################################################
;select dir (disk, track, sector & DMA)
;==============================================================================
selectDir:
ld a,(seldsk) ;select disk
ld c,a
call BIOS$SELDSK
;
ld bc,(track) ;select track
call BIOS$SELTRK
;
ld bc,(sector) ;select sector
call BIOS$SELSEC
;
ld bc,DirBuf ;set DMA address
jp BIOS$SETDMA
;
;##############################################################################
;BDOS FUNCTION 45: Set Error Mode
;parameters: e=error mode (FF=return, FE=return & display else default)
;==============================================================================
;
BDOS$setErrorMode:
ld c,45 ;set BDOS error mode
jp bdos
;
;##############################################################################
;BDOS: flush buffers
;==============================================================================
;
BDOS$flushBuffers:
ld c,030h ;flush buffers
ld e,0FFh ;all active
jp bdos
;
;##############################################################################
;set door open media flags
;==============================================================================
;
setMediaFlags:
call pushall ;push all registers (and popall return address)
;
ld a,54 ;save offset to media flag in SCB
ld (SCBPT$OFFSET),a
;
ld a,0FFh ;save set operation
ld (SCBPT$SET),a
;
;ld a,0FFh ;save media flag value (same as set operation)
ld (SCBPT$VALUE),a
;
ld c,031h ;get/set SCB
ld de,SCBPT
jp bdos
;
SCBPT:
SCBPT$OFFSET: ds 1 ;Offset within SCB
SCBPT$SET ds 1 ;0FFh=set byte, 0FEh=set word, 000h=get
SCBPT$VALUE ds 2 ;byte or word value to be set
;
;##############################################################################
;BIOS FUNCTION 31: get disk parameter block
;==============================================================================
;
BDOS$GETDPB:
ld c,31 ;get disk parameter block
jp bdos
;
;##############################################################################
;BIOS FUNCTION 9: Select Disk
;
;parameters: C=disk drive (0-15)
; E=initial select flag (lsbit = 0)
;==============================================================================
;
BIOS$SELDSK:
ld a,9
ld (BIOSPB),a
call BDOS$directBIOS
;
ld a,h ;error?
or l
ret nz ;(no)
;
PRINT$ 1,"BIOS SELDSK: Invalid drive.",0
jp exit
;
;##############################################################################
;==============================================================================
BIOS$SELTRK:
push af
ld a,10
ld (BIOSPB),a
pop af
jp BDOS$directBIOS
;
;##############################################################################
;==============================================================================
BIOS$SELSEC:
ld a,11
ld (BIOSPB),a
jp BDOS$directBIOS
;
;##############################################################################
;==============================================================================
BIOS$SETDMA:
ld a,12
ld (BIOSPB),a
jp BDOS$directBIOS
;
;##############################################################################
;==============================================================================
BIOS$READ:
ld a,13
ld (BIOSPB),a
call BDOS$directBIOS
;
or a ;error?
ret z ;(no)
;
cp a,1 ;non-recoverable error?
jr nz,.l0010 ;(no)
;
PRINT$ 1,"BIOS READ: Non-recoverable error.",0
jr .l9999
;
.l0010: cp a,0FFh ;media changed error?
jr nz,.l0020 ;(no)
PRINT$ 1,"BIOS READ: media changed error.",0
jr .l9999
;
.l0020: PRINT$ 1,"BIOS READ: Unknown error: ",0
call dec_b ;output acc as decimal
;
.l9999 jp exit ;return to CP/M
;
;##############################################################################
;==============================================================================
BDOS$directBIOS:
ld (BIOSPB+1),a ;store A register
ld (BIOSPB+2),bc ;store BC registers
ld (BIOSPB+4),de ;store DE registers
ld (BIOSPB+6),hl ;store HL registers
;
ld c,032h ;bdos - direct BIOS calls
ld de,BIOSPB
jp bdos
;
;##############################################################################
;variable storage
;==============================================================================
BIOSPB: ds 1 ;BIOS function number
ds 1 ;A register
ds 2 ;BC register
ds 2 ;DE register
ds 2 ;HL register
;
curdsk: ds 1 ;current drive
seldsk: ds 1 ;select drive
track: ds 2 ;track
sector: ds 2 ;sector
readflg:ds 1 ;read flag
quiet: db 1 ;quiet flag
;
index: ds 2 ;dir index
count: ds 2 ;dir count
;
DPB: ds 2 ;pointer to disk parameter block for selected drive
offset: ds 2 ;system track offset for selected drive
spt: ds 2 ;sectors per track
drm: ds 2 ;dir entries minus one
psh: ds 1 ;physical shift factor
secsiz: ds 2 ;physical sector size
partNum:ds 2 ;partition #
newPart:ds 2 ;new partition #
selPart:ds 2 ;select partition #
dotPart:ds 2 ;this partition #
;
name$: ds 16 ;a place to copy a dir name
path$: ds 256 ;a place to concatinate dir names
;##############################################################################
;## CP/M utility code ##
;==============================================================================
include cpmutil.z80
;##############################################################################
;## stack and dir buffer ##
;==============================================================================
;
ds 256 ;minimum stack area
ALIGN$ 512 ;align to 512 bytes
stack equ $
;
DirBuf equ $ ;dir buffer
;
;##############################################################################
end