Move top-level headers to cfg/ subdir.
[bertos.git] / cfg / cpu.h
1 /*!
2  * \file
3  * <!--
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.
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.1  2005/04/11 19:04:13  bernie
21  *#* Move top-level headers to cfg/ subdir.
22  *#*
23  *#* Revision 1.30  2005/03/15 00:20:09  bernie
24  *#* BREAKPOINT, IRQ_RUNNING(), IRQ_GETSTATE(): New DSP56K macros.
25  *#*
26  *#* Revision 1.29  2005/02/16 20:33:24  bernie
27  *#* Preliminary PPC support.
28  *#*
29  *#* Revision 1.28  2004/12/31 17:39:41  bernie
30  *#* Fix documentation.
31  *#*
32  *#* Revision 1.27  2004/12/31 17:02:47  bernie
33  *#* IRQ_SAVE_DISABLE(), IRQ_RESTORE(): Add null stubs for x86.
34  *#*
35  *#* Revision 1.26  2004/12/13 12:08:12  bernie
36  *#* DISABLE_IRQSAVE, ENABLE_IRQRESTORE, DISABLE_INTS, ENABLE_INTS: Remove obsolete macros.
37  *#*
38  *#* Revision 1.25  2004/12/08 08:31:02  bernie
39  *#* CPU_HARVARD: Define to 1 for AVR and DSP56K.
40  *#*/
41 #ifndef DEVLIB_CPU_H
42 #define DEVLIB_CPU_H
43
44 #include "compiler.h" /* for uintXX_t */
45
46
47 /*!
48  * \name Macros for determining CPU endianness.
49  * \{
50  */
51 #define CPU_BIG_ENDIAN    0x1234
52 #define CPU_LITTLE_ENDIAN 0x3412
53 /*\}*/
54
55 /*! Macro to include cpu-specific versions of the headers. */
56 #define CPU_HEADER(module)          PP_STRINGIZE(PP_CAT3(module, _, CPU_ID).h)
57
58
59 #if CPU_I196
60
61         #define NOP                     nop_instruction()
62         #define IRQ_DISABLE             disable_interrupt()
63         #define IRQ_ENABLE              enable_interrupt()
64
65         typedef uint16_t cpuflags_t; // FIXME
66         typedef unsigned int cpustack_t;
67
68         #define CPU_REG_BITS            16
69         #define CPU_REGS_CNT            16
70         #define CPU_STACK_GROWS_UPWARD  0
71         #define CPU_SP_ON_EMPTY_SLOT    0
72         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
73         #define CPU_HARVARD             0
74
75 #elif CPU_X86
76
77         #define NOP                     asm volatile ("nop")
78         #define IRQ_DISABLE             /* nothing */
79         #define IRQ_ENABLE              /* nothing */
80         #define IRQ_SAVE_DISABLE(x)     /* nothing */
81         #define IRQ_RESTORE(x)          /* nothing */
82
83         typedef uint32_t cpuflags_t; // FIXME
84         typedef uint32_t cpustack_t;
85
86         #define CPU_REG_BITS            32
87         #define CPU_REGS_CNT            7
88         #define CPU_STACK_GROWS_UPWARD  0
89         #define CPU_SP_ON_EMPTY_SLOT    0
90         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
91         #define CPU_HARVARD             0
92
93 #elif CPU_PPC
94         #define NOP                 asm volatile ("nop" ::)
95         #define IRQ_DISABLE         FIXME
96         #define IRQ_ENABLE          FIXME
97         #define IRQ_SAVE_DISABLE(x) FIXME
98         #define IRQ_RESTORE(x)      FIXME
99         #define IRQ_GETSTATE()      FIXME
100
101         typedef uint32_t cpuflags_t; // FIXME
102         typedef uint32_t cpustack_t; // FIXME
103
104         /* Register counts include SREG too */
105         #define CPU_REG_BITS           (CPU_PPC32 ? 32 : 64)
106         #define CPU_REGS_CNT           FIXME
107         #define CPU_SAVED_REGS_CNT     FIXME
108         #define CPU_STACK_GROWS_UPWARD 0  //FIXME
109         #define CPU_SP_ON_EMPTY_SLOT   0  //FIXME
110         #define CPU_BYTE_ORDER         (__BIG_ENDIAN__ ? CPU_BIG_ENDIAN : CPU_LITTLE_ENDIAN)
111         #define CPU_HARVARD            0
112
113 #elif CPU_DSP56K
114
115         #define NOP                     asm(nop)
116         #define BREAKPOINT              asm(debug)
117         #define IRQ_DISABLE             do { asm(bfset #0x0200,SR); asm(nop); } while (0)
118         #define IRQ_ENABLE              do { asm(bfclr #0x0200,SR); asm(nop); } while (0)
119
120         #define IRQ_SAVE_DISABLE(x)  \
121                 do { (void)x; asm(move SR,x); asm(bfset #0x0200,SR); } while (0)
122         #define IRQ_RESTORE(x)  \
123                 do { (void)x; asm(move x,SR); } while (0)
124
125         static inline bool irq_running(void)
126         {
127                 extern void *user_sp;
128                 return !!user_sp;
129         }
130         #define IRQ_RUNNING() irq_running()
131
132         static inline bool irq_getstate(void)
133         {
134                 uint16_t x;
135                 asm(move SR,x);
136                 return !(x & 0x0200);
137         }
138         #define IRQ_GETSTATE() irq_getstate()
139
140
141
142         typedef uint16_t cpuflags_t;
143         typedef unsigned int cpustack_t;
144
145         #define CPU_REG_BITS            16
146         #define CPU_REGS_CNT            FIXME
147         #define CPU_SAVED_REGS_CNT      8
148         #define CPU_STACK_GROWS_UPWARD  1
149         #define CPU_SP_ON_EMPTY_SLOT    0
150         #define CPU_BYTE_ORDER          CPU_BIG_ENDIAN
151         #define CPU_HARVARD             1
152
153         /* Memory is word-addessed in the DSP56K */
154         #define CPU_BITS_PER_CHAR  16
155         #define SIZEOF_SHORT        1
156         #define SIZEOF_INT          1
157         #define SIZEOF_LONG         2
158         #define SIZEOF_PTR          1
159
160 #elif CPU_AVR
161
162         #define NOP           asm volatile ("nop" ::)
163         #define IRQ_DISABLE   asm volatile ("cli" ::)
164         #define IRQ_ENABLE    asm volatile ("sei" ::)
165
166         #define IRQ_SAVE_DISABLE(x) \
167         do { \
168                 __asm__ __volatile__( \
169                         "in %0,__SREG__\n\t" \
170                         "cli" \
171                         : "=r" (x) : /* no inputs */ : "cc" \
172                 ); \
173         } while (0)
174
175         #define IRQ_RESTORE(x) \
176         do { \
177                 __asm__ __volatile__( \
178                         "out __SREG__,%0" : /* no outputs */ : "r" (x) : "cc" \
179                 ); \
180         } while (0)
181
182         #define IRQ_GETSTATE() \
183         ({ \
184                 uint8_t sreg; \
185                 __asm__ __volatile__( \
186                         "in %0,__SREG__\n\t" \
187                         : "=r" (sreg)  /* no inputs & no clobbers */ \
188                 ); \
189                 (bool)(sreg & 0x80); \
190         })
191
192         typedef uint8_t cpuflags_t;
193         typedef uint8_t cpustack_t;
194
195         /* Register counts include SREG too */
196         #define CPU_REG_BITS            8
197         #define CPU_REGS_CNT           33
198         #define CPU_SAVED_REGS_CNT     19
199         #define CPU_STACK_GROWS_UPWARD  0
200         #define CPU_SP_ON_EMPTY_SLOT    1
201         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
202         #define CPU_HARVARD             1
203
204         /*!
205          * Initialization value for registers in stack frame.
206          * The register index is not directly corrispondent to CPU
207          * register numbers. Index 0 is the SREG register: the initial
208          * value is all 0 but the interrupt bit (bit 7).
209          */
210         #define CPU_REG_INIT_VALUE(reg) (reg == 0 ? 0x80 : 0)
211
212 #endif
213
214 /*!
215  * Execute \a CODE atomically with respect to interrupts.
216  *
217  * \see IRQ_SAVE_DISABLE IRQ_RESTORE
218  */
219 #define ATOMIC(CODE) \
220         do { \
221                 cpuflags_t __flags; \
222                 IRQ_SAVE_DISABLE(__flags); \
223                 CODE; \
224                 IRQ_RESTORE(__flags); \
225         } while (0)
226
227
228 //! Default for macro not defined in the right arch section
229 #ifndef CPU_REG_INIT_VALUE
230         #define CPU_REG_INIT_VALUE(reg)     0
231 #endif
232
233
234 #ifndef CPU_STACK_GROWS_UPWARD
235         #error CPU_STACK_GROWS_UPWARD should have been defined to either 0 or 1
236 #endif
237
238 #ifndef CPU_SP_ON_EMPTY_SLOT
239         #error CPU_SP_ON_EMPTY_SLOT should have been defined to either 0 or 1
240 #endif
241
242 /*
243  * Support stack handling peculiarities of a few CPUs.
244  *
245  * Most processors let their stack grow downward and
246  * keep SP pointing at the last pushed value.
247  */
248 #if !CPU_STACK_GROWS_UPWARD
249         #if !CPU_SP_ON_EMPTY_SLOT
250                 /* Most microprocessors (x86, m68k...) */
251                 #define CPU_PUSH_WORD(sp, data) \
252                         do { *--(sp) = (data); } while (0)
253                 #define CPU_POP_WORD(sp) \
254                         (*(sp)++)
255         #else
256                 /* AVR insanity */
257                 #define CPU_PUSH_WORD(sp, data) \
258                         do { *(sp)-- = (data); } while (0)
259                 #define CPU_POP_WORD(sp) \
260                         (*++(sp))
261         #endif
262
263 #else /* CPU_STACK_GROWS_UPWARD */
264
265         #if !CPU_SP_ON_EMPTY_SLOT
266                 /* DSP56K and other weirdos */
267                 #define CPU_PUSH_WORD(sp, data) \
268                         do { *++(sp) = (cpustack_t)(data); } while (0)
269                 #define CPU_POP_WORD(sp) \
270                         (*(sp)--)
271         #else
272                 #error I bet you cannot find a CPU like this
273         #endif
274 #endif
275
276
277 #if CPU_DSP56K
278         /*
279          * DSP56k pushes both PC and SR to the stack in the JSR instruction, but
280          * RTS discards SR while returning (it does not restore it). So we push
281          * 0 to fake the same context.
282          */
283         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
284                 do { \
285                         CPU_PUSH_WORD((sp), (func)); \
286                         CPU_PUSH_WORD((sp), 0x100); \
287                 } while (0);
288
289 #elif CPU_AVR
290         /*
291          * In AVR, the addresses are pushed into the stack as little-endian, while
292          * memory accesses are big-endian (actually, it's a 8-bit CPU, so there is
293          * no natural endianess).
294          */
295         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
296                 do { \
297                         uint16_t funcaddr = (uint16_t)(func); \
298                         CPU_PUSH_WORD((sp), funcaddr); \
299                         CPU_PUSH_WORD((sp), funcaddr>>8); \
300                 } while (0)
301
302 #else
303         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
304                 CPU_PUSH_WORD((sp), (func))
305 #endif
306
307
308 /*!
309  * \name Default type sizes.
310  *
311  * These defaults are reasonable for most 16/32bit machines.
312  * Some of these macros may be overridden by CPU-specific code above.
313  *
314  * ANSI C requires that the following equations be true:
315  * \code
316  *   sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
317  *   sizeof(float) <= sizeof(double)
318  *   CPU_BITS_PER_CHAR  >= 8
319  *   CPU_BITS_PER_SHORT >= 8
320  *   CPU_BITS_PER_INT   >= 16
321  *   CPU_BITS_PER_LONG  >= 32
322  * \endcode
323  * \{
324  */
325 #ifndef SIZEOF_CHAR
326 #define SIZEOF_CHAR  1
327 #endif
328
329 #ifndef SIZEOF_SHORT
330 #define SIZEOF_SHORT  2
331 #endif
332
333 #ifndef SIZEOF_INT
334 #if CPU_REG_BITS < 32
335         #define SIZEOF_INT  2
336 #else
337         #define SIZEOF_INT  4
338 #endif
339 #endif /* !SIZEOF_INT */
340
341 #ifndef SIZEOF_LONG
342 #if CPU_REG_BITS > 32
343         #define SIZEOF_LONG  8
344 #else
345         #define SIZEOF_LONG  4
346 #endif
347 #endif
348
349 #ifndef SIZEOF_PTR
350 #define SIZEOF_PTR   SIZEOF_INT
351 #endif
352
353 #ifndef CPU_BITS_PER_CHAR
354 #define CPU_BITS_PER_CHAR   (SIZEOF_CHAR * 8)
355 #endif
356
357 #ifndef CPU_BITS_PER_SHORT
358 #define CPU_BITS_PER_SHORT  (SIZEOF_SHORT * CPU_BITS_PER_CHAR)
359 #endif
360
361 #ifndef CPU_BITS_PER_INT
362 #define CPU_BITS_PER_INT    (SIZEOF_INT * CPU_BITS_PER_CHAR)
363 #endif
364
365 #ifndef CPU_BITS_PER_LONG
366 #define CPU_BITS_PER_LONG   (SIZEOF_LONG * CPU_BITS_PER_CHAR)
367 #endif
368
369 #ifndef CPU_BITS_PER_PTR
370 #define CPU_BITS_PER_PTR    (SIZEOF_PTR * CPU_BITS_PER_CHAR)
371 #endif
372
373 #ifndef BREAKPOINT
374 #define BREAKPOINT /* nop */
375 #endif
376
377 /*\}*/
378
379 /* Sanity checks for the above definitions */
380 STATIC_ASSERT(sizeof(char) == SIZEOF_CHAR);
381 STATIC_ASSERT(sizeof(short) == SIZEOF_SHORT);
382 STATIC_ASSERT(sizeof(long) == SIZEOF_LONG);
383 STATIC_ASSERT(sizeof(int) == SIZEOF_INT);
384
385
386 /*!
387  * \def CPU_IDLE
388  *
389  * \brief Invoked by the scheduler to stop the CPU when idle.
390  *
391  * This hook can be redefined to put the CPU in low-power mode, or to
392  * profile system load with an external strobe, or to save CPU cycles
393  * in hosted environments such as emulators.
394  */
395 #ifndef CPU_IDLE
396         #if defined(ARCH_EMUL) && (ARCH & ARCH_EMUL)
397                 /* This emulator hook should yield the CPU to the host.  */
398                 EXTERN_C_BEGIN
399                 void SchedulerIdle(void);
400                 EXTERN_C_END
401                 #define CPU_IDLE SchedulerIdle()
402         #else /* !ARCH_EMUL */
403                 #define CPU_IDLE do { /* nothing */ } while (0)
404         #endif /* !ARCH_EMUL */
405 #endif /* !CPU_IDLE */
406
407 /* OBSOLETE */
408 #define SCHEDULER_IDLE CPU_IDLE
409
410 #endif /* DEVLIB_CPU_H */