Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/yabause/src/core.h
2 views
1
/* Copyright 2005 Guillaume Duhamel
2
Copyright 2005-2006 Theo Berkau
3
4
This file is part of Yabause.
5
6
Yabause 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
Yabause 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 Yabause; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
*/
20
21
#ifndef CORE_H
22
#define CORE_H
23
24
#include <stdio.h>
25
#include <string.h>
26
27
#ifndef ALIGNED
28
#ifdef _MSC_VER
29
#define ALIGNED(x) __declspec(align(x))
30
#else
31
#define ALIGNED(x) __attribute__((aligned(x)))
32
#endif
33
#endif
34
35
#ifndef STDCALL
36
#ifdef _MSC_VER
37
#define STDCALL __stdcall
38
#else
39
#define STDCALL
40
#endif
41
#endif
42
43
#ifndef FASTCALL
44
#ifdef __MINGW32__
45
#define FASTCALL __attribute__((fastcall))
46
#elif defined (__i386__)
47
#define FASTCALL __attribute__((regparm(3)))
48
#else
49
#define FASTCALL
50
#endif
51
#endif
52
53
/* When building multiple arches on OS X you must use the compiler-
54
provided endian flags instead of the one provided by autoconf */
55
#if defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__)
56
#undef WORDS_BIGENDIAN
57
#ifdef __BIG_ENDIAN__
58
#define WORDS_BIGENDIAN
59
#endif
60
#endif
61
62
63
#ifndef INLINE
64
#ifdef _MSC_VER
65
#define INLINE _inline
66
#else
67
#define INLINE inline
68
#endif
69
#endif
70
71
#ifdef GEKKO
72
/* Wii have both stdint.h and "yabause" definitions of fixed
73
size types */
74
#include <gccore.h>
75
typedef unsigned long pointer;
76
77
#else /* ! GEKKO */
78
79
#ifdef HAVE_STDINT_H
80
81
#include <stdint.h>
82
typedef uint8_t u8;
83
typedef int8_t s8;
84
typedef uint16_t u16;
85
typedef int16_t s16;
86
typedef uint32_t u32;
87
typedef int32_t s32;
88
typedef uint64_t u64;
89
typedef int64_t s64;
90
typedef uintptr_t pointer;
91
92
#else // !HAVE_STDINT_H
93
94
typedef unsigned char u8;
95
typedef unsigned short u16;
96
97
typedef signed char s8;
98
typedef signed short s16;
99
100
#if defined(__LP64__)
101
// Generic 64-bit
102
typedef unsigned int u32;
103
typedef unsigned long u64;
104
typedef unsigned long pointer;
105
106
typedef signed int s32;
107
typedef signed long s64;
108
109
#elif defined(_MSC_VER)
110
typedef unsigned long u32;
111
typedef unsigned __int64 u64;
112
typedef unsigned long long u64;
113
#ifdef _WIN64
114
typedef __int64 pointer;
115
#else
116
typedef unsigned long pointer;
117
#endif
118
119
typedef signed long s32;
120
typedef __int64 s64;
121
typedef signed long long s64;
122
123
#else
124
// 32-bit Linux GCC/MINGW/etc.
125
typedef unsigned long u32;
126
typedef unsigned long long u64;
127
typedef unsigned long pointer;
128
129
typedef signed long s32;
130
typedef signed long long s64;
131
#endif
132
133
#endif // !HAVE_STDINT_H
134
135
#endif // !GEKKO
136
137
typedef struct {
138
unsigned int size;
139
unsigned int done;
140
} IOCheck_struct;
141
142
static INLINE void ywrite(IOCheck_struct * check, void * ptr, size_t size, size_t nmemb, FILE * stream) {
143
check->done += (unsigned int)fwrite(ptr, size, nmemb, stream);
144
check->size += (unsigned int)nmemb;
145
}
146
147
static INLINE void yread(IOCheck_struct * check, void * ptr, size_t size, size_t nmemb, FILE * stream) {
148
check->done += (unsigned int)fread(ptr, size, nmemb, stream);
149
check->size += (unsigned int)nmemb;
150
}
151
152
static INLINE int StateWriteHeader(FILE *fp, const char *name, int version) {
153
IOCheck_struct check;
154
fprintf(fp, "%s", name);
155
check.done = 0;
156
check.size = 0;
157
ywrite(&check, (void *)&version, sizeof(version), 1, fp);
158
ywrite(&check, (void *)&version, sizeof(version), 1, fp); // place holder for size
159
return (check.done == check.size) ? ftell(fp) : -1;
160
}
161
162
static INLINE int StateFinishHeader(FILE *fp, int offset) {
163
/*
164
IOCheck_struct check;
165
int size = 0;
166
size = ftell(fp) - offset;
167
fseek(fp, offset - 4, SEEK_SET);
168
check.done = 0;
169
check.size = 0;
170
ywrite(&check, (void *)&size, sizeof(size), 1, fp); // write true size
171
fseek(fp, 0, SEEK_END);
172
return (check.done == check.size) ? (size + 12) : -1;
173
*/
174
return 0;
175
}
176
177
static INLINE int StateCheckRetrieveHeader(FILE *fp, const char *name, int *version, int *size) {
178
char id[4];
179
size_t ret;
180
181
if ((ret = fread((void *)id, 1, 4, fp)) != 4)
182
return -1;
183
184
if (strncmp(name, id, 4) != 0)
185
return -2;
186
187
if ((ret = fread((void *)version, 4, 1, fp)) != 1)
188
return -1;
189
190
if (fread((void *)size, 4, 1, fp) != 1)
191
return -1;
192
193
return 0;
194
}
195
196
//////////////////////////////////////////////////////////////////////////////
197
198
// Terrible, but I'm not sure how to do the equivalent in inline
199
#ifdef HAVE_C99_VARIADIC_MACROS
200
#define AddString(s, ...) \
201
{ \
202
sprintf(s, __VA_ARGS__); \
203
s += strlen(s); \
204
}
205
#else
206
#define AddString(s, r...) \
207
{ \
208
sprintf(s, ## r); \
209
s += strlen(s); \
210
}
211
#endif
212
213
//////////////////////////////////////////////////////////////////////////////
214
215
#ifdef HAVE_LIBMINI18N
216
#include "mini18n.h"
217
#else
218
#ifndef _
219
#define _(a) (a)
220
#endif
221
#endif
222
223
//////////////////////////////////////////////////////////////////////////////
224
225
/* Minimum/maximum values */
226
227
#undef MIN
228
#undef MAX
229
#define MIN(a,b) ((a) < (b) ? (a) : (b))
230
#define MAX(a,b) ((a) > (b) ? (a) : (b))
231
232
//////////////////////////////////////////////////////////////////////////////
233
234
/*
235
* BSWAP16(x) swaps two bytes in a 16-bit value (AABB -> BBAA) or adjacent
236
* bytes in a 32-bit value (AABBCCDD -> BBAADDCC).
237
*
238
* BSWAP32(x) reverses four bytes in a 32-bit value (AABBCCDD -> DDCCBBAA).
239
*
240
* WSWAP32(x) swaps two 16-bit words in a 32-bit value (AABBCCDD -> CCDDAABB).
241
*
242
* Any of these can be left undefined if there is no platform-specific
243
* optimization for them; the defaults below will then be used instead.
244
*/
245
246
#ifdef PSP
247
# define BSWAP16(x) ((typeof(x)) __builtin_allegrex_wsbh((x)))
248
# define BSWAP32(x) ((typeof(x)) __builtin_allegrex_wsbw((x)))
249
# define WSWAP32(x) ((typeof(x)) __builtin_allegrex_rotr((x), 16))
250
#endif
251
252
/* Defaults: */
253
254
#ifndef BSWAP16
255
# define BSWAP16(x) (((u32)(x)>>8 & 0x00FF00FF) | ((u32)(x) & 0x00FF00FF) << 8)
256
#endif
257
#ifndef BSWAP32
258
# define BSWAP32(x) ((u32)(x)>>24 | ((u32)(x)>>8 & 0xFF00) | ((u32)(x) & 0xFF00)<<8 | (u32)(x)<<24)
259
#endif
260
#ifndef WSWAP32
261
# define WSWAP32(x) ((u32)(x)>>16 | (u32)(x)<<16)
262
#endif
263
264
//////////////////////////////////////////////////////////////////////////////
265
266
#ifdef __GNUC__
267
268
#define UNUSED __attribute ((unused))
269
270
#ifdef DEBUG
271
#define USED_IF_DEBUG
272
#else
273
#define USED_IF_DEBUG __attribute ((unused))
274
#endif
275
276
#ifdef SMPC_DEBUG
277
#define USED_IF_SMPC_DEBUG
278
#else
279
#define USED_IF_SMPC_DEBUG __attribute ((unused))
280
#endif
281
282
/* LIKELY(x) indicates that x is likely to be true (nonzero);
283
* UNLIKELY(x) indicates that x is likely to be false (zero).
284
* Use like: "if (UNLIKELY(a < b)) {...}" */
285
#define LIKELY(x) (__builtin_expect(!!(x), 1))
286
#define UNLIKELY(x) (__builtin_expect(!!(x), 0))
287
288
#else
289
290
#define UNUSED
291
#define USED_IF_DEBUG
292
#define USED_IF_SMPC_DEBUG
293
#define LIKELY(x) (x)
294
#define UNLIKELY(x) (x)
295
296
#endif
297
298
#endif
299
300