all repos — mgba @ 73b58ec7c43f70f899147bbc1ef83e0ea2a4f0a6

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 _GBCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
100	// TODO
101}
102
103static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
104	return GBLoadROM(core->board, vf);
105}
106
107static bool _GBCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
108	UNUSED(core);
109	UNUSED(vf);
110	UNUSED(type);
111	// TODO
112	return false;
113}
114
115static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
116	return GBLoadSave(core->board, vf);
117}
118
119static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
120	if (!vf) {
121		return false;
122	}
123	struct Patch patch;
124	if (!loadPatch(vf, &patch)) {
125		return false;
126	}
127	GBApplyPatch(core->board, &patch);
128	return true;
129}
130
131static void _GBCoreUnloadROM(struct mCore* core) {
132	return GBUnloadROM(core->board);
133}
134
135static void _GBCoreReset(struct mCore* core) {
136	struct GBCore* gbcore = (struct GBCore*) core;
137	struct GB* gb = (struct GB*) core->board;
138	GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
139	LR35902Reset(core->cpu);
140}
141
142static void _GBCoreRunFrame(struct mCore* core) {
143	struct GB* gb = core->board;
144	int32_t frameCounter = gb->video.frameCounter;
145	while (gb->video.frameCounter == frameCounter) {
146		LR35902Run(core->cpu);
147	}
148}
149
150static void _GBCoreRunLoop(struct mCore* core) {
151	LR35902Run(core->cpu);
152}
153
154static void _GBCoreStep(struct mCore* core) {
155	LR35902Tick(core->cpu);
156}
157
158static bool _GBCoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
159	UNUSED(core);
160	UNUSED(vf);
161	UNUSED(flags);
162	// TODO
163	return false;
164}
165
166static bool _GBCoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
167	UNUSED(core);
168	UNUSED(vf);
169	UNUSED(flags);
170	// TODO
171	return false;
172}
173
174static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
175	struct GBCore* gbcore = (struct GBCore*) core;
176	gbcore->keys = keys;
177}
178
179static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
180	struct GBCore* gbcore = (struct GBCore*) core;
181	gbcore->keys |= keys;
182}
183
184static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
185	struct GBCore* gbcore = (struct GBCore*) core;
186	gbcore->keys &= ~keys;
187}
188
189static int32_t _GBCoreFrameCounter(struct mCore* core) {
190	struct GB* gb = core->board;
191	return gb->video.frameCounter;
192}
193
194static int32_t _GBCoreFrameCycles(struct mCore* core) {
195	UNUSED(core);
196	return GB_VIDEO_TOTAL_LENGTH;
197}
198
199static int32_t _GBCoreFrequency(struct mCore* core) {
200	UNUSED(core);
201	// TODO: GB differences
202	return DMG_LR35902_FREQUENCY;
203}
204
205static void _GBCoreGetGameTitle(struct mCore* core, char* title) {
206	GBGetGameTitle(core->board, title);
207}
208
209static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
210	struct GB* gb = core->board;
211	gb->memory.rtc = rtc;
212}
213
214struct mCore* GBCoreCreate(void) {
215	struct GBCore* gbcore = malloc(sizeof(*gbcore));
216	struct mCore* core = &gbcore->d;
217	memset(&core->opts, 0, sizeof(core->opts));
218	core->cpu = 0;
219	core->board = 0;
220	core->init = _GBCoreInit;
221	core->deinit = _GBCoreDeinit;
222	core->setSync = _GBCoreSetSync;
223	core->loadConfig = _GBCoreLoadConfig;
224	core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
225	core->setVideoBuffer = _GBCoreSetVideoBuffer;
226	core->getVideoBuffer = _GBCoreGetVideoBuffer;
227	core->getAudioChannel = _GBCoreGetAudioChannel;
228	core->setAVStream = _GBCoreSetAVStream;
229	core->isROM = GBIsROM;
230	core->loadROM = _GBCoreLoadROM;
231	core->loadBIOS = _GBCoreLoadBIOS;
232	core->loadSave = _GBCoreLoadSave;
233	core->loadPatch = _GBCoreLoadPatch;
234	core->unloadROM = _GBCoreUnloadROM;
235	core->reset = _GBCoreReset;
236	core->runFrame = _GBCoreRunFrame;
237	core->runLoop = _GBCoreRunLoop;
238	core->step = _GBCoreStep;
239	core->loadState = _GBCoreLoadState;
240	core->saveState = _GBCoreSaveState;
241	core->setKeys = _GBCoreSetKeys;
242	core->addKeys = _GBCoreAddKeys;
243	core->clearKeys = _GBCoreClearKeys;
244	core->frameCounter = _GBCoreFrameCounter;
245	core->frameCycles = _GBCoreFrameCycles;
246	core->frequency = _GBCoreFrequency;
247	core->getGameTitle = _GBCoreGetGameTitle;
248	core->setRTC = _GBCoreSetRTC;
249	return core;
250}