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 memset(videoBuffer, 0xFF, stride * VIDEO_VERTICAL_PIXELS);
39 struct GBA* gba = anonymousMemoryMap(sizeof(struct GBA));
40 struct ARMCore* cpu = anonymousMemoryMap(sizeof(struct ARMCore));
41 int activeKeys = 0;
42
43 renderer.outputBuffer = videoBuffer;
44 renderer.outputBufferStride = VIDEO_HORIZONTAL_PIXELS;
45
46 gba->keySource = &activeKeys;
47 gba->sync = 0;
48
49 FS_archive sdmcArchive = (FS_archive) {
50 ARCH_SDMC,
51 (FS_path) { PATH_EMPTY, 1, (u8*)"" },
52 0, 0
53 };
54 FSUSER_OpenArchive(0, &sdmcArchive);
55
56 struct VFile* rom = VFileOpen3DS(sdmcArchive, "/rom.gba", FS_OPEN_READ);
57
58 struct VFile* save = VFileOpen3DS(sdmcArchive, "/rom.sav", FS_OPEN_WRITE | FS_OPEN_CREATE);
59
60 GBACreate(gba);
61 ARMSetComponents(cpu, &gba->d, 0, 0);
62 ARMInit(cpu);
63
64 GBAVideoAssociateRenderer(&gba->video, &renderer.d);
65
66 GBALoadROM(gba, rom, save, 0);
67
68 ARMReset(cpu);
69
70 bool inVblank = false;
71 while (aptMainLoop()) {
72 ARMRunLoop(cpu);
73
74 if (!inVblank) {
75 if (GBARegisterDISPSTATIsInVblank(gba->video.dispstat)) {
76 u16 width, height;
77 u8* screen = gfxGetFramebuffer(GFX_BOTTOM, GFX_BOTTOM, &width, &height);
78 memcpy(screen, videoBuffer, stride * VIDEO_VERTICAL_PIXELS);
79 gfxFlushBuffers();
80 gfxSwapBuffersGpu();
81 gspWaitForEvent(GSPEVENT_VBlank0, false);
82 hidScanInput();
83 }
84 }
85 inVblank = GBARegisterDISPSTATGetInVblank(gba->video.dispstat);
86 }
87
88 ARMDeinit(cpu);
89 GBADestroy(gba);
90
91 rom->close(rom);
92 save->close(save);
93
94 mappedMemoryFree(gba, 0);
95 mappedMemoryFree(cpu, 0);
96
97 mappedMemoryFree(videoBuffer, 0);
98
99 fsExit();
100 gfxExit();
101 hidExit();
102 aptExit();
103 srvExit();
104 return 0;
105}