-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.inc
2343 lines (2155 loc) · 89.3 KB
/
common.inc
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
;*******************************************************************************
;* COMMON EQUATES -- MCU INDEPENDENT -- FOR ASM11 ASSEMBLER
;*******************************************************************************
;* Language : Motorola/Freescale/NXP 68HC11 Assembly Language (aspisys.com/ASM11)
;* Status : FREEWARE Copyright (c) 2021 by Tony Papadimitriou <tonyp@acm.org>
;* Original : http://www.aspisys.com/code/hc11/common.html
;*******************************************************************************
; Dot-ending names (XXX.) indicates a unique bit mask (possibly multiple bits)
; Underscore surrounded names (_XXX_) indicates special system symbols such as
; CCR offsets, etc.
; Constants use uppercase (XXX)
; Variables use lowercase and underscores as needed (my_var).
; Pointers begin with a point/dot (.xxx).
; Code labels use CamelCase
; Local labels end with @@ (although ASM11 allows @@ to be anywhere inside label)
;*******************************************************************************
#ifmain ;-----------------------------------------------------------------------
#Fatal This file can not be used stand-alone
#endif ;------------------------------------------------------------------------
NOT def -1 ;Used with XOR to get NOT result
;*******************************************************************************
; Symbol to the left of macro call (if present) and :MEXIT internal variable
; are SET to the integer Log2 (log base two) of the given expression.
; Quick-n-dirty calculation of integer part of log2(n) for values upto 2^31-1
; Useful to get power from value (e.g., as when used with various prescalers.)
; With the optional ShiftLeftBits parameter, one can shift the result into the
; expected bit positions (e.g., within a larger bitmap).
#ifnomdef Log2
Log2 macro Expr[,ShiftLeftBits]
mreq 1:Usage: Label @~0~ Expression[,ShiftLeftBits]
mdef 2,0
#temp :loop-2
#ifz ~1~
#temp :temp<{~2~}
#ifparm ~label~
~label~ set :temp
#endif
mexit :temp
#endif
mset 1,{~1~>1}
mtop
endm
#endif
;*******************************************************************************
; Restrict the value of a constant label between a minimum and a maximum
; printing a warning if the value is outside the allowed range
ConstMinMax macro Symbol[,MinValue][,MaxValue]
mreq 1:Symbol[,MinValue][,MaxValue]
mset 0,~2~<=~1,~<=~3~ [not {~1,~}]
#ifb ~2~~3~
merror No MinValue or MaxValue given
#endif
#ifnb ~2~
#if ~1,~ < ~2~
#Warning ~text~
#endif
#endif
#ifnb ~3~
#if ~1,~ > ~3~
#Warning ~text~
#endif
#endif
endm
;*******************************************************************************
; Restrict the value of a constant label to one of the specified values
; printing a warning if the value is not in the given set
ConstValues macro Symbol,Value[,Value]*
mreq 1,2:Symbol,Value[,Value]*
#ifndef ~1,~
mexit ;;mstop ~1,~ not yet defined
#endif
mdo 2
#if ~1,~ = ~{:mloop}.~
mexit
#endif
mloop :n
mset 0,~1,~
mdel 1
mset #
#Warning ~text~ ({~text~}) not in [~1~]
endm
;*******************************************************************************
VDD def 5000 ;MCU voltage in mV
;===============================================================================
#ifndef HZ||KHZ||MHZ
MHZ def 8 ;default for most anything
#ifdef __DK68HC11__
HZ def 9830400
#endif
#endif
;-------------------------------------------------------------------------------
? macro FromHz,ToHz
mreq 1,2:FromHz,ToHz
#ifdef BUS_~1~
mset 1,BUS_~1~
mset 2,BUS_~2~
#endif
#ifdef ~1~
#if ~1~\1000 >= 500
~2~ set ~1~/1000+1
#else
~2~ set ~1~/1000
#endif
#endif
#ifparm ~1.1.4~ = BUS_
~1.5~ set ~1~*4
#endif
endm
;-------------------------------------------------------------------------------
#ifdef BUS_MHZ
BUS_HZ def BUS_MHZ*1000000
BUS_KHZ def BUS_MHZ*1000
#endif
@? HZ,KHZ
@? KHZ,MHZ
MHZ def 8 ;default crystal speed
KHZ def MHZ*1000
HZ def KHZ*1000
BUS_HZ def HZ/4
BUS_KHZ def BUS_HZ/1000
BUS_MHZ def BUS_KHZ/1000
MSEC_CYCLES def BUS_KHZ ;number of cycles per millisecond
;-------------------------------------------------------------------------------
? macro
#temp :index-1
Bit{:temp}. equ 1<{:temp}
mtop 16
endm
@? ;Define Bit0 .. Bit15
;-------------------------------------------------------------------------------
; ISR (zero-based) offsets account for the return address of OS11 dispatcher's JSR
;-------------------------------------------------------------------------------
_DATA_ equ 0 ;start of stacked data, if any
_CCR_ next _DATA_,1
_B_ next _DATA_,1
_A_ next _DATA_,1
_X_ next _DATA_,2
_Y_ next _DATA_,2
_PC_ next _DATA_,2
_D_ equ _B_,2 ;This is for loading D reversed
;-------------------------------------- ;Alternative (old style) names
DATA_ equ _DATA_ ;start of stacked data, if any
PC_ equ _PC_,2
Y_ equ _Y_,2
X_ equ _X_,2
A_ equ _A_,1
B_ equ _B_,1
D_ equ _D_,2 ;This is for loading D reversed
CCR_ equ _CCR_,1
;-------------------------------------------------------------------------------
; CCR bits (masks)
;-------------------------------------------------------------------------------
S. equ %10000000 ;Stop inhibit
X. equ %01000000 ;XIRQ disable
H. equ %00100000 ;Half Carry
I. equ %00010000 ;IRQ disable
N. equ %00001000 ;Negative
Z. equ %00000100 ;Zero
V. equ %00000010 ;Overflow
C. equ %00000001 ;Carry
;-------------------------------------- ;BPROT Bits
PTCON. equ Bit4. ;Protect CONFIG programming
@bits BPRT,0,3
;-------------------------------------- ;CONFIG Bits
NOSEC. equ Bit3. ;NOSEC (no security option)
NOCOP. equ Bit2. ;NOCOP in CONFIG register
ROMON. equ Bit1. ;ROM is ON/enabled
EEON. equ Bit0. ;EEPROM is ON/enabled
;-------------------------------------- ;OPTION Bits
ADPU. equ %10000000 ;A/D Power-Up
CSEL. equ %01000000 ;Charge Pump Clock Select
IRQE. equ %00100000 ;IRQ Select Edge Sensitive Only
DLY. equ %00010000 ;Delay out of STOP mode
CME. equ %00001000 ;Clock Monitor Enable
@bits CR,0,1 ;COP Timer Rate Select
;-------------------------------------- ;SCI Bits
TIE. equ %10000000 ;SCI Transmitter Interrupt Enable
TCIE. equ %01000000 ;SCI Transmit Complete Interrupt Enable
RIE. equ %00100000 ;SCI Receiver Interrupt Enable
ILIE. equ %00010000 ;SCI Idle-Line Interrupt Enable
TE. equ %00001000 ;SCI Transmitter Enable
RE. equ %00000100 ;SCI Receiver Enable
RWU. equ %00000010 ;SCI Receiver Wakeup Control
SBK. equ %00000001 ;SCI Send Break
TDRE. equ %10000000 ;SCI Transmit Register Empty
TC. equ %01000000 ;SCI Transmit Complete Flag
RDRF. equ %00100000 ;SCI Receive Register Full
IDLE. equ %00010000 ;SCI Idle Line Detected
OR. equ %00001000 ;SCI Overrun Error
NF. equ %00000100 ;SCI Noise Error
FE. equ %00000010 ;SCI Framing Error
;-------------------------------------- ;SPI Bits
SPIE. equ %10000000 ;SPI Interrupt Enable
SPE. equ %01000000 ;SPI Enable
DWOM. equ %00100000 ;PortD OR Wired Mode
MSTR. equ %00010000 ;Master Mode Select
CPOL. equ %00001000 ;Clock Polarity
CPHA. equ %00000100 ;Clock Phase
@bits SRP,0,1 ;SPI Clock Rate Select
SPIF. equ %10000000 ;SPI Transfer Complete Flag
WCOL. equ %01000000 ;Write Collision
MODF. equ %00010000 ;Mode Fault
;-------------------------------------- ;SPI Pins
MISO. equ Bit2. ;Port D pins for manual SPI
MOSI. equ Bit3.
SCK. equ Bit4.
SS. equ Bit5.
#ifdef PORTD
SPI_MISO @pin PORTD,2
SPI_MOSI @pin PORTD,3
SPI_CLK @pin PORTD,4
SPI_SS @pin PORTD,5
#endif
;-------------------------------------- ;ADCTL (A/D Control/Status) Bits
CCF. equ %10000000 ;Conversions Complete Flag
SCAN. equ %00100000 ;Continuous Scan Control
MULT. equ %00010000 ;Multiple/Single Channel Control
CD. equ %00001000 ;Channel Select D
CC. equ %00000100 ;Channel Select C
CB. equ %00000010 ;Channel Select B
CA. equ %00000001 ;Channel Select A
;-------------------------------------- ;PACTL (Pulse Accumulator Control) Bits
DDRA7. equ %10000000 ;Data Directive for PortA[7]
PAEN. equ %01000000 ;Pulse Accumulator System Enable
PAMOD. equ %00100000 ;Pulse Accumulator Mode
PEDGE. equ %00010000 ;Pulse Accumulator Edge Control
DDRA3. equ %00001000 ;Data Directive for PortA[3]
I4O5. equ %00000100 ;IC4/OC5 select
@bits RTR,0,1 ;RTI Rate Select
;-------------------------------------- ;Timer Bits
TOF. equ %10000000
RTIF. equ %01000000
PAOVF. equ %00100000
PAIF. equ %00010000
;-------------------------------------- ;PIOC (Parallel IO Control) Bits
STAF. equ %10000000 ;STRA Interrupt Status Flag
STAI. equ %01000000 ;STRA Interrupt Enable Mask
CWOM. equ %00100000 ;Port C Wired-OR Mode
HNDS. equ %00010000 ;Handshake Mode
OIN. equ %00001000 ;Output or Input Handshake Select
PLS. equ %00000100 ;Pulse/Interlocked Handshake Operation
EGA. equ %00000010 ;Active Edge for STRA
INVB. equ %00000001 ;Invert STRB
;-------------------------------------- ;Vectors by name
Vsci def $FFD6
Vspi def $FFD8
Vpai def $FFDA
Vpao def $FFDC
Vrto def $FFDE
Vtic4 def $FFE0
Vtoc5 def Vtic4 ;alias for Vtic4
Vtoc4 def $FFE2
Vtoc3 def $FFE4
Vtoc2 def $FFE6
Vtoc1 def $FFE8
Vtic3 def $FFEA
Vtic2 def $FFEC
Vtic1 def $FFEE
Vrti def $FFF0
Virq def $FFF2
Vxirq def $FFF4
Vswi def $FFF6
Villop def $FFF8
Vcop def $FFFA
Vcmf def $FFFC
Vreset def $FFFE
;-------------------------------------- ;Miscellaneous
BYTE equ 1 ;8-bit quantity
WORD equ 2 ;16-bit quantity
DWORD equ 4 ;32-bit quantity
QWORD equ 8 ;64-bit quantity
ERASED_STATE def $FFFF ;[E]EPROM Erased State
JMP_OPCODE equ $7E ;JMP extended opcode
#ifdef DEBUG
BPS_RATE def 125 ;for SIM11x debugging, use maximum speed
#endif
BPS_RATE def 96 ;9600 bps for the SCI
;*******************************************************************************
; Displays an error message if the assembly-time condition is false
; (The condition should be written as to be compatible with the #IF directive)
assert macro Condition[,message]
mset #','
#if ~1~
mexit
#endif
mdel 1 ;;get rid of condition
mset # ;;unify string message
mdef 1,Assertion failed (~1~) ;;default message
merror ~1~
endm
;*******************************************************************************
; Macro(s)
SetChipSelects macro [[#]STACKTOP] ;to be called first thing after reset
#ifdef XRAM_END
mdef 1,#XRAM_END
#else
mdef 1,#STACKTOP
#endif
#temp {RAM>8&$F0}|{REGS>12}
#ifz :temp
clr XINIT
#else
lda #:temp
sta XINIT ;Set register base
#endif
#ifdef __ASPISYS__
#if MHZ <= 16
clr CSSTRH ;No clock stretch
#else
#Message One clock stretch for external EEPROM
lda #1 ;EEPROM 1 clock stretch
sta CSSTRH ;@>16MHz crystal (>4MHz bus)
#endif
clr CSGADR ;RAM at $0000
lda #%1 ;32K RAM
sta CSGSIZ
lda #%01000101 ;CSIO1 Active High, 32K ROM at $8000
sta CSCTL
#endif
lds ~1~
endm
;*******************************************************************************
; Macro to dynamically execute another macro or instruction as one atomic
; operation (i.e., with interrupts temporarily disabled).
; When done with the operation, it restores interrupts to their original status.
; You could use with any macro. For example, using an ADD.L (add longs) macro
; you could perform the addition with interrupts disabled, even if the original
; macro does not provide for atomicity of operation(s). Example:
; @atomic add.l,Number1,Number2,Answer
Atomic macro [@]CmdToDo[,Parameter]*
mreq 1:[@]CmdToDo[,Parameter]*
#ifparm ~1.1.1~ = @
mset 1,@~1~ ;;make macro a @@ call
#endif
mswap 0,1
mdel 1
mset #
pshcc
sei
~text~ ~1~
pulcc
endm
;*******************************************************************************
; Find a character in the string pointed to by X, and point right past it
; The user's original RegA is not destroyed
SkipChar macro [[#]Character]
mset #
#ifnb ~1~
psha
@@_lda_ ~1~
#endif
Loop$$$
tst ,x ;;is it end of ASCIZ string?
beq Done$$$
cmpa ,x
beq Done$$$ ;;char found, done
inx
bra Loop$$$ ;;repeat until end of ASCIZ string
Done$$$ inx
#ifnb ~1~
pula
#endif
endm
;*******************************************************************************
; Skip over the current and following characters as long as they match the target
; On completion, X -> first non-target character
; The user's original RegA is not destroyed
EatChar macro [[#]Character]
mset #
#ifnb ~1~
psha
@@_lda_ ~1~
#endif
Loop$$$ cmpa ,x ;;compare first char
bne Done$$$ ;;if not same, CBEQ won't work
inx
bra Loop$$$
Done$$$
#ifnb ~1~
pula
#endif
endm
;*******************************************************************************
; Find (in RegA) the minimum between RegA and operand
MinA macro Operand
mset #
mreq 1:Operand
cmpa ~1~
bls Done$$$
lda ~1~
Done$$$ endm
;*******************************************************************************
; Find (in RegA) the maximum between RegA and operand
MaxA macro Operand
mset #
mreq 1:Operand
cmpa ~1~
bhs Done$$$
lda ~1~
Done$$$ endm
;*******************************************************************************
; Point X to a TableEntry given its zero-based index, table address, and
; TableEntry bytesize. Index is optional (in case it is already loaded in RegA)
TblOfs macro [[#]Index],[#]Table,[#]TableEntrySize
mreq 2,3:[[#]Index],[#]Table,[#]TableEntrySize
@@_not_x_ ~2~
pshd
@@_lda_ ~1~ ;A = index
#temp
#ifparm ~3.1.1~ = # ;;special case optimization
#!if ~#3~ = 1 ;;for defined immediate of size 1
#temp 1 ;mark action taken
#endif
#ifnum ~#3~ ;;for numeric immediate
#if ~#3~ = 1 ;;of size 1
#temp 1 ;mark action taken
#endif
#endif
#endif
#ifz :temp
ldb ~3~ ;B = bytesize of TableEntry
mul ;D = offset into table
#else
tab
clra ;D = offset into table
#endif
addd ~2~
xgdx ;X -> TableEntry
puld
endm
;*******************************************************************************
; Macro to use XRAM if XRAM has been defined, RAM otherwise
XRAM macro
#RAM
#ifdef XRAM
#XRAM
#endif
endm
;*******************************************************************************
; EQU but it also copies the size of the original label
; (a single label is expected as parameter, no expressions)
equ macro Label
mset #
mreq 1:Label @~0~ Label
mset 0,Label expected, found
#ifnum ~1~
mstop ~text~ number (~1~)
#endif
#ifstr ~1~
mstop ~text~ string (~1~)
#endif
#ifnb ~'()[]+-*/\,<>^&|'2~
mstop ~text~ expr (~1~)
#endif
~label~ set ~1~,::~1~
endm
;*******************************************************************************
; Macro to allocate a variable to #RAM or #XRAM (if XRAM defined) based on size
; [Smaller variables (upto LONG size) go into #RAM, everything else into #XRAM]
Var macro Size[,MaxSizeForRAM]
#ifnb ~2~
mset 0,~2~ ;;set new MaxSizeForRAM default
#ifb ~1~
mexit ;;special case only to define MaxSizeForRAM
#endif
#endif
mdef 0,4 ;;startup MaxSizeForRAM default
#push
#RAM
#ifdef XRAM ;;when XRAM is defined
#if ~#1~ > ~text~ ;;anything above ~TEXT~ goes to XRAM
#XRAM
#endif
#if :RAM+~#1~ > RAM_END ;;if RAM is full or not enough, use XRAM
#XRAM
#endif
#endif
~label~ set *,~1~
rmb ~1~
#pull
endm
;*******************************************************************************
; Repeat a number of instructions a constant number of times
; Example: @rep 2,lsla,rolx ;shift left XA twice
Rep macro Times,Instruction1[,Instruction2]*
mreq 1,2:Times,Instruction1[,Instruction2]*
#ifz ~1~
mexit ;;nothing to do
#endif
mdo 2
#ifparm ~{:mloop}.1.2~ = @@ ;;convert @@macro to @macro
mset :mloop,~{:mloop}.2~
#endif
#ifparm ~{:mloop}.1.1~ = @ ;;convert @macro to @@macro
mset :mloop,@~{:mloop}.~
#endif
~{:mloop}.~
mloop :n
mtop ~1~
endm
;*******************************************************************************
; Checks the passed parameter and issues an error if it is X-indexed
; (This is meant to be called from other macros -- not to be called directly)
_not_x_ macro
mset #
#ifb ~1~
mexit ;;nothing to do
#endif
#ifparm ~'~,1~'.{:1}~ = x
mstop X-index not allowed (~1~)
#endif
endm
;*******************************************************************************
; Checks the passed parameter and issues an error if it is Y-indexed
; (This is meant to be called from other macros -- not to be called directly)
_not_y_ macro
mset #
#ifb ~1~
mexit ;;nothing to do
#endif
#ifparm ~'~,1~'.{:1}~ = y
mstop Y-index not allowed (~1~)
#endif
endm
;*******************************************************************************
; Checks the passed parameter and issues an error if it is immediate mode
; (This is meant to be called from other macros -- not to be called directly)
_not_#_ macro
#ifb ~1~
mexit ;;nothing to do
#endif
#ifparm ~#~
mstop Immediate mode not allowed (~1~)
#endif
endm
;*******************************************************************************
; Checks the passed parameter and issues an error if it is NOT immediate mode
; (This is meant to be called from other macros -- not to be called directly)
_#_ macro
#ifb ~1~
mexit ;;nothing to do
#endif
#ifb ~#~
mstop Immediate mode expected (~1~)
#endif
endm
;*******************************************************************************
; Check if no size info exists for give symbol (parameter)
_nosize_ macro
mset #
mset 0,mstop [~00~] No size (~1~)
#ifnb ~#~
~text~ [immediate]
#endif
#ifb ~1,~
~text~ [blank]
#endif
#ifnum ~1,~
~text~ [numeric]
#endif
#ifndef ~1,~
~text~ [undefined]
#endif
#ifz ::~1,~
~text~ [constant]
#endif
endm
;*******************************************************************************
; Check if all given non-immediate mode operands have the same size
; (Skip immediate mode ones, and constants -- labels with size zero)
; (This is meant to be called from other macros -- not to be called directly)
_samesize_ macro Operand1,Operand2[,Operand]*
mreq 1,2:Operand1,Operand2[,Operand]*
mset 0
mdo
mswap 1,{:mloop}
#ifb ~#~
#ifnb ~1,~
@@_nosize_ ~1~
#ifb ~text~
mset 0,~1~
#endif
#if ::~text,~ <> ::~1,~
mstop \@~text~\@ ({::~text,~}) size <> \@~1~\@ ({::~1,~})
#endif
#endif
#endif
mloop :n
endm
;*******************************************************************************
; Check if size exists, and optionally if it is one of the expected ones
; (This is meant to be called from other macros -- not to be called directly)
_size_ macro Label[,ExpectedSize]*
@@_nosize_ ~1~
mdo 2
#ifnb ~{:mloop}.~
#if ::~1,~ = ~{:mloop}.~
mexit
#endif
#endif
mloop :n
mswap 0,1
mdel 1
mset #
mstop \@~text,~\@ size {::~text,~} not in (~1~)
endm
;*******************************************************************************
; Optional LDA: Do a LDA instruction but only if the parameter is non-blank
; (This is meant to be called from other macros -- not to be called directly)
_lda_ macro
mset #
#ifb ~1~
mexit
#endif
#ifparm ~#~
#!ifz ~#1~
clra
mexit
#endif
#endif
lda ~1~
endm
;*******************************************************************************
; Optional STA: Do a STA instruction but only if the parameter is non-blank
; (This is meant to be called from other macros -- not to be called directly)
_sta_ macro
#ifnb ~@~
sta ~@~
#endif
endm
;*******************************************************************************
; LDA & CMP combo but it optimizes LDA to CLRA and eliminates CMP if the
; corresponding effective value is #0
; (This is meant to be called from other macros, but it may be called directly)
_ldacmp_ macro [#]Operand1,[#]Operand2
#ifparm ~#~
#ifdef ~#1~
mset 1,~#~{~#1~}
#endif
#endif
#ifparm ~1~ = #0
clra
#else
lda ~1~
#endif
mswap 1,2
#ifparm ~#~
#ifdef ~#1~
mset 1,~#~{~#1~}
#endif
#endif
cmpa ~1~ ;;needed even when #0 to adjust Carry
endm
;*******************************************************************************
; Swap two byte-size variables (does not protect RegA) - Primitive macro
; (This is meant to be called from other macros, but it may be called directly)
_swap_ macro Variable1,Variable2
lda ~1~
psha
lda ~2~
sta ~1~
pula
sta ~2~
endm
;*******************************************************************************
; Swap two same-size variables
swap.s macro Variable1,Variable2
mreq 1,2:Variable1,Variable2
@@_samesize_ ~@~
psha
mdo
@@_swap_, ~1,~+{:mloop-1}~,1~ ~2,~+{:mloop-1}~,2~
mloop ::~1,~
pula
endm
;*******************************************************************************
; Get A from the location pointed to by the parm, while bumping up that pointer
GetNextA macro Pointer
mreq 1:Pointer
#ifparm ~#~
mstop Pointer (~1~) must be a word variable
#endif
@@_not_x_ ~@~
ldx ~@~
lda ,x
inx
stx ~@~
endm
;*******************************************************************************
; Put A into the location pointed to by the parm, while bumping up that pointer
PutNextA macro Pointer
mreq 1:Pointer
#ifparm ~#~
mstop Pointer (~1~) must be a word variable
#endif
@@_not_x_ ~@~
ldx ~@~
sta ,x
inx
stx ~@~
endm
;*******************************************************************************
; Load Effective Address in RegX
lea macro
mset #
#ifb ~1~
mexit ;;no parm, nothing to do
#endif
#ifparm ~#~
ldx ~1~ ;;immediate value load as is
mexit
#endif
#ifparm ~1.1.1~ = . ;;dot-beginning symbols treat as pointers
ldx ~1~
mexit
#endif
#ifparm ~1.1.2~ = ?. ;;dot-beginning file-local symbols treat as pointers
ldx ~1~
mexit
#endif
#ifparm ~,1~ ;;indexed modes
#ifparm ~,1~ = ,x
#ifnb ~1,~
@aix #~1,~ ;;X-indexed add possible offset
#endif
mexit
#endif
ldx ~1~ ;;any other index treat as pointer (regardless of name convention)
mexit
#endif
ldx #~1~ ;;everything else is fixed RAM
endm
;*******************************************************************************
; Find the Greatest Common Divisor (GCD) of two constants.
; Answer is placed in LABEL (if present) and :MEXIT variable
gcd macro a,b
#ifz ~1~
mexit 1
#else ifz ~2~
mexit 1
#endif
#if ~1~ = ~2~
#ifparm ~label~
~label~ set ~1~
#endif
mexit ~1~
#else if ~1~ > ~2~
mset 1,{~1~-{~2~}}
mtop
#endif
mset 2,{~2~-{~1~}}
mtop
endm
;*******************************************************************************
; Find the Least Common Multiple (LCM) of two constants. Prerequisite macro: GCD
; Note: To avoid overflow, first divides, then multiplies. GCD certainly divides
; both numbers, so no problem doing so, and it's more efficient.
; Answer is placed in LABEL (if present) and :MEXIT variable
lcm macro a,b
@@gcd ~@~
#ifparm ~label~
~label~ set {~1~}/:mexit*{~2~}
#endif
mexit {~1~}/:mexit*{~2~}
endm
;*******************************************************************************
; Vector assignment
; (Places vector in #VECTORS segment, then returns to current segment)
Vector macro Vvector,ISR_Address
#ifb ~2~
#ifndef ~1~
mexit ;avoids error on empty but undefined vectors
#endif
#endif
mdef 2,AnRTI
#push
#VECTORS
#ppc
org ~1~
dw ~2~
org :ppc
#pull
endm
;*******************************************************************************
; PUSH any variable onto the stack. If size is missing, size one is assumed.
; (All registers are destroyed)
;*******************************************************************************
; PUSH any variable onto the stack. If 'newname' given the variable will then be
; assigned the name inside the string.
; Constants require a size as it cannot be determined automatically.
; (All registers may be destroyed)
PushV macro variable[ SizeOf(var)][ 'newname']
mset #' '
mset 0 ;;cancel any stack names
mreq 1:variable[ SizeOf(var)][ 'newname']
#ifstr ~{:nn}.~ ;;if last parm is a string
mset 0,~{:nn}.~ ;;it is the stack name
mset 0,~text.2.{:text-2}~ ;;after removing quotes
mdel :nn ;;parm no longer needed
#endif
#ifnb ~#~ ;;immediate mode
mreq 1,2:Constant SizeOf(Constant)
#if ~2~ < 0
merror Negative size (~1~): ~2~
#endif
#ifnb ~text~
mset 0,~text~,~2~
#endif
#temp
mdo
#ifz ~#1~>{:mloop-1*8}&$FF
#ifz :temp
#temp 1
clra
#endif
#else
#temp
lda ~1~>{:mloop-1*8}&$FF
#endif
#if :mloop = ~2~
psha ~text~
#else
psha
#endif
mloop ~2~
#if ~#1~ >= 0
#ifnz ~#1~>{:mloop*8}
merror Constant ~#1~ ({~#1~}) bigger than ~2~ byte(s)
#endif
#else
#ifnz -{~#1~(h)}>{:mloop*8-1}
merror Constant ~#1~ ({~#1~}) bigger than ~2~ byte(s)
#endif
#endif
#Message ~0~ ~1~ ({~2~*8}-bit)
mexit
#endif
#ifnb ~1,~
#ifnz ::~1,~
mdef 2,{::~1,~}
#endif
#endif
mdef 2,1
#if ~2~ < 0
merror Negative size (~1~): ~2~
#endif
#Message ~0~ ~1~ ({~2~*8}-bit)
#ifnb ~text~
mset 0,~text~,~2~
#endif
#if ~2~ = 1 ;;single-byte variable case
#ifb ~1.1.1~ = . ;;but not for pointers
#ifparm ~'~,1~'.1.3~ = ,sp ;;for SP and SPX only
#iftos ~1,~ ;;if at TOS, special case
pula ;;A = TOS value
psha ;;put it back the way it was
psha ~text~
mexit
#endif
#endif
lda ~1~
psha ~text~
mexit
#endif
#endif
#push ;;save current X offset etc.
#ifb ~,1~ = ,x ;;only if not X-indexed
@@lea ~1~ ;;HX -> variable
#x ;;zero X offsets
#else
#x ~1,~ ;;make use of offset
#endif
mdo
lda {~2~-:mloop},x
#if :mloop = ~2~
psha ~text~
#else
psha
#endif
mloop ~2~
#pull ;;restore current X offset etc.
#ifspauto
#spadd ~2~ ;;needed because of #PUSH/#PULL
#endif
endm
;*******************************************************************************
; PULL any variable from the stack. If size is missing, size one is assumed.
; (All registers may be destroyed)
PullV macro Variable[ SizeOf(Variable)]
mset #' '
mreq 1:Variable[ SizeOf(Variable)]
@@_not_#_ ~1~
#ifnz ::~1,~
mdef 2,{::~1,~}
#endif
mdef 2,1
#if ~2~ < 0
merror Negative size (~1~): ~2~
#endif
#Message ~0~ ~1~ ({~2~*8}-bit)
#if ~2~ = 1 ;;single-byte variable case
#ifb ~1.1.1~ = . ;;but not for pointers
#ifparm ~'~,1~'.1.3~ = ,sp ;;for SP and SPX only
#iftos ~1,~ ;;if at TOS, special case
mstop ~0~ to same stack location!
#endif
#endif