Keep doc/ directory around for Doxygen.
[bertos.git] / cpu.h
1 /*!
2  * \file
3  * <!--
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.
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.10  2004/08/02 20:20:29  aleph
21  * Merge from project_ks
22  *
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
26  *
27  * Revision 1.8  2004/07/30 14:15:53  rasky
28  * Nuovo supporto unificato per detect della CPU
29  *
30  * Revision 1.7  2004/07/20 23:26:48  bernie
31  * Fix two errors introduced by previous commit.
32  *
33  * Revision 1.6  2004/07/20 23:12:16  bernie
34  * Rationalize and document SCHEDULER_IDLE.
35  *
36  * Revision 1.5  2004/07/20 16:20:35  bernie
37  * Move byte-order macros to mware/byteorder.h; Add missing author names.
38  *
39  * Revision 1.4  2004/07/20 16:06:04  bernie
40  * Add macros to handle endianess issues.
41  *
42  * Revision 1.3  2004/07/18 21:49:51  bernie
43  * Fixes for GCC 3.5.
44  *
45  * Revision 1.2  2004/06/03 11:27:09  bernie
46  * Add dual-license information.
47  *
48  * Revision 1.1  2004/05/23 17:48:35  bernie
49  * Add top-level files.
50  *
51  */
52 #ifndef CPU_H
53 #define CPU_H
54
55 #include "compiler.h"
56
57
58 // Macros for determining CPU endianness
59 #define CPU_BIG_ENDIAN    0x1234
60 #define CPU_LITTLE_ENDIAN 0x3412
61
62 // Macros to include cpu-specific version of the headers
63 #define CPU_HEADER(module)          PP_STRINGIZE(PP_CAT3(module, _, CPU_ID).h)
64
65
66 #if CPU_I196
67
68         #define DISABLE_INTS            disable_interrupt()
69         #define ENABLE_INTS             enable_interrupt()
70         #define NOP                     nop_instruction()
71
72         typedef uint16_t cpuflags_t; // FIXME
73         typedef unsigned int cpustack_t;
74
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
79
80 #elif CPU_X86
81
82         #define NOP                     asm volatile ("nop")
83         #define DISABLE_INTS            /* nothing */
84         #define ENABLE_INTS             /* nothing */
85
86         typedef uint32_t cpuflags_t; // FIXME
87         typedef uint32_t cpustack_t;
88
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
93
94 #elif CPU_DSP56K
95
96         #define NOP                     asm(nop)
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)
99
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)
104
105         typedef uint16_t cpuflags_t;
106         typedef unsigned int cpustack_t;
107
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
113
114 #elif CPU_AVR
115
116         #define NOP                     asm volatile ("nop" ::)
117         #define DISABLE_INTS            asm volatile ("cli" ::)
118         #define ENABLE_INTS             asm volatile ("sei" ::)
119
120         #define DISABLE_IRQSAVE(x) \
121         do { \
122                 __asm__ __volatile__( \
123                         "in %0,__SREG__\n\t" \
124                         "cli" \
125                         : "=r" (x) : /* no inputs */ : "cc" \
126                 ); \
127         } while (0)
128
129         #define ENABLE_IRQRESTORE(x) \
130         do { \
131                 __asm__ __volatile__( \
132                         "out __SREG__,%0" : /* no outputs */ : "r" (x) : "cc" \
133                 ); \
134         } while (0)
135
136         typedef uint8_t cpuflags_t;
137         typedef uint8_t cpustack_t;
138
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
145
146         /*!
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).
151          */
152         #define CPU_REG_INIT_VALUE(reg) (reg == 0 ? 0x80 : 0)
153
154 #endif
155
156
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
160 #endif
161
162
163 #ifndef CPU_STACK_GROWS_UPWARD
164         #error CPU_STACK_GROWS_UPWARD should have been defined to either 0 or 1
165 #endif
166
167 #ifndef CPU_SP_ON_EMPTY_SLOT
168         #error CPU_SP_ON_EMPTY_SLOT should have been defined to either 0 or 1
169 #endif
170
171 /*
172  * Support stack handling peculiarities of a few CPUs.
173  *
174  * Most processors let their stack grow downward and
175  * keep SP pointing at the last pushed value.
176  */
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) \
183                         (*(sp)++)
184         #else
185                 /* AVR insanity */
186                 #define CPU_PUSH_WORD(sp, data) \
187                         do { *(sp)-- = (data); } while (0)
188                 #define CPU_POP_WORD(sp) \
189                         (*++(sp))
190         #endif
191
192 #else /* CPU_STACK_GROWS_UPWARD */
193
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) \
199                         (*(sp)--)
200         #else
201                 #error I bet you cannot find a CPU like this
202         #endif
203 #endif
204
205
206 #if CPU_DSP56K
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.
210          */
211         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
212                 do { \
213                         CPU_PUSH_WORD((sp), (func)); \
214                         CPU_PUSH_WORD((sp), 0x100); \
215                 } while (0);
216
217 #elif CPU_AVR
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).
221          */
222         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
223                 do { \
224                         uint16_t funcaddr = (uint16_t)(func); \
225                         CPU_PUSH_WORD((sp), funcaddr); \
226                         CPU_PUSH_WORD((sp), funcaddr>>8); \
227                 } while (0)
228
229 #else
230         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
231                 CPU_PUSH_WORD((sp), (func))
232 #endif
233
234
235 /*!
236  * \name SCHEDULER_IDLE
237  *
238  * \brief Invoked by the scheduler to stop the CPU when idle.
239  *
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.
243  */
244 #ifndef SCHEDULER_IDLE
245         #if defined(ARCH_EMUL) && (ARCH & ARCH_EMUL)
246                 /* This emulator hook should yeld the CPU to the host.  */
247                 EXTERN_C_BEGIN
248                 void SchedulerIdle(void);
249                 EXTERN_C_END
250         #else /* !ARCH_EMUL */
251                 #define SCHEDULER_IDLE /* nothing */
252         #endif /* !ARCH_EMUL */
253 #endif /* !SCHEDULER_IDLE */
254
255 #endif /* CPU_H */