bugfixes in sram memhandlers
[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{
117f236a 130 lprintf("psp_finish..\n");
2951214e 131 sceGuTerm();
132
133 //sceKernelSleepThread();
134 sceKernelExitGame();
135}
136
703e4c7b 137void psp_video_flip(int wait_vsync)
2951214e 138{
703e4c7b 139 if (wait_vsync) sceDisplayWaitVblankStart();
2b90fc61 140 sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565,
141 wait_vsync ? PSP_DISPLAY_SETBUF_IMMEDIATE : PSP_DISPLAY_SETBUF_NEXTFRAME);
2951214e 142 current_screen ^= 1;
db298784 143 psp_screen = current_screen ? VRAM_FB0 : VRAM_FB1;
703e4c7b 144}
145
146void *psp_video_get_active_fb(void)
147{
db298784 148 return current_screen ? VRAM_FB1 : VRAM_FB0;
2951214e 149}
150
151void psp_video_switch_to_single(void)
152{
db298784 153 psp_screen = VRAM_FB0;
2951214e 154 sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
155 current_screen = 0;
156}
157
158void psp_msleep(int ms)
159{
160 sceKernelDelayThread(ms * 1000);
161}
162
703e4c7b 163unsigned int psp_pad_read(int blocking)
2951214e 164{
16e89bed 165 unsigned int buttons;
2951214e 166 SceCtrlData pad;
703e4c7b 167 if (blocking)
168 sceCtrlReadBufferPositive(&pad, 1);
169 else sceCtrlPeekBufferPositive(&pad, 1);
16e89bed 170 buttons = pad.Buttons;
171
172 // analog..
173 buttons &= ~(BTN_NUB_UP|BTN_NUB_DOWN|BTN_NUB_LEFT|BTN_NUB_RIGHT);
174 if (pad.Lx < 128 - ANALOG_DEADZONE) buttons |= BTN_NUB_LEFT;
175 if (pad.Lx > 128 + ANALOG_DEADZONE) buttons |= BTN_NUB_RIGHT;
176 if (pad.Ly < 128 - ANALOG_DEADZONE) buttons |= BTN_NUB_UP;
177 if (pad.Ly > 128 + ANALOG_DEADZONE) buttons |= BTN_NUB_DOWN;
2951214e 178
16e89bed 179 return buttons;
2951214e 180}
181
2b90fc61 182int psp_get_cpu_clock(void)
183{
184 return scePowerGetCpuClockFrequencyInt();
185}
186
187int psp_set_cpu_clock(int clock)
188{
189 int ret = scePowerSetClockFrequency(clock, clock, clock/2);
190 if (ret != 0) lprintf("failed to set clock: %i\n", ret);
191
192 return ret;
193}
194
16e89bed 195char *psp_get_status_line(void)
196{
197 static char buff[64];
198 int ret, bat_percent, bat_time;
199 pspTime time;
200
201 ret = sceRtcGetCurrentClockLocalTime(&time);
202 bat_percent = scePowerGetBatteryLifePercent();
203 bat_time = scePowerGetBatteryLifeTime();
204 if (ret < 0 || bat_percent < 0 || bat_time < 0) return NULL;
205
206 snprintf(buff, sizeof(buff), "%02i:%02i bat: %3i%%", time.hour, time.minutes, bat_percent);
207 if (!scePowerIsPowerOnline())
208 snprintf(buff+strlen(buff), sizeof(buff)-strlen(buff), " (%i:%02i)", bat_time/60, bat_time%60);
209 return buff;
210}
211
703e4c7b 212/* alt logging */
fe9e3b25 213#define LOG_FILE "log.txt"
214
c6334564 215#ifndef LPRINTF_STDIO
fe9e3b25 216typedef struct _log_entry
217{
218 char buff[256];
219 struct _log_entry *next;
220} log_entry;
703e4c7b 221
fe9e3b25 222static log_entry *le_root = NULL;
c6334564 223#endif
16e89bed 224
703e4c7b 225void lprintf_f(const char *fmt, ...)
226{
227 va_list vl;
fe9e3b25 228
229#ifdef LPRINTF_STDIO
230 va_start(vl, fmt);
231 vprintf(fmt, vl);
232 va_end(vl);
233#else
234 static SceUID logfd = -1;
703e4c7b 235 char buff[256];
fe9e3b25 236 log_entry *le, *le1;
703e4c7b 237
16e89bed 238 if (logfd == -2) return; // disabled
239
fe9e3b25 240 va_start(vl, fmt);
241 vsnprintf(buff, sizeof(buff), fmt, vl);
242 va_end(vl);
243
244 // note: this is still unsafe code
245 if (main_thread_id != sceKernelGetThreadId())
703e4c7b 246 {
fe9e3b25 247 le = malloc(sizeof(*le));
248 if (le == NULL) return;
249 le->next = NULL;
250 strcpy(le->buff, buff);
251 if (le_root == NULL) le_root = le;
252 else {
253 for (le1 = le_root; le1->next != NULL; le1 = le1->next);
254 le1->next = le;
16e89bed 255 }
fe9e3b25 256 return;
703e4c7b 257 }
258
fe9e3b25 259 logfd = sceIoOpen(LOG_FILE, PSP_O_WRONLY|PSP_O_APPEND, 0777);
260 if (logfd < 0) {
261 logfd = -2;
262 return;
263 }
264
265 if (le_root != NULL)
266 {
267 le1 = le_root;
268 le_root = NULL;
269 sceKernelDelayThread(1000);
270 while (le1 != NULL) {
271 le = le1;
272 le1 = le->next;
273 sceIoWrite(logfd, le->buff, strlen(le->buff));
274 free(le);
275 }
276 }
703e4c7b 277
278 sceIoWrite(logfd, buff, strlen(buff));
16e89bed 279
280 // make sure it gets flushed
281 sceIoClose(logfd);
282 logfd = -1;
fe9e3b25 283#endif
703e4c7b 284}
285
286