Further Cortex-M3 core related macros.
[bertos.git] / bertos / cpu / types.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 type definitions.
35  *
36  * \author Giovanni Bajo <rasky@develer.com>
37  * \author Bernie Innocenti <bernie@codewiz.org>
38  * \author Stefano Fedrigo <aleph@develer.com>
39  * \author Francesco Sacchi <batt@develer.com>
40  */
41 #ifndef CPU_TYPES_H
42 #define CPU_TYPES_H
43
44 #include "detect.h"
45 #include "attr.h"
46 #include <cfg/compiler.h> /* for uintXX_t */
47
48 #if CPU_I196
49
50         typedef uint16_t cpu_flags_t; // FIXME
51         typedef unsigned int cpu_stack_t;
52         typedef cpu_stack_t cpu_aligned_stack_t;
53         typedef unsigned int cpu_atomic_t;
54         #warning Verify following constant
55         #define SIZEOF_CPUSTACK_T 2
56         #define SIZEOF_CPUALIGNED_T SIZEOF_CPUSTACK_T
57
58 #elif CPU_X86
59
60         /* Get cpu_flags_t definition from the hosting environment. */
61         #include <cfg/os.h>
62         #if OS_EMBEDDED
63                 typedef uint32_t cpu_flags_t; // FIXME
64         #endif /* OS_EMBEDDED */
65
66         typedef uint32_t cpu_atomic_t;
67
68         #if CPU_X86_64
69                 typedef uint64_t cpu_stack_t;
70                 typedef cpu_stack_t cpu_aligned_stack_t;
71                 #define SIZEOF_CPUSTACK_T 8
72                 #define SIZEOF_CPUALIGNED_T SIZEOF_CPUSTACK_T
73         #else
74                 typedef uint32_t cpu_stack_t;
75                 typedef cpu_stack_t cpu_aligned_stack_t;
76                 #define SIZEOF_CPUSTACK_T 4
77                 #define SIZEOF_CPUALIGNED_T SIZEOF_CPUSTACK_T
78         #endif
79
80 #elif CPU_ARM || CPU_CM3
81
82         typedef uint32_t cpu_flags_t;
83         typedef uint32_t cpu_atomic_t;
84         typedef uint32_t cpu_stack_t;
85         #define SIZEOF_CPUSTACK_T 4
86
87         #if CPU_ARM
88                 typedef uint64_t cpu_aligned_stack_t;
89                 #define SIZEOF_CPUALIGNED_T 8
90         #elif CPU_CM3
91                 typedef uint32_t cpu_aligned_stack_t;
92                 #define SIZEOF_CPUALIGNED_T 4
93         #else
94                 #error CPU configuration error
95         #endif
96
97 #elif CPU_PPC
98
99         /* Get cpu_flags_t definition from the hosting environment. */
100         #include <cfg/os.h>
101         #if OS_EMBEDDED
102                 typedef uint32_t cpu_flags_t;
103         #endif
104
105         typedef uint32_t cpu_atomic_t;
106         typedef uint32_t cpu_stack_t;
107         typedef  cpu_stack_t cpu_aligned_stack_t;
108         #define SIZEOF_CPUSTACK_T 4
109         #define SIZEOF_CPUALIGNED_T SIZEOF_CPUSTACK_T
110
111 #elif CPU_DSP56K
112
113         typedef uint16_t cpu_flags_t;
114         typedef uint16_t cpu_atomic_t;
115         typedef unsigned int cpu_stack_t;
116         typedef cpu_stack_t cpu_aligned_stack_t;
117         #warning Verify following costant
118         #define SIZEOF_CPUSTACK_T 2
119         #define SIZEOF_CPUALIGNED_T SIZEOF_CPUSTACK_T
120
121 #elif CPU_AVR
122
123         typedef uint8_t cpu_flags_t;
124         typedef uint8_t cpu_atomic_t;
125         typedef uint8_t cpu_stack_t;
126         typedef cpu_stack_t cpu_aligned_stack_t;
127         #define SIZEOF_CPUSTACK_T 1
128         #define SIZEOF_CPUALIGNED_T SIZEOF_CPUSTACK_T
129
130 #else
131         #error No CPU_... defined.
132 #endif
133
134 /**
135  * \name Default type sizes.
136  *
137  * These defaults are reasonable for most 16/32bit machines.
138  * Some of these macros may be overridden by CPU-specific code above.
139  *
140  * ANSI C requires that the following equations be true:
141  * \code
142  *   sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
143  *   sizeof(float) <= sizeof(double)
144  *   CPU_BITS_PER_CHAR  >= 8
145  *   CPU_BITS_PER_SHORT >= 8
146  *   CPU_BITS_PER_INT   >= 16
147  *   CPU_BITS_PER_LONG  >= 32
148  * \endcode
149  * \{
150  */
151 #ifndef SIZEOF_CHAR
152 #define SIZEOF_CHAR  1
153 #endif
154
155 #ifndef SIZEOF_SHORT
156 #define SIZEOF_SHORT  2
157 #endif
158
159 #ifndef SIZEOF_INT
160 #if CPU_REG_BITS < 32
161         #define SIZEOF_INT  2
162 #else
163         #define SIZEOF_INT  4
164 #endif
165 #endif /* !SIZEOF_INT */
166
167 #ifndef SIZEOF_LONG
168 #if CPU_REG_BITS > 32
169         #define SIZEOF_LONG  8
170 #else
171         #define SIZEOF_LONG  4
172 #endif
173 #endif
174
175 #ifndef SIZEOF_PTR
176 #if CPU_REG_BITS < 32
177         #define SIZEOF_PTR   2
178 #elif CPU_REG_BITS == 32
179         #define SIZEOF_PTR   4
180 #else /* CPU_REG_BITS > 32 */
181         #define SIZEOF_PTR   8
182 #endif
183 #endif
184
185 #ifndef SIZEOF_SIZE_T
186 #if CPU_REG_BITS < 32
187         #define SIZEOF_SIZE_T   2
188 #elif CPU_REG_BITS == 32
189         #define SIZEOF_SIZE_T   4
190 #else /* CPU_REG_BITS > 32 */
191         #define SIZEOF_SIZE_T   8
192 #endif
193 #endif
194
195 #ifndef CPU_BITS_PER_CHAR
196 #define CPU_BITS_PER_CHAR   (SIZEOF_CHAR * 8)
197 #endif
198
199 #ifndef CPU_BITS_PER_SHORT
200 #define CPU_BITS_PER_SHORT  (SIZEOF_SHORT * CPU_BITS_PER_CHAR)
201 #endif
202
203 #ifndef CPU_BITS_PER_INT
204 #define CPU_BITS_PER_INT    (SIZEOF_INT * CPU_BITS_PER_CHAR)
205 #endif
206
207 #ifndef CPU_BITS_PER_LONG
208 #define CPU_BITS_PER_LONG   (SIZEOF_LONG * CPU_BITS_PER_CHAR)
209 #endif
210
211 #ifndef CPU_BITS_PER_PTR
212 #define CPU_BITS_PER_PTR    (SIZEOF_PTR * CPU_BITS_PER_CHAR)
213 #endif
214
215
216 /*\}*/
217
218 #ifndef INT_MAX
219         #define INT_MAX ((int)((unsigned int)~0 >> 1))
220         #define INT_MIN (-INT_MAX - 1)
221 #endif
222
223 /* Sanity checks for the above definitions */
224 STATIC_ASSERT(sizeof(char) == SIZEOF_CHAR);
225 STATIC_ASSERT(sizeof(short) == SIZEOF_SHORT);
226 STATIC_ASSERT(sizeof(long) == SIZEOF_LONG);
227 STATIC_ASSERT(sizeof(int) == SIZEOF_INT);
228 STATIC_ASSERT(sizeof(void *) == SIZEOF_PTR);
229 STATIC_ASSERT(sizeof(int8_t) * CPU_BITS_PER_CHAR == 8);
230 STATIC_ASSERT(sizeof(uint8_t) * CPU_BITS_PER_CHAR == 8);
231 STATIC_ASSERT(sizeof(int16_t) * CPU_BITS_PER_CHAR == 16);
232 STATIC_ASSERT(sizeof(uint16_t) * CPU_BITS_PER_CHAR == 16);
233 STATIC_ASSERT(sizeof(int32_t) * CPU_BITS_PER_CHAR == 32);
234 STATIC_ASSERT(sizeof(uint32_t) * CPU_BITS_PER_CHAR == 32);
235 #ifdef __HAS_INT64_T__
236 STATIC_ASSERT(sizeof(int64_t) * CPU_BITS_PER_CHAR == 64);
237 STATIC_ASSERT(sizeof(uint64_t) * CPU_BITS_PER_CHAR == 64);
238 #endif
239 STATIC_ASSERT(sizeof(cpu_stack_t) == SIZEOF_CPUSTACK_T);
240 STATIC_ASSERT(sizeof(cpu_aligned_stack_t) == SIZEOF_CPUALIGNED_T);
241 STATIC_ASSERT(sizeof(size_t) == SIZEOF_SIZE_T);
242
243 #endif /* CPU_TYPES_H */