4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2004, 2005, 2006, 2007 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 2004 Giovanni Bajo
34 * \brief CPU-specific definitions
36 * \author Giovanni Bajo <rasky@develer.com>
37 * \author Bernardo Innocenti <bernie@develer.com>
38 * \author Stefano Fedrigo <aleph@develer.com>
39 * \author Francesco Sacchi <batt@develer.com>
45 #include <cfg/compiler.h> /* for uintXX_t */
46 #include <cfg/arch_config.h> /* ARCH_EMUL */
50 * \name Macros for determining CPU endianness.
53 #define CPU_BIG_ENDIAN 0x1234
54 #define CPU_LITTLE_ENDIAN 0x3412 /* Look twice, pal. This is not a bug. */
57 /** Macro to include cpu-specific versions of the headers. */
58 #define CPU_HEADER(module) PP_STRINGIZE(drv/PP_CAT3(module, _, CPU_ID).h)
60 /** Macro to include cpu-specific versions of implementation files. */
61 #define CPU_CSOURCE(module) PP_STRINGIZE(drv/PP_CAT3(module, _, CPU_ID).c)
66 #define NOP nop_instruction()
67 #define IRQ_DISABLE disable_interrupt()
68 #define IRQ_ENABLE enable_interrupt()
70 typedef uint16_t cpuflags_t; // FIXME
71 typedef unsigned int cpustack_t;
73 #define CPU_REG_BITS 16
74 #define CPU_REGS_CNT 16
75 #define CPU_STACK_GROWS_UPWARD 0
76 #define CPU_SP_ON_EMPTY_SLOT 0
77 #define CPU_BYTE_ORDER CPU_LITTLE_ENDIAN
82 #define NOP asm volatile ("nop")
84 /* Get IRQ_* definitions from the hosting environment. */
87 #define IRQ_DISABLE FIXME
88 #define IRQ_ENABLE FIXME
89 #define IRQ_SAVE_DISABLE(x) FIXME
90 #define IRQ_RESTORE(x) FIXME
91 typedef uint32_t cpuflags_t; // FIXME
92 #endif /* OS_EMBEDDED */
95 #define CPU_REGS_CNT 7
96 #define CPU_SAVED_REGS_CNT 7
97 #define CPU_STACK_GROWS_UPWARD 0
98 #define CPU_SP_ON_EMPTY_SLOT 0
99 #define CPU_BYTE_ORDER CPU_LITTLE_ENDIAN
100 #define CPU_HARVARD 0
103 typedef uint64_t cpustack_t;
104 #define CPU_REG_BITS 64
107 /* WIN64 is an IL32-P64 weirdo. */
108 #define SIZEOF_LONG 4
111 typedef uint32_t cpustack_t;
112 #define CPU_REG_BITS 32
117 typedef uint32_t cpuflags_t;
118 typedef uint32_t cpustack_t;
120 /* Register counts include SREG too */
121 #define CPU_REG_BITS 32
122 #define CPU_REGS_CNT 16
123 #define CPU_SAVED_REGS_CNT 9
124 #define CPU_STACK_GROWS_UPWARD 0
125 #define CPU_SP_ON_EMPTY_SLOT 0
126 #define CPU_BYTE_ORDER (__BIG_ENDIAN__ ? CPU_BIG_ENDIAN : CPU_LITTLE_ENDIAN)
127 #define CPU_HARVARD 0
129 #ifdef __IAR_SYSTEMS_ICC__
133 #if __CPU_MODE__ == 1 /* Thumb */
135 extern cpuflags_t get_CPSR(void);
136 extern void set_CPSR(cpuflags_t flags);
138 #define get_CPSR __get_CPSR
139 #define set_CPSR __set_CPSR
142 #define NOP __no_operation()
143 #define IRQ_DISABLE __disable_interrupt()
144 #define IRQ_ENABLE __enable_interrupt()
146 #define IRQ_SAVE_DISABLE(x) \
149 __disable_interrupt(); \
152 #define IRQ_RESTORE(x) \
157 #define IRQ_GETSTATE() \
158 ((bool)(get_CPSR() & 0xb0))
160 #define BREAKPOINT /* asm("bkpt 0") DOES NOT WORK */
162 #else /* !__IAR_SYSTEMS_ICC__ */
163 #define NOP asm volatile ("mov r0,r0" ::)
165 #define IRQ_DISABLE \
169 "orr r0, r0, #0xc0\n\t" \
179 "bic r0, r0, #0xc0\n\t" \
185 #define IRQ_SAVE_DISABLE(x) \
189 "orr r0, %0, #0xc0\n\t" \
197 #define IRQ_RESTORE(x) \
206 #define CPU_READ_FLAGS() \
217 #define IRQ_GETSTATE() (!((CPU_READ_FLAGS() & 0xc0) == 0xc0))
220 * Initialization value for registers in stack frame.
221 * The register index is not directly corrispondent to CPU
222 * register numbers, but is related to how are pushed to stack (\see asm_switch_context).
223 * Index (CPU_SAVED_REGS_CNT - 1) is the CPSR register:
224 * the initial value is taken from current CPSR.
226 #define CPU_REG_INIT_VALUE(reg) (reg == (CPU_SAVED_REGS_CNT - 1) ? CPU_READ_FLAGS() : 0)
228 #endif /* !__IAR_SYSTEMS_ICC_ */
231 #define NOP asm volatile ("nop" ::)
233 #define IRQ_DISABLE FIXME
234 #define IRQ_ENABLE FIXME
235 #define IRQ_SAVE_DISABLE(x) FIXME
236 #define IRQ_RESTORE(x) FIXME
237 #define IRQ_GETSTATE() FIXME
239 typedef uint32_t cpuflags_t; // FIXME
240 typedef uint32_t cpustack_t; // FIXME
242 /* Register counts include SREG too */
243 #define CPU_REG_BITS (CPU_PPC32 ? 32 : 64)
244 #define CPU_REGS_CNT FIXME
245 #define CPU_SAVED_REGS_CNT FIXME
246 #define CPU_STACK_GROWS_UPWARD 0 //FIXME
247 #define CPU_SP_ON_EMPTY_SLOT 0 //FIXME
248 #define CPU_BYTE_ORDER (__BIG_ENDIAN__ ? CPU_BIG_ENDIAN : CPU_LITTLE_ENDIAN)
249 #define CPU_HARVARD 0
254 #define BREAKPOINT asm(debug)
255 #define IRQ_DISABLE do { asm(bfset #0x0200,SR); asm(nop); } while (0)
256 #define IRQ_ENABLE do { asm(bfclr #0x0200,SR); asm(nop); } while (0)
258 #define IRQ_SAVE_DISABLE(x) \
259 do { (void)x; asm(move SR,x); asm(bfset #0x0200,SR); } while (0)
260 #define IRQ_RESTORE(x) \
261 do { (void)x; asm(move x,SR); } while (0)
263 static inline bool irq_running(void)
265 extern void *user_sp;
268 #define IRQ_RUNNING() irq_running()
270 static inline bool irq_getstate(void)
274 return !(x & 0x0200);
276 #define IRQ_GETSTATE() irq_getstate()
278 typedef uint16_t cpuflags_t;
279 typedef unsigned int cpustack_t;
281 #define CPU_REG_BITS 16
282 #define CPU_REGS_CNT FIXME
283 #define CPU_SAVED_REGS_CNT 8
284 #define CPU_STACK_GROWS_UPWARD 1
285 #define CPU_SP_ON_EMPTY_SLOT 0
286 #define CPU_BYTE_ORDER CPU_BIG_ENDIAN
287 #define CPU_HARVARD 1
289 /* Memory is word-addessed in the DSP56K */
290 #define CPU_BITS_PER_CHAR 16
291 #define SIZEOF_SHORT 1
293 #define SIZEOF_LONG 2
298 #define NOP asm volatile ("nop" ::)
299 #define IRQ_DISABLE asm volatile ("cli" ::)
300 #define IRQ_ENABLE asm volatile ("sei" ::)
302 #define IRQ_SAVE_DISABLE(x) \
304 __asm__ __volatile__( \
305 "in %0,__SREG__\n\t" \
307 : "=r" (x) : /* no inputs */ : "cc" \
311 #define IRQ_RESTORE(x) \
313 __asm__ __volatile__( \
314 "out __SREG__,%0" : /* no outputs */ : "r" (x) : "cc" \
318 #define IRQ_GETSTATE() \
321 __asm__ __volatile__( \
322 "in %0,__SREG__\n\t" \
323 : "=r" (sreg) /* no inputs & no clobbers */ \
325 (bool)(sreg & 0x80); \
328 typedef uint8_t cpuflags_t;
329 typedef uint8_t cpustack_t;
331 /* Register counts include SREG too */
332 #define CPU_REG_BITS 8
333 #define CPU_REGS_CNT 33
334 #define CPU_SAVED_REGS_CNT 19
335 #define CPU_STACK_GROWS_UPWARD 0
336 #define CPU_SP_ON_EMPTY_SLOT 1
337 #define CPU_BYTE_ORDER CPU_LITTLE_ENDIAN
338 #define CPU_HARVARD 1
341 * Initialization value for registers in stack frame.
342 * The register index is not directly corrispondent to CPU
343 * register numbers. Index 0 is the SREG register: the initial
344 * value is all 0 but the interrupt bit (bit 7).
346 #define CPU_REG_INIT_VALUE(reg) (reg == 0 ? 0x80 : 0)
349 #error No CPU_... defined.
353 * Execute \a CODE atomically with respect to interrupts.
355 * \see IRQ_SAVE_DISABLE IRQ_RESTORE
357 #define ATOMIC(CODE) \
359 cpuflags_t __flags; \
360 IRQ_SAVE_DISABLE(__flags); \
362 IRQ_RESTORE(__flags); \
366 /// Default for macro not defined in the right arch section
367 #ifndef CPU_REG_INIT_VALUE
368 #define CPU_REG_INIT_VALUE(reg) 0
372 #ifndef CPU_STACK_GROWS_UPWARD
373 #error CPU_STACK_GROWS_UPWARD should have been defined to either 0 or 1
376 #ifndef CPU_SP_ON_EMPTY_SLOT
377 #error CPU_SP_ON_EMPTY_SLOT should have been defined to either 0 or 1
381 * Support stack handling peculiarities of a few CPUs.
383 * Most processors let their stack grow downward and
384 * keep SP pointing at the last pushed value.
386 #if !CPU_STACK_GROWS_UPWARD
387 #if !CPU_SP_ON_EMPTY_SLOT
388 /* Most microprocessors (x86, m68k...) */
389 #define CPU_PUSH_WORD(sp, data) \
390 do { *--(sp) = (data); } while (0)
391 #define CPU_POP_WORD(sp) \
395 #define CPU_PUSH_WORD(sp, data) \
396 do { *(sp)-- = (data); } while (0)
397 #define CPU_POP_WORD(sp) \
401 #else /* CPU_STACK_GROWS_UPWARD */
403 #if !CPU_SP_ON_EMPTY_SLOT
404 /* DSP56K and other weirdos */
405 #define CPU_PUSH_WORD(sp, data) \
406 do { *++(sp) = (cpustack_t)(data); } while (0)
407 #define CPU_POP_WORD(sp) \
410 #error I bet you cannot find a CPU like this
417 * DSP56k pushes both PC and SR to the stack in the JSR instruction, but
418 * RTS discards SR while returning (it does not restore it). So we push
419 * 0 to fake the same context.
421 #define CPU_PUSH_CALL_CONTEXT(sp, func) \
423 CPU_PUSH_WORD((sp), (func)); \
424 CPU_PUSH_WORD((sp), 0x100); \
429 * In AVR, the addresses are pushed into the stack as little-endian, while
430 * memory accesses are big-endian (actually, it's a 8-bit CPU, so there is
431 * no natural endianess).
433 #define CPU_PUSH_CALL_CONTEXT(sp, func) \
435 uint16_t funcaddr = (uint16_t)(func); \
436 CPU_PUSH_WORD((sp), funcaddr); \
437 CPU_PUSH_WORD((sp), funcaddr>>8); \
441 #define CPU_PUSH_CALL_CONTEXT(sp, func) \
442 CPU_PUSH_WORD((sp), (cpustack_t)(func))
447 * \name Default type sizes.
449 * These defaults are reasonable for most 16/32bit machines.
450 * Some of these macros may be overridden by CPU-specific code above.
452 * ANSI C requires that the following equations be true:
454 * sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
455 * sizeof(float) <= sizeof(double)
456 * CPU_BITS_PER_CHAR >= 8
457 * CPU_BITS_PER_SHORT >= 8
458 * CPU_BITS_PER_INT >= 16
459 * CPU_BITS_PER_LONG >= 32
464 #define SIZEOF_CHAR 1
468 #define SIZEOF_SHORT 2
472 #if CPU_REG_BITS < 32
477 #endif /* !SIZEOF_INT */
480 #if CPU_REG_BITS > 32
481 #define SIZEOF_LONG 8
483 #define SIZEOF_LONG 4
488 #if CPU_REG_BITS < 32
490 #elif CPU_REG_BITS == 32
492 #else /* CPU_REG_BITS > 32 */
497 #ifndef CPU_BITS_PER_CHAR
498 #define CPU_BITS_PER_CHAR (SIZEOF_CHAR * 8)
501 #ifndef CPU_BITS_PER_SHORT
502 #define CPU_BITS_PER_SHORT (SIZEOF_SHORT * CPU_BITS_PER_CHAR)
505 #ifndef CPU_BITS_PER_INT
506 #define CPU_BITS_PER_INT (SIZEOF_INT * CPU_BITS_PER_CHAR)
509 #ifndef CPU_BITS_PER_LONG
510 #define CPU_BITS_PER_LONG (SIZEOF_LONG * CPU_BITS_PER_CHAR)
513 #ifndef CPU_BITS_PER_PTR
514 #define CPU_BITS_PER_PTR (SIZEOF_PTR * CPU_BITS_PER_CHAR)
518 #define BREAKPOINT /* nop */
523 /* Sanity checks for the above definitions */
524 STATIC_ASSERT(sizeof(char) == SIZEOF_CHAR);
525 STATIC_ASSERT(sizeof(short) == SIZEOF_SHORT);
526 STATIC_ASSERT(sizeof(long) == SIZEOF_LONG);
527 STATIC_ASSERT(sizeof(int) == SIZEOF_INT);
528 STATIC_ASSERT(sizeof(void *) == SIZEOF_PTR);
529 STATIC_ASSERT(sizeof(int8_t) * CPU_BITS_PER_CHAR == 8);
530 STATIC_ASSERT(sizeof(uint8_t) * CPU_BITS_PER_CHAR == 8);
531 STATIC_ASSERT(sizeof(int16_t) * CPU_BITS_PER_CHAR == 16);
532 STATIC_ASSERT(sizeof(uint16_t) * CPU_BITS_PER_CHAR == 16);
533 STATIC_ASSERT(sizeof(int32_t) * CPU_BITS_PER_CHAR == 32);
534 STATIC_ASSERT(sizeof(uint32_t) * CPU_BITS_PER_CHAR == 32);
535 #ifdef __HAS_INT64_T__
536 STATIC_ASSERT(sizeof(int64_t) * CPU_BITS_PER_CHAR == 64);
537 STATIC_ASSERT(sizeof(uint64_t) * CPU_BITS_PER_CHAR == 64);
543 * \brief Invoked by the scheduler to stop the CPU when idle.
545 * This hook can be redefined to put the CPU in low-power mode, or to
546 * profile system load with an external strobe, or to save CPU cycles
547 * in hosted environments such as emulators.
550 #if defined(ARCH_EMUL) && (ARCH & ARCH_EMUL)
551 /* This emulator hook should yield the CPU to the host. */
553 void emul_idle(void);
555 #define CPU_IDLE emul_idle()
556 #else /* !ARCH_EMUL */
557 #define CPU_IDLE do { /* nothing */ } while (0)
558 #endif /* !ARCH_EMUL */
559 #endif /* !CPU_IDLE */
561 #endif /* CPU_CPU_H */