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