-
Notifications
You must be signed in to change notification settings - Fork 6
/
file.asm
1017 lines (950 loc) · 18.7 KB
/
file.asm
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
keep obj/file
mcopy file.mac
****************************************************************
*
* File2
*
* This module contains the subroutines that depend on the file
* system and shell.
*
* This version uses shell 2.0 calls.
*
****************************************************************
copy directPage
****************************************************************
*
* FileCommon - common data for the file module
*
****************************************************************
*
FileCommon privdata
;
; Constants
;
fileBuffSize equ 8*1024 max size of a file name buffer
;
; Shell call records
;
ffDCB dc i'14' fast file DCB
ffAction ds 2
ffIndex ds 2
ffFlags ds 2
ffFileHandle ds 4
ffPathName ds 4
ffAccess ds 2
ffFileType ds 2
ffAuxType ds 4
ffStorageType ds 2
ffCreate ds 8
ffMod ds 8
ffOption ds 4
ffFileLength ds 4
ffBlocksUsed ds 4
ffCheckSum ds 2
stlf_dcb dc i'11' set linfo DCB
stlf_src ds 4 source file
stlf_out ds 4 output file
stlf_prm dc a4'PRM' parameter list
stlf_lan dc a4'LAN' language specific string
stlf_mer ds 1 maximum error allowed
stlf_mef ds 1 maximum error found
stlf_lop ds 1 operations flag
stlf_kep ds 1 keep flag
stlf_ltm ds 4 set of letters with -
stlf_ltp ds 4 set of letters with +
stlf_org ds 4 origin
prm dc i'4,0'
lan dc i'4,0'
;
; global variables
;
libRefnum ds 2 refnum for the open library file
name ds 256 file name
end
****************************************************************
*
* AppendOSNames - append two GS/OS path names
*
* Inputs:
* p1,p2 - pointers to input format GS/OS names
* flags -
* 0 - return an input string
* 1 - return an output string
*
* Outputs:
* X-A - nil for out of memory, otherwise a pointer to the string
*
* Notes:
* This subroutine reserves a memory buffer based on
* the actual length of the expanded path name. The
* caller is responsible for disposing of the memory.
*
****************************************************************
*
AppendOSNames private
out equ 1 return pointer
chars equ 5 pointer to input format portion of out
sub (4:p1,4:p2,2:flags),8
clc find the length of the buffer
lda [p1]
adc [p2]
inc A
inc A
ldx flags
beq lb1
inc A
inc A
sta chars
lb1 pea 0 allocate the memory
pha
jsr MLalloc
sta out
stx out+2
ora out+2
beq lb7
lda flags if flags then
beq lb2
lda chars set the length of the buffer
sta [out]
add4 out,#2,chars chars = out+2
bra lb3 else
lb2 move4 out,chars chars = out
lb3 anop endif
clc set the length
lda [p1]
adc [p2]
sta [chars]
lda [p1] move in the first string
beq lb4a
tax
ldy #2
short M
lb4 lda [p1],Y
sta [chars],Y
iny
dex
bne lb4
long M
lb4a clc update chars
lda [p1]
adc chars
sta chars
bcc lb5
inc chars+2
lb5 lda [p2] move in the second string
beq lb7
tax
ldy #2
short M
lb6 lda [p2],Y
sta [chars],Y
iny
dex
bne lb6
long M
lb7 ret 4:out
end
****************************************************************
*
* CloseLibrary - close a library file
*
* Inputs:
* dictionary - ptr to dictionary buffer
* libRefnum - reference number for the library file
*
****************************************************************
*
CloseLibrary start
using FileCommon
ph4 dictionary dispose of the dictionary buffer
jsr Free
lda libRefnum close the library file
sta clRefnum
OSClose clRec
rts
clRec dc i'1'
clRefnum ds 2
end
****************************************************************
*
* ConvertString - convert an output string to an input string
*
* Inputs:
* str - output string pointer
*
* Outputs:
* Returns a pointer to the input string buffer
*
* Notes:
* Allocates an appropriate size buffer.
*
****************************************************************
*
ConvertString private
ptr equ 1
sub (4:str),4
add4 str,#2
lda [str]
inc A
inc A
pea 0
pha
jsr MLalloc
sta ptr
stx ptr+2
lda [str]
tay
iny
short M
lb1 lda [str],Y
sta [ptr],Y
dey
bpl lb1
long M
ret 4:ptr
end
****************************************************************
*
* GetLibFile - get a library file name from the library directory
*
* Inputs:
* libIndex - index in the directory of the last library
*
* Outputs:
* r0 - pointer to a GS/OS path name
* C - set if a file was found, else clear
*
* Notes:
* The file name pointer points to a dynamically
* allocated buffer. The caller is responsible for
* disposing of the buffer. The buffer is allocated
* and returned even if the call fails.
*
****************************************************************
*
GetLibFile start
using FileCommon
using Common
stz found no name found, yet
ph4 #fileBuffSize+4 get a file name buffer
jsr MLalloc
sta gdName
sta r0
stx gdName+2
stx r2
lda #fileBuffSize+4 set its size
sta [r0]
OSOpen opRec open the library prefix
bcs lb3
lda opRefnum set the reference number
sta gdRefnum
sta clRefnum
lb1 inc libIndex try the next file in the library prefix
lda libIndex
sta gdIndex
OSGet_Dir_Entry gdRec
bcs lb2
lda gdFiletype
cmp #LIB
bne lb1
ph4 #libname append the file name to the library name
add4 r0,#2
ph4 r0
ph2 #0
jsr AppendOSNames
sta r0
stx r2
ph4 gdName free the name buffer
jsr Free
inc found found a file
lb2 OSClose clRec close the library direcory
lb3 lda found return found
lsr A
rts
;
; Local data
;
found ds 2 was a path name found?
libname dos '13/' library directory name
opRec dc i'2' open record
opRefnum ds 2
dc a4'libname'
clRec dc i'1' close record
clRefnum ds 2
gdRec dc i'7' GetDirEntry record
gdRefnum ds 2
ds 2 file flags
dc i'0' base (absolute displacement number)
gdIndex ds 2 index into the directory
gdName ds 4 file name
ds 2 entry number
gdFiletype ds 2 file type
end
****************************************************************
*
* GetLibList - process libraries in {Libraries} shell variable
*
* Inputs:
* slist - list of command line libraries
*
* Outputs:
* slist - updated
* libFromShell - true if {Libraries} existed, else false
*
****************************************************************
*
GetLibList start
using FileCommon
using Common
;
; Read the library shell variable
;
stz libFromShell libFromShell = false
ph4 #fileBuffSize+4 allocate default space for the
jsr MLAlloc variable
sta rdValue
sta r0
stx rdValue+2
stx r2
lda #fileBuffSize set the buffer size
sta [r0]
OSRead_Variable rdRef read the shell variable
bcs lb1
ldy #2 quit if the value is null
lda [r0],Y
beq lb1
;
; Append the variable list to the command line file list
;
ph4 slist append a space
ph4 #blank
ph2 #0
jsr AppendOSNames
phx
pha
ph4 slist
jsr Free
pl4 slist
ph4 slist append the variable's contents
add4 rdValue,#2,r0
ph4 r0
ph2 #0
jsr AppendOSNames
phx
pha
ph4 slist
jsr Free
pl4 slist
inc libFromShell libFromShell = true
lb1 ph4 rdValue dispose of our buffer
jsr Free
rts
;
; Local data
;
blank dos ' ' space
lib dos Libraries name of the library shell variable
rdRef dc i'3' Read_Variable record
rdName dc a4'lib'
rdValue ds 4
ds 2
end
****************************************************************
*
* GetLInfo - get the command link information
*
* Outputs:
* slist - ptr to list of input file names
* kname - keep file name (nil for none)
* merr - maximum error level allowed
* merrf - maxiumum error level found so far
* lops - language operations
* kflag - keep flag
* mflags - minus flags
* pflags - plus flags
* org - origin
*
* C - set if an error occurred
*
****************************************************************
*
GetLInfo start
using FileCommon
using Common
;
; Get info
;
ph4 #fileBuffSize+4 allocate default space for the names
jsr MLAlloc
sta stlf_src
sta r0
stx stlf_src+2
stx r2
ph4 #fileBuffSize+4
jsr MLAlloc
sta stlf_out
sta r4
stx stlf_out+2
stx r6
lda #fileBuffSize set the buffer sizes
sta [r0]
sta [r4]
lla stlf_prm,prm get file info
lla stlf_lan,lan
OSGet_LInfo stlf_dcb
;
; Convert the file names
;
ph4 stlf_src
jsr ConvertString
sta slist
stx slist+2
ph4 stlf_src
jsr Free
ph4 stlf_out
jsr ConvertString
sta kname
stx kname+2
ph4 stlf_out
jsr Free
;
; Set the scalars
;
lda stlf_mer
and #$00FF
sta merr
lda stlf_mef
and #$00FF
sta merrf
lda stlf_lop
and #$00FF
sta lops
lda stlf_kep
and #$00FF
sta kflag
move4 stlf_ltm,mflags
move4 stlf_ltp,pflags
move4 stlf_org,org
clc
rts
end
****************************************************************
*
* OpenLibrary - open a library file
*
* Inputs:
* fname - name of the library to open
*
* Outputs:
* libRefnum - reference number for the library file
*
****************************************************************
*
OpenLibrary start
using FileCommon
move4 fname,opPathname
OSOpen opRec
bcc lb1
lda #1
jmp TermError
lb1 lda opRefnum
sta libRefnum
rts
opRec dc i'2'
opRefnum ds 2
opPathname ds 4
end
****************************************************************
*
* Purge - purge a file
*
* Inputs:
* fname - name of the file to purge
*
****************************************************************
*
Purge start
using FileCommon
move4 fname,ffPathName
lda #7
sta ffAction
lda #$C000
sta ffFlags
OSFastFile ffDCB
rts
end
****************************************************************
*
* PurgePlusM - purge memory only files
*
****************************************************************
*
PurgePlusM start
using Common
using FileCommon
lda memory skip this check if +m was not used
jeq ff4
ph4 #fileBuffSize+4 get a file name buffer
jsr MLalloc
sta ffPathName
sta r0
stx ffPathName+2
stx r2
lda #fileBuffSize+4
sta [r0]
add4 ffPathName,#2
stz index for each file index do
ff1 lda #1 do an indexed load of the file
sta ffAction
lda index
sta ffIndex
sub4 ffPathName,#2
OSFastFile ffDCB
bcs ff3 quit if there is no file
add4 ffPathName,#2
lda ffFlags quit if the file is not a memory file
bne ff2
lda #5 remove the file
sta ffAction
OSFastFile ffDCB
bra ff1 try again with the same index
ff2 lda #7 purge the file
sta ffAction
OSFastFile ffDCB
inc index next file index
bra ff1
ff3 ph4 r0 dispose of the file buffer
jsr Free
ff4 rts
;
; Local data
;
index ds 2 indexed load index
end
****************************************************************
*
* Read - open a file for input
*
* Inputs:
* fname - name of the file to open
*
* Outputs:
* r0 - pointer to the first byte in the file
* r4 - length of the file
* r8 - file type
*
****************************************************************
*
Read start
using Common
using FileCommon
lda memory if +m then
beq ff1
move4 fname,ffPathName try loading the file from memory
lda #2
sta ffAction
lda #$C000
sta ffFlags
OSFastFile ffDCB
bra ff2 else
ff1 move4 fname,ffPathName try loading the file from disk
stz ffAction
lda #$C000
sta ffFlags
OSFastFile ffDCB
ff2 bcc lb1
lda #6 file not found: flag the error
jmp TermError
lb1 lda ffFileLength make sure the file is not empty
ora ffFileLength+2
bne lb2
lda #13
jmp TermError
lb2 move4 ffFileHandle,r4 return the file parameters
ldy #2
lda [r4]
sta r0
lda [r4],Y
sta r2
move4 ffFileLength,r4
lda ffFileType
sta r8
rts
end
****************************************************************
*
* ReadLibraryHeader - read the dictionary for a library
*
* Inputs:
* libRefnum - reference number for the library file
*
* Outputs:
* libSymbols - pointer to the first entry in the lib symbol table
* libLength - length of the symbol table
* libNames - pointer to the first library name
* libDisp - set to 0
* didLibSegment - set to false
*
****************************************************************
*
ReadLibraryHeader start
using FileCommon
lda libRefnum read the library header
sta rdRefnum
sta dcRefnum
sta mkRefnum
OSRead rdRec
bcc lb0
err1 lda #1
jmp TermError
lb0 lda version if version = 2 then
and #$00FF
cmp #2
bne lb1
lda type2 if the segment type is not libDict then
and #$00FF flag the error
bra lb2
lb1 lda type1 if the segment is not libDict then
and #$001F
lb2 cmp #8
beq lb3
lda #10 TermError(10)
jmp TermError
lb3 ph4 length get space for the dictionary
jsr MLalloc
sta dictionary
sta dcBuffer
stx dictionary+2
stx dcBuffer+2
OSSet_Mark mkRec set the file mark to the file start
move4 length,dcLength read the dictionary
OSRead dcRec
bcs err1
move4 dictionary,r0 {find the library symbol table}
lda version if version = 0 then
and #$00FF
bne lb4
add4 r0,#$24 add in the segment header length
bra lb7 else if version in [1,2] then
lb4 cmp #3
bge lb6
ldy #$2A add in the disp to the data
clc
lda [r0],Y
adc r0
sta r0
bcc lb5
inc r2
lb5 bra lb7 else
lb6 lda #3 flag an unsuported segment error
jmp TermError
lb7 jsr SkipLConst skip the first lconst record
add4 r0,#5,libSymbols set the library symbol table pointer
ldy #1 set the length of the symbol table
lda [r0],Y
sta libLength
iny
iny
lda [r0],Y
sta libLength+2
jsr SkipLConst skip the symbol table
add4 r0,#5,libNames set the library names pointer
jsr SkipLConst verify the last record is LConst
stz libDisp no symbols processed
stz libDisp+2
stz didLibSegment no segments processed
rts
;
; SkipLConst - skip an lconst record
;
SkipLConst anop
lda [r0] verify that the first thing is an
and #$00FF lconst record
cmp #$F2
beq sc1
lda #11 {illegal data error}
jmp TermError
sc1 ldy #1 skip it
clc
lda [r0],Y
adc r0
tax
iny
iny
lda [r0],Y
adc r2
sta r2
stx r0
add4 r0,#5
rts
;
; Local data
;
header anop header for the first library segment
length ds 4 length of the segment, in bytes
ds 4 reserved space
ds 4 length
type1 ds 1 segment type, versions 0 and 1
ds 1 label length
ds 1 number length
version ds 1 segment version
ds 4 bank size
type2 ds 2 segment type, version 2
headerend anop
rdRec dc i'4' read record for reading the first
rdRefnum ds 2 segment header
dc a4'header'
dc i4'headerend-header'
ds 4
dc i'1' cache the blocks!
dcRec dc i'4' read record for reading the dictionary
dcRefnum ds 2
dcBuffer ds 4
dcLength ds 4
ds 4
dc i'1' cache the blocks!
mkRec dc i'3' for SetMark; used to set the file
mkRefnum ds 2 mark back to the start of the file
dc i'0'
dc i4'0'
end
****************************************************************
*
* ReadLibrarySegment - read a segment from the library
*
* Inputs:
* libseg - pointer to any old library segment
* libRefnum - reference number for the library
* r0 - disp in the file
*
* Outputs:
* libseg - pointer to the new library segment
* seg - pointer to the first byte in the segment
*
****************************************************************
*
ReadLibrarySegment start
using Common
using FileCommon
lda libRefnum set file reference numbers
sta rdRefnum
sta dcRefnum
sta mkRefnum
move4 r0,mkDisp set the mark in the file
OSSet_Mark mkRec
bcs err1
OSRead rdRec read the library length
bcc lb1
err1 lda #6
jmp TermError
lb1 ph4 libSeg free any old segment
jsr Free
ph4 length get space for the dictionary
jsr MLalloc
sta libSeg
sta dcBuffer
sta seg
stx libSeg+2
stx dcBuffer+2
stx seg+2
OSSet_Mark mkRec set the file mark to the segment start
move4 length,dcLength read the segment
OSRead dcRec
bcs err1
rts
;
; Local data
;
header anop header for the first library segment
length ds 4 length of the segment, in bytes
headerend anop
rdRec dc i'4' read record for reading the first
rdRefnum ds 2 segment header
dc a4'header'
dc i4'headerend-header'
ds 4
dc i'1' cache the blocks!
dcRec dc i'4' read record for reading the segment
dcRefnum ds 2
dcBuffer ds 4
dcLength ds 4
ds 4
dc i'1' cache the blocks!
mkRec dc i'3' for SetMark; used to set the file
mkRefnum ds 2 mark back to the start of the file
dc i'0'
mkDisp dc i4'0'
end
****************************************************************
*
* ReadVariable - read a shell variable
*
* Inputs:
* name - GS/OS version of the variable name
*
* Outputs:
* returns a pointer to the shell variable value
*
* Notes:
* A value is always returned. If there is no shell
* variable, a value with a length of 0 is returned.
*
* The buffer is allocated dynamically. The caller must
* dispose of the buffer.
*
****************************************************************
*
ReadVariable start
value equ 1 pointer to the value
sub (4:name),4
move4 name,rdName set the name
OSRead_Variable rdRef read the shell variable
ph4 rdValue return the value
jsr ConvertString
sta value
stx value+2
ret 4:value
rdRef dc i'3' Read_Variable record
rdName ds 4
rdValue dc a4'buff2'
ds 2
buff2 dc i'256' variable value
ds 256
end
****************************************************************
*
* ScanFastFile - see if the file is in the FastFile list
*
* Inputs:
* fname - file name
*
* Outputs:
* A - 1 if the file is in the list and is memory, else 0
*
****************************************************************
*
ScanFastFile start
using Common
using FileCommon
val equ 1 return value
ptr equ 3 work pointer
fullName equ 7 expanded version of fname
index equ 11 indexed load index
sub (4:fname),12
stz val assume there is no match
ph4 #fileBuffSize+4 get a file name buffer
jsr MLalloc
sta ffPathName
sta ptr
stx ffPathName+2
stx ptr+2
lda #fileBuffSize+4
sta [ptr]
add4 ptr,#2
add4 ffPathName,#2
ph4 #fileBuffSize+4 get another buffer for the expanded
jsr MLalloc input name
sta fullName
sta exOut
stx fullName+2
stx exOut+2
lda #fileBuffSize+4
sta [fullName]
move4 fname,exIn
OSExpandDevices exRec
jcs ff5
add4 fullName,#2 make sure it is lowercase
lda [fullName]
jeq ff5
tax
ldy #2
short M
lb1 lda [fullName],Y
ora #$20
sta [fullName],Y
iny
dex
bne lb1
long M
stz index for each file index do
ff1 lda #1 do an indexed load of the file
sta ffAction
lda index
sta ffIndex
sub4 ffPathName,#2
OSFastFile ffDCB
bcs ff5 quit if there is no file
add4 ffPathName,#2
lda ffFlags skip if the file is not a memory file
bne ff4
lda [fullName] skip if the names are different
cmp [ptr]
bne ff4
tax
ldy #2
short M
ff2 lda [ptr],Y
ora #$20
cmp [fullName],Y
bne ff4
iny
dex
bne ff2
long M
lda #1 names match: val = true
sta val
lda #7 purge the file
sta ffAction
OSFastFile ffDCB
bra ff5 quit
ff4 long M
lda #7 purge the file
sta ffAction
OSFastFile ffDCB
inc index next file index
brl ff1
ff5 ph4 ptr free the name buffers
jsr Free
ph4 fullName
jsr Free
ret 2:val
exRec dc i'2' ExpandDevices record
exIn ds 4
exOut ds 4
end
****************************************************************
*
* SetLInfo - set language info before return to shell
*
****************************************************************
*
SetLInfo start
using FileCommon
using Common
;
; Set the scalars
;
short M
lda merr
sta stlf_mer
lda merrf
sta stlf_mef
lda lops
and #$FC