Remove bogus Doxygen markup.
[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.14  2004/08/24 13:29:28  bernie
21  * Trim CVS log; Rename header guards.
22  *
23  * Revision 1.12  2004/08/14 19:37:57  rasky
24  * Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
25  *
26  * Revision 1.11  2004/08/05 17:39:56  bernie
27  * Fix a Doxygen tag.
28  *
29  * Revision 1.10  2004/08/02 20:20:29  aleph
30  * Merge from project_ks
31  *
32  * Revision 1.9  2004/07/30 14:24:16  rasky
33  * Task switching con salvataggio perfetto stato di interrupt (SR)
34  * Kernel monitor per dump informazioni su stack dei processi
35  */
36 #ifndef DEVLIB_CPU_H
37 #define DEVLIB_CPU_H
38
39 #include "compiler.h" /* for uintXX_t, PP_CAT3(), PP_STRINGIZE() */
40
41
42 // Macros for determining CPU endianness
43 #define CPU_BIG_ENDIAN    0x1234
44 #define CPU_LITTLE_ENDIAN 0x3412
45
46 // Macros to include cpu-specific version of the headers
47 #define CPU_HEADER(module)          PP_STRINGIZE(PP_CAT3(module, _, CPU_ID).h)
48
49
50 #if CPU_I196
51
52         #define DISABLE_INTS            disable_interrupt()
53         #define ENABLE_INTS             enable_interrupt()
54         #define NOP                     nop_instruction()
55
56         typedef uint16_t cpuflags_t; // FIXME
57         typedef unsigned int cpustack_t;
58
59         #define CPU_REG_BITS            16
60         #define CPU_REGS_CNT            16
61         #define CPU_STACK_GROWS_UPWARD  0
62         #define CPU_SP_ON_EMPTY_SLOT    0
63         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
64
65 #elif CPU_X86
66
67         #define NOP                     asm volatile ("nop")
68         #define DISABLE_INTS            /* nothing */
69         #define ENABLE_INTS             /* nothing */
70
71         typedef uint32_t cpuflags_t; // FIXME
72         typedef uint32_t cpustack_t;
73
74         #define CPU_REG_BITS            32
75         #define CPU_REGS_CNT            7
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_DSP56K
81
82         #define NOP                     asm(nop)
83         #define DISABLE_INTS            do { asm(bfset #0x0200,SR); asm(nop); } while (0)
84         #define ENABLE_INTS             do { asm(bfclr #0x0200,SR); asm(nop); } while (0)
85
86         #define DISABLE_IRQSAVE(x)  \
87                 do { (void)x; asm(move SR,x); asm(bfset #0x0200,SR); } while (0)
88         #define ENABLE_IRQRESTORE(x)  \
89                 do { (void)x; asm(move x,SR); } while (0)
90
91         typedef uint16_t cpuflags_t;
92         typedef unsigned int cpustack_t;
93
94         #define CPU_REG_BITS            16
95         #define CPU_REGS_CNT            FIXME
96         #define CPU_SAVED_REGS_CNT      8
97         #define CPU_STACK_GROWS_UPWARD  1
98         #define CPU_SP_ON_EMPTY_SLOT    0
99         #define CPU_BYTE_ORDER          CPU_BIG_ENDIAN
100
101         /* Memory is word-addessed in the DSP56K */
102         #define BITSP_PER_CHAR  16
103         #define SIZEOF_SHORT     1
104         #define SIZEOF_INT       1
105         #define SIZEOF_LONG      2
106         #define SIZEOF_PTR       1
107
108 #elif CPU_AVR
109
110         #define NOP                     asm volatile ("nop" ::)
111         #define DISABLE_INTS            asm volatile ("cli" ::)
112         #define ENABLE_INTS             asm volatile ("sei" ::)
113
114         #define DISABLE_IRQSAVE(x) \
115         do { \
116                 __asm__ __volatile__( \
117                         "in %0,__SREG__\n\t" \
118                         "cli" \
119                         : "=r" (x) : /* no inputs */ : "cc" \
120                 ); \
121         } while (0)
122
123         #define ENABLE_IRQRESTORE(x) \
124         do { \
125                 __asm__ __volatile__( \
126                         "out __SREG__,%0" : /* no outputs */ : "r" (x) : "cc" \
127                 ); \
128         } while (0)
129
130         typedef uint8_t cpuflags_t;
131         typedef uint8_t cpustack_t;
132
133         /* Register counts include SREG too */
134         #define CPU_REG_BITS            8
135         #define CPU_REGS_CNT           33
136         #define CPU_SAVED_REGS_CNT     19
137         #define CPU_STACK_GROWS_UPWARD  0
138         #define CPU_SP_ON_EMPTY_SLOT    1
139         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
140
141         /*!
142          * Initialization value for registers in stack frame.
143          * The register index is not directly corrispondent to CPU
144          * register numbers. Index 0 is the SREG register: the initial
145          * value is all 0 but the interrupt bit (bit 7).
146          */
147         #define CPU_REG_INIT_VALUE(reg) (reg == 0 ? 0x80 : 0)
148
149 #endif
150
151
152 //! Default for macro not defined in the right arch section
153 #ifndef CPU_REG_INIT_VALUE
154         #define CPU_REG_INIT_VALUE(reg)     0
155 #endif
156
157
158 #ifndef CPU_STACK_GROWS_UPWARD
159         #error CPU_STACK_GROWS_UPWARD should have been defined to either 0 or 1
160 #endif
161
162 #ifndef CPU_SP_ON_EMPTY_SLOT
163         #error CPU_SP_ON_EMPTY_SLOT should have been defined to either 0 or 1
164 #endif
165
166 /*
167  * Support stack handling peculiarities of a few CPUs.
168  *
169  * Most processors let their stack grow downward and
170  * keep SP pointing at the last pushed value.
171  */
172 #if !CPU_STACK_GROWS_UPWARD
173         #if !CPU_SP_ON_EMPTY_SLOT
174                 /* Most microprocessors (x86, m68k...) */
175                 #define CPU_PUSH_WORD(sp, data) \
176                         do { *--(sp) = (data); } while (0)
177                 #define CPU_POP_WORD(sp) \
178                         (*(sp)++)
179         #else
180                 /* AVR insanity */
181                 #define CPU_PUSH_WORD(sp, data) \
182                         do { *(sp)-- = (data); } while (0)
183                 #define CPU_POP_WORD(sp) \
184                         (*++(sp))
185         #endif
186
187 #else /* CPU_STACK_GROWS_UPWARD */
188
189         #if !CPU_SP_ON_EMPTY_SLOT
190                 /* DSP56K and other weirdos */
191                 #define CPU_PUSH_WORD(sp, data) \
192                         do { *++(sp) = (cpustack_t)(data); } while (0)
193                 #define CPU_POP_WORD(sp) \
194                         (*(sp)--)
195         #else
196                 #error I bet you cannot find a CPU like this
197         #endif
198 #endif
199
200
201 #if CPU_DSP56K
202         /* DSP56k pushes both PC and SR to the stack in the JSR instruction, but
203          * RTS discards SR while returning (it does not restore it). So we push
204          * 0 to fake the same context.
205          */
206         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
207                 do { \
208                         CPU_PUSH_WORD((sp), (func)); \
209                         CPU_PUSH_WORD((sp), 0x100); \
210                 } while (0);
211
212 #elif CPU_AVR
213         /* In AVR, the addresses are pushed into the stack as little-endian, while
214          * memory accesses are big-endian (actually, it's a 8-bit CPU, so there is
215          * no natural endianess).
216          */
217         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
218                 do { \
219                         uint16_t funcaddr = (uint16_t)(func); \
220                         CPU_PUSH_WORD((sp), funcaddr); \
221                         CPU_PUSH_WORD((sp), funcaddr>>8); \
222                 } while (0)
223
224 #else
225         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
226                 CPU_PUSH_WORD((sp), (func))
227 #endif
228
229
230 /*!
231  * \def SIZEOF_CHAR SIZEOF_SHORT SIZEOF_INT SIZEOF_LONG SIZEOF_PTR
232  * \def BITS_PER_CHAR BITS_PER_SHORT BITS_PER_INT BITS_PER_LONG BITS_PER_PTR
233  *
234  * \brief Default type sizes
235  *
236  * These defaults are reasonable for most 16/32bit machines.
237  * Some of these macros may be overridden by CPU-specific code above.
238  *
239  * ANSI C specifies that the following equations must be true:
240  * \code
241  *   sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
242  *   sizeof(float) <= sizeof(double)
243  *   BITS_PER_CHAR  >= 8
244  *   BITS_PER_SHORT >= 8
245  *   BITS_PER_INT   >= 16
246  *   BITS_PER_LONG  >= 32
247  * \end code
248  * \{
249  */
250 #ifndef SIZEOF_CHAR
251 #define SIZEOF_CHAR  1
252 #endif
253
254 #ifndef SIZEOF_SHORT
255 #define SIZEOF_SHORT  2
256 #endif
257
258 #ifndef SIZEOF_INT
259 #if CPU_REG_BITS < 32
260         #define SIZEOF_INT  2
261 #else
262         #define SIZEOF_INT  4
263 #endif
264 #endif /* !SIZEOF_INT */
265
266 #ifndef SIZEOF_LONG
267 #define SIZEOF_LONG  4
268 #endif
269
270 #ifndef SIZEOF_PTR
271 #define SIZEOF_PTR   SIZEOF_INT
272 #endif
273
274 #ifndef BITS_PER_CHAR
275 #define BITS_PER_CHAR   (SIZEOF_CHAR * 8)
276 #endif
277
278 #ifndef BITS_PER_SHORT
279 #define BITS_PER_SHORT  (SIZEOF_SHORT * BITS_PER_CHAR)
280 #endif
281
282 #ifndef BITS_PER_INT
283 #define BITS_PER_INT    (SIZEOF_INT * BITS_PER_CHAR)
284 #endif
285
286 #ifndef BITS_PER_LONG
287 #define BITS_PER_LONG   (SIZEOF_LONG * BITS_PER_CHAR)
288 #endif
289
290 #ifndef BITS_PER_PTR
291 #define BITS_PER_PTR    (SIZEOF_PTR * BITS_PER_CHAR)
292 #endif
293 /*\}*/
294
295
296 /*!
297  * \def SCHEDULER_IDLE
298  *
299  * \brief Invoked by the scheduler to stop the CPU when idle.
300  *
301  * This hook can be redefined to put the CPU in low-power mode, or to
302  * profile system load with an external strobe, or to save CPU cycles
303  * in hosted environments such as emulators.
304  */
305 #ifndef SCHEDULER_IDLE
306         #if defined(ARCH_EMUL) && (ARCH & ARCH_EMUL)
307                 /* This emulator hook should yield the CPU to the host.  */
308                 EXTERN_C_BEGIN
309                 void SchedulerIdle(void);
310                 EXTERN_C_END
311                 #define SCHEDULER_IDLE SchedulerIdle()
312         #else /* !ARCH_EMUL */
313                 #define SCHEDULER_IDLE do { /* nothing */ } while (0)
314         #endif /* !ARCH_EMUL */
315 #endif /* !SCHEDULER_IDLE */
316
317 #endif /* DEVLIB_CPU_H */