all repos — mgba @ a75c019fab24f045069cb9a923d02795f4d6f564

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