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