Change sreg type to cpuflags_t.
[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(drv/PP_CAT3(module, _, CPU_ID).h)
59
60 /** Macro to include cpu-specific versions of implementation files. */
61 #define CPU_CSOURCE(module)         PP_STRINGIZE(drv/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     9
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                 #define NOP         asm volatile ("mov r0,r0" ::)
164
165                 #define IRQ_DISABLE \
166                 do { \
167                         asm volatile ( \
168                                 "mrs r0, cpsr\n\t" \
169                                 "orr r0, r0, #0xc0\n\t" \
170                                 "msr cpsr_c, r0" \
171                                 ::: "r0" \
172                         ); \
173                 } while (0)
174
175                 #define IRQ_ENABLE \
176                 do { \
177                         asm volatile ( \
178                                 "mrs r0, cpsr\n\t" \
179                                 "bic r0, r0, #0xc0\n\t" \
180                                 "msr cpsr_c, r0" \
181                                 ::: "r0" \
182                         ); \
183                 } while (0)
184
185                 #define IRQ_SAVE_DISABLE(x) \
186                 do { \
187                         asm volatile ( \
188                                 "mrs %0, cpsr\n\t" \
189                                 "orr r0, %0, #0xc0\n\t" \
190                                 "msr cpsr_c, r0" \
191                                 : "=r" (x) \
192                                 : /* no inputs */ \
193                                 : "r0" \
194                         ); \
195                 } while (0)
196
197                 #define IRQ_RESTORE(x) \
198                 do { \
199                         asm volatile ( \
200                                 "msr cpsr_c, %0" \
201                                 : /* no outputs */ \
202                                 : "r" (x) \
203                         ); \
204                 } while (0)
205
206                 #define CPU_READ_FLAGS() \
207                 ({ \
208                         cpuflags_t sreg; \
209                         asm volatile ( \
210                                 "mrs %0, cpsr\n\t" \
211                                 : "=r" (sreg) \
212                                 : /* no inputs */ \
213                         ); \
214                         sreg; \
215                 })
216
217                 #define IRQ_GETSTATE() (!((CPU_READ_FLAGS() & 0xc0) == 0xc0))
218
219                 /**
220                  * Initialization value for registers in stack frame.
221                  * The register index is not directly corrispondent to CPU
222                  * register numbers, but is related to how are pushed to
223                  * stack (\see asm_switch_context).
224                  * Index (CPU_SAVED_REGS_CNT - 1) is the CPSR register,
225                  * the initial value is set to:
226                  * - All flags (N, Z, C, V) set to 0.
227                  * - IRQ and FIQ enabled.
228                  * - ARM state.
229                  * - CPU in Supervisor Mode (SVC).
230                  */
231                 #define CPU_REG_INIT_VALUE(reg) (reg == (CPU_SAVED_REGS_CNT - 1) ? 0x13 : 0)
232
233         #endif /* !__IAR_SYSTEMS_ICC_ */
234
235 #elif CPU_PPC
236         #define NOP                 asm volatile ("nop" ::)
237
238         #define IRQ_DISABLE         FIXME
239         #define IRQ_ENABLE          FIXME
240         #define IRQ_SAVE_DISABLE(x) FIXME
241         #define IRQ_RESTORE(x)      FIXME
242         #define IRQ_GETSTATE()      FIXME
243
244         typedef uint32_t cpuflags_t; // FIXME
245         typedef uint32_t cpustack_t; // FIXME
246
247         /* Register counts include SREG too */
248         #define CPU_REG_BITS           (CPU_PPC32 ? 32 : 64)
249         #define CPU_REGS_CNT           FIXME
250         #define CPU_SAVED_REGS_CNT     FIXME
251         #define CPU_STACK_GROWS_UPWARD 0  //FIXME
252         #define CPU_SP_ON_EMPTY_SLOT   0  //FIXME
253         #define CPU_BYTE_ORDER         (__BIG_ENDIAN__ ? CPU_BIG_ENDIAN : CPU_LITTLE_ENDIAN)
254         #define CPU_HARVARD            0
255
256 #elif CPU_DSP56K
257
258         #define NOP                     asm(nop)
259         #define BREAKPOINT              asm(debug)
260         #define IRQ_DISABLE             do { asm(bfset #0x0200,SR); asm(nop); } while (0)
261         #define IRQ_ENABLE              do { asm(bfclr #0x0200,SR); asm(nop); } while (0)
262
263         #define IRQ_SAVE_DISABLE(x)  \
264                 do { (void)x; asm(move SR,x); asm(bfset #0x0200,SR); } while (0)
265         #define IRQ_RESTORE(x)  \
266                 do { (void)x; asm(move x,SR); } while (0)
267
268         static inline bool irq_running(void)
269         {
270                 extern void *user_sp;
271                 return !!user_sp;
272         }
273         #define IRQ_RUNNING() irq_running()
274
275         static inline bool irq_getstate(void)
276         {
277                 uint16_t x;
278                 asm(move SR,x);
279                 return !(x & 0x0200);
280         }
281         #define IRQ_GETSTATE() irq_getstate()
282
283         typedef uint16_t cpuflags_t;
284         typedef unsigned int cpustack_t;
285
286         #define CPU_REG_BITS            16
287         #define CPU_REGS_CNT            FIXME
288         #define CPU_SAVED_REGS_CNT      8
289         #define CPU_STACK_GROWS_UPWARD  1
290         #define CPU_SP_ON_EMPTY_SLOT    0
291         #define CPU_BYTE_ORDER          CPU_BIG_ENDIAN
292         #define CPU_HARVARD             1
293
294         /* Memory is word-addessed in the DSP56K */
295         #define CPU_BITS_PER_CHAR  16
296         #define SIZEOF_SHORT        1
297         #define SIZEOF_INT          1
298         #define SIZEOF_LONG         2
299         #define SIZEOF_PTR          1
300
301 #elif CPU_AVR
302
303         #define NOP           asm volatile ("nop" ::)
304         #define IRQ_DISABLE   asm volatile ("cli" ::)
305         #define IRQ_ENABLE    asm volatile ("sei" ::)
306
307         #define IRQ_SAVE_DISABLE(x) \
308         do { \
309                 __asm__ __volatile__( \
310                         "in %0,__SREG__\n\t" \
311                         "cli" \
312                         : "=r" (x) : /* no inputs */ : "cc" \
313                 ); \
314         } while (0)
315
316         #define IRQ_RESTORE(x) \
317         do { \
318                 __asm__ __volatile__( \
319                         "out __SREG__,%0" : /* no outputs */ : "r" (x) : "cc" \
320                 ); \
321         } while (0)
322
323         #define IRQ_GETSTATE() \
324         ({ \
325                 uint8_t sreg; \
326                 __asm__ __volatile__( \
327                         "in %0,__SREG__\n\t" \
328                         : "=r" (sreg)  /* no inputs & no clobbers */ \
329                 ); \
330                 (bool)(sreg & 0x80); \
331         })
332
333         typedef uint8_t cpuflags_t;
334         typedef uint8_t cpustack_t;
335
336         /* Register counts include SREG too */
337         #define CPU_REG_BITS            8
338         #define CPU_REGS_CNT           33
339         #define CPU_SAVED_REGS_CNT     19
340         #define CPU_STACK_GROWS_UPWARD  0
341         #define CPU_SP_ON_EMPTY_SLOT    1
342         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
343         #define CPU_HARVARD             1
344
345         /**
346          * Initialization value for registers in stack frame.
347          * The register index is not directly corrispondent to CPU
348          * register numbers. Index 0 is the SREG register: the initial
349          * value is all 0 but the interrupt bit (bit 7).
350          */
351         #define CPU_REG_INIT_VALUE(reg) (reg == 0 ? 0x80 : 0)
352
353 #else
354         #error No CPU_... defined.
355 #endif
356
357 /**
358  * Execute \a CODE atomically with respect to interrupts.
359  *
360  * \see IRQ_SAVE_DISABLE IRQ_RESTORE
361  */
362 #define ATOMIC(CODE) \
363         do { \
364                 cpuflags_t __flags; \
365                 IRQ_SAVE_DISABLE(__flags); \
366                 CODE; \
367                 IRQ_RESTORE(__flags); \
368         } while (0)
369
370
371 /// Default for macro not defined in the right arch section
372 #ifndef CPU_REG_INIT_VALUE
373         #define CPU_REG_INIT_VALUE(reg)     0
374 #endif
375
376
377 #ifndef CPU_STACK_GROWS_UPWARD
378         #error CPU_STACK_GROWS_UPWARD should have been defined to either 0 or 1
379 #endif
380
381 #ifndef CPU_SP_ON_EMPTY_SLOT
382         #error CPU_SP_ON_EMPTY_SLOT should have been defined to either 0 or 1
383 #endif
384
385 /*
386  * Support stack handling peculiarities of a few CPUs.
387  *
388  * Most processors let their stack grow downward and
389  * keep SP pointing at the last pushed value.
390  */
391 #if !CPU_STACK_GROWS_UPWARD
392         #if !CPU_SP_ON_EMPTY_SLOT
393                 /* Most microprocessors (x86, m68k...) */
394                 #define CPU_PUSH_WORD(sp, data) \
395                         do { *--(sp) = (data); } while (0)
396                 #define CPU_POP_WORD(sp) \
397                         (*(sp)++)
398         #else
399                 /* AVR insanity */
400                 #define CPU_PUSH_WORD(sp, data) \
401                         do { *(sp)-- = (data); } while (0)
402                 #define CPU_POP_WORD(sp) \
403                         (*++(sp))
404         #endif
405
406 #else /* CPU_STACK_GROWS_UPWARD */
407
408         #if !CPU_SP_ON_EMPTY_SLOT
409                 /* DSP56K and other weirdos */
410                 #define CPU_PUSH_WORD(sp, data) \
411                         do { *++(sp) = (cpustack_t)(data); } while (0)
412                 #define CPU_POP_WORD(sp) \
413                         (*(sp)--)
414         #else
415                 #error I bet you cannot find a CPU like this
416         #endif
417 #endif
418
419
420 #if CPU_DSP56K
421         /*
422          * DSP56k pushes both PC and SR to the stack in the JSR instruction, but
423          * RTS discards SR while returning (it does not restore it). So we push
424          * 0 to fake the same context.
425          */
426         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
427                 do { \
428                         CPU_PUSH_WORD((sp), (func)); \
429                         CPU_PUSH_WORD((sp), 0x100); \
430                 } while (0);
431
432 #elif CPU_AVR
433         /*
434          * In AVR, the addresses are pushed into the stack as little-endian, while
435          * memory accesses are big-endian (actually, it's a 8-bit CPU, so there is
436          * no natural endianess).
437          */
438         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
439                 do { \
440                         uint16_t funcaddr = (uint16_t)(func); \
441                         CPU_PUSH_WORD((sp), funcaddr); \
442                         CPU_PUSH_WORD((sp), funcaddr>>8); \
443                 } while (0)
444
445 #else
446         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
447                 CPU_PUSH_WORD((sp), (cpustack_t)(func))
448 #endif
449
450
451 /**
452  * \name Default type sizes.
453  *
454  * These defaults are reasonable for most 16/32bit machines.
455  * Some of these macros may be overridden by CPU-specific code above.
456  *
457  * ANSI C requires that the following equations be true:
458  * \code
459  *   sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
460  *   sizeof(float) <= sizeof(double)
461  *   CPU_BITS_PER_CHAR  >= 8
462  *   CPU_BITS_PER_SHORT >= 8
463  *   CPU_BITS_PER_INT   >= 16
464  *   CPU_BITS_PER_LONG  >= 32
465  * \endcode
466  * \{
467  */
468 #ifndef SIZEOF_CHAR
469 #define SIZEOF_CHAR  1
470 #endif
471
472 #ifndef SIZEOF_SHORT
473 #define SIZEOF_SHORT  2
474 #endif
475
476 #ifndef SIZEOF_INT
477 #if CPU_REG_BITS < 32
478         #define SIZEOF_INT  2
479 #else
480         #define SIZEOF_INT  4
481 #endif
482 #endif /* !SIZEOF_INT */
483
484 #ifndef SIZEOF_LONG
485 #if CPU_REG_BITS > 32
486         #define SIZEOF_LONG  8
487 #else
488         #define SIZEOF_LONG  4
489 #endif
490 #endif
491
492 #ifndef SIZEOF_PTR
493 #if CPU_REG_BITS < 32
494         #define SIZEOF_PTR   2
495 #elif CPU_REG_BITS == 32
496         #define SIZEOF_PTR   4
497 #else /* CPU_REG_BITS > 32 */
498         #define SIZEOF_PTR   8
499 #endif
500 #endif
501
502 #ifndef CPU_BITS_PER_CHAR
503 #define CPU_BITS_PER_CHAR   (SIZEOF_CHAR * 8)
504 #endif
505
506 #ifndef CPU_BITS_PER_SHORT
507 #define CPU_BITS_PER_SHORT  (SIZEOF_SHORT * CPU_BITS_PER_CHAR)
508 #endif
509
510 #ifndef CPU_BITS_PER_INT
511 #define CPU_BITS_PER_INT    (SIZEOF_INT * CPU_BITS_PER_CHAR)
512 #endif
513
514 #ifndef CPU_BITS_PER_LONG
515 #define CPU_BITS_PER_LONG   (SIZEOF_LONG * CPU_BITS_PER_CHAR)
516 #endif
517
518 #ifndef CPU_BITS_PER_PTR
519 #define CPU_BITS_PER_PTR    (SIZEOF_PTR * CPU_BITS_PER_CHAR)
520 #endif
521
522 #ifndef BREAKPOINT
523 #define BREAKPOINT /* nop */
524 #endif
525
526 /*\}*/
527
528 /* Sanity checks for the above definitions */
529 STATIC_ASSERT(sizeof(char) == SIZEOF_CHAR);
530 STATIC_ASSERT(sizeof(short) == SIZEOF_SHORT);
531 STATIC_ASSERT(sizeof(long) == SIZEOF_LONG);
532 STATIC_ASSERT(sizeof(int) == SIZEOF_INT);
533 STATIC_ASSERT(sizeof(void *) == SIZEOF_PTR);
534 STATIC_ASSERT(sizeof(int8_t) * CPU_BITS_PER_CHAR == 8);
535 STATIC_ASSERT(sizeof(uint8_t) * CPU_BITS_PER_CHAR == 8);
536 STATIC_ASSERT(sizeof(int16_t) * CPU_BITS_PER_CHAR == 16);
537 STATIC_ASSERT(sizeof(uint16_t) * CPU_BITS_PER_CHAR == 16);
538 STATIC_ASSERT(sizeof(int32_t) * CPU_BITS_PER_CHAR == 32);
539 STATIC_ASSERT(sizeof(uint32_t) * CPU_BITS_PER_CHAR == 32);
540 #ifdef __HAS_INT64_T__
541 STATIC_ASSERT(sizeof(int64_t) * CPU_BITS_PER_CHAR == 64);
542 STATIC_ASSERT(sizeof(uint64_t) * CPU_BITS_PER_CHAR == 64);
543 #endif
544
545 /**
546  * \def CPU_IDLE
547  *
548  * \brief Invoked by the scheduler to stop the CPU when idle.
549  *
550  * This hook can be redefined to put the CPU in low-power mode, or to
551  * profile system load with an external strobe, or to save CPU cycles
552  * in hosted environments such as emulators.
553  */
554 #ifndef CPU_IDLE
555         #if defined(ARCH_EMUL) && (ARCH & ARCH_EMUL)
556                 /* This emulator hook should yield the CPU to the host.  */
557                 EXTERN_C_BEGIN
558                 void emul_idle(void);
559                 EXTERN_C_END
560                 #define CPU_IDLE emul_idle()
561         #else /* !ARCH_EMUL */
562                 #define CPU_IDLE do { /* nothing */ } while (0)
563         #endif /* !ARCH_EMUL */
564 #endif /* !CPU_IDLE */
565
566 #endif /* CPU_CPU_H */