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