various bugfixes
[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>
2b90fc61 8#include <psppower.h>
2951214e 9#include <pspgu.h>
10
11#include "psp.h"
12#include "../common/lprintf.h"
13
14PSP_MODULE_INFO("PicoDrive", 0, 1, 34);
15
db298784 16unsigned int __attribute__((aligned(16))) guCmdList[GU_CMDLIST_SIZE];
17
18void *psp_screen = VRAM_FB0;
2951214e 19static int current_screen = 0; /* front bufer */
20
703e4c7b 21static SceUID logfd = -1;
2951214e 22
23/* Exit callback */
24static int exit_callback(int arg1, int arg2, void *common)
25{
26 sceKernelExitGame();
27 return 0;
28}
29
30/* Callback thread */
31static int callback_thread(SceSize args, void *argp)
32{
33 int cbid;
34
0953046b 35 lprintf("callback_thread started with id %i, priority %i\n",
36 sceKernelGetThreadId(), sceKernelGetThreadCurrentPriority());
2951214e 37
38 cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
39 sceKernelRegisterExitCallback(cbid);
40
41 sceKernelSleepThreadCB();
42
43 return 0;
44}
45
46void psp_init(void)
47{
0953046b 48 SceUID thid;
2951214e 49
0953046b 50 lprintf("entered psp_init, threadId %i, priority %i\n", sceKernelGetThreadId(),
51 sceKernelGetThreadCurrentPriority());
2951214e 52
53 thid = sceKernelCreateThread("update_thread", callback_thread, 0x11, 0xFA0, 0, 0);
54 if (thid >= 0)
55 {
56 sceKernelStartThread(thid, 0, 0);
57 }
58
59 /* video */
60 sceDisplaySetMode(0, 480, 272);
db298784 61 sceDisplaySetFrameBuf(VRAM_FB1, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
2951214e 62 current_screen = 1;
db298784 63 psp_screen = VRAM_FB0;
2951214e 64
65 /* gu */
66 sceGuInit();
67
db298784 68 sceGuStart(GU_DIRECT, guCmdList);
6f748c47 69 sceGuDrawBuffer(GU_PSM_5650, (void *)VRAMOFFS_FB0, 512);
70 sceGuDispBuffer(480, 272, (void *)VRAMOFFS_FB1, 512); // don't care
db298784 71 sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT);
6f748c47 72 sceGuDepthBuffer((void *)VRAMOFFS_DEPTH, 512);
db298784 73 sceGuOffset(2048 - (480 / 2), 2048 - (272 / 2));
74 sceGuViewport(2048, 2048, 480, 272);
75 sceGuDepthRange(0xc350, 0x2710);
76 sceGuScissor(0, 0, 480, 272);
77 sceGuEnable(GU_SCISSOR_TEST);
db298784 78
79 sceGuDepthMask(0xffff);
80 sceGuDisable(GU_DEPTH_TEST);
81
82 sceGuFrontFace(GU_CW);
db298784 83 sceGuEnable(GU_TEXTURE_2D);
db298784 84 sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
6f748c47 85 sceGuAmbientColor(0xffffffff);
86 sceGuColor(0xffffffff);
db298784 87 sceGuFinish();
88 sceGuSync(0, 0);
89
90 sceDisplayWaitVblankStart();
91 sceGuDisplay(GU_TRUE);
92
93
2951214e 94 /* input */
95 sceCtrlSetSamplingCycle(0);
96 sceCtrlSetSamplingMode(0);
97}
98
99void psp_finish(void)
100{
101 sceGuTerm();
102
103 //sceKernelSleepThread();
104 sceKernelExitGame();
105}
106
703e4c7b 107void psp_video_flip(int wait_vsync)
2951214e 108{
703e4c7b 109 if (wait_vsync) sceDisplayWaitVblankStart();
2b90fc61 110 sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565,
111 wait_vsync ? PSP_DISPLAY_SETBUF_IMMEDIATE : PSP_DISPLAY_SETBUF_NEXTFRAME);
2951214e 112 current_screen ^= 1;
db298784 113 psp_screen = current_screen ? VRAM_FB0 : VRAM_FB1;
703e4c7b 114}
115
116void *psp_video_get_active_fb(void)
117{
db298784 118 return current_screen ? VRAM_FB1 : VRAM_FB0;
2951214e 119}
120
121void psp_video_switch_to_single(void)
122{
db298784 123 psp_screen = VRAM_FB0;
2951214e 124 sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
125 current_screen = 0;
126}
127
128void psp_msleep(int ms)
129{
130 sceKernelDelayThread(ms * 1000);
131}
132
703e4c7b 133unsigned int psp_pad_read(int blocking)
2951214e 134{
135 SceCtrlData pad;
703e4c7b 136 if (blocking)
137 sceCtrlReadBufferPositive(&pad, 1);
138 else sceCtrlPeekBufferPositive(&pad, 1);
2951214e 139
140 return pad.Buttons;
141}
142
2b90fc61 143int psp_get_cpu_clock(void)
144{
145 return scePowerGetCpuClockFrequencyInt();
146}
147
148int psp_set_cpu_clock(int clock)
149{
150 int ret = scePowerSetClockFrequency(clock, clock, clock/2);
151 if (ret != 0) lprintf("failed to set clock: %i\n", ret);
152
153 return ret;
154}
155
703e4c7b 156/* alt logging */
157#define LOG_FILE "log.log"
158
159void lprintf_f(const char *fmt, ...)
160{
161 va_list vl;
162 char buff[256];
163
164 if (logfd < 0)
165 {
166 logfd = sceIoOpen(LOG_FILE, PSP_O_WRONLY|PSP_O_APPEND, 0777);
167 if (logfd < 0)
168 return;
169 }
170
171 va_start(vl, fmt);
172 vsnprintf(buff, sizeof(buff), fmt, vl);
173 va_end(vl);
174
175 sceIoWrite(logfd, buff, strlen(buff));
176//sceKernelDelayThread(200 * 1000);
177sceIoClose(logfd);
178logfd = -1;
179}
180
181