psp port runs, bad colors
[libpicofe.git] / psp / psp.c
CommitLineData
703e4c7b 1#include <stdio.h>
2#include <stdarg.h>
3#include <string.h>
4
2951214e 5#include <pspkernel.h>
703e4c7b 6#include <pspiofilemgr.h>
2951214e 7#include <pspdisplay.h>
8#include <pspgu.h>
9
10#include "psp.h"
11#include "../common/lprintf.h"
12
13PSP_MODULE_INFO("PicoDrive", 0, 1, 34);
14
15void *psp_screen = PSP_VRAM_BASE0;
16static int current_screen = 0; /* front bufer */
17
703e4c7b 18static SceUID logfd = -1;
2951214e 19
20/* Exit callback */
21static int exit_callback(int arg1, int arg2, void *common)
22{
23 sceKernelExitGame();
24 return 0;
25}
26
27/* Callback thread */
28static 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
42void 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
68void psp_finish(void)
69{
70 sceGuTerm();
71
72 //sceKernelSleepThread();
73 sceKernelExitGame();
74}
75
703e4c7b 76void psp_video_flip(int wait_vsync)
2951214e 77{
703e4c7b 78 if (wait_vsync) sceDisplayWaitVblankStart();
2951214e 79 sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
80 current_screen ^= 1;
703e4c7b 81 psp_screen = current_screen ? PSP_VRAM_BASE0 : PSP_VRAM_BASE1;
82}
83
84void *psp_video_get_active_fb(void)
85{
86 return current_screen ? PSP_VRAM_BASE1 : PSP_VRAM_BASE0;
2951214e 87}
88
89void 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
96void psp_msleep(int ms)
97{
98 sceKernelDelayThread(ms * 1000);
99}
100
703e4c7b 101unsigned int psp_pad_read(int blocking)
2951214e 102{
103 SceCtrlData pad;
703e4c7b 104 if (blocking)
105 sceCtrlReadBufferPositive(&pad, 1);
106 else sceCtrlPeekBufferPositive(&pad, 1);
2951214e 107
108 return pad.Buttons;
109}
110
703e4c7b 111/* alt logging */
112#define LOG_FILE "log.log"
113
114void 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);
132sceIoClose(logfd);
133logfd = -1;
134}
135
136