]> git.cryptolib.org Git - avr-crypto-lib.git/blob - avr-asm-macros.S
bug fix + docu
[avr-crypto-lib.git] / avr-asm-macros.S
1 /* avr-asm-macros.S */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
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 //#ifndef AVR_ASM_MACROS__S__
30 //#define AVR_ASM_MACROS__S__
31 #include <avr/io.h>
32
33 /*******************************************************************************
34 *  MACRO SECTION                                                               *
35 *******************************************************************************/
36
37 .macro push_ p1:req, p2:vararg
38         push \p1
39 .ifnb \p2
40         push_ \p2
41 .endif
42 .endm
43
44 .macro pop_ p1:req, p2:vararg
45         pop \p1
46 .ifnb \p2
47         pop_ \p2
48 .endif
49 .endm
50
51 .macro push_range from:req, to:req
52         push \from
53 .if     \to-\from
54         push_range "(\from+1)",\to
55 .endif
56 .endm
57
58 .macro pop_range from:req, to:req
59         pop \to
60 .if     \to-\from
61         pop_range \from,"(\to-1)"
62 .endif
63 .endm
64
65 .macro stack_alloc size:req, reg1=r30, reg2=r31
66         in r0, _SFR_IO_ADDR(SREG)
67         in \reg1, _SFR_IO_ADDR(SPL)
68         in \reg2, _SFR_IO_ADDR(SPH)
69         sbiw \reg1, \size
70         cli
71         out _SFR_IO_ADDR(SPH), \reg2
72         out _SFR_IO_ADDR(SREG), r0
73         out _SFR_IO_ADDR(SPL), \reg1
74 .endm
75
76 .macro stack_free size:req, reg1=r30, reg2=r31
77         in r0, _SFR_IO_ADDR(SREG)
78         in \reg1, _SFR_IO_ADDR(SPL)
79         in \reg2, _SFR_IO_ADDR(SPH)
80         adiw \reg1, \size
81         cli
82         out _SFR_IO_ADDR(SPH), \reg2
83         out _SFR_IO_ADDR(SREG), r0
84         out _SFR_IO_ADDR(SPL), \reg1
85 .endm
86
87
88 .macro stack_alloc_large size:req, reg1=r30, reg2=r31
89         in r0, _SFR_IO_ADDR(SREG)
90         in \reg1, _SFR_IO_ADDR(SPL)
91         in \reg2, _SFR_IO_ADDR(SPH)
92         subi \reg1, lo8(\size)
93         sbci \reg2, hi8(\size)
94         cli
95         out _SFR_IO_ADDR(SPH), \reg2
96         out _SFR_IO_ADDR(SREG), r0
97         out _SFR_IO_ADDR(SPL), \reg1
98 .endm
99
100 .macro stack_free_large size:req, reg1=r30, reg2=r31
101         in r0, _SFR_IO_ADDR(SREG)
102         in \reg1, _SFR_IO_ADDR(SPL)
103         in \reg2, _SFR_IO_ADDR(SPH)
104         adiw \reg1, 63
105         adiw \reg1, (\size-63)
106         cli
107         out _SFR_IO_ADDR(SPH), \reg2
108         out _SFR_IO_ADDR(SREG), r0
109         out _SFR_IO_ADDR(SPL), \reg1
110 .endm
111
112 .macro stack_free_large2 size:req, reg1=r30, reg2=r31
113         in r0, _SFR_IO_ADDR(SREG)
114         in \reg1, _SFR_IO_ADDR(SPL)
115         in \reg2, _SFR_IO_ADDR(SPH)
116         adiw \reg1, 63
117         adiw \reg1, 63
118         adiw \reg1, (\size-63*2)
119         cli
120         out _SFR_IO_ADDR(SPH), \reg2
121         out _SFR_IO_ADDR(SREG), r0
122         out _SFR_IO_ADDR(SPL), \reg1
123 .endm
124
125 .macro stack_free_large3 size:req, reg1=r30, reg2=r31
126         in r0, _SFR_IO_ADDR(SREG)
127         in \reg1, _SFR_IO_ADDR(SPL)
128         in \reg2, _SFR_IO_ADDR(SPH)
129         push r16
130         push r17
131         ldi r16, lo8(\size)
132         ldi r17, hi8(\size)
133         add \reg1, r16
134         adc \reg2, r17
135         pop r17
136         pop r16
137         cli
138         out _SFR_IO_ADDR(SPH), \reg2
139         out _SFR_IO_ADDR(SREG), r0
140         out _SFR_IO_ADDR(SPL), \reg1
141 .endm
142
143
144 /*******************************************************************************
145 * END of MACRO SECTION                                                         *
146 *******************************************************************************/
147
148
149 //#endif /* AVR_ASM_MACROS__S__ */
150