]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/avr-asm-macros.S
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / test_src / avr-asm-macros.S
1 /* avr-asm-macros.S */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /* 
21  * File:        avr-asm-macros.S
22  * Author:      Daniel Otte
23  * Date:        2008-08-13
24  * License:     GPLv3 or later
25  * Description: some macros which are quite usefull
26  * 
27  */
28  
29 #include <avr/io.h>
30
31 /*******************************************************************************
32 *  MACRO SECTION                                                               *
33 *******************************************************************************/
34
35 .macro push_ p1:req, p2:vararg
36         push \p1
37 .ifnb \p2       
38         push_ \p2
39 .endif
40 .endm
41
42 .macro pop_ p1:req, p2:vararg
43         pop \p1
44 .ifnb \p2       
45         pop_ \p2
46 .endif
47 .endm
48
49 .macro push_range from:req, to:req
50         push \from
51 .if     \to-\from
52         push_range "(\from+1)",\to
53 .endif          
54 .endm
55
56 .macro pop_range from:req, to:req
57         pop \to
58 .if     \to-\from
59         pop_range \from,"(\to-1)"       
60 .endif
61 .endm
62
63 .macro stack_alloc size:req, reg1=r30, reg2=r31
64         in r0, _SFR_IO_ADDR(SREG)
65         in \reg1, _SFR_IO_ADDR(SPL)
66         in \reg2, _SFR_IO_ADDR(SPH)
67         sbiw \reg1, \size 
68         cli
69         out _SFR_IO_ADDR(SPH), \reg2
70         out _SFR_IO_ADDR(SREG), r0
71         out _SFR_IO_ADDR(SPL), \reg1
72 .endm
73
74 .macro stack_free size:req, reg1=r30, reg2=r31
75         in r0, _SFR_IO_ADDR(SREG)
76         in \reg1, _SFR_IO_ADDR(SPL)
77         in \reg2, _SFR_IO_ADDR(SPH)
78         adiw \reg1, \size 
79         cli
80         out _SFR_IO_ADDR(SPH), \reg2
81         out _SFR_IO_ADDR(SREG), r0
82         out _SFR_IO_ADDR(SPL), \reg1
83 .endm
84
85
86 .macro stack_alloc_large size:req, reg1=r30, reg2=r31
87         in r0, _SFR_IO_ADDR(SREG)
88         in \reg1, _SFR_IO_ADDR(SPL)
89         in \reg2, _SFR_IO_ADDR(SPH)
90         subi \reg1, lo8(\size)
91         sbci \reg2, hi8(\size)   
92         cli
93         out _SFR_IO_ADDR(SPH), \reg2
94         out _SFR_IO_ADDR(SREG), r0
95         out _SFR_IO_ADDR(SPL), \reg1
96 .endm
97
98 .macro stack_free_large size:req, reg1=r30, reg2=r31
99         in r0, _SFR_IO_ADDR(SREG)
100         in \reg1, _SFR_IO_ADDR(SPL)
101         in \reg2, _SFR_IO_ADDR(SPH)
102         adiw \reg1, 63
103         adiw \reg1, (\size-63) 
104         cli
105         out _SFR_IO_ADDR(SPH), \reg2
106         out _SFR_IO_ADDR(SREG), r0
107         out _SFR_IO_ADDR(SPL), \reg1
108 .endm
109
110 .macro stack_free_large2 size:req, reg1=r30, reg2=r31
111         in r0, _SFR_IO_ADDR(SREG)
112         in \reg1, _SFR_IO_ADDR(SPL)
113         in \reg2, _SFR_IO_ADDR(SPH)
114         adiw \reg1, 63
115         adiw \reg1, 63
116         adiw \reg1, (\size-63*2) 
117         cli
118         out _SFR_IO_ADDR(SPH), \reg2
119         out _SFR_IO_ADDR(SREG), r0
120         out _SFR_IO_ADDR(SPL), \reg1
121 .endm
122
123 .macro stack_free_large3 size:req, reg1=r30, reg2=r31
124         in r0, _SFR_IO_ADDR(SREG)
125         in \reg1, _SFR_IO_ADDR(SPL)
126         in \reg2, _SFR_IO_ADDR(SPH)
127         push r16
128         push r17
129         ldi r16, lo8(\size)
130         ldi r17, hi8(\size)
131         add \reg1, r16
132         adc \reg2, r17
133         pop r17
134         pop r16 
135         cli
136         out _SFR_IO_ADDR(SPH), \reg2
137         out _SFR_IO_ADDR(SREG), r0
138         out _SFR_IO_ADDR(SPL), \reg1
139 .endm
140
141 .macro LOAD_IO reg:req io:req
142 .if _SFR_IO_REG_P(\io)
143         in \reg, _SFR_IO_ADDR(\io)
144 .else
145         lds \reg, _SFR_MEM_ADDR(\io)
146 .endif
147 .endm
148
149 .macro STORE_IO io:req reg:req
150 .if _SFR_IO_REG_P(\io)
151         out _SFR_IO_ADDR(\io), \reg
152 .else
153         sts _SFR_MEM_ADDR(\io), \reg
154 .endif
155 .endm
156
157
158 .macro CLEAR_BIT_IO io:req bit:req reg:req
159 .if _SFR_IO_REG_P(\io)
160         cbi _SFR_IO_ADDR(\io), \bit
161 .else
162         lds \reg, _SFR_MEM_ADDR(\io)
163         andi \reg, ~_BV(\bit)
164         sts _SFR_MEM_ADDR(\io), \reg
165 .endif
166 .endm
167
168 .macro SET_BIT_IO io:req bit:req reg:req
169 .if _SFR_IO_REG_P(\io)
170         sbi _SFR_IO_ADDR(\io), \bit
171 .else
172         lds \reg, _SFR_MEM_ADDR(\io)
173         ori \reg, _BV(\bit)
174         sts _SFR_MEM_ADDR(\io), \reg
175 .endif
176 .endm
177
178 /*******************************************************************************
179 * END of MACRO SECTION                                                         *
180 *******************************************************************************/
181
182
183