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.
9 * \brief CPU-specific definitions
13 * \author Giovanni Bajo <rasky@develer.com>
14 * \author Bernardo Innocenti <bernie@develer.com>
15 * \author Stefano Fedrigo <aleph@develer.com>
20 *#* Revision 1.13 2006/03/27 04:49:23 bernie
21 *#* CPU_IDLE(): Fix for new emulator.
23 *#* Revision 1.12 2006/03/21 10:52:39 bernie
24 *#* Update ARM support.
26 *#* Revision 1.11 2006/03/20 17:49:00 bernie
29 *#* Revision 1.10 2006/02/24 01:17:30 bernie
30 *#* CPU_SAVED_REGS_CNT: Declare for x86/x86_64.
32 *#* Revision 1.9 2006/02/23 09:08:43 bernie
33 *#* Add note for a frequently reported non-bug.
35 *#* Revision 1.8 2006/02/10 12:37:45 bernie
36 *#* Add support for ARM on IAR.
38 *#* Revision 1.7 2005/11/27 03:04:38 bernie
39 *#* Add POSIX emulation for IRQ_* macros; Add Qt support.
41 *#* Revision 1.6 2005/07/19 07:26:49 bernie
42 *#* Add missing #endif.
44 *#* Revision 1.5 2005/06/27 21:24:17 bernie
45 *#* CPU_CSOURCE(): New macro.
47 *#* Revision 1.4 2005/06/14 06:15:10 bernie
48 *#* Add X86_64 support.
50 *#* Revision 1.3 2005/04/12 04:06:17 bernie
51 *#* Catch missing CPU earlier.
53 *#* Revision 1.2 2005/04/11 19:10:27 bernie
54 *#* Include top-level headers from cfg/ subdir.
56 *#* Revision 1.1 2005/04/11 19:04:13 bernie
57 *#* Move top-level headers to cfg/ subdir.
59 *#* Revision 1.30 2005/03/15 00:20:09 bernie
60 *#* BREAKPOINT, IRQ_RUNNING(), IRQ_GETSTATE(): New DSP56K macros.
62 *#* Revision 1.29 2005/02/16 20:33:24 bernie
63 *#* Preliminary PPC support.
65 *#* Revision 1.28 2004/12/31 17:39:41 bernie
66 *#* Fix documentation.
68 *#* Revision 1.27 2004/12/31 17:02:47 bernie
69 *#* IRQ_SAVE_DISABLE(), IRQ_RESTORE(): Add null stubs for x86.
71 *#* Revision 1.26 2004/12/13 12:08:12 bernie
72 *#* DISABLE_IRQSAVE, ENABLE_IRQRESTORE, DISABLE_INTS, ENABLE_INTS: Remove obsolete macros.
74 *#* Revision 1.25 2004/12/08 08:31:02 bernie
75 *#* CPU_HARVARD: Define to 1 for AVR and DSP56K.
80 #include <cfg/compiler.h> /* for uintXX_t */
81 #include <cfg/arch_config.h> /* ARCH_EMUL */
85 * \name Macros for determining CPU endianness.
88 #define CPU_BIG_ENDIAN 0x1234
89 #define CPU_LITTLE_ENDIAN 0x3412 /* Look twice, pal. This is not a bug. */
92 /*! Macro to include cpu-specific versions of the headers. */
93 #define CPU_HEADER(module) PP_STRINGIZE(PP_CAT3(module, _, CPU_ID).h)
95 /*! Macro to include cpu-specific versions of implementation files. */
96 #define CPU_CSOURCE(module) PP_STRINGIZE(PP_CAT3(module, _, CPU_ID).c)
101 #define NOP nop_instruction()
102 #define IRQ_DISABLE disable_interrupt()
103 #define IRQ_ENABLE enable_interrupt()
105 typedef uint16_t cpuflags_t; // FIXME
106 typedef unsigned int cpustack_t;
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
117 #define NOP asm volatile ("nop")
119 /* Get IRQ_* definitions from the hosting environment. */
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 */
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
138 typedef uint64_t cpustack_t;
139 #define CPU_REG_BITS 64
142 /* WIN64 is an IL32-P64 weirdo. */
143 #define SIZEOF_LONG 4
146 typedef uint32_t cpustack_t;
147 #define CPU_REG_BITS 32
152 typedef uint32_t cpuflags_t;
153 typedef uint32_t cpustack_t;
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
164 #ifdef __IAR_SYSTEMS_ICC__
168 #if __CPU_MODE__ == 1 /* Thumb */
170 extern cpuflags_t get_CPSR(void);
171 extern void set_CPSR(cpuflags_t flags);
173 #define get_CPSR __get_CPSR
174 #define set_CPSR __set_CPSR
177 #define NOP __no_operation()
178 #define IRQ_DISABLE __disable_interrupt()
179 #define IRQ_ENABLE __enable_interrupt()
181 #define IRQ_SAVE_DISABLE(x) \
184 __disable_interrupt(); \
187 #define IRQ_RESTORE(x) \
192 #define IRQ_GETSTATE() \
193 ((bool)(get_CPSR() & 0xb0))
195 #define BREAKPOINT /* asm("bkpt 0") DOES NOT WORK */
197 #else /* !__IAR_SYSTEMS_ICC__ */
199 #warning "IRQ_ macros need testing!"
201 #define NOP asm volatile ("mov r0,r0" ::)
203 #define IRQ_DISABLE \
207 "orr r0, r0, #0xb0\n\t" \
217 "bic r0, r0, #0xb0\n\t" \
223 #define IRQ_SAVE_DISABLE(x) \
228 "orr r0, r0, #0xb0\n\t" \
236 #define IRQ_RESTORE(x) \
247 #define IRQ_GETSTATE() \
257 (bool)(sreg & 0xb0); \
260 #endif /* __IAR_SYSTEMS_ICC_ */
263 #define NOP asm volatile ("nop" ::)
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
271 typedef uint32_t cpuflags_t; // FIXME
272 typedef uint32_t cpustack_t; // FIXME
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
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)
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)
295 static inline bool irq_running(void)
297 extern void *user_sp;
300 #define IRQ_RUNNING() irq_running()
302 static inline bool irq_getstate(void)
306 return !(x & 0x0200);
308 #define IRQ_GETSTATE() irq_getstate()
310 typedef uint16_t cpuflags_t;
311 typedef unsigned int cpustack_t;
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
321 /* Memory is word-addessed in the DSP56K */
322 #define CPU_BITS_PER_CHAR 16
323 #define SIZEOF_SHORT 1
325 #define SIZEOF_LONG 2
330 #define NOP asm volatile ("nop" ::)
331 #define IRQ_DISABLE asm volatile ("cli" ::)
332 #define IRQ_ENABLE asm volatile ("sei" ::)
334 #define IRQ_SAVE_DISABLE(x) \
336 __asm__ __volatile__( \
337 "in %0,__SREG__\n\t" \
339 : "=r" (x) : /* no inputs */ : "cc" \
343 #define IRQ_RESTORE(x) \
345 __asm__ __volatile__( \
346 "out __SREG__,%0" : /* no outputs */ : "r" (x) : "cc" \
350 #define IRQ_GETSTATE() \
353 __asm__ __volatile__( \
354 "in %0,__SREG__\n\t" \
355 : "=r" (sreg) /* no inputs & no clobbers */ \
357 (bool)(sreg & 0x80); \
360 typedef uint8_t cpuflags_t;
361 typedef uint8_t cpustack_t;
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
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).
378 #define CPU_REG_INIT_VALUE(reg) (reg == 0 ? 0x80 : 0)
381 #error No CPU_... defined.
385 * Execute \a CODE atomically with respect to interrupts.
387 * \see IRQ_SAVE_DISABLE IRQ_RESTORE
389 #define ATOMIC(CODE) \
391 cpuflags_t __flags; \
392 IRQ_SAVE_DISABLE(__flags); \
394 IRQ_RESTORE(__flags); \
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
404 #ifndef CPU_STACK_GROWS_UPWARD
405 #error CPU_STACK_GROWS_UPWARD should have been defined to either 0 or 1
408 #ifndef CPU_SP_ON_EMPTY_SLOT
409 #error CPU_SP_ON_EMPTY_SLOT should have been defined to either 0 or 1
413 * Support stack handling peculiarities of a few CPUs.
415 * Most processors let their stack grow downward and
416 * keep SP pointing at the last pushed value.
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) \
427 #define CPU_PUSH_WORD(sp, data) \
428 do { *(sp)-- = (data); } while (0)
429 #define CPU_POP_WORD(sp) \
433 #else /* CPU_STACK_GROWS_UPWARD */
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) \
442 #error I bet you cannot find a CPU like this
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.
453 #define CPU_PUSH_CALL_CONTEXT(sp, func) \
455 CPU_PUSH_WORD((sp), (func)); \
456 CPU_PUSH_WORD((sp), 0x100); \
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).
465 #define CPU_PUSH_CALL_CONTEXT(sp, func) \
467 uint16_t funcaddr = (uint16_t)(func); \
468 CPU_PUSH_WORD((sp), funcaddr); \
469 CPU_PUSH_WORD((sp), funcaddr>>8); \
473 #define CPU_PUSH_CALL_CONTEXT(sp, func) \
474 CPU_PUSH_WORD((sp), (cpustack_t)(func))
479 * \name Default type sizes.
481 * These defaults are reasonable for most 16/32bit machines.
482 * Some of these macros may be overridden by CPU-specific code above.
484 * ANSI C requires that the following equations be true:
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
496 #define SIZEOF_CHAR 1
500 #define SIZEOF_SHORT 2
504 #if CPU_REG_BITS < 32
509 #endif /* !SIZEOF_INT */
512 #if CPU_REG_BITS > 32
513 #define SIZEOF_LONG 8
515 #define SIZEOF_LONG 4
520 #if CPU_REG_BITS < 32
522 #elif CPU_REG_BITS == 32
524 #else /* CPU_REG_BITS > 32 */
529 #ifndef CPU_BITS_PER_CHAR
530 #define CPU_BITS_PER_CHAR (SIZEOF_CHAR * 8)
533 #ifndef CPU_BITS_PER_SHORT
534 #define CPU_BITS_PER_SHORT (SIZEOF_SHORT * CPU_BITS_PER_CHAR)
537 #ifndef CPU_BITS_PER_INT
538 #define CPU_BITS_PER_INT (SIZEOF_INT * CPU_BITS_PER_CHAR)
541 #ifndef CPU_BITS_PER_LONG
542 #define CPU_BITS_PER_LONG (SIZEOF_LONG * CPU_BITS_PER_CHAR)
545 #ifndef CPU_BITS_PER_PTR
546 #define CPU_BITS_PER_PTR (SIZEOF_PTR * CPU_BITS_PER_CHAR)
550 #define BREAKPOINT /* nop */
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);
575 * \brief Invoked by the scheduler to stop the CPU when idle.
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.
582 #if defined(ARCH_EMUL) && (ARCH & ARCH_EMUL)
583 /* This emulator hook should yield the CPU to the host. */
585 void emul_idle(void);
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 */
593 #endif /* DEVLIB_CPU_H */