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