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