some work on PSP CLUT
[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, VRAMOFFS_FB0, 512); // point to back fb?
68         sceGuDispBuffer(480, 272, VRAMOFFS_FB1, 512);
69         sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT);
70         sceGuDepthBuffer(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 //      sceGuAlphaFunc(GU_GREATER, 0, 0xff);
77 //      sceGuEnable(GU_ALPHA_TEST);
78 //      sceGuDepthFunc(GU_ALWAYS); // GU_GEQUAL);
79 //      sceGuEnable(GU_DEPTH_TEST);
80
81         sceGuDepthMask(0xffff);
82         sceGuDisable(GU_DEPTH_TEST);
83
84         sceGuFrontFace(GU_CW);
85 //      sceGuShadeModel(GU_SMOOTH);
86 //      sceGuEnable(GU_CULL_FACE);
87         sceGuEnable(GU_TEXTURE_2D);
88 //      sceGuEnable(GU_CLIP_PLANES);
89         sceGuTexMode(GU_PSM_5650, 0, 0, 0);
90         sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
91         sceGuTexFilter(GU_NEAREST, GU_NEAREST);
92 //      sceGuAmbientColor(0xffffffff);
93 //      sceGuEnable(GU_BLEND);
94 //      sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
95         sceGuFinish();
96         sceGuSync(0, 0);
97
98         sceDisplayWaitVblankStart();
99         sceGuDisplay(GU_TRUE);
100
101
102         /* input */
103         sceCtrlSetSamplingCycle(0);
104         sceCtrlSetSamplingMode(0);
105 }
106
107 void psp_finish(void)
108 {
109         sceGuTerm();
110
111         //sceKernelSleepThread();
112         sceKernelExitGame();
113 }
114
115 void psp_video_flip(int wait_vsync)
116 {
117         if (wait_vsync) sceDisplayWaitVblankStart();
118         sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565,
119                 wait_vsync ? PSP_DISPLAY_SETBUF_IMMEDIATE : PSP_DISPLAY_SETBUF_NEXTFRAME);
120         current_screen ^= 1;
121         psp_screen = current_screen ? VRAM_FB0 : VRAM_FB1;
122 }
123
124 void *psp_video_get_active_fb(void)
125 {
126         return current_screen ? VRAM_FB1 : VRAM_FB0;
127 }
128
129 void psp_video_switch_to_single(void)
130 {
131         psp_screen = VRAM_FB0;
132         sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
133         current_screen = 0;
134 }
135
136 void psp_msleep(int ms)
137 {
138         sceKernelDelayThread(ms * 1000);
139 }
140
141 unsigned int psp_pad_read(int blocking)
142 {
143         SceCtrlData pad;
144         if (blocking)
145              sceCtrlReadBufferPositive(&pad, 1);
146         else sceCtrlPeekBufferPositive(&pad, 1);
147
148         return pad.Buttons;
149 }
150
151 int psp_get_cpu_clock(void)
152 {
153         return scePowerGetCpuClockFrequencyInt();
154 }
155
156 int psp_set_cpu_clock(int clock)
157 {
158         int ret = scePowerSetClockFrequency(clock, clock, clock/2);
159         if (ret != 0) lprintf("failed to set clock: %i\n", ret);
160
161         return ret;
162 }
163
164 /* alt logging */
165 #define LOG_FILE "log.log"
166
167 void lprintf_f(const char *fmt, ...)
168 {
169         va_list vl;
170         char buff[256];
171
172         if (logfd < 0)
173         {
174                 logfd = sceIoOpen(LOG_FILE, PSP_O_WRONLY|PSP_O_APPEND, 0777);
175                 if (logfd < 0)
176                         return;
177         }
178
179         va_start(vl, fmt);
180         vsnprintf(buff, sizeof(buff), fmt, vl);
181         va_end(vl);
182
183         sceIoWrite(logfd, buff, strlen(buff));
184 //sceKernelDelayThread(200 * 1000);
185 sceIoClose(logfd);
186 logfd = -1;
187 }
188
189