5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
14 #include <pspkernel.h>
15 #include <pspiofilemgr.h>
16 #include <pspdisplay.h>
24 #include "../common/lprintf.h"
27 extern int pico_main(void);
31 PSP_MODULE_INFO("PicoDrive", 0, 1, 51);
34 int main() { return pico_main(); } /* just a wrapper */
38 PSP_MODULE_INFO("PicoDrive", 0x1000, 1, 51);
39 PSP_MAIN_THREAD_ATTR(0);
45 /* this is the thing we need the kernel mode for */
46 pspSdkInstallNoDeviceCheckPatch();
48 thid = sceKernelCreateThread("pico_main", (SceKernelThreadEntry) pico_main, 32, 0x2000, PSP_THREAD_ATTR_USER, NULL);
50 sceKernelStartThread(thid, 0, 0);
52 sceKernelExitDeleteThread(0);
54 while (engineState != PGS_Quit)
55 sceKernelDelayThread(1024 * 1024);
63 int psp_unhandled_suspend = 0;
65 unsigned int __attribute__((aligned(16))) guCmdList[GU_CMDLIST_SIZE];
67 void *psp_screen = VRAM_FB0;
69 static int current_screen = 0; /* front bufer */
71 static SceUID main_thread_id = -1;
73 #define ANALOG_DEADZONE 80
76 static int exit_callback(int arg1, int arg2, void *common)
83 static int power_callback(int unknown, int pwrflags, void *common)
85 lprintf("power_callback: flags: 0x%08X\n", pwrflags);
87 /* check for power switch and suspending as one is manual and the other automatic */
88 if (pwrflags & PSP_POWER_CB_POWER_SWITCH || pwrflags & PSP_POWER_CB_SUSPENDING || pwrflags & PSP_POWER_CB_STANDBY)
90 psp_unhandled_suspend = 1;
91 if (engineState != PGS_Suspending)
92 engineStateSuspend = engineState;
93 sceKernelDelayThread(100000); // ??
95 else if (pwrflags & PSP_POWER_CB_RESUME_COMPLETE)
97 engineState = PGS_SuspendWake;
100 //sceDisplayWaitVblankStart();
104 /* Callback thread */
105 static int callback_thread(SceSize args, void *argp)
109 lprintf("callback_thread started with id %08x, priority %i\n",
110 sceKernelGetThreadId(), sceKernelGetThreadCurrentPriority());
112 cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
113 sceKernelRegisterExitCallback(cbid);
114 cbid = sceKernelCreateCallback("Power Callback", power_callback, NULL);
115 scePowerRegisterCallback(0, cbid);
117 sceKernelSleepThreadCB();
127 /* fw 1.5 sometimes returns 8002032c, although getcwd works */
128 r = getcwd(buff, sizeof(buff));
129 if (r) sceIoChdir(buff);
131 main_thread_id = sceKernelGetThreadId();
133 lprintf("\n%s\n", "PicoDrive v" VERSION " " __DATE__ " " __TIME__);
134 lprintf("running on %08x kernel\n", sceKernelDevkitVersion()),
135 lprintf("entered psp_init, threadId %08x, priority %i\n", main_thread_id,
136 sceKernelGetThreadCurrentPriority());
138 thid = sceKernelCreateThread("update_thread", callback_thread, 0x11, 0xFA0, 0, NULL);
141 sceKernelStartThread(thid, 0, 0);
145 sceDisplaySetMode(0, 480, 272);
146 sceDisplaySetFrameBuf(VRAM_FB1, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
148 psp_screen = VRAM_FB0;
153 sceGuStart(GU_DIRECT, guCmdList);
154 sceGuDrawBuffer(GU_PSM_5650, (void *)VRAMOFFS_FB0, 512);
155 sceGuDispBuffer(480, 272, (void *)VRAMOFFS_FB1, 512); // don't care
156 sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT);
157 sceGuDepthBuffer((void *)VRAMOFFS_DEPTH, 512);
158 sceGuOffset(2048 - (480 / 2), 2048 - (272 / 2));
159 sceGuViewport(2048, 2048, 480, 272);
160 sceGuDepthRange(0xc350, 0x2710);
161 sceGuScissor(0, 0, 480, 272);
162 sceGuEnable(GU_SCISSOR_TEST);
164 sceGuDepthMask(0xffff);
165 sceGuDisable(GU_DEPTH_TEST);
167 sceGuFrontFace(GU_CW);
168 sceGuEnable(GU_TEXTURE_2D);
169 sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
170 sceGuAmbientColor(0xffffffff);
171 sceGuColor(0xffffffff);
175 sceDisplayWaitVblankStart();
176 sceGuDisplay(GU_TRUE);
180 sceCtrlSetSamplingCycle(0);
181 sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
184 void psp_finish(void)
186 lprintf("psp_finish..\n");
189 //sceKernelSleepThread();
193 void psp_video_flip(int wait_vsync)
195 if (wait_vsync) sceDisplayWaitVblankStart();
196 sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565,
197 wait_vsync ? PSP_DISPLAY_SETBUF_IMMEDIATE : PSP_DISPLAY_SETBUF_NEXTFRAME);
199 psp_screen = current_screen ? VRAM_FB0 : VRAM_FB1;
202 void *psp_video_get_active_fb(void)
204 return current_screen ? VRAM_FB1 : VRAM_FB0;
207 void psp_video_switch_to_single(void)
209 psp_screen = VRAM_FB0;
210 sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
214 void psp_msleep(int ms)
216 sceKernelDelayThread(ms * 1000);
219 unsigned int psp_pad_read(int blocking)
221 unsigned int buttons;
224 sceCtrlReadBufferPositive(&pad, 1);
225 else sceCtrlPeekBufferPositive(&pad, 1);
226 buttons = pad.Buttons;
229 buttons &= ~(PBTN_NUB_UP|PBTN_NUB_DOWN|PBTN_NUB_LEFT|PBTN_NUB_RIGHT);
230 if (pad.Lx < 128 - ANALOG_DEADZONE) buttons |= PBTN_NUB_LEFT;
231 if (pad.Lx > 128 + ANALOG_DEADZONE) buttons |= PBTN_NUB_RIGHT;
232 if (pad.Ly < 128 - ANALOG_DEADZONE) buttons |= PBTN_NUB_UP;
233 if (pad.Ly > 128 + ANALOG_DEADZONE) buttons |= PBTN_NUB_DOWN;
238 int psp_get_cpu_clock(void)
240 return scePowerGetCpuClockFrequencyInt();
243 int psp_set_cpu_clock(int clock)
245 int ret = scePowerSetClockFrequency(clock, clock, clock/2);
246 if (ret != 0) lprintf("failed to set clock: %i\n", ret);
251 char *psp_get_status_line(void)
253 static char buff[64];
254 int ret, bat_percent, bat_time;
257 ret = sceRtcGetCurrentClockLocalTime(&time);
258 bat_percent = scePowerGetBatteryLifePercent();
259 bat_time = scePowerGetBatteryLifeTime();
260 if (ret < 0 || bat_percent < 0 || bat_time < 0) return NULL;
262 snprintf(buff, sizeof(buff), "%02i:%02i bat: %3i%%", time.hour, time.minutes, bat_percent);
263 if (!scePowerIsPowerOnline())
264 snprintf(buff+strlen(buff), sizeof(buff)-strlen(buff), " (%i:%02i)", bat_time/60, bat_time%60);
268 void psp_wait_suspend(void)
270 // probably should do something smarter here?
271 sceDisplayWaitVblankStart();
274 void psp_resume_suspend(void)
276 // for some reason file IO doesn't seem to work
277 // after resume for some period of time, at least on 1.5
280 for (i = 0; i < 30; i++) {
281 fd = sceIoOpen("EBOOT.PBP", PSP_O_RDONLY, 0777);
283 sceKernelDelayThread(100 * 1024);
285 if (fd >= 0) sceIoClose(fd);
286 sceDisplayWaitVblankStart();
288 lprintf("io resumed after %i tries\n", i);
290 lprintf("io resume failed with %08x\n", fd);
291 sceKernelDelayThread(500 * 1024);
296 #define LOG_FILE "log.txt"
298 #ifndef LPRINTF_STDIO
299 typedef struct _log_entry
302 struct _log_entry *next;
305 static log_entry *le_root = NULL;
308 /* strange: if this function leaks memory (before psp_init() call?),
309 * resume after suspend breaks on 3.90 */
310 void lprintf(const char *fmt, ...)
319 static SceUID logfd = -1;
320 static int msg_count = 0;
324 if (logfd == -2) return; // disabled
327 vsnprintf(buff, sizeof(buff), fmt, vl);
330 // note: this is still unsafe code
331 if (main_thread_id != sceKernelGetThreadId())
333 le = malloc(sizeof(*le));
334 if (le == NULL) return;
336 strcpy(le->buff, buff);
337 if (le_root == NULL) le_root = le;
339 for (le1 = le_root; le1->next != NULL; le1 = le1->next);
345 logfd = sceIoOpen(LOG_FILE, PSP_O_WRONLY|PSP_O_APPEND, 0777);
347 if (msg_count == 0) logfd = -2;
355 sceKernelDelayThread(1000);
356 while (le1 != NULL) {
359 sceIoWrite(logfd, le->buff, strlen(le->buff));
365 sceIoWrite(logfd, buff, strlen(buff));
368 // make sure it gets flushed