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