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