cue/bin finally implemented
[picodrive.git] / platform / psp / psp.c
CommitLineData
8b99ab90 1// (c) Copyright 2007 notaz, All rights reserved.
2// Free for non-commercial use.
3
4// For commercial use, separate licencing terms must be obtained.
5
7d4906bf 6#include <stdio.h>
110df09c 7#include <stdlib.h>
7d4906bf 8#include <stdarg.h>
9#include <string.h>
ea08c296 10#include <unistd.h>
7d4906bf 11
1820b5a7 12#include <pspkernel.h>
7d4906bf 13#include <pspiofilemgr.h>
1820b5a7 14#include <pspdisplay.h>
70357ce5 15#include <psppower.h>
9caf44b5 16#include <psprtc.h>
1820b5a7 17#include <pspgu.h>
2445b7cb 18#include <pspsdk.h>
1820b5a7 19
20#include "psp.h"
b542be46 21#include "emu.h"
1820b5a7 22#include "../common/lprintf.h"
23
2445b7cb 24extern int pico_main(void);
25
26#ifndef FW15
27
651b1a25 28PSP_MODULE_INFO("PicoDrive", 0, 1, 40);
110df09c 29PSP_HEAP_SIZE_MAX();
1820b5a7 30
2445b7cb 31int main() { return pico_main(); } /* just a wrapper */
32
33#else
34
651b1a25 35PSP_MODULE_INFO("PicoDrive", 0x1000, 1, 40);
2445b7cb 36PSP_MAIN_THREAD_ATTR(0);
37
38int main()
39{
40 SceUID thid;
41
42 /* this is the thing we need the kernel mode for */
43 pspSdkInstallNoDeviceCheckPatch();
44
45 thid = sceKernelCreateThread("pico_main", (SceKernelThreadEntry) pico_main, 32, 0x2000, PSP_THREAD_ATTR_USER, NULL);
46 if (thid >= 0)
47 sceKernelStartThread(thid, 0, 0);
ff6b7429 48#ifndef GCOV
2445b7cb 49 sceKernelExitDeleteThread(0);
ff6b7429 50#else
51 while (engineState != PGS_Quit)
52 sceKernelDelayThread(1024 * 1024);
53#endif
2445b7cb 54
55 return 0;
56}
57
58#endif
59
9112b6ce 60unsigned int __attribute__((aligned(16))) guCmdList[GU_CMDLIST_SIZE];
61
62void *psp_screen = VRAM_FB0;
ea08c296 63int psp_unhandled_suspend = 0;
64
1820b5a7 65static int current_screen = 0; /* front bufer */
66
110df09c 67static SceUID main_thread_id = -1;
68
9caf44b5 69#define ANALOG_DEADZONE 80
1820b5a7 70
71/* Exit callback */
72static int exit_callback(int arg1, int arg2, void *common)
73{
74 sceKernelExitGame();
75 return 0;
76}
77
b542be46 78/* Power Callback */
79static int power_callback(int unknown, int pwrflags, void *common)
80{
7d0143a2 81 static int old_state = PGS_Menu;
82
83 lprintf("power_callback: flags: 0x%08X\n", pwrflags);
84
b542be46 85 /* check for power switch and suspending as one is manual and the other automatic */
7d0143a2 86 if (pwrflags & PSP_POWER_CB_POWER_SWITCH || pwrflags & PSP_POWER_CB_SUSPENDING || pwrflags & PSP_POWER_CB_STANDBY)
b542be46 87 {
7d0143a2 88 if (engineState != PGS_Suspending) {
89 old_state = engineState;
90 engineState = PGS_Suspending;
91 }
b542be46 92 }
7d0143a2 93 else if (pwrflags & PSP_POWER_CB_RESUME_COMPLETE)
ea08c296 94 {
7d0143a2 95 engineState = old_state;
ea08c296 96 psp_unhandled_suspend = 1;
97 }
7d0143a2 98
2445b7cb 99 //sceDisplayWaitVblankStart();
b542be46 100 return 0;
101}
102
1820b5a7 103/* Callback thread */
104static int callback_thread(SceSize args, void *argp)
105{
106 int cbid;
107
4b167c12 108 lprintf("callback_thread started with id %08x, priority %i\n",
a4221917 109 sceKernelGetThreadId(), sceKernelGetThreadCurrentPriority());
1820b5a7 110
111 cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
112 sceKernelRegisterExitCallback(cbid);
b542be46 113 cbid = sceKernelCreateCallback("Power Callback", power_callback, NULL);
114 scePowerRegisterCallback(0, cbid);
1820b5a7 115
116 sceKernelSleepThreadCB();
117
118 return 0;
119}
120
121void psp_init(void)
122{
a4221917 123 SceUID thid;
ea08c296 124 char buff[128], *r;
125
126 /* fw 1.5 sometimes returns 8002032c, although getcwd works */
127 r = getcwd(buff, sizeof(buff));
128 if (r) sceIoChdir(buff);
1820b5a7 129
110df09c 130 main_thread_id = sceKernelGetThreadId();
131
132 lprintf("running on %08x kernel\n", sceKernelDevkitVersion()),
133 lprintf("entered psp_init, threadId %08x, priority %i\n", main_thread_id,
a4221917 134 sceKernelGetThreadCurrentPriority());
1820b5a7 135
2445b7cb 136 thid = sceKernelCreateThread("update_thread", callback_thread, 0x11, 0xFA0, 0, NULL);
1820b5a7 137 if (thid >= 0)
138 {
139 sceKernelStartThread(thid, 0, 0);
140 }
141
142 /* video */
143 sceDisplaySetMode(0, 480, 272);
9112b6ce 144 sceDisplaySetFrameBuf(VRAM_FB1, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
1820b5a7 145 current_screen = 1;
9112b6ce 146 psp_screen = VRAM_FB0;
1820b5a7 147
148 /* gu */
149 sceGuInit();
150
9112b6ce 151 sceGuStart(GU_DIRECT, guCmdList);
8ab3e3c1 152 sceGuDrawBuffer(GU_PSM_5650, (void *)VRAMOFFS_FB0, 512);
153 sceGuDispBuffer(480, 272, (void *)VRAMOFFS_FB1, 512); // don't care
9112b6ce 154 sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT);
8ab3e3c1 155 sceGuDepthBuffer((void *)VRAMOFFS_DEPTH, 512);
9112b6ce 156 sceGuOffset(2048 - (480 / 2), 2048 - (272 / 2));
157 sceGuViewport(2048, 2048, 480, 272);
158 sceGuDepthRange(0xc350, 0x2710);
159 sceGuScissor(0, 0, 480, 272);
160 sceGuEnable(GU_SCISSOR_TEST);
9112b6ce 161
162 sceGuDepthMask(0xffff);
163 sceGuDisable(GU_DEPTH_TEST);
164
165 sceGuFrontFace(GU_CW);
9112b6ce 166 sceGuEnable(GU_TEXTURE_2D);
9112b6ce 167 sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
8ab3e3c1 168 sceGuAmbientColor(0xffffffff);
169 sceGuColor(0xffffffff);
9112b6ce 170 sceGuFinish();
171 sceGuSync(0, 0);
172
173 sceDisplayWaitVblankStart();
174 sceGuDisplay(GU_TRUE);
175
176
1820b5a7 177 /* input */
178 sceCtrlSetSamplingCycle(0);
9caf44b5 179 sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
1820b5a7 180}
181
182void psp_finish(void)
183{
aefb65bc 184 lprintf("psp_finish..\n");
1820b5a7 185 sceGuTerm();
186
187 //sceKernelSleepThread();
188 sceKernelExitGame();
189}
190
7d4906bf 191void psp_video_flip(int wait_vsync)
1820b5a7 192{
7d4906bf 193 if (wait_vsync) sceDisplayWaitVblankStart();
70357ce5 194 sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565,
195 wait_vsync ? PSP_DISPLAY_SETBUF_IMMEDIATE : PSP_DISPLAY_SETBUF_NEXTFRAME);
1820b5a7 196 current_screen ^= 1;
9112b6ce 197 psp_screen = current_screen ? VRAM_FB0 : VRAM_FB1;
7d4906bf 198}
199
200void *psp_video_get_active_fb(void)
201{
9112b6ce 202 return current_screen ? VRAM_FB1 : VRAM_FB0;
1820b5a7 203}
204
205void psp_video_switch_to_single(void)
206{
9112b6ce 207 psp_screen = VRAM_FB0;
1820b5a7 208 sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
209 current_screen = 0;
210}
211
212void psp_msleep(int ms)
213{
214 sceKernelDelayThread(ms * 1000);
215}
216
7d4906bf 217unsigned int psp_pad_read(int blocking)
1820b5a7 218{
9caf44b5 219 unsigned int buttons;
1820b5a7 220 SceCtrlData pad;
7d4906bf 221 if (blocking)
222 sceCtrlReadBufferPositive(&pad, 1);
223 else sceCtrlPeekBufferPositive(&pad, 1);
9caf44b5 224 buttons = pad.Buttons;
225
226 // analog..
227 buttons &= ~(BTN_NUB_UP|BTN_NUB_DOWN|BTN_NUB_LEFT|BTN_NUB_RIGHT);
228 if (pad.Lx < 128 - ANALOG_DEADZONE) buttons |= BTN_NUB_LEFT;
229 if (pad.Lx > 128 + ANALOG_DEADZONE) buttons |= BTN_NUB_RIGHT;
230 if (pad.Ly < 128 - ANALOG_DEADZONE) buttons |= BTN_NUB_UP;
231 if (pad.Ly > 128 + ANALOG_DEADZONE) buttons |= BTN_NUB_DOWN;
1820b5a7 232
9caf44b5 233 return buttons;
1820b5a7 234}
235
70357ce5 236int psp_get_cpu_clock(void)
237{
238 return scePowerGetCpuClockFrequencyInt();
239}
240
241int psp_set_cpu_clock(int clock)
242{
243 int ret = scePowerSetClockFrequency(clock, clock, clock/2);
244 if (ret != 0) lprintf("failed to set clock: %i\n", ret);
245
246 return ret;
247}
248
9caf44b5 249char *psp_get_status_line(void)
250{
251 static char buff[64];
252 int ret, bat_percent, bat_time;
253 pspTime time;
254
255 ret = sceRtcGetCurrentClockLocalTime(&time);
256 bat_percent = scePowerGetBatteryLifePercent();
257 bat_time = scePowerGetBatteryLifeTime();
258 if (ret < 0 || bat_percent < 0 || bat_time < 0) return NULL;
259
260 snprintf(buff, sizeof(buff), "%02i:%02i bat: %3i%%", time.hour, time.minutes, bat_percent);
261 if (!scePowerIsPowerOnline())
262 snprintf(buff+strlen(buff), sizeof(buff)-strlen(buff), " (%i:%02i)", bat_time/60, bat_time%60);
263 return buff;
264}
265
7d0143a2 266void psp_wait_suspend(void)
267{
268 // probably should do something smarter here?
269 sceDisplayWaitVblankStart();
270}
271
ea08c296 272void psp_resume_suspend(void)
273{
274 // for some reason file IO doesn't seem to work
275 // after resume for some period of time, at least on 1.5
276 SceUID fd;
277 int i;
278 for (i = 0; i < 30; i++) {
279 fd = sceIoOpen("dummy.txt", PSP_O_WRONLY|PSP_O_APPEND, 0777);
280 if (fd != 0x80010013) break; // device not available
281 sceKernelDelayThread(32 * 1024);
282 }
283 if (fd >= 0) sceIoClose(fd);
284 sceDisplayWaitVblankStart();
285 psp_unhandled_suspend = 0;
286 if (i < 30)
287 lprintf("io resumed after %i tries\n", i);
288 else lprintf("io resume failed\n");
289}
290
7d4906bf 291/* alt logging */
110df09c 292#define LOG_FILE "log.txt"
293
c6196c0f 294#ifndef LPRINTF_STDIO
110df09c 295typedef struct _log_entry
296{
297 char buff[256];
298 struct _log_entry *next;
299} log_entry;
7d4906bf 300
110df09c 301static log_entry *le_root = NULL;
c6196c0f 302#endif
9caf44b5 303
7d0143a2 304void lprintf(const char *fmt, ...)
7d4906bf 305{
306 va_list vl;
110df09c 307
308#ifdef LPRINTF_STDIO
309 va_start(vl, fmt);
310 vprintf(fmt, vl);
311 va_end(vl);
312#else
313 static SceUID logfd = -1;
ea08c296 314 static int msg_count = 0;
7d4906bf 315 char buff[256];
110df09c 316 log_entry *le, *le1;
7d4906bf 317
9caf44b5 318 if (logfd == -2) return; // disabled
319
110df09c 320 va_start(vl, fmt);
321 vsnprintf(buff, sizeof(buff), fmt, vl);
322 va_end(vl);
323
324 // note: this is still unsafe code
325 if (main_thread_id != sceKernelGetThreadId())
7d4906bf 326 {
110df09c 327 le = malloc(sizeof(*le));
328 if (le == NULL) return;
329 le->next = NULL;
330 strcpy(le->buff, buff);
331 if (le_root == NULL) le_root = le;
332 else {
333 for (le1 = le_root; le1->next != NULL; le1 = le1->next);
334 le1->next = le;
9caf44b5 335 }
110df09c 336 return;
7d4906bf 337 }
338
110df09c 339 logfd = sceIoOpen(LOG_FILE, PSP_O_WRONLY|PSP_O_APPEND, 0777);
340 if (logfd < 0) {
ea08c296 341 if (msg_count == 0) logfd = -2;
110df09c 342 return;
343 }
344
345 if (le_root != NULL)
346 {
347 le1 = le_root;
348 le_root = NULL;
349 sceKernelDelayThread(1000);
350 while (le1 != NULL) {
351 le = le1;
352 le1 = le->next;
353 sceIoWrite(logfd, le->buff, strlen(le->buff));
354 free(le);
ea08c296 355 msg_count++;
110df09c 356 }
357 }
7d4906bf 358
359 sceIoWrite(logfd, buff, strlen(buff));
ea08c296 360 msg_count++;
9caf44b5 361
362 // make sure it gets flushed
363 sceIoClose(logfd);
364 logfd = -1;
110df09c 365#endif
7d4906bf 366}
367
368