Add missing #endif.
[bertos.git] / cfg / cpu.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 2004 Giovanni Bajo
6  * This file is part of DevLib - See README.devlib for information.
7  * -->
8  *
9  * \brief CPU-specific definitions
10  *
11  * \version $Id$
12  *
13  * \author Giovanni Bajo <rasky@develer.com>
14  * \author Bernardo Innocenti <bernie@develer.com>
15  * \author Stefano Fedrigo <aleph@develer.com>
16  */
17
18 /*#*
19  *#* $Log$
20  *#* Revision 1.6  2005/07/19 07:26:49  bernie
21  *#* Add missing #endif.
22  *#*
23  *#* Revision 1.5  2005/06/27 21:24:17  bernie
24  *#* CPU_CSOURCE(): New macro.
25  *#*
26  *#* Revision 1.4  2005/06/14 06:15:10  bernie
27  *#* Add X86_64 support.
28  *#*
29  *#* Revision 1.3  2005/04/12 04:06:17  bernie
30  *#* Catch missing CPU earlier.
31  *#*
32  *#* Revision 1.2  2005/04/11 19:10:27  bernie
33  *#* Include top-level headers from cfg/ subdir.
34  *#*
35  *#* Revision 1.1  2005/04/11 19:04:13  bernie
36  *#* Move top-level headers to cfg/ subdir.
37  *#*
38  *#* Revision 1.30  2005/03/15 00:20:09  bernie
39  *#* BREAKPOINT, IRQ_RUNNING(), IRQ_GETSTATE(): New DSP56K macros.
40  *#*
41  *#* Revision 1.29  2005/02/16 20:33:24  bernie
42  *#* Preliminary PPC support.
43  *#*
44  *#* Revision 1.28  2004/12/31 17:39:41  bernie
45  *#* Fix documentation.
46  *#*
47  *#* Revision 1.27  2004/12/31 17:02:47  bernie
48  *#* IRQ_SAVE_DISABLE(), IRQ_RESTORE(): Add null stubs for x86.
49  *#*
50  *#* Revision 1.26  2004/12/13 12:08:12  bernie
51  *#* DISABLE_IRQSAVE, ENABLE_IRQRESTORE, DISABLE_INTS, ENABLE_INTS: Remove obsolete macros.
52  *#*
53  *#* Revision 1.25  2004/12/08 08:31:02  bernie
54  *#* CPU_HARVARD: Define to 1 for AVR and DSP56K.
55  *#*/
56 #ifndef DEVLIB_CPU_H
57 #define DEVLIB_CPU_H
58
59 #include <cfg/compiler.h> /* for uintXX_t */
60
61
62 /*!
63  * \name Macros for determining CPU endianness.
64  * \{
65  */
66 #define CPU_BIG_ENDIAN    0x1234
67 #define CPU_LITTLE_ENDIAN 0x3412
68 /*\}*/
69
70 /*! Macro to include cpu-specific versions of the headers. */
71 #define CPU_HEADER(module)          PP_STRINGIZE(PP_CAT3(module, _, CPU_ID).h)
72
73 /*! Macro to include cpu-specific versions of implementation files. */
74 #define CPU_CSOURCE(module)         PP_STRINGIZE(PP_CAT3(module, _, CPU_ID).c)
75
76
77 #if CPU_I196
78
79         #define NOP                     nop_instruction()
80         #define IRQ_DISABLE             disable_interrupt()
81         #define IRQ_ENABLE              enable_interrupt()
82
83         typedef uint16_t cpuflags_t; // FIXME
84         typedef unsigned int cpustack_t;
85
86         #define CPU_REG_BITS            16
87         #define CPU_REGS_CNT            16
88         #define CPU_STACK_GROWS_UPWARD  0
89         #define CPU_SP_ON_EMPTY_SLOT    0
90         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
91         #define CPU_HARVARD             0
92
93 #elif CPU_X86
94
95         #define NOP                     asm volatile ("nop")
96         #define IRQ_DISABLE             FIXME
97         #define IRQ_ENABLE              FIXME
98         #define IRQ_SAVE_DISABLE(x)     FIXME
99         #define IRQ_RESTORE(x)          FIXME
100
101         typedef uint32_t cpuflags_t; // FIXME
102
103         #define CPU_REGS_CNT            7
104         #define CPU_STACK_GROWS_UPWARD  0
105         #define CPU_SP_ON_EMPTY_SLOT    0
106         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
107         #define CPU_HARVARD             0
108
109         #if CPU_X86_64
110                 typedef uint64_t cpustack_t;
111                 #define CPU_REG_BITS    64
112
113                 #ifdef __WIN64__
114                         /* WIN64 is an IL32-P64 weirdo. */
115                         #define SIZEOF_LONG  4
116                 #endif
117         #else
118                 typedef uint32_t cpustack_t;
119                 #define CPU_REG_BITS    32
120         #endif
121
122 #elif CPU_PPC
123         #define NOP                 asm volatile ("nop" ::)
124         #define IRQ_DISABLE         FIXME
125         #define IRQ_ENABLE          FIXME
126         #define IRQ_SAVE_DISABLE(x) FIXME
127         #define IRQ_RESTORE(x)      FIXME
128         #define IRQ_GETSTATE()      FIXME
129
130         typedef uint32_t cpuflags_t; // FIXME
131         typedef uint32_t cpustack_t; // FIXME
132
133         /* Register counts include SREG too */
134         #define CPU_REG_BITS           (CPU_PPC32 ? 32 : 64)
135         #define CPU_REGS_CNT           FIXME
136         #define CPU_SAVED_REGS_CNT     FIXME
137         #define CPU_STACK_GROWS_UPWARD 0  //FIXME
138         #define CPU_SP_ON_EMPTY_SLOT   0  //FIXME
139         #define CPU_BYTE_ORDER         (__BIG_ENDIAN__ ? CPU_BIG_ENDIAN : CPU_LITTLE_ENDIAN)
140         #define CPU_HARVARD            0
141
142 #elif CPU_DSP56K
143
144         #define NOP                     asm(nop)
145         #define BREAKPOINT              asm(debug)
146         #define IRQ_DISABLE             do { asm(bfset #0x0200,SR); asm(nop); } while (0)
147         #define IRQ_ENABLE              do { asm(bfclr #0x0200,SR); asm(nop); } while (0)
148
149         #define IRQ_SAVE_DISABLE(x)  \
150                 do { (void)x; asm(move SR,x); asm(bfset #0x0200,SR); } while (0)
151         #define IRQ_RESTORE(x)  \
152                 do { (void)x; asm(move x,SR); } while (0)
153
154         static inline bool irq_running(void)
155         {
156                 extern void *user_sp;
157                 return !!user_sp;
158         }
159         #define IRQ_RUNNING() irq_running()
160
161         static inline bool irq_getstate(void)
162         {
163                 uint16_t x;
164                 asm(move SR,x);
165                 return !(x & 0x0200);
166         }
167         #define IRQ_GETSTATE() irq_getstate()
168
169         typedef uint16_t cpuflags_t;
170         typedef unsigned int cpustack_t;
171
172         #define CPU_REG_BITS            16
173         #define CPU_REGS_CNT            FIXME
174         #define CPU_SAVED_REGS_CNT      8
175         #define CPU_STACK_GROWS_UPWARD  1
176         #define CPU_SP_ON_EMPTY_SLOT    0
177         #define CPU_BYTE_ORDER          CPU_BIG_ENDIAN
178         #define CPU_HARVARD             1
179
180         /* Memory is word-addessed in the DSP56K */
181         #define CPU_BITS_PER_CHAR  16
182         #define SIZEOF_SHORT        1
183         #define SIZEOF_INT          1
184         #define SIZEOF_LONG         2
185         #define SIZEOF_PTR          1
186
187 #elif CPU_AVR
188
189         #define NOP           asm volatile ("nop" ::)
190         #define IRQ_DISABLE   asm volatile ("cli" ::)
191         #define IRQ_ENABLE    asm volatile ("sei" ::)
192
193         #define IRQ_SAVE_DISABLE(x) \
194         do { \
195                 __asm__ __volatile__( \
196                         "in %0,__SREG__\n\t" \
197                         "cli" \
198                         : "=r" (x) : /* no inputs */ : "cc" \
199                 ); \
200         } while (0)
201
202         #define IRQ_RESTORE(x) \
203         do { \
204                 __asm__ __volatile__( \
205                         "out __SREG__,%0" : /* no outputs */ : "r" (x) : "cc" \
206                 ); \
207         } while (0)
208
209         #define IRQ_GETSTATE() \
210         ({ \
211                 uint8_t sreg; \
212                 __asm__ __volatile__( \
213                         "in %0,__SREG__\n\t" \
214                         : "=r" (sreg)  /* no inputs & no clobbers */ \
215                 ); \
216                 (bool)(sreg & 0x80); \
217         })
218
219         typedef uint8_t cpuflags_t;
220         typedef uint8_t cpustack_t;
221
222         /* Register counts include SREG too */
223         #define CPU_REG_BITS            8
224         #define CPU_REGS_CNT           33
225         #define CPU_SAVED_REGS_CNT     19
226         #define CPU_STACK_GROWS_UPWARD  0
227         #define CPU_SP_ON_EMPTY_SLOT    1
228         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
229         #define CPU_HARVARD             1
230
231         /*!
232          * Initialization value for registers in stack frame.
233          * The register index is not directly corrispondent to CPU
234          * register numbers. Index 0 is the SREG register: the initial
235          * value is all 0 but the interrupt bit (bit 7).
236          */
237         #define CPU_REG_INIT_VALUE(reg) (reg == 0 ? 0x80 : 0)
238
239 #else
240         #error No CPU_... defined.
241 #endif
242
243 /*!
244  * Execute \a CODE atomically with respect to interrupts.
245  *
246  * \see IRQ_SAVE_DISABLE IRQ_RESTORE
247  */
248 #define ATOMIC(CODE) \
249         do { \
250                 cpuflags_t __flags; \
251                 IRQ_SAVE_DISABLE(__flags); \
252                 CODE; \
253                 IRQ_RESTORE(__flags); \
254         } while (0)
255
256
257 //! Default for macro not defined in the right arch section
258 #ifndef CPU_REG_INIT_VALUE
259         #define CPU_REG_INIT_VALUE(reg)     0
260 #endif
261
262
263 #ifndef CPU_STACK_GROWS_UPWARD
264         #error CPU_STACK_GROWS_UPWARD should have been defined to either 0 or 1
265 #endif
266
267 #ifndef CPU_SP_ON_EMPTY_SLOT
268         #error CPU_SP_ON_EMPTY_SLOT should have been defined to either 0 or 1
269 #endif
270
271 /*
272  * Support stack handling peculiarities of a few CPUs.
273  *
274  * Most processors let their stack grow downward and
275  * keep SP pointing at the last pushed value.
276  */
277 #if !CPU_STACK_GROWS_UPWARD
278         #if !CPU_SP_ON_EMPTY_SLOT
279                 /* Most microprocessors (x86, m68k...) */
280                 #define CPU_PUSH_WORD(sp, data) \
281                         do { *--(sp) = (data); } while (0)
282                 #define CPU_POP_WORD(sp) \
283                         (*(sp)++)
284         #else
285                 /* AVR insanity */
286                 #define CPU_PUSH_WORD(sp, data) \
287                         do { *(sp)-- = (data); } while (0)
288                 #define CPU_POP_WORD(sp) \
289                         (*++(sp))
290         #endif
291
292 #else /* CPU_STACK_GROWS_UPWARD */
293
294         #if !CPU_SP_ON_EMPTY_SLOT
295                 /* DSP56K and other weirdos */
296                 #define CPU_PUSH_WORD(sp, data) \
297                         do { *++(sp) = (cpustack_t)(data); } while (0)
298                 #define CPU_POP_WORD(sp) \
299                         (*(sp)--)
300         #else
301                 #error I bet you cannot find a CPU like this
302         #endif
303 #endif
304
305
306 #if CPU_DSP56K
307         /*
308          * DSP56k pushes both PC and SR to the stack in the JSR instruction, but
309          * RTS discards SR while returning (it does not restore it). So we push
310          * 0 to fake the same context.
311          */
312         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
313                 do { \
314                         CPU_PUSH_WORD((sp), (func)); \
315                         CPU_PUSH_WORD((sp), 0x100); \
316                 } while (0);
317
318 #elif CPU_AVR
319         /*
320          * In AVR, the addresses are pushed into the stack as little-endian, while
321          * memory accesses are big-endian (actually, it's a 8-bit CPU, so there is
322          * no natural endianess).
323          */
324         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
325                 do { \
326                         uint16_t funcaddr = (uint16_t)(func); \
327                         CPU_PUSH_WORD((sp), funcaddr); \
328                         CPU_PUSH_WORD((sp), funcaddr>>8); \
329                 } while (0)
330
331 #else
332         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
333                 CPU_PUSH_WORD((sp), (func))
334 #endif
335
336
337 /*!
338  * \name Default type sizes.
339  *
340  * These defaults are reasonable for most 16/32bit machines.
341  * Some of these macros may be overridden by CPU-specific code above.
342  *
343  * ANSI C requires that the following equations be true:
344  * \code
345  *   sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
346  *   sizeof(float) <= sizeof(double)
347  *   CPU_BITS_PER_CHAR  >= 8
348  *   CPU_BITS_PER_SHORT >= 8
349  *   CPU_BITS_PER_INT   >= 16
350  *   CPU_BITS_PER_LONG  >= 32
351  * \endcode
352  * \{
353  */
354 #ifndef SIZEOF_CHAR
355 #define SIZEOF_CHAR  1
356 #endif
357
358 #ifndef SIZEOF_SHORT
359 #define SIZEOF_SHORT  2
360 #endif
361
362 #ifndef SIZEOF_INT
363 #if CPU_REG_BITS < 32
364         #define SIZEOF_INT  2
365 #else
366         #define SIZEOF_INT  4
367 #endif
368 #endif /* !SIZEOF_INT */
369
370 #ifndef SIZEOF_LONG
371 #if CPU_REG_BITS > 32
372         #define SIZEOF_LONG  8
373 #else
374         #define SIZEOF_LONG  4
375 #endif
376 #endif
377
378 #ifndef SIZEOF_PTR
379 #if CPU_REG_BITS < 32
380         #define SIZEOF_PTR   2
381 #elif CPU_REG_BITS == 32
382         #define SIZEOF_PTR   4
383 #else /* CPU_REG_BITS > 32 */
384         #define SIZEOF_PTR   8
385 #endif
386 #endif
387
388 #ifndef CPU_BITS_PER_CHAR
389 #define CPU_BITS_PER_CHAR   (SIZEOF_CHAR * 8)
390 #endif
391
392 #ifndef CPU_BITS_PER_SHORT
393 #define CPU_BITS_PER_SHORT  (SIZEOF_SHORT * CPU_BITS_PER_CHAR)
394 #endif
395
396 #ifndef CPU_BITS_PER_INT
397 #define CPU_BITS_PER_INT    (SIZEOF_INT * CPU_BITS_PER_CHAR)
398 #endif
399
400 #ifndef CPU_BITS_PER_LONG
401 #define CPU_BITS_PER_LONG   (SIZEOF_LONG * CPU_BITS_PER_CHAR)
402 #endif
403
404 #ifndef CPU_BITS_PER_PTR
405 #define CPU_BITS_PER_PTR    (SIZEOF_PTR * CPU_BITS_PER_CHAR)
406 #endif
407
408 #ifndef BREAKPOINT
409 #define BREAKPOINT /* nop */
410 #endif
411
412 /*\}*/
413
414 /* Sanity checks for the above definitions */
415 STATIC_ASSERT(sizeof(char) == SIZEOF_CHAR);
416 STATIC_ASSERT(sizeof(short) == SIZEOF_SHORT);
417 STATIC_ASSERT(sizeof(long) == SIZEOF_LONG);
418 STATIC_ASSERT(sizeof(int) == SIZEOF_INT);
419 STATIC_ASSERT(sizeof(void *) == SIZEOF_PTR);
420 STATIC_ASSERT(sizeof(int8_t) * CPU_BITS_PER_CHAR == 8);
421 STATIC_ASSERT(sizeof(uint8_t) * CPU_BITS_PER_CHAR == 8);
422 STATIC_ASSERT(sizeof(int16_t) * CPU_BITS_PER_CHAR == 16);
423 STATIC_ASSERT(sizeof(uint16_t) * CPU_BITS_PER_CHAR == 16);
424 STATIC_ASSERT(sizeof(int32_t) * CPU_BITS_PER_CHAR == 32);
425 STATIC_ASSERT(sizeof(uint32_t) * CPU_BITS_PER_CHAR == 32);
426 #ifdef __HAS_INT64_T__
427 STATIC_ASSERT(sizeof(int64_t) * CPU_BITS_PER_CHAR == 64);
428 STATIC_ASSERT(sizeof(uint64_t) * CPU_BITS_PER_CHAR == 64);
429 #endif
430
431 /*!
432  * \def CPU_IDLE
433  *
434  * \brief Invoked by the scheduler to stop the CPU when idle.
435  *
436  * This hook can be redefined to put the CPU in low-power mode, or to
437  * profile system load with an external strobe, or to save CPU cycles
438  * in hosted environments such as emulators.
439  */
440 #ifndef CPU_IDLE
441         #if defined(ARCH_EMUL) && (ARCH & ARCH_EMUL)
442                 /* This emulator hook should yield the CPU to the host.  */
443                 EXTERN_C_BEGIN
444                 void SchedulerIdle(void);
445                 EXTERN_C_END
446                 #define CPU_IDLE SchedulerIdle()
447         #else /* !ARCH_EMUL */
448                 #define CPU_IDLE do { /* nothing */ } while (0)
449         #endif /* !ARCH_EMUL */
450 #endif /* !CPU_IDLE */
451
452 /* OBSOLETE */
453 #define SCHEDULER_IDLE CPU_IDLE
454
455 #endif /* DEVLIB_CPU_H */