some work on PSP CLUT
[picodrive.git] / platform / psp / psp.c
CommitLineData
7d4906bf 1#include <stdio.h>
2#include <stdarg.h>
3#include <string.h>
4
1820b5a7 5#include <pspkernel.h>
7d4906bf 6#include <pspiofilemgr.h>
1820b5a7 7#include <pspdisplay.h>
70357ce5 8#include <psppower.h>
1820b5a7 9#include <pspgu.h>
10
11#include "psp.h"
12#include "../common/lprintf.h"
13
14PSP_MODULE_INFO("PicoDrive", 0, 1, 34);
15
9112b6ce 16unsigned int __attribute__((aligned(16))) guCmdList[GU_CMDLIST_SIZE];
17
18void *psp_screen = VRAM_FB0;
1820b5a7 19static int current_screen = 0; /* front bufer */
20
7d4906bf 21static SceUID logfd = -1;
1820b5a7 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
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
45void 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);
9112b6ce 59 sceDisplaySetFrameBuf(VRAM_FB1, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
1820b5a7 60 current_screen = 1;
9112b6ce 61 psp_screen = VRAM_FB0;
1820b5a7 62
63 /* gu */
64 sceGuInit();
65
9112b6ce 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
1820b5a7 102 /* input */
103 sceCtrlSetSamplingCycle(0);
104 sceCtrlSetSamplingMode(0);
105}
106
107void psp_finish(void)
108{
109 sceGuTerm();
110
111 //sceKernelSleepThread();
112 sceKernelExitGame();
113}
114
7d4906bf 115void psp_video_flip(int wait_vsync)
1820b5a7 116{
7d4906bf 117 if (wait_vsync) sceDisplayWaitVblankStart();
70357ce5 118 sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565,
119 wait_vsync ? PSP_DISPLAY_SETBUF_IMMEDIATE : PSP_DISPLAY_SETBUF_NEXTFRAME);
1820b5a7 120 current_screen ^= 1;
9112b6ce 121 psp_screen = current_screen ? VRAM_FB0 : VRAM_FB1;
7d4906bf 122}
123
124void *psp_video_get_active_fb(void)
125{
9112b6ce 126 return current_screen ? VRAM_FB1 : VRAM_FB0;
1820b5a7 127}
128
129void psp_video_switch_to_single(void)
130{
9112b6ce 131 psp_screen = VRAM_FB0;
1820b5a7 132 sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
133 current_screen = 0;
134}
135
136void psp_msleep(int ms)
137{
138 sceKernelDelayThread(ms * 1000);
139}
140
7d4906bf 141unsigned int psp_pad_read(int blocking)
1820b5a7 142{
143 SceCtrlData pad;
7d4906bf 144 if (blocking)
145 sceCtrlReadBufferPositive(&pad, 1);
146 else sceCtrlPeekBufferPositive(&pad, 1);
1820b5a7 147
148 return pad.Buttons;
149}
150
70357ce5 151int psp_get_cpu_clock(void)
152{
153 return scePowerGetCpuClockFrequencyInt();
154}
155
156int 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
7d4906bf 164/* alt logging */
165#define LOG_FILE "log.log"
166
167void 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);
185sceIoClose(logfd);
186logfd = -1;
187}
188
189