| 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 | |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <stdarg.h> |
| 9 | #include <string.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | #include <pspkernel.h> |
| 13 | #include <pspiofilemgr.h> |
| 14 | #include <pspdisplay.h> |
| 15 | #include <psppower.h> |
| 16 | #include <psprtc.h> |
| 17 | #include <pspgu.h> |
| 18 | #include <pspsdk.h> |
| 19 | |
| 20 | #include "psp.h" |
| 21 | #include "emu.h" |
| 22 | #include "../common/lprintf.h" |
| 23 | #include "version.h" |
| 24 | |
| 25 | extern int pico_main(void); |
| 26 | |
| 27 | #ifndef FW15 |
| 28 | |
| 29 | PSP_MODULE_INFO("PicoDrive", 0, 1, 51); |
| 30 | PSP_HEAP_SIZE_MAX(); |
| 31 | |
| 32 | int main() { return pico_main(); } /* just a wrapper */ |
| 33 | |
| 34 | #else |
| 35 | |
| 36 | PSP_MODULE_INFO("PicoDrive", 0x1000, 1, 51); |
| 37 | PSP_MAIN_THREAD_ATTR(0); |
| 38 | |
| 39 | int 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); |
| 49 | #ifndef GCOV |
| 50 | sceKernelExitDeleteThread(0); |
| 51 | #else |
| 52 | while (engineState != PGS_Quit) |
| 53 | sceKernelDelayThread(1024 * 1024); |
| 54 | #endif |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | #endif |
| 60 | |
| 61 | int psp_unhandled_suspend = 0; |
| 62 | |
| 63 | unsigned int __attribute__((aligned(16))) guCmdList[GU_CMDLIST_SIZE]; |
| 64 | |
| 65 | void *psp_screen = VRAM_FB0; |
| 66 | |
| 67 | static int current_screen = 0; /* front bufer */ |
| 68 | |
| 69 | static SceUID main_thread_id = -1; |
| 70 | |
| 71 | #define ANALOG_DEADZONE 80 |
| 72 | |
| 73 | /* Exit callback */ |
| 74 | static int exit_callback(int arg1, int arg2, void *common) |
| 75 | { |
| 76 | sceKernelExitGame(); |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | /* Power Callback */ |
| 81 | static int power_callback(int unknown, int pwrflags, void *common) |
| 82 | { |
| 83 | lprintf("power_callback: flags: 0x%08X\n", pwrflags); |
| 84 | |
| 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) |
| 87 | { |
| 88 | psp_unhandled_suspend = 1; |
| 89 | if (engineState != PGS_Suspending) |
| 90 | engineStateSuspend = engineState; |
| 91 | sceKernelDelayThread(100000); // ?? |
| 92 | } |
| 93 | else if (pwrflags & PSP_POWER_CB_RESUME_COMPLETE) |
| 94 | { |
| 95 | engineState = PGS_SuspendWake; |
| 96 | } |
| 97 | |
| 98 | //sceDisplayWaitVblankStart(); |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | /* Callback thread */ |
| 103 | static int callback_thread(SceSize args, void *argp) |
| 104 | { |
| 105 | int cbid; |
| 106 | |
| 107 | lprintf("callback_thread started with id %08x, priority %i\n", |
| 108 | sceKernelGetThreadId(), sceKernelGetThreadCurrentPriority()); |
| 109 | |
| 110 | cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL); |
| 111 | sceKernelRegisterExitCallback(cbid); |
| 112 | cbid = sceKernelCreateCallback("Power Callback", power_callback, NULL); |
| 113 | scePowerRegisterCallback(0, cbid); |
| 114 | |
| 115 | sceKernelSleepThreadCB(); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | void psp_init(void) |
| 121 | { |
| 122 | SceUID thid; |
| 123 | char buff[128], *r; |
| 124 | |
| 125 | /* fw 1.5 sometimes returns 8002032c, although getcwd works */ |
| 126 | r = getcwd(buff, sizeof(buff)); |
| 127 | if (r) sceIoChdir(buff); |
| 128 | |
| 129 | main_thread_id = sceKernelGetThreadId(); |
| 130 | |
| 131 | lprintf("\n%s\n", "PicoDrive v" VERSION " " __DATE__ " " __TIME__); |
| 132 | lprintf("running on %08x kernel\n", sceKernelDevkitVersion()), |
| 133 | lprintf("entered psp_init, threadId %08x, priority %i\n", main_thread_id, |
| 134 | sceKernelGetThreadCurrentPriority()); |
| 135 | |
| 136 | thid = sceKernelCreateThread("update_thread", callback_thread, 0x11, 0xFA0, 0, NULL); |
| 137 | if (thid >= 0) |
| 138 | { |
| 139 | sceKernelStartThread(thid, 0, 0); |
| 140 | } |
| 141 | |
| 142 | /* video */ |
| 143 | sceDisplaySetMode(0, 480, 272); |
| 144 | sceDisplaySetFrameBuf(VRAM_FB1, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME); |
| 145 | current_screen = 1; |
| 146 | psp_screen = VRAM_FB0; |
| 147 | |
| 148 | /* gu */ |
| 149 | sceGuInit(); |
| 150 | |
| 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); |
| 161 | |
| 162 | sceGuDepthMask(0xffff); |
| 163 | sceGuDisable(GU_DEPTH_TEST); |
| 164 | |
| 165 | sceGuFrontFace(GU_CW); |
| 166 | sceGuEnable(GU_TEXTURE_2D); |
| 167 | sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB); |
| 168 | sceGuAmbientColor(0xffffffff); |
| 169 | sceGuColor(0xffffffff); |
| 170 | sceGuFinish(); |
| 171 | sceGuSync(0, 0); |
| 172 | |
| 173 | sceDisplayWaitVblankStart(); |
| 174 | sceGuDisplay(GU_TRUE); |
| 175 | |
| 176 | |
| 177 | /* input */ |
| 178 | sceCtrlSetSamplingCycle(0); |
| 179 | sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG); |
| 180 | } |
| 181 | |
| 182 | void psp_finish(void) |
| 183 | { |
| 184 | lprintf("psp_finish..\n"); |
| 185 | sceGuTerm(); |
| 186 | |
| 187 | //sceKernelSleepThread(); |
| 188 | sceKernelExitGame(); |
| 189 | } |
| 190 | |
| 191 | void psp_video_flip(int wait_vsync) |
| 192 | { |
| 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); |
| 196 | current_screen ^= 1; |
| 197 | psp_screen = current_screen ? VRAM_FB0 : VRAM_FB1; |
| 198 | } |
| 199 | |
| 200 | void *psp_video_get_active_fb(void) |
| 201 | { |
| 202 | return current_screen ? VRAM_FB1 : VRAM_FB0; |
| 203 | } |
| 204 | |
| 205 | void psp_video_switch_to_single(void) |
| 206 | { |
| 207 | psp_screen = VRAM_FB0; |
| 208 | sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME); |
| 209 | current_screen = 0; |
| 210 | } |
| 211 | |
| 212 | void psp_msleep(int ms) |
| 213 | { |
| 214 | sceKernelDelayThread(ms * 1000); |
| 215 | } |
| 216 | |
| 217 | unsigned int psp_pad_read(int blocking) |
| 218 | { |
| 219 | unsigned int buttons; |
| 220 | SceCtrlData pad; |
| 221 | if (blocking) |
| 222 | sceCtrlReadBufferPositive(&pad, 1); |
| 223 | else sceCtrlPeekBufferPositive(&pad, 1); |
| 224 | buttons = pad.Buttons; |
| 225 | |
| 226 | // analog.. |
| 227 | buttons &= ~(PBTN_NUB_UP|PBTN_NUB_DOWN|PBTN_NUB_LEFT|PBTN_NUB_RIGHT); |
| 228 | if (pad.Lx < 128 - ANALOG_DEADZONE) buttons |= PBTN_NUB_LEFT; |
| 229 | if (pad.Lx > 128 + ANALOG_DEADZONE) buttons |= PBTN_NUB_RIGHT; |
| 230 | if (pad.Ly < 128 - ANALOG_DEADZONE) buttons |= PBTN_NUB_UP; |
| 231 | if (pad.Ly > 128 + ANALOG_DEADZONE) buttons |= PBTN_NUB_DOWN; |
| 232 | |
| 233 | return buttons; |
| 234 | } |
| 235 | |
| 236 | int psp_get_cpu_clock(void) |
| 237 | { |
| 238 | return scePowerGetCpuClockFrequencyInt(); |
| 239 | } |
| 240 | |
| 241 | int 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 | |
| 249 | char *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 | |
| 266 | void psp_wait_suspend(void) |
| 267 | { |
| 268 | // probably should do something smarter here? |
| 269 | sceDisplayWaitVblankStart(); |
| 270 | } |
| 271 | |
| 272 | void 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("EBOOT.PBP", PSP_O_RDONLY, 0777); |
| 280 | if (fd >= 0) break; |
| 281 | sceKernelDelayThread(100 * 1024); |
| 282 | } |
| 283 | if (fd >= 0) sceIoClose(fd); |
| 284 | sceDisplayWaitVblankStart(); |
| 285 | if (i < 30) |
| 286 | lprintf("io resumed after %i tries\n", i); |
| 287 | else { |
| 288 | lprintf("io resume failed with %08x\n", fd); |
| 289 | sceKernelDelayThread(500 * 1024); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /* alt logging */ |
| 294 | #define LOG_FILE "log.txt" |
| 295 | |
| 296 | #ifndef LPRINTF_STDIO |
| 297 | typedef struct _log_entry |
| 298 | { |
| 299 | char buff[256]; |
| 300 | struct _log_entry *next; |
| 301 | } log_entry; |
| 302 | |
| 303 | static log_entry *le_root = NULL; |
| 304 | #endif |
| 305 | |
| 306 | /* strange: if this function leaks memory (before psp_init() call?), |
| 307 | * resume after suspend breaks on 3.90 */ |
| 308 | void lprintf(const char *fmt, ...) |
| 309 | { |
| 310 | va_list vl; |
| 311 | |
| 312 | #ifdef LPRINTF_STDIO |
| 313 | va_start(vl, fmt); |
| 314 | vprintf(fmt, vl); |
| 315 | va_end(vl); |
| 316 | #else |
| 317 | static SceUID logfd = -1; |
| 318 | static int msg_count = 0; |
| 319 | char buff[256]; |
| 320 | log_entry *le, *le1; |
| 321 | |
| 322 | if (logfd == -2) return; // disabled |
| 323 | |
| 324 | va_start(vl, fmt); |
| 325 | vsnprintf(buff, sizeof(buff), fmt, vl); |
| 326 | va_end(vl); |
| 327 | |
| 328 | // note: this is still unsafe code |
| 329 | if (main_thread_id != sceKernelGetThreadId()) |
| 330 | { |
| 331 | le = malloc(sizeof(*le)); |
| 332 | if (le == NULL) return; |
| 333 | le->next = NULL; |
| 334 | strcpy(le->buff, buff); |
| 335 | if (le_root == NULL) le_root = le; |
| 336 | else { |
| 337 | for (le1 = le_root; le1->next != NULL; le1 = le1->next); |
| 338 | le1->next = le; |
| 339 | } |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | logfd = sceIoOpen(LOG_FILE, PSP_O_WRONLY|PSP_O_APPEND, 0777); |
| 344 | if (logfd < 0) { |
| 345 | if (msg_count == 0) logfd = -2; |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | if (le_root != NULL) |
| 350 | { |
| 351 | le1 = le_root; |
| 352 | le_root = NULL; |
| 353 | sceKernelDelayThread(1000); |
| 354 | while (le1 != NULL) { |
| 355 | le = le1; |
| 356 | le1 = le->next; |
| 357 | sceIoWrite(logfd, le->buff, strlen(le->buff)); |
| 358 | free(le); |
| 359 | msg_count++; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | sceIoWrite(logfd, buff, strlen(buff)); |
| 364 | msg_count++; |
| 365 | |
| 366 | // make sure it gets flushed |
| 367 | sceIoClose(logfd); |
| 368 | logfd = -1; |
| 369 | #endif |
| 370 | } |
| 371 | |
| 372 | |