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