-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodsct.asm
More file actions
1401 lines (1256 loc) · 36.7 KB
/
Copy pathmodsct.asm
File metadata and controls
1401 lines (1256 loc) · 36.7 KB
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
; ============================================================================
; modsct.asm — Script Tokenizer for PETProject
;
; Converts a BASIC-like script from the editor gap buffer into tokenized form
; and stores it in the REU. The companion module modscr.asm (Script Runner)
; reads the REU output, installs keyword handlers at $C000, stashes the IDE,
; and hands off to the BASIC interpreter.
;
; Loads at $A000 (up to 8KB: $A000-$BFFF).
; Requires: REU (1700/1764/1750 or compatible). Errors without one.
;
; Memory layout while active:
; $A000-$AFFF : code + kwtab + include table ($A800) + REU param block ($A7F0)
; $B000-$BFFF : staging buffer — tokenized output before DMA to REU
; $0801+ : include source load buffer (gap buffer, reused after tokenization)
;
; REU layout (linear addresses):
; $000000-$003FFF : main tokenized script (up to 16KB)
; $004000-$007FFF : include pool — packed tokenized includes (up to 16KB)
; $008000-$0080DF : include table (16 entries × 14 bytes = 224 bytes)
; $008100-$00810F : script metadata block (magic, lengths, counts, version)
; $009000+ : IDE snapshot — written by modscr, not us
;
; MOD_STATUS return values:
; $01 : Error (see status bar — REU absent, disk error, syntax)
; $05 : Script tokenized to REU successfully
;
; Extended token assignments ($CC-$D8):
; $CC ASSEMBLE $CD INCLUDE $CE RUNPROG $CF SCRATCH
; $D0 DELETE $D1 EXISTS $D2 RENAME $D3 STATUS
; $D4 DRIVE $D5 ONERR $D6 PAUSE $D7 COPY $D8 DIR
;
; kwtab ordering: longest-first within groups; prefix conflicts resolved:
; RUNPROG (7) before RUN (3) — RUN is a prefix of RUNPROG
; ONERR (5) before ON (2) — ON is a prefix of ONERR
;
; Linker config (modsct.cfg):
; MEMORY { LOADADDR: start=$A000, size=2; CODE: start=$A000, size=$2000; }
; SEGMENTS { LOADADDR: load=LOADADDR; CODE: load=CODE; }
; ============================================================================
.setcpu "6502"
; ---- Module parameter block ----
MOD_MAGIC = $0212
MOD_MAGIC_VAL = $4D
MOD_BUF_LO = $0214
MOD_BUF_HI = $0215
MOD_GAP_START_LO = $0216
MOD_GAP_START_HI = $0217
MOD_GAP_END_LO = $0218
MOD_GAP_END_HI = $0219
MOD_BUF_END_LO = $021A
MOD_BUF_END_HI = $021B
MOD_DRIVE = $021C
MOD_STATUS = $021E
; ---- Kernal ----
SETLFS = $FFBA
SETNAM = $FFBD
OPEN = $FFC0
CHKIN = $FFC6
CHRIN = $FFCF
CLRCHN = $FFCC
CLOSE = $FFC3
READST = $FFB7
; ---- REU registers ----
REU_STATUS = $DF00
REU_COMMAND = $DF01
REU_C64_LO = $DF02
REU_C64_HI = $DF03
REU_REU_LO = $DF04
REU_REU_MED = $DF05
REU_REU_HI = $DF06
REU_LEN_LO = $DF07
REU_LEN_HI = $DF08
REU_INTMASK = $DF09
REU_ADDCTL = $DF0A
; REU commands: bit 7 = execute, bits 1-0 = type
REU_CMD_STASH = $90 ; C64 -> REU (EXECUTE | FF00-trigger-disable, immediate)
REU_CMD_FETCH = $91 ; REU -> C64 (EXECUTE | FF00-trigger-disable, immediate)
; ---- REU layout (linear addresses, 3-byte) ----
REU_SCRIPT_LO = $00 ; $000000: main tokenized script
REU_SCRIPT_MED = $00
REU_SCRIPT_HI = $00
REU_IPOOL_LO = $00 ; $004000: include pool start
REU_IPOOL_MED = $40
REU_IPOOL_HI = $00
REU_ITABLE_LO = $00 ; $008000: include table (224 bytes)
REU_ITABLE_MED = $80
REU_ITABLE_HI = $00
REU_META_LO = $00 ; $008100: metadata block
REU_META_MED = $81
REU_META_HI = $00
; ---- Metadata block offsets ----
META_MAGIC0 = 0 ; $53 'S'
META_MAGIC1 = 1 ; $43 'C'
META_SCLEN_LO = 2 ; main script tokenized length lo
META_SCLEN_HI = 3 ; main script tokenized length hi
META_ICOUNT = 4 ; number of includes tokenized
META_VERSION = 5 ; $01
META_SIZE = 6
; ---- ZP — same layout as modtok (no conflict between phases) ----
LINENO = $3A ; line number lo (hi=$3B); = KW_TOKEN in try_keyword
KW_TOKEN = $3A
KW_XSAVE = $3B
TMP16 = $3C ; general 16-bit scratch lo (hi=$3D)
IN_STRING = $3E
AFTER_REM = $3F
LINK_PTR = $F7 ; lo (hi=$F8) — link word back-patch pointer / scan pointer
BASIC_ADDR = $F9 ; lo (hi=$FA) — running C64 address tracker
SRC_PTR = $FB ; lo (hi=$FC) — source walker
DST_PTR = $FD ; lo (hi=$FE) — staging output pointer
KW_CHAR = $FF
; ---- Module RAM (not in binary — runtime use only) ----
; These addresses fall within $A000-$BFFF but above the code.
; ca65 does not emit data for them; they are used as scratch RAM.
; REU parameter block — filled before each reu_exec call
REU_PB_C64LO = $A7F0
REU_PB_C64HI = $A7F1
REU_PB_REULO = $A7F2
REU_PB_REUME = $A7F3
REU_PB_REUHI = $A7F4
REU_PB_LENLO = $A7F5
REU_PB_LENHI = $A7F6
REU_PB_CMD = $A7F7
; Include table: 16 entries × 14 bytes = 224 bytes
INCL_TABLE = $A800 ; [+0..7]=name(8), [+8..10]=REU addr(3), [+11..12]=len(2), [+13]=flags
INCL_ENTRY_SZ = 14
INCL_MAX = 16
INCL_COUNT = $A8E0 ; 1 byte: number of entries in table
INCL_POOL_LO = $A8E1 ; 3 bytes: next free offset in REU include pool
INCL_POOL_MED = $A8E2
INCL_POOL_HI = $A8E3
INCL_FNAME = $A8E4 ; 8-byte filename buffer (for current include lookup/store)
INCL_FLEN = $A8EC ; 1 byte: current include filename length
INCL_LOAD_LO = $A8ED ; load length lo
INCL_LOAD_HI = $A8EE ; load length hi
; Script staging buffer: tokenized output before DMA to REU
STAGING = $B000 ; 4KB: $B000-$BFFF
STAGING_END = $C000
; Include source load buffer: reuse gap buffer area after main tokenization
LOAD_BUF = $0801
LOAD_BUF_END = $9FFF
; Logical file number for disk I/O
MOD_LA = 5
BASIC_START = $0801
; ============================================================================
.segment "LOADADDR"
.word $A000
.segment "CODE"
jmp tokenize
; ============================================================================
; tokenize — main entry point
; ============================================================================
tokenize:
sei ; protect ZP and REU setup from IRQ
lda MOD_MAGIC
cmp #MOD_MAGIC_VAL
beq :+
jmp @err_bad
: ; Detect REU — required
jsr reu_detect
bcs :+
jmp @err_noreu
: ; Init include pool tracking
lda #0
sta INCL_COUNT
lda #REU_IPOOL_LO
sta INCL_POOL_LO
lda #REU_IPOOL_MED
sta INCL_POOL_MED
lda #REU_IPOOL_HI
sta INCL_POOL_HI
; ---- Phase 1: tokenize main script to STAGING ----
jsr tok_main_script ; tokenizes gap buffer → STAGING; sets TMP16=length
bcc :+
jmp @err_bad ; tokenization error (shouldn't normally happen)
: ; Save script length for metadata
lda TMP16
sta tok_sclen_lo ; stash for metadata
lda TMP16+1
sta tok_sclen_hi
; ---- Phase 2: DMA staging → REU script slot ----
lda #<STAGING
sta REU_PB_C64LO
lda #>STAGING
sta REU_PB_C64HI
lda #REU_SCRIPT_LO
sta REU_PB_REULO
lda #REU_SCRIPT_MED
sta REU_PB_REUME
lda #REU_SCRIPT_HI
sta REU_PB_REUHI
lda TMP16
sta REU_PB_LENLO
lda TMP16+1
sta REU_PB_LENHI
lda #REU_CMD_STASH
jsr reu_exec
; ---- Phase 3: scan staging for INCLUDE tokens, process each ----
; STAGING still has the tokenized main script; scan it in C64 RAM.
jsr scan_for_includes
bcc :+
jmp @err_disk ; disk error loading an include
: ; ---- Phase 4: write include table and metadata to REU ----
jsr write_include_table
jsr write_metadata
; Success
cli
lda #$05 ; MOD_STATUS $05 = script tokenized to REU
sta MOD_STATUS
rts
@err_noreu:
cli
jsr set_status_noreu
lda #$01
sta MOD_STATUS
rts
@err_disk:
cli
jsr set_status_disk
lda #$01
sta MOD_STATUS
rts
@err_bad:
cli
lda #$01
sta MOD_STATUS
rts
; Saved script length (used in metadata)
tok_sclen_lo: .byte 0
tok_sclen_hi: .byte 0
; ============================================================================
; tok_main_script — tokenize gap buffer to STAGING.
; Source: MOD_BUF_LO/HI .. MOD_GAP_START_LO/HI (text before gap = all content).
; Output: STAGING contains tokenized BASIC (no PRG load-address header).
; TMP16 = byte count of tokenized output.
; C=0 on success, C=1 on error.
; ============================================================================
tok_main_script:
; Source: gap buffer start → gap start (text before gap = all content)
lda MOD_BUF_LO
sta SRC_PTR
lda MOD_BUF_HI
sta SRC_PTR+1
; End of source = gap start (text after gap is empty post-cursor space)
; Use MOD_GAP_START as the source end — it marks end of text content.
; If gap is at the very start (empty buffer), MOD_GAP_START = MOD_BUF.
; Also honor MOD_GAP_END to MOD_BUF_END for text after cursor.
; For now: source = [MOD_BUF .. MOD_GAP_START) ∪ [MOD_GAP_END .. MOD_BUF_END)
; Simple approach: walk SRC_PTR, skip gap in tok_src_peek (like modasm src_peek).
; Store gap bounds so tok_src_peek can skip them
lda MOD_GAP_START_LO
sta tok_gap_s_lo
lda MOD_GAP_START_HI
sta tok_gap_s_hi
lda MOD_GAP_END_LO
sta tok_gap_e_lo
lda MOD_GAP_END_HI
sta tok_gap_e_hi
lda MOD_BUF_END_LO
sta tok_src_end_lo
lda MOD_BUF_END_HI
sta tok_src_end_hi
; Output: staging buffer
lda #<STAGING
sta DST_PTR
lda #>STAGING
sta DST_PTR+1
; BASIC_ADDR tracks the runtime $0801-based address of each emitted byte
lda #<BASIC_START
sta BASIC_ADDR
lda #>BASIC_START
sta BASIC_ADDR+1
; ---- @line_loop ----
@line_loop:
jsr tok_src_at_end
beq :+ ; zero = NOT at end → continue
jmp @all_done
:
; Check for blank/empty line — skip bare CRs
jsr tok_src_peek
bne :+ ; non-zero = not empty → continue
jmp @all_done
:
cmp #$0D
bne @parse_lineno
jsr tok_src_advance
jmp @line_loop
@parse_lineno:
lda #0
sta LINENO
sta LINENO+1
@digit_loop:
jsr tok_src_peek
cmp #'0'
bcc @digits_done
cmp #'9'+1
bcs @digits_done
sec
sbc #'0'
pha
; LINENO = LINENO*10 + digit (same multiply as modtok)
lda LINENO
asl a
sta TMP16
lda LINENO+1
rol a
sta TMP16+1
asl LINENO
rol LINENO+1
asl LINENO
rol LINENO+1
asl LINENO
rol LINENO+1
lda LINENO
clc
adc TMP16
sta LINENO
lda LINENO+1
adc TMP16+1
sta LINENO+1
pla
clc
adc LINENO
sta LINENO
bcc :+
inc LINENO+1
: jsr tok_src_advance
jmp @digit_loop
@digits_done:
@skip_spaces:
jsr tok_src_peek
cmp #$20
bne @begin_line
jsr tok_src_advance
jmp @skip_spaces
@begin_line:
; Save DST_PTR as LINK_PTR for back-patching link word
lda DST_PTR
sta LINK_PTR
lda DST_PTR+1
sta LINK_PTR+1
lda #0
jsr emit_byte ; link word lo placeholder
jsr emit_byte ; link word hi placeholder
lda LINENO
jsr emit_byte ; line number lo
lda LINENO+1
jsr emit_byte ; line number hi
; Advance BASIC_ADDR past the 4-byte header
lda BASIC_ADDR
clc
adc #4
sta BASIC_ADDR
bcc :+
inc BASIC_ADDR+1
:
lda #0
sta IN_STRING
sta AFTER_REM
; ---- @token_loop ----
@token_loop:
jsr tok_src_peek
beq @all_done
cmp #$0D
beq @end_line
lda AFTER_REM
bne @literal
; Track quote toggle
jsr tok_src_peek
cmp #$22
bne @no_quote
lda IN_STRING
eor #$FF
sta IN_STRING
lda #$22
jsr tok_src_advance
jsr emit_byte
jsr inc_basic_addr
jmp @token_loop
@no_quote:
lda IN_STRING
bne @literal
; Attempt keyword match
jsr try_keyword
bcc @literal
; Keyword matched: A = token
cmp #$8F ; REM token — rest of line is literal
bne :+
lda #$FF
sta AFTER_REM
lda #$8F
: jsr emit_byte
jsr inc_basic_addr
jmp @token_loop
@literal:
jsr tok_src_peek
jsr tok_src_advance
jsr emit_byte
jsr inc_basic_addr
jmp @token_loop
@end_line:
jsr tok_src_advance ; consume CR
lda #0
jsr emit_byte ; null terminator
jsr inc_basic_addr
; Back-patch link word with current BASIC_ADDR
ldy #0
lda BASIC_ADDR
sta (LINK_PTR),y
iny
lda BASIC_ADDR+1
sta (LINK_PTR),y
jmp @line_loop
@all_done:
lda #0
jsr emit_byte
jsr emit_byte ; $00 $00 end-of-program marker
; NOTE: do NOT zero LINK_PTR here. The last line's link word was already
; back-patched in @end_line to point at this end-of-program marker. BASIC
; detects end-of-program by following that link and finding the $00 $00
; marker. Zeroing the last line's link word would orphan it and make BASIC
; stop one line early.
; Compute byte count: DST_PTR - STAGING
lda DST_PTR
sec
sbc #<STAGING
sta TMP16
lda DST_PTR+1
sbc #>STAGING
sta TMP16+1
clc
rts
; Gap-skipping source walker state (set up before tok_main_script)
tok_gap_s_lo: .byte 0
tok_gap_s_hi: .byte 0
tok_gap_e_lo: .byte 0
tok_gap_e_hi: .byte 0
tok_src_end_lo: .byte 0
tok_src_end_hi: .byte 0
; ============================================================================
; tok_src_peek — read byte at SRC_PTR, skipping the gap. Returns A=0 at end.
; ============================================================================
tok_src_peek:
; If SRC_PTR == gap start: jump to gap end
lda SRC_PTR+1
cmp tok_gap_s_hi
bne @check_end
lda SRC_PTR
cmp tok_gap_s_lo
bne @check_end
lda tok_gap_e_lo
sta SRC_PTR
lda tok_gap_e_hi
sta SRC_PTR+1
@check_end:
lda SRC_PTR+1
cmp tok_src_end_hi
bcc @read
bne @at_end
lda SRC_PTR
cmp tok_src_end_lo
bcs @at_end
@read:
ldy #0
lda (SRC_PTR),y
ora #0 ; set Z correctly
rts
@at_end:
lda #0
rts
; ============================================================================
; tok_src_advance — advance SRC_PTR by 1
; ============================================================================
tok_src_advance:
inc SRC_PTR
bne :+
inc SRC_PTR+1
: rts
; ============================================================================
; tok_src_at_end — returns A=$FF if at end, else $00
; ============================================================================
tok_src_at_end:
; Skip gap if at gap start
lda SRC_PTR+1
cmp tok_gap_s_hi
bne :+
lda SRC_PTR
cmp tok_gap_s_lo
bne :+
lda tok_gap_e_lo
sta SRC_PTR
lda tok_gap_e_hi
sta SRC_PTR+1
:
lda SRC_PTR+1
cmp tok_src_end_hi
bcc @not_end
bne @at_end
lda SRC_PTR
cmp tok_src_end_lo
bcs @at_end
@not_end:
lda #0
rts
@at_end:
lda #$FF
rts
; ============================================================================
; scan_for_includes — walk STAGING for $CD (INCLUDE) tokens.
; For each one found: extract filename, check table, if new: load+tokenize.
; C=0 on success, C=1 on disk error.
; ============================================================================
scan_for_includes:
; Set up scan pointer at start of STAGING
lda #<STAGING
sta LINK_PTR
lda #>STAGING
sta LINK_PTR+1
; End of scan = DST_PTR (set by tok_main_script to one past last byte)
; DST_PTR was left pointing just past the $00 $00 end marker
@scan_line:
; Check if scan pointer >= DST_PTR (end of staging content)
lda LINK_PTR+1
cmp DST_PTR+1
bcc @not_at_scan_end
beq :+ ; hi bytes equal — check lo
jmp @scan_done ; hi > hi → done
: lda LINK_PTR
cmp DST_PTR
bcc @not_at_scan_end ; lo < lo → not done
jmp @scan_done ; lo >= lo → done
@not_at_scan_end:
; Read link word lo/hi to advance to next line (skip link + line# = 4 bytes)
ldy #2 ; byte offset 2 = line number lo (skip link word)
; Actually walk byte by byte to handle $00 end-of-line correctly.
; Skip 4-byte header (link+lineno)
jsr scan_advance4
; Walk tokens in this line
@scan_token:
ldy #0
lda (LINK_PTR),y
beq @scan_next_line ; $00 = end of line, advance pointer past it
cmp #$22 ; quote — skip string literal
beq @scan_skip_string
cmp #$8F ; REM — skip rest of line
beq @scan_skip_rem
cmp #$CD ; INCLUDE token!
beq @found_include
jsr scan_advance1
jmp @scan_token
@scan_skip_string:
jsr scan_advance1 ; skip opening quote
@scan_str_inner:
ldy #0
lda (LINK_PTR),y
beq @scan_next_line ; end of line inside string (malformed, skip)
cmp #$22
beq @scan_str_end
jsr scan_advance1
jmp @scan_str_inner
@scan_str_end:
jsr scan_advance1 ; skip closing quote
jmp @scan_token
@scan_skip_rem:
jsr scan_advance1 ; skip past any remaining bytes until EOL
ldy #0
lda (LINK_PTR),y
bne @scan_skip_rem
; fall into @scan_next_line
@scan_next_line:
jsr scan_advance1 ; skip the $00 end-of-line byte
jmp @scan_line
@found_include:
; LINK_PTR points at $CD. Advance past it.
jsr scan_advance1
; Skip spaces
@skip_sp:
ldy #0
lda (LINK_PTR),y
cmp #$20
bne @expect_quote
jsr scan_advance1
jmp @skip_sp
@expect_quote:
cmp #$22
bne @scan_token ; malformed INCLUDE — no quote, skip it
jsr scan_advance1 ; skip opening quote
; Read filename into INCL_FNAME
ldy #0
@read_fname:
lda (LINK_PTR),y
beq @fname_done ; EOL — malformed, treat as end of filename
cmp #$22
beq @fname_done ; closing quote
cpy #7 ; max 8 chars (0-7)
bcs @skip_fname_char
sta INCL_FNAME,y
iny
@skip_fname_char:
jsr scan_advance1
jmp @read_fname
@fname_done:
; Zero-pad INCL_FNAME to 8 bytes
sty INCL_FLEN ; save actual length
lda #0
@pad_fname:
cpy #8
bcs @fname_padded
sta INCL_FNAME,y
iny
bne @pad_fname
@fname_padded:
; Advance past closing quote if we stopped on one
ldy #0
lda (LINK_PTR),y
cmp #$22
bne :+
jsr scan_advance1
:
; Check if already in include table
jsr find_in_incl_table
bcs :+ ; carry set = NOT found → process it
jmp @scan_token ; carry clear = already in table → skip
:
; New include — load, tokenize, store
jsr load_and_tok_include
bcs :+ ; carry set = disk error
jmp @scan_token ; carry clear = success → continue scan
: ; Disk error
sec
rts
@scan_done:
clc
rts
; ============================================================================
; scan_advance1 — advance LINK_PTR by 1
; ============================================================================
scan_advance1:
inc LINK_PTR
bne :+
inc LINK_PTR+1
: rts
; ============================================================================
; scan_advance4 — advance LINK_PTR by 4 (skip link+lineno header)
; ============================================================================
scan_advance4:
lda LINK_PTR
clc
adc #4
sta LINK_PTR
bcc :+
inc LINK_PTR+1
: rts
; ============================================================================
; find_in_incl_table — search INCL_TABLE for INCL_FNAME (8-byte padded).
; C=0: found (TMP16 = pointer to entry).
; C=1: not found.
; ============================================================================
find_in_incl_table:
lda #<INCL_TABLE
sta TMP16
lda #>INCL_TABLE
sta TMP16+1
ldx INCL_COUNT
beq @not_found
@loop:
ldy #0
@cmp:
lda (TMP16),y
cmp INCL_FNAME,y
bne @next
iny
cpy #8
bne @cmp
clc ; found
rts
@next:
lda TMP16
clc
adc #INCL_ENTRY_SZ
sta TMP16
bcc :+
inc TMP16+1
: dex
bne @loop
@not_found:
sec
rts
; ============================================================================
; load_and_tok_include — load INCL_FNAME from disk, tokenize to STAGING,
; DMA to REU include pool, add entry to INCL_TABLE.
; C=0 success, C=1 disk error.
; ============================================================================
load_and_tok_include:
; Check table not full
lda INCL_COUNT
cmp #INCL_MAX
bcc :+
jmp @err_full
:
; ---- Load source file from disk into LOAD_BUF ----
lda #MOD_LA
ldx MOD_DRIVE
ldy #2 ; SA=2 = read
jsr SETLFS
lda INCL_FLEN ; filename length
ldx #<INCL_FNAME
ldy #>INCL_FNAME
jsr SETNAM
jsr OPEN
bcc :+
jmp @err_disk
:
ldx #MOD_LA
jsr CHKIN
bcc :+
jmp @err_close
:
; Read bytes into LOAD_BUF
lda #<LOAD_BUF
sta TMP16
lda #>LOAD_BUF
sta TMP16+1
@read_loop:
jsr READST
and #$42 ; EOF or error
bne @read_done
jsr CHRIN
ldy #0
sta (TMP16),y
inc TMP16
bne @read_loop
inc TMP16+1
jmp @read_loop
@read_done:
jsr CLRCHN
lda #MOD_LA
jsr CLOSE
; Calculate source length in INCL_LOAD_LO/HI
lda TMP16
sec
sbc #<LOAD_BUF
sta INCL_LOAD_LO
lda TMP16+1
sbc #>LOAD_BUF
sta INCL_LOAD_HI
; ---- Tokenize LOAD_BUF to STAGING ----
; Reuse tok_main_script machinery: point SRC_PTR at LOAD_BUF,
; set gap so it's entirely past the end (no gap in include source).
lda #<LOAD_BUF
sta SRC_PTR
lda #>LOAD_BUF
sta SRC_PTR+1
; Gap: set gap_start = gap_end = end of include source (no gap)
lda TMP16 ; TMP16 still = LOAD_BUF + length (end address)
sta tok_gap_s_lo
sta tok_gap_e_lo
lda TMP16+1
sta tok_gap_s_hi
sta tok_gap_e_hi
sta tok_src_end_lo ; same end
lda TMP16+1
sta tok_src_end_hi
; Rewind STAGING output
lda #<STAGING
sta DST_PTR
lda #>STAGING
sta DST_PTR+1
lda #<BASIC_START
sta BASIC_ADDR
lda #>BASIC_START
sta BASIC_ADDR+1
; Tokenize (reuse the same @line_loop code via a JSR — not possible inline).
; Call tok_main_script with current ZP state.
; tok_main_script uses SRC_PTR/DST_PTR/etc. that we've just set up.
jsr tok_main_script ; returns TMP16 = tokenized length
; ---- DMA STAGING → REU include pool ----
lda #<STAGING
sta REU_PB_C64LO
lda #>STAGING
sta REU_PB_C64HI
lda INCL_POOL_LO
sta REU_PB_REULO
lda INCL_POOL_MED
sta REU_PB_REUME
lda INCL_POOL_HI
sta REU_PB_REUHI
lda TMP16
sta REU_PB_LENLO
lda TMP16+1
sta REU_PB_LENHI
lda #REU_CMD_STASH
jsr reu_exec
; ---- Add to include table ----
; Compute entry address: INCL_TABLE + INCL_COUNT * INCL_ENTRY_SZ
lda INCL_COUNT
; × 14 = × 8 + × 4 + × 2
sta TMP16
lda #0
sta TMP16+1
; TMP16 = count; compute count * 14
asl TMP16 ; ×2
rol TMP16+1
; save ×2 in $3C (reuse KW_TOKEN area momentarily — done with tokenize phase)
lda TMP16
pha
lda TMP16+1
pha
asl TMP16 ; ×4
rol TMP16+1
asl TMP16 ; ×8
rol TMP16+1
pla ; pop ×2 hi
adc TMP16+1
sta TMP16+1
pla ; pop ×2 lo
adc TMP16
sta TMP16 ; TMP16 = count * 10... wait, need ×14 = ×8+×4+×2
; Actually: ×14 = ×8 + ×4 + ×2. Let me redo.
; TMP16 currently has count*8 (after the two ASLs above).
; We need count*8 + count*4 + count*2. We pulled ×2 from stack.
; And TMP16 = count*8 + count*2 now (after the adds above).
; That's count*10. Need count*14 = count*10 + count*4.
; To get count*4 we need to recompute. Let's just use a simpler approach.
; INCL_ENTRY_SZ = 14 — just multiply by repeated addition for small counts.
; INCL_COUNT <= 16 so at most 16 additions of 14.
; Restart: TMP16 = INCL_COUNT * 14 via loop.
lda INCL_COUNT
beq @zero_offset
tax
lda #0
sta TMP16
sta TMP16+1
@mul14:
lda TMP16
clc
adc #INCL_ENTRY_SZ
sta TMP16
bcc :+
inc TMP16+1
: dex
bne @mul14
@zero_offset:
; TMP16 now = INCL_COUNT * INCL_ENTRY_SZ
lda TMP16
clc
adc #<INCL_TABLE
sta TMP16
lda TMP16+1
adc #>INCL_TABLE
sta TMP16+1 ; TMP16 = pointer to new entry
; Write 8-byte filename
ldy #0
@wname:
lda INCL_FNAME,y
sta (TMP16),y
iny
cpy #8
bne @wname
; Write 3-byte REU offset (current pool position before this include)
lda INCL_POOL_LO
sta (TMP16),y
iny
lda INCL_POOL_MED
sta (TMP16),y
iny
lda INCL_POOL_HI
sta (TMP16),y
iny
; Write 2-byte tokenized length
lda REU_PB_LENLO ; = TMP16 saved length
sta (TMP16),y
iny
lda REU_PB_LENHI
sta (TMP16),y
iny
; Write flags byte: $01 = tokenized
lda #$01
sta (TMP16),y
; Advance pool pointer by tokenized length
lda INCL_POOL_LO
clc
adc REU_PB_LENLO
sta INCL_POOL_LO
lda INCL_POOL_MED
adc REU_PB_LENHI
sta INCL_POOL_MED
bcc :+
inc INCL_POOL_HI
:
inc INCL_COUNT
clc