-
Notifications
You must be signed in to change notification settings - Fork 3
/
basrsx.mac
1756 lines (1534 loc) · 28.4 KB
/
basrsx.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
TITLE BASIC-11 interpreter
SUBTTL RSX180/RSX280 system-dependent routines
.Z80
; Hector Peraza, 2016-2020
include BASDEF.INC
include SYSFN.INC
include QIO.INC
include TCB.INC
include FCB.INC
include AST.INC
include ERRORS.INC
CR equ 0Dh
LF equ 0Ah
EOF equ 1Ah
; Event flag numbers
TIEFN equ 1 ; event flag for AST terminal input
TOEFN equ 2 ; event flag for terminal output, attach, etc.
FIOEFN equ 3 ; first event flag number for file I/O ops.
; Terminal queue offsets
TQ.IP equ 0 ; input pointer
TQ.OP equ TQ.IP+1 ; output pointer
TQ.BUF equ TQ.OP+1 ; buffer
public $INIT,$TTGC,$TTIN,$TTOUT,$FLINP,$FLUSH,$EXIT,DATIM
public $CHKCC,FILSPE,CATALOG,RCTLO,TTYLC,TTYUC,FDELET,FRENAM
public ADDEXT,CMPEXT,FRESET,FCREAT,FOPEN,FCLOSE,FREAD,FWRITE
public FVERS,FWAIT
extrn SAVREG,MSG,MSGHL,READY,CHKFIL,DOMSG,INIT,CCFLG,ADDHLA
extrn CPHLDE,PUTCHR,VERSION,HEAD2,HEAD3,ISDIG,READY,T4
;-----------------------------------------------------------------------
cseg
; Initialization
$INIT: ld hl,-GTKSZ
add hl,sp ; allocate space on the stack
ld sp,hl
ex de,hl ; DE = dest buffer
ld hl,0 ; HL = 0 (current task)
SC .GTSK ; get task info
ld hl,GT.END
add hl,de
ld c,(hl) ; fetch top of memory
inc hl
ld b,(hl)
ld hl,GTKSZ
add hl,de
ld sp,hl ; restore stack
push bc ; push top address
ld a,0FFh
ld (ttlc),a ; enable lowercase
ld ix,TTQ
call qinit ; initialize terminal queue
ld de,'TI'
ld c,0
ld b,LUNTI
SC .ALUN ; assign TI: to LUNTI
call ATTACH ; attach terminal with ASTs
call TTCLR ; clear output buffer
call $FLINP ; clear input buffer
call MSG
db CR,LF,'RSX180 BASIC-11 ',0
ld hl,VERSION
call MSGHL ; output banner
call crlf
ld hl,FDBSZ+4 ; FDB size + status
ld (FNBSZ),hl
pop hl ; return top of memory in HL
ld l,0 ; (must be even!)
ret
; Exit to system
$EXIT: call DETACH ; detach from terminal
ld hl,EX.SUC ; exit with success code
SC .EXIT
;-----------------------------------------------------------------------
; Terminal I/O routines
; Unsolicited character AST routine.
TTAST: ex (sp),hl ; fetch argument (character)
push de
push bc
push ix
ld ix,TTQ
ld c,l ; get char into reg C for qput
ld a,c
cp 03h ; ^C ?
jr nz,ast1
call qinit ; clear input queue if yes
ld hl,CCFLG+1
set 7,(hl) ; set ^C flag
ast1: call qput ; store char in queue
ld e,TIEFN
SC .SETF ; set event flag
pop ix
pop bc
pop de
pop hl
SC .ASTX ; exit AST
; Single-character input, no echo
$TTGC: call $FLUSH ; send pending output to terminal
push ix
push hl
push de
push bc
ci1: ld e,TIEFN
SC .CLEF ; clear event flag
ld ix,TTQ
call qget ; get char from input queue
jr nc,ci2
ld e,TIEFN ; if none ready
;; SC .WTSE ; then wait
SC .STSE
jr ci1
ci2: ld hl,ttlc
bit 0,(hl) ; translate lowercase to uppercase?
call z,UCASE ; convert character if yes
pop bc
pop de
pop hl
pop ix
ret
; Reset terminal input queue
qinit: xor a
ld (ix+TQ.IP),a
ld (ix+TQ.OP),a
ret
; Fetch character from queue
qget: ld a,(ix+TQ.OP)
cp (ix+TQ.IP)
scf
ret z ; return if buffer empty
ld e,a
inc a
and 3Fh
ld (ix+TQ.OP),a
ld d,0
push ix
add ix,de
ld a,(ix+TQ.BUF) ; get char
pop ix
or a
ret
; Store character in queue
qput: ld a,(ix+TQ.IP)
ld e,a
inc a
and 3Fh
cp (ix+TQ.OP)
scf
ret z ; return if buffer full
ld (ix+TQ.IP),a
ld d,0
push ix
add ix,de
ld (ix+TQ.BUF),c ; store char
pop ix
or a
ret
; Flush (discard) input buffer
$FLINP: push ix
push hl
ld ix,TTQ
call qinit ; reset terminal input queue
ld hl,ttibuf
ld (ttiptr),hl ; clear input line
ld (hl),EOF
pop hl
pop ix
ret
; Character input (line-buffered)
$TTIN: call $FLUSH ; flush any pending output
push hl
tt0: ld hl,(ttiptr) ; get line pointer into HL
ld a,(hl) ; fetch character
cp EOF ; end of line?
jr nz,tt1 ; return if not
push de
push bc
ld hl,ttibuf
ld (ttiptr),hl ; else reset line pointer
ld e,126
call GETLN ; and collect a new line
pop bc
pop de
jr tt0 ; loop
tt1: inc hl ; advance line pointer
ld (ttiptr),hl
pop hl
or a
ret
; Read line from terminal into HL buffer, max length in E
; TODO: handle ^R
GETLN: ld b,1
gln1: call $TTGC
cp 8 ; backspace?
jr z,del
cp 7FH ; DEL?
jr z,del ; handle like backspace
cp CR ; CR?
jr z,eol
ld c,a
ld a,b ; check length
cp e ; maximum count reached?
jr nc,glnerr ; jump if yes
ld a,c ; check character
cp ' ' ; control char?
jr nc,gln2 ; jump if not
ld a,'^' ; else echo it as ^x
call $TTOUT
ld a,c
add a,'@'
call $TTOUT
ld a,c
cp 3 ; ^C?
jr z,ctrlc
cp 1Ah ; ^Z?
jr z,ctrlu
cp 15h ; ^U?
jr z,ctrlu
jr gln3
gln2: call $TTOUT ; printable character
gln3: ld (hl),c ; store it
inc hl
inc b
jr gln1
del: dec b ; backup one char
jr z,GETLN
call bcksp
dec hl
dec c
ld a,(hl) ; if it was a control char
cp ' ' ; then we need to erase
call c,bcksp ; also the '^'
jr gln1
bcksp: ld a,8
call $TTOUT
ld a,' '
call $TTOUT
ld a,8
call $TTOUT
ret
glnerr: ld a,7
call $TTOUT
jr gln1
ctrlu: dec b ; ^U discards current input
ld c,b ; and restarts input
ld b,0
or a
sbc hl,bc
ld a,CR
call $TTOUT
ld a,LF
call $TTOUT
jr GETLN
ctrlc: ld d,c ; ^C discards input
dec b ; and returns EOF
ld c,b
ld b,0
or a
sbc hl,bc
ld a,d
scf
eol: push af
ld (hl),a
ld a,CR
call $TTOUT
ld a,LF
call $TTOUT
inc hl
ld (hl),EOF
pop af
ret
; Character output (buffered)
$TTOUT: push hl
ld hl,(ttoptr)
ld (hl),a ; store char in buffer
inc hl ; advance pointer
ld (ttoptr),hl
pop hl
push bc
ld c,a
ld a,(ttocnt)
inc a ; increase byte counter
ld (ttocnt),a
cp 128 ; buffer full?
ld a,c
pop bc
jr nc,$FLUSH ; output it if yes
cp LF ;CR ; CR?
ret nz ; return if not, else flush buffer
$FLUSH: ld a,(ttocnt)
or a ; check in case the function is called
ret z ; separately
push bc
push de
push hl
snd0: xor a
ld (ttoqio+Q.SUBF),a
ld hl,(ttocnt) ; bytes to write
ld h,a
ld (ttoqio+Q.LEN),hl
ld hl,0
ld (QIOSB+2),hl ; clear counter of bytes sent
ld hl,ttoqio
SC .QIO ; .QIOW
;; jr c,snd1 ; if queuing failed
ld hl,(QIOSB+2) ; fetch byte count
;; ld a,(QIOSB) ; fetch return code
ld a,h
or l ; anything sent?
jr z,snd0 ; keep trying if not
ld a,(ttocnt)
sub l ; all bytes sent?
ld (ttocnt),a
ld de,ttobuf
jr z,snd1 ; return if yes
ld c,a ; else prepare counter of bytes to move
ld b,0
ld h,b ; just in case
add hl,de ; HL = src (ttobuf+sent), DE = dst (ttobuf)
ldir ; shift buffer, we'll send the remainder later
snd1: ld (ttoptr),de ; set new buffer pointer (ttobuf+diff)
pop hl
pop de
pop bc
ret
TTCLR: push hl
ld hl,ttobuf
ld (ttoptr),hl
xor a
ld (ttocnt),a
pop hl
ret
RCTLO: push hl
ld a,TF.CCO ; IO.WVB subfunction
ld (ttoqio+Q.SUBF),a
ld hl,0
ld (ttoqio+Q.LEN),hl
ld hl,ttoqio
SC .QIO ; .QIOW
pop hl
ret
; Enable/disable lowercase
TTYLC: ld a,0FFh
ld (ttlc),a ; enable lowercase
ret
TTYUC: xor a
ld (ttlc),a ; convert input to uppercase
ret
; Check for ^C
$CHKCC: ret ; nothing to do here, flag is set via AST
; Convert char in A to uppercase
UCASE: cp 'a'
ret c
cp 'z'+1
ret nc
and 5Fh
ret
; Attach terminal with ASTs.
ATTACH: ld hl,attqio
SC .QIO ; .QIOW
ret c ; if queuing failed
ld a,(QIOSB) ; fetch return code
or a
ret z
scf
ret
; Detach terminal.
DETACH: ld hl,IO.DET
ld (attqio+Q.FUNC),hl
ld hl,attqio
SC .QIO
ret c
ld a,(QIOSB) ; fetch return code
or a
ret z
scf
ret
;-----------------------------------------------------------------------
; Get date and time
DATIM: call SAVREG ; save registers
ld hl,-8
add hl,sp ; allocate 8-byte buffer
ld sp,hl
SC .GDAT ; get system time
jr c,dt1
ld ix,0
add ix,sp
ld hl,HEAD2 ; address to store date
ld a,(ix+3) ; day
call ahex
ld a,(ix+2) ; month
call bcdbin
dec a ; make it base 0
add a,a
add a,a ; *4
ex de,hl ; DE = dst
ld hl,months
call ADDHLA ; HL = src
ld bc,5
ldir ; store month name and separators
ex de,hl
ld a,(ix+0) ; year
call ahex
ld a,(ix+1) ; year
call ahex
ld hl,HEAD3 ; address to store time
ld a,(ix+4) ; hours
call ahex
ld (hl),':'
inc hl
ld a,(ix+5) ; min
call ahex
ld (hl),':'
inc hl
ld a,(ix+6) ; sec
call ahex
xor a
dt1: push af
ld hl,8
add hl,sp
ld sp,hl ; cleanup stack
pop af
ret
months: db '-JAN-FEB-MAR-APR-MAY-JUN-JUL-AUG-SEP-OCT-NOV-DEC-'
; Convert BCD number in A to binary.
bcdbin: push de
ld d,a
and 0F0h
ld e,a ; high nibble in E
ld a,d
and 0Fh ; low nibble
srl e
add a,e
srl e
srl e
add a,e
pop de
ret
; Store A as hexadecimal value at (HL).
ahex: push af
rrca
rrca
rrca
rrca
call nhex
pop af
nhex: and 0Fh
add a,90h
daa
adc a,40h
daa
ld (hl),a
inc hl
ret
;-----------------------------------------------------------------------
; File I/O routines
FRESET: xor a
ret
; Return file version number, used only by SAVE/REPLACE commands.
; Called with HL = FDB address.
FVERS: push hl
ld bc,F.VER
add hl,bc
ld c,(hl)
inc hl
ld b,(hl)
pop hl
ret
; Create file. HL = FDB address, T4 = LUN.
FCREAT: call SAVREG
ld a,(T4)
add a,LUNSY-1
ld (hl),a ; use F.LNK of FDB to store LUN
ld (opnqio+Q.P1),hl ; set FDB address
ld b,a ; LUN in B
ld de,F.DEV
add hl,de
ld e,(hl)
inc hl
ld d,(hl)
inc hl
ld c,(hl) ; device name in DE-C
SC .ALUN
ret c
ld l,IO.CRE
ld h,0 ; non-contiguous file
ld (opnqio+Q.FUNC),hl ; set function and subfunction code
ld a,b
ld (opnqio+Q.LUN),a ; set LUN
add a,FIOEFN
ld (opnqio+Q.EFN),a ; set event flag number
ld hl,0
ld (opnqio+Q.P2),hl ; no blocks to allocate
ld (opnqio+Q.P2+2),hl
ld hl,opnqio
SC .QIO ; .QIOW
ret c ; if queuing failed
ld a,(QIOSB) ; fetch return code
or a ; check result
ret z
scf
ret
; Open file. HL = FDB address, T4 = LUN, C = mode: 1 (R/W) or 0 (R/O)
FOPEN: call SAVREG
ld a,(T4)
add a,LUNSY-1
ld (hl),a ; use F.LNK field of FDB to store LUN
ld (opnqio+Q.P1),hl ; set FDB
ld b,a ; LUN now in reg B
push bc ; save LUN and mode
ld de,F.DEV
add hl,de
ld e,(hl)
inc hl
ld d,(hl)
inc hl
ld c,(hl) ; device name in DE-C
SC .ALUN
pop bc ; restore LUN and mode
ret c
ld a,c ; check mode
or a
ld a,SF.ACR OR SF.SHR
jr z,fopn1
or SF.ACW
fopn1: ld h,a
ld l,IO.ACC
ld (opnqio+Q.FUNC),hl ; function and subfunction
ld a,b
ld (opnqio+Q.LUN),a ; set LUN
add a,FIOEFN
ld (opnqio+Q.EFN),a ; set event flag number
ld hl,opnqio
SC .QIO ; .QIOW
ret c ; if queuing failed
ld a,(QIOSB) ; fetch return code
or a ; check result
IF 1
scf
ret nz
ld hl,-FINFSZ
add hl,sp
ld sp,hl
ld (ratqio+Q.BUF),hl
push hl
ld a,(opnqio+Q.LUN) ; get LUN
ld (ratqio+Q.LUN),a
add a,FIOEFN
ld (ratqio+Q.EFN),a
ld hl,ratqio
SC .QIO ; read file attributes
pop hl
jr c,fopn2
ld a,(QIOSB)
or a
scf
jr nz,fopn2
ld de,(opnqio+Q.P1) ; get FDB address
inc hl ; don't overwrite LUN
inc de
inc hl
inc de
ld a,FN.DEV OR FN.DIR OR FN.NAME OR FN.EXT OR FN.VER
ld (de),a
inc hl
inc de
ld bc,FDBSZ-3
ldir ; store full filespec
xor a
fopn2: ld hl,FINFSZ
add hl,sp
ld sp,hl
or a
ENDIF
ret z
scf
ret
; Close file. HL = FDB address.
FCLOSE: ld c,(hl)
FCLOS1: push hl
ld a,c
ld (clsqio+Q.LUN),a ; LUN
add a,FIOEFN
ld (clsqio+Q.EFN),a ; event flag number
ld hl,clsqio
SC .QIO ; .QIOW
pop hl
ret c ; if queuing failed
ld a,(QIOSB)
or a ; check result
ret z
scf
ret
; Initiate block read from file.
; Called with BC = block #, DE = buf addr, HL = FDB addr
FREAD: exx
ld hl,IO.RVB
exx
jr frw ; finish via common code
; Initiate block write to file.
; Called with BC = block #, DE = buf addr, HL = FDB addr
FWRITE: exx
ld hl,IO.WVB
exx
frw: ld a,(hl) ; A = LUN
ld (rwqio+Q.LUN),a
add a,FIOEFN
ld (rwqio+Q.EFN),a
ld (rwqio+Q.BUF),de
push hl
push de
ld de,FDBSZ
add hl,de
ld (rwqio+Q.IOSB),hl
exx
ld (rwqio+Q.FUNC),hl
exx
ld (rwqio+Q.BLK),bc
ld hl,0
ld (rwqio+Q.BLK+2),hl
ld (rwqio+Q.OFS),hl
ld (rwqio+Q.VFC),hl
ld hl,512
ld (rwqio+Q.LEN),hl
ld hl,rwqio
SC .QIO ; queue I/O operation
pop de
pop hl
ret
; Wait for file I/O to complete. HL = FDB address.
; Returns error/success code in A, bytes transferred in BC.
FWAIT: push de
ld a,(hl) ; get LUN
add a,FIOEFN ; obtain event flag number
ld e,a
; SC .WTSE ; wait for event flag
SC .STSE ; stop for event flag
pop de
ret c
push hl
push de
ld de,FDBSZ
add hl,de
ld a,(hl)
inc hl
inc hl
ld c,(hl)
inc hl
ld b,(hl)
pop de
pop hl
or a
ret z
scf
ret
; Add file ext (HL) if necessary (C=0) or forced (C=1).
; DE = FDB addr.
ADDEXT: push hl
ld hl,F.EXT
add hl,de
ld a,c
or a
jr nz,ade0
ld a,(hl)
cp ' '
jr nz,ade1
;; push hl
;; ld hl,F.ATTR
;; add hl,de
;; pop hl
;; ld a,(hl)
;; and FA.EXT
;; jr z,ade1
ade0: ex de,hl
pop hl
ld bc,3
ldir
ret
ade1: pop hl
inc hl
inc hl
inc hl
ret
; Compare file extension: HL = FCB, DE = ext. Returns Z/NZ.
CMPEXT: push hl
push bc
ld bc,F.EXT
add hl,bc
ld b,3
cpe1: ld a,(de)
cp (hl)
jr nz,cpe2
inc hl
inc de
djnz cpe1
cpe2: pop bc
pop hl
ret
; Delete file, HL = FNB address.
FDELET: call SAVREG
push hl
ld de,F.DEV
add hl,de
ld e,(hl)
inc hl
ld d,(hl)
inc hl
ld c,(hl)
ld b,13+LUNSY-1
pop hl
SC .ALUN
ret c
ld (delqio+Q.P1),hl ; set FDB
ld a,13+LUNSY-1
ld (delqio+Q.LUN),a ; LUN
add a,FIOEFN
ld (delqio+Q.EFN),a ; event flag number
ld hl,delqio
SC .QIO ; .QIOW
ret c ; if queuing failed
ld a,(QIOSB)
or a ; check result
ret z
scf
ret
; Rename file, DE = address of old FNB, HL = address of new FNB.
FRENAM: call SAVREG
push hl
push de
ld de,F.DEV
add hl,de
ld e,(hl)
inc hl
ld d,(hl)
inc hl
ld c,(hl)
ld b,13+LUNSY-1
SC .ALUN
pop de
pop hl
ret c
ld (renqio+Q.P1),de ; set old FDB
ld (renqio+Q.P2),hl ; set new FDB
ld a,13+LUNSY-1
ld (renqio+Q.LUN),a ; LUN
add a,FIOEFN
ld (renqio+Q.EFN),a ; event flag number
ld hl,renqio
SC .QIO ; .QIOW
ret c ; if queuing failed
ld a,(QIOSB)
or a ; check result
ret z
scf
ret
;-----------------------------------------------------------------------
; --- CAT
defpat: db 'SY0:*.*;*'
patlen equ $ - defpat
CATALOG:call SAVREG
call CHKFIL ; get filename argument
ld iy,(SYSBUF) ; use SYSBUF for I/O
ld a,b ; check string length
or c
jr nz,cat1 ; branch if filespec given
ld de,defpat ; else default to all files
ld bc,patlen
cat1: ld ix,-(FDBSZ+4)
add ix,sp ; alloc FDB on stack
ld sp,ix
ex de,hl
ld (fptr),hl
ld a,c
ld (fcnt),a
call PFN
jr c,cate1
ld a,(fcnt)
or a
jr z,cat2
cate1: rst 10h
db 1Bh ; illegal file specification
cat2: call prep ; prepare file matching pattern
call dir ; display directory
jr c,cate2
ld hl,FDBSZ+4
add hl,sp
ld sp,hl
jp READY
cate2: rst 10h
db 13h ; channel I/O error
dir: push ix
pop hl
ld bc,F.DIR
add hl,bc ; HL = directory name field
ld a,(ix+F.ATTR)
and FN.DIR ; directory name specified?
jr nz,dir14 ; use it if yes
; else use current dir
ld hl,cdname ; use cdname as dst buffer
ld c,GD.TSK
SC .GDIR ; get current dir name
ret c
ld a,(hl)
or a ; current dir set?
jr nz,dir12 ; jump if yes
ld hl,mdfn ; else default to master dir
dir12: ; here we are with IX = file descriptor block and HL = dir name.
; we just need to set the directory name in the file descriptor
; and setup the flags accordingly.
dir14: call prepfb ; prepare file descriptor
ld (ix+F.ATTR),FN.DIR ; set ONLY the directory bit
push ix
pop hl
ld a,13 ; use system channel
ld (T4),a
ld c,0 ; read-only mode
call FOPEN ; open directory
ret c
ld l,(iy+BUFAD) ; get buffer address
ld h,(iy+BUFAD+1)
push hl
ld (ratqio+Q.BUF),hl
ld a,(ix) ; get LUN
ld (ratqio+Q.LUN),a
add a,FIOEFN
ld (ratqio+Q.EFN),a
ld hl,ratqio
SC .QIO ; retrieve file info
pop de
jr c,dir20
ld a,(QIOSB)
or a
jr nz,dir20 ; should not happen... (dir just got deleted?)
ld hl,F.ATTR
add hl,de ; index into buffer
ld a,(hl) ; check F.ATTR
and FA.DIR ; is a directory?
jr nz,dir21 ; jump if yes
dir20: ld c,(ix) ; else close directory
call FCLOS1
ld a,E.NODIR ; and print error message
scf
ret
dir21: call crlf
IF 1
;; call MSG
;; db 'DIRECTORY ',0
ld hl,F.DEV
add hl,de
ld a,(hl)
call PUTCHR ; display device name
inc hl
ld a,(hl)
call PUTCHR
inc hl
ld a,(hl)
ld l,a
ld h,0
call HLDEC
inc hl
ld a,':'
call PUTCHR
ld a,'['
call PUTCHR
ld hl,F.NAME
add hl,de
ld b,9
call prname ; display directory name
ld a,']'
call PUTCHR
call crlf
call crlf
ENDIF
xor a
ld (iy+BLKNO),a
ld (iy+BLKNO+1),a
dir3: ; main loop
ld c,(iy+BLKNO) ; dir block #
ld b,(iy+BLKNO+1)
ld e,(iy+BUFAD) ; buf address
ld d,(iy+BUFAD+1)
push de
push ix
pop hl ; FDB
call FREAD ; read directory entry
ld bc,0
call nc,FWAIT
pop hl ; HL = buf address
ld e,a
ld a,b