-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbasmain.mac
2224 lines (1968 loc) · 48.6 KB
/
basmain.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 Main section, File I/O
.Z80
; Z80 port by Hector Peraza, 2016-2020
ident 'V02-03'
include BASDEF.INC
include BASTKN.INC
include ERRORS.INC
CR equ 0Dh
LF equ 0Ah
TAB equ 09h
public START,VERSION,CPHLDE,CPIXBC,ADDHLA,READY,FLINE
public FLINEN,FNDSTR,FRESTR,FNDSTL,CHKFIL,SKPSYM,LITEVAL
public SKPEOL,SKPOBJ,SKIPBL,SKPLIN,SAVCHR,SAVCH1,DISPAT
public ALLOC,CKCTLC,CCSTAT,VFBLK,VFBLK1,DNPACK,VARSIZ
public CHKISE,CHKOSE,FILEA,CLOSYS,CLOSCH,CLOSALL,GETCHR
public RDBLK,LINGET,ODEVTT,OPNSYS,OPNFIL,INITPG,PUTCHR
public WRBLK,SETCOL,PURGALL,BYE,INIRD,NAMSET,RDNXBL
public RDVFBL,WRVFBL,DELETE,NAMETO,MSG,MSG1,DOMSG,MSGHL
public SAVREG,ISDIG,ISLETR,NEGHL,NEGBC,CHKBFP,CPDEHL
public CPBCHL,CLRFNB
extrn INIT,STOP,DATIM,EDIT,INTEVAL,CLRFAC,BYENTR
extrn $TTIN,$TTOUT,$FLUSH,$FLINP,$EXIT,RDYNTR,EOFNTR
extrn DIMUL,$POLSH,$UNPOL,FOPEN,FCLOSE,FREAD,FWRITE
extrn FCREAT,CMPEXT,ADDEXT,EVAL,UACTST,$CHKCC,RCTLO
extrn FRESET,FDELET,FRENAM,FVERS,FWAIT
;-----------------------------------------------------------------------
cseg
START:: jp INIT
VERSION:db 'V02-03 ',0
READY: ld sp,(STK) ; restore SP to the saved initial value
xor a
ld (CHNFLG),a ; clear CHAIN/OVERLAY flag
ld hl,0
ld (EDITLN),hl
ld (CPSAVE),hl
call IREADY ; flush terminal input
call RDYNTR ; empty routine in BASCLI
call MSG ; display prompt
db CR,LF,'READY',CR,LF,0
ld hl,0
ld (IDEV),hl ; clear current input channel descriptor
jp EDIT
FLINE: ld a,(hl) ; get next program byte
or a
scf
ret m ; if token -> return with CY set
inc hl
ld b,a
ld c,(hl) ; BC now contains the next 2 program bytes
inc hl ; (note order!)
bit 0,c ; odd value (offset to line number)?
scf
ret z ; return if not
dec bc ; else decrement to fix offset (make even)
push hl
ld hl,(LINTAB)
add hl,bc ; index into line number table
ld c,(hl) ; return line number in BC
inc hl
ld b,(hl)
inc hl
ex de,hl ; return ptr to LINTAB+2 in DE
pop hl
ret
FLINEN: call SAVREG ; save BC,DE,HL,IX,IY
ld hl,(CODE)
ld de,(CPSAVE)
call CPHLDE
jr nc,fln1
ld hl,0FFFFh ; -1
ld (FAC2),hl
ret
fln1: call FLINE
jr c,fln2
call CLRFAC ; clear FP accum
ld (FAC2),bc ; store line number
fln2: call SKPEOL ; find end of statement
ld de,(CPSAVE)
call CPHLDE
jr nc,fln1 ; loop
ret
; Evaluate string expression.
; Returns DE = pointer to string, C = length
FNDSTR: call EVAL ; evaluate expression
jr nc,sne1 ; if not a string expression -> syntax error
call FRESTR
ld c,0
ld a,d
or e
ret z
dec de
ld a,(de)
ld c,a
inc de
inc de
inc de
ret
sne1: rst 10h
db 06h ; syntax error
; Free string by clearing backpointer.
FRESTR: pop bc ; get return address into BC
pop de ; pop pointer to string address into DE
push bc ; push return address back
inc de
ld a,d
or e
ret z ; return if null string
xor a
ld (de),a ; clear backpointer
inc de
ld (de),a
dec de
ret
; Find string end and calculate length
FNDSTL: ld a,(hl)
cp T.DBLQ ; " token
jr z,fstl1
cp T.SNGQ ; ' token?
jr nz,sne1 ; no -> syntax error
fstl1: call CHKFIL ; find string end
inc hl ; skip ending quote token
ret
CHKFIL: inc hl ; skip quote token and "text" token
inc hl
FNDETX: ld e,l ; save begin of string in DE
ld d,h
fnx1: ld a,(hl) ; search for ending zero-byte
inc hl
or a
jr nz,fnx1
push hl
sbc hl,de ; note CY is clear from above
ld c,l
ld b,h
pop hl
dec bc ; BC = string length
ld a,b
or c ; set Z/NZ condition
ret
; Skip to next variable
SKPSYM: push bc
call VARSIZ ; get length of entry into BC
add hl,bc
pop bc
ret
; Return in BC the length of symbol table entry for variable in (HL).
VARSIZ: ld a,(hl)
cp 04h ; check variable type
ld bc,10
ret nc ; if >= 4 (array)
cp 1+1
ld bc,6
ret c ; if <= 1 (integer or string)
ld bc,8
ret
; Allocate space for I/O buffer, BC = size in bytes.
; Returns HL = buffer address.
FREGET: call DNPACK
inc bc
res 0,c ; ensure size is even
ld hl,(HIFREE)
or a
sbc hl,bc
ret c ; no space
ex de,hl
ld hl,(HISTR)
call CPHLDE ; HIFREE - BC < HISTR?
ret c ; return if yes
ex de,hl
ld (HIFREE),hl ; else set new HIFREE
or a
ret
IFDEF CALLS
GTFTAB: ld bc,(FTABI)
ld a,b
or c
ret
ENDIF
INITPG: call SAVREG ; save BC,DE,HL,IX,IY
call PURGALL ; purge all channels
call FRESET ; reset disk system
ld hl,0
ld (BUFCHN),hl ; clear list of free buffers
ld (GSBCTR),hl ; clear GOSUB depth
ld hl,034D9h
ld (RND1),hl ; initial RND() seed = 1.01468E-07
ld hl,0E6A9h
ld (RND2),hl
ld hl,(PDL)
ld a,0FFh ; -1
ld (hl),a ; clear push-down list
inc hl ; (first entry = 0FFFFh)
ld (hl),a
inc hl
ex de,hl
ld hl,(LIMIT)
or a
sbc hl,de ; TODO: check for <= 0?
ex de,hl
iclr: ld (hl),0
inc hl
dec de
ld a,d
or e
jr nz,iclr
ld hl,(LINTAB)
ld de,(DEFTAB) ; DE = begin of user function table
undf: call CPHLDE ; continue until LINTAB reached
ret nc
inc de
inc de
ld a,0FFh
ld (de),a ; store -1 (undefine function)
inc de
ld (de),a
inc de
jr undf
; Load "tokenized" number into FP accum (evaluate literal).
; A = token value, HL = ptr to program line.
LITEVAL:call CLRFAC ; clear FP accum, preserves A
cp 0FFh ; floating point number follows? T.???
jr z,lit1 ; yes -> store it into FP accum
cp 0FCh ; "explicit" integer? T.???
jr z,lit2
cp 0FEh ; or "normal" integer? T.???
jr z,lit2 ; yes, load it HI-first
jr lit3 ; otherwise load a single byte
lit1: ld a,(hl)
inc hl
ld (FAC1+1),a
ld a,(hl)
inc hl
ld (FAC1),a
lit2: ld a,(hl)
inc hl
ld (FAC2+1),a
lit3: ld a,(hl)
inc hl
ld (FAC2),a
ret
; Reset PRINT output pointer
ODEVTT: push hl
ld hl,0
ld (ODEV),hl ; clear current output channel descriptor
ld hl,CLMNTT
ld (COLUMN),hl
pop hl
ret
; Save (and restore later) the CPU registers BC,DE,HL,IX,IY. Note that AF
; is not saved, since some routines use the CY flag to return status.
SAVREG: ex (sp),hl ; push hl, pop ret address = func+3
push ix
push iy
push de
push bc
push hl ; push <func+3>
ld hl,restor
ex (sp),hl ; push <restor>, pop <func+3>
push hl ; push <func+3>
push af
ld hl,14 ; af,func+3,restor,bc,de,iy,ix,hl,func+3
add hl,sp ; ^^
ld a,(hl)
inc hl
ld h,(hl)
ld l,a ; restore hl from stack
pop af
ret ; co-program call to <func+3>
restor: pop bc
pop de
pop iy
pop ix
pop hl
ret
; Find end of statement.
SKPEOL: ld a,(hl)
cp T.EOL ; '\' token?
jr z,skip1 ; increment HL and return if yes
call SKPOBJ ; skip token and its arguments
jr SKPEOL ; loop
; Skip object.
SKPOBJ: ld a,(hl)
inc hl
or a ; a token?
jp p,skip1 ; increment HL and return if not
dec hl
cp T.TEXT ; token was "literal" or number?
jr nc,skplit ; if yes, brach to skip it
inc hl
cp T.FN ; is it FN?
jr z,skip1 ; increment HL and return if yes
dec hl
cp T.NEXT ; was it NEXT?
jr z,skip11 ; skip the next 11 bytes and return if yes
cp T.CALL ; is it CALL?
jr z,skip4 ; skip the next 4 bytes if yes
skip1: inc hl
ret
skip5: inc hl
skip4: inc hl
inc hl
skip2: inc hl
inc hl
ret
skip11: ld bc,11
add hl,bc
ret
; "literal" or number
skplit: jr z,skpnz ; if "literal", skip until null found
cp 0FFh ; float literal token?
jr z,skip5
cp 0FBh ; T.???
jr z,skip2
cp T.LBYT ; literal byte?
inc hl
jr z,skip1 ; skip byte
jr skip2 ; else skip word
skpnz: ld a,(hl)
inc hl
or a
jr nz,skpnz
ret
; Skip blanks
skip: inc hl
SKIPBL: ld a,(hl)
cp ' ' ; space?
jr z,skip ; yes, skip
cp TAB ; TAB?
jr z,skip ; yes, skip it too
ret
; Skip to the start of next program line.
SKPLIN: call SKPEOL ; skip to end of statement
ld a,(hl) ; get program code byte
ld d,a
or a
jp m,skpt ; jump if a token
inc hl
ld e,(hl) ; else get object offset into DE (note order!)
dec hl
bit 0,e ; even?
jr z,SKPLIN ; jump if yes (not a program line)
dec de
push hl
ld hl,(LINTAB)
add hl,de ; index into line number table
ex de,hl ; return table entry in DE
pop hl
or a
ret
skpt: cp T.EOF ; end of program?
jr nz,SKPLIN ; loop if not
scf ; else return with CY set
ret
; Char output routine for PRINT function: store char @T2, incr column @T1.
SAVCHR: nop ; see NUMSGN
SAVCH1: push hl
ld hl,(T2)
ld (hl),a ; store char
inc hl ; advance pointer
ld (T2),hl
ld hl,T1
inc (hl) ; increment count
pop hl
ret
;-----------------------------------------------------------------------
; Alloc array space from free space.
; IX points to variable, array limits in SS1MAX and SS2MAX.
; HL and DE preserved.
ALLOC: call UACTST ; in CALLs module
push hl
push de
call GETSIZ ; get number of bytes needed into DE
ld hl,(HIFREE)
or a
sbc hl,de
jr c,atl1 ; -> arrays too large
push hl ; remember new limit
push de
ld de,(HISTR)
call CPDEHL
pop de
jr c,atl1 ; if DE > HISTR -> arrays too large
ld (ix+2),l ; store array address
ld (ix+3),h
push hl
ld hl,(SS1MAX)
ld (ix+4),l ; store limits
ld (ix+5),h
ld hl,(SS2MAX)
ld (ix+6),l
ld (ix+7),h
pop hl
ld a,(ix+0) ; get variable type
and 03h
IF 0
ld bc,0 ; if numeric, initialize to 0
jr nz,alloc2
dec bc ; if string, initialize to null strings (-1)
jr alloc2
alloc1: ld (hl),c ; initialize value
inc hl
ld (hl),b
inc hl
alloc2: push de
ld de,(HIFREE)
call CPDEHL
pop de
jr c,alloc1 ; loop to initialize all array elements
ELSE
ld c,0 ; if numeric, initialize to 0
jr nz,alloc1
dec c ; if string, initialize to null strings (-1)
alloc1: ld (hl),c ; initialize value
inc hl
dec de
ld a,d
or e
jr nz,alloc1 ; loop to initialize all array elements
ENDIF
pop hl ; restore new limit
ld (HIFREE),hl ; set new HIFREE
pop de
pop hl
ret
atl1: rst 10h
db 04h ; arrays too large
; Get array size in bytes.
; IX = variable address, array limits in SS1MAX and SS2MAX.
; Return value in DE, HL and BC preserved.
GETSIZ: push hl
push bc
ld bc,(SS1MAX)
inc bc ; one extra element (arrays are 0-based)
ld hl,(SS2MAX)
gtsz1: inc hl
ld a,h
or l
jr z,gtsz1 ; if 2nd dimension was -1 make it 1
ld de,0 ; multiply HL*BC*2, result in DE
ld a,16 ;
gtsz2: add hl,hl ;
jr nc,gtsz3 ;
ex de,hl ;
add hl,bc ;
ex de,hl ;
jr c,atl1 ; overflow -> arrays too large
gtsz3: sla e ;
rl d ;
jr c,atl1 ;
dec a ;
jr nz,gtsz2 ; DE = 2 times # elements needed
ld a,(ix+0)
and 03h
jr z,gtsz4
and 01h
jr nz,gtsz4
sla e ; get number of bytes needed
rl d
jr c,atl1 ; overflow -> arrays too large
gtsz4: pop bc
pop hl
ret
; Pack strings towards low memory.
DNPACK: call SAVREG ; save BC,DE,HL,IX,IY
ld bc,0
push bc ; push a zero
ld ix,0
add ix,sp
ld bc,(LOSTR) ; get lo-str into BC
ld hl,(LOFREE) ; get lo-free into HL
ld (LOSTR),hl ; set lo-str = lo-free
jr dnp2
dnp1: pop de ; drop old length
ld a,(bc) ; get string length
inc bc
ld e,a
ld d,0
push de ; push new length
or a
jr nz,dnp4
dnp2: push hl
dnp3: ld e,c ; !!!write better
ld d,b
ld hl,(HISTR)
call CPHLDE ; BC < hi-str?
pop hl
jr c,dnp1 ; loop if yes
ld (HISTR),hl ; set new hi-str
pop bc ; restore stack
ret
dnp4: ld a,(bc) ; get string backpointer into DE
inc bc
ld d,a ; note order: first HI-byte
ld a,(bc)
inc bc
ld e,a ; then LO-byte
ld a,c
add a,(ix+0)
ld c,a
ld a,b
adc a,(ix+1)
ld b,a ; BC+3 += length
inc bc ; +1 (trailing length byte)
bit 0,e ; odd number? (i.e. offset to variable)
jr z,dnp5 ; jump if not (array element)
dec de ; fix offset
push hl
ld hl,(SYMBOL)
add hl,de ; index into symbol table
ex de,hl
pop hl
dnp5: push hl
ld hl,(PDL)
call CPHLDE
jr nc,dnp3 ; quit if DE >= PDL
ld hl,(ARRAYS)
call CPHLDE
jr nc,dnp3 ; quit if DE >= ARRAYS
ld hl,(HIFREE)
call CPHLDE
jr nc,dnp6 ; do if DE >= HIFREE
ld hl,(LOFREE)
call CPHLDE
jr nc,dnp3 ; quit if DE >= LOFREE
ld hl,(SYMBOL)
call CPHLDE
jr nc,dnp6 ; do if DE >= SYMBOL
push ix
pop hl
call CPHLDE
jr c,dnp3 ; quit if DE < SP
ld hl,(STK)
call CPDEHL
jr c,dnp3 ; if DE > STK
dnp6: pop hl
ld a,(ix+0)
add a,4
ld (ix+0),a
ld a,(ix+1)
adc a,0
ld (ix+1),a ; len += 4
ld a,c
sub (ix+0)
ld c,a
ld a,b
sbc a,(ix+1)
ld b,a ; BC -= len+4 (back to start of string) !!!write better
ld a,(de)
cp c
jr nz,dnp7 ; jump if BC != (DE) - backptr != varptr
inc de
ld a,(de)
dec de
cp b
jr nz,dnp7
ld a,l
ld (de),a ; store new string address in var block
inc de
ld a,h
ld (de),a
ld e,c ; DE = src
ld d,b
pop bc ; BC = len (here always > 0)
push bc
ex de,hl ; HL = src, DE = dst
ldir
ex de,hl ; HL = dst + len
ld c,e ; BC = src + len
ld b,d
jp dnp2 ; loop
dnp7: ld a,c
add a,(ix+0)
ld c,a ; BC += len...
ld a,b
adc a,(ix+1)
ld b,a
jp dnp2 ; loop
;-----------------------------------------------------------------------
CCSTAT: ld a,(CCFLG) ; ^C disabled?
or a
ret z ; no, return
; fall thru to check ^C status
; check ^C status bits, return CY if ^C was detected.
CCBITS: call $CHKCC
ld a,(CCFLG+1) ; check ^C status
or a
ret p ; if no ^C signaled return with no CY set
xor a
ld (CCFLG+1),a ; clear ^C signaled bit
scf ; set carry flag
ret
; Flush keyboard input if ^C was detected, return CY if ^C happened.
CKCTLC: ld a,(CCFLG) ; if ^C disabled, return
or a
ret nz
call CCBITS ; check ^C status
ret nc ; return if no ^C signaled
call $FLINP
scf
ret
;-----------------------------------------------------------------------
; Compute block number of virtual array file and offset to array
; element.
; Called with IX = chan descr address, DE = elem size
; FP accum = long index of elem
; Returns DE = block# BC = offset IY = buffer descr address
VFBLK: push hl
push de ; push item size
call $POLSH ; enter polish mode
dw DIMUL ; compute itemsize * index
dw $UNPOL ; leave polish mode
call VFBLK1 ; get block number and offset
pop hl
push bc
ld c,(ix+2)
ld b,(ix+3)
push bc
pop iy
call CKVLIM ; ensure block is within limits
pop bc
ret ; return CY if DE > HIBLK
; Return CY if DE > (iy+HIBLK)
CKVLIM:
IF 0
ld a,(iy+BUFEND)
cp d
ret nz
ld a,(iy+BUFEND+1)
cp e
ret
ELSE
or a
ret
ENDIF
; Divide 32-bit number on (SP=l):(SP+2=h) by 512
; Returns BC = n%512, DE = n/512
VFBLK1: pop hl ; pop return address
pop bc
pop de
push hl ; restore return address
ld d,e
ld e,b
ld a,b
and 01h
ld b,a ; BC = n%512
srl d ; DE = n/256/2 = n/512
rr e
ret
;-----------------------------------------------------------------------
; ---- < File I/O > ----
CHKISE: ld bc,4000h ; mode = open for read only
push bc
ld bc,IDEV ; BC = &IDEV
jr chk1
CHKOSE: ld bc,2000h ; mode = open for write only
push bc
ld bc,ODEV ; BC = &ODEV
chk1: push bc
call INTEVAL ; evaluate integer expression
ld de,(FAC2) ; DE = result (channel number)
ld a,d
or e
jr z,chk2 ; return if zero
push de ; push channel number
push hl ; push HL
ex de,hl ; get channel number into HL
call FILEA ; get channel descriptor address
ld a,(ix+1) ; test MSB (virtual array bit)
or a
jp m,icnerr ; if set -> illegal channel number
ld a,(ix+2) ; check buffer descriptor address
or (ix+3)
jr z,cnoerr ; if zero -> channel not open
pop hl ; restore HL
pop de ; pop channel number
pop bc ; BC = &IDEV or &ODEV
ex (sp),hl ; push HL, pop mode bit mask
ld a,(ix+1)
and h ; check direction
pop hl ; restore HL
jr z,direrr ; error if not the same
push ix
ex (sp),hl ; push HL, pop channel descr address
ld a,l
ld (bc),a ; store channel descr address in IDEV/ODEV
inc bc
ld a,h
ld (bc),a
pop hl ; restore HL
ret
chk2: pop bc ; restore stack
pop bc
ret
cnoerr: rst 10h
db 15h ; channel not open
icnerr: rst 10h
db 16h ; illegal channel number
direrr: rst 10h
db 18h ; illegal I/O direction
; Get channel descriptor address into IX from channel number in HL
FILEA: ld (T4),hl ; T4 = channel number
ld a,h
or l
jr z,icnerr ; channel number can't be zero
dec hl
add hl,hl ; *2
add hl,hl ; *4
ld de,13*4
call CPHLDE ; > 52? (chan# was > 13?)
jr c,icnerr ; if yes, error -> illegal channel number
ex de,hl
ld ix,SEQFIL
add ix,de ; IX = SEQFIL + 4*chan#
ret
; Channel I/O error routine, purge channels before calling the error handler.
IOERR: call PURGALL ; purge all channels
rst 10h
db 13h ; channel I/O error
; Allocate and initialize a file I/O buffer (two for DOUBLEBUF files),
; IX = channel descriptor address, on return IY contains the buffer
; descriptor address.
ALLBUF: ld a,(T1)
cp 13 ; system channel?
jr nz,alb1 ; jump if not
ld iy,(SYSBUF) ; else use the fixed system buffer descr
call CLRFNB ; clear FDB
jr alb2
alb1: call GETBUF ; allocate a file I/O buffer, return addr in IY
jr c,buferr ; on error -> buffer storage overflow
alb2: push iy
pop bc
ld (ix+2),c ; store buffer address in channel descriptor
ld (ix+3),b
ld (iy+BUF2),c
ld (iy+BUF2+1),b
bit 4,(ix+1) ; double buffering?
ret z ; return if not
ld e,(iy+BFDB)
ld d,(iy+BFDB+1)
push de
call GETBUF ; else allocate another file I/O buffer
pop de
jr c,buferr ; on error -> buffer storage overflow
ld (iy+BFDB),e ; second FDB pointer points to first
ld (iy+BFDB+1),d ; so only one copy is used
ld c,(ix+2)
ld b,(ix+3)
ld (iy+BUF2),c ; make 2nd buffer's 2nd ptr point to 1st buffer
ld (iy+BUF2+1),b
push bc
ex (sp),iy ; swap BC and IY
pop bc
ld (iy+BUF2),c ; make 1st buffer's 2nd ptr point to 2nd buffer
ld (iy+BUF2+1),b
ret
buferr: rst 10h
db 14h ; buffer storage overflow
; Allocate a buffer and a buffer descriptor block for file I/O (they are
; contiguous), return the block address in IY. Return CY set if not enough
; space for new block.
GETBUF: ld bc,(BUFCHN) ; check list of free buffers
ld a,b
or c
jr z,gtb1 ; if empty -> allocate a fresh one
push bc
pop iy
ld c,(iy+0)
ld b,(iy+1)
ld (BUFCHN),bc ; otherwise reuse it, update list pointer
; with the link value stored in the buffer
push iy
pop hl
jr gtb2 ; clear buffer and return with
; IY pointing to the reallocated buffer
gtb1: ld hl,(FNBSZ)
ld bc,BUFSZ+BDSZ
add hl,bc ; size = buffer + buffer descr block
ld c,l
ld b,h
call FREGET ; allocate space
ret c ; return with CY set if not enough space
push hl
pop iy
gtb2: ld bc,BDSZ
add hl,bc ; FNB begins after buffer descriptor
ld (iy+BFDB),l ; set FNB address in buffer descriptor
ld (iy+BFDB+1),h
ld bc,(FNBSZ)
add hl,bc ; I/O buffer begins after FNB
ld (iy+BUFAD),l ; set buffer address in buffer descriptor
ld (iy+BUFAD+1),h
CLRFNB: ld l,(iy+BFDB)
ld h,(iy+BFDB+1)
ld bc,(FNBSZ) ; note: size in bytes
bclr1: ld (hl),0 ; clear FDB
inc hl
dec bc
ld a,b
or c
jr nz,bclr1
ret ; note CY is clear
; Release file I/O buffer, IX = channel descriptor address
RLSBUF: push iy ; push IY (!!!may not be necessary)
push bc
ld a,(T1)
cp 13 ; system channel?
jr z,rls3 ; if yes, return (uses fixed buffer)
ld c,(ix+2) ; BC = buffer descriptor address
ld b,(ix+3)
push bc
pop iy
ld a,(iy+BUF2) ; DOUBLEBUF file?
cp c
jr nz,rls1 ; jump if yes
ld a,(iy+BUF2+1)
cp b
jr nz,rls1
ld bc,(BUFCHN)
ld (iy+BUFAD),c ; enter buffer into the linked list of free
ld (iy+BUFAD+1),b ; buffers
jr rls2
rls1: push hl
ld l,(iy+BUF2)
ld h,(iy+BUF2+1)
ld bc,(BUFCHN)
ld (hl),c ; save link into 2nd buffer
inc hl
ld (hl),b
dec hl
ld (iy+BUFAD),l ; chain 1st buffer to 2nd
ld (iy+BUFAD+1),h
pop hl
rls2: ld (BUFCHN),iy ; set new list end
rls3: pop bc
pop iy ; pop IY
ret
; Close system channel
CLOSYS: ld de,13
ld (T1),de ; set channel number to system channel
; Close the specified channel (DE = channel number).
CLOSCH: call SAVREG ; save BC,DE,HL,IX,IY
ld l,e ; get channel number into HL
ld h,d
call FILEA ; get channel descriptor address
ld c,(ix+2)
ld b,(ix+3) ; BC = buffer descriptor address
ld a,b
or c
jr nz,cls1 ; branch if buffer allocated (file is open)
ld (ix+0),0
ld a,(ix+1)
and 80h
ld (ix+1),a ; otherwise clear all but the MSB
scf ; and return with CY set
ret
cls1: push bc
pop iy ; IY = buffer descriptor address
push de ; push channel number
ld a,(ix+1) ; MSB set (virtual array channel)?
or a
jp m,cls4 ; jump if yes
bit 5,(ix+1) ; file opened for write only? (2000h)
jr z,cls2 ; jump if not
ld e,(iy+BUFEND) ; DE = bufend
ld d,(iy+BUFEND+1)
ld l,(iy+BUFPTR) ; HL = bufptr (current position)
ld h,(iy+BUFPTR+1)
inc hl
IF 1 ; !!!CP/M -- move this to the system-dependent section
jr cls11
cls10: ld (hl),1Ah ; clear till end of buffer
;-- ld (hl),0 ; clear till end of buffer
inc hl
cls11: call CPHLDE
jr nc,cls10 ; loop while HL <= DE
call WRBLK ; write block
ELSE
ld e,(iy+BUFAD)
ld d,(iy+BUFAD+1)
call CPHLDE ; BUFPTR points to begin of buffer?
call nz,WRBLK ; write block if not
ENDIF
cls2: ld l,(iy+BFDB) ; HL = FDB address
ld h,(iy+BFDB+1)
call FWAIT ; wait for last buffer, may be a DOUBLEBUF file
jr nc,cls3 ; jump on success (close file and return)
cp E.EOF
jr z,cls3 ; EOF error is OK
or a
jr nz,cls5 ; anything else is fatal