335876130765ffd001011786086c1da0aef8101b
[bertos.git] / cpu.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 2004 Giovanni Bajo
6  * This file is part of DevLib - See devlib/README for information.
7  * -->
8  *
9  * \brief CPU-specific definitions
10  *
11  * \version $Id$
12  *
13  * \author Giovanni Bajo <rasky@develer.com>
14  */
15
16 /*
17  * $Log$
18  * Revision 1.3  2004/07/18 21:49:51  bernie
19  * Fixes for GCC 3.5.
20  *
21  * Revision 1.2  2004/06/03 11:27:09  bernie
22  * Add dual-license information.
23  *
24  * Revision 1.1  2004/05/23 17:48:35  bernie
25  * Add top-level files.
26  *
27  */
28 #ifndef CPU_H
29 #define CPU_H
30
31 #include "compiler.h"
32
33 //! Initialization value for registers in stack frame
34 #define CPU_REG_INIT_VALUE(reg)     0
35
36 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)  /* 80C196 */
37
38         #define DISABLE_INTS            disable_interrupt()
39         #define ENABLE_INTS             enable_interrupt()
40         #define NOP                     nop_instruction()
41         #define SCHEDULER_IDLE          /* Hmmm... could we go in STOP mode? */
42
43         typedef uint16_t cpuflags_t; // FIXME
44         typedef unsigned int cpustack_t;
45
46         #define CPU_REGS_CNT            16
47         #define CPU_STACK_GROWS_UPWARD  0
48         #define CPU_SP_ON_EMPTY_SLOT    0
49
50 #elif defined(__i386__) || defined(_MSC_VER) /* x86 */
51
52         #define NOP                     asm volatile ("nop")
53         #define DISABLE_INTS            /* nothing */
54         #define ENABLE_INTS             /* nothing */
55         #define SCHEDULER_IDLE          SchedulerIdle()
56
57         typedef uint32_t cpuflags_t; // FIXME
58         typedef uint32_t cpustack_t;
59
60         #define CPU_REGS_CNT            7
61         #define CPU_STACK_GROWS_UPWARD  0
62         #define CPU_SP_ON_EMPTY_SLOT    0
63
64 #elif defined(__m56800E__) || defined(__m56800__) /* DSP56K */
65
66         #define NOP                     asm(nop)
67         #define DISABLE_INTS            do { asm(bfset #0x0200,SR); asm(nop); } while (0)
68         #define ENABLE_INTS             do { asm(bfclr #0x0200,SR); asm(nop); } while (0)
69         #define SCHEDULER_IDLE          /* nothing */
70
71         #define DISABLE_IRQSAVE(x)  \
72                 do { asm(move SR,x); asm(bfset #0x0200,SR); } while (0)
73         #define ENABLE_IRQRESTORE(x)  \
74                 do { asm(move x,SR); } while (0)
75
76         typedef uint16_t cpuflags_t;
77         typedef unsigned int cpustack_t;
78
79         #define CPU_REGS_CNT            FIXME
80         #define CPU_SAVED_REGS_CNT      28
81         #define CPU_STACK_GROWS_UPWARD  1
82         #define CPU_SP_ON_EMPTY_SLOT    0
83
84         #undef CPU_REG_INIT_VALUE
85         INLINE uint16_t CPU_REG_INIT_VALUE(int reg)
86         {
87                 if (reg == 14)
88                 {
89                         uint16_t omr_img;
90                         asm(move OMR, omr_img);
91                         return omr_img & (BV(3)/*EX*/ | BV(1)/*MB*/ | BV(0)/*MA*/);
92                 }
93                 else if (reg == 16)/*M01*/
94                         return 0xFFFF;
95                 return 0;
96         }
97
98 #elif defined (__AVR__)
99
100         #define NOP                     asm volatile ("nop" ::)
101         #define DISABLE_INTS            asm volatile ("cli" ::)
102         #define ENABLE_INTS             asm volatile ("sei" ::)
103         #define SCHEDULER_IDLE          /* nothing */
104
105         #define DISABLE_IRQSAVE(x) \
106         do { \
107                 __asm__ __volatile__( \
108                         "in %0,__SREG__\n\t" \
109                         "cli" \
110                         : "=r" (x) : /* no inputs */ : "cc" \
111                 ); \
112         } while (0)
113
114         #define ENABLE_IRQRESTORE(x) \
115         do { \
116                 __asm__ __volatile__( \
117                         "out __SREG__,%0" : /* no outputs */ : "r" (x) : "cc" \
118                 ); \
119         } while (0)
120
121         typedef uint8_t cpuflags_t;
122         typedef uint8_t cpustack_t;
123
124         #define CPU_REGS_CNT            32
125         #define CPU_SAVED_REGS_CNT      18
126         #define CPU_STACK_GROWS_UPWARD  0
127         #define CPU_SP_ON_EMPTY_SLOT    1
128
129 #else
130         #error Unknown CPU
131 #endif
132
133
134 #ifndef CPU_STACK_GROWS_UPWARD
135         #error CPU_STACK_GROWS_UPWARD should have been defined to either 0 or 1
136 #endif
137
138 #ifndef CPU_SP_ON_EMPTY_SLOT
139         #error CPU_SP_ON_EMPTY_SLOT should have been defined to either 0 or 1
140 #endif
141
142 /*
143  * Support stack handling peculiarities of a few CPUs.
144  *
145  * Most processors let their stack grow downward and
146  * keep SP pointing at the last pushed value.
147  */
148 #if !CPU_STACK_GROWS_UPWARD
149         #if !CPU_SP_ON_EMPTY_SLOT
150                 /* Most microprocessors (x86, m68k...) */
151                 #define CPU_PUSH_WORD(sp, data) \
152                         do { *--(sp) = (data); } while (0)
153                 #define CPU_POP_WORD(sp) \
154                         (*(sp)++)
155         #else
156                 /* AVR insanity */
157                 #define CPU_PUSH_WORD(sp, data) \
158                         do { *(sp)-- = (data); } while (0)
159                 #define CPU_POP_WORD(sp) \
160                         (*++(sp))
161         #endif
162
163 #else /* CPU_STACK_GROWS_UPWARD */
164
165         #if !CPU_SP_ON_EMPTY_SLOT
166                 /* DSP56K and other weirdos */
167                 #define CPU_PUSH_WORD(sp, data) \
168                         do { *++(sp) = (cpustack_t)(data); } while (0)
169                 #define CPU_POP_WORD(sp) \
170                         (*(sp)--)
171         #else
172                 #error I bet you cannot find a CPU like this
173         #endif
174 #endif
175
176
177 #if defined(__m56800E__) || defined(__m56800__)
178         /* DSP56k pushes both PC and SR to the stack in the JSR instruction, but
179          * RTS discards SR while returning (it does not restore it). So we push
180          * 0 to fake the same context.
181          */
182         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
183                 do { \
184                         CPU_PUSH_WORD((sp), (func)); \
185                         CPU_PUSH_WORD((sp), 0); \
186                 } while (0);
187
188 #elif defined (__AVR__)
189         /* In AVR, the addresses are pushed into the stack as little-endian, while
190          * memory accesses are big-endian (actually, it's a 8-bit CPU, so there is
191          * no natural endianess).
192          */
193         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
194                 do { \
195                         uint16_t funcaddr = (uint16_t)(func); \
196                         CPU_PUSH_WORD((sp), funcaddr); \
197                         CPU_PUSH_WORD((sp), funcaddr>>8); \
198                 } while (0)
199
200 #else
201         #define CPU_PUSH_CALL_CONTEXT(sp, func) \
202                 CPU_PUSH_WORD((sp), (func))
203 #endif
204
205 #endif /* CPU_H */