-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodasm.asm
More file actions
2777 lines (2522 loc) · 65.3 KB
/
Copy pathmodasm.asm
File metadata and controls
2777 lines (2522 loc) · 65.3 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
; ============================================================================
; modasm.asm - Two-pass 6502 assembler module for PETProject
;
; Loaded at $A000 by the editor (BASIC ROM paged out: $01=$36).
; Kernal remains mapped throughout - channel I/O works normally.
; ($36 = LORAM=0, HIRAM=1, CHAREN=1: RAM at $A000, Kernal+I/O in)
; Entry: JSR $A000 (first 3 bytes = JMP assemble)
;
; Input: Plain text in editor gap buffer (via MOD_BUF/GAP params).
; Source lines: [label:] MNEMONIC [operand] [;comment] CR
; Labels: up to 8 chars, alphanumeric + underscore, end with ':'
; Mnemonics: uppercase, all official 6502
; Operands: #val val val,X val,Y (val) (val,X) (val),Y A
; Values: $xx/$xxxx hex, decimal, label, label+N, label-N, <expr, >expr
; Directives: .org / *= addr .byte val[,val...] .word val[,val...]
; .text "string"
;
; Output: PRG file written directly to disk via Kernal channel I/O.
; Filename: editor prompts for output name via status bar before assembly.
; PRG header: $01,$08 (or .org address if specified before any code).
;
; Symbol table: $C000-$CFFF, 10 bytes/entry (8-char name + 2-byte value).
; Up to 409 symbols. Linear scan - fast enough for casual use.
;
; Error reporting: first error message written to ASM_ERR_MSG area,
; line number in ASM_ERR_LINE_LO/HI. MOD_STATUS=$01.
;
; Zero page (saved/restored):
; $FB/$FC = SRC_PTR walks source (gap-aware)
; $FD/$FE = unused (available)
; $3A-$3F = scratch
;
; State variables at $0200-$0257 (page 2, Kernal-safe zones):
; $0200-$0211: before MOD_* param block - ZP_SAVE + core state
; $0223-$0257: after MOD_* param block - err info, fname, gap ptrs, parse scratch
; Deliberately avoids $0259-$027C (Kernal open-file device/SA/name tables)
; which are overwritten by OPEN/CHKOUT during output file creation.
;
; Mode constants (opcode table encoding):
; IMP=0 ACC=1 IMM=2 ZP=3 ZPX=4 ZPY=5 ABS=6 ABX=7 ABY=8 IND=9 IZX=10 IZY=11 REL=12
; ============================================================================
.setcpu "6502"
; ---- Parameter block ($0212-$0220) ----
MOD_MAGIC = $0212
MOD_MAGIC_VAL = $4D
MOD_COMMAND = $0213
; ---- Batch invocation (MOD_COMMAND = ASM_CMD_BATCH) ----
; Set by MODSCRH's ASSEMBLE keyword handler. In batch mode there is no
; keyboard and no status-bar prompt: the output filename is passed in the
; unused page-2 area $02C0-$02D0 instead. Keep these three definitions in
; sync with modscrh.asm.
ASM_CMD_BATCH = $01
ASM_BATCH_FNAME_LEN = $02C0 ; output filename length (1-16)
ASM_BATCH_FNAME = $02C1 ; output filename (16 bytes max)
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 routines ----
SETLFS = $FFBA
SETNAM = $FFBD
OPEN = $FFC0
CHKIN = $FFC6
CHKOUT = $FFC9
CHRIN = $FFCF
CHROUT = $FFD2
CLOSE = $FFC3
CLRCHN = $FFCC
GETIN = $FFE4
READST = $FFB7
; ---- Screen/color RAM ----
SCREEN = $0400
COLOR = $D800
STATUS_ROW = SCREEN ; row 0 = status bar
COLS = 40
DEFAULT_COLOR = 14 ; light blue
; ---- ZP ----
SRC_PTR = $FB ; lo (hi=$FC) - source walker, gap-aware
TMP = $3A ; general scratch (hi=$3B)
TMP2 = $3C ; (hi=$3D)
TMP3 = $3E ; (hi=$3F)
; ---- Mode constants ----
MODE_IMP = 0
MODE_ACC = 1
MODE_IMM = 2
MODE_ZP = 3
MODE_ZPX = 4
MODE_ZPY = 5
MODE_ABS = 6
MODE_ABX = 7
MODE_ABY = 8
MODE_IND = 9
MODE_IZX = 10
MODE_IZY = 11
MODE_REL = 12
; ---- Assembler state (page 2, always RAM regardless of banking) ----
ASM_PC_LO = $C000 ; current program counter lo
ASM_PC_HI = $C001 ; current program counter hi
ASM_PASS = $C002 ; 0=pass1, 1=pass2
ASM_SYM_LO = $C003 ; symbol count lo
ASM_SYM_HI = $C004 ; symbol count hi
ASM_LINE_LO = $C005 ; current line number lo
ASM_LINE_HI = $C006 ; current line number hi
ASM_ERR = $C007 ; $FF = error encountered
ASM_ERR_LINE_LO = $C008 ; line number of first error lo
ASM_ERR_LINE_HI = $C009 ; line number of first error hi
ASM_ERR_MSG = $C00A ; 20-byte error message (screen codes, zero-terminated)
ASM_OUT_LA = $C01E ; logical file number for output
ASM_OUT_OPEN = $C01F ; $FF = output file is open
ASM_ORG_SET = $C020 ; $FF = .org has been seen (affects PRG header)
ASM_FNAME_LEN = $C021 ; output filename length
ASM_FNAME = $C022 ; output filename (16 bytes)
; ---- ZP save area ----
ZP_SAVE = $C03A ; 10 bytes: saves $3A-$3F, $FB-$FE
; ---- Gap pointers (copied from params) ----
ASM_GAP_S_LO = $C032
ASM_GAP_S_HI = $C033
ASM_GAP_E_LO = $C034
ASM_GAP_E_HI = $C035
ASM_BUF_LO = $C036
ASM_BUF_HI = $C037
ASM_END_LO = $C038
ASM_END_HI = $C039
; ---- Scratch for mnemonic/operand parsing ----
ASM_MNEM = $C044 ; 4 bytes: current mnemonic/directive name
ASM_MODE = $C048 ; resolved addressing mode
ASM_OPCODE = $C049 ; resolved opcode byte
ASM_VAL_LO = $C04A ; operand value lo
ASM_VAL_HI = $C04B ; operand value hi
ASM_INSTR_SIZE = $C04C ; total instruction size (1+operand bytes)
ASM_LABEL = $C04D ; 8 bytes: current label (space-padded)
ASM_YPEEK = $C055 ; scratch: src_peek Y save (1 byte, at end of state block)
ASM_OPC_PTR_LO = $C056 ; saved opcode table pointer lo (preserved across parse_operand)
ASM_OPC_PTR_HI = $C057 ; saved opcode table pointer hi
; FIX (load address): PRG load address = address of first .org/.* seen, or $0801.
; Stored here during pass 1; used by open_output for the PRG header.
; Lives in the gap between ASM_OPC_PTR_HI ($C057) and SYM_TABLE ($C060).
ASM_LOAD_LO = $C058 ; PRG load address lo
ASM_LOAD_HI = $C059 ; PRG load address hi
; Activity spinner — same corner cell and toggle mechanic as MODDIS.
ASM_SPINNER = $C05A ; 8-bit line counter; every 16 lines → color flip
ASM_SPIN_IDX = $C05B ; current color value written to SPIN_CELL
SPIN_CELL = $D800 ; color RAM col 0, row 0 (top-left corner)
SPIN_COLOR_A = $01 ; white
SPIN_COLOR_B = $00 ; black
; ---- Symbol table ----
SYM_TABLE = $C060 ; 10 bytes/entry: 8-char name + 2-byte value
SYM_ENTRY_SIZE = 10
SYM_MAX = 400
; ---- Output file ----
ASM_OUT_LA_VAL = 4 ; logical file 4 for output
; ============================================================================
; Include / source-stack state
; ----------------------------------------------------------------------------
; The assembler reads source through a stack of "frames". Frame 0 is the
; editor's in-memory gap buffer (the normal, no-include case). A .include
; directive pushes a FILE frame that streams the included file from disk one
; line at a time into LINE_BUF, so SRC_PTR always points at real RAM and the
; parser's rewind-on-lookahead (check_for_label) keeps working unchanged.
;
; Placed at fixed addresses high in MAIN ($BFxx). Code ends well below $B000,
; so this free RAM is in the same always-visible bank ($01=$36) as the code —
; no banking concerns, and it doesn't touch the packed $C0xx state or the
; symbol table.
;
; Frame layout (SRC_FRAME_SIZE bytes each):
; +0 KIND 0 = buffer (frame 0 only), 1 = file
; +1 LFN KERNAL logical file number (file frames)
; +2 SRC_LO saved SRC_PTR lo (resume position)
; +3 SRC_HI saved SRC_PTR hi
; +4 LINE_LO saved ASM_LINE counter lo (per-file line numbers)
; +5 LINE_HI saved ASM_LINE counter hi
; ============================================================================
SRC_FRAME_SIZE = 6
SRC_MAX_DEPTH = 8 ; >=8 deep .include nesting -> ?INCLUDE TOO DEEP
SRC_DEPTH = $BF00 ; current top frame index (0 = buffer only)
SRC_STACK = $BF01 ; SRC_MAX_DEPTH * SRC_FRAME_SIZE = 48 bytes ($BF01..$BF30)
SRC_FRAME_KIND = 0 ; field offsets within a frame
SRC_FRAME_LFN = 1
SRC_FRAME_SLO = 2
SRC_FRAME_SHI = 3
SRC_FRAME_LLO = 4
SRC_FRAME_LHI = 5
SRC_KIND_BUFFER = 0
SRC_KIND_FILE = 1
LINE_BUF = $BF40 ; one shared line buffer for the active file frame
LINE_BUF_MAX = 80 ; ($BF40..$BF8F); longer lines truncate with error
LINE_BUF_END = $BF31 ; lo byte of address past last content byte (hi=$BF always)
; set by fill_line_buf; used by @file_skip to detect line end
INC_LFN_BASE = 5 ; LFN for depth-1 include; depth-N uses LFN (INC_LFN_BASE+N-1)
; ============================================================================
; Module entry point
; ============================================================================
.segment "LOADADDR"
.word $A000
.segment "CODE"
jmp assemble ; $A000: module entry (JMP convention)
; ============================================================================
; assemble - main entry point
; ============================================================================
assemble:
; Disable IRQ for the duration of assembly.
; The C64 Kernal IRQ handler uses $FB/$FC as a scratch pointer for
; cursor blink, which would corrupt SRC_PTR mid-assembly.
sei
; Ensure $01=$36 (BASIC ROM out, Kernal+I/O in, RAM at $A000-$BFFF visible).
; Do this FIRST before touching any state - if caller failed to set banking,
; our code here would be unreadable (BASIC ROM at $A000). But since we're
; already executing (caller's JMP got us here), banking must be at least
; partially working. Belt-and-suspenders: force it explicitly.
; Must set $00 first to make bits 0-2 outputs, then write $01.
lda $00
ora #$07
sta $00
lda #$36
sta $01
; Save ZP
ldx #0
@zpsave:
lda $3A,x
sta ZP_SAVE,x
inx
cpx #6
bne @zpsave
lda $FB
sta ZP_SAVE+6
lda $FC
sta ZP_SAVE+7
lda $FD
sta ZP_SAVE+8
lda $FE
sta ZP_SAVE+9
; Copy gap/buffer params
lda MOD_GAP_START_LO
sta ASM_GAP_S_LO
lda MOD_GAP_START_HI
sta ASM_GAP_S_HI
lda MOD_GAP_END_LO
sta ASM_GAP_E_LO
lda MOD_GAP_END_HI
sta ASM_GAP_E_HI
lda MOD_BUF_LO
sta ASM_BUF_LO
lda MOD_BUF_HI
sta ASM_BUF_HI
lda MOD_BUF_END_LO
sta ASM_END_LO
lda MOD_BUF_END_HI
sta ASM_END_HI
; Init state
lda #0
sta ASM_PC_LO
sta ASM_PC_HI
sta ASM_SYM_LO
sta ASM_SYM_HI
sta ASM_LINE_LO
sta ASM_LINE_HI
sta ASM_ERR
sta ASM_OUT_OPEN
sta ASM_ORG_SET
sta ASM_FNAME_LEN
; Default PC = $0801
lda #$01
sta ASM_PC_LO
lda #$08
sta ASM_PC_HI
; FIX (load address): default load address = $0801.
; Updated to first .org/.* address seen during pass 1.
lda #$01
sta ASM_LOAD_LO
lda #$08
sta ASM_LOAD_HI
; Output filename: interactive prompt, or batch param area when invoked
; by the script runner (MODSCRH ASSEMBLE keyword - no keyboard available).
lda MOD_COMMAND
cmp #ASM_CMD_BATCH
bne @name_interactive
; ---- Batch: filename was placed at ASM_BATCH_FNAME by the caller ----
lda ASM_BATCH_FNAME_LEN
beq @name_bad ; empty name - refuse
cmp #17
bcs @name_bad ; >16 chars won't fit ASM_FNAME
sta ASM_FNAME_LEN
tay
@name_copy:
dey
lda ASM_BATCH_FNAME,y
sta ASM_FNAME,y
cpy #0
bne @name_copy
jmp @name_done
@name_bad:
lda #$01
sta MOD_STATUS ; report error to caller
jmp @restore_zp
@name_interactive:
; Prompt for output filename - needs IRQ for keyboard scan (GETIN)
cli
jsr prompt_outfile ; fills ASM_FNAME/ASM_FNAME_LEN; C=1 if cancelled
sei
bcc @name_done
jmp @restore_zp ; cancelled - return with MOD_STATUS unchanged ($FF)
@name_done:
; ---- Pass 1: build symbol table ----
lda #0
sta ASM_PASS
jsr run_pass
; ---- Pass 2: emit code ----
; Only if no errors in pass 1
lda ASM_ERR
bne @done_err
lda #1
sta ASM_PASS
; FIX (load address): reset PC for pass 2 BEFORE opening the output file,
; so that open_output reads ASM_LOAD_LO/HI (set during pass 1) not ASM_PC
; (which held the end-of-pass-1 address).
lda #$01
sta ASM_PC_LO
lda #$08
sta ASM_PC_HI
lda #0
sta ASM_LINE_LO
sta ASM_LINE_HI
; Open output file - needs IRQ for serial bus
cli
jsr open_output
sei
bcc :+
jmp @done_err ; open failed - error already set
:
jsr run_pass
; Close output file - needs IRQ for serial bus
cli
jsr close_output
sei
lda ASM_ERR
bne @done_err
; Success
lda #$00
sta MOD_STATUS
jmp @restore_zp
@done_err:
; Copy error info to params area for editor to display
jsr copy_error_to_status
lda #$01
sta MOD_STATUS
@restore_zp:
; Restore spinner cell to default color before returning
lda #SPIN_COLOR_A
sta SPIN_CELL
; NOTE: Do NOT restore $01 here - we're still executing in $A000-$BFFF range.
; Restoring BASIC ROM ($01=$37) while executing here would immediately
; put BASIC ROM under the CPU, causing a JAM on the very next fetch.
; modules.asm restores $01=$37 after our RTS returns to it.
ldx #0
@zprest:
lda ZP_SAVE,x
sta $3A,x
inx
cpx #6
bne @zprest
lda ZP_SAVE+6
sta $FB
lda ZP_SAVE+7
sta $FC
lda ZP_SAVE+8
sta $FD
lda ZP_SAVE+9
sta $FE
cli ; re-enable IRQ before returning
rts
; ============================================================================
; run_pass - walk entire source buffer, parse and process each line.
; ============================================================================
run_pass:
; Reset SRC_PTR to start of buffer
lda ASM_BUF_LO
sta SRC_PTR
lda ASM_BUF_HI
sta SRC_PTR+1
; Init activity spinner (once per pass; two passes = two blink cycles)
lda #0
sta ASM_SPINNER
lda #SPIN_COLOR_A
sta ASM_SPIN_IDX
sta SPIN_CELL ; show initial color immediately
; Init source frame stack: depth=0, frame 0 = buffer kind.
; Frame 0 is never popped; the SRC/LINE fields are unused for it.
; This lays the groundwork for .include without changing behaviour.
lda #0
sta SRC_DEPTH
sta SRC_STACK + SRC_FRAME_LFN
sta SRC_STACK + SRC_FRAME_SLO
sta SRC_STACK + SRC_FRAME_SHI
sta SRC_STACK + SRC_FRAME_LLO
sta SRC_STACK + SRC_FRAME_LHI
lda #SRC_KIND_BUFFER
sta SRC_STACK + SRC_FRAME_KIND
@line_loop:
; Bounds check: done if SRC_PTR >= BUF_END (or GAP_START, skipping gap)
jsr src_at_end
bne @done_pass
; Secondary check: treat a NUL byte as end-of-source.
; The editor buffer tail past the last CR may be NUL-filled.
; src_at_end only checks the address bounds; without this, the line loop
; would spin forever on NUL bytes between the last CR and BUF_END.
jsr src_peek
beq @done_pass
; Increment line counter
inc ASM_LINE_LO
bne :+
inc ASM_LINE_HI
:
; Activity spinner: tick every 16 lines, toggle color cell
inc ASM_SPINNER
lda ASM_SPINNER
and #$0F
bne :+
lda ASM_SPIN_IDX
eor #(SPIN_COLOR_A ^ SPIN_COLOR_B)
sta ASM_SPIN_IDX
sta SPIN_CELL
:
; Save SRC_PTR before parse_line (not needed now, remove later)
; Parse and process one line
jsr parse_line
; Advance SRC_PTR to next line (past CR)
jsr skip_to_next_line
jmp @line_loop
@done_pass:
rts
; ============================================================================
; parse_line - parse one source line at SRC_PTR.
; ============================================================================
parse_line:
; Skip leading spaces/tabs
jsr skip_spaces
; Peek at first char
jsr src_peek ; A = current char (or 0 at end)
bne :+
jmp @done
:
cmp #$0D
bne :+
jmp @done
:
cmp #';'
bne :+
jmp @done
:
; Check for '*=' (origin set)
cmp #'*'
bne @not_star
jsr src_advance
jsr src_peek
cmp #'='
beq :+
jmp @err_syntax
:
jsr src_advance
jsr skip_spaces
jsr parse_value ; result in TMP/TMP+1
bcc :+
jmp @err_val
:
lda TMP
sta ASM_PC_LO
lda TMP+1
sta ASM_PC_HI
; FIX (load address): capture load address from first *= seen
lda ASM_ORG_SET
bne :+
lda TMP
sta ASM_LOAD_LO
lda TMP+1
sta ASM_LOAD_HI
:
lda #$FF
sta ASM_ORG_SET
jmp @done
@not_star:
; Check for directive ('.')
cmp #'.'
bne @not_directive
jsr parse_directive
jmp @done
@not_directive:
; Check if this is a label (contains ':' before space/end)
jsr check_for_label ; C=1 if label found, label in ASM_LABEL
bcc @no_label
; Process label
lda ASM_PASS
bne @skip_label_def ; pass 2: ignore label definitions
; Pass 1: add label to symbol table with current PC
jsr sym_define
bcs @err_sym ; symbol table full or duplicate
@skip_label_def:
; Skip spaces after label
jsr skip_spaces
; Peek: might be end of line (label-only line)
jsr src_peek
bne :+
jmp @done
: cmp #$0D
bne :+
jmp @done
: cmp #';'
bne :+
jmp @done
:
@no_label:
; Parse mnemonic (3 uppercase chars)
jsr parse_mnemonic ; fills ASM_MNEM, C=1 on error
bcs @err_mnem
; Look up in opcode table
jsr lookup_mnemonic ; C=1 = not found
bcs @err_unknown
; Skip space between mnemonic and operand
jsr skip_spaces
; Save opcode table pointer (TMP2) before parse_operand, which may
; call sym_lookup and corrupt TMP2.
lda TMP2
sta ASM_OPC_PTR_LO
lda TMP2+1
sta ASM_OPC_PTR_HI
; Parse operand → ASM_MODE, ASM_VAL_LO/HI
jsr parse_operand ; C=1 on error
bcs @err_operand
; Restore opcode table pointer after parse_operand may have clobbered TMP2
lda ASM_OPC_PTR_LO
sta TMP2
lda ASM_OPC_PTR_HI
sta TMP2+1
; Find opcode for this mnemonic+mode combination
jsr find_opcode ; C=1 = mode not valid for this mnemonic
bcs @err_mode
; Compute instruction size
lda ASM_MODE
tax
lda op_size_tab,x ; operand bytes
clc
adc #1 ; +1 for opcode
sta ASM_INSTR_SIZE
; Pass 1: just advance PC
lda ASM_PASS
beq @pass1_advance
; Pass 2: emit opcode + operand
jsr emit_instruction
bcs @err_emit
jmp @done
@pass1_advance:
lda ASM_INSTR_SIZE
clc
adc ASM_PC_LO
sta ASM_PC_LO
bcc @done
inc ASM_PC_HI
jmp @done
@err_syntax:
jsr set_err_syntax
jmp @done
@err_val:
jsr set_err_value
jmp @done
@err_sym:
jsr set_err_symbol
jmp @done
@err_mnem:
jsr set_err_mnemonic
jmp @done
@err_unknown:
jsr set_err_unknown
jmp @done
@err_operand:
jsr set_err_operand
jmp @done
@err_mode:
jsr set_err_mode
jmp @done
@err_emit:
jsr set_err_io
@done:
rts
; ============================================================================
; check_for_label - scan ahead to see if current token ends with ':'
; If yes: consume the label, store 8-char padded name in ASM_LABEL, C=1.
; If no: leave SRC_PTR unchanged, C=0.
; ============================================================================
check_for_label:
; Save SRC_PTR
lda SRC_PTR
sta TMP2
lda SRC_PTR+1
sta TMP2+1
; Init ASM_LABEL to spaces
ldy #0
@pad:
lda #' '
sta ASM_LABEL,y
iny
cpy #8
bne @pad
; Read up to 8 chars, stop at non-alphanumeric/underscore
ldy #0
@read:
jsr src_peek
beq @no_label
cmp #':'
beq @found_label
cmp #' '
beq @no_label
cmp #$0D
beq @no_label
; Check valid label char: A-Z, a-z, 0-9, _
jsr is_alnum_or_under ; C=1 if valid
bcc @no_label
; Normalize to uppercase
cmp #$61
bcc @store
cmp #$7B
bcs @store
sec
sbc #$20
@store:
cpy #8
bcs @skip_char ; >8 chars: keep scanning but don't store
sta ASM_LABEL,y
iny
@skip_char:
jsr src_advance
jmp @read
@found_label:
jsr src_advance ; consume ':'
sec
rts
@no_label:
; Restore SRC_PTR
lda TMP2
sta SRC_PTR
lda TMP2+1
sta SRC_PTR+1
clc
rts
; ============================================================================
; parse_mnemonic - read 3 uppercase chars into ASM_MNEM. C=1 on error.
; ============================================================================
parse_mnemonic:
ldy #0
@loop:
jsr src_peek
beq @err
cmp #$0D
beq @err
cmp #' '
beq @err
; Convert lowercase PETSCII (a-z = $61-$7A) to uppercase (A-Z = $41-$5A)
cmp #$61
bcc @not_lower
cmp #$7B
bcs @not_lower
sec
sbc #$20 ; lowercase ? uppercase
@not_lower:
; Now accept A-Z only
cmp #'A'
bcc @err
cmp #'Z'+1
bcs @err
sta ASM_MNEM,y
jsr src_advance
iny
cpy #3
bne @loop
clc
rts
@err:
sec
rts
; ============================================================================
; lookup_mnemonic - search opcode_table for ASM_MNEM match.
; On success: C=0, TMP2 = pointer to first mode/opcode pair in table entry.
; On failure: C=1.
; ============================================================================
lookup_mnemonic:
lda #<opcode_table
sta TMP2
lda #>opcode_table
sta TMP2+1
@entry:
; Read 3-char mnemonic from table
ldy #0
lda (TMP2),y
beq @not_found ; $00 = end of table
cmp ASM_MNEM+0
bne @next
iny
lda (TMP2),y
cmp ASM_MNEM+1
bne @next
iny
lda (TMP2),y
cmp ASM_MNEM+2
bne @next
; Match! Advance TMP2 past the 3-char name to first mode/opcode pair
lda TMP2
clc
adc #3
sta TMP2
bcc :+
inc TMP2+1
:
clc
rts
@next:
; Skip to next entry: advance past 3-char name, then scan for $FF terminator
lda TMP2
clc
adc #3
sta TMP2
bcc :+
inc TMP2+1
:
@scan_ff:
ldy #0
lda (TMP2),y
cmp #$FF
beq @skip_ff
lda TMP2
clc
adc #2
sta TMP2
bcc @scan_ff
inc TMP2+1
jmp @scan_ff
@skip_ff:
; Advance past $FF
inc TMP2
bne @entry
inc TMP2+1
jmp @entry
@not_found:
sec
rts
; ============================================================================
; find_opcode - given ASM_MODE and TMP2 pointing to mode/opcode pairs,
; find matching opcode. C=0 on success (ASM_OPCODE set). C=1 on failure.
; Also handles ZP?ABS promotion if ZP mode not available.
; ============================================================================
find_opcode:
; TMP2 points to first mode/opcode pair of matched mnemonic entry
; Save TMP2 for potential retry
lda TMP2
sta TMP3
lda TMP2+1
sta TMP3+1
; First try: exact mode match
jsr @scan_for_mode
bcc @done
; If mode was ZP and not found, try ABS
lda ASM_MODE
cmp #MODE_ZP
bne @try_zpx_abx
lda #MODE_ABS
sta ASM_MODE
; Restore TMP2
lda TMP3
sta TMP2
lda TMP3+1
sta TMP2+1
jsr @scan_for_mode
bcc @done
; ABS also not found - fall through to try REL
lda #MODE_ZP ; restore original mode for @no_promo check
sta ASM_MODE
jmp @no_promo
@try_zpx_abx:
cmp #MODE_ZPX
bne @try_zpy_aby
lda #MODE_ABX
sta ASM_MODE
lda TMP3
sta TMP2
lda TMP3+1
sta TMP2+1
jsr @scan_for_mode
bcc @done
lda #MODE_ZPX
sta ASM_MODE
jmp @no_promo
@try_zpy_aby:
cmp #MODE_ZPY
bne @no_promo
lda #MODE_ABY
sta ASM_MODE
lda TMP3
sta TMP2
lda TMP3+1
sta TMP2+1
jsr @scan_for_mode
bcc @done
lda #MODE_ZPY
sta ASM_MODE
; fall through to @no_promo
@no_promo:
; If mode is ZP or ABS, also try REL (for branch instructions)
lda ASM_MODE
cmp #MODE_ZP
beq @try_rel
cmp #MODE_ABS
bne @fail
@try_rel:
lda #MODE_REL
sta ASM_MODE
lda TMP3
sta TMP2
lda TMP3+1
sta TMP2+1
jsr @scan_for_mode
bcc @done
; Restore original mode for error message
lda #MODE_ABS
sta ASM_MODE
@fail:
sec
@done:
rts
@scan_for_mode:
ldy #0
lda (TMP2),y
cmp #$FF
beq @sfm_miss
cmp ASM_MODE
beq @sfm_hit
; Advance 2 bytes
lda TMP2
clc
adc #2
sta TMP2
bcc @scan_for_mode
inc TMP2+1
jmp @scan_for_mode
@sfm_hit:
iny
lda (TMP2),y
sta ASM_OPCODE
clc
rts
@sfm_miss:
sec
rts
; ============================================================================
; parse_operand - determine addressing mode and value from source.
; Sets ASM_MODE, ASM_VAL_LO, ASM_VAL_HI. C=1 on error.
; ============================================================================
parse_operand:
; Check for end of line ? IMP
jsr src_peek
bne :+
jmp @imp
:
cmp #$0D
bne :+
jmp @imp
:
cmp #';'
bne :+
jmp @imp
:
; 'A' or 'a' alone ? ACC
jsr upcase_a
cmp #'A'
bne @not_acc
; Peek next: must be space, CR, or ';'
jsr src_advance
jsr src_peek
cmp #' '
beq @acc_ok
cmp #$0D
beq @acc_ok
cmp #';'
beq @acc_ok
beq @acc_ok
; Not just 'A' - back up and treat as label/value
; (can't easily un-advance, so just treat as error if non-label)
; Actually 'A' alone as a label is unusual - treat as ACC and hope for best
@acc_ok:
lda #MODE_ACC