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
21PSP2_MODULE_INFO(0, 0, "mGBA");
22
23#define PSP2_HORIZONTAL_PIXELS 960
24#define PSP2_VERTICAL_PIXELS 544
25
26static void allocFramebuffer(SceDisplayFrameBuf* fb, int nfbs, SceUID* memblock) {
27 size_t baseSize = 0x200000;
28 size_t size = baseSize * nfbs;
29 *memblock = sceKernelAllocMemBlock("fb", SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW, size, 0);
30 sceKernelGetMemBlockBase(*memblock, &fb[0].base);
31 sceGxmMapMemory(fb[0].base, size, SCE_GXM_MEMORY_ATTRIB_RW);
32
33 int i;
34 for (i = 0; i < nfbs; ++i) {
35 fb[i].size = sizeof(fb[i]);
36 fb[i].pitch = PSP2_HORIZONTAL_PIXELS;
37 fb[i].width = PSP2_HORIZONTAL_PIXELS;
38 fb[i].height = PSP2_VERTICAL_PIXELS;
39 fb[i].pixelformat = PSP2_DISPLAY_PIXELFORMAT_A8B8G8R8;
40 fb[i].base = (char*) fb[0].base + i * baseSize;
41 }
42}
43
44int main() {
45 printf("%s initializing", projectName);
46 bool running = true;
47
48 struct GBAVideoSoftwareRenderer renderer;
49 GBAVideoSoftwareRendererCreate(&renderer);
50
51 struct GBA* gba = anonymousMemoryMap(sizeof(struct GBA));
52 struct ARMCore* cpu = anonymousMemoryMap(sizeof(struct ARMCore));
53
54 printf("GBA: %08X", gba);
55 printf("CPU: %08X", cpu);
56 int activeKeys = 0;
57
58 SceGxmInitializeParams gxmParams;
59 gxmParams.flags = 0;
60 gxmParams.displayQueueMaxPendingCount = 2;
61 gxmParams.displayQueueCallback = 0;
62 gxmParams.displayQueueCallbackDataSize = 0;
63 gxmParams.parameterBufferSize = 0x1000000;
64 int ret = sceGxmInitialize(&gxmParams);
65 printf("sceGxmInitialize: %08X", ret);
66
67 SceDisplayFrameBuf fb[2];
68 int currentFb = 0;
69 SceUID memblock;
70 allocFramebuffer(fb, 2, &memblock);
71 printf("fb[0]: %08X", fb[0].base);
72 printf("fb[1]: %08X", fb[1].base);
73
74 renderer.outputBuffer = fb[0].base;
75 renderer.outputBufferStride = PSP2_HORIZONTAL_PIXELS;
76
77 struct VFile* rom = VFileOpenSce("cache0:/VitaDefilerClient/Documents/GBA/rom.gba", PSP2_O_RDONLY, 0666);
78 struct VFile* save = VFileOpenSce("cache0:/VitaDefilerClient/Documents/GBA/rom.sav", PSP2_O_RDWR | PSP2_O_CREAT, 0666);
79
80 printf("ROM: %08X", rom);
81 printf("Save: %08X", save);
82
83 GBACreate(gba);
84 ARMSetComponents(cpu, &gba->d, 0, 0);
85 ARMInit(cpu);
86 printf("%s initialized.", "CPU");
87
88 gba->keySource = &activeKeys;
89 gba->sync = 0;
90
91 GBAVideoAssociateRenderer(&gba->video, &renderer.d);
92
93 GBALoadROM(gba, rom, save, 0);
94 printf("%s loaded.", "ROM");
95
96 ARMReset(cpu);
97
98 printf("%s all set and ready to roll.", projectName);
99
100 int frameCounter = 0;
101 while (running) {
102 ARMRunLoop(cpu);
103
104 if (frameCounter != gba->video.frameCounter) {
105 SceCtrlData pad;
106 sceCtrlPeekBufferPositive(0, &pad, 1);
107 activeKeys = 0;
108 if (pad.buttons & PSP2_CTRL_CROSS) {
109 activeKeys |= 1 << GBA_KEY_A;
110 }
111 if (pad.buttons & PSP2_CTRL_CIRCLE) {
112 activeKeys |= 1 << GBA_KEY_B;
113 }
114 if (pad.buttons & PSP2_CTRL_START) {
115 activeKeys |= 1 << GBA_KEY_START;
116 }
117 if (pad.buttons & PSP2_CTRL_SELECT) {
118 activeKeys |= 1 << GBA_KEY_SELECT;
119 }
120 if (pad.buttons & PSP2_CTRL_UP) {
121 activeKeys |= 1 << GBA_KEY_UP;
122 }
123 if (pad.buttons & PSP2_CTRL_DOWN) {
124 activeKeys |= 1 << GBA_KEY_DOWN;
125 }
126 if (pad.buttons & PSP2_CTRL_LEFT) {
127 activeKeys |= 1 << GBA_KEY_LEFT;
128 }
129 if (pad.buttons & PSP2_CTRL_RIGHT) {
130 activeKeys |= 1 << GBA_KEY_RIGHT;
131 }
132 if (pad.buttons & PSP2_CTRL_LTRIGGER) {
133 activeKeys |= 1 << GBA_KEY_L;
134 }
135 if (pad.buttons & PSP2_CTRL_RTRIGGER) {
136 activeKeys |= 1 << GBA_KEY_R;
137 }
138
139 sceDisplaySetFrameBuf(&fb[currentFb], PSP2_DISPLAY_SETBUF_NEXTFRAME);
140 sceDisplayWaitVblankStart();
141 currentFb = !currentFb;
142 renderer.outputBuffer = fb[currentFb].base;
143
144 frameCounter = gba->video.frameCounter;
145 }
146 }
147 printf("%s shutting down...", projectName);
148
149 ARMDeinit(cpu);
150 GBADestroy(gba);
151
152 rom->close(rom);
153 save->close(save);
154
155 mappedMemoryFree(gba, 0);
156 mappedMemoryFree(cpu, 0);
157 return 0;
158}