misc.h Source File

Back to the index.

misc.h
Go to the documentation of this file.
1 #ifndef MISC_H
2 #define MISC_H
3 
4 /*
5  * Copyright (C) 2003-2018 Anders Gavare. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *
31  * Misc. definitions for GXemul.
32  */
33 
34 
35 #include <sys/types.h>
36 #include <inttypes.h>
37 
38 /*
39  * ../../config.h contains #defines set by the configure script. Some of these
40  * might reduce speed of the emulator, so don't enable them unless you
41  * need them.
42  */
43 
44 #include "../../config.h"
45 
46 #define COPYRIGHT_MSG "Copyright (C) 2003-2018 Anders Gavare"
47 
48 // The recommended way to add a specific message to the startup banner or
49 // about box is to use the SECONDARY_MSG. This should end with a newline
50 // character, unless it is completely empty.
51 //
52 // Example: "Modified by XYZ to include support for machine type UVW.\n"
53 //
54 #define SECONDARY_MSG ""
55 
56 
57 #include <string>
58 using std::string;
59 typedef char stringchar;
60 
61 #include <map>
62 using std::map;
63 using std::pair;
64 
65 #include <list>
66 using std::list;
67 
68 #include <vector>
69 using std::vector;
70 
71 #include <sstream>
72 using std::stringstream;
73 
74 #include <ostream>
75 using std::ostream;
76 
77 #include <iostream>
78 using std::cout;
79 using std::cerr;
80 
81 #ifndef NDEBUG
82 #include "thirdparty/debug_new.h"
83 #endif
84 
85 #include "refcount_ptr.h"
86 
87 
88 #ifdef NO_C99_PRINTF_DEFINES
89 /*
90  * This is a SUPER-UGLY HACK which happens to work on some machines.
91  * The correct solution is to upgrade your compiler to C99.
92  */
93 #ifdef NO_C99_64BIT_LONGLONG
94 #define PRIi8 "i"
95 #define PRIi16 "i"
96 #define PRIi32 "i"
97 #define PRIi64 "lli"
98 #define PRIx8 "x"
99 #define PRIx16 "x"
100 #define PRIx32 "x"
101 #define PRIx64 "llx"
102 #else
103 #define PRIi8 "i"
104 #define PRIi16 "i"
105 #define PRIi32 "i"
106 #define PRIi64 "li"
107 #define PRIx8 "x"
108 #define PRIx16 "x"
109 #define PRIx32 "x"
110 #define PRIx64 "lx"
111 #endif
112 #endif
113 
114 
115 #ifdef NO_MAP_ANON
116 #ifdef mmap
117 #undef mmap
118 #endif
119 #include <fcntl.h>
120 #include <stdlib.h>
121 #include <sys/mman.h>
122 static void *no_map_anon_mmap(void *addr, size_t len, int prot, int flags,
123  int nonsense_fd, off_t offset)
124 {
125  void *p;
126  int fd = open("/dev/zero", O_RDWR);
127  if (fd < 0) {
128  fprintf(stderr, "Could not open /dev/zero\n");
129  exit(1);
130  }
131 
132  printf("addr=%p len=%lli prot=0x%x flags=0x%x nonsense_fd=%i "
133  "offset=%16lli\n", addr, (long long) len, prot, flags,
134  nonsense_fd, (long long) offset);
135 
136  p = mmap(addr, len, prot, flags, fd, offset);
137 
138  printf("p = %p\n", p);
139 
140  /* TODO: Close the descriptor? */
141  return p;
142 }
143 #define mmap no_map_anon_mmap
144 #endif
145 
146 
147 /* tmp dir to use if the TMPDIR environment variable isn't set: */
148 #define DEFAULT_TMP_DIR "/tmp"
149 
150 
151 struct cpu;
152 struct emul;
153 struct machine;
154 struct memory;
155 
157 {
160 };
161 
162 #define NO_BYTE_ORDER_OVERRIDE -1
163 #define EMUL_UNDEFINED_ENDIAN 0
164 #define EMUL_LITTLE_ENDIAN 1
165 #define EMUL_BIG_ENDIAN 2
166 
167 #define SWAP32(x) ((((x) & 0xff000000) >> 24) | (((x)&0xff) << 24) | \
168  (((x) & 0xff0000) >> 8) | (((x) & 0xff00) << 8))
169 #define SWAP16(x) ((((x) & 0xff00) >> 8) | (((x)&0xff) << 8))
170 
171 #ifdef HOST_LITTLE_ENDIAN
172 #define LE16_TO_HOST(x) (x)
173 #define BE16_TO_HOST(x) (SWAP16(x))
174 #else
175 #define LE16_TO_HOST(x) (SWAP16(x))
176 #define BE16_TO_HOST(x) (x)
177 #endif
178 
179 #ifdef HOST_LITTLE_ENDIAN
180 #define LE32_TO_HOST(x) (x)
181 #define BE32_TO_HOST(x) (SWAP32(x))
182 #else
183 #define LE32_TO_HOST(x) (SWAP32(x))
184 #define BE32_TO_HOST(x) (x)
185 #endif
186 
187 #ifdef HOST_LITTLE_ENDIAN
188 #define LE64_TO_HOST(x) (x)
189 #define BE64_TO_HOST(x) ( (((x) >> 56) & 0xff) + \
190  ((((x) >> 48) & 0xff) << 8) + \
191  ((((x) >> 40) & 0xff) << 16) + \
192  ((((x) >> 32) & 0xff) << 24) + \
193  ((((x) >> 24) & 0xff) << 32) + \
194  ((((x) >> 16) & 0xff) << 40) + \
195  ((((x) >> 8) & 0xff) << 48) + \
196  (((x) & 0xff) << 56) )
197 #else
198 #define BE64_TO_HOST(x) (x)
199 #define LE64_TO_HOST(x) ( (((x) >> 56) & 0xff) + \
200  ((((x) >> 48) & 0xff) << 8) + \
201  ((((x) >> 40) & 0xff) << 16) + \
202  ((((x) >> 32) & 0xff) << 24) + \
203  ((((x) >> 24) & 0xff) << 32) + \
204  ((((x) >> 16) & 0xff) << 40) + \
205  ((((x) >> 8) & 0xff) << 48) + \
206  (((x) & 0xff) << 56) )
207 #endif
208 
209 
210 /* Debug stuff: */
211 #define DEBUG_BUFSIZE 1024
212 #define DEBUG_INDENTATION 4
213 
214 
215 #ifdef HAVE___FUNCTION__
216 
217 #define FAILURE(error_msg) { \
218  char where_msg[400]; \
219  snprintf(where_msg, sizeof(where_msg), \
220  "%s, line %i, function %s().\n", \
221  __FILE__, __LINE__, __FUNCTION__); \
222  fprintf(stderr, "\n%s, in %s\n", error_msg, where_msg); \
223  exit(1); \
224  }
225 
226 #else
227 
228 #define FAILURE(error_msg) { \
229  char where_msg[400]; \
230  snprintf(where_msg, sizeof(where_msg), \
231  "%s, line %i\n", __FILE__, __LINE__); \
232  fprintf(stderr, "\n%s, in %s.\n", error_msg, where_msg);\
233  exit(1); \
234  }
235 
236 #endif /* !HAVE___FUNCTION__ */
237 
238 
239 #define CHECK_ALLOCATION(ptr) { \
240  if ((ptr) == NULL) \
241  FAILURE("Out of memory"); \
242  }
243 
244 
245 /* bootblock.c: */
246 int load_bootblock(struct machine *m, struct cpu *cpu,
247  int *n_loadp, char ***load_namesp);
248 
249 
250 /* bootblock_apple.c: */
251 int apple_load_bootblock(struct machine *m, struct cpu *cpu,
252  int disk_id, int disk_type, int *n_loadp, char ***load_namesp);
253 
254 
255 /* bootblock_iso9660.c: */
256 int iso_load_bootblock(struct machine *m, struct cpu *cpu,
257  int disk_id, int disk_type, int iso_type, unsigned char *buf,
258  int *n_loadp, char ***load_namesp);
259 
260 
261 /* dec_prom.c: */
262 int decstation_prom_emul(struct cpu *cpu);
263 
264 
265 /* dreamcast.c: */
266 void dreamcast_machine_setup(struct machine *);
267 void dreamcast_emul(struct cpu *cpu);
268 
269 
270 /* dreamcast_scramble.c: */
271 void dreamcast_descramble(char *from, char *to);
272 
273 
274 /* file.c: */
275 int file_n_executables_loaded(void);
276 void file_load(struct machine *machine, struct memory *mem,
277  char *filename, uint64_t *entrypointp,
278  int arch, uint64_t *gpp, int *byte_order, uint64_t *tocp);
279 
280 
281 /* luna88kprom.c: */
282 void luna88kprom_init(struct machine *machine);
283 int luna88kprom_emul(struct cpu *cpu);
284 
285 
286 /* main.c: */
287 void debug_indentation(int diff);
288 void debug(const char *fmt, ...);
289 void fatal(const char *fmt, ...);
290 
291 
292 /* misc.c: */
293 unsigned long long mystrtoull(const char *s, char **endp, int base);
294 int mymkstemp(char *templ);
295 #ifdef USE_STRLCPY_REPLACEMENTS
296 size_t mystrlcpy(char *dst, const char *src, size_t size);
297 size_t mystrlcat(char *dst, const char *src, size_t size);
298 #endif
299 void print_separator_line(void);
300 
301 
302 /* mvmeprom.c: */
303 void mvmeprom_init(struct machine *machine);
304 int mvmeprom_emul(struct cpu *cpu);
305 
306 
307 /* ps2_bios.c: */
308 int playstation2_sifbios_emul(struct cpu *cpu);
309 
310 
311 /* sh_ipl_g.c: */
312 void sh_ipl_g_emul_init(struct machine *machine);
313 int sh_ipl_g_emul(struct cpu *);
314 
315 
316 /* yamon.c: */
317 void yamon_machine_setup(struct machine *machine, uint64_t env);
318 int yamon_emul(struct cpu *);
319 
320 
321 #endif /* MISC_H */
int load_bootblock(struct machine *m, struct cpu *cpu, int *n_loadp, char ***load_namesp)
Definition: bootblock.cc:65
void fatal(const char *fmt,...)
Definition: main.cc:152
void sh_ipl_g_emul_init(struct machine *machine)
Definition: sh_ipl_g.cc:51
void print_separator_line(void)
Definition: misc.cc:164
int yamon_emul(struct cpu *)
Definition: yamon.cc:139
char stringchar
Definition: misc.h:59
void debug(const char *fmt,...)
Definition: main.cc:133
Endianness
Definition: misc.h:156
int playstation2_sifbios_emul(struct cpu *cpu)
Definition: ps2_bios.cc:51
int file_n_executables_loaded(void)
Definition: file.cc:102
void dreamcast_machine_setup(struct machine *)
Definition: dreamcast.cc:200
unsigned long long mystrtoull(const char *s, char **endp, int base)
Definition: misc.cc:46
int sh_ipl_g_emul(struct cpu *)
Definition: sh_ipl_g.cc:98
int iso_load_bootblock(struct machine *m, struct cpu *cpu, int disk_id, int disk_type, int iso_type, unsigned char *buf, int *n_loadp, char ***load_namesp)
size_t mystrlcpy(char *dst, const char *src, size_t size)
void yamon_machine_setup(struct machine *machine, uint64_t env)
Definition: yamon.cc:52
Definition: emul.h:37
int mymkstemp(char *templ)
Definition: misc.cc:114
uint32_t addr
Definition: cpu.h:326
int decstation_prom_emul(struct cpu *cpu)
Definition: dec_prom.cc:229
void file_load(struct machine *machine, struct memory *mem, char *filename, uint64_t *entrypointp, int arch, uint64_t *gpp, int *byte_order, uint64_t *tocp)
Definition: file.cc:118
void debug_indentation(int diff)
Definition: main.cc:120
void dreamcast_emul(struct cpu *cpu)
Definition: dreamcast.cc:239
Definition: memory.h:75
int apple_load_bootblock(struct machine *m, struct cpu *cpu, int disk_id, int disk_type, int *n_loadp, char ***load_namesp)
int mvmeprom_emul(struct cpu *cpu)
Definition: mvmeprom.cc:99
size_t mystrlcat(char *dst, const char *src, size_t size)
void mvmeprom_init(struct machine *machine)
Definition: mvmeprom.cc:55
int luna88kprom_emul(struct cpu *cpu)
Definition: luna88kprom.cc:90
void luna88kprom_init(struct machine *machine)
Definition: luna88kprom.cc:50
void dreamcast_descramble(char *from, char *to)

Generated on Sun Sep 30 2018 16:05:18 for GXemul by doxygen 1.8.13