all repos — mgba @ 752b4faaff506092ed6017f14d6adb6463337243

mGBA Game Boy Advance Emulator

OpenEmu: Convert to using mCore
Jeffrey Pfau jeffrey@endrift.com
Sun, 07 Feb 2016 03:05:03 -0800
commit

752b4faaff506092ed6017f14d6adb6463337243

parent

c581764e236ab115d50f7a362c1b9abb02962a24

2 files changed, 53 insertions(+), 48 deletions(-)

jump to
M src/gba/core.csrc/gba/core.c

@@ -19,6 +19,7 @@ struct GBACore {

struct mCore d; struct GBAVideoSoftwareRenderer renderer; int keys; + struct ARMComponent* components[GBA_COMPONENT_MAX]; }; static bool _GBACoreInit(struct mCore* core) {

@@ -36,7 +37,8 @@ core->board = gba;

GBACreate(gba); // TODO: Restore debugger and cheats - ARMSetComponents(cpu, &gba->d, 0, 0); + memset(gbacore->components, 0, sizeof(gbacore->components)); + ARMSetComponents(cpu, &gba->d, GBA_COMPONENT_MAX, gbacore->components); ARMInit(cpu); GBAVideoSoftwareRendererCreate(&gbacore->renderer);
M src/platform/openemu/mGBAGameCore.msrc/platform/openemu/mGBAGameCore.m

@@ -26,11 +26,14 @@ #import "mGBAGameCore.h"

#include "util/common.h" +#include "core/core.h" +#include "gba/audio.h" #include "gba/cheats.h" +#include "gba/core.h" #include "gba/cheats/gameshark.h" -#include "gba/renderers/video-software.h" +#include "gba/gba.h" +#include "gba/input.h" #include "gba/serialize.h" -#include "gba/context/context.h" #include "util/circle-buffer.h" #include "util/memory.h" #include "util/vfs.h"

@@ -43,11 +46,11 @@ #define SAMPLES 1024

@interface mGBAGameCore () <OEGBASystemResponderClient> { - struct GBAContext context; - struct GBAVideoSoftwareRenderer renderer; + struct mCore* core; + struct GBA* gba; struct GBACheatDevice cheats; + void* outputBuffer; NSMutableDictionary *cheatSets; - uint16_t keys; } @end

@@ -57,21 +60,25 @@ - (id)init

{ if ((self = [super init])) { - // TODO: Add a log handler - GBAContextInit(&context, 0); + core = GBACoreCreate(); + mCoreInitConfig(core, nil); + struct mCoreOptions opts = { .useBios = true, }; - mCoreConfigLoadDefaults(&context.config, &opts); - GBAVideoSoftwareRendererCreate(&renderer); - renderer.outputBuffer = malloc(256 * VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL); - renderer.outputBufferStride = 256; - context.renderer = &renderer.d; - GBAAudioResizeBuffer(&context.gba->audio, SAMPLES); + mCoreConfigLoadDefaults(&core->config, &opts); + core->init(core); + + unsigned width, height; + core->desiredVideoDimensions(core, &width, &height); + outputBuffer = malloc(width * height * BYTES_PER_PIXEL); + core->setVideoBuffer(core, outputBuffer, width); + + gba = core->board; + GBAAudioResizeBuffer(&gba->audio, SAMPLES); GBACheatDeviceCreate(&cheats); - GBACheatAttachDevice(context.gba, &cheats); + GBACheatAttachDevice(gba, &cheats); cheatSets = [[NSMutableDictionary alloc] init]; - keys = 0; } return self;

@@ -79,7 +86,7 @@ }

- (void)dealloc { - GBAContextDeinit(&context); + core->deinit(core); [cheatSets enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { UNUSED(key); UNUSED(stop);

@@ -92,7 +99,7 @@ UNUSED(stop);

GBACheatSetDeinit([obj pointerValue]); }]; [cheatSets release]; - free(renderer.outputBuffer); + free(outputBuffer); [super dealloc]; }

@@ -106,50 +113,42 @@ [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:batterySavesDirectory]

withIntermediateDirectories:YES attributes:nil error:nil]; - if (context.dirs.save) { - context.dirs.save->close(context.dirs.save); + if (core->dirs.save) { + core->dirs.save->close(core->dirs.save); } - context.dirs.save = VDirOpen([batterySavesDirectory UTF8String]); + core->dirs.save = VDirOpen([batterySavesDirectory UTF8String]); - if (!GBAContextLoadROM(&context, [path UTF8String], true)) { + if (!mCoreLoadFile(core, [path UTF8String])) { *error = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreCouldNotLoadROMError userInfo:nil]; return NO; } + mCoreAutoloadSave(core); - if (!GBAContextStart(&context)) { - *error = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreCouldNotStartCoreError userInfo:nil]; - return NO; - } + core->reset(core); return YES; } - (void)executeFrame { - GBAContextFrame(&context, keys); + core->runFrame(core); int16_t samples[SAMPLES * 2]; size_t available = 0; - available = blip_samples_avail(context.gba->audio.psg.left); - blip_read_samples(context.gba->audio.psg.left, samples, available, true); - blip_read_samples(context.gba->audio.psg.right, samples + 1, available, true); + available = blip_samples_avail(core->getAudioChannel(core, 0)); + blip_read_samples(core->getAudioChannel(core, 0), samples, available, true); + blip_read_samples(core->getAudioChannel(core, 1), samples + 1, available, true); [[self ringBufferAtIndex:0] write:samples maxLength:available * 4]; } - (void)resetEmulation { - ARMReset(context.cpu); -} - -- (void)stopEmulation -{ - GBAContextStop(&context); - [super stopEmulation]; + core->reset(core); } - (void)setupEmulation { - blip_set_rates(context.gba->audio.psg.left, GBA_ARM7TDMI_FREQUENCY, 32768); - blip_set_rates(context.gba->audio.psg.right, GBA_ARM7TDMI_FREQUENCY, 32768); + blip_set_rates(core->getAudioChannel(core, 0), GBA_ARM7TDMI_FREQUENCY, 32768); + blip_set_rates(core->getAudioChannel(core, 1), GBA_ARM7TDMI_FREQUENCY, 32768); } #pragma mark - Video

@@ -161,17 +160,21 @@ }

- (OEIntRect)screenRect { - return OEIntRectMake(0, 0, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS); + unsigned width, height; + core->desiredVideoDimensions(core, &width, &height); + return OEIntRectMake(0, 0, width, height); } - (OEIntSize)bufferSize { - return OEIntSizeMake(256, VIDEO_VERTICAL_PIXELS); + unsigned width, height; + core->desiredVideoDimensions(core, &width, &height); + return OEIntSizeMake(width, height); } - (const void *)videoBuffer { - return renderer.outputBuffer; + return outputBuffer; } - (GLenum)pixelFormat

@@ -211,7 +214,7 @@

- (NSData *)serializeStateWithError:(NSError **)outError { struct VFile* vf = VFileMemChunk(nil, 0); - if (!GBASaveStateNamed(context.gba, vf, SAVESTATE_SAVEDATA)) { + if (!core->saveState(core, vf, SAVESTATE_SAVEDATA)) { *outError = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreCouldNotLoadStateError userInfo:nil]; vf->close(vf); return nil;

@@ -227,7 +230,7 @@

- (BOOL)deserializeState:(NSData *)state withError:(NSError **)outError { struct VFile* vf = VFileFromConstMemory(state.bytes, state.length); - if (!GBALoadStateNamed(context.gba, vf, SAVESTATE_SAVEDATA)) { + if (!core->loadState(core, vf, SAVESTATE_SAVEDATA)) { *outError = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreCouldNotLoadStateError userInfo:nil]; vf->close(vf); return NO;

@@ -239,14 +242,14 @@

- (void)saveStateToFileAtPath:(NSString *)fileName completionHandler:(void (^)(BOOL, NSError *))block { struct VFile* vf = VFileOpen([fileName UTF8String], O_CREAT | O_TRUNC | O_RDWR); - block(GBASaveStateNamed(context.gba, vf, 0), nil); + block(core->saveState(core, vf, 0), nil); vf->close(vf); } - (void)loadStateFromFileAtPath:(NSString *)fileName completionHandler:(void (^)(BOOL, NSError *))block { struct VFile* vf = VFileOpen([fileName UTF8String], O_RDONLY); - block(GBALoadStateNamed(context.gba, vf, 0), nil); + block(core->loadState(core, vf, 0), nil); vf->close(vf); }

@@ -268,13 +271,13 @@

- (oneway void)didPushGBAButton:(OEGBAButton)button forPlayer:(NSUInteger)player { UNUSED(player); - keys |= 1 << GBAMap[button]; + core->addKeys(core, 1 << GBAMap[button]); } - (oneway void)didReleaseGBAButton:(OEGBAButton)button forPlayer:(NSUInteger)player { UNUSED(player); - keys &= ~(1 << GBAMap[button]); + core->clearKeys(core, 1 << GBAMap[button]); } #pragma mark - Cheats