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