all repos — mgba @ 3d402f6afda8cb79e84110146d60843cb046708a

mGBA Game Boy Advance Emulator

src/gb/core.c (view raw)

  1/* Copyright (c) 2013-2016 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "core.h"
  7
  8#include "core/core.h"
  9#include "gb/gb.h"
 10#include "gb/renderers/software.h"
 11#include "util/memory.h"
 12#include "util/patch.h"
 13
 14struct GBCore {
 15	struct mCore d;
 16	struct GBVideoSoftwareRenderer renderer;
 17	uint8_t keys;
 18};
 19
 20static bool _GBCoreInit(struct mCore* core) {
 21	struct GBCore* gbcore = (struct GBCore*) core;
 22
 23	struct LR35902Core* cpu = anonymousMemoryMap(sizeof(struct LR35902Core));
 24	struct GB* gb = anonymousMemoryMap(sizeof(struct GB));
 25	if (!cpu || !gb) {
 26		free(cpu);
 27		free(gb);
 28		return false;
 29	}
 30	core->cpu = cpu;
 31	core->board = gb;
 32
 33	GBCreate(gb);
 34	LR35902SetComponents(cpu, &gb->d, 0, 0);
 35	LR35902Init(cpu);
 36
 37	GBVideoSoftwareRendererCreate(&gbcore->renderer);
 38
 39	gb->keySource = &gbcore->keys;
 40
 41#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 42	mDirectorySetInit(&core->dirs);
 43#endif
 44	
 45	return true;
 46}
 47
 48static void _GBCoreDeinit(struct mCore* core) {
 49	LR35902Deinit(core->cpu);
 50	GBDestroy(core->board);
 51	mappedMemoryFree(core->cpu, sizeof(struct LR35902Core));
 52	mappedMemoryFree(core->board, sizeof(struct GB));
 53#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 54	mDirectorySetDeinit(&core->dirs);
 55#endif
 56}
 57
 58static void _GBCoreSetSync(struct mCore* core, struct mCoreSync* sync) {
 59	struct GB* gb = core->board;
 60	gb->sync = sync;
 61}
 62
 63static void _GBCoreLoadConfig(struct mCore* core) {
 64	UNUSED(core);
 65	// TODO
 66}
 67
 68static void _GBCoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
 69	UNUSED(core);
 70	*width = GB_VIDEO_HORIZONTAL_PIXELS;
 71	*height = GB_VIDEO_VERTICAL_PIXELS;
 72}
 73
 74static void _GBCoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
 75	struct GBCore* gbcore = (struct GBCore*) core;
 76	gbcore->renderer.outputBuffer = buffer;
 77	gbcore->renderer.outputBufferStride = stride;
 78}
 79
 80static struct blip_t* _GBCoreGetAudioChannel(struct mCore* core, int ch) {
 81	struct GB* gb = core->board;
 82	switch (ch) {
 83	case 0:
 84		return gb->audio.left;
 85	case 1:
 86		return gb->audio.right;
 87	default:
 88		return NULL;
 89	}
 90}
 91
 92static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
 93	return GBLoadROM(core->board, vf);
 94}
 95
 96static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
 97	return GBLoadSave(core->board, vf);
 98}
 99
100static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
101	if (!vf) {
102		return false;
103	}
104	struct Patch patch;
105	if (!loadPatch(vf, &patch)) {
106		return false;
107	}
108	GBApplyPatch(core->board, &patch);
109	return true;
110}
111
112static void _GBCoreUnloadROM(struct mCore* core) {
113	return GBUnloadROM(core->board);
114}
115
116static void _GBCoreReset(struct mCore* core) {
117	struct GBCore* gbcore = (struct GBCore*) core;
118	struct GB* gb = (struct GB*) core->board;
119	GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
120	LR35902Reset(core->cpu);
121}
122
123static void _GBCoreRunFrame(struct mCore* core) {
124	struct GB* gb = core->board;
125	int32_t frameCounter = gb->video.frameCounter;
126	while (gb->video.frameCounter == frameCounter) {
127		LR35902Run(core->cpu);
128	}
129}
130
131static void _GBCoreRunLoop(struct mCore* core) {
132	LR35902Run(core->cpu);
133}
134
135static void _GBCoreStep(struct mCore* core) {
136	LR35902Tick(core->cpu);
137}
138
139static bool _GBCoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
140	UNUSED(core);
141	UNUSED(vf);
142	UNUSED(flags);
143	// TODO
144	return false;
145}
146
147static bool _GBCoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
148	UNUSED(core);
149	UNUSED(vf);
150	UNUSED(flags);
151	// TODO
152	return false;
153}
154
155static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
156	struct GBCore* gbcore = (struct GBCore*) core;
157	gbcore->keys = keys;
158}
159
160static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
161	struct GBCore* gbcore = (struct GBCore*) core;
162	gbcore->keys |= keys;
163}
164
165static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
166	struct GBCore* gbcore = (struct GBCore*) core;
167	gbcore->keys &= ~keys;
168}
169
170static int32_t _GBCoreFrameCounter(struct mCore* core) {
171	struct GB* gb = core->board;
172	return gb->video.frameCounter;
173}
174
175static int32_t _GBCoreFrameCycles(struct mCore* core) {
176	UNUSED(core);
177	return GB_VIDEO_TOTAL_LENGTH;
178}
179
180static int32_t _GBCoreFrequency(struct mCore* core) {
181	UNUSED(core);
182	// TODO: GB differences
183	return DMG_LR35902_FREQUENCY;
184}
185
186static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
187	struct GB* gb = core->board;
188	gb->memory.rtc = rtc;
189}
190
191struct mCore* GBCoreCreate(void) {
192	struct GBCore* gbcore = malloc(sizeof(*gbcore));
193	struct mCore* core = &gbcore->d;
194	memset(&core->opts, 0, sizeof(core->opts));
195	core->cpu = 0;
196	core->board = 0;
197	core->init = _GBCoreInit;
198	core->deinit = _GBCoreDeinit;
199	core->setSync = _GBCoreSetSync;
200	core->loadConfig = _GBCoreLoadConfig;
201	core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
202	core->setVideoBuffer = _GBCoreSetVideoBuffer;
203	core->getAudioChannel = _GBCoreGetAudioChannel;
204	core->isROM = GBIsROM;
205	core->loadROM = _GBCoreLoadROM;
206	core->loadSave = _GBCoreLoadSave;
207	core->loadPatch = _GBCoreLoadPatch;
208	core->unloadROM = _GBCoreUnloadROM;
209	core->reset = _GBCoreReset;
210	core->runFrame = _GBCoreRunFrame;
211	core->runLoop = _GBCoreRunLoop;
212	core->step = _GBCoreStep;
213	core->loadState = _GBCoreLoadState;
214	core->saveState = _GBCoreSaveState;
215	core->setKeys = _GBCoreSetKeys;
216	core->addKeys = _GBCoreAddKeys;
217	core->clearKeys = _GBCoreClearKeys;
218	core->frameCounter = _GBCoreFrameCounter;
219	core->frameCycles = _GBCoreFrameCycles;
220	core->frequency = _GBCoreFrequency;
221	core->setRTC = _GBCoreSetRTC;
222	return core;
223}