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