1 // (c) Copyright 2007 notaz, All rights reserved.
2 // Free for non-commercial use.
4 // For commercial use, separate licencing terms must be obtained.
12 #include <pspkernel.h>
13 #include <pspiofilemgr.h>
14 #include <pspdisplay.h>
22 #include "../common/lprintf.h"
24 extern int pico_main(void);
28 PSP_MODULE_INFO("PicoDrive", 0, 1, 40);
31 int main() { return pico_main(); } /* just a wrapper */
35 PSP_MODULE_INFO("PicoDrive", 0x1000, 1, 40);
36 PSP_MAIN_THREAD_ATTR(0);
42 /* this is the thing we need the kernel mode for */
43 pspSdkInstallNoDeviceCheckPatch();
45 thid = sceKernelCreateThread("pico_main", (SceKernelThreadEntry) pico_main, 32, 0x2000, PSP_THREAD_ATTR_USER, NULL);
47 sceKernelStartThread(thid, 0, 0);
49 sceKernelExitDeleteThread(0);
51 while (engineState != PGS_Quit)
52 sceKernelDelayThread(1024 * 1024);
60 unsigned int __attribute__((aligned(16))) guCmdList[GU_CMDLIST_SIZE];
62 void *psp_screen = VRAM_FB0;
63 int psp_unhandled_suspend = 0;
65 static int current_screen = 0; /* front bufer */
67 static SceUID main_thread_id = -1;
69 #define ANALOG_DEADZONE 80
72 static int exit_callback(int arg1, int arg2, void *common)
79 static int power_callback(int unknown, int pwrflags, void *common)
81 static int old_state = PGS_Menu;
83 lprintf("power_callback: flags: 0x%08X\n", pwrflags);
85 /* check for power switch and suspending as one is manual and the other automatic */
86 if (pwrflags & PSP_POWER_CB_POWER_SWITCH || pwrflags & PSP_POWER_CB_SUSPENDING || pwrflags & PSP_POWER_CB_STANDBY)
88 if (engineState != PGS_Suspending) {
89 old_state = engineState;
90 engineState = PGS_Suspending;
93 else if (pwrflags & PSP_POWER_CB_RESUME_COMPLETE)
95 engineState = old_state;
96 psp_unhandled_suspend = 1;
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("running on %08x kernel\n", sceKernelDevkitVersion()),
133 lprintf("entered psp_init, threadId %08x, priority %i\n", main_thread_id,
134 sceKernelGetThreadCurrentPriority());
136 thid = sceKernelCreateThread("update_thread", callback_thread, 0x11, 0xFA0, 0, NULL);
139 sceKernelStartThread(thid, 0, 0);
143 sceDisplaySetMode(0, 480, 272);
144 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 sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT);
155 sceGuDepthBuffer((void *)VRAMOFFS_DEPTH, 512);
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);
162 sceGuDepthMask(0xffff);
163 sceGuDisable(GU_DEPTH_TEST);
165 sceGuFrontFace(GU_CW);
166 sceGuEnable(GU_TEXTURE_2D);
167 sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
168 sceGuAmbientColor(0xffffffff);
169 sceGuColor(0xffffffff);
173 sceDisplayWaitVblankStart();
174 sceGuDisplay(GU_TRUE);
178 sceCtrlSetSamplingCycle(0);
179 sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
182 void psp_finish(void)
184 lprintf("psp_finish..\n");
187 //sceKernelSleepThread();
191 void psp_video_flip(int wait_vsync)
193 if (wait_vsync) sceDisplayWaitVblankStart();
194 sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565,
195 wait_vsync ? PSP_DISPLAY_SETBUF_IMMEDIATE : PSP_DISPLAY_SETBUF_NEXTFRAME);
197 psp_screen = current_screen ? VRAM_FB0 : VRAM_FB1;
200 void *psp_video_get_active_fb(void)
202 return current_screen ? VRAM_FB1 : VRAM_FB0;
205 void psp_video_switch_to_single(void)
207 psp_screen = VRAM_FB0;
208 sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
212 void psp_msleep(int ms)
214 sceKernelDelayThread(ms * 1000);
217 unsigned int psp_pad_read(int blocking)
219 unsigned int buttons;
222 sceCtrlReadBufferPositive(&pad, 1);
223 else sceCtrlPeekBufferPositive(&pad, 1);
224 buttons = pad.Buttons;
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;
236 int psp_get_cpu_clock(void)
238 return scePowerGetCpuClockFrequencyInt();
241 int psp_set_cpu_clock(int clock)
243 int ret = scePowerSetClockFrequency(clock, clock, clock/2);
244 if (ret != 0) lprintf("failed to set clock: %i\n", ret);
249 char *psp_get_status_line(void)
251 static char buff[64];
252 int ret, bat_percent, bat_time;
255 ret = sceRtcGetCurrentClockLocalTime(&time);
256 bat_percent = scePowerGetBatteryLifePercent();
257 bat_time = scePowerGetBatteryLifeTime();
258 if (ret < 0 || bat_percent < 0 || bat_time < 0) return NULL;
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);
266 void psp_wait_suspend(void)
268 // probably should do something smarter here?
269 sceDisplayWaitVblankStart();
272 void psp_resume_suspend(void)
274 // for some reason file IO doesn't seem to work
275 // after resume for some period of time, at least on 1.5
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);
283 if (fd >= 0) sceIoClose(fd);
284 sceDisplayWaitVblankStart();
285 psp_unhandled_suspend = 0;
287 lprintf("io resumed after %i tries\n", i);
288 else lprintf("io resume failed\n");
292 #define LOG_FILE "log.txt"
294 #ifndef LPRINTF_STDIO
295 typedef struct _log_entry
298 struct _log_entry *next;
301 static log_entry *le_root = NULL;
304 void lprintf(const char *fmt, ...)
313 static SceUID logfd = -1;
314 static int msg_count = 0;
318 if (logfd == -2) return; // disabled
321 vsnprintf(buff, sizeof(buff), fmt, vl);
324 // note: this is still unsafe code
325 if (main_thread_id != sceKernelGetThreadId())
327 le = malloc(sizeof(*le));
328 if (le == NULL) return;
330 strcpy(le->buff, buff);
331 if (le_root == NULL) le_root = le;
333 for (le1 = le_root; le1->next != NULL; le1 = le1->next);
339 logfd = sceIoOpen(LOG_FILE, PSP_O_WRONLY|PSP_O_APPEND, 0777);
341 if (msg_count == 0) logfd = -2;
349 sceKernelDelayThread(1000);
350 while (le1 != NULL) {
353 sceIoWrite(logfd, le->buff, strlen(le->buff));
359 sceIoWrite(logfd, buff, strlen(buff));
362 // make sure it gets flushed