aboutsummaryrefslogtreecommitdiff
path: root/amforth-6.5/avr8/macros.asm
blob: 6ee20a5660ba4f67415879b503dccd6044333f00 (plain)
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
; conditional assembly needs preparation
.set DICT_COMPILER2 = 0 ;
.set cpu_msp430 = 0
.set cpu_avr8   = 1

.include "user.inc"

  .def zerol = r2
  .def zeroh = r3
  .def upl = r4
  .def uph = r5

  .def al  = r6
  .def ah  = r7
  .def bl  = r8
  .def bh  = r9

; internal
  .def mcu_boot = r10
  .def isrflag  = r11

  .def temp4 = r14
  .def temp5 = r15

  .def temp0 = r16
  .def temp1 = r17
  .def temp2 = r18
  .def temp3 = r19

  .def temp6 = r20
  .def temp7 = r21

  .def tosl = r24
  .def tosh = r25

  .def wl = r22
  .def wh = r23

.macro loadtos
    ld tosl, Y+
    ld tosh, Y+
.endmacro

.macro savetos
    st -Y, tosh
    st -Y, tosl
.endmacro

.macro in_
.if (@1 < $40)
  in @0,@1
.else
  lds @0,@1
.endif
.endmacro

.macro out_
.if (@0 < $40)
  out @0,@1
.else
  sts @0,@1
.endif
.endmacro

.macro sbi_
.if (@0 < $40)
  sbi @0,@1
.else
  in_ @2,@0
  ori @2,exp2(@1)
  out_ @0,@2
.endif
.endmacro

.macro cbi_
.if (@0 < $40)
  cbi @0,@1
.else
  in_ @2,@0
  andi @2,~(exp2(@1))
  out_ @0,@2
.endif
.endmacro

.macro jmp_
	; a more flexible macro
    .ifdef @0
    .if (@0-pc > 2040) || (pc-@0>2040)
	jmp @0
	.else
	rjmp @0
	.endif
	.else
	jmp @0
	.endif
.endmacro
.macro call_
	; a more flexible macro
    .ifdef @0
    .if (@0-pc > 2040) || (pc-@0>2040)
	call @0
	.else
	rcall @0
	.endif
	.else
	call @0
	.endif
.endmacro

;               F_CPU
;    µsec   16000000   14745600    8000000  1000000
;    1            16      14,74          8        1
;    10          160     147,45         80       10
;    100        1600    1474,56        800      100
;    1000      16000   14745,6        8000     1000
;
; cycles = µsec * f_cpu / 1e6
; n_loops=cycles/5
;
;     cycles already used will be subtracted from the delay
;     the waittime resolution is 1 cycle (delay from exact to +1 cycle)
;     the maximum delay at 20MHz (50ns/clock) is 38350ns
;     waitcount register must specify an immediate register
;
; busy waits a specfied amount of microseconds
.macro   delay
      .set cycles = ( ( @0 * F_CPU ) / 1000000 )
      .if (cycles > ( 256 * 255 * 4 + 2))
        .error "MACRO delay - too many cycles to burn"
      .else
        .if (cycles > 6)
          .set  loop_cycles = (cycles / 4)      
          ldi   zl,low(loop_cycles)
          ldi   zh,high(loop_cycles)
          sbiw  Z, 1
          brne  pc-1
          .set  cycles = (cycles - (loop_cycles * 4))
        .endif
        .if (cycles > 0)
          .if   (cycles & 4)
            rjmp  pc+1
            rjmp  pc+1
          .endif
          .if   (cycles & 2)
            rjmp  pc+1
          .endif
          .if   (cycles & 1)
            nop
          .endif
        .endif
      .endif
.endmacro

; portability macros, they come from the msp430 branches

.macro DEST
    .dw @0
.endm