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>
25 #include <pico/pico_int.h>
26 #include "../common/emu.h"
27 #include "../common/version.h"
29 extern int pico_main(int argc, char *argv[]);
33 PSP_MODULE_INFO("PicoDrive", 0, 1, 97);
35 int main(int argc, char *argv[]) { return pico_main(argc, argv); } /* just a wrapper */
39 PSP_MODULE_INFO("PicoDrive", 0x1000, 1, 97);
40 PSP_MAIN_THREAD_ATTR(0);
42 int main(int argc, char *argv[])
46 /* this is the thing we need the kernel mode for */
47 pspSdkInstallNoDeviceCheckPatch();
49 thid = sceKernelCreateThread("pico_main", (SceKernelThreadEntry) pico_main, 32, 0x2000, PSP_THREAD_ATTR_USER, NULL);
51 sceKernelStartThread(thid, argc, argv);
53 sceKernelExitDeleteThread(0);
55 while (engineState != PGS_Quit)
56 sceKernelDelayThread(1024 * 1024);
64 int psp_unhandled_suspend = 0;
66 unsigned int __attribute__((aligned(16))) guCmdList[GU_CMDLIST_SIZE];
68 void *psp_screen = VRAM_FB0; /* back buffer */
70 static SceUID main_thread_id = -1;
72 #define ANALOG_DEADZONE 80
75 static int exit_callback(int arg1, int arg2, void *common)
82 static int power_callback(int unknown, int pwrflags, void *common)
84 lprintf("power_callback: flags: 0x%08X\n", pwrflags);
86 /* check for power switch and suspending as one is manual and the other automatic */
87 if (pwrflags & PSP_POWER_CB_POWER_SWITCH || pwrflags & PSP_POWER_CB_SUSPENDING || pwrflags & PSP_POWER_CB_STANDBY)
89 psp_unhandled_suspend = 1;
90 if (engineState != PGS_Suspending)
91 engineStateSuspend = engineState;
92 sceKernelDelayThread(100000); // ??
94 else if (pwrflags & PSP_POWER_CB_RESUME_COMPLETE)
96 engineState = PGS_SuspendWake;
99 //sceDisplayWaitVblankStart();
103 /* Callback thread */
104 static int callback_thread(SceSize args, void *argp)
108 lprintf("callback_thread started with id %08x, priority %i\n",
109 sceKernelGetThreadId(), sceKernelGetThreadCurrentPriority());
111 cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
112 sceKernelRegisterExitCallback(cbid);
113 cbid = sceKernelCreateCallback("Power Callback", power_callback, NULL);
114 scePowerRegisterCallback(0, cbid);
116 sceKernelSleepThreadCB();
126 /* fw 1.5 sometimes returns 8002032c, although getcwd works */
127 r = getcwd(buff, sizeof(buff));
128 if (r) sceIoChdir(buff);
130 main_thread_id = sceKernelGetThreadId();
132 lprintf("\n%s\n", "PicoDrive v" VERSION " " __DATE__ " " __TIME__);
133 lprintf("running on %08x kernel\n", sceKernelDevkitVersion()),
134 lprintf("entered psp_init, threadId %08x, priority %i\n", main_thread_id,
135 sceKernelGetThreadCurrentPriority());
137 thid = sceKernelCreateThread("update_thread", callback_thread, 0x11, 0xFA0, 0, NULL);
140 sceKernelStartThread(thid, 0, 0);
144 sceDisplaySetMode(0, 480, 272);
145 sceDisplaySetFrameBuf(VRAM_FB1, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
146 psp_screen = VRAM_FB0;
151 sceGuStart(GU_DIRECT, guCmdList);
152 sceGuDrawBuffer(GU_PSM_5650, (void *)VRAMOFFS_FB0, 512);
153 sceGuDispBuffer(480, 272, (void *)VRAMOFFS_FB1, 512); // don't care
154 sceGuDepthBuffer((void *)VRAMOFFS_DEPTH, 512);
157 sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT);
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 PSP_DISPLAY_SETBUF_IMMEDIATE);
198 psp_screen = (void *)((unsigned long)psp_screen ^ (VRAMOFFS_FB1 ^ VRAMOFFS_FB0));
201 void *psp_video_get_active_fb(void)
203 return (void *)((unsigned long)psp_screen ^ (VRAMOFFS_FB1 ^ VRAMOFFS_FB0));
206 void psp_msleep(int ms)
208 sceKernelDelayThread(ms * 1000);
211 unsigned int psp_pad_read(int blocking)
213 unsigned int buttons;
216 sceCtrlReadBufferPositive(&pad, 1);
217 else sceCtrlPeekBufferPositive(&pad, 1);
218 buttons = pad.Buttons;
221 buttons &= ~(PSP_NUB_UP|PSP_NUB_DOWN|PSP_NUB_LEFT|PSP_NUB_RIGHT);
222 if (pad.Lx < 128 - ANALOG_DEADZONE) buttons |= PSP_NUB_LEFT;
223 if (pad.Lx > 128 + ANALOG_DEADZONE) buttons |= PSP_NUB_RIGHT;
224 if (pad.Ly < 128 - ANALOG_DEADZONE) buttons |= PSP_NUB_UP;
225 if (pad.Ly > 128 + ANALOG_DEADZONE) buttons |= PSP_NUB_DOWN;
230 int psp_get_cpu_clock(void)
232 return scePowerGetCpuClockFrequencyInt();
235 int psp_set_cpu_clock(int clock)
237 int ret = scePowerSetClockFrequency(clock, clock, clock/2);
238 if (ret != 0) lprintf("failed to set clock: %i\n", ret);
243 char *psp_get_status_line(void)
245 static char buff[64];
246 int ret, bat_percent, bat_time;
249 ret = sceRtcGetCurrentClockLocalTime(&time);
250 bat_percent = scePowerGetBatteryLifePercent();
251 bat_time = scePowerGetBatteryLifeTime();
252 if (ret < 0 || bat_percent < 0 || bat_time < 0) return NULL;
254 snprintf(buff, sizeof(buff), "%02i:%02i bat: %3i%%", time.hour, time.minute, bat_percent);
255 if (!scePowerIsPowerOnline())
256 snprintf(buff+strlen(buff), sizeof(buff)-strlen(buff), " (%i:%02i)", bat_time/60, bat_time%60);
260 void psp_wait_suspend(void)
262 // probably should do something smarter here?
263 sceDisplayWaitVblankStart();
266 void psp_resume_suspend(void)
268 // for some reason file IO doesn't seem to work
269 // after resume for some period of time, at least on 1.5
272 for (i = 0; i < 30; i++) {
273 fd = sceIoOpen("EBOOT.PBP", PSP_O_RDONLY, 0777);
275 sceKernelDelayThread(100 * 1024);
277 if (fd >= 0) sceIoClose(fd);
278 sceDisplayWaitVblankStart();
280 lprintf("io resumed after %i tries\n", i);
282 lprintf("io resume failed with %08x\n", fd);
283 sceKernelDelayThread(500 * 1024);
288 #define LOG_FILE "log.txt"
290 #ifndef LPRINTF_STDIO
291 typedef struct _log_entry
294 struct _log_entry *next;
297 static log_entry *le_root = NULL;
300 /* strange: if this function leaks memory (before psp_init() call?),
301 * resume after suspend breaks on 3.90 */
302 void lprintf(const char *fmt, ...)
311 static SceUID logfd = -1;
312 static int msg_count = 0;
316 if (logfd == -2) return; // disabled
319 vsnprintf(buff, sizeof(buff), fmt, vl);
322 // note: this is still unsafe code
323 if (main_thread_id != sceKernelGetThreadId())
325 le = malloc(sizeof(*le));
326 if (le == NULL) return;
328 strcpy(le->buff, buff);
329 if (le_root == NULL) le_root = le;
331 for (le1 = le_root; le1->next != NULL; le1 = le1->next);
337 logfd = sceIoOpen(LOG_FILE, PSP_O_WRONLY|PSP_O_APPEND, 0777);
339 if (msg_count == 0) logfd = -2;
347 sceKernelDelayThread(1000);
348 while (le1 != NULL) {
351 sceIoWrite(logfd, le->buff, strlen(le->buff));
357 sceIoWrite(logfd, buff, strlen(buff));
360 // make sure it gets flushed