all repos — mgba @ 3abd2ca0289ef1bd0ec10340b047304202a3cb3b

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