final src and Makefile adjustments for PSP release
[libpicofe.git] / psp / psp.c
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
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdarg.h>
9 #include <string.h>
10
11 #include <pspkernel.h>
12 #include <pspiofilemgr.h>
13 #include <pspdisplay.h>
14 #include <psppower.h>
15 #include <psprtc.h>
16 #include <pspgu.h>
17
18 #include "psp.h"
19 #include "emu.h"
20 #include "../common/lprintf.h"
21
22 PSP_MODULE_INFO("PicoDrive", 0, 1, 34);
23 PSP_HEAP_SIZE_MAX();
24
25 unsigned int __attribute__((aligned(16))) guCmdList[GU_CMDLIST_SIZE];
26
27 void *psp_screen = VRAM_FB0;
28 static int current_screen = 0; /* front bufer */
29
30 static SceUID main_thread_id = -1;
31
32 #define ANALOG_DEADZONE 80
33
34 /* Exit callback */
35 static int exit_callback(int arg1, int arg2, void *common)
36 {
37         sceKernelExitGame();
38         return 0;
39 }
40
41 /* Power Callback */
42 static 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
54 /* Callback thread */
55 static int callback_thread(SceSize args, void *argp)
56 {
57         int cbid;
58
59         lprintf("callback_thread started with id %08x, priority %i\n",
60                 sceKernelGetThreadId(), sceKernelGetThreadCurrentPriority());
61
62         cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
63         sceKernelRegisterExitCallback(cbid);
64         cbid = sceKernelCreateCallback("Power Callback", power_callback, NULL);
65         scePowerRegisterCallback(0, cbid);
66
67         sceKernelSleepThreadCB();
68
69         return 0;
70 }
71
72 void psp_init(void)
73 {
74         SceUID thid;
75
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,
80                 sceKernelGetThreadCurrentPriority());
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);
90         sceDisplaySetFrameBuf(VRAM_FB1, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
91         current_screen = 1;
92         psp_screen = VRAM_FB0;
93
94         /* gu */
95         sceGuInit();
96
97         sceGuStart(GU_DIRECT, guCmdList);
98         sceGuDrawBuffer(GU_PSM_5650, (void *)VRAMOFFS_FB0, 512);
99         sceGuDispBuffer(480, 272, (void *)VRAMOFFS_FB1, 512); // don't care
100         sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT);
101         sceGuDepthBuffer((void *)VRAMOFFS_DEPTH, 512);
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);
107
108         sceGuDepthMask(0xffff);
109         sceGuDisable(GU_DEPTH_TEST);
110
111         sceGuFrontFace(GU_CW);
112         sceGuEnable(GU_TEXTURE_2D);
113         sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
114         sceGuAmbientColor(0xffffffff);
115         sceGuColor(0xffffffff);
116         sceGuFinish();
117         sceGuSync(0, 0);
118
119         sceDisplayWaitVblankStart();
120         sceGuDisplay(GU_TRUE);
121
122
123         /* input */
124         sceCtrlSetSamplingCycle(0);
125         sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
126 }
127
128 void psp_finish(void)
129 {
130         sceGuTerm();
131
132         //sceKernelSleepThread();
133         sceKernelExitGame();
134 }
135
136 void psp_video_flip(int wait_vsync)
137 {
138         if (wait_vsync) sceDisplayWaitVblankStart();
139         sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565,
140                 wait_vsync ? PSP_DISPLAY_SETBUF_IMMEDIATE : PSP_DISPLAY_SETBUF_NEXTFRAME);
141         current_screen ^= 1;
142         psp_screen = current_screen ? VRAM_FB0 : VRAM_FB1;
143 }
144
145 void *psp_video_get_active_fb(void)
146 {
147         return current_screen ? VRAM_FB1 : VRAM_FB0;
148 }
149
150 void psp_video_switch_to_single(void)
151 {
152         psp_screen = VRAM_FB0;
153         sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
154         current_screen = 0;
155 }
156
157 void psp_msleep(int ms)
158 {
159         sceKernelDelayThread(ms * 1000);
160 }
161
162 unsigned int psp_pad_read(int blocking)
163 {
164         unsigned int buttons;
165         SceCtrlData pad;
166         if (blocking)
167              sceCtrlReadBufferPositive(&pad, 1);
168         else sceCtrlPeekBufferPositive(&pad, 1);
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;
177
178         return buttons;
179 }
180
181 int psp_get_cpu_clock(void)
182 {
183         return scePowerGetCpuClockFrequencyInt();
184 }
185
186 int 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
194 char *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
211 /* alt logging */
212 #define LOG_FILE "log.txt"
213
214 typedef struct _log_entry
215 {
216         char buff[256];
217         struct _log_entry *next;
218 } log_entry;
219
220 static log_entry *le_root = NULL;
221
222 void lprintf_f(const char *fmt, ...)
223 {
224         va_list vl;
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;
232         char buff[256];
233         log_entry *le, *le1;
234
235         if (logfd == -2) return; // disabled
236
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())
243         {
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;
252                 }
253                 return;
254         }
255
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         }
274
275         sceIoWrite(logfd, buff, strlen(buff));
276
277         // make sure it gets flushed
278         sceIoClose(logfd);
279         logfd = -1;
280 #endif
281 }
282
283