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