Use stdbool for readability
jump to
@@ -5,6 +5,7 @@ #include <ctype.h>
#include <limits.h> #include <math.h> #include <stdarg.h> +#include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <stdio.h>
@@ -395,7 +395,7 @@ dv = next;
} } -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, ' '); size_t cmdLength; struct DebugVector* dv = 0;@@ -405,7 +405,7 @@ dv = _DVParse(debugger, firstSpace + 1, count - cmdLength - 1);
if (dv && dv->type == DV_ERROR_TYPE) { printf("Parse error\n"); _DVFree(dv); - return 0; + return false; } } else { cmdLength = count;@@ -420,12 +420,12 @@ }
if (strncasecmp(name, line, cmdLength) == 0) { _debuggerCommands[i].command(debugger, dv); _DVFree(dv); - return 1; + return true; } } _DVFree(dv); printf("Command not found\n"); - return 0; + return false; } static char* _prompt(EditLine* el) {
@@ -500,7 +500,7 @@ } else {
goto connectionLost; } } - while (1) { + while (true) { ssize_t messageLen = SocketRecv(stub->connection, stub->line, GDB_STUB_MAX_LINE - 1); if (messageLen == 0) { goto connectionLost;
@@ -4,7 +4,7 @@ #include "debugger.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) \ { \@@ -46,14 +46,14 @@ CREATE_WATCHPOINT_SHIM(store8, 1, void, (struct ARMCore* cpu, uint32_t address, int8_t value, int* cycleCounter), address, value, cycleCounter)
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) -static int _checkWatchpoints(struct DebugBreakpoint* watchpoints, uint32_t address, int width) { +static bool _checkWatchpoints(struct DebugBreakpoint* watchpoints, uint32_t address, int width) { width -= 1; for (; watchpoints; watchpoints = watchpoints->next) { if (!((watchpoints->address ^ address) & ~width)) { - return 1; + return true; } } - return 0; + return false; } void ARMDebuggerInstallMemoryShim(struct ARMDebugger* debugger) {
@@ -11,7 +11,7 @@ #define SWEEP_CYCLES (GBA_ARM7TDMI_FREQUENCY / 128)
static int32_t _updateSquareChannel(struct GBAAudioSquareControl* envelope, int duty); 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 _updateChannel2(struct GBAAudioChannel2* ch); static int32_t _updateChannel3(struct GBAAudioChannel3* ch);@@ -497,7 +497,7 @@ envelope->nextStep += envelope->stepTime * (GBA_ARM7TDMI_FREQUENCY >> 6);
} } -static int _updateSweep(struct GBAAudioChannel1* ch) { +static bool _updateSweep(struct GBAAudioChannel1* ch) { if (ch->sweep.direction) { int frequency = ch->control.frequency; frequency -= frequency >> ch->sweep.shift;@@ -510,11 +510,11 @@ frequency += frequency >> ch->sweep.shift;
if (frequency < 2048) { ch->control.frequency = frequency; } else { - return 0; + return false; } } ch->nextSweep += ch->sweep.time * SWEEP_CYCLES; - return 1; + return true; } static int32_t _updateChannel1(struct GBAAudioChannel1* ch) {
@@ -73,28 +73,28 @@ }
return fd; } -int GBASaveState(struct GBA* gba, int slot) { +bool GBASaveState(struct GBA* gba, int slot) { int fd = _getStateFd(gba, slot); if (fd < 0) { - return 0; + return false; } struct GBASerializedState* state = GBAMapState(fd); GBASerialize(gba, state); GBADeallocateState(state); 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); if (fd < 0) { - return 0; + return false; } struct GBASerializedState* state = GBAMapState(fd); GBADeserialize(gba, state); GBADeallocateState(state); close(fd); - return 1; + return true; } struct GBASerializedState* GBAMapState(int fd) {
@@ -228,8 +228,8 @@
void GBASerialize(struct GBA* gba, struct GBASerializedState* state); void GBADeserialize(struct GBA* gba, struct GBASerializedState* state); -int GBASaveState(struct GBA* gba, int slot); -int GBALoadState(struct GBA* gba, int slot); +bool GBASaveState(struct GBA* gba, int slot); +bool GBALoadState(struct GBA* gba, int slot); struct GBASerializedState* GBAMapState(int fd); struct GBASerializedState* GBAAllocateState(void);
@@ -200,7 +200,7 @@ threadContext->rewindBufferCapacity = opts->rewindBufferCapacity;
threadContext->rewindBufferInterval = opts->rewindBufferInterval; } -int GBAThreadStart(struct GBAThread* threadContext) { +bool GBAThreadStart(struct GBAThread* threadContext) { // TODO: error check threadContext->activeKeys = 0; threadContext->state = THREAD_INITIALIZED;@@ -239,11 +239,11 @@ ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
} MutexUnlock(&threadContext->stateMutex); - return 0; + return true; } -int GBAThreadHasStarted(struct GBAThread* threadContext) { - int hasStarted; +bool GBAThreadHasStarted(struct GBAThread* threadContext) { + bool hasStarted; MutexLock(&threadContext->stateMutex); hasStarted = threadContext->state > THREAD_INITIALIZED; MutexUnlock(&threadContext->stateMutex);@@ -357,8 +357,8 @@ }
MutexUnlock(&threadContext->sync.videoFrameMutex); } -int GBAThreadIsPaused(struct GBAThread* threadContext) { - int isPaused; +bool GBAThreadIsPaused(struct GBAThread* threadContext) { + bool isPaused; MutexLock(&threadContext->stateMutex); _waitOnInterrupt(threadContext); isPaused = threadContext->state == THREAD_PAUSED;@@ -367,7 +367,7 @@ return isPaused;
} void GBAThreadTogglePause(struct GBAThread* threadContext) { - int frameOn = 1; + bool frameOn = true; MutexLock(&threadContext->stateMutex); _waitOnInterrupt(threadContext); if (threadContext->state == THREAD_PAUSED) {@@ -378,7 +378,7 @@ if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
threadContext->debugger->state = DEBUGGER_EXITING; } threadContext->state = THREAD_PAUSED; - frameOn = 0; + frameOn = false; } MutexUnlock(&threadContext->stateMutex); MutexLock(&threadContext->sync.videoFrameMutex);@@ -430,20 +430,20 @@ thread->frameCallback(thread);
} } -int GBASyncWaitFrameStart(struct GBASync* sync, int frameskip) { +bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip) { if (!sync) { - return 1; + return true; } MutexLock(&sync->videoFrameMutex); ConditionWake(&sync->videoFrameRequiredCond); if (!sync->videoFrameOn) { - return 0; + return false; } ConditionWait(&sync->videoFrameAvailableCond, &sync->videoFrameMutex); sync->videoFramePending = 0; sync->videoFrameSkip = frameskip; - return 1; + return true; } void GBASyncWaitFrameEnd(struct GBASync* sync) {@@ -454,7 +454,7 @@
MutexUnlock(&sync->videoFrameMutex); } -int GBASyncDrawingFrame(struct GBASync* sync) { +bool GBASyncDrawingFrame(struct GBASync* sync) { return sync->videoFrameSkip <= 0; }
@@ -27,7 +27,7 @@ struct GBASync {
int videoFramePending; int videoFrameWait; int videoFrameSkip; - int videoFrameOn; + bool videoFrameOn; Mutex videoFrameMutex; Condition videoFrameAvailableCond; Condition videoFrameRequiredCond;@@ -80,8 +80,8 @@ };
void GBAMapOptionsToContext(struct StartupOptions*, struct GBAThread*); -int GBAThreadStart(struct GBAThread* threadContext); -int GBAThreadHasStarted(struct GBAThread* threadContext); +bool GBAThreadStart(struct GBAThread* threadContext); +bool GBAThreadHasStarted(struct GBAThread* threadContext); void GBAThreadEnd(struct GBAThread* threadContext); void GBAThreadReset(struct GBAThread* threadContext); void GBAThreadJoin(struct GBAThread* threadContext);@@ -91,14 +91,14 @@ void GBAThreadContinue(struct GBAThread* threadContext);
void GBAThreadPause(struct GBAThread* threadContext); void GBAThreadUnpause(struct GBAThread* threadContext); -int GBAThreadIsPaused(struct GBAThread* threadContext); +bool GBAThreadIsPaused(struct GBAThread* threadContext); void GBAThreadTogglePause(struct GBAThread* threadContext); struct GBAThread* GBAThreadGetContext(void); void GBASyncPostFrame(struct GBASync* sync); -int GBASyncWaitFrameStart(struct GBASync* sync, int frameskip); +bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip); void GBASyncWaitFrameEnd(struct GBASync* sync); -int GBASyncDrawingFrame(struct GBASync* sync); +bool GBASyncDrawingFrame(struct GBASync* sync); void GBASyncProduceAudio(struct GBASync* sync, int wait); void GBASyncLockAudio(struct GBASync* sync);
@@ -36,9 +36,9 @@ #endif
{ 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)); opts->fd = -1; opts->biosFd = -1;@@ -66,7 +66,7 @@ break;
#ifdef USE_CLI_DEBUGGER case 'd': if (opts->debuggerType != DEBUGGER_NONE) { - return 0; + return false; } opts->debuggerType = DEBUGGER_CLI; break;@@ -74,7 +74,7 @@ #endif
#ifdef USE_GDB_STUB case 'g': if (opts->debuggerType != DEBUGGER_NONE) { - return 0; + return false; } opts->debuggerType = DEBUGGER_GDB; break;@@ -91,7 +91,7 @@ break;
default: if (subparser) { if (!subparser->parse(subparser, ch, optarg)) { - return 0; + return false; } } break;@@ -104,10 +104,10 @@ opts->fname = argv[0];
} else if (argc == 0) { opts->fname = _defaultFilename; } else { - return 0; + return false; } opts->fd = open(opts->fname, O_RDONLY); - return 1; + return true; } void initParserForGraphics(struct SubParser* parser, struct GraphicsOpts* opts) {@@ -121,39 +121,39 @@ opts->width = 240;
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); struct GraphicsOpts* graphicsOpts = parser->opts; switch (option) { case 'f': graphicsOpts->fullscreen = 1; - return 1; + return true; case '2': if (graphicsOpts->multiplier != 1) { - return 0; + return false; } graphicsOpts->multiplier = 2; graphicsOpts->width *= graphicsOpts->multiplier; graphicsOpts->height *= graphicsOpts->multiplier; - return 1; + return true; case '3': if (graphicsOpts->multiplier != 1) { - return 0; + return false; } graphicsOpts->multiplier = 3; graphicsOpts->width *= graphicsOpts->multiplier; graphicsOpts->height *= graphicsOpts->multiplier; - return 1; + return true; case '4': if (graphicsOpts->multiplier != 1) { - return 0; + return false; } graphicsOpts->multiplier = 4; graphicsOpts->width *= graphicsOpts->multiplier; graphicsOpts->height *= graphicsOpts->multiplier; - return 1; + return true; default: - return 0; + return false; } }
@@ -30,7 +30,7 @@ };
struct SubParser { 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; void* opts; };@@ -42,7 +42,7 @@ int width;
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 initParserForGraphics(struct SubParser* parser, struct GraphicsOpts* opts);
@@ -8,10 +8,10 @@ #define FPS_TARGET 60.f
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) { GBALog(0, GBA_LOG_ERROR, "Could not initialize SDL sound system"); - return 0; + return false; } context->desiredSpec.freq = 44100;@@ -24,10 +24,10 @@ context->audio = 0;
context->drift = 0.f; if (SDL_OpenAudio(&context->desiredSpec, &context->obtainedSpec) < 0) { GBALog(0, GBA_LOG_ERROR, "Could not open SDL sound system"); - return 0; + return false; } SDL_PauseAudio(0); - return 1; + return true; } void GBASDLDeinitAudio(struct GBASDLAudio* context) {
@@ -13,7 +13,7 @@ float ratio;
struct GBAAudio* audio; }; -int GBASDLInitAudio(struct GBASDLAudio* context); +bool GBASDLInitAudio(struct GBASDLAudio* context); void GBASDLDeinitAudio(struct GBASDLAudio* context); #endif
@@ -11,16 +11,16 @@ #else
#define GUI_MOD KMOD_CTRL #endif -int GBASDLInitEvents(struct GBASDLEvents* context) { +bool GBASDLInitEvents(struct GBASDLEvents* context) { if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) { - return 0; + return false; } SDL_JoystickEventState(SDL_ENABLE); context->joystick = SDL_JoystickOpen(0); #if !SDL_VERSION_ATLEAST(2, 0, 0) SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); #endif - return 1; + return true; } void GBASDLDeinitEvents(struct GBASDLEvents* context) {
@@ -16,7 +16,7 @@ int windowUpdated;
#endif }; -int GBASDLInitEvents(struct GBASDLEvents*); +bool GBASDLInitEvents(struct GBASDLEvents*); void GBASDLDeinitEvents(struct GBASDLEvents*); void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const union SDL_Event* event);
@@ -3,32 +3,32 @@
#include "util/patch.h" 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); char buffer[5]; if (read(patch->patchfd, buffer, 5) != 5) { - return 0; + return false; } if (memcmp(buffer, "PATCH", 5) != 0) { - return 0; + return false; } lseek(patch->patchfd, -3, SEEK_END); if (read(patch->patchfd, buffer, 3) != 3) { - return 0; + return false; } if (memcmp(buffer, "EOF", 3) != 0) { - return 0; + return false; } patch->outputSize = _IPSOutputSize; patch->applyPatch = _IPSApplyPatch; - return 1; + return true; } size_t _IPSOutputSize(struct Patch* patch, size_t inSize) {@@ -36,49 +36,49 @@ UNUSED(patch);
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) { - return 0; + return false; } uint8_t* buf = out; - while (1) { + while (true) { uint32_t offset = 0; uint16_t size = 0; if (read(patch->patchfd, &offset, 3) != 3) { - return 0; + return false; } if (offset == 0x464F45) { - return 1; + return true; } offset = (offset >> 16) | (offset & 0xFF00) | ((offset << 16) & 0xFF0000); if (read(patch->patchfd, &size, 2) != 2) { - return 0; + return false; } if (!size) { // RLE chunk if (read(patch->patchfd, &size, 2) != 2) { - return 0; + return false; } size = (size >> 8) | (size << 8); uint8_t byte; if (read(patch->patchfd, &byte, 1) != 1) { - return 0; + return false; } if (offset + size > outSize) { - return 0; + return false; } memset(&buf[offset], byte, size); } else { size = (size >> 8) | (size << 8); if (offset + size > outSize) { - return 0; + return false; } if (read(patch->patchfd, &buf[offset], size) != size) { - return 0; + return false; } } }
@@ -1,8 +1,10 @@
#ifndef PATCH_IPS_H #define PATCH_IPS_H +#include "common.h" + struct Patch; -int loadPatchIPS(struct Patch* patch); +bool loadPatchIPS(struct Patch* patch); #endif
@@ -12,19 +12,19 @@ BUFFER_SIZE = 1024
}; 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); -int loadPatchUPS(struct Patch* patch) { +bool loadPatchUPS(struct Patch* patch) { lseek(patch->patchfd, 0, SEEK_SET); char buffer[BUFFER_SIZE]; if (read(patch->patchfd, buffer, 4) != 4) { - return 0; + return false; } if (memcmp(buffer, "UPS1", 4) != 0) { - return 0; + return false; } size_t filesize = lseek(patch->patchfd, 0, SEEK_END);@@ -32,7 +32,7 @@
uint32_t goodCrc32; lseek(patch->patchfd, PATCH_CHECKSUM, SEEK_END); if (read(patch->patchfd, &goodCrc32, 4) != 4) { - return 0; + return false; } size_t blocksize;@@ -53,12 +53,12 @@ }
} if (crc != goodCrc32) { - return 0; + return false; } patch->outputSize = _UPSOutputSize; patch->applyPatch = _UPSApplyPatch; - return 1; + return true; } size_t _UPSOutputSize(struct Patch* patch, size_t inSize) {@@ -70,14 +70,14 @@ }
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 size_t filesize = lseek(patch->patchfd, 0, SEEK_END); lseek(patch->patchfd, 4, SEEK_SET); _UPSDecodeLength(patch->patchfd); // Discard input size if (_UPSDecodeLength(patch->patchfd) != outSize) { - return 0; + return false; } size_t offset = 0;@@ -87,9 +87,9 @@ while (alreadyRead < filesize + IN_CHECKSUM) {
offset += _UPSDecodeLength(patch->patchfd); uint8_t byte; - while (1) { + while (true) { if (read(patch->patchfd, &byte, 1) != 1) { - return 0; + return false; } buf[offset] ^= byte; ++offset;@@ -103,21 +103,21 @@
uint32_t goodCrc32; lseek(patch->patchfd, OUT_CHECKSUM, SEEK_END); if (read(patch->patchfd, &goodCrc32, 4) != 4) { - return 0; + return false; } lseek(patch->patchfd, 0, SEEK_SET); if (crc32(out, outSize) != goodCrc32) { - return 0; + return false; } - return 1; + return true; } size_t _UPSDecodeLength(int fd) { size_t shift = 1; size_t value = 0; uint8_t byte; - while (1) { + while (true) { if (read(fd, &byte, 1) != 1) { break; }
@@ -1,8 +1,10 @@
#ifndef PATCH_UPS_H #define PATCH_UPS_H +#include "common.h" + struct Patch; -int loadPatchUPS(struct Patch* patch); +bool loadPatchUPS(struct Patch* patch); #endif
@@ -3,18 +3,18 @@
#include "util/patch-ips.h" #include "util/patch-ups.h" -int loadPatch(int patchfd, struct Patch* patch) { +bool loadPatch(int patchfd, struct Patch* patch) { patch->patchfd = patchfd; if (loadPatchIPS(patch)) { - return 1; + return true; } if (loadPatchUPS(patch)) { - return 1; + return true; } patch->outputSize = 0; patch->applyPatch = 0; - return 0; + return false; }
@@ -7,9 +7,9 @@ struct Patch {
int patchfd; 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