optimizations, fixes, hacks, psp, ...
[libpicofe.git] / psp / psp.c
CommitLineData
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"
5ecedd0c 13#include "emu.h"
2951214e 14#include "../common/lprintf.h"
15
16PSP_MODULE_INFO("PicoDrive", 0, 1, 34);
17
db298784 18unsigned int __attribute__((aligned(16))) guCmdList[GU_CMDLIST_SIZE];
19
20void *psp_screen = VRAM_FB0;
2951214e 21static int current_screen = 0; /* front bufer */
22
16e89bed 23#define ANALOG_DEADZONE 80
2951214e 24
25/* Exit callback */
26static int exit_callback(int arg1, int arg2, void *common)
27{
28 sceKernelExitGame();
29 return 0;
30}
31
5ecedd0c 32/* Power Callback */
33static 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
2951214e 45/* Callback thread */
46static int callback_thread(SceSize args, void *argp)
47{
48 int cbid;
49
a6df06b7 50 lprintf("callback_thread started with id %08x, priority %i\n",
0953046b 51 sceKernelGetThreadId(), sceKernelGetThreadCurrentPriority());
2951214e 52
53 cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
54 sceKernelRegisterExitCallback(cbid);
5ecedd0c 55 cbid = sceKernelCreateCallback("Power Callback", power_callback, NULL);
56 scePowerRegisterCallback(0, cbid);
2951214e 57
58 sceKernelSleepThreadCB();
59
60 return 0;
61}
62
63void psp_init(void)
64{
0953046b 65 SceUID thid;
2951214e 66
16e89bed 67 lprintf("running in %08x kernel\n", sceKernelDevkitVersion()),
a6df06b7 68 lprintf("entered psp_init, threadId %08x, priority %i\n", sceKernelGetThreadId(),
0953046b 69 sceKernelGetThreadCurrentPriority());
2951214e 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);
db298784 79 sceDisplaySetFrameBuf(VRAM_FB1, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
2951214e 80 current_screen = 1;
db298784 81 psp_screen = VRAM_FB0;
2951214e 82
83 /* gu */
84 sceGuInit();
85
db298784 86 sceGuStart(GU_DIRECT, guCmdList);
6f748c47 87 sceGuDrawBuffer(GU_PSM_5650, (void *)VRAMOFFS_FB0, 512);
88 sceGuDispBuffer(480, 272, (void *)VRAMOFFS_FB1, 512); // don't care
db298784 89 sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT);
6f748c47 90 sceGuDepthBuffer((void *)VRAMOFFS_DEPTH, 512);
db298784 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);
db298784 96
97 sceGuDepthMask(0xffff);
98 sceGuDisable(GU_DEPTH_TEST);
99
100 sceGuFrontFace(GU_CW);
db298784 101 sceGuEnable(GU_TEXTURE_2D);
db298784 102 sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
6f748c47 103 sceGuAmbientColor(0xffffffff);
104 sceGuColor(0xffffffff);
db298784 105 sceGuFinish();
106 sceGuSync(0, 0);
107
108 sceDisplayWaitVblankStart();
109 sceGuDisplay(GU_TRUE);
110
111
2951214e 112 /* input */
113 sceCtrlSetSamplingCycle(0);
16e89bed 114 sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
2951214e 115}
116
117void psp_finish(void)
118{
119 sceGuTerm();
120
121 //sceKernelSleepThread();
122 sceKernelExitGame();
123}
124
703e4c7b 125void psp_video_flip(int wait_vsync)
2951214e 126{
703e4c7b 127 if (wait_vsync) sceDisplayWaitVblankStart();
2b90fc61 128 sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565,
129 wait_vsync ? PSP_DISPLAY_SETBUF_IMMEDIATE : PSP_DISPLAY_SETBUF_NEXTFRAME);
2951214e 130 current_screen ^= 1;
db298784 131 psp_screen = current_screen ? VRAM_FB0 : VRAM_FB1;
703e4c7b 132}
133
134void *psp_video_get_active_fb(void)
135{
db298784 136 return current_screen ? VRAM_FB1 : VRAM_FB0;
2951214e 137}
138
139void psp_video_switch_to_single(void)
140{
db298784 141 psp_screen = VRAM_FB0;
2951214e 142 sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
143 current_screen = 0;
144}
145
146void psp_msleep(int ms)
147{
148 sceKernelDelayThread(ms * 1000);
149}
150
703e4c7b 151unsigned int psp_pad_read(int blocking)
2951214e 152{
16e89bed 153 unsigned int buttons;
2951214e 154 SceCtrlData pad;
703e4c7b 155 if (blocking)
156 sceCtrlReadBufferPositive(&pad, 1);
157 else sceCtrlPeekBufferPositive(&pad, 1);
16e89bed 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;
2951214e 166
16e89bed 167 return buttons;
2951214e 168}
169
2b90fc61 170int psp_get_cpu_clock(void)
171{
172 return scePowerGetCpuClockFrequencyInt();
173}
174
175int 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
16e89bed 183char *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
703e4c7b 200/* alt logging */
201#define LOG_FILE "log.log"
202
16e89bed 203static SceUID logfd = -1;
204
703e4c7b 205void lprintf_f(const char *fmt, ...)
206{
207 va_list vl;
208 char buff[256];
209
16e89bed 210 if (logfd == -2) return; // disabled
211
703e4c7b 212 if (logfd < 0)
213 {
214 logfd = sceIoOpen(LOG_FILE, PSP_O_WRONLY|PSP_O_APPEND, 0777);
16e89bed 215 if (logfd < 0) {
216 logfd = -2;
703e4c7b 217 return;
16e89bed 218 }
703e4c7b 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));
16e89bed 226
227 // make sure it gets flushed
228 sceIoClose(logfd);
229 logfd = -1;
703e4c7b 230}
231
232