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