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