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