Use stdbool for readability

This commit is contained in:
Jeffrey Pfau 2014-07-16 00:46:02 -07:00
parent fce2fb9252
commit d8654f3b88
21 changed files with 113 additions and 108 deletions

View File

@ -5,6 +5,7 @@
#include <limits.h> #include <limits.h>
#include <math.h> #include <math.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>

View File

@ -395,7 +395,7 @@ static void _DVFree(struct DebugVector* dv) {
} }
} }
static int _parse(struct CLIDebugger* debugger, const char* line, size_t count) { static bool _parse(struct CLIDebugger* debugger, const char* line, size_t count) {
const char* firstSpace = strchr(line, ' '); const char* firstSpace = strchr(line, ' ');
size_t cmdLength; size_t cmdLength;
struct DebugVector* dv = 0; struct DebugVector* dv = 0;
@ -405,7 +405,7 @@ static int _parse(struct CLIDebugger* debugger, const char* line, size_t count)
if (dv && dv->type == DV_ERROR_TYPE) { if (dv && dv->type == DV_ERROR_TYPE) {
printf("Parse error\n"); printf("Parse error\n");
_DVFree(dv); _DVFree(dv);
return 0; return false;
} }
} else { } else {
cmdLength = count; cmdLength = count;
@ -420,12 +420,12 @@ static int _parse(struct CLIDebugger* debugger, const char* line, size_t count)
if (strncasecmp(name, line, cmdLength) == 0) { if (strncasecmp(name, line, cmdLength) == 0) {
_debuggerCommands[i].command(debugger, dv); _debuggerCommands[i].command(debugger, dv);
_DVFree(dv); _DVFree(dv);
return 1; return true;
} }
} }
_DVFree(dv); _DVFree(dv);
printf("Command not found\n"); printf("Command not found\n");
return 0; return false;
} }
static char* _prompt(EditLine* el) { static char* _prompt(EditLine* el) {

View File

@ -500,7 +500,7 @@ void GDBStubUpdate(struct GDBStub* stub) {
goto connectionLost; goto connectionLost;
} }
} }
while (1) { while (true) {
ssize_t messageLen = SocketRecv(stub->connection, stub->line, GDB_STUB_MAX_LINE - 1); ssize_t messageLen = SocketRecv(stub->connection, stub->line, GDB_STUB_MAX_LINE - 1);
if (messageLen == 0) { if (messageLen == 0) {
goto connectionLost; goto connectionLost;

View File

@ -4,7 +4,7 @@
#include <string.h> #include <string.h>
static int _checkWatchpoints(struct DebugBreakpoint* watchpoints, uint32_t address, int width); static bool _checkWatchpoints(struct DebugBreakpoint* watchpoints, uint32_t address, int width);
#define FIND_DEBUGGER(DEBUGGER, CPU) \ #define FIND_DEBUGGER(DEBUGGER, CPU) \
{ \ { \
@ -46,14 +46,14 @@ CREATE_WATCHPOINT_SHIM(store8, 1, void, (struct ARMCore* cpu, uint32_t address,
CREATE_SHIM(waitMultiple, int, (struct ARMCore* cpu, uint32_t startAddress, int count), startAddress, count) CREATE_SHIM(waitMultiple, int, (struct ARMCore* cpu, uint32_t startAddress, int count), startAddress, count)
CREATE_SHIM(setActiveRegion, void, (struct ARMCore* cpu, uint32_t address), address) CREATE_SHIM(setActiveRegion, void, (struct ARMCore* cpu, uint32_t address), address)
static int _checkWatchpoints(struct DebugBreakpoint* watchpoints, uint32_t address, int width) { static bool _checkWatchpoints(struct DebugBreakpoint* watchpoints, uint32_t address, int width) {
width -= 1; width -= 1;
for (; watchpoints; watchpoints = watchpoints->next) { for (; watchpoints; watchpoints = watchpoints->next) {
if (!((watchpoints->address ^ address) & ~width)) { if (!((watchpoints->address ^ address) & ~width)) {
return 1; return true;
} }
} }
return 0; return false;
} }
void ARMDebuggerInstallMemoryShim(struct ARMDebugger* debugger) { void ARMDebuggerInstallMemoryShim(struct ARMDebugger* debugger) {

View File

@ -11,7 +11,7 @@ const unsigned GBA_AUDIO_FIFO_SIZE = 8 * sizeof(int32_t);
static int32_t _updateSquareChannel(struct GBAAudioSquareControl* envelope, int duty); static int32_t _updateSquareChannel(struct GBAAudioSquareControl* envelope, int duty);
static void _updateEnvelope(struct GBAAudioEnvelope* envelope); static void _updateEnvelope(struct GBAAudioEnvelope* envelope);
static int _updateSweep(struct GBAAudioChannel1* ch); static bool _updateSweep(struct GBAAudioChannel1* ch);
static int32_t _updateChannel1(struct GBAAudioChannel1* ch); static int32_t _updateChannel1(struct GBAAudioChannel1* ch);
static int32_t _updateChannel2(struct GBAAudioChannel2* ch); static int32_t _updateChannel2(struct GBAAudioChannel2* ch);
static int32_t _updateChannel3(struct GBAAudioChannel3* ch); static int32_t _updateChannel3(struct GBAAudioChannel3* ch);
@ -497,7 +497,7 @@ static void _updateEnvelope(struct GBAAudioEnvelope* envelope) {
} }
} }
static int _updateSweep(struct GBAAudioChannel1* ch) { static bool _updateSweep(struct GBAAudioChannel1* ch) {
if (ch->sweep.direction) { if (ch->sweep.direction) {
int frequency = ch->control.frequency; int frequency = ch->control.frequency;
frequency -= frequency >> ch->sweep.shift; frequency -= frequency >> ch->sweep.shift;
@ -510,11 +510,11 @@ static int _updateSweep(struct GBAAudioChannel1* ch) {
if (frequency < 2048) { if (frequency < 2048) {
ch->control.frequency = frequency; ch->control.frequency = frequency;
} else { } else {
return 0; return false;
} }
} }
ch->nextSweep += ch->sweep.time * SWEEP_CYCLES; ch->nextSweep += ch->sweep.time * SWEEP_CYCLES;
return 1; return true;
} }
static int32_t _updateChannel1(struct GBAAudioChannel1* ch) { static int32_t _updateChannel1(struct GBAAudioChannel1* ch) {

View File

@ -73,28 +73,28 @@ static int _getStateFd(struct GBA* gba, int slot) {
return fd; return fd;
} }
int GBASaveState(struct GBA* gba, int slot) { bool GBASaveState(struct GBA* gba, int slot) {
int fd = _getStateFd(gba, slot); int fd = _getStateFd(gba, slot);
if (fd < 0) { if (fd < 0) {
return 0; return false;
} }
struct GBASerializedState* state = GBAMapState(fd); struct GBASerializedState* state = GBAMapState(fd);
GBASerialize(gba, state); GBASerialize(gba, state);
GBADeallocateState(state); GBADeallocateState(state);
close(fd); close(fd);
return 1; return true;
} }
int GBALoadState(struct GBA* gba, int slot) { bool GBALoadState(struct GBA* gba, int slot) {
int fd = _getStateFd(gba, slot); int fd = _getStateFd(gba, slot);
if (fd < 0) { if (fd < 0) {
return 0; return false;
} }
struct GBASerializedState* state = GBAMapState(fd); struct GBASerializedState* state = GBAMapState(fd);
GBADeserialize(gba, state); GBADeserialize(gba, state);
GBADeallocateState(state); GBADeallocateState(state);
close(fd); close(fd);
return 1; return true;
} }
struct GBASerializedState* GBAMapState(int fd) { struct GBASerializedState* GBAMapState(int fd) {

View File

@ -228,8 +228,8 @@ struct GBASerializedState {
void GBASerialize(struct GBA* gba, struct GBASerializedState* state); void GBASerialize(struct GBA* gba, struct GBASerializedState* state);
void GBADeserialize(struct GBA* gba, struct GBASerializedState* state); void GBADeserialize(struct GBA* gba, struct GBASerializedState* state);
int GBASaveState(struct GBA* gba, int slot); bool GBASaveState(struct GBA* gba, int slot);
int GBALoadState(struct GBA* gba, int slot); bool GBALoadState(struct GBA* gba, int slot);
struct GBASerializedState* GBAMapState(int fd); struct GBASerializedState* GBAMapState(int fd);
struct GBASerializedState* GBAAllocateState(void); struct GBASerializedState* GBAAllocateState(void);

View File

@ -200,7 +200,7 @@ void GBAMapOptionsToContext(struct StartupOptions* opts, struct GBAThread* threa
threadContext->rewindBufferInterval = opts->rewindBufferInterval; threadContext->rewindBufferInterval = opts->rewindBufferInterval;
} }
int GBAThreadStart(struct GBAThread* threadContext) { bool GBAThreadStart(struct GBAThread* threadContext) {
// TODO: error check // TODO: error check
threadContext->activeKeys = 0; threadContext->activeKeys = 0;
threadContext->state = THREAD_INITIALIZED; threadContext->state = THREAD_INITIALIZED;
@ -239,11 +239,11 @@ int GBAThreadStart(struct GBAThread* threadContext) {
} }
MutexUnlock(&threadContext->stateMutex); MutexUnlock(&threadContext->stateMutex);
return 0; return true;
} }
int GBAThreadHasStarted(struct GBAThread* threadContext) { bool GBAThreadHasStarted(struct GBAThread* threadContext) {
int hasStarted; bool hasStarted;
MutexLock(&threadContext->stateMutex); MutexLock(&threadContext->stateMutex);
hasStarted = threadContext->state > THREAD_INITIALIZED; hasStarted = threadContext->state > THREAD_INITIALIZED;
MutexUnlock(&threadContext->stateMutex); MutexUnlock(&threadContext->stateMutex);
@ -357,8 +357,8 @@ void GBAThreadUnpause(struct GBAThread* threadContext) {
MutexUnlock(&threadContext->sync.videoFrameMutex); MutexUnlock(&threadContext->sync.videoFrameMutex);
} }
int GBAThreadIsPaused(struct GBAThread* threadContext) { bool GBAThreadIsPaused(struct GBAThread* threadContext) {
int isPaused; bool isPaused;
MutexLock(&threadContext->stateMutex); MutexLock(&threadContext->stateMutex);
_waitOnInterrupt(threadContext); _waitOnInterrupt(threadContext);
isPaused = threadContext->state == THREAD_PAUSED; isPaused = threadContext->state == THREAD_PAUSED;
@ -367,7 +367,7 @@ int GBAThreadIsPaused(struct GBAThread* threadContext) {
} }
void GBAThreadTogglePause(struct GBAThread* threadContext) { void GBAThreadTogglePause(struct GBAThread* threadContext) {
int frameOn = 1; bool frameOn = true;
MutexLock(&threadContext->stateMutex); MutexLock(&threadContext->stateMutex);
_waitOnInterrupt(threadContext); _waitOnInterrupt(threadContext);
if (threadContext->state == THREAD_PAUSED) { if (threadContext->state == THREAD_PAUSED) {
@ -378,7 +378,7 @@ void GBAThreadTogglePause(struct GBAThread* threadContext) {
threadContext->debugger->state = DEBUGGER_EXITING; threadContext->debugger->state = DEBUGGER_EXITING;
} }
threadContext->state = THREAD_PAUSED; threadContext->state = THREAD_PAUSED;
frameOn = 0; frameOn = false;
} }
MutexUnlock(&threadContext->stateMutex); MutexUnlock(&threadContext->stateMutex);
MutexLock(&threadContext->sync.videoFrameMutex); MutexLock(&threadContext->sync.videoFrameMutex);
@ -430,20 +430,20 @@ void GBASyncPostFrame(struct GBASync* sync) {
} }
} }
int GBASyncWaitFrameStart(struct GBASync* sync, int frameskip) { bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip) {
if (!sync) { if (!sync) {
return 1; return true;
} }
MutexLock(&sync->videoFrameMutex); MutexLock(&sync->videoFrameMutex);
ConditionWake(&sync->videoFrameRequiredCond); ConditionWake(&sync->videoFrameRequiredCond);
if (!sync->videoFrameOn) { if (!sync->videoFrameOn) {
return 0; return false;
} }
ConditionWait(&sync->videoFrameAvailableCond, &sync->videoFrameMutex); ConditionWait(&sync->videoFrameAvailableCond, &sync->videoFrameMutex);
sync->videoFramePending = 0; sync->videoFramePending = 0;
sync->videoFrameSkip = frameskip; sync->videoFrameSkip = frameskip;
return 1; return true;
} }
void GBASyncWaitFrameEnd(struct GBASync* sync) { void GBASyncWaitFrameEnd(struct GBASync* sync) {
@ -454,7 +454,7 @@ void GBASyncWaitFrameEnd(struct GBASync* sync) {
MutexUnlock(&sync->videoFrameMutex); MutexUnlock(&sync->videoFrameMutex);
} }
int GBASyncDrawingFrame(struct GBASync* sync) { bool GBASyncDrawingFrame(struct GBASync* sync) {
return sync->videoFrameSkip <= 0; return sync->videoFrameSkip <= 0;
} }

View File

@ -27,7 +27,7 @@ struct GBASync {
int videoFramePending; int videoFramePending;
int videoFrameWait; int videoFrameWait;
int videoFrameSkip; int videoFrameSkip;
int videoFrameOn; bool videoFrameOn;
Mutex videoFrameMutex; Mutex videoFrameMutex;
Condition videoFrameAvailableCond; Condition videoFrameAvailableCond;
Condition videoFrameRequiredCond; Condition videoFrameRequiredCond;
@ -80,8 +80,8 @@ struct GBAThread {
void GBAMapOptionsToContext(struct StartupOptions*, struct GBAThread*); void GBAMapOptionsToContext(struct StartupOptions*, struct GBAThread*);
int GBAThreadStart(struct GBAThread* threadContext); bool GBAThreadStart(struct GBAThread* threadContext);
int GBAThreadHasStarted(struct GBAThread* threadContext); bool GBAThreadHasStarted(struct GBAThread* threadContext);
void GBAThreadEnd(struct GBAThread* threadContext); void GBAThreadEnd(struct GBAThread* threadContext);
void GBAThreadReset(struct GBAThread* threadContext); void GBAThreadReset(struct GBAThread* threadContext);
void GBAThreadJoin(struct GBAThread* threadContext); void GBAThreadJoin(struct GBAThread* threadContext);
@ -91,14 +91,14 @@ void GBAThreadContinue(struct GBAThread* threadContext);
void GBAThreadPause(struct GBAThread* threadContext); void GBAThreadPause(struct GBAThread* threadContext);
void GBAThreadUnpause(struct GBAThread* threadContext); void GBAThreadUnpause(struct GBAThread* threadContext);
int GBAThreadIsPaused(struct GBAThread* threadContext); bool GBAThreadIsPaused(struct GBAThread* threadContext);
void GBAThreadTogglePause(struct GBAThread* threadContext); void GBAThreadTogglePause(struct GBAThread* threadContext);
struct GBAThread* GBAThreadGetContext(void); struct GBAThread* GBAThreadGetContext(void);
void GBASyncPostFrame(struct GBASync* sync); void GBASyncPostFrame(struct GBASync* sync);
int GBASyncWaitFrameStart(struct GBASync* sync, int frameskip); bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip);
void GBASyncWaitFrameEnd(struct GBASync* sync); void GBASyncWaitFrameEnd(struct GBASync* sync);
int GBASyncDrawingFrame(struct GBASync* sync); bool GBASyncDrawingFrame(struct GBASync* sync);
void GBASyncProduceAudio(struct GBASync* sync, int wait); void GBASyncProduceAudio(struct GBASync* sync, int wait);
void GBASyncLockAudio(struct GBASync* sync); void GBASyncLockAudio(struct GBASync* sync);

View File

@ -36,9 +36,9 @@ static const struct option _options[] = {
{ 0, 0, 0, 0 } { 0, 0, 0, 0 }
}; };
int _parseGraphicsArg(struct SubParser* parser, int option, const char* arg); bool _parseGraphicsArg(struct SubParser* parser, int option, const char* arg);
int parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, struct SubParser* subparser) { bool parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, struct SubParser* subparser) {
memset(opts, 0, sizeof(*opts)); memset(opts, 0, sizeof(*opts));
opts->fd = -1; opts->fd = -1;
opts->biosFd = -1; opts->biosFd = -1;
@ -66,7 +66,7 @@ int parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, s
#ifdef USE_CLI_DEBUGGER #ifdef USE_CLI_DEBUGGER
case 'd': case 'd':
if (opts->debuggerType != DEBUGGER_NONE) { if (opts->debuggerType != DEBUGGER_NONE) {
return 0; return false;
} }
opts->debuggerType = DEBUGGER_CLI; opts->debuggerType = DEBUGGER_CLI;
break; break;
@ -74,7 +74,7 @@ int parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, s
#ifdef USE_GDB_STUB #ifdef USE_GDB_STUB
case 'g': case 'g':
if (opts->debuggerType != DEBUGGER_NONE) { if (opts->debuggerType != DEBUGGER_NONE) {
return 0; return false;
} }
opts->debuggerType = DEBUGGER_GDB; opts->debuggerType = DEBUGGER_GDB;
break; break;
@ -91,7 +91,7 @@ int parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, s
default: default:
if (subparser) { if (subparser) {
if (!subparser->parse(subparser, ch, optarg)) { if (!subparser->parse(subparser, ch, optarg)) {
return 0; return false;
} }
} }
break; break;
@ -104,10 +104,10 @@ int parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, s
} else if (argc == 0) { } else if (argc == 0) {
opts->fname = _defaultFilename; opts->fname = _defaultFilename;
} else { } else {
return 0; return false;
} }
opts->fd = open(opts->fname, O_RDONLY); opts->fd = open(opts->fname, O_RDONLY);
return 1; return true;
} }
void initParserForGraphics(struct SubParser* parser, struct GraphicsOpts* opts) { void initParserForGraphics(struct SubParser* parser, struct GraphicsOpts* opts) {
@ -121,39 +121,39 @@ void initParserForGraphics(struct SubParser* parser, struct GraphicsOpts* opts)
opts->height = 160; opts->height = 160;
} }
int _parseGraphicsArg(struct SubParser* parser, int option, const char* arg) { bool _parseGraphicsArg(struct SubParser* parser, int option, const char* arg) {
UNUSED(arg); UNUSED(arg);
struct GraphicsOpts* graphicsOpts = parser->opts; struct GraphicsOpts* graphicsOpts = parser->opts;
switch (option) { switch (option) {
case 'f': case 'f':
graphicsOpts->fullscreen = 1; graphicsOpts->fullscreen = 1;
return 1; return true;
case '2': case '2':
if (graphicsOpts->multiplier != 1) { if (graphicsOpts->multiplier != 1) {
return 0; return false;
} }
graphicsOpts->multiplier = 2; graphicsOpts->multiplier = 2;
graphicsOpts->width *= graphicsOpts->multiplier; graphicsOpts->width *= graphicsOpts->multiplier;
graphicsOpts->height *= graphicsOpts->multiplier; graphicsOpts->height *= graphicsOpts->multiplier;
return 1; return true;
case '3': case '3':
if (graphicsOpts->multiplier != 1) { if (graphicsOpts->multiplier != 1) {
return 0; return false;
} }
graphicsOpts->multiplier = 3; graphicsOpts->multiplier = 3;
graphicsOpts->width *= graphicsOpts->multiplier; graphicsOpts->width *= graphicsOpts->multiplier;
graphicsOpts->height *= graphicsOpts->multiplier; graphicsOpts->height *= graphicsOpts->multiplier;
return 1; return true;
case '4': case '4':
if (graphicsOpts->multiplier != 1) { if (graphicsOpts->multiplier != 1) {
return 0; return false;
} }
graphicsOpts->multiplier = 4; graphicsOpts->multiplier = 4;
graphicsOpts->width *= graphicsOpts->multiplier; graphicsOpts->width *= graphicsOpts->multiplier;
graphicsOpts->height *= graphicsOpts->multiplier; graphicsOpts->height *= graphicsOpts->multiplier;
return 1; return true;
default: default:
return 0; return false;
} }
} }

View File

@ -30,7 +30,7 @@ struct StartupOptions {
struct SubParser { struct SubParser {
const char* usage; const char* usage;
int (*parse)(struct SubParser* parser, int option, const char* arg); bool (*parse)(struct SubParser* parser, int option, const char* arg);
const char* extraOptions; const char* extraOptions;
void* opts; void* opts;
}; };
@ -42,7 +42,7 @@ struct GraphicsOpts {
int height; int height;
}; };
int parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, struct SubParser* subparser); bool parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, struct SubParser* subparser);
void usage(const char* arg0, const char* extraOptions); void usage(const char* arg0, const char* extraOptions);
void initParserForGraphics(struct SubParser* parser, struct GraphicsOpts* opts); void initParserForGraphics(struct SubParser* parser, struct GraphicsOpts* opts);

View File

@ -8,10 +8,10 @@
static void _GBASDLAudioCallback(void* context, Uint8* data, int len); static void _GBASDLAudioCallback(void* context, Uint8* data, int len);
int GBASDLInitAudio(struct GBASDLAudio* context) { bool GBASDLInitAudio(struct GBASDLAudio* context) {
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
GBALog(0, GBA_LOG_ERROR, "Could not initialize SDL sound system"); GBALog(0, GBA_LOG_ERROR, "Could not initialize SDL sound system");
return 0; return false;
} }
context->desiredSpec.freq = 44100; context->desiredSpec.freq = 44100;
@ -24,10 +24,10 @@ int GBASDLInitAudio(struct GBASDLAudio* context) {
context->drift = 0.f; context->drift = 0.f;
if (SDL_OpenAudio(&context->desiredSpec, &context->obtainedSpec) < 0) { if (SDL_OpenAudio(&context->desiredSpec, &context->obtainedSpec) < 0) {
GBALog(0, GBA_LOG_ERROR, "Could not open SDL sound system"); GBALog(0, GBA_LOG_ERROR, "Could not open SDL sound system");
return 0; return false;
} }
SDL_PauseAudio(0); SDL_PauseAudio(0);
return 1; return true;
} }
void GBASDLDeinitAudio(struct GBASDLAudio* context) { void GBASDLDeinitAudio(struct GBASDLAudio* context) {

View File

@ -13,7 +13,7 @@ struct GBASDLAudio {
struct GBAAudio* audio; struct GBAAudio* audio;
}; };
int GBASDLInitAudio(struct GBASDLAudio* context); bool GBASDLInitAudio(struct GBASDLAudio* context);
void GBASDLDeinitAudio(struct GBASDLAudio* context); void GBASDLDeinitAudio(struct GBASDLAudio* context);
#endif #endif

View File

@ -11,16 +11,16 @@
#define GUI_MOD KMOD_CTRL #define GUI_MOD KMOD_CTRL
#endif #endif
int GBASDLInitEvents(struct GBASDLEvents* context) { bool GBASDLInitEvents(struct GBASDLEvents* context) {
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) { if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
return 0; return false;
} }
SDL_JoystickEventState(SDL_ENABLE); SDL_JoystickEventState(SDL_ENABLE);
context->joystick = SDL_JoystickOpen(0); context->joystick = SDL_JoystickOpen(0);
#if !SDL_VERSION_ATLEAST(2, 0, 0) #if !SDL_VERSION_ATLEAST(2, 0, 0)
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
#endif #endif
return 1; return true;
} }
void GBASDLDeinitEvents(struct GBASDLEvents* context) { void GBASDLDeinitEvents(struct GBASDLEvents* context) {

View File

@ -16,7 +16,7 @@ struct GBASDLEvents {
#endif #endif
}; };
int GBASDLInitEvents(struct GBASDLEvents*); bool GBASDLInitEvents(struct GBASDLEvents*);
void GBASDLDeinitEvents(struct GBASDLEvents*); void GBASDLDeinitEvents(struct GBASDLEvents*);
void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const union SDL_Event* event); void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const union SDL_Event* event);

View File

@ -3,32 +3,32 @@
#include "util/patch.h" #include "util/patch.h"
static size_t _IPSOutputSize(struct Patch* patch, size_t inSize); static size_t _IPSOutputSize(struct Patch* patch, size_t inSize);
static int _IPSApplyPatch(struct Patch* patch, void* out, size_t outSize); static bool _IPSApplyPatch(struct Patch* patch, void* out, size_t outSize);
int loadPatchIPS(struct Patch* patch) { bool loadPatchIPS(struct Patch* patch) {
lseek(patch->patchfd, 0, SEEK_SET); lseek(patch->patchfd, 0, SEEK_SET);
char buffer[5]; char buffer[5];
if (read(patch->patchfd, buffer, 5) != 5) { if (read(patch->patchfd, buffer, 5) != 5) {
return 0; return false;
} }
if (memcmp(buffer, "PATCH", 5) != 0) { if (memcmp(buffer, "PATCH", 5) != 0) {
return 0; return false;
} }
lseek(patch->patchfd, -3, SEEK_END); lseek(patch->patchfd, -3, SEEK_END);
if (read(patch->patchfd, buffer, 3) != 3) { if (read(patch->patchfd, buffer, 3) != 3) {
return 0; return false;
} }
if (memcmp(buffer, "EOF", 3) != 0) { if (memcmp(buffer, "EOF", 3) != 0) {
return 0; return false;
} }
patch->outputSize = _IPSOutputSize; patch->outputSize = _IPSOutputSize;
patch->applyPatch = _IPSApplyPatch; patch->applyPatch = _IPSApplyPatch;
return 1; return true;
} }
size_t _IPSOutputSize(struct Patch* patch, size_t inSize) { size_t _IPSOutputSize(struct Patch* patch, size_t inSize) {
@ -36,49 +36,49 @@ size_t _IPSOutputSize(struct Patch* patch, size_t inSize) {
return inSize; return inSize;
} }
int _IPSApplyPatch(struct Patch* patch, void* out, size_t outSize) { bool _IPSApplyPatch(struct Patch* patch, void* out, size_t outSize) {
if (lseek(patch->patchfd, 5, SEEK_SET) != 5) { if (lseek(patch->patchfd, 5, SEEK_SET) != 5) {
return 0; return false;
} }
uint8_t* buf = out; uint8_t* buf = out;
while (1) { while (true) {
uint32_t offset = 0; uint32_t offset = 0;
uint16_t size = 0; uint16_t size = 0;
if (read(patch->patchfd, &offset, 3) != 3) { if (read(patch->patchfd, &offset, 3) != 3) {
return 0; return false;
} }
if (offset == 0x464F45) { if (offset == 0x464F45) {
return 1; return true;
} }
offset = (offset >> 16) | (offset & 0xFF00) | ((offset << 16) & 0xFF0000); offset = (offset >> 16) | (offset & 0xFF00) | ((offset << 16) & 0xFF0000);
if (read(patch->patchfd, &size, 2) != 2) { if (read(patch->patchfd, &size, 2) != 2) {
return 0; return false;
} }
if (!size) { if (!size) {
// RLE chunk // RLE chunk
if (read(patch->patchfd, &size, 2) != 2) { if (read(patch->patchfd, &size, 2) != 2) {
return 0; return false;
} }
size = (size >> 8) | (size << 8); size = (size >> 8) | (size << 8);
uint8_t byte; uint8_t byte;
if (read(patch->patchfd, &byte, 1) != 1) { if (read(patch->patchfd, &byte, 1) != 1) {
return 0; return false;
} }
if (offset + size > outSize) { if (offset + size > outSize) {
return 0; return false;
} }
memset(&buf[offset], byte, size); memset(&buf[offset], byte, size);
} else { } else {
size = (size >> 8) | (size << 8); size = (size >> 8) | (size << 8);
if (offset + size > outSize) { if (offset + size > outSize) {
return 0; return false;
} }
if (read(patch->patchfd, &buf[offset], size) != size) { if (read(patch->patchfd, &buf[offset], size) != size) {
return 0; return false;
} }
} }
} }

View File

@ -1,8 +1,10 @@
#ifndef PATCH_IPS_H #ifndef PATCH_IPS_H
#define PATCH_IPS_H #define PATCH_IPS_H
#include "common.h"
struct Patch; struct Patch;
int loadPatchIPS(struct Patch* patch); bool loadPatchIPS(struct Patch* patch);
#endif #endif

View File

@ -12,19 +12,19 @@ enum {
}; };
static size_t _UPSOutputSize(struct Patch* patch, size_t inSize); static size_t _UPSOutputSize(struct Patch* patch, size_t inSize);
static int _UPSApplyPatch(struct Patch* patch, void* out, size_t outSize); static bool _UPSApplyPatch(struct Patch* patch, void* out, size_t outSize);
static size_t _UPSDecodeLength(int fd); static size_t _UPSDecodeLength(int fd);
int loadPatchUPS(struct Patch* patch) { bool loadPatchUPS(struct Patch* patch) {
lseek(patch->patchfd, 0, SEEK_SET); lseek(patch->patchfd, 0, SEEK_SET);
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
if (read(patch->patchfd, buffer, 4) != 4) { if (read(patch->patchfd, buffer, 4) != 4) {
return 0; return false;
} }
if (memcmp(buffer, "UPS1", 4) != 0) { if (memcmp(buffer, "UPS1", 4) != 0) {
return 0; return false;
} }
size_t filesize = lseek(patch->patchfd, 0, SEEK_END); size_t filesize = lseek(patch->patchfd, 0, SEEK_END);
@ -32,7 +32,7 @@ int loadPatchUPS(struct Patch* patch) {
uint32_t goodCrc32; uint32_t goodCrc32;
lseek(patch->patchfd, PATCH_CHECKSUM, SEEK_END); lseek(patch->patchfd, PATCH_CHECKSUM, SEEK_END);
if (read(patch->patchfd, &goodCrc32, 4) != 4) { if (read(patch->patchfd, &goodCrc32, 4) != 4) {
return 0; return false;
} }
size_t blocksize; size_t blocksize;
@ -53,12 +53,12 @@ int loadPatchUPS(struct Patch* patch) {
} }
if (crc != goodCrc32) { if (crc != goodCrc32) {
return 0; return false;
} }
patch->outputSize = _UPSOutputSize; patch->outputSize = _UPSOutputSize;
patch->applyPatch = _UPSApplyPatch; patch->applyPatch = _UPSApplyPatch;
return 1; return true;
} }
size_t _UPSOutputSize(struct Patch* patch, size_t inSize) { size_t _UPSOutputSize(struct Patch* patch, size_t inSize) {
@ -70,14 +70,14 @@ size_t _UPSOutputSize(struct Patch* patch, size_t inSize) {
return _UPSDecodeLength(patch->patchfd); return _UPSDecodeLength(patch->patchfd);
} }
int _UPSApplyPatch(struct Patch* patch, void* out, size_t outSize) { bool _UPSApplyPatch(struct Patch* patch, void* out, size_t outSize) {
// TODO: Input checksum // TODO: Input checksum
size_t filesize = lseek(patch->patchfd, 0, SEEK_END); size_t filesize = lseek(patch->patchfd, 0, SEEK_END);
lseek(patch->patchfd, 4, SEEK_SET); lseek(patch->patchfd, 4, SEEK_SET);
_UPSDecodeLength(patch->patchfd); // Discard input size _UPSDecodeLength(patch->patchfd); // Discard input size
if (_UPSDecodeLength(patch->patchfd) != outSize) { if (_UPSDecodeLength(patch->patchfd) != outSize) {
return 0; return false;
} }
size_t offset = 0; size_t offset = 0;
@ -87,9 +87,9 @@ int _UPSApplyPatch(struct Patch* patch, void* out, size_t outSize) {
offset += _UPSDecodeLength(patch->patchfd); offset += _UPSDecodeLength(patch->patchfd);
uint8_t byte; uint8_t byte;
while (1) { while (true) {
if (read(patch->patchfd, &byte, 1) != 1) { if (read(patch->patchfd, &byte, 1) != 1) {
return 0; return false;
} }
buf[offset] ^= byte; buf[offset] ^= byte;
++offset; ++offset;
@ -103,21 +103,21 @@ int _UPSApplyPatch(struct Patch* patch, void* out, size_t outSize) {
uint32_t goodCrc32; uint32_t goodCrc32;
lseek(patch->patchfd, OUT_CHECKSUM, SEEK_END); lseek(patch->patchfd, OUT_CHECKSUM, SEEK_END);
if (read(patch->patchfd, &goodCrc32, 4) != 4) { if (read(patch->patchfd, &goodCrc32, 4) != 4) {
return 0; return false;
} }
lseek(patch->patchfd, 0, SEEK_SET); lseek(patch->patchfd, 0, SEEK_SET);
if (crc32(out, outSize) != goodCrc32) { if (crc32(out, outSize) != goodCrc32) {
return 0; return false;
} }
return 1; return true;
} }
size_t _UPSDecodeLength(int fd) { size_t _UPSDecodeLength(int fd) {
size_t shift = 1; size_t shift = 1;
size_t value = 0; size_t value = 0;
uint8_t byte; uint8_t byte;
while (1) { while (true) {
if (read(fd, &byte, 1) != 1) { if (read(fd, &byte, 1) != 1) {
break; break;
} }

View File

@ -1,8 +1,10 @@
#ifndef PATCH_UPS_H #ifndef PATCH_UPS_H
#define PATCH_UPS_H #define PATCH_UPS_H
#include "common.h"
struct Patch; struct Patch;
int loadPatchUPS(struct Patch* patch); bool loadPatchUPS(struct Patch* patch);
#endif #endif

View File

@ -3,18 +3,18 @@
#include "util/patch-ips.h" #include "util/patch-ips.h"
#include "util/patch-ups.h" #include "util/patch-ups.h"
int loadPatch(int patchfd, struct Patch* patch) { bool loadPatch(int patchfd, struct Patch* patch) {
patch->patchfd = patchfd; patch->patchfd = patchfd;
if (loadPatchIPS(patch)) { if (loadPatchIPS(patch)) {
return 1; return true;
} }
if (loadPatchUPS(patch)) { if (loadPatchUPS(patch)) {
return 1; return true;
} }
patch->outputSize = 0; patch->outputSize = 0;
patch->applyPatch = 0; patch->applyPatch = 0;
return 0; return false;
} }

View File

@ -7,9 +7,9 @@ struct Patch {
int patchfd; int patchfd;
size_t (*outputSize)(struct Patch* patch, size_t inSize); size_t (*outputSize)(struct Patch* patch, size_t inSize);
int (*applyPatch)(struct Patch* patch, void* out, size_t outSize); bool (*applyPatch)(struct Patch* patch, void* out, size_t outSize);
}; };
int loadPatch(int patchfd, struct Patch* patch); bool loadPatch(int patchfd, struct Patch* patch);
#endif #endif