-
Notifications
You must be signed in to change notification settings - Fork 3
/
rsxio.mac
1112 lines (940 loc) · 21.7 KB
/
rsxio.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
;**********************************************************************;
; ;
; This file is part of ZSM4, a Z80/Z180/Z280 relocatable macro- ;
; assembler written in Z80 assembly. ;
; Copyright (C) 2017-2023, Hector Peraza. ;
; ;
; This program is free software; you can redistribute it and/or ;
; modify it under the terms of the GNU General Public License as ;
; published by the Free Software Foundation; either version 2 of ;
; the License, or (at your option) any later version. ;
; ;
; This program is distributed in the hope that it will be useful, ;
; but WITHOUT ANY WARRANTY; without even the implied warranty of ;
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;
; GNU General Public License for more details. ;
; ;
; You should have received a copy of the GNU General Public License ;
; along with this program; if not, write to the Free Software ;
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ;
; ;
;**********************************************************************;
TITLE Z80 Assembler - System-dependent routines - RSX180 version
.Z80
include ZSM.INC
include SYSFN.INC
include FCB.INC
include GCML.INC
include TCB.INC
include FCSLIB.INC
include ERRORS.INC
; rqst SYSLIB,FCSLIB
public GNB,WNB,WNB2,CLINE,WLINE,REWIND,CLSINP
public CLOSE1,CLOSE2,OPNLIB,CLSLIB,INCLVL,INCMEM
extrn Z80ASM,HOFNAM,LFLAG,OFLAG,QFLAG,ERRFLG,CMPHD
extrn MAXMEM,SYMTBL,VALID,MALLOC,MFREE,FNDREC,NAMLEN
extrn FNDNXT,DSPTR,PTR1,HOFDAT,HOFTIM,CNV2HX,UMODE
extrn ZERODS,DEFCPU,RADIX,CMNPTR,ERRCNT
extrn GCML,RCML,UCASE,PERROR,TTFLSH,PUTCH,PUTSTR
extrn PFN,FOPEN,FCREAT,FGETCH,FPUTCH,FCLOSE,FPOS
extrn INIFCB,FSEEK,HLDEC,BCD2BIN,RCTRLO
ESC equ 1Bh
LUNCML equ 1 ; LUN for command file
LUNOBJ equ 2 ; LUN for object file
LUNLST equ 3 ; LUN for listing file
LUNMAC equ 6 ; LUN 6 is used for source file
; and 7,8,9,10,11 for INCLUDE files
cseg
;-----------------------------------------------------------------------
SUBTTL Initializations
; Command syntax is as follows:
;
; MAC [objfile][,[lstfile]]=srcfile[/option][/option...]
; or
; MAC @cmdfile
;
; where option can be:
;
; /L force generation of listing file
; /Sn set max length of symbols in REL file (default = 6)
; /U assume all Undefined symbols are External
; /M initialize DEFS data areas to zero
; /Zn set initial processor type to Z80, Z180 or Z280
; (n=0, 1 or 2 respectively; defaults to 0 = Z80)
; /Iddn:[dir] define an include file search path
; /Dsym[=val] define symbol and optionally assign a value to it
START: ld sp,STACK
call INIT ; get task name and set prompt
m1: call TTFLSH
ld iy,GDAT
call GCML ; get command line tail
jr c,m2 ; jump on error
ld (PTR1),hl ; save pointer to command tail
call PROCESS ; process the command
call UPDEXC ; update program exit code
jr m1 ; loop for more
m2: cp E.EOF ; end of command file?
jr z,m3 ; exit normally if yes
call CMLERR ; else display error
ld iy,GDAT
call RCML ; reset command state
jr m1 ; and loop
m3: call TTFLSH
ld hl,(EXITC) ; fetch exit code
SC .EXIT ; exit
;-----------------------------------------------------------------------
; Get task name and setup default prompt
INIT: ld hl,0
ld de,GTKBUF
SC .GTSK ; get task info
ld hl,GTKBUF+GT.NAME
call ckdots ; skip initial 3 dots if present
push hl
ld de,(GDAT+G.PRM) ; set prompt to our task name
inc de
inc de
ld bc,3
ldir
pop hl
ld de,errpfx+2 ; set task name in error message prefix
ld bc,3
ldir
ld hl,EX.SUC
ld (EXITC),hl ; default to 'success' exit code
ret
ckdots: ld b,3
push hl
ckd1: ld a,(hl)
cp '.'
jr nz,ckd2
inc hl
djnz ckd1
inc sp
inc sp
ret
ckd2: pop hl
ret
;-----------------------------------------------------------------------
; Print error message in the form 'MAC -- message'.
CMDERR: ld hl,snerr
ERROR: push hl
call RCTRLO ; resume output, if suspended
ld hl,errpfx
call PUTSTR ; output program name
pop hl
call PUTSTR ; output message
call ATLINE
ld c,CR
call PUTCH
call UPDERR ; update exit code
scf
ret
snerr: db 'Command syntax error',0
; Like the above one, but displays a standard error message.
SYSERR: push af
call RCTRLO ; resume output, if suspended
ld hl,errpfx
call PUTSTR ; output program name
pop af
call PERROR ; output system error message
call ATLINE ; and line number of command file
ld c,CR
call PUTCH
ld hl,EX.SEV ; system errors are always 'severe'
ld (EXITC),hl
scf
ret
; Display GCML or system error message
CMLERR: or a
jp m,SYSERR
ld hl,ERROR
push hl
cp GE.OK ; check for GCML codes
ld hl,msgok
ret z
cp GE.SN
ld hl,snerr
ret z
cp GE.LEN
ld hl,msglen
ret z
cp GE.NLV
ld hl,msgnlv
ret z
pop hl
jp SYSERR ; else is a system error
msgok: db 'No error',0
msglen: db 'Line too long in command file',0
msgnlv: db 'Command nesting level too deep',0
; Display line number of indirect file where error happened
ATLINE: ld hl,GDAT+G.ST
bit GS.FIL,(hl) ; command line active?
ret z ; return if not, command is from terminal
ld hl,(GDAT+G.LN)
ld a,h ; ensure line number is valid
or l
ret z
push hl
ld hl,atln
call PUTSTR
pop hl
xor a
jp HLDEC ; display line number and return
atln: db ' at line ',0
; Update exit code
UPDEXC: ld hl,(ERRCNT) ; get error count
ld a,h
or l ; any errors?
ret z ; no, return
UPDERR: ld hl,(EXITC) ; else get current exit code
ld a,l
cp EX.SEV ; severe error?
ret nc ; yes, return
ld hl,EX.ERR ; else set to error
ld (EXITC),hl
ret
;-----------------------------------------------------------------------
; Get next non-blank char. Returns Z flag set on end of line.
GETNB: ld a,(de)
call ISEOL
ret z
cp ' '
ret nz
inc de
jr GETNB
; Return Z if char in A is end-of-line.
ISEOL: or a
ret z
cp CR
ret z
cp LF
ret z
cp ESC
ret z
cp EOF
ret
;-----------------------------------------------------------------------
; Parse the command line, get file names and options
PARSE: ld hl,(SYMTBL)
ld (SYMPTR##),hl
ld (hl),0 ; reset symbol table
ld hl,(MAXMEM)
ld (DSPTR##),hl
ld (hl),0 ; reset dynamic memory storage
xor a ; setup default options
ld (UMODE),a ; Undefined symbols do not default to External
ld (ZERODS),a ; no DS zeroing
ld (DEFCPU),a ; CPU is Z80
ld (OPTSW),a
ld (INCDIR),a ; no additional include dir
ld a,6
ld (NAMLEN),a ; REL symbol length = 6
ld hl,10
ld (RADIX),hl ; default radix = 10 (for /D option)
ld hl,0
ld (CMNPTR),hl ; clear COMMON pointer (for /D option)
ld hl,(PTR1)
ld ix,OFNAME
call PFN ; parse object file name
jp c,CMDERR
ld ix,LFNAME
call INIFCB ; no listing file unless explicitly indicated
ld a,(hl) ; see where PFN stopped
cp ','
jr nz,par0
ld a,(OPTSW)
or 01h
ld (OPTSW),a
inc hl
call PFN ; parse listing file name
jp c,CMDERR
ld a,(hl)
par0: cp '='
jp nz,CMDERR ; must be equal sign
inc hl
ld ix,IFNAME
call PFN ; parse input file name
jp c,CMDERR
ld (PTR1),hl
par1: ld de,(PTR1)
par2: call GETNB ; get next non-blank char
call ISEOL ; end of line?
ret z ; return if yes
cp '/' ; switch?
jp nz,CMDERR ; error if not
inc de
ld a,(de) ; get switch
or a
jp z,CMDERR
inc de
ld (PTR1),de
call UCASE
cp 'L' ; /L (force listing)
jr z,swlst
cp 'Z' ; /Zcpu (default CPU type)
jr z,swcpu
cp 'D' ; /Dsymbol[=value] (define symbol)
jr z,swdef
cp 'S' ; /Sn (set max REL symbol length)
jr z,swlen
cp 'X' ; /X (generate cross-ref data)
jp z,swxrf
cp 'U' ; /U (assume undefined are externals)
jp z,swuex
cp 'M' ; /M (init DS to zeros)
jp z,swzds
cp 'I' ; /Idir (define INCLUDE dir)
jp z,swinc
jp CMDERR
swlst: ld a,(OPTSW)
or 02h
ld (OPTSW),a
jr par1
swcpu: ld a,(de)
sub '0'
jp c,CMDERR
cp 2+1
jp nc,CMDERR
ld (DEFCPU),a ; set CPU type
inc de
jr par2
swdef: ld (PTR1),de ; set text pointer for ID routine
call ID##
ld a,(IDLEN##)
or a
jp z,CMDERR ; symbol name required
call GNC##
cp '='
ld hl,0
jr nz,def0 ; if no value specified, assume 0
call INT## ; else convert value
ld hl,(INTBUF##)
def0: ld (VAL##),hl
ld a,GBLSYM ; symbol is Global
ld (SYMMOD##),a
call ADDSYM## ; enter symbol
; jp c,... ; error - can't enter symbol (unlikely)
; jp z,... ; error - multiple defined (ignore/redefine)
ld hl,(IDADR##)
ld a,(hl)
and 0Fh ; clear undef bit
add a,EQUNAME ; treat like EQU
ld (hl),a
jp par1
swlen: ld a,(de)
cp '5'
jp c,CMDERR
cp '8'+1
jp nc,CMDERR
sub '0'
ld (NAMLEN),a ; set REL symbol length
inc de
jp par2
swuex: ld a,1
ld (UMODE),a
jp par1
swzds: ld a,1
ld (ZERODS),a
jp par1
swinc: ld a,(INCDIR)
or a ; additional include path already specified?
jp nz,CMDERR ; error if yes
ld ix,-FNSZ
add ix,sp ; create temporary storage on stack
ld sp,ix ; for PFN call
ex de,hl
call PFN ; parse filespec
ld (PTR1),hl ; remember where we stopped
ld a,(ix+F.ATTR)
ld c,a
and NOT (FN.DEV OR FN.DIR)
jr nz,swinc3 ; only device name and/or directory are allowed
ld a,c
and FN.DEV OR FN.DIR
jr z,swinc3 ; and at least one of the two must be present
ld a,c
ld (INCDIR),a ; store flags
and FN.DEV ; device name specified?
ld a,c
jr z,swinc1 ; no, skip
push ix
pop hl
ld de,F.DEV
add hl,de
ld de,INCDIR+1
ld bc,3
ldir ; copy device name
swinc1: and FN.DIR ; directory specified?
jr z,swinc2 ; no, return
push ix
pop hl
ld de,F.DIR
add hl,de
ld de,INCDIR+1+3
ld bc,9
ldir ; copy directory name
swinc2: ld hl,FNSZ ; restore stack
add hl,sp
ld sp,hl
jp par1 ; and continue
swinc3: ld hl,FNSZ
add hl,sp
ld sp,hl ; restore stack
jp CMDERR ; and report error
swxrf:
;...
jp par1
addhla: add a,l
ld l,a
ret nc
inc h
ret
chkeq: ld hl,(PTR1)
ld a,(hl)
cp '='
ret nz
inc hl
ld (PTR1),hl
ret
;----------------------------------------------------------------------
; Process the command
PROCESS:
; initialize variables
ld hl,(GTKBUF+GT.END)
;; dec hl
ld (MAXMEM),hl ; save max memory address
ld hl,($MEMRY)
ld (SYMTBL),hl ; save start of symbol table
; ld (SYMPTR),hl
ld iy,FFLAGS
ld (iy),0
; parse the command line and setup header fields
call PARSE
ret c ; return on error
; open input file
ld a,(IFNAME+F.ATTR)
and FN.EXT
jr nz,proc1
ld hl,MACEXT
ld de,IFNAME+F.EXT
ld bc,3
ldir
ld a,(IFNAME+F.ATTR)
or FN.EXT
ld (IFNAME+F.ATTR),a
proc1: ld ix,IOB1
ld a,LUNMAC
ld (ix+FC.LUN),a
ld (ix+FC.MODE),1 SHL FM.RD
call FOPEN
jp c,SYSERR
ld hl,CLSFIL
push hl
set 0,(iy) ; input file successfully open
; create object file
ld a,(OFNAME+F.ATTR)
or a ; output file specified?
jr nz,proc2 ; branch if yes
ld c,a
ld a,(OPTSW)
and 01h ; comma present (no object wanted)?
ld a,'Z'
ld (OFLAG),a
jr nz,proc5 ; skip object file creation if yes
ld a,'@'
ld (OFLAG),a
ld a,c
proc2: and FN.NAME ; name specified?
jr nz,proc3 ; branch if yes
ld hl,IFNAME+F.NAME
ld de,OFNAME+F.NAME
ld bc,9
ldir ; if no output name given use input.OBJ
ld a,(OFNAME+F.ATTR)
or FN.NAME
ld (OFNAME+F.ATTR),a
proc3: ld a,(OFNAME+F.ATTR)
and FN.EXT ; extension specified?
jr nz,proc4 ; branch if yes
ld hl,OBJEXT ; else use .OBJ
ld de,OFNAME+F.EXT
ld bc,3
ldir
ld a,(OFNAME+F.ATTR)
or FN.EXT
ld (OFNAME+F.ATTR),a
proc4: ld ix,IOB2
ld (ix+FC.MODE),1 SHL FM.WR
ld de,0
ld c,0
xor a ; non-contiguous file
call FCREAT ; create the file
jp c,SYSERR
set 1,(iy) ; object file successfully created
; create listing file
proc5: ld a,(LFNAME+F.ATTR)
or a ; listing file specified?
jr nz,proc6
ld c,a
ld a,(OPTSW)
and 02h ; /L switch specified?
ld a,'Z'
ld (LFLAG),a
jr z,proc9 ; skip listing file creation if not
ld a,'@'
ld (LFLAG),a
ld a,c
proc6: and FN.NAME
jr nz,proc7
ld hl,IFNAME+F.NAME
ld de,LFNAME+F.NAME
ld bc,9
ldir ; if no output name given use input.LST
ld a,(LFNAME+F.ATTR)
or FN.NAME
ld (LFNAME+F.ATTR),a
proc7: ld a,(LFNAME+F.ATTR)
and FN.EXT
jr nz,proc8
ld hl,LSTEXT
ld de,LFNAME+F.EXT
ld bc,3
ldir
ld a,(LFNAME+F.ATTR)
or FN.EXT
ld (LFNAME+F.ATTR),a
proc8: ld ix,IOB3
ld (ix+FC.MODE),1 SHL FM.WR
ld de,0
ld c,0
xor a ; non-contiguous file
call FCREAT
jp c,SYSERR
set 2,(iy) ; listing file successfully created
; header line message
proc9: ld hl,IFNAME+F.NAME
ld de,HOFNAM
ld bc,9
ldir ; set file name
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,HOFDAT ; address to store date
ld a,(ix+3) ; day
call CNV2HX
ld a,(ix+2) ; month
call BCD2BIN
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 CNV2HX
ld a,(ix+1) ; year
call CNV2HX
ld hl,HOFTIM ; address to store time
ld a,(ix+4) ; hours
call CNV2HX
ld (hl),':'
inc hl
ld a,(ix+5) ; min
call CNV2HX
ld (hl),':'
inc hl
ld a,(ix+6) ; sec
call CNV2HX
dt1: ld hl,8
add hl,sp
ld sp,hl ; cleanup stack
xor a
ld (INCLVL),a
dec a
ld (QFLAG),a
pop hl ; the assembler will call the
; file close functions
jp Z80ASM ; call the assembler and return
CLSFIL: bit 0,(iy) ; input file open?
jr z,clsf1
ld ix,IOB1 ; yes, close it
call FCLOSE
clsf1: bit 1,(iy) ; object file open?
jr z,clsf2
ld ix,IOB2 ; yes, close it
call FCLOSE
clsf2: bit 2,(iy) ; listing file open?
ret z
ld ix,IOB3 ; yes, close it
call FCLOSE
ret
MACEXT: db 'MAC'
OBJEXT: db 'OBJ'
LSTEXT: db 'LST'
MONTHS: db '-Jan-Feb-Mar-Apr-May-Jun-Jul-Aug-Sep-Oct-Nov-Dec-'
SUBTTL Console, Printer and File I/O
; WLINE - Write line to list device and append newline
; HL -> buffer, term = null
WLINE: call PUTSTR
ld c,LF
call PUTCH
ld c,CR
jp PUTCH
; CLINE - Write line to console and append newline
; HL -> buffer, term = null
CLINE: ld c,LF
call PUTCH
call PUTSTR
ld c,CR
jp PUTCH
; GNB - Get next byte (IOB1)
GNB: push ix
push hl
push de
push bc
ld ix,IOB1
call FGETCH
jr nc,GNB1
cp E.EOF
call nz,SYSERR
ld a,EOF
GNB1: and 7Fh ; strip parity bit
bit FM.TT,(ix+FC.MODE)
jr z,GNB2
cp CR
jr nz,GNB2
ld a,LF
GNB2: pop bc
pop de
pop hl
pop ix
ret
; REWIND - Rewind input file
REWIND: ld a,(INCLVL)
or a
jr z,REW1
call CLSLIB ; close any open INCLUDE files
jr REWIND
REW1: push ix
ld de,0
ld bc,0
ld ix,IOB1
call FSEEK
call c,SYSERR
pop ix
ret
; CLSINP - Close input file
CLSINP: ld a,(INCLVL)
or a
jr z,CLS0
call CLSLIB ; close any open INCLUDE files
jr CLSINP
CLS0: ld ix,IOB1
jp FCLOSE
; WNB - Write next byte (LST)
; Byte in A reg
WNB: ld iy,FFLAGS
bit 2,(iy)
scf
ret z
push ix
and 7Fh ; strip parity bit
ld ix,IOB3
call FPUTCH
pop ix
ret nc
res 2,(iy)
jp SYSERR
; WNB2 - Write next byte (OBJ)
WNB2: ld iy,FFLAGS
bit 1,(iy)
scf
ret z
push ix
ld ix,IOB2
call FPUTCH
pop ix
ret nc
res 1,(iy)
jp SYSERR
; CLOSE1 - Close LST file
CLOSE1: push ix
ld ix,IOB3
jr CLOSE
; CLOSE2 - Close OBJ file
CLOSE2: push ix
ld ix,IOB2
CLOSE: call FCLOSE
call c,SYSERR
pop ix
ret
; OPNLIB - Open MACLIB or INCLUDE file
; HL points to file name
;
; Input file structure as saved in high memory:
;
; db STINPF ; type = include/input file state
; dw len ; length of the following fields:
; ds 4 ; current file position
; ds 1 ; current FC.MODE
; db lun ; current LUN
; ds FINFSZ ; current file name block
OPNLIB: ld a,(INCLVL)
cp 5 ; check nested include level
jp nc,FNERR1 ; error if above maximum
push hl
ld bc,4+1+1+FINFSZ ; file pos + LUN + mode + file name
ld e,STINPF ; type = file
call MALLOC ; allocate block
pop de
ret c ; on error, return
push hl ; save block address
push de ; save pointer to file name
ld ix,IOB1
call FPOS
call c,SYSERR
ld (hl),c ; store source file position
inc hl
ld (hl),b
inc hl
ld (hl),e
inc hl
ld (hl),d
inc hl
ld a,(IOB1+FC.MODE)
ld (hl),a ; store FC.MODE
inc hl
ld a,(IOB1+FC.LUN)
ld (hl),a ; store LUN
inc a
ld (IOB1+FC.LUN),a ; new LUN
inc hl
ex de,hl ; DE = address to store FDB
ld hl,IFNAME
ld bc,FINFSZ
ldir ; store FDB
pop hl ; restore ptr to filename
ld ix,IFNAME ; FDB address in IX
call PFN ; parse file name
push hl
jp c,FNERR ; bad file name
ld a,(ix+F.ATTR)
and FN.EXT
jr nz,OPN1
ld de,IFNAME+F.EXT
ld hl,MACEXT ; default include extension is .MAC
ld bc,3
ldir
OPN1: ld ix,IOB1
ld (ix+FC.MODE),1 SHL FM.RD
call FOPEN ; open file
jr nc,OPN4 ; branch on success
ld a,(IFNAME+F.ATTR)
and FN.DEV OR FN.DIR ; include file with explicit device and/or dir?
jr nz,FNERR ; yes, error
ld a,(INCDIR)
or a ; search include path specified?
jr z,FNERR ; no, return error
ld c,a
and FN.DEV ; search path specifies a device?
ld a,c
jr z,OPN2 ; no, try directory
ld hl,INCDIR+1
ld de,IFNAME+F.DEV
ld bc,3
ldir ; else copy device name
ld c,a
ld hl,IFNAME+F.ATTR
ld a,(hl)
or FN.DEV ; set bit in attributes field
ld (hl),a
ld a,c
OPN2: and FN.DIR ; search path specifies a directory?
jr z,OPN3 ; no, skip
ld hl,INCDIR+1+3
ld de,IFNAME+F.DIR
ld bc,9
ldir ; else copy directory name
ld hl,IFNAME+F.ATTR
ld a,(hl)
or FN.DIR ; set bit in attributes field
ld (hl),a
OPN3: ld (ix+FC.MODE),1 SHL FM.RD
call FOPEN ; try again
jr c,FNERR ; no can do
OPN4: pop hl
ex (sp),hl ; drop block address, save record pointer
ld hl,INCLVL
inc (hl)
pop hl ; restore record pointer
or a
ret
FNERR: pop hl
ex (sp),hl ; restore block address, save record pointer
push hl ; push block address
ld de,4
add hl,de
ld a,(hl)
ld (IOB1+FC.MODE),a
inc hl
ld a,(hl)
ld (IOB1+FC.LUN),a ; restore old LUN
inc hl
ld de,IFNAME
ld bc,FINFSZ
ldir ; restore old FDB
pop hl ; pop block address
dec hl
dec hl
dec hl
call MFREE ; free allocated block
pop hl ; restore record pointer
FNERR1: ld a,'V'
ld (ERRFLG),a ; set error flag
scf
ret
; CLSLIB - Close current MACLIB or INCLUDE file
CLSLIB: ld a,(INCLVL)
or a
ret z ; nothing to do, no INCLUDEs active
dec a
ld (INCLVL),a
ld ix,IOB1
call FCLOSE ; close current file
ld c,STINPF
call FNDREC ; get previous file record
; jp c,... ; should not happen
push hl ; save start of file record
inc hl
inc hl
inc hl
ld c,(hl) ; get starting file position
inc hl
ld b,(hl)
inc hl
ld e,(hl)
inc hl
ld d,(hl)
inc hl
ld a,(hl) ; get FC.MODE
res FS.BV,a ; buffer is no longer valid
ld (IOB1+FC.MODE),a
inc hl
ld a,(hl) ; get LUN
ld (IOB1+FC.LUN),a
inc hl
push bc
push de
ld de,IFNAME
ld bc,FINFSZ
ldir ; restore FDB
pop de
pop bc
ld ix,IOB1
call FSEEK ; restore prev file position
call c,SYSERR
pop hl ; pop start of record
call MFREE ; release storage
or 0FFh ; NZ
ret