forked from erinjense/LED-slave-MC9S08QG8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.inc
2903 lines (2676 loc) · 103 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 ASM8 ASSEMBLER *
;*******************************************************************************
;* Language : Motorola/Freescale/NXP HC08/9S08 Assembly Language (aspisys.com/ASM8)
;*******************************************************************************
; FREEWARE, Copyright (c) Tony G. Papadimitriou <tonyp@acm.org>
;*******************************************************************************
; General naming conventions (older code may not yet follow precisely):
;-------------------------------------------------------------------------------
; Dot-ending names (XXX.) indicate a unique bit by position number (7 thru 0)
; Underscore-ending names (XXX_) indicate a bit mask (possibly multiple bits)
; Underscore surrounded names (_XXX_) indicate 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 ASM8 allows @@ to be anywhere inside label)
;*******************************************************************************
#if :version < 1200
#Warning Some macros are incompatible with ASM8 v{:version(2)} (please update to ASM8 v12 or later)
#endif
#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][,Message]
mreq 1:Symbol[,MinValue][,MaxValue]
mdef 4,~2~ <= ~1,~ <= ~3~ [not {~1,~}]
mset 0,~4~
#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 3000 ;MCU voltage in mV
;===============================================================================
?max macro CONST,UpperLimit
#if ~1~ > ~2~
merror ~1~ ({~1~}) > ~2~ not allowed
#endif
endm
;-------------------------------------------------------------------------------
BDIV def 2 ;default bus divider (9S08)
BDIV_ @Log2 BDIV,6 ;move BDIV bits to Bit7..Bit6
@?max BDIV,8
;===============================================================================
?hz 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_
#ifhcs
#ifdef _PA_||_PT_||_PL_
~1.5~ set ~1~*BDIV
#else
~1.5~ set ~1~*2*BDIV
#endif
#else
~1.5~ set ~1~*4
#endif
#endif
endm
;-------------------------------------------------------------------------------
#ifdef BUS_MHZ
BUS_HZ def BUS_MHZ*1000000
BUS_KHZ def BUS_MHZ*1000
#endif
@?hz HZ,KHZ
@?hz KHZ,MHZ
MHZ def 16 ;default crystal speed
KHZ def MHZ*1000
HZ def KHZ*1000
#ifhcs
#ifdef _PA_||_PT_||_PL_
BUS_HZ def HZ/BDIV
#else
BUS_HZ def HZ/2/BDIV
#endif
#else
BUS_HZ def HZ/4
#endif
BUS_KHZ def BUS_HZ/1000
BUS_MHZ def BUS_KHZ/1000
;===============================================================================
#ifhcs
?calc_fll_factor macro
#ifdef _QD_
#temp1 1
mset 0,512
#else ifdef _FL_
#temp1 2
mset 0,512,608
#else ifdef _PA_||_PT_||_PL_
#temp1 1
mset 0,1024
#else ifdef _AC_
#temp1 2
mset 0,1,64
#else
#temp1 6
mset 0,512,1024,1536,608,1216,1824
#endif
mdo
#temp ~text[,]{:mloop}~
;;------------------------------------- ;MCU variants with ICG
#ifdef ICGC1
#temp2 HZ*7/243000/:temp
#if :temp2 >= 4
#if :temp2 <= 18
#ifz :temp2\2
FLL_FACTOR def :temp
#endif
#endif
#endif
;;-------------------------------------
#else
;;------------------------------------- ;MCU variants without ICG
#ifz HZ*10\:temp
#if HZ*10/:temp >= 312500
#if HZ*10/:temp <= 390625
FLL_FACTOR def :temp
#endif
#endif
#endif
#endif
;;-------------------------------------
mloop :temp1
endm
@?calc_fll_factor
FLL_FACTOR def 512
@?max FLL_FACTOR,1824
#temp ;assume low RANGE
#ifdef TCXO
#if TCXO >= 1000000
#temp 1 ;high RANGE
#ifdef RDIV
@ConstMinMax TCXO/RDIV,31250,39063
#endif
#endif
#else !if XTAL >= 1000000
#temp 1 ;high RANGE
#ifdef RDIV
@ConstMinMax XTAL/RDIV,31250,39063
#endif
#endif
#!if RDIV > 128
#temp 1 ;high RANGE
#endif
RANGE def :temp ;default divider range (0,1)
#ifdef RDIV
#ifz RANGE
@ConstValues RDIV,1,2,4,8,16,32,64,128
RDIV_ @Log2 RDIV,3 ;move RDIV bits to Bit5..Bit3
#else
@ConstValues RDIV,32,64,128,256,512,1024
RDIV_ @Log2 RDIV/32,3 ;move RDIV bits to Bit5..Bit3
#endif
#else
RDIV_ equ 0 ;a dummy no-op value
#endif
RANGE_ equ RANGE<RANGE_SEL. ;move RANGE bit to correct bit
@?max RANGE,1
#endif
;===============================================================================
#ifhcs
#ifdef ICGC1
#ifnz FLL_FACTOR
#temp HZ*7/243000/FLL_FACTOR
#if :temp >= 4
#if :temp <= 18
#ifz :temp\2
MFD equ :temp
#else
#Warning Out-of-range MFD {:temp}
#endif
#endif
#endif
#endif
#else
#ifnz FLL_FACTOR
#temp HZ*10/FLL_FACTOR
#ifz HZ*10\FLL_FACTOR
#Message Trim to IRCLK {:temp(1)} Hz (FLL_FACTOR={FLL_FACTOR})
#else
#Warning Unsupported CPU clock ({HZ(6)} MHz)
#endif
#if :temp < 312500
#Warning Out-of-range trim value {:temp(1)} Hz
#else if :temp > 390625
#Warning Out-of-range trim value {:temp(1)} Hz
#endif
#endif
#endif
#endif
MSEC_CYCLES def BUS_KHZ ;rough number of cycles per millisecond
;-------------------------------------------------------------------------------
? macro
#temp :index-1
Bit{:temp}. equ {:temp}
Bit{:temp}_ equ 1<{:temp}
mtop 16
endm
@? ;Define Bit0 .. Bit15
;-------------------------------------------------------------------------------
; CCR bits (by position)
;-------------------------------------------------------------------------------
@bitnum CCR_V,7 ;Overflow flag in CCR
@bitnum CCR_U1,6 ;--Undefined
@bitnum CCR_U0,5 ;--Undefined
@bitnum CCR_H,4 ;Half-Carry flag in CCR
@bitnum CCR_I,3 ;Interrupts flag in CCR
@bitnum CCR_N,2 ;Negative flag in CCR
@bitnum CCR_Z,1 ;Zero flag in CCR
@bitnum CCR_C,0 ;Carry flag in CCR
CMP_AFFECTED_ equ CCR_V_|CCR_N_|CCR_Z_|CCR_C_ ;CMP affected bits
CMP_NOT_AFFECTED_ equ CMP_AFFECTED_^NOT ;CMP not affected bits
;-------------------------------------------------------------------------------
; SP (one-based) offsets account for the return address of OS8 dispatcher's CALL
;-------------------------------------------------------------------------------
#temp 1+:ab ;start of stacked data, if any
_H_ setn :temp ;stacked by OS8 SWI ISR
_CCR_ setn :temp ;stacked by HC[S]08 interrupt
_A_ setn :temp ; -//-
_X_ setn :temp ; -//-
_PCH_ setn :temp ; -//-
_PCL_ setn :temp ; -//-
_DATA_ setn :temp ;offset to caller's TOS data
_PC_ set _PCH_,2 ;alias for full PC word
_INT_FRAME_SIZE_ set _DATA_-_H_
;*******************************************************************************
; 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 to test if a symbol is at TOS, and issue an error if not
tos macro Symbol
mset #
#iftos ~1,~
mexit
#endif
merror ~1,~ is expected at TOS
endm
;*******************************************************************************
; For each defined port letter, repeat the given instruction.
; Use * as the wildcard that will be replaced with the corresponding letter.
; * may appear once both in the CmdToDo and the Mask to match.
; If LetterList exists, it will be used instead of the full letter list.
ForEachPort macro [@]CmdToDo,Mask*[,LetterList]
mset #','
mreq 1,2:[@]CmdToDo,Mask* (where * will be replaced by the letter)
mset 0,~3~
mdef 0,ABCDEFGHIJKLMNOPQRSTUVWXYZ
#ifparm ~1.1.1~ = @
mset 1,@~1~ ;;make macro a @@ call
#endif
mdo
#ifdef ~2'*'~~text.{:mloop}.1~~2'*'2~
~1'*'~~text.{:mloop}.1~~1'*'2~
#endif
mloop :text
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 #
#push
#spauto :sp
pshcc
sei
~text~ ~1~
pulcc
#if :sp <> :spcheck
#Warning Macro \@~text~\@ resizes stack, ~0~ may fail
#endif
#pull
endm
;*******************************************************************************
; Find a character in the string pointed to by HX, and point right past it.
; The user's original RegA is not destroyed.
; CCR[Z] = 1 if end of string reached while searching
SkipChar macro [[#]Character]
mset #
#ifnb ~1~
#push
#spauto :sp
psha
@@_lda_ ~1~
#endif
Loop$$$
tst ,x ;;is it end of ASCIZ string?
cbeq x+,Done$$$ ;;char found, done
bne Loop$$$ ;;repeat until end of ASCIZ string
Done$$$
#ifnb ~1~
pula
#pull
#endif
endm
;*******************************************************************************
; Skip over the current and following characters as long as they match the target
; On completion, HX -> first non-target character
; The user's original RegA is not destroyed
EatChar macro [[#]Character]
mset #
#ifnb ~1~
#push
#spauto :sp
psha
@@_lda_ ~1~
#endif
cbeq x+,* ;;eat up matching chars
aix #-1 ;;went too far, back up one
#ifnb ~1~
pula
#pull
#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
;*******************************************************************************
; Long version of CBEQA
cjeqa macro #operand,target
#ifparm ~2~ = *
mset 2,{*}
#endif
cbeqa ~1~,Go$$$
bra Done$$$
Go$$$
jmp ~2~
Done$$$
endm
;*******************************************************************************
; Long version of CBEQX
cjeqx macro #operand,target
#ifparm ~2~ = *
mset 2,{*}
#endif
cbeqx ~1~,Go$$$
bra Done$$$
Go$$$
jmp ~2~
Done$$$
endm
;*******************************************************************************
; Point HX 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
#ifnb ~1~
@@_size_, ~1~ 1
#endif
@@_size_, ~3~ 1
@@_not_x_ ~2~
#push
#spauto :sp
psha
@@_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
ldx ~3~ ;X = bytesize of TableEntry
mul ;XA = offset into table
add ~[2.-2]~
xgax
#else
add ~[2.-2]~ ;XA = offset into table
tax
clra
#endif
adc ~[2.-1]~
tah ;HX -> TableEntry
pula
#pull
endm
;*******************************************************************************
; Macro to use XRAM if XRAM has been defined, RAM otherwise
XRAM macro
#RAM
#ifdef XRAM
#XRAM
#endif
endm
;*******************************************************************************
; Macro to define TASK_STACK_SIZE based on which RAM is used for stack
FixTaskStackSize macro MinimumTaskStackSizeConstant
#ifndef _MTOS_
#Hint ~0~ only useful under MTOS, ignored
mexit
#endif
mdef 1,REQUIRED_STACK
#temp MAXTASKS
#ifdef MY_MAXTASKS
#temp MY_MAXTASKS
#endif
#!if STACKTOP > XRAM
TASK_STACK_SIZE def XRAM_END-:XRAM/:temp
#endif
TASK_STACK_SIZE def RAM_END-:RAM/:temp
#Message TASK_STACK_SIZE = {TASK_STACK_SIZE} byte(s) per task
#!if TASK_STACK_SIZE < ~1~
#Warning TASK_STACK_SIZE ({TASK_STACK_SIZE}) too low (<{~1~})
#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 vars (upto LONG or MaxSizeForRAM) go to #RAM, everything else to #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
#else if :RAM+~#1~ > RAM_END+1 ;;if RAM is full or not enough, use XRAM
#XRAM
#endif
#endif
~label~ set *,~1~
rmb ~1~ ;~label~
#pull
endm
;*******************************************************************************
asciz macro [Message,]MaxSizeWithoutTrailingZero
mset #','
#ifstr ~1~
#if :1-2 > ~2~
merror Message is longer ({:1-2}) than allowed ({~2~})
#endif
#if * < EEPROM
merror Can't place constant in RAM
#endif
fcc ~1~
fcb:{~2~-:1+3} 0
mexit
#endif
#if * < EEPROM
rmb ~1~+1
#else
fcb:{~1~+1} 0
#endif
#ifnb ~label~
#size ~label~
#endif
endm
;*******************************************************************************
; Repeat one or more user instructions a given number of times
; Example1: @rep #2,lsla,rolx ;shift left XA twice
; Example2: @rep count@@,sp ;repeat NOP count@@,sp times
; nop
; mresume
Rep macro [#]Times[,Instruction]*
#if :n = 1 ;;block follows macro
mset #
mreq 1:Times
#push
#spauto :sp
#if ::~1,~ <= 1
psha ;;protect user's A
lda ~1~ ;;A = counter value
Loop$$$
psha ;;save current counter
lda 2,asp ;;A = user's A
msuspend ;;run user code
sta 2,asp ;;save user's A for later
pula ;;restore current counter
dbnza Loop$$$ ;;one less iteration to go
pula ;;restore user's A
#else if ::~1,~ = 2
pshhx ;;protect user's HX
ldhx ~1~ ;;HX = counter value
Loop$$$
pshhx ;;save current counter
ldhx 3,asp ;;HX = user's HX
msuspend ;;run user code
sthx 3,asp ;;save user's HX for later
pulhx ;;restore current counter
aix #-1 ;;one less iteration to go
cphx #0
bne Loop$$$
pulhx ;;restore user's HX
#else
merror Unsupported control variable size (~1~: {::~1,~})
#endif
#pull
mexit
#endif
mreq 1,2:[#]Times,Instruction[,Instruction]*
#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
;*******************************************************************************
; Macro to define local variables (var@@) for an SPAUTO based subroutine
; Format is name[space]bytesize
; (If bytesize not present, size ONE is assumed for all except for pointers.
; By my convention pointer names begin with a point/dot [e.g., .buffer is a
; pointer to 'buffer']. In this case, the default bytesize is TWO)
Local macro
mreq 1:Name n[,Name n]*
@@_needs_spauto_
;;--------------------------------------------------------------------
;;count total parameters size while formatting as Label<space>Size
;;--------------------------------------------------------------------
#ifnum ~1~
#temp ~1~
#else
#temp
mdo
#ifb ~{:mloop}' '2~
#ifparm ~{:mloop}.1.1~ = .
mset :mloop,~{:mloop}.~ 2 ;;append default pointer size of two
#else
mset :mloop,~{:mloop}.~ 1 ;;append default data size of one
#endif
#endif
#ifnonum ~{:mloop}' '2~
#ifndef ~{:mloop}' '2~
merror Parm {:mloop} \@~{:mloop}' '2~\@ not label[ size]
#endif
#endif
#temp :temp+~{:mloop}' '2~
#if :temp > 127
merror Too much RAM ({:temp}) for locals @{:mloop}
#endif
mloop :n
#endif
;;--------------------------------------------------------------------
;;create local structure
;;--------------------------------------------------------------------
#push
ais #-{:temp}
#pull
#spadd :temp
#ifnonum ~1~
#temp ::
mdo
#ifparm ~{:mloop}.1.1~ = ?
~{:mloop}' '~ next :temp,~{:mloop}' '2~ ;;define file local variable
#else
~{:mloop}' '~@@ next :temp,~{:mloop}' '2~ ;;define proc local variable
#endif
mloop :n
#endif
mexit :ais ;;return size in :MEXIT
endm
;*******************************************************************************
; Macro to define parameters (var@@) passed into an SPAUTO based subroutine
; Format is name[space]bytesize
; (If bytesize not present, size ONE is assumed for all except for pointers.
; By my convention pointer names begin with a point/dot [e.g., .buffer is a
; pointer to 'buffer']. In this case, the default bytesize is TWO)
Parms macro
mreq 1:Name n[,Name n]*
@@_needs_spauto_
;;--------------------------------------------------------------------
;;Format parameter(s) as Label<space>Size
;;--------------------------------------------------------------------
mdo
#ifb ~{:mloop}' '2~
#ifparm ~{:mloop}.1.1~ = .
mset :mloop,~{:mloop}.~ 2 ;;append default pointer size of two
#else
mset :mloop,~{:mloop}.~ 1 ;;append default data size of one
#endif
#endif
#ifnonum ~{:mloop}' '2~
#ifndef ~{:mloop}' '2~
merror Parm {:mloop} \@~{:mloop}' '2~\@ not label[ size]
#endif
#endif
mloop :n
;;--------------------------------------------------------------------
;;define structure
;;--------------------------------------------------------------------
#temp 1
mdo
#ifparm ~{:mloop}.1.1~ = ?
~{:mloop}' '~ next :temp,~{:mloop}' '2~ ;;define user parameter (file-local)
#else
~{:mloop}' '~@@ next :temp,~{:mloop}' '2~ ;;define user parameter (proc-local)
#endif
mloop :n
mexit :temp-1 ;;return size in :MEXIT
endm
;*******************************************************************************
; Check if we're in SPAUTO mode, and issue a (user defined) warning if not
_needs_spauto_ macro [optional warning]
#ifspauto
mexit
#endif
mset #
#ifb ~1~
mstop #SPAUTO mode required
#endif
#Warning ~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
#Error X-index not allowed (~1~)
#endif
mdel 1
mtop
endm
;*******************************************************************************
; Checks the passed parameter and issues an error if it is SP-indexed
; (This is meant to be called from other macros -- not to be called directly)
_not_sp_ macro
mset #' '
#ifb ~1~
mexit ;;nothing to do
#endif
#ifparm ~'~,1~'.{:1-1}~ = sp
#Error SP-index not allowed (~1~)
#endif
mdel 1
mtop
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
mset #' '
#ifb ~1~
mexit ;;nothing to do
#endif
#ifparm ~#~
#Error Immediate mode not allowed (~1~)
#endif
mdel 1
mtop
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
mset #' '
#ifb ~1~
mexit ;;nothing to do
#endif
#ifb ~#~
#Error Immediate mode expected (~1~)
#endif
mdel 1
mtop
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]*
#ifparm ~#~
mexit ;;ignore for immediate mode
#endif
@@_nosize_ ~1~
mdo 2
#ifnb ~{:mloop}.~
#if ::~1,~ = ~{:mloop}.~
mexit
#endif
#endif
mloop :n
mswap 0,1
mdel 1
mset #
#ifnb ~1~
mstop \@~text,~\@ size is {::~text,~}, expected ~1~
#endif
endm
;*******************************************************************************
; Reload HX from RegHx location if the parm specified is X-indexed
; If RegHX location is not specified, the previous one will be used (or error).
; Finally, load HX from the referenced location, regardless of addressing mode.
; (This is meant to be called from other macros -- not to be called directly)
_ldhx_ macro Parm[ RegHX]
mset #' '
mreq 1:Parm[,RegHX]
mdef 2,~text~
mset 0,~2~ ;;use this as new default
#ifb ~2~
merror RegHX location is needed 1st time
#endif
#ifparm ~'~,1~'.{:1}~ = x ;for any X-indexed mode
#ifparm ~,1~ = ,spx ;for SPX mode ...
tsx ;reload stack frame pointer
#else ;for all others
#ifhcs
ldhx ~2~ ;reload original HX
#else
psha
lda ~2~ ;reload original HX
ldx ~2,~+1~,2~
tah
pula
#endif
#endif
#endif
@lea ~1~ ;load actual parameter
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~