-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathca65hl.h
2926 lines (2646 loc) · 115 KB
/
ca65hl.h
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
; --------------------------------------------------------------------------------------------
; https://mit-license.org/
; Copyright © 2022 Julian Terrell [email protected]
;
; Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
; documentation files (the “Software”), to deal in the Software without restriction, including without limitation
; the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
; and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above
; copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
; TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
; CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
; DEALINGS IN THE SOFTWARE.
; --------------------------------------------------------------------------------------------
; File: ca65hl.h
; Section: ca65hl
;
; This is a recreation of ca65 macros to allow for some high-level like structured code for branches and loops.
; This code started (rewritten) around Feb 2022
;
; This file is only macro code, intended to add functionality. No memory is used and no supporting 6502 code needed.
;
; Some macros are intended to be 'private' to this file. They are are prefixed with a triple underscore.
;
; Macros for use outside of this file:
;
; > setBranch
; > setLongBranch
; > mb (Move Byte)
; > mw (Move Word)
; > if
; > elseif
; > else
; > endif
; > do..while
; > repeat..until
; > forever
; > while <> do
; > endwhile
; > break
; > continue
; > for..next
; > switch..case..endswitch
; --------------------------------------------------------------------------------------------
; Note: types for ___loopType and corresponding labels
;
; ___loopType Start of Structure
;
; "FOR" -> "FOR_%04X_START"
; "DO_WHILE" -> "DO_WHILE_%04X_START"
; "WHILE_DO_ENDWHILE" -> "WHILE_DO_ENDWHILE_%04X_START"
; "SWITCH" -> No corresponding start label
;
; Stack names:
;
; ___loopType Stack names
;
; "FOR" -> "FOR_STATEMENT_STACK"
; "DO_WHILE" -> "DO_WHILE_STATEMENT_STACK"
; "WHILE_DO_ENDWHILE" -> "WHILE_DO_ENDWHILE_STATEMENT_STACK"
; "SWITCH" -> "SWITCH_STATEMENT_STACK"
; --------------------------------------------------------------------------------------------
.ifndef ::CA65HL_H
::CA65HL_H = 1
; Set the following to 0 to disable customSyntax for instructions:
; ::CA65HL_USE_CUSTOM_SYNTAX = 0
; Set the following for warnings from ca65hl: (0 to 2)
; ::CA65HL_WARNING_LEVEL = 0
; Set the following for console output for debugging ca65hl
; ::CA65HL_DEBUG = 1
; --------------------------------------------------------------------------------------------
; Include macros to enable custom array syntax for instructions
.ifndef ::CA65HL_USE_CUSTOM_SYNTAX
::CA65HL_USE_CUSTOM_SYNTAX = 1
.endif
; Warn in some cases if turned on:
.ifndef ::CA65HL_WARNING_LEVEL
::CA65HL_WARNING_LEVEL = 2
.endif
; debugging for this file only. Change this to '1' for some console output.
.ifndef ::CA65HL_DEBUG
::CA65HL_DEBUG = 0
.endif
; check for funcCall.h - I don't remember why this is important
.ifdef ::_FUNC_CALL_H
.warning "ca65hl.h should be included before funcCall.h"
.endif
; --------------------------------------------------------------------------------------------
.include "stacks.h" ; macros that allow for named stacks
.include "tokeneval.h" ; macros to make token evaluation easier
.include "debug.h" ; macros to help with debugging
.if ::CA65HL_USE_CUSTOM_SYNTAX
.include "customSyntax.h" ; macros to enable allow custom syntax for instructions
.endif
; --------------------------------------------------------------------------------------------
; symbols to track some values for if, loops, etc
.scope FLOW_CONTROL_VALUES
IF_STATEMENT_COUNT .set 0 ; if statement label counter - always incremented after every 'if'
LAST_ENDIF_COUNT .set 0 ; keep track of the IF count of last IF-ENDIF block
DO_WHILE_STATEMENT_COUNT .set 0 ; while loop counter
WHILE_DO_ENDWHILE_STATEMENT_COUNT .set 0 ; while..do endwhile counter
FOR_STATEMENT_COUNT .set 0 ; for statement counter
SWITCH_STATEMENT_COUNT .set 0 ; switch statement counter
SWITCH_STATEMENT_DATA_SEG_DEFINED .set 0 ; flag: if on, use data segment defined by switch_SetDataSeg
NEGATE_CONDITION .set 0 ; flag: if on, conditions are inverted
IF_STATEMENT_ACTIVE .set 0 ; flag: if executing an 'if' macro (no calling an 'if' while a condition is being processed)
LONG_JUMP_ACTIVE .set 0 ; flag: use JMP to branch
LONG_JUMP_WARNINGS .set 1 ; flag: output warnings if long jump not needed
INTERNAL_CALL .set 0 ; flag: if on, 'if' macro being invoked from this file.
LOOP_TYPE_DEFINED .set 0 ; flag: only undefine ___loopType if set
PRINT_LABELS .set 0 ; send macro generated labels to console
MB_RIGHT_SIDE_ONLY .set 0
.endscope
; --------------------------------------------------------------------------------------------
; Substitutes for branch mnemonics. Edit or add to as desired.
; 'set' or 'clear' can be added after keywords when in use.
; 'set' will have no effect, 'clear' will invert the flag.
.define less !C
.define greaterORequal C
.define carry C
.define zero Z
.define equal Z
.define plus !N
.define positive !N
.define minus N
.define negative N
.define bit7 N
.define overflow V
.define bit6 V
.define bitset !Z
.define greater G ; Use greater and less/equal macros
.define lessORequal !G
; --------------------------------------------------------------------------------------------
; Function: printTokenListDebugCA65HL
;
; Parameters:
;
; parameter - token list to be displayed. Enclose in {} to include commas
;
; If the option is enabled, ca65hl will display some information to the console
; on what it is processing.
.macro printTokenListDebugCA65HL parameter
.if ::CA65HL_DEBUG
printTokenList {parameter}
.endif
.endmacro
; --------------------------------------------------------------------------------------------
; Function: ca65hl_Listing param, tok, position
;
; Parameters:
;
; opt1 - Must be 'on' or 'off' (not in quotes) to turn listing on or off.
; comment - String to print as a comment to the console
;
; This macro turns on or off the listing of instructions and labels to the console.
.macro ca65hl_Listing opt1, comment
.if .xmatch( opt1, on )
FLOW_CONTROL_VALUES::PRINT_LABELS .set 1
.elseif .xmatch( opt1, off )
FLOW_CONTROL_VALUES::PRINT_LABELS .set 0
.else
.error "Invalid option. Should be: 'on' or 'off'."
.endif
.if ::CA65HL_USE_CUSTOM_SYNTAX
customSyntax_Output opt1
.endif
.ifnblank comment
.out .concat("; ", comment)
.endif
.endmacro
; --------------------------------------------------------------------------------------------
; Function: ___emitLabel
;
; Parameters:
;
; labelName - name of label as a string
;
; This macro creates a label with the name of the string, as well as optionally
; printing the label to the console.
.macro ___emitLabel labelName
.if FLOW_CONTROL_VALUES::PRINT_LABELS
.out .concat(labelName, ":")
.endif
.ident(labelName):
.endmacro
; --------------------------------------------------------------------------------------------
; Function: ___findToken param, tok, position
;
; Parameters:
;
; param - Token list to search through.
; tok - Token to find.
; position - Passed identifier to store found position in.
; Should be initialized to zero to search the entire token list.
;
; Note:
; It won't find the token in the very first (0) position!
; This macro is defined elsewhere in my source, so check if defined first.
.if !.definedmacro( ___findToken )
.macro ___findToken param, tok, position
.repeat .tcount( {param} ), c
.if .xmatch( {.mid(c, 1, {param}) }, {tok})
.if !position ; do not remove this check
position .set c
.exitmacro
.endif
.endif
.endrepeat
.endmacro
.endif
; --------------------------------------------------------------------------------------------
; Function: ___error
;
; Parameters:
;
; message - Error message to display.
;
; This macro will output an error message and halt assembly.
; (.fatal doesn't output line numbers)
.macro ___error message
.error message
.fatal "Halting"
.endmacro
; --------------------------------------------------------------------------------------------
; Function: branchOnGreater
;
; Parameters:
;
; label - Label to branch to.
;
; Simulate a branch for 'G' flag set: 'greater'
.macro branchOnGreater label
beq :+
bcs label
:
.endmacro
; --------------------------------------------------------------------------------------------
; Function: branchOnLessOrEqual
;
; Parameters:
;
; label - Label to branch to.
;
; Simulate a branch for 'G' flag clear: 'less or equal'
.macro branchOnLessOrEqual label
beq label
bcc label
.endmacro
; --------------------------------------------------------------------------------------------
; Branch macros section - to define the next branch instruction to be generated
; --------------------------------------------------------------------------------------------
; keep track if a branch has been defined for the next ___Branch macro
.scope ___branchSet
branchDefined .set 0
.endscope
; --------------------------------------------------------------------------------------------
; Inline C-style defines that resolve to instructions.
; Used with macro <___Branch>
; Branch instructions:
; C, Z, N, V, G flag can be followed by the keyword 'set' or 'clear'.
.define BranchOn_C_set bcs
.define BranchOn_C_clear bcc
.define BranchOn_Z_set beq
.define BranchOn_Z_clear bne
.define BranchOn_N_set bmi
.define BranchOn_N_clear bpl
.define BranchOn_V_set bvs
.define BranchOn_V_clear bvc
.define BranchOn_G_set branchOnGreater
.define BranchOn_G_clear branchOnLessOrEqual
; --------------------------------------------------------------------------------------------
; Function: ___Branch (F, S, label)
;
; Parameters:
;
; F - Flag to indicate branch instruction: C, Z, N, V, G
; S - Status of flag: 'set' or 'clear'
; label - label to branch to
;
; This inline macro will expand to a branch instruction.
; (uses .left to turn .ident into a token list: ca65 will recognized as an above .define)
.define ___Branch (___F, ___S, label) .left(1, .ident( .sprintf("BranchOn_%s_%s", .string(___F), .string(___S)))) label
; --------------------------------------------------------------------------------------------
; Function: setBranchFlag f, setBranchCondition s
;
; Parameters:
;
; f - Set flag for next invocation of <___Branch>
; s - Set flag status for next invocation of <___Branch>
;
; Individually define branch flag and flag status for next branch.
.macro setBranchFlag _f
___branchSet::branchDefined .set 1
.define branchFlag _f
.endmacro
.macro setBranchCondition _s
.define branchCondition _s
.endmacro
; --------------------------------------------------------------------------------------------
; Function: ___setBranch branch
;
; Parameters:
;
; branch - Set flag and status for next invocation of <___Branch>
;
; Define both branch flag and flag status for next branch. Also set
; ___branchSet::branchDefined flag that they are set. Used internally with no error checking.
.macro ___setBranch branch
___branchSet::branchDefined .set 1
setBranchFlag {.left(1,branch)}
setBranchCondition {.right(1,branch)}
.endmacro
; --------------------------------------------------------------------------------------------
; Function: setBranch branch
;
; Parameters:
;
; branch - Set flag and status for next invocation of <___Branch>
;
; Define both branch flag and flag status for next branch. Also set ___branchSet::branchDefined
; flag that they are set. Ignore if branch is already defined.
; This allows overriding user macro calls to setBranch with inline branch definition via '==', '!='
; Does some error checking for the user.
.macro setBranch branch
.if !___branchSet::branchDefined
___branchSet::branchDefined .set 1
.if .xmatch(.left(1,{branch}), !) ; check for !, skip
.define ___BRANCH() .mid(1, .tcount({branch}) - 1, {branch})
.else
.define ___BRANCH() branch
.endif
; error check: must be C Z N V G
.if !(.xmatch( {.left(1,___BRANCH)}, C) || .xmatch( {.left(1,___BRANCH)}, Z) || .xmatch( {.left(1,___BRANCH)}, N) || .xmatch( {.left(1,___BRANCH)}, V) || .xmatch( {.left(1,___BRANCH)}, G))
___error "Expected: Valid flag: C, Z, N, V, G"
.endif
setBranchFlag {.left(1,___BRANCH)}
.if .tcount({___BRANCH}) > 1
.if !(.xmatch( {.right(1,___BRANCH)}, set) || .xmatch( {.right(1,___BRANCH)}, clear))
___error "Expected: 'set' or 'clear'"
.endif
setBranchCondition {.right(1,___BRANCH)}
.else
setBranchCondition set
.endif
.if .xmatch(.left(1,{branch}), !)
___invertBranchCondition
.endif
.undefine ___BRANCH
.endif
.endmacro
; --------------------------------------------------------------------------------------------
; Function: ___invertBranchCondition
;
; Parameters: none
;
; Invert current branch setting
.macro ___invertBranchCondition
.define ___tempCondition branchCondition
.undefine branchCondition
.if .xmatch (___tempCondition, set)
.define branchCondition clear
.else
.define branchCondition set
.endif
.undefine ___tempCondition
.endmacro
; --------------------------------------------------------------------------------------------
; Function: ___clearBranchSet
;
; Parameters: none
;
; Clear defines after a branch is output as code. Clear that branch is defined.
.macro ___clearBranchSet
___branchSet::branchDefined .set 0
.undefine branchFlag
.undefine branchCondition
.endmacro
; --------------------------------------------------------------------------------------------
; Function: setLongBranch
;
; Parameters:
;
; l - long branch setting: 'on' or '+' to turn on, 'off' or '-' to turn off
; v - long branch warnings: 'on' or '+' to turn on, 'off' or '-' to turn off
;
; If long branch turned on, JMP will be output to branch to a user label, break, or endif.
; If warnings turned on an .assert will output a warning for branches that could have
; been reached without a JMP instruction.
.macro setLongBranch l, v
.if .xmatch(l, on) || .xmatch(l, +)
FLOW_CONTROL_VALUES::LONG_JUMP_ACTIVE .set 1
.elseif .xmatch(l, off) || .xmatch(l, -)
FLOW_CONTROL_VALUES::LONG_JUMP_ACTIVE .set 0
.else
___error "Unknown long branch setting."
.endif
.ifnblank v
.if .xmatch(v, on) || .xmatch(v, +)
FLOW_CONTROL_VALUES::LONG_JUMP_WARNINGS .set 1
.elseif .xmatch(v, off) || .xmatch(v, -)
FLOW_CONTROL_VALUES::LONG_JUMP_WARNINGS .set 0
.else
___error "Unknown long branch setting."
.endif
.endif
.endmacro
; --------------------------------------------------------------------------------------------
; Function: deprecateLongBranch
;
; Parameters:
;
; none
;
; If long or short branch forced, will be ignored for backward branches.
; Warn that this is the case. This will be removed in future versions.
.macro deprecateLongBranch
.warning "Long branch option depreciated for backward branches. Backward branches automatically generated!"
.endmacro
; --------------------------------------------------------------------------------------------
; SECTION: Compare
.scope ___compare
found .set 0
operator .set 0
position .set 0
regFound .set 0
.enum
EQUAL = 1
NEQUAL
GREATEREQUAL
LESSEQUAL
GREATER
LESS
.endenum
.endscope
; --------------------------------------------------------------------------------------------
.macro ___findCompareOperator exp
.local pos
.local op
.local bracketCount
;default:
pos .set 0
op .set 0
bracketCount .set 0
___compare::found .set 0
___compare::operator .set 0
___compare::position .set 0
.repeat .tcount({exp}), i
.if !op
.if .xmatch ( {.mid(i,1,{exp})}, {(} )
bracketCount .set bracketCount + 1
.elseif .xmatch ( {.mid(i,1,{exp})}, {)} )
bracketCount .set bracketCount - 1
.endif
.if bracketCount = 0
.if .xmatch ( {.mid(i,1,{exp})}, = )
op .set ___compare::EQUAL
.elseif .xmatch ( {.mid(i,1,{exp})}, <> )
op .set ___compare::NEQUAL
.elseif .xmatch ( {.mid(i,1,{exp})}, >= )
op .set ___compare::GREATEREQUAL
.elseif .xmatch ( {.mid(i,1,{exp})}, <= )
op .set ___compare::LESSEQUAL
.elseif .xmatch ( {.mid(i,1,{exp})}, > )
op .set ___compare::GREATER
.elseif .xmatch ( {.mid(i,1,{exp})}, < )
op .set ___compare::LESS
.endif
.endif
pos .set i
.endif
.endrepeat
.if op
___compare::found .set 1
___compare::operator .set op
___compare::position .set pos
.endif
.endmacro
; --------------------------------------------------------------------------------------------;
; called by IF when compare operator found in a statement
; ___compare::operator and ___compare::position must be set first
;
.macro ___doCompare exp
.local left
.local right
.local REGA, REGX, REGY
REGA = 1
REGX = 2
REGY = 3
.define _LEFT () .mid (0, ___compare::position, {exp})
.define _RIGHT () .mid (___compare::position + 1, .tcount({exp}) - ___compare::position - 1, {exp})
; first look for a register
; check if register on the right side. Left is always the register.
left .set 0
.if .xmatch (_RIGHT, a)
left .set REGA
.elseif .xmatch (_RIGHT, x)
left .set REGX
.elseif .xmatch (_RIGHT, y)
left .set REGY
.endif
; if we found a register on the right side, pretend it is on the left by switching the compare and the defines
.if left
.if ___compare::operator = ___compare::GREATER
___compare::operator .set ___compare::LESS
.elseif ___compare::operator = ___compare::GREATEREQUAL
___compare::operator .set ___compare::LESSEQUAL
.endif
; switch sides
.define _TEMPRIGHT _RIGHT
.undefine _RIGHT
.define _RIGHT _LEFT
.undefine _LEFT
.define _LEFT _TEMPRIGHT
.undefine _TEMPRIGHT
.else
.if .xmatch (_LEFT, a)
left .set REGA
.elseif .xmatch (_LEFT, x)
left .set REGX
.elseif .xmatch (_LEFT, y)
left .set REGY
.endif
.endif
; no registers found, so first we will load a register, or eval an expression
.if !left
.define _LEFT1 .left (1, {_LEFT})
; look for characters, then see if they match a supported instruction
.if .match( _LEFT1, abc)
.if .xmatch(_LEFT1,lda)
left .set REGA
.elseif .xmatch(_LEFT1,ldx)
left .set REGX
.elseif .xmatch(_LEFT1,ldy)
left .set REGY
.elseif .xmatch(_LEFT1,inx)
left .set REGX
.elseif .xmatch(_LEFT1,dex)
left .set REGX
.elseif .xmatch(_LEFT1,iny)
left .set REGY
.elseif .xmatch(_LEFT1,dey)
left .set REGY
.endif
; if match found, output the instruction
.if left
_LEFT1 {.right( .tcount({_LEFT}) - 1, {_LEFT} )}
.endif
.endif
.if !left
___evalOperations {_LEFT}
left .set ___compare::regFound
.endif
.undefine _LEFT1
.endif
.if !left
___error "Unknown register to use in comparison macro."
.exitmacro
.endif
.if left = REGA
cmp _RIGHT
.elseif left = REGX
cpx _RIGHT
.elseif left = REGY
cpy _RIGHT
.endif
.if ___compare::operator = ___compare::GREATER
___setBranch G set
.elseif ___compare::operator = ___compare::LESS
___setBranch C clear
.elseif ___compare::operator = ___compare::EQUAL
___setBranch Z set
.elseif ___compare::operator = ___compare::GREATEREQUAL
___setBranch C set
.elseif ___compare::operator = ___compare::LESSEQUAL
___setBranch G clear
.elseif ___compare::operator = ___compare::NEQUAL
___setBranch Z clear
.endif
.undefine _LEFT
.undefine _RIGHT
.endmacro
; --------------------------------------------------------------------------------------------
; .macro mb section
.macro ___evalOperations exp
FLOW_CONTROL_VALUES::MB_RIGHT_SIDE_ONLY .set 1
mb {exp}
FLOW_CONTROL_VALUES::MB_RIGHT_SIDE_ONLY .set 0
.endmacro
.macro ___mb_findNextOp
.local sBracketCount, bracketCount
sBracketCount .set 0
bracketCount .set 0
.repeat .tcount({EXP}) - currentTokenNumber
.if (!( xmatchToken { + } || xmatchToken { - } || xmatchToken { & } || xmatchToken { | } || xmatchToken { << } || xmatchToken { >> } || xmatchToken { ^ } )) || sBracketCount || bracketCount
sBracketCount .set sBracketCount + xmatchToken { [ } - xmatchToken { ] }
bracketCount .set bracketCount + xmatchToken { ( } - xmatchToken { ) }
nextToken
.endif
.endrepeat
.if bracketCount || sBracketCount
___error "Mismatched () or []"
.endif
.endmacro
.macro mb withReg, exp
.local leftSideTokenCount, rightSideToLoadTokenCount
.local pendingLoadRightSide
.local leftReg, rightReg
.local simpleXYIncrement
.local requestReg
.local pos
.local REGA, REGX, REGY
REGA = 1
REGX = 2
REGY = 3
pendingLoadRightSide .set 0
simpleXYIncrement .set 0
requestReg .set 0
rightReg .set 0
leftReg .set 0
pos .set 0
; -1 will allow proper operation when called from ___evalOperations
leftSideTokenCount .set -1
.ifblank exp
.define EXP () withReg
.else
; register to use passed in withReg:
.define EXP () exp
.ifnblank withReg
.if .xmatch({withReg}, a)
requestReg .set REGA
.elseif .xmatch({withReg}, x)
requestReg .set REGX
.elseif .xmatch({withReg}, y)
requestReg .set REGY
.else
___error "Unknown register."
.endif
.endif
.endif
printTokenListDebugCA65HL {MB_:REG_: withReg | EXP_:exp}
startTokenListEval {EXP}
previousToken
verifyNextToken { a x y abc :: # ( }, "Register, identifier or immediate value expected."
nextToken
.if !FLOW_CONTROL_VALUES::MB_RIGHT_SIDE_ONLY
printTokenListDebugCA65HL { mb_ first token: currentToken}
allowAllTokens
; is it a register? (zero is default)
.if xmatchToken { a } || xmatchToken { x } || xmatchToken { y }
.if xmatchToken { a }
leftReg .set REGA
.elseif xmatchToken { x }
leftReg .set REGX
.else
leftReg .set REGY
.endif
leftSideTokenCount .set 1
.else
; skip until = or := found
.repeat .tcount({EXP})
.if (!xmatchToken { = }) && (!xmatchToken { := })
nextToken
.endif
.endrepeat
leftSideTokenCount .set currentTokenNumber
previousToken
.endif
verifyNextToken { = := }, "Assignment with '=' or ':=' expected."
nextToken; skip the =, :=
verifyNextToken { a x y abc :: # ( }, "Register, identifier or immediate value expected."
nextToken
.endif
; is there a register on the right?
.if xmatchToken { a }
rightReg .set REGA
.elseif xmatchToken { x }
rightReg .set REGX
.elseif xmatchToken { y }
rightReg .set REGY
.else
allowAllTokens
___mb_findNextOp
rightSideToLoadTokenCount .set currentTokenNumber - leftSideTokenCount - 1
pendingLoadRightSide .set 1
previousToken
.endif
verifyNextToken { + - & | << >> ^ }, "Operation expected."
nextToken
allowAllTokens
saveTokenListPosition
; check for special cases that allow constants after the operator
.if xmatchToken { + }
nextToken
.if ( rightReg = REGX || rightReg = REGY ) && matchToken { 123 }
.if rightReg = REGX
.repeat currentToken
inx
.endrepeat
.else
.repeat currentToken
iny
.endrepeat
.endif
simpleXYIncrement .set 1
.endif
.elseif xmatchToken { - }
nextToken
.if ( rightReg = REGX || rightReg = REGY ) && matchToken { 123 }
.if rightReg = REGX
.repeat currentToken
dex
.endrepeat
.else
.repeat currentToken
dey
.endrepeat
.endif
simpleXYIncrement .set 1
.endif
.endif
restoreTokenListPosition ; return to the operator position
; only allow incrementing x or y when using inx / iny
.if simpleXYIncrement
nextToken
nextToken
.if !EOT
___error "Unexpected trailing characters!"
.endif
; check for need to override right side to reg A:
.elseif xmatchToken { & } || xmatchToken { | } || xmatchToken { << } || xmatchToken { >> } || xmatchToken { ^ } || xmatchToken { + } || xmatchToken { - }
.if rightReg = REGX
txa
.elseif rightReg = REGY
tya
.endif
rightReg .set REGA
.endif
; no register determined?
.if !rightReg
.if requestReg
rightReg .set requestReg
.else
rightReg .set REGA
.endif
.elseif requestReg
.if rightReg <> requestReg
___error "Could not use requested register for operation."
.endif
.endif
; let ___compare know which reg is active:
___compare::regFound .set rightReg
.if pendingLoadRightSide
printTokenListDebugCA65HL { MB right side to load : .mid( leftSideTokenCount + 1, rightSideToLoadTokenCount, {EXP}) }
.if rightReg = REGA
lda .mid( leftSideTokenCount + 1, rightSideToLoadTokenCount, {EXP})
.elseif rightReg = REGX
ldx .mid( leftSideTokenCount + 1, rightSideToLoadTokenCount, {EXP})
.else
ldy .mid( leftSideTokenCount + 1, rightSideToLoadTokenCount, {EXP})
.endif
.endif
.repeat .tcount({EXP}) - currentTokenNumber
.if !EOT
; --------------------------------------------------------------------------------------------
.if xmatchToken { + }
nextToken
.if !xmatchToken { c } ; c means WITH carry
clc
.else
nextToken ; skip the c
.endif
pos .set currentTokenNumber
___mb_findNextOp
adc {.mid( pos, currentTokenNumber - pos, {EXP} )}
.endif
; --------------------------------------------------------------------------------------------
.if xmatchToken { - }
nextToken
.if !xmatchToken { c } ; c means WITH carry
sec
.else
nextToken ; skip the c
.endif
pos .set currentTokenNumber
___mb_findNextOp
sbc {.mid( pos, currentTokenNumber - pos, {EXP} )}
.endif
; --------------------------------------------------------------------------------------------
.if xmatchToken { & }
nextToken
pos .set currentTokenNumber
___mb_findNextOp
and {.mid( pos, currentTokenNumber - pos, {EXP} )}
.endif
; --------------------------------------------------------------------------------------------
.if xmatchToken { | }
nextToken
pos .set currentTokenNumber
___mb_findNextOp
ora {.mid( pos, currentTokenNumber - pos, {EXP} )}
.endif
; --------------------------------------------------------------------------------------------
.if xmatchToken { << }
nextToken
.if matchToken { 123 }
.repeat currentToken
asl
.endrepeat
nextToken
.if !EOT
___error "No addition operations allowed after shift."
.endif
.else
___error "Shift left requires constant value for number of shift operations."
.endif
;___mb_findNextOp
.endif
; --------------------------------------------------------------------------------------------
.if xmatchToken { >> }
nextToken
.if matchToken { 123 }
.repeat currentToken
lsr
.endrepeat
nextToken
.if !EOT
___error "No addition operations allowed after shift."
.endif
.else
___error "Shift left requires constant value for number of shift operations."
.endif
;___mb_findNextOp
.endif
; --------------------------------------------------------------------------------------------
.if xmatchToken { ^ }
nextToken
pos .set currentTokenNumber
___mb_findNextOp
eor {.mid( pos, currentTokenNumber - pos, {EXP} )}
.endif
.endif
.endrepeat
.if !FLOW_CONTROL_VALUES::MB_RIGHT_SIDE_ONLY
.if !leftReg
.if rightReg = REGA
sta .left(leftSideTokenCount, {EXP})
.elseif rightReg = REGX
stx .left(leftSideTokenCount, {EXP})
.else
sty .left(leftSideTokenCount, {EXP})
.endif
.elseif rightReg = REGA
.if leftReg = REGX
tax
.elseif leftReg = REGY
tay
.endif
.elseif rightReg = REGY
.if leftReg = REGA
tya
.elseif leftReg <> REGY
__error "Not supported."
.endif
.elseif rightReg = REGX
.if leftReg = REGA
txa
.elseif leftReg <> REGX
__error "Not supported."
.endif
.endif
.endif
.undefine EXP
endTokenListEval
.endmacro
; --------------------------------------------------------------------------------------------
; Helper macros for .macro mw (Move Word)
; This will output the appropriate amount of load/store instructions
; Arguments: CPU register to use, source, destination, dest. size, source size, and immediate mode flag
; Source must be the same size or smaller then the dest.
; If the source is smaller, load a zero, skip further loads, and fill the destination with zero
; We could optionally sign extend, not implemented yet.
; Arguments don't need error checking, should be valid from calling macro
.scope ___moveWord
previousImm .set 0
.endscope
; small macro for ___moveMem_ca65hl macro, to skip loading the same immediate value if possible
.macro ___LOAD_imm_ca65HL value
.if .const(value)
.if !( value = ___moveWord::previousImm )
LOAD # ( value )
___moveWord::previousImm .set value
.endif
.else
LOAD # ( value )
.endif
.endmacro
.macro ___moveMem_ca65hl reg, source, dest, destsize, sourcesize, imm
.local sourcecount
sourcecount .set sourcesize
.if .xmatch(reg, a)
.define LOAD lda
.define STORE sta
.elseif .xmatch(reg, x)