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