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