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