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