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/processmgr.h>
19#include <psp2/kernel/sysmem.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_format(256, 256, SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
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 if (pad.buttons & PSP2_CTRL_TRIANGLE) {
79 running = false;
80 break;
81 }
82
83 activeKeys = 0;
84 if (pad.buttons & PSP2_CTRL_CROSS) {
85 activeKeys |= 1 << GBA_KEY_A;
86 }
87 if (pad.buttons & PSP2_CTRL_CIRCLE) {
88 activeKeys |= 1 << GBA_KEY_B;
89 }
90 if (pad.buttons & PSP2_CTRL_START) {
91 activeKeys |= 1 << GBA_KEY_START;
92 }
93 if (pad.buttons & PSP2_CTRL_SELECT) {
94 activeKeys |= 1 << GBA_KEY_SELECT;
95 }
96 if (pad.buttons & PSP2_CTRL_UP) {
97 activeKeys |= 1 << GBA_KEY_UP;
98 }
99 if (pad.buttons & PSP2_CTRL_DOWN) {
100 activeKeys |= 1 << GBA_KEY_DOWN;
101 }
102 if (pad.buttons & PSP2_CTRL_LEFT) {
103 activeKeys |= 1 << GBA_KEY_LEFT;
104 }
105 if (pad.buttons & PSP2_CTRL_RIGHT) {
106 activeKeys |= 1 << GBA_KEY_RIGHT;
107 }
108 if (pad.buttons & PSP2_CTRL_LTRIGGER) {
109 activeKeys |= 1 << GBA_KEY_L;
110 }
111 if (pad.buttons & PSP2_CTRL_RTRIGGER) {
112 activeKeys |= 1 << GBA_KEY_R;
113 }
114
115 vita2d_start_drawing();
116 vita2d_clear_screen();
117 vita2d_draw_texture_scale(tex, 120, 32, 3.0f, 3.0f);
118 vita2d_end_drawing();
119 vita2d_swap_buffers();
120
121 frameCounter = gba->video.frameCounter;
122 }
123 }
124 printf("%s shutting down...", projectName);
125
126 ARMDeinit(cpu);
127 GBADestroy(gba);
128
129 rom->close(rom);
130 save->close(save);
131
132 mappedMemoryFree(gba, 0);
133 mappedMemoryFree(cpu, 0);
134
135 vita2d_fini();
136 vita2d_free_texture(tex);
137
138 sceKernelExitProcess(0);
139 return 0;
140}