psp mp3 implementation
[picodrive.git] / platform / psp / psp.c
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <string.h>
4
5 #include <pspkernel.h>
6 #include <pspiofilemgr.h>
7 #include <pspdisplay.h>
8 #include <psppower.h>
9 #include <psprtc.h>
10 #include <pspgu.h>
11
12 #include "psp.h"
13 #include "../common/lprintf.h"
14
15 PSP_MODULE_INFO("PicoDrive", 0, 1, 34);
16
17 unsigned int __attribute__((aligned(16))) guCmdList[GU_CMDLIST_SIZE];
18
19 void *psp_screen = VRAM_FB0;
20 static int current_screen = 0; /* front bufer */
21
22 #define ANALOG_DEADZONE 80
23
24 /* Exit callback */
25 static int exit_callback(int arg1, int arg2, void *common)
26 {
27         sceKernelExitGame();
28         return 0;
29 }
30
31 /* Callback thread */
32 static int callback_thread(SceSize args, void *argp)
33 {
34         int cbid;
35
36         lprintf("callback_thread started with id %08x, priority %i\n",
37                 sceKernelGetThreadId(), sceKernelGetThreadCurrentPriority());
38
39         cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
40         sceKernelRegisterExitCallback(cbid);
41
42         sceKernelSleepThreadCB();
43
44         return 0;
45 }
46
47 void psp_init(void)
48 {
49         SceUID thid;
50
51         lprintf("running in %08x kernel\n", sceKernelDevkitVersion()),
52         lprintf("entered psp_init, threadId %08x, priority %i\n", sceKernelGetThreadId(),
53                 sceKernelGetThreadCurrentPriority());
54
55         thid = sceKernelCreateThread("update_thread", callback_thread, 0x11, 0xFA0, 0, 0);
56         if (thid >= 0)
57         {
58                 sceKernelStartThread(thid, 0, 0);
59         }
60
61         /* video */
62         sceDisplaySetMode(0, 480, 272);
63         sceDisplaySetFrameBuf(VRAM_FB1, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
64         current_screen = 1;
65         psp_screen = VRAM_FB0;
66
67         /* gu */
68         sceGuInit();
69
70         sceGuStart(GU_DIRECT, guCmdList);
71         sceGuDrawBuffer(GU_PSM_5650, (void *)VRAMOFFS_FB0, 512);
72         sceGuDispBuffer(480, 272, (void *)VRAMOFFS_FB1, 512); // don't care
73         sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT);
74         sceGuDepthBuffer((void *)VRAMOFFS_DEPTH, 512);
75         sceGuOffset(2048 - (480 / 2), 2048 - (272 / 2));
76         sceGuViewport(2048, 2048, 480, 272);
77         sceGuDepthRange(0xc350, 0x2710);
78         sceGuScissor(0, 0, 480, 272);
79         sceGuEnable(GU_SCISSOR_TEST);
80
81         sceGuDepthMask(0xffff);
82         sceGuDisable(GU_DEPTH_TEST);
83
84         sceGuFrontFace(GU_CW);
85         sceGuEnable(GU_TEXTURE_2D);
86         sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
87         sceGuAmbientColor(0xffffffff);
88         sceGuColor(0xffffffff);
89         sceGuFinish();
90         sceGuSync(0, 0);
91
92         sceDisplayWaitVblankStart();
93         sceGuDisplay(GU_TRUE);
94
95
96         /* input */
97         sceCtrlSetSamplingCycle(0);
98         sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
99 }
100
101 void psp_finish(void)
102 {
103         sceGuTerm();
104
105         //sceKernelSleepThread();
106         sceKernelExitGame();
107 }
108
109 void psp_video_flip(int wait_vsync)
110 {
111         if (wait_vsync) sceDisplayWaitVblankStart();
112         sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565,
113                 wait_vsync ? PSP_DISPLAY_SETBUF_IMMEDIATE : PSP_DISPLAY_SETBUF_NEXTFRAME);
114         current_screen ^= 1;
115         psp_screen = current_screen ? VRAM_FB0 : VRAM_FB1;
116 }
117
118 void *psp_video_get_active_fb(void)
119 {
120         return current_screen ? VRAM_FB1 : VRAM_FB0;
121 }
122
123 void psp_video_switch_to_single(void)
124 {
125         psp_screen = VRAM_FB0;
126         sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
127         current_screen = 0;
128 }
129
130 void psp_msleep(int ms)
131 {
132         sceKernelDelayThread(ms * 1000);
133 }
134
135 unsigned int psp_pad_read(int blocking)
136 {
137         unsigned int buttons;
138         SceCtrlData pad;
139         if (blocking)
140              sceCtrlReadBufferPositive(&pad, 1);
141         else sceCtrlPeekBufferPositive(&pad, 1);
142         buttons = pad.Buttons;
143
144         // analog..
145         buttons &= ~(BTN_NUB_UP|BTN_NUB_DOWN|BTN_NUB_LEFT|BTN_NUB_RIGHT);
146         if (pad.Lx < 128 - ANALOG_DEADZONE) buttons |= BTN_NUB_LEFT;
147         if (pad.Lx > 128 + ANALOG_DEADZONE) buttons |= BTN_NUB_RIGHT;
148         if (pad.Ly < 128 - ANALOG_DEADZONE) buttons |= BTN_NUB_UP;
149         if (pad.Ly > 128 + ANALOG_DEADZONE) buttons |= BTN_NUB_DOWN;
150
151         return buttons;
152 }
153
154 int psp_get_cpu_clock(void)
155 {
156         return scePowerGetCpuClockFrequencyInt();
157 }
158
159 int psp_set_cpu_clock(int clock)
160 {
161         int ret = scePowerSetClockFrequency(clock, clock, clock/2);
162         if (ret != 0) lprintf("failed to set clock: %i\n", ret);
163
164         return ret;
165 }
166
167 char *psp_get_status_line(void)
168 {
169         static char buff[64];
170         int ret, bat_percent, bat_time;
171         pspTime time;
172
173         ret = sceRtcGetCurrentClockLocalTime(&time);
174         bat_percent = scePowerGetBatteryLifePercent();
175         bat_time = scePowerGetBatteryLifeTime();
176         if (ret < 0 || bat_percent < 0 || bat_time < 0) return NULL;
177
178         snprintf(buff, sizeof(buff), "%02i:%02i  bat: %3i%%", time.hour, time.minutes, bat_percent);
179         if (!scePowerIsPowerOnline())
180                 snprintf(buff+strlen(buff), sizeof(buff)-strlen(buff), " (%i:%02i)", bat_time/60, bat_time%60);
181         return buff;
182 }
183
184 /* alt logging */
185 #define LOG_FILE "log.log"
186
187 static SceUID logfd = -1;
188
189 void lprintf_f(const char *fmt, ...)
190 {
191         va_list vl;
192         char buff[256];
193
194         if (logfd == -2) return; // disabled
195
196         if (logfd < 0)
197         {
198                 logfd = sceIoOpen(LOG_FILE, PSP_O_WRONLY|PSP_O_APPEND, 0777);
199                 if (logfd < 0) {
200                         logfd = -2;
201                         return;
202                 }
203         }
204
205         va_start(vl, fmt);
206         vsnprintf(buff, sizeof(buff), fmt, vl);
207         va_end(vl);
208
209         sceIoWrite(logfd, buff, strlen(buff));
210
211         // make sure it gets flushed
212         sceIoClose(logfd);
213         logfd = -1;
214 }
215
216