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