Update copyright, fix header guard.
[bertos.git] / cpu / cpu.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004, 2005, 2006, 2007 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2004 Giovanni Bajo
31  *
32  * -->
33  *
34  * \brief CPU-specific definitions
35  *
36  * \author Giovanni Bajo <rasky@develer.com>
37  * \author Bernardo Innocenti <bernie@develer.com>
38  * \author Stefano Fedrigo <aleph@develer.com>
39  * \author Francesco Sacchi <batt@develer.com>
40  */
41 #ifndef CPU_CPU_H
42 #define CPU_CPU_H
43
44 #include "detect.h"
45 #include <cfg/compiler.h> /* for uintXX_t */
46 #include <cfg/arch_config.h>  /* ARCH_EMUL */
47
48
49 /**
50  * \name Macros for determining CPU endianness.
51  * \{
52  */
53 #define CPU_BIG_ENDIAN    0x1234
54 #define CPU_LITTLE_ENDIAN 0x3412 /* Look twice, pal. This is not a bug. */
55 /*\}*/
56
57 /** Macro to include cpu-specific versions of the headers. */
58 #define CPU_HEADER(module)          PP_STRINGIZE(PP_CAT3(module, _, CPU_ID).h)
59
60 /** Macro to include cpu-specific versions of implementation files. */
61 #define CPU_CSOURCE(module)         PP_STRINGIZE(PP_CAT3(module, _, CPU_ID).c)
62
63
64 #if CPU_I196
65
66         #define NOP                     nop_instruction()
67         #define IRQ_DISABLE             disable_interrupt()
68         #define IRQ_ENABLE              enable_interrupt()
69
70         typedef uint16_t cpuflags_t; // FIXME
71         typedef unsigned int cpustack_t;
72
73         #define CPU_REG_BITS            16
74         #define CPU_REGS_CNT            16
75         #define CPU_STACK_GROWS_UPWARD  0
76         #define CPU_SP_ON_EMPTY_SLOT    0
77         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
78         #define CPU_HARVARD             0
79
80 #elif CPU_X86
81
82         #define NOP                     asm volatile ("nop")
83
84         /* Get IRQ_* definitions from the hosting environment. */
85         #include <cfg/os.h>
86         #if OS_EMBEDDED
87                 #define IRQ_DISABLE             FIXME
88                 #define IRQ_ENABLE              FIXME
89                 #define IRQ_SAVE_DISABLE(x)     FIXME
90                 #define IRQ_RESTORE(x)          FIXME
91                 typedef uint32_t cpuflags_t; // FIXME
92         #endif /* OS_EMBEDDED */
93
94
95         #define CPU_REGS_CNT            7
96         #define CPU_SAVED_REGS_CNT      7
97         #define CPU_STACK_GROWS_UPWARD  0
98         #define CPU_SP_ON_EMPTY_SLOT    0
99         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
100         #define CPU_HARVARD             0
101
102         #if CPU_X86_64
103                 typedef uint64_t cpustack_t;
104                 #define CPU_REG_BITS    64
105
106                 #ifdef __WIN64__
107                         /* WIN64 is an IL32-P64 weirdo. */
108                         #define SIZEOF_LONG  4
109                 #endif
110         #else
111                 typedef uint32_t cpustack_t;
112                 #define CPU_REG_BITS    32
113         #endif
114
115 #elif CPU_ARM
116
117         typedef uint32_t cpuflags_t;
118         typedef uint32_t cpustack_t;
119
120         /* Register counts include SREG too */
121         #define CPU_REG_BITS           32
122         #define CPU_REGS_CNT           16
123         #define CPU_SAVED_REGS_CNT     FIXME
124         #define CPU_STACK_GROWS_UPWARD 0
125         #define CPU_SP_ON_EMPTY_SLOT   0
126         #define CPU_BYTE_ORDER         (__BIG_ENDIAN__ ? CPU_BIG_ENDIAN : CPU_LITTLE_ENDIAN)
127         #define CPU_HARVARD            0
128
129         #ifdef __IAR_SYSTEMS_ICC__
130
131                 #include <inarm.h>
132
133                 #if __CPU_MODE__ == 1 /* Thumb */
134                         /* Use stubs */
135                         extern cpuflags_t get_CPSR(void);
136                         extern void set_CPSR(cpuflags_t flags);
137                 #else
138                         #define get_CPSR __get_CPSR
139                         #define set_CPSR __set_CPSR
140                 #endif
141
142                 #define NOP         __no_operation()
143                 #define IRQ_DISABLE __disable_interrupt()
144                 #define IRQ_ENABLE  __enable_interrupt()
145
146                 #define IRQ_SAVE_DISABLE(x) \
147                 do { \
148                         (x) = get_CPSR(); \
149                         __disable_interrupt(); \
150                 } while (0)
151
152                 #define IRQ_RESTORE(x) \
153                 do { \
154                         set_CPSR(x); \
155                 } while (0)
156
157                 #define IRQ_GETSTATE() \
158                         ((bool)(get_CPSR() & 0xb0))
159
160                 #define BREAKPOINT  /* asm("bkpt 0") DOES NOT WORK */
161
162         #else /* !__IAR_SYSTEMS_ICC__ */
163
164                 #warning "IRQ_ macros need testing!"
165                 #warning "Test now or die :-)"
166
167                 #define NOP         asm volatile ("mov r0,r0" ::)
168
169                 #define IRQ_DISABLE \
170                 do { \
171                         asm volatile ( \
172                                 "mrs r0, cpsr\n\t" \
173                                 "orr r0, r0, #0xc0\n\t" \
174                                 "msr cpsr_c, r0" \
175                                 ::: "r0" \
176                         ); \
177                 } while (0)
178
179                 #define IRQ_ENABLE \
180                 do { \
181                         asm volatile ( \
182                                 "mrs r0, cpsr\n\t" \
183                                 "bic r0, r0, #0xc0\n\t" \
184                                 "msr cpsr_c, r0" \
185                                 ::: "r0" \
186                         ); \
187                 } while (0)
188
189                 #define IRQ_SAVE_DISABLE(x) \
190                 do { \
191                         asm volatile ( \
192                                 "mrs %0, cpsr\n\t" \
193                                 "orr r0, %0, #0xc0\n\t" \
194                                 "msr cpsr_c, r0" \
195                                 : "=r" (x) \
196                                 : /* no inputs */ \
197                                 : "r0" \
198                         ); \
199                 } while (0)
200
201                 #define IRQ_RESTORE(x) \
202                 do { \
203                         asm volatile ( \
204                                 "msr cpsr_c, %0" \
205                                 : /* no outputs */ \
206                                 : "r" (x) \
207                         ); \
208                 } while (0)
209
210                 #define IRQ_GETSTATE() \
211                 ({ \
212                         uint32_t sreg; \
213                         asm volatile ( \
214                                 "mrs %0, cpsr\n\t" \
215                                 : "=r" (sreg) \
216                                 : /* no inputs */ \
217                         ); \
218                         !((sreg & 0xc0) == 0xc0); \
219                 })
220
221         #endif /* !__IAR_SYSTEMS_ICC_ */
222
223 #elif CPU_PPC
224         #define NOP                 asm volatile ("nop" ::)
225
226         #define IRQ_DISABLE         FIXME
227         #define IRQ_ENABLE          FIXME
228         #define IRQ_SAVE_DISABLE(x) FIXME
229         #define IRQ_RESTORE(x)      FIXME
230         #define IRQ_GETSTATE()      FIXME
231
232         typedef uint32_t cpuflags_t; // FIXME
233         typedef uint32_t cpustack_t; // FIXME
234
235         /* Register counts include SREG too */
236         #define CPU_REG_BITS           (CPU_PPC32 ? 32 : 64)
237         #define CPU_REGS_CNT           FIXME
238         #define CPU_SAVED_REGS_CNT     FIXME
239         #define CPU_STACK_GROWS_UPWARD 0  //FIXME
240         #define CPU_SP_ON_EMPTY_SLOT   0  //FIXME
241         #define CPU_BYTE_ORDER         (__BIG_ENDIAN__ ? CPU_BIG_ENDIAN : CPU_LITTLE_ENDIAN)
242         #define CPU_HARVARD            0
243
244 #elif CPU_DSP56K
245
246         #define NOP                     asm(nop)
247         #define BREAKPOINT              asm(debug)
248         #define IRQ_DISABLE             do { asm(bfset #0x0200,SR); asm(nop); } while (0)
249         #define IRQ_ENABLE              do { asm(bfclr #0x0200,SR); asm(nop); } while (0)
250
251         #define IRQ_SAVE_DISABLE(x)  \
252                 do { (void)x; asm(move SR,x); asm(bfset #0x0200,SR); } while (0)
253         #define IRQ_RESTORE(x)  \
254                 do { (void)x; asm(move x,SR); } while (0)
255
256         static inline bool irq_running(void)
257         {
258                 extern void *user_sp;
259                 return !!user_sp;
260         }
261         #define IRQ_RUNNING() irq_running()
262
263         static inline bool irq_getstate(void)
264         {
265                 uint16_t x;
266                 asm(move SR,x);
267                 return !(x & 0x0200);
268         }
269         #define IRQ_GETSTATE() irq_getstate()
270
271         typedef uint16_t cpuflags_t;
272         typedef unsigned int cpustack_t;
273
274         #define CPU_REG_BITS            16
275         #define CPU_REGS_CNT            FIXME
276         #define CPU_SAVED_REGS_CNT      8
277         #define CPU_STACK_GROWS_UPWARD  1
278         #define CPU_SP_ON_EMPTY_SLOT    0
279         #define CPU_BYTE_ORDER          CPU_BIG_ENDIAN
280         #define CPU_HARVARD             1
281
282         /* Memory is word-addessed in the DSP56K */
283         #define CPU_BITS_PER_CHAR  16
284         #define SIZEOF_SHORT        1
285         #define SIZEOF_INT          1
286         #define SIZEOF_LONG         2
287         #define SIZEOF_PTR          1
288
289 #elif CPU_AVR
290
291         #define NOP           asm volatile ("nop" ::)
292         #define IRQ_DISABLE   asm volatile ("cli" ::)
293         #define IRQ_ENABLE    asm volatile ("sei" ::)
294
295         #define IRQ_SAVE_DISABLE(x) \
296         do { \
297                 __asm__ __volatile__( \
298                         "in %0,__SREG__\n\t" \
299                         "cli" \
300                         : "=r" (x) : /* no inputs */ : "cc" \
301                 ); \
302         } while (0)
303
304         #define IRQ_RESTORE(x) \
305         do { \
306                 __asm__ __volatile__( \
307                         "out __SREG__,%0" : /* no outputs */ : "r" (x) : "cc" \
308                 ); \
309         } while (0)
310
311         #define IRQ_GETSTATE() \
312         ({ \
313                 uint8_t sreg; \
314                 __asm__ __volatile__( \
315                         "in %0,__SREG__\n\t" \
316                         : "=r" (sreg)  /* no inputs & no clobbers */ \
317                 ); \
318                 (bool)(sreg & 0x80); \
319         })
320
321         typedef uint8_t cpuflags_t;
322         typedef uint8_t cpustack_t;
323
324         /* Register counts include SREG too */
325         #define CPU_REG_BITS            8
326         #define CPU_REGS_CNT           33
327         #define CPU_SAVED_REGS_CNT     19
328         #define CPU_STACK_GROWS_UPWARD  0
329         #define CPU_SP_ON_EMPTY_SLOT    1
330         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
331         #define CPU_HARVARD             1
332
333         /**
334          * Initialization value for registers in stack frame.
335          * The register index is not directly corrispondent to CPU
336          * register numbers. Index 0 is the SREG register: the initial
337          * value is all 0 but the interrupt bit (bit 7).
338          */
339         #define CPU_REG_INIT_VALUE(reg) (reg == 0 ? 0x80 : 0)
340
341 #else
342         #error No CPU_... defined.
343 #endif
344
345 /**
346  * Execute \a CODE atomically with respect to interrupts.
347  *
348  * \see IRQ_SAVE_DISABLE IRQ_RESTORE
349  */
350 #define ATOMIC(CODE) \
351         do { \
352                 cpuflags_t __flags; \
353                 IRQ_SAVE_DISABLE(__flags); \
354                 CODE; \
355                 IRQ_RESTORE(__flags); \
356         } while (0)
357
358
359 /// Default for macro not defined in the right arch section
360 #ifndef CPU_REG_INIT_VALUE
361         #define CPU_REG_INIT_VALUE(reg)     0
362 #endif
363
364
365 #ifndef CPU_STACK_GROWS_UPWARD
366         #error CPU_STACK_GROWS_UPWARD should have been defined to either 0 or 1
367 #endif
368
369 #ifndef CPU_SP_ON_EMPTY_SLOT
370         #error CPU_SP_ON_EMPTY_SLOT should have been defined to either 0 or 1
371 #endif
372
373 /*
374  * Support stack handling peculiarities of a few CPUs.
375  *
376  * Most processors let their stack grow downward and
377  * keep SP pointing at the last pushed value.
378  */
379 #if !CPU_STACK_GROWS_UPWARD
380         #if !CPU_SP_ON_EMPTY_SLOT
381                 /* Most microprocessors (x86, m68k...) */
382                 #define CPU_PUSH_WORD(sp, data) \
383                         do { *--(sp) = (data); } while (0)
384                 #define CPU_POP_WORD(sp) \
385                         (*(sp)++)
386         #else
387                 /* AVR insanity */
388                 #define CPU_PUSH_WORD(sp, data) \
389                         do { *(sp)-- = (data); } while (0)
390                 #define CPU_POP_WORD(sp) \
391                         (*++(sp))
392         #endif
393
394 #else /* CPU_STACK_GROWS_UPWARD */
395
396         #if !CPU_SP_ON_EMPTY_SLOT
397                 /* DSP56K and other weirdos */
398                 #define CPU_PUSH_WORD(sp, data) \
399                         do { *++(sp) = (cpustack_t)(data); } while (0)
400                 #define CPU_POP_WORD(sp) \
401                         (*(sp)--)
402         #else
403                 #error I bet you cannot find a CPU like this
404         #endif
405 #endif
406
407
408 #if CPU_DSP56K
409         /*
410          * DSP56k pushes both PC and SR to the stack in the JSR instruction, but
411          * RTS discards SR while returning (it does not restore it). So we push
412          * 0 to fake the same context.
413          */
414         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
415                 do { \
416                         CPU_PUSH_WORD((sp), (func)); \
417                         CPU_PUSH_WORD((sp), 0x100); \
418                 } while (0);
419
420 #elif CPU_AVR
421         /*
422          * In AVR, the addresses are pushed into the stack as little-endian, while
423          * memory accesses are big-endian (actually, it's a 8-bit CPU, so there is
424          * no natural endianess).
425          */
426         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
427                 do { \
428                         uint16_t funcaddr = (uint16_t)(func); \
429                         CPU_PUSH_WORD((sp), funcaddr); \
430                         CPU_PUSH_WORD((sp), funcaddr>>8); \
431                 } while (0)
432
433 #else
434         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
435                 CPU_PUSH_WORD((sp), (cpustack_t)(func))
436 #endif
437
438
439 /**
440  * \name Default type sizes.
441  *
442  * These defaults are reasonable for most 16/32bit machines.
443  * Some of these macros may be overridden by CPU-specific code above.
444  *
445  * ANSI C requires that the following equations be true:
446  * \code
447  *   sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
448  *   sizeof(float) <= sizeof(double)
449  *   CPU_BITS_PER_CHAR  >= 8
450  *   CPU_BITS_PER_SHORT >= 8
451  *   CPU_BITS_PER_INT   >= 16
452  *   CPU_BITS_PER_LONG  >= 32
453  * \endcode
454  * \{
455  */
456 #ifndef SIZEOF_CHAR
457 #define SIZEOF_CHAR  1
458 #endif
459
460 #ifndef SIZEOF_SHORT
461 #define SIZEOF_SHORT  2
462 #endif
463
464 #ifndef SIZEOF_INT
465 #if CPU_REG_BITS < 32
466         #define SIZEOF_INT  2
467 #else
468         #define SIZEOF_INT  4
469 #endif
470 #endif /* !SIZEOF_INT */
471
472 #ifndef SIZEOF_LONG
473 #if CPU_REG_BITS > 32
474         #define SIZEOF_LONG  8
475 #else
476         #define SIZEOF_LONG  4
477 #endif
478 #endif
479
480 #ifndef SIZEOF_PTR
481 #if CPU_REG_BITS < 32
482         #define SIZEOF_PTR   2
483 #elif CPU_REG_BITS == 32
484         #define SIZEOF_PTR   4
485 #else /* CPU_REG_BITS > 32 */
486         #define SIZEOF_PTR   8
487 #endif
488 #endif
489
490 #ifndef CPU_BITS_PER_CHAR
491 #define CPU_BITS_PER_CHAR   (SIZEOF_CHAR * 8)
492 #endif
493
494 #ifndef CPU_BITS_PER_SHORT
495 #define CPU_BITS_PER_SHORT  (SIZEOF_SHORT * CPU_BITS_PER_CHAR)
496 #endif
497
498 #ifndef CPU_BITS_PER_INT
499 #define CPU_BITS_PER_INT    (SIZEOF_INT * CPU_BITS_PER_CHAR)
500 #endif
501
502 #ifndef CPU_BITS_PER_LONG
503 #define CPU_BITS_PER_LONG   (SIZEOF_LONG * CPU_BITS_PER_CHAR)
504 #endif
505
506 #ifndef CPU_BITS_PER_PTR
507 #define CPU_BITS_PER_PTR    (SIZEOF_PTR * CPU_BITS_PER_CHAR)
508 #endif
509
510 #ifndef BREAKPOINT
511 #define BREAKPOINT /* nop */
512 #endif
513
514 /*\}*/
515
516 /* Sanity checks for the above definitions */
517 STATIC_ASSERT(sizeof(char) == SIZEOF_CHAR);
518 STATIC_ASSERT(sizeof(short) == SIZEOF_SHORT);
519 STATIC_ASSERT(sizeof(long) == SIZEOF_LONG);
520 STATIC_ASSERT(sizeof(int) == SIZEOF_INT);
521 STATIC_ASSERT(sizeof(void *) == SIZEOF_PTR);
522 STATIC_ASSERT(sizeof(int8_t) * CPU_BITS_PER_CHAR == 8);
523 STATIC_ASSERT(sizeof(uint8_t) * CPU_BITS_PER_CHAR == 8);
524 STATIC_ASSERT(sizeof(int16_t) * CPU_BITS_PER_CHAR == 16);
525 STATIC_ASSERT(sizeof(uint16_t) * CPU_BITS_PER_CHAR == 16);
526 STATIC_ASSERT(sizeof(int32_t) * CPU_BITS_PER_CHAR == 32);
527 STATIC_ASSERT(sizeof(uint32_t) * CPU_BITS_PER_CHAR == 32);
528 #ifdef __HAS_INT64_T__
529 STATIC_ASSERT(sizeof(int64_t) * CPU_BITS_PER_CHAR == 64);
530 STATIC_ASSERT(sizeof(uint64_t) * CPU_BITS_PER_CHAR == 64);
531 #endif
532
533 /**
534  * \def CPU_IDLE
535  *
536  * \brief Invoked by the scheduler to stop the CPU when idle.
537  *
538  * This hook can be redefined to put the CPU in low-power mode, or to
539  * profile system load with an external strobe, or to save CPU cycles
540  * in hosted environments such as emulators.
541  */
542 #ifndef CPU_IDLE
543         #if defined(ARCH_EMUL) && (ARCH & ARCH_EMUL)
544                 /* This emulator hook should yield the CPU to the host.  */
545                 EXTERN_C_BEGIN
546                 void emul_idle(void);
547                 EXTERN_C_END
548                 #define CPU_IDLE emul_idle()
549         #else /* !ARCH_EMUL */
550                 #define CPU_IDLE do { /* nothing */ } while (0)
551         #endif /* !ARCH_EMUL */
552 #endif /* !CPU_IDLE */
553
554 #endif /* CPU_CPU_H */