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