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