psp gfx scaling/etc stuff
[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);
8ab3e3c1 67 sceGuDrawBuffer(GU_PSM_5650, (void *)VRAMOFFS_FB0, 512);
68 sceGuDispBuffer(480, 272, (void *)VRAMOFFS_FB1, 512); // don't care
9112b6ce 69 sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT);
8ab3e3c1 70 sceGuDepthBuffer((void *)VRAMOFFS_DEPTH, 512);
9112b6ce 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);
9112b6ce 76
77 sceGuDepthMask(0xffff);
78 sceGuDisable(GU_DEPTH_TEST);
79
80 sceGuFrontFace(GU_CW);
9112b6ce 81 sceGuEnable(GU_TEXTURE_2D);
9112b6ce 82 sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
8ab3e3c1 83 sceGuAmbientColor(0xffffffff);
84 sceGuColor(0xffffffff);
9112b6ce 85 sceGuFinish();
86 sceGuSync(0, 0);
87
88 sceDisplayWaitVblankStart();
89 sceGuDisplay(GU_TRUE);
90
91
1820b5a7 92 /* input */
93 sceCtrlSetSamplingCycle(0);
94 sceCtrlSetSamplingMode(0);
95}
96
97void psp_finish(void)
98{
99 sceGuTerm();
100
101 //sceKernelSleepThread();
102 sceKernelExitGame();
103}
104
7d4906bf 105void psp_video_flip(int wait_vsync)
1820b5a7 106{
7d4906bf 107 if (wait_vsync) sceDisplayWaitVblankStart();
70357ce5 108 sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565,
109 wait_vsync ? PSP_DISPLAY_SETBUF_IMMEDIATE : PSP_DISPLAY_SETBUF_NEXTFRAME);
1820b5a7 110 current_screen ^= 1;
9112b6ce 111 psp_screen = current_screen ? VRAM_FB0 : VRAM_FB1;
7d4906bf 112}
113
114void *psp_video_get_active_fb(void)
115{
9112b6ce 116 return current_screen ? VRAM_FB1 : VRAM_FB0;
1820b5a7 117}
118
119void psp_video_switch_to_single(void)
120{
9112b6ce 121 psp_screen = VRAM_FB0;
1820b5a7 122 sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
123 current_screen = 0;
124}
125
126void psp_msleep(int ms)
127{
128 sceKernelDelayThread(ms * 1000);
129}
130
7d4906bf 131unsigned int psp_pad_read(int blocking)
1820b5a7 132{
133 SceCtrlData pad;
7d4906bf 134 if (blocking)
135 sceCtrlReadBufferPositive(&pad, 1);
136 else sceCtrlPeekBufferPositive(&pad, 1);
1820b5a7 137
138 return pad.Buttons;
139}
140
70357ce5 141int psp_get_cpu_clock(void)
142{
143 return scePowerGetCpuClockFrequencyInt();
144}
145
146int 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
7d4906bf 154/* alt logging */
155#define LOG_FILE "log.log"
156
157void 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);
175sceIoClose(logfd);
176logfd = -1;
177}
178
179