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