Add defines for avr target.
[bertos.git] / bertos / cpu / attr.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos 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 2 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004, 2005, 2006, 2007 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2004 Giovanni Bajo
31  *
32  * -->
33  *
34  * \brief CPU-specific attributes.
35  *
36  * \author Giovanni Bajo <rasky@develer.com>
37  * \author Bernardo Innocenti <bernie@develer.com>
38  * \author Stefano Fedrigo <aleph@develer.com>
39  * \author Francesco Sacchi <batt@develer.com>
40  */
41 #ifndef CPU_ATTR_H
42 #define CPU_ATTR_H
43
44 #include "detect.h"
45 #include <cfg/compiler.h> /* for uintXX_t */
46 #include <cfg/arch_config.h>  /* ARCH_EMUL */
47
48 #include "appconfig.h" // CONFIG_FAST_MEM
49
50 /**
51  * \name Macros for determining CPU endianness.
52  * \{
53  */
54 #define CPU_BIG_ENDIAN    0x1234
55 #define CPU_LITTLE_ENDIAN 0x3412 /* Look twice, pal. This is not a bug. */
56 /*\}*/
57
58 /** Macro to include cpu-specific versions of the headers. */
59 #define CPU_HEADER(module)          PP_STRINGIZE(drv/PP_CAT3(module, _, CPU_ID).h)
60
61 /** Macro to include cpu-specific versions of implementation files. */
62 #define CPU_CSOURCE(module)         PP_STRINGIZE(drv/PP_CAT3(module, _, CPU_ID).c)
63
64
65 #if CPU_I196
66
67         #define NOP                     nop_instruction()
68
69         #define CPU_REG_BITS            16
70         #define CPU_REGS_CNT            16
71         #define CPU_STACK_GROWS_UPWARD  0
72         #define CPU_SP_ON_EMPTY_SLOT    0
73         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
74         #define CPU_HARVARD             0
75
76 #elif CPU_X86
77
78         #define NOP                     asm volatile ("nop")
79
80         #define CPU_REGS_CNT            7
81         #define CPU_SAVED_REGS_CNT      7
82         #define CPU_STACK_GROWS_UPWARD  0
83         #define CPU_SP_ON_EMPTY_SLOT    0
84         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
85         #define CPU_HARVARD             0
86
87         #if CPU_X86_64
88                 #define CPU_REG_BITS    64
89
90                 #ifdef __WIN64__
91                         /* WIN64 is an IL32-P64 weirdo. */
92                         #define SIZEOF_LONG  4
93                 #endif
94         #else
95                 #define CPU_REG_BITS    32
96         #endif
97
98 #elif CPU_ARM
99
100         /* Register counts include SREG too */
101         #define CPU_REG_BITS           32
102         #define CPU_REGS_CNT           16
103         #define CPU_SAVED_REGS_CNT     9
104         #define CPU_STACK_GROWS_UPWARD 0
105         #define CPU_SP_ON_EMPTY_SLOT   0
106         #define CPU_HARVARD            0
107
108         #ifdef __IAR_SYSTEMS_ICC__
109                 #warning Check CPU_BYTE_ORDER
110                 #define CPU_BYTE_ORDER (__BIG_ENDIAN__ ? CPU_BIG_ENDIAN : CPU_LITTLE_ENDIAN)
111
112                 #define NOP            __no_operation()
113
114         #else /* GCC and compatibles */
115
116                 #if defined(__ARMEB__)
117                         #define CPU_BYTE_ORDER CPU_BIG_ENDIAN
118                 #elif defined(__ARMEL__)
119                         #define CPU_BYTE_ORDER CPU_LITTLE_ENDIAN
120                 #else
121                         #error Unable to detect ARM endianness!
122                 #endif
123
124                 #define NOP            asm volatile ("mov r0,r0" ::)
125
126                 /**
127                  * Initialization value for registers in stack frame.
128                  * The register index is not directly corrispondent to CPU
129                  * register numbers, but is related to how are pushed to
130                  * stack (\see asm_switch_context).
131                  * Index (CPU_SAVED_REGS_CNT - 1) is the CPSR register,
132                  * the initial value is set to:
133                  * - All flags (N, Z, C, V) set to 0.
134                  * - IRQ and FIQ enabled.
135                  * - ARM state.
136                  * - CPU in Supervisor Mode (SVC).
137                  */
138                 #define CPU_REG_INIT_VALUE(reg) (reg == (CPU_SAVED_REGS_CNT - 1) ? 0x13 : 0)
139
140                 #if CONFIG_FAST_MEM
141                         /**
142                          * Function attribute for use with performance critical code.
143                          *
144                          * On the AT91 family, code residing in flash has wait states.
145                          * Moving functions to the data section is a quick & dirty way
146                          * to get them transparently copied to SRAM for zero-wait-state
147                          * operation.
148                          */
149                         #define FAST_FUNC __attribute__((section(".data")))
150
151                         /**
152                          * Data attribute to move constant data to fast memory storage.
153                          *
154                          * \see FAST_FUNC
155                          */
156                         #define FAST_RODATA __attribute__((section(".data")))
157
158                 #else // !CONFIG_FAST_MEM
159                         #define FAST_RODATA /**/
160                         #define FAST_FUNC /**/
161                 #endif
162
163                 /**
164                  * Function attribute to declare an interrupt service routine.
165                  */
166                 #define ISR_FUNC __attribute__((interrupt))
167
168         #endif /* !__IAR_SYSTEMS_ICC_ */
169
170 #elif CPU_PPC
171         #define NOP                 asm volatile ("nop" ::)
172
173         /* Register counts include SREG too */
174         #define CPU_REG_BITS           (CPU_PPC32 ? 32 : 64)
175         #define CPU_REGS_CNT           FIXME
176         #define CPU_SAVED_REGS_CNT     FIXME
177         #define CPU_STACK_GROWS_UPWARD 0  //FIXME
178         #define CPU_SP_ON_EMPTY_SLOT   0  //FIXME
179         #define CPU_BYTE_ORDER         (__BIG_ENDIAN__ ? CPU_BIG_ENDIAN : CPU_LITTLE_ENDIAN)
180         #define CPU_HARVARD            0
181
182 #elif CPU_DSP56K
183
184         #define NOP                     asm(nop)
185
186         #define CPU_REG_BITS            16
187         #define CPU_REGS_CNT            FIXME
188         #define CPU_SAVED_REGS_CNT      8
189         #define CPU_STACK_GROWS_UPWARD  1
190         #define CPU_SP_ON_EMPTY_SLOT    0
191         #define CPU_BYTE_ORDER          CPU_BIG_ENDIAN
192         #define CPU_HARVARD             1
193
194         /* Memory is word-addessed in the DSP56K */
195         #define CPU_BITS_PER_CHAR  16
196         #define SIZEOF_SHORT        1
197         #define SIZEOF_INT          1
198         #define SIZEOF_LONG         2
199         #define SIZEOF_PTR          1
200
201 #elif CPU_AVR
202
203         #define NOP           asm volatile ("nop" ::)
204
205         /* Register counts include SREG too */
206         #define CPU_REG_BITS            8
207         #define CPU_REGS_CNT           33
208         #define CPU_SAVED_REGS_CNT     19
209         #define CPU_STACK_GROWS_UPWARD  0
210         #define CPU_SP_ON_EMPTY_SLOT    1
211         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
212         #define CPU_HARVARD             1
213
214         /**
215          * Initialization value for registers in stack frame.
216          * The register index is not directly corrispondent to CPU
217          * register numbers. Index 0 is the SREG register: the initial
218          * value is all 0 but the interrupt bit (bit 7).
219          */
220         #define CPU_REG_INIT_VALUE(reg) (reg == 0 ? 0x80 : 0)
221
222         #warning FIXME:This macro are empty for AVR target, implent it!
223         /**
224          * Function attribute for use with performance critical code.
225          */
226         #define FAST_FUNC /* */
227
228         /**
229          * Data attribute to move constant data to fast memory storage.
230          *
231          * \see FAST_FUNC
232          */
233         #define FAST_RODATA /* */
234
235 #else
236         #error No CPU_... defined.
237 #endif
238
239 /// Default for macro not defined in the right arch section
240 #ifndef CPU_REG_INIT_VALUE
241         #define CPU_REG_INIT_VALUE(reg)     0
242 #endif
243
244
245 #ifndef CPU_STACK_GROWS_UPWARD
246         #error CPU_STACK_GROWS_UPWARD should have been defined to either 0 or 1
247 #endif
248
249 #ifndef CPU_SP_ON_EMPTY_SLOT
250         #error CPU_SP_ON_EMPTY_SLOT should have been defined to either 0 or 1
251 #endif
252
253 /*
254  * Support stack handling peculiarities of a few CPUs.
255  *
256  * Most processors let their stack grow downward and
257  * keep SP pointing at the last pushed value.
258  */
259 #if !CPU_STACK_GROWS_UPWARD
260         #if !CPU_SP_ON_EMPTY_SLOT
261                 /* Most microprocessors (x86, m68k...) */
262                 #define CPU_PUSH_WORD(sp, data) \
263                         do { *--(sp) = (data); } while (0)
264                 #define CPU_POP_WORD(sp) \
265                         (*(sp)++)
266         #else
267                 /* AVR insanity */
268                 #define CPU_PUSH_WORD(sp, data) \
269                         do { *(sp)-- = (data); } while (0)
270                 #define CPU_POP_WORD(sp) \
271                         (*++(sp))
272         #endif
273
274 #else /* CPU_STACK_GROWS_UPWARD */
275
276         #if !CPU_SP_ON_EMPTY_SLOT
277                 /* DSP56K and other weirdos */
278                 #define CPU_PUSH_WORD(sp, data) \
279                         do { *++(sp) = (cpustack_t)(data); } while (0)
280                 #define CPU_POP_WORD(sp) \
281                         (*(sp)--)
282         #else
283                 #error I bet you cannot find a CPU like this
284         #endif
285 #endif
286
287
288 #if CPU_DSP56K
289         /*
290          * DSP56k pushes both PC and SR to the stack in the JSR instruction, but
291          * RTS discards SR while returning (it does not restore it). So we push
292          * 0 to fake the same context.
293          */
294         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
295                 do { \
296                         CPU_PUSH_WORD((sp), (func)); \
297                         CPU_PUSH_WORD((sp), 0x100); \
298                 } while (0);
299
300 #elif CPU_AVR
301         /*
302          * In AVR, the addresses are pushed into the stack as little-endian, while
303          * memory accesses are big-endian (actually, it's a 8-bit CPU, so there is
304          * no natural endianess).
305          */
306         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
307                 do { \
308                         uint16_t funcaddr = (uint16_t)(func); \
309                         CPU_PUSH_WORD((sp), funcaddr); \
310                         CPU_PUSH_WORD((sp), funcaddr>>8); \
311                 } while (0)
312
313         /*
314          * If the kernel is in idle-spinning, the processor executes:
315          *
316          * IRQ_ENABLE;
317          * CPU_IDLE;
318          * IRQ_DISABLE;
319          *
320          * IRQ_ENABLE is translated in asm as "sei" and IRQ_DISABLE as "cli".
321          * We could define CPU_IDLE to expand to none, so the resulting
322          * asm code would be:
323          *
324          * sei;
325          * cli;
326          *
327          * But Atmel datasheet states:
328          * "When using the SEI instruction to enable interrupts,
329          * the instruction following SEI will be executed *before*
330          * any pending interrupts", so "cli" is executed before any
331          * pending interrupt with the result that IRQs will *NOT*
332          * be enabled!
333          * To ensure that IRQ will run a NOP is required.
334          */
335         #define CPU_IDLE NOP
336
337 #else
338         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
339                 CPU_PUSH_WORD((sp), (cpustack_t)(func))
340 #endif
341
342 /**
343  * \def CPU_IDLE
344  *
345  * \brief Invoked by the scheduler to stop the CPU when idle.
346  *
347  * This hook can be redefined to put the CPU in low-power mode, or to
348  * profile system load with an external strobe, or to save CPU cycles
349  * in hosted environments such as emulators.
350  */
351 #ifndef CPU_IDLE
352         #if defined(ARCH_EMUL) && (ARCH & ARCH_EMUL)
353                 /* This emulator hook should yield the CPU to the host.  */
354                 EXTERN_C_BEGIN
355                 void emul_idle(void);
356                 EXTERN_C_END
357                 #define CPU_IDLE emul_idle()
358         #else /* !ARCH_EMUL */
359                 #define CPU_IDLE do { /* nothing */ } while (0)
360         #endif /* !ARCH_EMUL */
361 #endif /* !CPU_IDLE */
362
363 #endif /* CPU_ATTR_H */