FAME + some random stuff added
[libpicofe.git] / 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 <pspgu.h>
10
11 #include "psp.h"
12 #include "../common/lprintf.h"
13
14 PSP_MODULE_INFO("PicoDrive", 0, 1, 34);
15
16 void *psp_screen = PSP_VRAM_BASE0;
17 static int current_screen = 0; /* front bufer */
18
19 static SceUID logfd = -1;
20
21 /* Exit callback */
22 static int exit_callback(int arg1, int arg2, void *common)
23 {
24         sceKernelExitGame();
25         return 0;
26 }
27
28 /* Callback thread */
29 static int callback_thread(SceSize args, void *argp)
30 {
31         int cbid;
32
33         lprintf("callback_thread started with id %i\n", sceKernelGetThreadId());
34
35         cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
36         sceKernelRegisterExitCallback(cbid);
37
38         sceKernelSleepThreadCB();
39
40         return 0;
41 }
42
43 void psp_init(void)
44 {
45         int thid;
46
47         lprintf("entered psp_init, threadId %i\n", sceKernelGetThreadId());
48
49         thid = sceKernelCreateThread("update_thread", callback_thread, 0x11, 0xFA0, 0, 0);
50         if (thid >= 0)
51         {
52                 sceKernelStartThread(thid, 0, 0);
53         }
54
55         /* video */
56         sceDisplaySetMode(0, 480, 272);
57         sceDisplaySetFrameBuf(PSP_VRAM_BASE1, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
58         current_screen = 1;
59         psp_screen = PSP_VRAM_BASE0;
60
61         /* gu */
62         sceGuInit();
63
64         /* input */
65         sceCtrlSetSamplingCycle(0);
66         sceCtrlSetSamplingMode(0);
67 }
68
69 void psp_finish(void)
70 {
71         sceGuTerm();
72
73         //sceKernelSleepThread();
74         sceKernelExitGame();
75 }
76
77 void psp_video_flip(int wait_vsync)
78 {
79         if (wait_vsync) sceDisplayWaitVblankStart();
80         sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565,
81                 wait_vsync ? PSP_DISPLAY_SETBUF_IMMEDIATE : PSP_DISPLAY_SETBUF_NEXTFRAME);
82         current_screen ^= 1;
83         psp_screen = current_screen ? PSP_VRAM_BASE0 : PSP_VRAM_BASE1;
84 }
85
86 void *psp_video_get_active_fb(void)
87 {
88         return current_screen ? PSP_VRAM_BASE1 : PSP_VRAM_BASE0;
89 }
90
91 void psp_video_switch_to_single(void)
92 {
93         psp_screen = PSP_VRAM_BASE0;
94         sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
95         current_screen = 0;
96 }
97
98 void psp_msleep(int ms)
99 {
100         sceKernelDelayThread(ms * 1000);
101 }
102
103 unsigned int psp_pad_read(int blocking)
104 {
105         SceCtrlData pad;
106         if (blocking)
107              sceCtrlReadBufferPositive(&pad, 1);
108         else sceCtrlPeekBufferPositive(&pad, 1);
109
110         return pad.Buttons;
111 }
112
113 int psp_get_cpu_clock(void)
114 {
115         return scePowerGetCpuClockFrequencyInt();
116 }
117
118 int psp_set_cpu_clock(int clock)
119 {
120         int ret = scePowerSetClockFrequency(clock, clock, clock/2);
121         if (ret != 0) lprintf("failed to set clock: %i\n", ret);
122
123         return ret;
124 }
125
126 /* alt logging */
127 #define LOG_FILE "log.log"
128
129 void lprintf_f(const char *fmt, ...)
130 {
131         va_list vl;
132         char buff[256];
133
134         if (logfd < 0)
135         {
136                 logfd = sceIoOpen(LOG_FILE, PSP_O_WRONLY|PSP_O_APPEND, 0777);
137                 if (logfd < 0)
138                         return;
139         }
140
141         va_start(vl, fmt);
142         vsnprintf(buff, sizeof(buff), fmt, vl);
143         va_end(vl);
144
145         sceIoWrite(logfd, buff, strlen(buff));
146 //sceKernelDelayThread(200 * 1000);
147 sceIoClose(logfd);
148 logfd = -1;
149 }
150
151