all repos — mgba @ 127a2d0820f5d09523d71af41f903c731fff9d04

mGBA Game Boy Advance Emulator

src/platform/psp2/main.c (view raw)

  1/* Copyright (c) 2013-2015 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/gba.h"
  7#include "gba/video.h"
  8
  9#include "gba/renderers/video-software.h"
 10#include "util/memory.h"
 11#include "util/vfs.h"
 12#include "platform/psp2/sce-vfs.h"
 13
 14#include <psp2/ctrl.h>
 15#include <psp2/display.h>
 16#include <psp2/gxm.h>
 17#include <psp2/moduleinfo.h>
 18#include <psp2/kernel/memorymgr.h>
 19#include <psp2/kernel/processmgr.h>
 20
 21#include <vita2d.h>
 22
 23PSP2_MODULE_INFO(0, 0, "mGBA");
 24
 25#define PSP2_HORIZONTAL_PIXELS 960
 26#define PSP2_VERTICAL_PIXELS 544
 27
 28int main() {
 29	printf("%s initializing", projectName);
 30	bool running = true;
 31
 32	struct GBAVideoSoftwareRenderer renderer;
 33	GBAVideoSoftwareRendererCreate(&renderer);
 34
 35	struct GBA* gba = anonymousMemoryMap(sizeof(struct GBA));
 36	struct ARMCore* cpu = anonymousMemoryMap(sizeof(struct ARMCore));
 37
 38	printf("GBA: %08X", gba);
 39	printf("CPU: %08X", cpu);
 40	int activeKeys = 0;
 41
 42	vita2d_init();
 43	vita2d_texture* tex = vita2d_create_empty_texture(256, 256);
 44
 45	renderer.outputBuffer = vita2d_texture_get_datap(tex);
 46	renderer.outputBufferStride = 256;
 47
 48	struct VFile* rom = VFileOpenSce("cache0:/VitaDefilerClient/Documents/GBA/rom.gba", PSP2_O_RDONLY, 0666);
 49	struct VFile* save = VFileOpenSce("cache0:/VitaDefilerClient/Documents/GBA/rom.sav", PSP2_O_RDWR | PSP2_O_CREAT, 0666);
 50
 51	printf("ROM: %08X", rom);
 52	printf("Save: %08X", save);
 53
 54	GBACreate(gba);
 55	ARMSetComponents(cpu, &gba->d, 0, 0);
 56	ARMInit(cpu);
 57	printf("%s initialized.", "CPU");
 58
 59	gba->keySource = &activeKeys;
 60	gba->sync = 0;
 61
 62	GBAVideoAssociateRenderer(&gba->video, &renderer.d);
 63
 64	GBALoadROM(gba, rom, save, 0);
 65	printf("%s loaded.", "ROM");
 66
 67	ARMReset(cpu);
 68
 69	printf("%s all set and ready to roll.", projectName);
 70
 71	int frameCounter = 0;
 72	while (running) {
 73		ARMRunLoop(cpu);
 74
 75		if (frameCounter != gba->video.frameCounter) {
 76			SceCtrlData pad;
 77			sceCtrlPeekBufferPositive(0, &pad, 1);
 78			activeKeys = 0;
 79			if (pad.buttons & PSP2_CTRL_CROSS) {
 80				activeKeys |= 1 << GBA_KEY_A;
 81			}
 82			if (pad.buttons & PSP2_CTRL_CIRCLE) {
 83				activeKeys |= 1 << GBA_KEY_B;
 84			}
 85			if (pad.buttons & PSP2_CTRL_START) {
 86				activeKeys |= 1 << GBA_KEY_START;
 87			}
 88			if (pad.buttons & PSP2_CTRL_SELECT) {
 89				activeKeys |= 1 << GBA_KEY_SELECT;
 90			}
 91			if (pad.buttons & PSP2_CTRL_UP) {
 92				activeKeys |= 1 << GBA_KEY_UP;
 93			}
 94			if (pad.buttons & PSP2_CTRL_DOWN) {
 95				activeKeys |= 1 << GBA_KEY_DOWN;
 96			}
 97			if (pad.buttons & PSP2_CTRL_LEFT) {
 98				activeKeys |= 1 << GBA_KEY_LEFT;
 99			}
100			if (pad.buttons & PSP2_CTRL_RIGHT) {
101				activeKeys |= 1 << GBA_KEY_RIGHT;
102			}
103			if (pad.buttons & PSP2_CTRL_LTRIGGER) {
104				activeKeys |= 1 << GBA_KEY_L;
105			}
106			if (pad.buttons & PSP2_CTRL_RTRIGGER) {
107				activeKeys |= 1 << GBA_KEY_R;
108			}
109
110			vita2d_start_drawing();
111			vita2d_clear_screen();
112			vita2d_draw_texture_scale(tex, 120, 32, 3.0f, 3.0f);
113			vita2d_end_drawing();
114			vita2d_swap_buffers();
115
116			frameCounter = gba->video.frameCounter;
117		}
118	}
119	printf("%s shutting down...", projectName);
120
121	ARMDeinit(cpu);
122	GBADestroy(gba);
123
124	rom->close(rom);
125	save->close(save);
126
127	mappedMemoryFree(gba, 0);
128	mappedMemoryFree(cpu, 0);
129
130	vita2d_fini();
131	vita2d_free_texture(tex);
132
133	sceKernelExitProcess(0);
134	return 0;
135}