mirror of https://github.com/mgba-emu/mgba.git
3DS: Use GBAContext
This commit is contained in:
parent
2c7926ef66
commit
ca42faae3c
|
@ -3,10 +3,10 @@
|
||||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
#include "gba/gba.h"
|
|
||||||
#include "gba/video.h"
|
|
||||||
|
|
||||||
#include "gba/renderers/video-software.h"
|
#include "gba/renderers/video-software.h"
|
||||||
|
#include "gba/supervisor/context.h"
|
||||||
|
#include "gba/video.h"
|
||||||
#include "util/memory.h"
|
#include "util/memory.h"
|
||||||
|
|
||||||
#include "3ds-vfs.h"
|
#include "3ds-vfs.h"
|
||||||
|
@ -19,6 +19,7 @@ static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const ch
|
||||||
static Handle logFile;
|
static Handle logFile;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
struct GBAContext context;
|
||||||
srvInit();
|
srvInit();
|
||||||
aptInit();
|
aptInit();
|
||||||
hidInit(0);
|
hidInit(0);
|
||||||
|
@ -32,71 +33,51 @@ int main() {
|
||||||
FSUSER_OpenArchive(0, &sdmcArchive);
|
FSUSER_OpenArchive(0, &sdmcArchive);
|
||||||
FSUSER_OpenFile(0, &logFile, sdmcArchive, FS_makePath(PATH_CHAR, "/mgba.log"), FS_OPEN_WRITE | FS_OPEN_CREATE, FS_ATTRIBUTE_NONE);
|
FSUSER_OpenFile(0, &logFile, sdmcArchive, FS_makePath(PATH_CHAR, "/mgba.log"), FS_OPEN_WRITE | FS_OPEN_CREATE, FS_ATTRIBUTE_NONE);
|
||||||
|
|
||||||
|
GBAContextInit(&context, 0);
|
||||||
|
struct GBAOptions opts = {
|
||||||
|
.useBios = true,
|
||||||
|
.logLevel = 0,
|
||||||
|
.idleOptimization = IDLE_LOOP_REMOVE
|
||||||
|
};
|
||||||
|
GBAConfigLoadDefaults(&context.config, &opts);
|
||||||
|
context.gba->logHandler = GBA3DSLog;
|
||||||
|
|
||||||
struct GBAVideoSoftwareRenderer renderer;
|
struct GBAVideoSoftwareRenderer renderer;
|
||||||
GBAVideoSoftwareRendererCreate(&renderer);
|
GBAVideoSoftwareRendererCreate(&renderer);
|
||||||
|
|
||||||
size_t stride = VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL;
|
size_t stride = VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL;
|
||||||
color_t* videoBuffer = anonymousMemoryMap(stride * VIDEO_VERTICAL_PIXELS);
|
color_t* videoBuffer = anonymousMemoryMap(stride * VIDEO_VERTICAL_PIXELS);
|
||||||
struct GBA* gba = anonymousMemoryMap(sizeof(struct GBA));
|
|
||||||
struct ARMCore* cpu = anonymousMemoryMap(sizeof(struct ARMCore));
|
|
||||||
int activeKeys = 0;
|
|
||||||
|
|
||||||
renderer.outputBuffer = videoBuffer;
|
renderer.outputBuffer = videoBuffer;
|
||||||
renderer.outputBufferStride = VIDEO_HORIZONTAL_PIXELS;
|
renderer.outputBufferStride = VIDEO_HORIZONTAL_PIXELS;
|
||||||
|
GBAVideoAssociateRenderer(&context.gba->video, &renderer.d);
|
||||||
|
|
||||||
struct VFile* rom = VFileOpen3DS(&sdmcArchive, "/rom.gba", FS_OPEN_READ);
|
GBAContextLoadROM(&context, "/rom.gba", true);
|
||||||
struct VFile* save = VFileOpen3DS(&sdmcArchive, "/rom.sav", FS_OPEN_READ | FS_OPEN_WRITE);
|
GBAContextStart(&context);
|
||||||
|
|
||||||
GBACreate(gba);
|
|
||||||
ARMSetComponents(cpu, &gba->d, 0, 0);
|
|
||||||
ARMInit(cpu);
|
|
||||||
|
|
||||||
gba->keySource = &activeKeys;
|
|
||||||
gba->sync = 0;
|
|
||||||
|
|
||||||
GBAVideoAssociateRenderer(&gba->video, &renderer.d);
|
|
||||||
|
|
||||||
GBALoadROM(gba, rom, save, 0);
|
|
||||||
|
|
||||||
gba->logHandler = GBA3DSLog;
|
|
||||||
|
|
||||||
ARMReset(cpu);
|
|
||||||
|
|
||||||
int frameCounter = 0;
|
|
||||||
while (aptMainLoop()) {
|
while (aptMainLoop()) {
|
||||||
ARMRunLoop(cpu);
|
hidScanInput();
|
||||||
|
int activeKeys = hidKeysHeld() & 0x3FF;
|
||||||
if (frameCounter != gba->video.frameCounter) {
|
if (hidKeysDown() & KEY_X) {
|
||||||
u16 width, height;
|
break;
|
||||||
u16* screen = (u16*) gfxGetFramebuffer(GFX_BOTTOM, GFX_BOTTOM, &height, &width);
|
|
||||||
u32 startX = (width - VIDEO_HORIZONTAL_PIXELS) / 2;
|
|
||||||
u32 startY = (height + VIDEO_VERTICAL_PIXELS) / 2 - 1;
|
|
||||||
u32 x, y;
|
|
||||||
for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
|
|
||||||
for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
|
|
||||||
screen[startY - y + (startX + x) * height] = videoBuffer[y * VIDEO_HORIZONTAL_PIXELS + x];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
gfxFlushBuffers();
|
|
||||||
gfxSwapBuffers();
|
|
||||||
gspWaitForVBlank1();
|
|
||||||
hidScanInput();
|
|
||||||
activeKeys = hidKeysHeld() & 0x3FF;
|
|
||||||
if (hidKeysDown() & KEY_X) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
frameCounter = gba->video.frameCounter;
|
|
||||||
}
|
}
|
||||||
|
GBAContextFrame(&context, activeKeys);
|
||||||
|
|
||||||
|
u16 width, height;
|
||||||
|
u16* screen = (u16*) gfxGetFramebuffer(GFX_BOTTOM, GFX_BOTTOM, &height, &width);
|
||||||
|
u32 startX = (width - VIDEO_HORIZONTAL_PIXELS) / 2;
|
||||||
|
u32 startY = (height + VIDEO_VERTICAL_PIXELS) / 2 - 1;
|
||||||
|
u32 x, y;
|
||||||
|
for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
|
||||||
|
for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
|
||||||
|
screen[startY - y + (startX + x) * height] = videoBuffer[y * VIDEO_HORIZONTAL_PIXELS + x];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gfxFlushBuffers();
|
||||||
|
gfxSwapBuffers();
|
||||||
|
gspWaitForVBlank1();
|
||||||
}
|
}
|
||||||
|
|
||||||
ARMDeinit(cpu);
|
GBAContextStop(&context);
|
||||||
GBADestroy(gba);
|
GBAContextDeinit(&context);
|
||||||
|
|
||||||
rom->close(rom);
|
|
||||||
save->close(save);
|
|
||||||
|
|
||||||
mappedMemoryFree(gba, 0);
|
|
||||||
mappedMemoryFree(cpu, 0);
|
|
||||||
|
|
||||||
mappedMemoryFree(videoBuffer, 0);
|
mappedMemoryFree(videoBuffer, 0);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue