703e4c7b |
1 | #include <stdio.h> |
2 | #include <stdarg.h> |
3 | #include <string.h> |
4 | |
2951214e |
5 | #include <pspkernel.h> |
703e4c7b |
6 | #include <pspiofilemgr.h> |
2951214e |
7 | #include <pspdisplay.h> |
2b90fc61 |
8 | #include <psppower.h> |
16e89bed |
9 | #include <psprtc.h> |
2951214e |
10 | #include <pspgu.h> |
11 | |
12 | #include "psp.h" |
13 | #include "../common/lprintf.h" |
14 | |
15 | PSP_MODULE_INFO("PicoDrive", 0, 1, 34); |
16 | |
db298784 |
17 | unsigned int __attribute__((aligned(16))) guCmdList[GU_CMDLIST_SIZE]; |
18 | |
19 | void *psp_screen = VRAM_FB0; |
2951214e |
20 | static int current_screen = 0; /* front bufer */ |
21 | |
16e89bed |
22 | #define ANALOG_DEADZONE 80 |
2951214e |
23 | |
24 | /* Exit callback */ |
25 | static int exit_callback(int arg1, int arg2, void *common) |
26 | { |
27 | sceKernelExitGame(); |
28 | return 0; |
29 | } |
30 | |
31 | /* Callback thread */ |
32 | static int callback_thread(SceSize args, void *argp) |
33 | { |
34 | int cbid; |
35 | |
a6df06b7 |
36 | lprintf("callback_thread started with id %08x, priority %i\n", |
0953046b |
37 | sceKernelGetThreadId(), sceKernelGetThreadCurrentPriority()); |
2951214e |
38 | |
39 | cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL); |
40 | sceKernelRegisterExitCallback(cbid); |
41 | |
42 | sceKernelSleepThreadCB(); |
43 | |
44 | return 0; |
45 | } |
46 | |
47 | void psp_init(void) |
48 | { |
0953046b |
49 | SceUID thid; |
2951214e |
50 | |
16e89bed |
51 | lprintf("running in %08x kernel\n", sceKernelDevkitVersion()), |
a6df06b7 |
52 | lprintf("entered psp_init, threadId %08x, priority %i\n", sceKernelGetThreadId(), |
0953046b |
53 | sceKernelGetThreadCurrentPriority()); |
2951214e |
54 | |
55 | thid = sceKernelCreateThread("update_thread", callback_thread, 0x11, 0xFA0, 0, 0); |
56 | if (thid >= 0) |
57 | { |
58 | sceKernelStartThread(thid, 0, 0); |
59 | } |
60 | |
61 | /* video */ |
62 | sceDisplaySetMode(0, 480, 272); |
db298784 |
63 | sceDisplaySetFrameBuf(VRAM_FB1, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME); |
2951214e |
64 | current_screen = 1; |
db298784 |
65 | psp_screen = VRAM_FB0; |
2951214e |
66 | |
67 | /* gu */ |
68 | sceGuInit(); |
69 | |
db298784 |
70 | sceGuStart(GU_DIRECT, guCmdList); |
6f748c47 |
71 | sceGuDrawBuffer(GU_PSM_5650, (void *)VRAMOFFS_FB0, 512); |
72 | sceGuDispBuffer(480, 272, (void *)VRAMOFFS_FB1, 512); // don't care |
db298784 |
73 | sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT); |
6f748c47 |
74 | sceGuDepthBuffer((void *)VRAMOFFS_DEPTH, 512); |
db298784 |
75 | sceGuOffset(2048 - (480 / 2), 2048 - (272 / 2)); |
76 | sceGuViewport(2048, 2048, 480, 272); |
77 | sceGuDepthRange(0xc350, 0x2710); |
78 | sceGuScissor(0, 0, 480, 272); |
79 | sceGuEnable(GU_SCISSOR_TEST); |
db298784 |
80 | |
81 | sceGuDepthMask(0xffff); |
82 | sceGuDisable(GU_DEPTH_TEST); |
83 | |
84 | sceGuFrontFace(GU_CW); |
db298784 |
85 | sceGuEnable(GU_TEXTURE_2D); |
db298784 |
86 | sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB); |
6f748c47 |
87 | sceGuAmbientColor(0xffffffff); |
88 | sceGuColor(0xffffffff); |
db298784 |
89 | sceGuFinish(); |
90 | sceGuSync(0, 0); |
91 | |
92 | sceDisplayWaitVblankStart(); |
93 | sceGuDisplay(GU_TRUE); |
94 | |
95 | |
2951214e |
96 | /* input */ |
97 | sceCtrlSetSamplingCycle(0); |
16e89bed |
98 | sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG); |
2951214e |
99 | } |
100 | |
101 | void psp_finish(void) |
102 | { |
103 | sceGuTerm(); |
104 | |
105 | //sceKernelSleepThread(); |
106 | sceKernelExitGame(); |
107 | } |
108 | |
703e4c7b |
109 | void psp_video_flip(int wait_vsync) |
2951214e |
110 | { |
703e4c7b |
111 | if (wait_vsync) sceDisplayWaitVblankStart(); |
2b90fc61 |
112 | sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, |
113 | wait_vsync ? PSP_DISPLAY_SETBUF_IMMEDIATE : PSP_DISPLAY_SETBUF_NEXTFRAME); |
2951214e |
114 | current_screen ^= 1; |
db298784 |
115 | psp_screen = current_screen ? VRAM_FB0 : VRAM_FB1; |
703e4c7b |
116 | } |
117 | |
118 | void *psp_video_get_active_fb(void) |
119 | { |
db298784 |
120 | return current_screen ? VRAM_FB1 : VRAM_FB0; |
2951214e |
121 | } |
122 | |
123 | void psp_video_switch_to_single(void) |
124 | { |
db298784 |
125 | psp_screen = VRAM_FB0; |
2951214e |
126 | sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME); |
127 | current_screen = 0; |
128 | } |
129 | |
130 | void psp_msleep(int ms) |
131 | { |
132 | sceKernelDelayThread(ms * 1000); |
133 | } |
134 | |
703e4c7b |
135 | unsigned int psp_pad_read(int blocking) |
2951214e |
136 | { |
16e89bed |
137 | unsigned int buttons; |
2951214e |
138 | SceCtrlData pad; |
703e4c7b |
139 | if (blocking) |
140 | sceCtrlReadBufferPositive(&pad, 1); |
141 | else sceCtrlPeekBufferPositive(&pad, 1); |
16e89bed |
142 | buttons = pad.Buttons; |
143 | |
144 | // analog.. |
145 | buttons &= ~(BTN_NUB_UP|BTN_NUB_DOWN|BTN_NUB_LEFT|BTN_NUB_RIGHT); |
146 | if (pad.Lx < 128 - ANALOG_DEADZONE) buttons |= BTN_NUB_LEFT; |
147 | if (pad.Lx > 128 + ANALOG_DEADZONE) buttons |= BTN_NUB_RIGHT; |
148 | if (pad.Ly < 128 - ANALOG_DEADZONE) buttons |= BTN_NUB_UP; |
149 | if (pad.Ly > 128 + ANALOG_DEADZONE) buttons |= BTN_NUB_DOWN; |
2951214e |
150 | |
16e89bed |
151 | return buttons; |
2951214e |
152 | } |
153 | |
2b90fc61 |
154 | int psp_get_cpu_clock(void) |
155 | { |
156 | return scePowerGetCpuClockFrequencyInt(); |
157 | } |
158 | |
159 | int psp_set_cpu_clock(int clock) |
160 | { |
161 | int ret = scePowerSetClockFrequency(clock, clock, clock/2); |
162 | if (ret != 0) lprintf("failed to set clock: %i\n", ret); |
163 | |
164 | return ret; |
165 | } |
166 | |
16e89bed |
167 | char *psp_get_status_line(void) |
168 | { |
169 | static char buff[64]; |
170 | int ret, bat_percent, bat_time; |
171 | pspTime time; |
172 | |
173 | ret = sceRtcGetCurrentClockLocalTime(&time); |
174 | bat_percent = scePowerGetBatteryLifePercent(); |
175 | bat_time = scePowerGetBatteryLifeTime(); |
176 | if (ret < 0 || bat_percent < 0 || bat_time < 0) return NULL; |
177 | |
178 | snprintf(buff, sizeof(buff), "%02i:%02i bat: %3i%%", time.hour, time.minutes, bat_percent); |
179 | if (!scePowerIsPowerOnline()) |
180 | snprintf(buff+strlen(buff), sizeof(buff)-strlen(buff), " (%i:%02i)", bat_time/60, bat_time%60); |
181 | return buff; |
182 | } |
183 | |
703e4c7b |
184 | /* alt logging */ |
185 | #define LOG_FILE "log.log" |
186 | |
16e89bed |
187 | static SceUID logfd = -1; |
188 | |
703e4c7b |
189 | void lprintf_f(const char *fmt, ...) |
190 | { |
191 | va_list vl; |
192 | char buff[256]; |
193 | |
16e89bed |
194 | if (logfd == -2) return; // disabled |
195 | |
703e4c7b |
196 | if (logfd < 0) |
197 | { |
198 | logfd = sceIoOpen(LOG_FILE, PSP_O_WRONLY|PSP_O_APPEND, 0777); |
16e89bed |
199 | if (logfd < 0) { |
200 | logfd = -2; |
703e4c7b |
201 | return; |
16e89bed |
202 | } |
703e4c7b |
203 | } |
204 | |
205 | va_start(vl, fmt); |
206 | vsnprintf(buff, sizeof(buff), fmt, vl); |
207 | va_end(vl); |
208 | |
209 | sceIoWrite(logfd, buff, strlen(buff)); |
16e89bed |
210 | |
211 | // make sure it gets flushed |
212 | sceIoClose(logfd); |
213 | logfd = -1; |
703e4c7b |
214 | } |
215 | |
216 | |