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