all repos — mgba @ 40d14fe6133b8df8f75be1e9f78a5e4bc7920a92

mGBA Game Boy Advance Emulator

src/platform/3ds/main.c (view raw)

  1/* Copyright (c) 2013-2014 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 "gba.h"
  7#include "gba-video.h"
  8
  9#include "renderers/video-software.h"
 10#include "util/memory.h"
 11
 12#include "3ds-vfs.h"
 13
 14#include <3ds.h>
 15
 16int main() {
 17	srvInit();
 18	aptInit();
 19	hidInit(0);
 20	gfxInit();
 21	fsInit();
 22
 23#ifdef COLOR_16_BIT
 24#ifdef COLOR_5_6_5
 25	gfxSetScreenFormat(GFX_BOTTOM, GSP_RGB565_OES);
 26#else
 27	gfxSetScreenFormat(GFX_BOTTOM, GSP_RGB5_A1_OES);
 28#endif
 29#else
 30	gfxSetScreenFormat(GFX_BOTTOM, GSP_RGBA8_OES);
 31#endif
 32
 33	struct GBAVideoSoftwareRenderer renderer;
 34	GBAVideoSoftwareRendererCreate(&renderer);
 35
 36	size_t stride = VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL;
 37	color_t* videoBuffer = anonymousMemoryMap(stride * VIDEO_VERTICAL_PIXELS);
 38	struct GBA* gba = anonymousMemoryMap(sizeof(struct GBA));
 39	struct ARMCore* cpu = anonymousMemoryMap(sizeof(struct ARMCore));
 40	int activeKeys = 0;
 41
 42	renderer.outputBuffer = videoBuffer;
 43	renderer.outputBufferStride = VIDEO_HORIZONTAL_PIXELS;
 44
 45	gba->keySource = &activeKeys;
 46	gba->sync = 0;
 47
 48	FS_archive sdmcArchive = (FS_archive) {
 49		ARCH_SDMC,
 50		(FS_path) { PATH_EMPTY, 1, (u8*)"" },
 51		0, 0
 52	};
 53	FSUSER_OpenArchive(0, &sdmcArchive);
 54
 55	struct VFile* rom = VFileOpen3DS(sdmcArchive, "/rom.gba", FS_OPEN_READ);
 56
 57	struct VFile* save = VFileOpen3DS(sdmcArchive, "/rom.sav", FS_OPEN_WRITE | FS_OPEN_CREATE);
 58
 59	GBACreate(gba);
 60	ARMSetComponents(cpu, &gba->d, 0, 0);
 61	ARMInit(cpu);
 62
 63	GBAVideoAssociateRenderer(&gba->video, &renderer.d);
 64
 65	GBALoadROM(gba, rom, save, 0);
 66
 67	ARMReset(cpu);
 68
 69	bool inVblank = false;
 70	while (aptMainLoop()) {
 71		ARMRunLoop(cpu);
 72
 73		if (!inVblank) {
 74			if (GBARegisterDISPSTATIsInVblank(gba->video.dispstat)) {
 75				GX_RequestDma(0, (u32*) videoBuffer, (u32*) gfxGetFramebuffer(GFX_BOTTOM, GFX_BOTTOM, 0, 0), stride * VIDEO_VERTICAL_PIXELS);
 76				gfxFlushBuffers();
 77				gfxSwapBuffersGpu();
 78				gspWaitForVBlank();
 79				hidScanInput();
 80			}
 81		}
 82		inVblank = GBARegisterDISPSTATGetInVblank(gba->video.dispstat);
 83	}
 84
 85	ARMDeinit(cpu);
 86	GBADestroy(gba);
 87
 88	rom->close(rom);
 89	save->close(save);
 90
 91	mappedMemoryFree(gba, 0);
 92	mappedMemoryFree(cpu, 0);
 93
 94	mappedMemoryFree(videoBuffer, 0);
 95
 96	fsExit();
 97	gfxExit();
 98	hidExit();
 99	aptExit();
100	srvExit();
101	return 0;
102}