all repos — mgba @ 52152a3a12f36f91ace52b31ab9f0c129b8cd19e

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 enum mPlatform _GBCorePlatform(struct mCore* core) {
 59	UNUSED(core);
 60	return PLATFORM_GB;
 61}
 62
 63static void _GBCoreSetSync(struct mCore* core, struct mCoreSync* sync) {
 64	struct GB* gb = core->board;
 65	gb->sync = sync;
 66}
 67
 68static void _GBCoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
 69	UNUSED(config);
 70
 71	struct GB* gb = core->board;
 72	gb->audio.masterVolume = core->opts.volume;
 73}
 74
 75static void _GBCoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
 76	UNUSED(core);
 77	*width = GB_VIDEO_HORIZONTAL_PIXELS;
 78	*height = GB_VIDEO_VERTICAL_PIXELS;
 79}
 80
 81static void _GBCoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
 82	struct GBCore* gbcore = (struct GBCore*) core;
 83	gbcore->renderer.outputBuffer = buffer;
 84	gbcore->renderer.outputBufferStride = stride;
 85}
 86
 87static void _GBCoreGetVideoBuffer(struct mCore* core, color_t** buffer, size_t* stride) {
 88	struct GBCore* gbcore = (struct GBCore*) core;
 89	*buffer = gbcore->renderer.outputBuffer;
 90	*stride = gbcore->renderer.outputBufferStride;
 91}
 92
 93static struct blip_t* _GBCoreGetAudioChannel(struct mCore* core, int ch) {
 94	struct GB* gb = core->board;
 95	switch (ch) {
 96	case 0:
 97		return gb->audio.left;
 98	case 1:
 99		return gb->audio.right;
100	default:
101		return NULL;
102	}
103}
104
105static void _GBCoreSetAudioBufferSize(struct mCore* core, size_t samples) {
106	struct GB* gb = core->board;
107	GBAudioResizeBuffer(&gb->audio, samples);
108}
109
110static size_t _GBCoreGetAudioBufferSize(struct mCore* core) {
111	struct GB* gb = core->board;
112	return gb->audio.samples;
113}
114
115static void _GBCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
116	// TODO
117}
118
119static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
120	return GBLoadROM(core->board, vf);
121}
122
123static bool _GBCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
124	UNUSED(core);
125	UNUSED(vf);
126	UNUSED(type);
127	// TODO
128	return false;
129}
130
131static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
132	return GBLoadSave(core->board, vf);
133}
134
135static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
136	if (!vf) {
137		return false;
138	}
139	struct Patch patch;
140	if (!loadPatch(vf, &patch)) {
141		return false;
142	}
143	GBApplyPatch(core->board, &patch);
144	return true;
145}
146
147static void _GBCoreUnloadROM(struct mCore* core) {
148	return GBUnloadROM(core->board);
149}
150
151static void _GBCoreReset(struct mCore* core) {
152	struct GBCore* gbcore = (struct GBCore*) core;
153	struct GB* gb = (struct GB*) core->board;
154	GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
155	LR35902Reset(core->cpu);
156}
157
158static void _GBCoreRunFrame(struct mCore* core) {
159	struct GB* gb = core->board;
160	int32_t frameCounter = gb->video.frameCounter;
161	while (gb->video.frameCounter == frameCounter) {
162		LR35902Run(core->cpu);
163	}
164}
165
166static void _GBCoreRunLoop(struct mCore* core) {
167	LR35902Run(core->cpu);
168}
169
170static void _GBCoreStep(struct mCore* core) {
171	LR35902Tick(core->cpu);
172}
173
174static bool _GBCoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
175	UNUSED(core);
176	UNUSED(vf);
177	UNUSED(flags);
178	// TODO
179	return false;
180}
181
182static bool _GBCoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
183	UNUSED(core);
184	UNUSED(vf);
185	UNUSED(flags);
186	// TODO
187	return false;
188}
189
190static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
191	struct GBCore* gbcore = (struct GBCore*) core;
192	gbcore->keys = keys;
193}
194
195static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
196	struct GBCore* gbcore = (struct GBCore*) core;
197	gbcore->keys |= keys;
198}
199
200static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
201	struct GBCore* gbcore = (struct GBCore*) core;
202	gbcore->keys &= ~keys;
203}
204
205static int32_t _GBCoreFrameCounter(struct mCore* core) {
206	struct GB* gb = core->board;
207	return gb->video.frameCounter;
208}
209
210static int32_t _GBCoreFrameCycles(struct mCore* core) {
211	UNUSED(core);
212	return GB_VIDEO_TOTAL_LENGTH;
213}
214
215static int32_t _GBCoreFrequency(struct mCore* core) {
216	UNUSED(core);
217	// TODO: GB differences
218	return DMG_LR35902_FREQUENCY;
219}
220
221static void _GBCoreGetGameTitle(struct mCore* core, char* title) {
222	GBGetGameTitle(core->board, title);
223}
224
225static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
226	struct GB* gb = core->board;
227	gb->memory.rtc = rtc;
228}
229
230struct mCore* GBCoreCreate(void) {
231	struct GBCore* gbcore = malloc(sizeof(*gbcore));
232	struct mCore* core = &gbcore->d;
233	memset(&core->opts, 0, sizeof(core->opts));
234	core->cpu = 0;
235	core->board = 0;
236	core->init = _GBCoreInit;
237	core->deinit = _GBCoreDeinit;
238	core->platform = _GBCorePlatform;
239	core->setSync = _GBCoreSetSync;
240	core->loadConfig = _GBCoreLoadConfig;
241	core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
242	core->setVideoBuffer = _GBCoreSetVideoBuffer;
243	core->getVideoBuffer = _GBCoreGetVideoBuffer;
244	core->getAudioChannel = _GBCoreGetAudioChannel;
245	core->setAudioBufferSize = _GBCoreSetAudioBufferSize;
246	core->getAudioBufferSize = _GBCoreGetAudioBufferSize;
247	core->setAVStream = _GBCoreSetAVStream;
248	core->isROM = GBIsROM;
249	core->loadROM = _GBCoreLoadROM;
250	core->loadBIOS = _GBCoreLoadBIOS;
251	core->loadSave = _GBCoreLoadSave;
252	core->loadPatch = _GBCoreLoadPatch;
253	core->unloadROM = _GBCoreUnloadROM;
254	core->reset = _GBCoreReset;
255	core->runFrame = _GBCoreRunFrame;
256	core->runLoop = _GBCoreRunLoop;
257	core->step = _GBCoreStep;
258	core->loadState = _GBCoreLoadState;
259	core->saveState = _GBCoreSaveState;
260	core->setKeys = _GBCoreSetKeys;
261	core->addKeys = _GBCoreAddKeys;
262	core->clearKeys = _GBCoreClearKeys;
263	core->frameCounter = _GBCoreFrameCounter;
264	core->frameCycles = _GBCoreFrameCycles;
265	core->frequency = _GBCoreFrequency;
266	core->getGameTitle = _GBCoreGetGameTitle;
267	core->setRTC = _GBCoreSetRTC;
268	return core;
269}