1
//------------------------------------------------------------------------
2
// SYSTEM : System specific code
3
//------------------------------------------------------------------------
5
// GL-Friendly Node Builder (C) 2000-2007 Andrew Apted
7
// Based on 'BSP 2.3' by Colin Reed, Lee Killough and others.
9
// This program is free software; you can redistribute it and/or
10
// modify it under the terms of the GNU General Public License
11
// as published by the Free Software Foundation; either version 2
12
// of the License, or (at your option) any later version.
14
// This program is distributed in the hope that it will be useful,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
// GNU General Public License for more details.
19
//------------------------------------------------------------------------
33
#define DEBUG_ENABLED 0
35
#define DEBUGGING_FILE "gb_debug.txt"
37
#define DEBUG_ENDIAN 0
39
static int cpu_big_endian = 0;
42
#define SYS_MSG_BUFLEN 4000
44
static char message_buf[SYS_MSG_BUFLEN];
47
static FILE *debug_fp = NULL;
54
void FatalError(const char *str, ...)
59
vsnprintf(message_buf, sizeof(message_buf), str, args);
62
(* cur_funcs->fatal_error)("\nError: *** %s ***\n\n", message_buf);
68
void InternalError(const char *str, ...)
73
vsnprintf(message_buf, sizeof(message_buf), str, args);
76
(* cur_funcs->fatal_error)("\nINTERNAL ERROR: *** %s ***\n\n", message_buf);
82
void PrintMsg(const char *str, ...)
87
vsnprintf(message_buf, sizeof(message_buf), str, args);
90
(* cur_funcs->print_msg)("%s", message_buf);
93
PrintDebug(">>> %s", message_buf);
100
void PrintVerbose(const char *str, ...)
105
vsnprintf(message_buf, sizeof(message_buf), str, args);
108
if (! cur_info->quiet)
109
(* cur_funcs->print_msg)("%s", message_buf);
112
PrintDebug(">>> %s", message_buf);
119
void PrintWarn(const char *str, ...)
124
vsnprintf(message_buf, sizeof(message_buf), str, args);
127
(* cur_funcs->print_msg)("Warning: %s", message_buf);
129
cur_comms->total_big_warn++;
132
PrintDebug("Warning: %s", message_buf);
139
void PrintMiniWarn(const char *str, ...)
144
vsnprintf(message_buf, sizeof(message_buf), str, args);
147
if (cur_info->mini_warnings)
148
(* cur_funcs->print_msg)("Warning: %s", message_buf);
150
cur_comms->total_small_warn++;
153
PrintDebug("MiniWarn: %s", message_buf);
160
void SetErrorMsg(const char *str, ...)
165
vsnprintf(message_buf, sizeof(message_buf), str, args);
168
GlbspFree(cur_comms->message);
170
cur_comms->message = GlbspStrDup(message_buf);
174
/* -------- debugging code ----------------------------- */
182
debug_fp = fopen(DEBUGGING_FILE, "w");
185
PrintWarn("Unable to open DEBUG FILE: %s\n", DEBUGGING_FILE);
187
PrintDebug("=== START OF DEBUG FILE ===\n");
199
PrintDebug("=== END OF DEBUG FILE ===\n");
210
void PrintDebug(const char *str, ...)
218
vfprintf(debug_fp, str, args);
229
/* -------- endian code ----------------------------- */
234
// Parts inspired by the Yadex endian.cc code.
236
void InitEndian(void)
245
/* sanity-check type sizes */
247
if (sizeof(uint8_g) != 1)
248
FatalError("Sanity check failed: sizeof(uint8_g) = %d",
249
(int)sizeof(uint8_g));
251
if (sizeof(uint16_g) != 2)
252
FatalError("Sanity check failed: sizeof(uint16_g) = %d",
253
(int)sizeof(uint16_g));
255
if (sizeof(uint32_g) != 4)
256
FatalError("Sanity check failed: sizeof(uint32_g) = %d",
257
(int)sizeof(uint32_g));
259
/* check endianness */
261
memset((uint32_g *) u.mem, 0, sizeof(u.mem));
263
u.mem[0] = 0x70; u.mem[1] = 0x71;
264
u.mem[2] = 0x72; u.mem[3] = 0x73;
267
PrintDebug("Endianness magic value: 0x%08x\n", u.val);
270
if (u.val == 0x70717273)
272
else if (u.val == 0x73727170)
275
FatalError("Sanity check failed: weird endianness (0x%08x)", u.val);
278
PrintDebug("Endianness = %s\n", cpu_big_endian ? "BIG" : "LITTLE");
280
PrintDebug("Endianness check: 0x1234 --> 0x%04x\n",
281
(int) Endian_U16(0x1234));
283
PrintDebug("Endianness check: 0x11223344 --> 0x%08x\n",
284
Endian_U32(0x11223344));
291
uint16_g Endian_U16(uint16_g x)
294
return (x >> 8) | (x << 8);
302
uint32_g Endian_U32(uint32_g x)
305
return (x >> 24) | ((x >> 8) & 0xff00) |
306
((x << 8) & 0xff0000) | (x << 24);