-
Notifications
You must be signed in to change notification settings - Fork 0
/
krusader1.3CBS.asm
3094 lines (2852 loc) · 67.9 KB
/
krusader1.3CBS.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
; KRUSADER - An Assembly Language IDE for the Replica 1
; 6502 Version 1.3 - 23 December, 2007
; (c) Ken Wessen (ken.wessen@gmail.com)
; Notes:
; - 11 bytes free (17 free if TABTOSPACE = 0)
; - Entry points:
; SHELL = $711C($F01C)
; MOVEDN = $7304($F204)
; DEBUG = -($FE14)
; XBRK = -($FE1E) - debugger re-entry point
; DSMBL = $7BCA($FACA)
CBS6000 =1
APPLE1 =0
INROM =0 ; Not in rom
TABTOSPACE = 1
UNK_ERR_CHECK = 0
MINIMONITOR = INROM & 1
DOTRACE = MINIMONITOR & 1
BRKAS2 = 1 ; if 1, BRK will assemble to two $00 bytes
; if set, then minimonitor will work unchanged
; for both hardware and software interrupts
.if INROM
.org $F000
.else
.org $7100
.endif
.start MAIN
.if INROM
MONTOR =ESCAPE
.ELSE
.IF CBS6000
MONTOR =$E000
.else
MONTOR =$FF1F
.endif
GETLINE =MONTOR ; doesn't work in RAM version because needs adjusted monitor code
.endif
; Constants
BS =$08 ; backspace
SP =$20 ; space
CR =$0D ; carriage return
LF =$0A ; line feed
ESC =$1B ; escape
INMASK =$7F
LNMSZ =$03
LBLSZ =$06 ; labels are up to 6 characters
MNESZ =$03 ; mnemonics are always 3 characters
ARGSZ =$0E ; arguments are up to 14 characters
COMSZ =$0A ; comments fill the rest - up to 10 characters
ENDLBL =LNMSZ+LBLSZ+1
ENDMNE =ENDLBL+MNESZ+1
ENDARG =ENDMNE+ARGSZ+1
ENDLN =ENDARG+COMSZ+1
SYMSZ =$06 ; size of labels
LINESZ =$27 ; size of a line
USESZ =LINESZ-LNMSZ-1 ; usable size of a line
CNTSZ =COMM-LABEL-1 ; size of content in a line
MAXSYM =$20 ; at most 32 local symbols (256B) and
MAXFRF =$55 ; 85 forward references (896B)
; globals are limited by 1 byte index => max of 256 (2K)
; global symbol table grows downwards
; Symbols used in source code
IMV ='#' ; Indicates immediate mode value
HEX ='$' ; Indicates a hex value
OPEN ='(' ; Open bracket for indirect addressing
CLOSE =')' ; Close bracket for indirect addressing
PC ='*' ; Indicates PC relative addressing
LOBYTE ='<' ; Indicates lo-byte of following word
HIBYTE ='>' ; Indicates hi-byte of following word
PLUS ='+' ; Plus in simple expressions
MINUS ='-' ; Minus in simple expressions
DOT ='.' ; Indicates a local label
QUOTE =''' ; delimits a string
COMMA =','
CMNT =';' ; indicates a full line comment
PROMPT ='?'
EOL =$00 ; end of line marker
EOFLD =$01 ; end of field in tokenised source line
BLANK =$02 ; used to mark a blank line
PRGEND =$FE ; used to flag end of program parsing
FAIL =$FF ; used to flag failure in various searches
; Zero page storage
.if CBS6000
IOBUF =$5C ; I/O buffer for source code input and analysis
.else
IOBUF =$00 ; I/O buffer for source code input and analysis
.endif
LABEL =$04 ; offset to IOBUF, label starts here
MNE =$0B ; offset to IOBUF, mnemonic starts here
ARGS =$0F ; offset to IOBUF, arguments start here
COMM =$1D ; offset to IOBUF, comments start here
FREFTL =$29 ; address of forward reference table
FREFTH =$2A
NFREF =$2B ; number of forward symbols
RECNM =$2C ; number of table entries
RECSZ =$2D ; size of table entries
RECSIG =$2E ; significant characters in table entries
XSAV =$2F
YSAV =$30
CURMNE =$3C ; Holds the current mne index
CURADM =$3D ; Holds the current addressing mode
LVALL =$3E ; Storage for a label value
LVALH =$3F
TBLL =$40 ; address of search table
TBLH =$41
STRL =$42 ; address of search string
STRH =$43
SCRTCH =$44 ; scratch location
NPTCH =$45 ; counts frefs when patching
PTCHTL =$46 ; address of forward reference being patched
PTCHTH =$47
FLGSAV =$48
MISCL =$50 ; Miscellaneous address pointer
MISCH =$51
MISC2L =$52 ; And another
MISC2H =$53
TEMP1 =$54 ; general purpose storage
TEMP2 =$55
TEMP3 =$56
TEMP4 =$57
LMNE =TEMP3 ; alias for compression and expansion routines
RMNE =TEMP4
FRFLAG =$58 ; if nonzero, disallow forward references
ERFLAG =$59 ; if nonzero, do not report error line
HADFRF =$5A ; if nonzero, handled a forward reference
PRFLAG =$5B
; want these to persist if possible when going into the monitor
; to test code etc, so put them right up high
XQT =$E0
; these 6 locations must be contiguous
GSYMTL =$E9 ; address of the global symbol table
GSYMTH =$EA
NGSYM =$EB ; number of global symbols
LSYMTL =$EC ; address of the local symbol table
LSYMTH =$ED
NLSYM =$EE ; number of local symbols
; these 7 locations must be contiguous
REGS =$F0
SAVP =REGS
SAVS =$F1
SAVY =$F2
SAVX =$F3
SAVA =$F4
CURPCL =$F5 ; Current PC
CURPCH =$F6
CODEH =$F8 ; hi byte of code storage area (low is $00)
TABLEH =$F9 ; hi byte of symbol table area
; these 4 locations must be contiguous
LINEL =$FA ; Current source line number (starts at 0)
LINEH =$FB
CURLNL =$FC ; Current source line address
CURLNH =$FD
SRCSTL =$FE ; source code start address
SRCSTH =$FF
; for disassembler
FORMAT =FREFTL ; re-use spare locations
LENGTH =FREFTH
COUNT =NFREF
PCL =CURPCL
PCH =CURPCH
; ****************************************
; COMMAND SHELL/EDITOR CODE
; ****************************************
MAIN
.if INROM
LDA #$03
STA CODEH
LDA #$20
STA SRCSTH
LDA #$7C
STA TABLEH
.else
LDA #$03
STA CODEH
LDA #$1D
STA SRCSTH
LDA #$6D
STA TABLEH
.endif
LDX #MSGSZ
.NEXT LDA MSG-1,X
JSR OUTCH
DEX
BNE .NEXT
DEX
TXS ; reset stack pointer on startup
JSR SHINIT ; default source line and address data
; JMP SHELL
; falls through to SHELL
; ****************************************
SHELL ; Loops forever
; also the re-entry point
CLD ; just incase
LDA #$00
STA PRFLAG
JSR FILBUF
LDX #ARGS
STX FRFLAG ; set flags in SHELL
STX ERFLAG
JSR CRLF
LDA #PROMPT
JSR OUTCH ; prompt
JSR OUTSP ; can drop this if desperate for 3 more bytes :-)
.KEY JSR GETCH
CMP #BS
BEQ SHELL ; start again
CMP #CR
BEQ .RUN
JSR OUTCH
STA IOBUF,X
INX
BNE .KEY ; always branches
.RUN LDA ARGS + IOBUF
BEQ SHELL ; empty command line
LDA ARGS + IOBUF+1 ; ensure command is just a single letter
BEQ .OK
CMP #SP
BNE .ERR;SHLERR
.OK LDX #NUMCMD
.NEXT LDA CMDS-1,X ; find the typed command
CMP ARGS + IOBUF
BEQ GOTCMD
DEX
BNE .NEXT
.ERR PHA ; put dummy data on the stack
PHA
SHLERR
LDY #SYNTAX
ERR2 PLA ; need to clean up the stack
PLA
JSR SHWERR
BNE SHELL
GOTCMD JSR RUNCMD
JMP SHELL ; ready for next command
; ****************************************
SHINIT
LDA #$00
TAY
STA SRCSTL ; low byte zero for storage area
STA (SRCSTL),Y ; and put a zero in it for EOP
TOSTRT ; set LINEL,H and CURLNL,H to the start
LDA SRCSTH
STA CURLNH
LDA #$00
STA LINEL
STA LINEH ; 0 lines
STA CURLNL
RTS ; leaves $00 in A
; ****************************************
PANIC
JSR SHINIT
LDA ARGS + IOBUF+2
BNE .SKIP
LDA #$01
.SKIP STA (SRCSTL),Y ; Y is $00 from SHINIT
RTS
; ****************************************
VALUE
JSR ADDARG
BEQ SHLERR
JSR CRLF
LDA LVALH
LDX LVALL
JMP PRNTAX
; ****************************************
RUN
JSR ADDARG
BEQ SHLERR
JSR CRLF
JMP (LVALL) ; jump to the address
; ****************************************
ADDARG ; convert argument to address
LDX #$02
LDA ARGS + IOBUF,X
BEQ .NOARG
PHA
JSR EVAL
PLA
;CPX #FAIL
INX
BEQ ERR2;SHLERR
.NOARG RTS
; ****************************************
PCTOLV
LDA CURPCL
STA LVALL
LDA CURPCH
STA LVALH
RTS
; ****************************************
LVTOPC
LDA LVALL
STA CURPCL
LDA LVALH
STA CURPCH
RTS
; ****************************************
FILLSP
LDA #SP
FILBUF ; fill the buffer with the contents of A
LDX #LINESZ
.CLR STA IOBUF-1,X
DEX
BNE .CLR
RTS
; ****************************************
RUNCMD
LDA CMDH-1,X
PHA
LDA CMDL-1,X
PHA
RTS
; ****************************************
NEW
JSR SHINIT
JMP INSERT
; ****************************************
LIST ; L - list all
; L nnn - list from line nnn
JSR TOSTRT
JSR GETARG
BEQ .NEXT ; no args, list from start
JSR GOTOLN ; deal with arguments if necessary
.NEXT LDY #$00
LDA (CURLNL),Y
BEQ .RET
JSR PRNTLN
JSR UPDTCL
.if CBS6000
LDA ACIA_SR
AND #$01
.else
LDA KBDRDY
.endif
.if APPLE1
BPL .NEXT
.else
BEQ .NEXT
.endif
.if CBS6000
LDA ACIA_DAT
.else
LDA KBD
.endif
.RET RTS
; ****************************************
MEM
JSR TOEND ; set CURLNL,H to the end
JSR CRLF
LDX #$04
.LOOP LDA CURLNL-1,X
JSR OUTHEX
CPX #$03
BNE .SKIP
JSR PRDASH
.SKIP DEX
BNE .LOOP
RET RTS
; ****************************************
GETARG ; get the one or two numeric arguments
; to the list, edit, delete and insert commands
; store them in TEMP1-4 as found
; arg count in Y or X has FAIL
LDY #$00
STY YSAV
LDX #$01
.NEXT LDA ARGS + IOBUF,X
BEQ .DONE ; null terminator
CMP #SP ; find the space
BEQ .CVT
CMP #HEX ; or $ symbol
BEQ .CVT
INX
BNE .NEXT
.CVT INC YSAV ; count args
LDA #HEX
STA ARGS + IOBUF,X ; replace the space with '$' and convert
JSR CONVRT
;CPX #FAIL
INX
BEQ LCLERR
;INX
LDA LVALL
STA TEMP1,Y
INY
LDA LVALH
STA TEMP1,Y
INY
BNE .NEXT ; always branches
.DONE LDY YSAV
RTS ; m in TEMP1,2, n in TEMP3,4
; ****************************************
EDIT
JSR GETARG
;CPY #$01
DEY
BNE LCLERR
JSR DELETE ; must not overwrite the command input buffer
; JMP INSERT
; falls through to INSERT
; ****************************************
INSERT
JSR GETARG ; deal with arguments if necessary
;CPX #FAIL
INX
BEQ LCLERR
;CPY #$00 ; no args
TYA
BNE .ARGS
JSR TOEND ; insert at the end
CLC
BCC .IN
.ARGS JSR GOTOLN ; if no such line will insert at end
.IN JSR INPUT ; Get one line
CPX #FAIL ; Was there an error?
BEQ RET;
; Save the tokenised line and update pointers
; tokenised line is in IOBUF, size X
; move up from CURLNL,H to make space
STX XSAV ; save X (data size)
LDA CURLNH
STA MISCH
STA MISC2H
LDA CURLNL
STA MISCL ; src in MISCL,H now
CLC
ADC XSAV
STA MISC2L
BCC .READY
INC MISC2H ; MISC2L,H is destination
.READY JSR GETSZ
JSR MOVEUP ; do the move
LDY #$00
; now move the line to the source storage area
; Y bytes, from IOBUF to CURLN
.MOVE LDA IOBUF,Y
STA (CURLNL),Y
INY
CPY XSAV
BNE .MOVE
JSR UPDTCL ; update CURLNL,H
BNE .IN ; always branches
; ****************************************
LCLERR ; local error wrapper
; shared by the routines around it
JMP SHLERR
; ****************************************
GETSZ ; SIZE = TEMP1,2 = lastlnL,H - MISCL,H + 1
LDX #-$04
.LOOP LDA CURLNH+1,X
PHA ; save CURLN and LINEN on the stack
INX
BNE .LOOP
JSR TOEND
SEC
LDA CURLNL
SBC MISCL
STA TEMP1
LDA CURLNH
SBC MISCH
STA TEMP2
INC TEMP1
BNE .SKIP
INC TEMP2
.SKIP LDX #$04
.LOOP2 PLA ; get CURLN and LINEN from the stack
STA LINEL-1,X
DEX
BNE .LOOP2
RTS
; ****************************************
DELETE ; Delete the specified range
; Moves from address of line arg2 (MISCL,H)
; to address of line arg1 (MISC2L,H)
JSR GETARG
; CPY #$00
BEQ LCLERR
STY YSAV
.DOIT JSR GOTOLN ; this leaves TEMP1 in Y and TEMP2 in X
CPX #FAIL
BEQ LCLERR
LDA CURLNL
STA MISC2L
LDA CURLNH
STA MISC2H ; destination address is set in MISC2L,H
LDA YSAV
;CMP #$01
LSR
BEQ .INC
LDX TEMP4
LDY TEMP3 ; Validate the range arguments
CPX TEMP2 ; First compare high bytes
BNE .CHK ; If TEMP4 != TEMP2, we just need to check carry
CPY TEMP1 ; Compare low bytes when needed
.CHK BCC LCLERR ; If carry clear, 2nd argument is too low
.INC INY ; Now increment the second argument
BNE .CONT
INX
.CONT STX TEMP2
STY TEMP1
JSR GOTOLN
LDA CURLNL
STA MISCL
LDA CURLNH
STA MISCH
JSR GETSZ
; JMP MOVEDN
; falls through
; ****************************************
; Memory moving routines
; From http://www.6502.org/source/general/memory_move.html
; ****************************************
; Some aliases for the following two memory move routines
FROM =MISCL ; move from MISCL,H
TO =MISC2L ; to MISCL2,H
SIZEL =TEMP1
SIZEH =TEMP2
MOVEDN ; Move memory down
LDY #$00
LDX SIZEH
BEQ .MD2
.MD1 LDA (FROM),Y ; move a page at a time
STA (TO),Y
INY
BNE .MD1
INC FROM+1
INC TO+1
DEX
BNE .MD1
.MD2 LDX SIZEL
BEQ .MD4
.MD3 LDA (FROM),Y ; move the remaining bytes
STA (TO),Y
INY
DEX
BNE .MD3
.MD4 RTS
MOVEUP ; Move memory up
LDX SIZEH ; the last byte must be moved first
CLC ; start at the final pages of FROM and TO
TXA
ADC FROM+1
STA FROM+1
CLC
TXA
ADC TO+1
STA TO+1
INX ; allows the use of BNE after the DEX below
LDY SIZEL
BEQ .MU3
DEY ; move bytes on the last page first
BEQ .MU2
.MU1 LDA (FROM),Y
STA (TO),Y
DEY
BNE .MU1
.MU2 LDA (FROM),Y ; handle Y = 0 separately
STA (TO),Y
.MU3 DEY
DEC FROM+1 ; move the next page (if any)
DEC TO+1
DEX
BNE .MU1
RTS
; ****************************************
TOEND
LDA #$FF
STA TEMP2 ; makes illegal line number
; JMP GOTOLN ; so CURLNL,H will be set to the end
; falls through
; ****************************************
GOTOLN ; go to line number given in TEMP1,2
; sets CURLNL,H to the appropriate address
; and leaves TEMP1 in Y and TEMP2 in X
; if not present, return #FAIL in X
; and LINEL,H will be set to the next available line number
EOP = LFAIL
GOTIT = LRET
JSR TOSTRT
.NXTLN ; is the current line number the same
; as specified in TEMP1,2?
; Z set if equal
; C set if TEMP1,2 >= LINEL,H
LDY TEMP1
CPY LINEL
BNE .NO
LDX TEMP2
CPX LINEH
BEQ GOTIT
.NO LDY #$FF
.NXTBT INY ; find EOL
LDA (CURLNL),Y
BNE .NXTBT
TYA
;CPY #$00
BEQ EOP ; null at start of line => end of program
INY
JSR UPDTCL ; increment CURLNL,H by Y bytes
BNE .NXTLN ; always branches
;.EOP LDX #FAIL
;.GOTIT RTS ; address is now in CURLNL,H
; ****************************************
PRNTLN ; print out the current line (preserve X)
JSR CRLF
STX XSAV
JSR DETKN
INY
JSR PRLNNM
LDX #$00
.PRINT LDA LABEL + IOBUF,X
BEQ .DONE ; null terminator
JSR OUTCH
INX
;CPX #USESZ
BNE .PRINT
.DONE LDX XSAV
RTS
; ****************************************
NEXTCH ; Check for valid character in A
; Also allows direct entry to appropriate location
; Flag success with C flag
JSR GETCH
.if TABTOSPACE
CMP #$09 ; is it a tab?
BNE .SKIP
LDA #SP
.endif
.SKIP CMP #SP ; valid ASCII range is $20 to $5D
BPL CHANM ; check alpha numeric entries
TAY
PLA
PLA
PLA
PLA ; wipe out return addresses
CPY #BS
BEQ INPUT ; just do it all again
.NOBS CPY #CR
BNE LFAIL
CPX #LABEL ; CR at start of LABEL means a blank line
BEQ DOBLNK
LDA #EOL
STA IOBUF,X
BEQ GOTEOL
LFAIL LDX #FAIL ; may flag error or just end
LRET RTS
CHANM CPX #LINESZ ; ignore any characters over the end of the line
BPL CHNO
CMP #']'+1 ; is character is in range $20-$5D?
BPL CHNO ; branch to NO...
CHOK SEC ; C flag on indicates success
RTS
CHKLBL CMP #DOT ; here are more specific checks
BEQ CHOK
CHKALN CMP #'0' ; check alpha-numeric
BMI CHNO ; less than 0
CMP #'9'+1
BMI CHOK ; between 0 and 9
CHKLET CMP #'A'
BMI CHNO ; less than A
CMP #'Z'+1
BMI CHOK ; between A and Z
CHNO CLC
RTS ; C flag off indicates failure
; ****************************************
DOBLNK
LDA #BLANK
TAX ; BLANK = #$02, and that is also the
STA IOBUF ; tokenised size of a blank line
LDA #EOL ; (and only a blank line)
STA IOBUF+1
ENDIN RTS
INPUT
JSR FILLSP
LDA #EOL ; need this marker at the start of the comments
STA COMM + IOBUF ; for when return hit in args field
JSR CRLF
JSR PRLNNM
LDX #LABEL ; point to LABEL area
LDA #ENDLBL
JSR ONEFLD
JSR INSSPC ; Move to mnemonic field
LDA LABEL + IOBUF
CMP #CMNT
BEQ .CMNT
LDA #ENDMNE
JSR ONEFLD
JSR INSSPC ; Move to args field
LDA #ENDARG
JSR ONEFLD
.CMNT LDA #EOL
JSR ONEFLD
GOTEOL ;JMP TOTKN
; falls through
; ****************************************
TOTKN ; tokenise to IOBUF to calc size
; then move memory to make space
; then copy from IOBUF into the space
LDX #$00
STX MISCH
LDA #SP
STA TEMP2
LDA #LABEL + IOBUF
STA MISCL
LDA #EOFLD
STA TEMP1
JSR TKNISE
LDY LABEL + IOBUF
CPY #CMNT
BNE .CONT
LDA #MNE + IOBUF
BNE ISCMNT ; always branches
.CONT TXA ; save X
PHA
; JSR SRCHMN ; is it a mnemonic?
SRCHMN ; Search the table of mnemonics for the mnemonic in MNE
; Return the index in A
CMPMNE ; compress the 3 char mnemonic
; at MNE to MNE+2 into 2 chars
; at LMNE and RMNE
CLC
ROR LMNE
LDX #$03
.NEXT2 SEC
LDA MNE + IOBUF-1,X
SBC #'A'-1
LDY #$05
.LOOP2 LSR
ROR LMNE
ROR RMNE
DEY
BNE .LOOP2
DEX
BNE .NEXT2
LDX #NUMMN ; Number of mnemonics
.LOOP LDA LMNETB-1,X
CMP LMNE
BNE .NXT
LDA RMNETB-1,X
CMP RMNE
BEQ .FND
.NXT DEX
BNE .LOOP
.FND DEX ; X = $FF for failure
; RTS
TXA
CMP #FAIL
BNE .FOUND
LDA MNE + IOBUF ; or a directive?
CMP #DOT
BNE .ERR
LDX #NUMDIR
LDA MNE + IOBUF+1
.NEXT CMP DIRS-1,X
BEQ .FDIR
DEX
BNE .NEXT
.ERR PLA
LDY #INVMNE
JMP SHWERR
.FDIR DEX
.FOUND TAY ; put mnemonic/directive code in Y
INY ; offset by 1 so no code $00
PLA ; restore Y
TAX
STY IOBUF,X
INX
LDA #ARGS + IOBUF
STA MISCL
; LDA #EOFLD
; STA TEMP1
JSR TKNISE
STX XSAV
INC XSAV
LDA #COMM + IOBUF
ISCMNT STA MISCL
LDA #EOL
STA TEMP1
STA TEMP2
JSR TKNISE
CPX XSAV
BNE .RET
DEX ; no args or comments, so stop early
STA IOBUF-1,X ; A already holds $00
.RET RTS
ONEFLD ; do one entry field
; A holds the end point for the field
STA TEMP1 ; last position
.NEXT JSR NEXTCH ; catches ESC, CR and BS
BCC .NEXT ; only allow legal keys
JSR OUTCH ; echo
STA IOBUF,X
INX
CMP #SP
BEQ .FILL
CPX TEMP1
BNE .NEXT
.RET RTS
.FILL LDA TEMP1
BEQ .NEXT ; just treat a space normally
CPX TEMP1 ; fill spaces
BEQ .RET
LDA #SP
STA IOBUF,X
JSR OUTCH
.CONT INX
BNE .FILL ; always branches
; ****************************************
INSSPC
LDA IOBUF-1,X ; was previous character a space?
CMP #SP
BEQ .JUMP
.GET JSR NEXTCH ; handles BS, CR and ESC
CMP #SP
BNE .GET ; only let SP through
.JUMP STA IOBUF,X ; insert the space
INX
JMP OUTCH
TKNISE
LDY #$00
.NEXT LDA (MISCL),Y
BEQ .EOF
CMP TEMP2
BEQ .EOF ; null terminator
STA IOBUF,X
INX
INC MISCL
BNE .NEXT
.EOF LDA TEMP1
STA IOBUF,X
INX
RTS
; ****************************************
DETKN ; Load a line to the IOBUF
; (detokenising as necessary)
; On return, Y holds tokenised size
JSR FILLSP
LDY #$00
LDX #LABEL
.LBL LDA (CURLNL),Y
BEQ .EOP ; indicates end of program
CMP #BLANK
BNE .SKIP
INY
LDA #EOL
BEQ .EOL
.SKIP CMP #EOFLD
BEQ .CHK
STA IOBUF,X
INX
INY
BNE .LBL
.CHK LDA LABEL + IOBUF
CMP #CMNT
BNE .NEXT
LDX #MNE
BNE .CMNT ; always branches
.NEXT INY
LDA (CURLNL),Y ; get mnemonic code
TAX
DEX ; correct for offset in tokenise
STX CURMNE ; store mnemonic for assembler
CPX #NUMMN
BPL .DIR
TYA ; save Y
PHA
JSR EXPMNE
PLA ; restore Y
TAY
BNE .REST
.DIR STX MNE + IOBUF+1
LDA #DOT
STA MNE + IOBUF
.REST INY
LDX #ARGS ; point to ARGS area
.LOOP LDA (CURLNL),Y
BEQ .EOL ; indicates end of line
CMP #EOFLD
BNE .CONT
INY
LDX #COMM ; point to COMM area
BNE .LOOP
.CONT STA IOBUF,X
INX
.CMNT INY
BNE .LOOP
.EOP LDX #PRGEND
.EOL STA IOBUF,X
RTS
; ****************************************
; ASSEMBLER CODE
; ****************************************
ASSEM ; Run an assembly
JSR INIT ; Set the default values
JSR CRLF
JSR MYPRPC
.NEXT JSR DO1LN ; line is in the buffer - parse it
;CPX #FAIL
INX
BEQ SHWERR
CPX #PRGEND+1 ; +1 because of INX above
BNE .NEXT