forked from beefviper/hode-vs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
system_psp.cpp
429 lines (361 loc) · 11.8 KB
/
system_psp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include <pspaudio.h>
#include <pspaudiolib.h>
#include <pspctrl.h>
#include <pspdisplay.h>
#include <pspgu.h>
#include <pspkernel.h>
#include <pspsdk.h>
#include "system.h"
struct System_PSP : System {
uint32_t _buttons;
int _shakeDx, _shakeDy;
bool _stretchScreen;
AudioCallback _audioCb;
int _audioChannel;
SceUID _audioMutex;
uint32_t _vramOffset;
System_PSP();
virtual ~System_PSP();
virtual void init(const char *title, int w, int h, bool fullscreen, bool widescreen, bool yuv);
virtual void destroy();
virtual void setScaler(const char *name, int multiplier);
virtual void setGamma(float gamma);
virtual void setPalette(const uint8_t *pal, int n, int depth);
virtual void clearPalette();
virtual void copyRect(int x, int y, int w, int h, const uint8_t *buf, int pitch);
virtual void copyYuv(int w, int h, const uint8_t *y, int ypitch, const uint8_t *u, int upitch, const uint8_t *v, int vpitch);
virtual void fillRect(int x, int y, int w, int h, uint8_t color);
virtual void copyRectWidescreen(int w, int h, const uint8_t *buf, const uint8_t *pal);
virtual void shakeScreen(int dx, int dy);
virtual void updateScreen(bool drawWidescreen);
virtual void processEvents();
virtual void sleep(int duration);
virtual uint32_t getTimeStamp();
virtual void startAudio(AudioCallback callback);
virtual void stopAudio();
virtual void lockAudio();
virtual void unlockAudio();
virtual AudioCallback setAudioCallback(AudioCallback callback);
};
static System_PSP system_psp;
System *const g_system = &system_psp;
// static const int AUDIO_FREQ = 44100;
static const int AUDIO_SAMPLES_COUNT = 2048;
static const int SCREEN_W = 480;
static const int SCREEN_H = 272;
static const int SCREEN_PITCH = 512;
static const int GAME_W = 256;
static const int GAME_H = 192;
static const int BLUR_TEX_W = 16;
static const int BLUR_TEX_H = 16;
static uint32_t WHITE_COLOR = GU_RGBA(255, 255, 255, 255);
static uint32_t __attribute__((aligned(16))) _dlist[1024];
static uint32_t __attribute__((aligned(16))) _clut[256];
static uint8_t __attribute__((aligned(16))) _texture[256 * 256];
static uint32_t __attribute__((aligned(16))) _clut2[256];
static uint8_t __attribute__((aligned(16))) _texture2[256 * 256];
PSP_MODULE_INFO("Heart of Darkness", 0, 2, 9);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER);
struct Vertex {
uint16_t u, v;
int16_t x, y, z;
};
static void initVertex2D(Vertex *vertices, int w, int h, int targetW, int targetH) {
vertices[0].u = 0; vertices[1].u = w;
vertices[0].v = 0; vertices[1].v = h;
vertices[0].x = 0; vertices[1].x = targetW;
vertices[0].y = 0; vertices[1].y = targetH;
vertices[0].z = 0; vertices[1].z = 0;
}
void System_printLog(FILE *fp, const char *s) {
if (fp == stderr) {
static bool firstOpen = false;
if (!firstOpen) {
fp = fopen("stderr.txt", "w");
firstOpen = true;
} else {
fp = fopen("stderr.txt", "a");
}
} else if (fp == stdout) {
static bool firstOpen = false;
if (!firstOpen) {
fp = fopen("stdout.txt", "w");
firstOpen = true;
} else {
fp = fopen("stdout.txt", "a");
}
} else {
return;
}
fprintf(fp, "%s\n", s);
fclose(fp);
}
void System_fatalError(const char *s) {
sceKernelExitGame();
}
bool System_hasCommandLine() {
return false;
}
static int exitCallback(int arg1, int arg2, void *common) {
g_system->inp.quit = true;
return 0;
}
static int callbackThread(SceSize args, void *argp) {
const int cb = sceKernelCreateCallback("Exit Callback", exitCallback, NULL);
sceKernelRegisterExitCallback(cb);
sceKernelSleepThreadCB();
return 0;
}
System_PSP::System_PSP() {
}
System_PSP::~System_PSP() {
}
void System_PSP::init(const char *title, int w, int h, bool fullscreen, bool widescreen, bool yuv) {
memset(&inp, 0, sizeof(inp));
memset(&pad, 0, sizeof(pad));
_buttons = 0;
_shakeDx = _shakeDy = 0;
_stretchScreen = false; // keep 4:3 AR
memset(&_audioCb, 0, sizeof(_audioCb));
_audioChannel = -1;
_audioMutex = 0;
const int th = sceKernelCreateThread("update_thread", callbackThread, 0x11, 0xFA0, 0, 0);
if (th >= 0) {
sceKernelStartThread(th, 0, 0);
}
sceKernelDcacheWritebackAll();
sceCtrlSetSamplingCycle(0);
sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
sceGuInit();
sceGuStart(GU_DIRECT, _dlist);
const int fbSize = SCREEN_PITCH * SCREEN_H * sizeof(uint32_t); // rgba
const int zbSize = SCREEN_PITCH * SCREEN_H * sizeof(uint16_t); // 16 bits
_vramOffset = 0;
sceGuDrawBuffer(GU_PSM_8888, (void *)_vramOffset, SCREEN_PITCH); _vramOffset += fbSize;
sceGuDispBuffer(SCREEN_W, SCREEN_H, (void *)_vramOffset, SCREEN_PITCH); _vramOffset += fbSize;
sceGuDepthBuffer((void *)_vramOffset, SCREEN_PITCH); _vramOffset += zbSize;
sceGuOffset(2048 - (SCREEN_W / 2), 2048 - (SCREEN_H / 2));
sceGuViewport(2048, 2048, SCREEN_W, SCREEN_H);
sceGuScissor(0, 0, SCREEN_W, SCREEN_H);
sceGuEnable(GU_SCISSOR_TEST);
sceGuEnable(GU_TEXTURE_2D);
sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT);
sceGuFinish();
sceGuSync(GU_SYNC_WHAT_DONE, GU_SYNC_FINISH);
sceDisplayWaitVblankStart();
sceGuDisplay(GU_TRUE);
}
void System_PSP::destroy() {
sceGuTerm();
sceKernelExitGame();
}
void System_PSP::setScaler(const char *name, int multiplier) {
}
void System_PSP::setGamma(float gamma) {
}
void System_PSP::setPalette(const uint8_t *pal, int n, int depth) {
const int shift = 8 - depth;
for (int i = 0; i < n; ++i) {
int r = pal[i * 3];
int g = pal[i * 3 + 1];
int b = pal[i * 3 + 2];
if (shift != 0) {
r = (r << shift) | (r >> depth);
g = (g << shift) | (g >> depth);
b = (b << shift) | (b >> depth);
}
_clut[i] = GU_RGBA(r, g, b, 255);
}
sceKernelDcacheWritebackRange(_clut, sizeof(_clut));
}
void System_PSP::clearPalette() {
for (int i = 0; i < 256; ++i) {
_clut[i] = GU_RGBA(0, 0, 0, 255);
}
sceKernelDcacheWritebackRange(_clut, sizeof(_clut));
}
void System_PSP::copyRect(int x, int y, int w, int h, const uint8_t *buf, int pitch) {
uint8_t *p = _texture + y * GAME_W + x;
if (w == GAME_W && w == pitch) {
memcpy(p, buf, w * h);
} else {
for (int i = 0; i < h; ++i) {
memcpy(p, buf, w);
p += GAME_W;
buf += pitch;
}
}
sceKernelDcacheWritebackRange(_texture, sizeof(_texture));
}
void System_PSP::copyYuv(int w, int h, const uint8_t *y, int ypitch, const uint8_t *u, int upitch, const uint8_t *v, int vpitch) {
}
void System_PSP::fillRect(int x, int y, int w, int h, uint8_t color) {
uint8_t *p = _texture + y * GAME_W + x;
if (w == GAME_W) {
memset(p, color, w * h);
} else {
for (int i = 0; i < h; ++i) {
memset(p, color, w);
p += GAME_W;
}
}
sceKernelDcacheWritebackRange(_texture, sizeof(_texture));
}
void System_PSP::copyRectWidescreen(int w, int h, const uint8_t *buf, const uint8_t *pal) {
for (int i = 0; i < 256; ++i) {
_clut2[i] = GU_RGBA(pal[i * 3], pal[i * 3 + 1], pal[i * 3 + 2], 255);
}
sceKernelDcacheWritebackRange(_clut2, sizeof(_clut2));
memcpy(_texture2, buf, w * h);
sceKernelDcacheWritebackRange(_texture2, sizeof(_texture2));
sceGuStart(GU_DIRECT, _dlist);
sceGuDrawBufferList(GU_PSM_8888, (void *)_vramOffset, BLUR_TEX_W);
sceGuOffset(2048 - (BLUR_TEX_W / 2), 2048 - (BLUR_TEX_H / 2));
sceGuViewport(2048, 2048, BLUR_TEX_W, BLUR_TEX_H);
sceGuClearColor(WHITE_COLOR);
sceGuClear(GU_COLOR_BUFFER_BIT);
sceGuClutMode(GU_PSM_8888, 0, 0xFF, 0);
sceGuClutLoad(256 / 8, _clut2);
sceGuTexMode(GU_PSM_T8, 0, 0, 0);
sceGuTexImage(0, 256, 256, 256, _texture2);
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
sceGuTexFilter(GU_LINEAR, GU_LINEAR);
Vertex *vertices = (Vertex *)sceGuGetMemory(2 * sizeof(struct Vertex));
initVertex2D(vertices, GAME_W, GAME_H, BLUR_TEX_W, BLUR_TEX_H);
sceGuDrawArray(GU_SPRITES, GU_TEXTURE_16BIT | GU_VERTEX_16BIT | GU_TRANSFORM_2D, 2, 0, vertices);
sceGuFinish();
}
void System_PSP::shakeScreen(int dx, int dy) {
_shakeDx = dx;
_shakeDy = dy;
}
void System_PSP::updateScreen(bool drawWidescreen) {
sceGuStart(GU_DIRECT, _dlist);
sceGuClearColor(0);
sceGuClear(GU_COLOR_BUFFER_BIT);
if (!_stretchScreen && drawWidescreen) {
sceGuTexMode(GU_PSM_8888, 0, 0, 0);
sceGuTexImage(0, BLUR_TEX_W, BLUR_TEX_H, BLUR_TEX_W, (uint8_t *)sceGeEdramGetAddr() + _vramOffset);
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
sceGuTexFilter(GU_LINEAR, GU_LINEAR);
Vertex *vertices = (Vertex *)sceGuGetMemory(2 * sizeof(Vertex));
initVertex2D(vertices, BLUR_TEX_W, BLUR_TEX_H, SCREEN_W, SCREEN_H);
sceGuDrawArray(GU_SPRITES, GU_TEXTURE_16BIT | GU_VERTEX_16BIT | GU_TRANSFORM_2D, 2, 0, vertices);
}
sceGuClutMode(GU_PSM_8888, 0, 0xFF, 0);
sceGuClutLoad(256 / 8, _clut);
sceGuTexMode(GU_PSM_T8, 0, 0, 0);
sceGuTexImage(0, 256, 256, 256, _texture);
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
sceGuTexFilter(GU_LINEAR, GU_LINEAR);
Vertex *vertices = (Vertex *)sceGuGetMemory(2 * sizeof(Vertex));
initVertex2D(vertices, GAME_W, GAME_H, SCREEN_W, SCREEN_H);
vertices[0].x += _shakeDx;
vertices[1].x += _shakeDx;
vertices[0].y += _shakeDy;
vertices[1].y += _shakeDy;
if (!_stretchScreen) {
const int w = (SCREEN_H * GAME_W / GAME_H);
const int dx = (SCREEN_W - w) / 2;
vertices[0].x += dx;
vertices[1].x -= dx;
}
sceGuDrawArray(GU_SPRITES, GU_TEXTURE_16BIT | GU_VERTEX_16BIT | GU_TRANSFORM_2D, 2, 0, vertices);
sceGuFinish();
sceGuSync(GU_SYNC_WHAT_DONE, GU_SYNC_FINISH);
sceDisplayWaitVblankStart();
sceGuSwapBuffers();
}
void System_PSP::processEvents() {
inp.prevMask = inp.mask;
inp.mask = 0;
static const struct {
int psp;
int sys;
} mapping[] = {
{ PSP_CTRL_UP, SYS_INP_UP },
{ PSP_CTRL_RIGHT, SYS_INP_RIGHT },
{ PSP_CTRL_DOWN, SYS_INP_DOWN },
{ PSP_CTRL_LEFT, SYS_INP_LEFT },
{ PSP_CTRL_CROSS, SYS_INP_JUMP },
{ PSP_CTRL_SQUARE, SYS_INP_RUN },
{ PSP_CTRL_CIRCLE, SYS_INP_SHOOT },
{ PSP_CTRL_TRIANGLE, SYS_INP_SHOOT | SYS_INP_RUN },
{ PSP_CTRL_START, SYS_INP_ESC },
{ 0, 0 }
};
SceCtrlData data;
sceCtrlPeekBufferPositive(&data, 1);
for (int i = 0; mapping[i].psp != 0; ++i) {
if (data.Buttons & mapping[i].psp) {
inp.mask |= mapping[i].sys;
}
}
static const int lxMargin = 64;
if (data.Lx < 127 - lxMargin) {
inp.mask |= SYS_INP_LEFT;
} else if (data.Lx > 127 + lxMargin) {
inp.mask |= SYS_INP_RIGHT;
}
static const int lyMargin = 64;
if (data.Ly < 127 - lyMargin) {
inp.mask |= SYS_INP_UP;
} else if (data.Ly > 127 + lyMargin) {
inp.mask |= SYS_INP_DOWN;
}
const uint32_t mask = data.Buttons ^ _buttons;
if ((data.Buttons & PSP_CTRL_LTRIGGER) & mask) {
_stretchScreen = !_stretchScreen;
}
if ((data.Buttons & PSP_CTRL_RTRIGGER) & mask) {
}
_buttons = data.Buttons;
}
void System_PSP::sleep(int duration) {
sceKernelDelayThread(duration * 1000);
}
uint32_t System_PSP::getTimeStamp() {
struct timeval tv;
sceKernelLibcGettimeofday(&tv, 0);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
static void audioCallback(void *buf, unsigned int samples, void *userdata) { // 44100hz S16 stereo
int16_t buf22khz[samples];
memset(buf22khz, 0, sizeof(buf22khz));
system_psp.lockAudio();
(system_psp._audioCb.proc)(system_psp._audioCb.userdata, buf22khz, samples);
system_psp.unlockAudio();
uint32_t *buf44khz = (uint32_t *)buf;
static int16_t prev;
for (unsigned int i = 0; i < samples; ++i) {
const int16_t current = buf22khz[i];
buf44khz[i] = (current << 16) | (((prev + current) >> 1) & 0xFFFF);
prev = current;
}
}
void System_PSP::startAudio(AudioCallback callback) {
// sceAudioSetFrequency(AUDIO_FREQ);
pspAudioInit();
_audioCb = callback;
_audioMutex = sceKernelCreateSema("audio_lock", 0, 1, 1, 0);
_audioChannel = sceAudioChReserve(PSP_AUDIO_NEXT_CHANNEL, AUDIO_SAMPLES_COUNT, PSP_AUDIO_FORMAT_STEREO);
pspAudioSetChannelCallback(_audioChannel, audioCallback, 0);
}
void System_PSP::stopAudio() {
sceAudioChRelease(_audioChannel);
sceKernelDeleteSema(_audioMutex);
pspAudioEnd();
}
void System_PSP::lockAudio() {
sceKernelWaitSema(_audioMutex, 1, 0);
}
void System_PSP::unlockAudio() {
sceKernelSignalSema(_audioMutex, 1);
}
AudioCallback System_PSP::setAudioCallback(AudioCallback callback) {
AudioCallback cb = _audioCb;
lockAudio();
_audioCb = callback;
unlockAudio();
return cb;
}