4 * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 2004 Giovanni Bajo
6 * This file is part of DevLib - See devlib/README 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.10 2004/08/02 20:20:29 aleph
21 * Merge from project_ks
23 * Revision 1.9 2004/07/30 14:24:16 rasky
24 * Task switching con salvataggio perfetto stato di interrupt (SR)
25 * Kernel monitor per dump informazioni su stack dei processi
27 * Revision 1.8 2004/07/30 14:15:53 rasky
28 * Nuovo supporto unificato per detect della CPU
30 * Revision 1.7 2004/07/20 23:26:48 bernie
31 * Fix two errors introduced by previous commit.
33 * Revision 1.6 2004/07/20 23:12:16 bernie
34 * Rationalize and document SCHEDULER_IDLE.
36 * Revision 1.5 2004/07/20 16:20:35 bernie
37 * Move byte-order macros to mware/byteorder.h; Add missing author names.
39 * Revision 1.4 2004/07/20 16:06:04 bernie
40 * Add macros to handle endianess issues.
42 * Revision 1.3 2004/07/18 21:49:51 bernie
45 * Revision 1.2 2004/06/03 11:27:09 bernie
46 * Add dual-license information.
48 * Revision 1.1 2004/05/23 17:48:35 bernie
49 * Add top-level files.
58 // Macros for determining CPU endianness
59 #define CPU_BIG_ENDIAN 0x1234
60 #define CPU_LITTLE_ENDIAN 0x3412
62 // Macros to include cpu-specific version of the headers
63 #define CPU_HEADER(module) PP_STRINGIZE(PP_CAT3(module, _, CPU_ID).h)
68 #define DISABLE_INTS disable_interrupt()
69 #define ENABLE_INTS enable_interrupt()
70 #define NOP nop_instruction()
72 typedef uint16_t cpuflags_t; // FIXME
73 typedef unsigned int cpustack_t;
75 #define CPU_REGS_CNT 16
76 #define CPU_STACK_GROWS_UPWARD 0
77 #define CPU_SP_ON_EMPTY_SLOT 0
78 #define CPU_BYTE_ORDER CPU_LITTLE_ENDIAN
82 #define NOP asm volatile ("nop")
83 #define DISABLE_INTS /* nothing */
84 #define ENABLE_INTS /* nothing */
86 typedef uint32_t cpuflags_t; // FIXME
87 typedef uint32_t cpustack_t;
89 #define CPU_REGS_CNT 7
90 #define CPU_STACK_GROWS_UPWARD 0
91 #define CPU_SP_ON_EMPTY_SLOT 0
92 #define CPU_BYTE_ORDER CPU_LITTLE_ENDIAN
97 #define DISABLE_INTS do { asm(bfset #0x0200,SR); asm(nop); } while (0)
98 #define ENABLE_INTS do { asm(bfclr #0x0200,SR); asm(nop); } while (0)
100 #define DISABLE_IRQSAVE(x) \
101 do { asm(move SR,x); asm(bfset #0x0200,SR); } while (0)
102 #define ENABLE_IRQRESTORE(x) \
103 do { asm(move x,SR); } while (0)
105 typedef uint16_t cpuflags_t;
106 typedef unsigned int cpustack_t;
108 #define CPU_REGS_CNT FIXME
109 #define CPU_SAVED_REGS_CNT 8
110 #define CPU_STACK_GROWS_UPWARD 1
111 #define CPU_SP_ON_EMPTY_SLOT 0
112 #define CPU_BYTE_ORDER CPU_BIG_ENDIAN
116 #define NOP asm volatile ("nop" ::)
117 #define DISABLE_INTS asm volatile ("cli" ::)
118 #define ENABLE_INTS asm volatile ("sei" ::)
120 #define DISABLE_IRQSAVE(x) \
122 __asm__ __volatile__( \
123 "in %0,__SREG__\n\t" \
125 : "=r" (x) : /* no inputs */ : "cc" \
129 #define ENABLE_IRQRESTORE(x) \
131 __asm__ __volatile__( \
132 "out __SREG__,%0" : /* no outputs */ : "r" (x) : "cc" \
136 typedef uint8_t cpuflags_t;
137 typedef uint8_t cpustack_t;
139 /* Register counts include SREG too */
140 #define CPU_REGS_CNT 33
141 #define CPU_SAVED_REGS_CNT 19
142 #define CPU_STACK_GROWS_UPWARD 0
143 #define CPU_SP_ON_EMPTY_SLOT 1
144 #define CPU_BYTE_ORDER CPU_LITTLE_ENDIAN
147 * Initialization value for registers in stack frame.
148 * The register index is not directly corrispondent to CPU
149 * register numbers. Index 0 is the SREG register: the initial
150 * value is all 0 but the interrupt bit (bit 7).
152 #define CPU_REG_INIT_VALUE(reg) (reg == 0 ? 0x80 : 0)
157 //! Default for macro not defined in the right arch section
158 #ifndef CPU_REG_INIT_VALUE
159 #define CPU_REG_INIT_VALUE(reg) 0
163 #ifndef CPU_STACK_GROWS_UPWARD
164 #error CPU_STACK_GROWS_UPWARD should have been defined to either 0 or 1
167 #ifndef CPU_SP_ON_EMPTY_SLOT
168 #error CPU_SP_ON_EMPTY_SLOT should have been defined to either 0 or 1
172 * Support stack handling peculiarities of a few CPUs.
174 * Most processors let their stack grow downward and
175 * keep SP pointing at the last pushed value.
177 #if !CPU_STACK_GROWS_UPWARD
178 #if !CPU_SP_ON_EMPTY_SLOT
179 /* Most microprocessors (x86, m68k...) */
180 #define CPU_PUSH_WORD(sp, data) \
181 do { *--(sp) = (data); } while (0)
182 #define CPU_POP_WORD(sp) \
186 #define CPU_PUSH_WORD(sp, data) \
187 do { *(sp)-- = (data); } while (0)
188 #define CPU_POP_WORD(sp) \
192 #else /* CPU_STACK_GROWS_UPWARD */
194 #if !CPU_SP_ON_EMPTY_SLOT
195 /* DSP56K and other weirdos */
196 #define CPU_PUSH_WORD(sp, data) \
197 do { *++(sp) = (cpustack_t)(data); } while (0)
198 #define CPU_POP_WORD(sp) \
201 #error I bet you cannot find a CPU like this
207 /* DSP56k pushes both PC and SR to the stack in the JSR instruction, but
208 * RTS discards SR while returning (it does not restore it). So we push
209 * 0 to fake the same context.
211 #define CPU_PUSH_CALL_CONTEXT(sp, func) \
213 CPU_PUSH_WORD((sp), (func)); \
214 CPU_PUSH_WORD((sp), 0x100); \
218 /* In AVR, the addresses are pushed into the stack as little-endian, while
219 * memory accesses are big-endian (actually, it's a 8-bit CPU, so there is
220 * no natural endianess).
222 #define CPU_PUSH_CALL_CONTEXT(sp, func) \
224 uint16_t funcaddr = (uint16_t)(func); \
225 CPU_PUSH_WORD((sp), funcaddr); \
226 CPU_PUSH_WORD((sp), funcaddr>>8); \
230 #define CPU_PUSH_CALL_CONTEXT(sp, func) \
231 CPU_PUSH_WORD((sp), (func))
236 * \name SCHEDULER_IDLE
238 * \brief Invoked by the scheduler to stop the CPU when idle.
240 * This hook can be redefined to put the CPU in low-power mode, or to
241 * profile system load with an external strobe, or to save CPU cycles
242 * in hosted environments such as emulators.
244 #ifndef SCHEDULER_IDLE
245 #if defined(ARCH_EMUL) && (ARCH & ARCH_EMUL)
246 /* This emulator hook should yeld the CPU to the host. */
248 void SchedulerIdle(void);
250 #else /* !ARCH_EMUL */
251 #define SCHEDULER_IDLE /* nothing */
252 #endif /* !ARCH_EMUL */
253 #endif /* !SCHEDULER_IDLE */