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 | |
14 | PSP_MODULE_INFO("PicoDrive", 0, 1, 34); |
15 | |
9112b6ce |
16 | unsigned int __attribute__((aligned(16))) guCmdList[GU_CMDLIST_SIZE]; |
17 | |
18 | void *psp_screen = VRAM_FB0; |
1820b5a7 |
19 | static int current_screen = 0; /* front bufer */ |
20 | |
7d4906bf |
21 | static SceUID logfd = -1; |
1820b5a7 |
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 | |
a4221917 |
35 | lprintf("callback_thread started with id %i, priority %i\n", |
36 | sceKernelGetThreadId(), sceKernelGetThreadCurrentPriority()); |
1820b5a7 |
37 | |
38 | cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL); |
39 | sceKernelRegisterExitCallback(cbid); |
40 | |
41 | sceKernelSleepThreadCB(); |
42 | |
43 | return 0; |
44 | } |
45 | |
46 | void psp_init(void) |
47 | { |
a4221917 |
48 | SceUID thid; |
1820b5a7 |
49 | |
a4221917 |
50 | lprintf("entered psp_init, threadId %i, priority %i\n", sceKernelGetThreadId(), |
51 | sceKernelGetThreadCurrentPriority()); |
1820b5a7 |
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); |
9112b6ce |
61 | sceDisplaySetFrameBuf(VRAM_FB1, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME); |
1820b5a7 |
62 | current_screen = 1; |
9112b6ce |
63 | psp_screen = VRAM_FB0; |
1820b5a7 |
64 | |
65 | /* gu */ |
66 | sceGuInit(); |
67 | |
9112b6ce |
68 | sceGuStart(GU_DIRECT, guCmdList); |
8ab3e3c1 |
69 | sceGuDrawBuffer(GU_PSM_5650, (void *)VRAMOFFS_FB0, 512); |
70 | sceGuDispBuffer(480, 272, (void *)VRAMOFFS_FB1, 512); // don't care |
9112b6ce |
71 | sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT); |
8ab3e3c1 |
72 | sceGuDepthBuffer((void *)VRAMOFFS_DEPTH, 512); |
9112b6ce |
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); |
9112b6ce |
78 | |
79 | sceGuDepthMask(0xffff); |
80 | sceGuDisable(GU_DEPTH_TEST); |
81 | |
82 | sceGuFrontFace(GU_CW); |
9112b6ce |
83 | sceGuEnable(GU_TEXTURE_2D); |
9112b6ce |
84 | sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB); |
8ab3e3c1 |
85 | sceGuAmbientColor(0xffffffff); |
86 | sceGuColor(0xffffffff); |
9112b6ce |
87 | sceGuFinish(); |
88 | sceGuSync(0, 0); |
89 | |
90 | sceDisplayWaitVblankStart(); |
91 | sceGuDisplay(GU_TRUE); |
92 | |
93 | |
1820b5a7 |
94 | /* input */ |
95 | sceCtrlSetSamplingCycle(0); |
96 | sceCtrlSetSamplingMode(0); |
97 | } |
98 | |
99 | void psp_finish(void) |
100 | { |
101 | sceGuTerm(); |
102 | |
103 | //sceKernelSleepThread(); |
104 | sceKernelExitGame(); |
105 | } |
106 | |
7d4906bf |
107 | void psp_video_flip(int wait_vsync) |
1820b5a7 |
108 | { |
7d4906bf |
109 | if (wait_vsync) sceDisplayWaitVblankStart(); |
70357ce5 |
110 | sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, |
111 | wait_vsync ? PSP_DISPLAY_SETBUF_IMMEDIATE : PSP_DISPLAY_SETBUF_NEXTFRAME); |
1820b5a7 |
112 | current_screen ^= 1; |
9112b6ce |
113 | psp_screen = current_screen ? VRAM_FB0 : VRAM_FB1; |
7d4906bf |
114 | } |
115 | |
116 | void *psp_video_get_active_fb(void) |
117 | { |
9112b6ce |
118 | return current_screen ? VRAM_FB1 : VRAM_FB0; |
1820b5a7 |
119 | } |
120 | |
121 | void psp_video_switch_to_single(void) |
122 | { |
9112b6ce |
123 | psp_screen = VRAM_FB0; |
1820b5a7 |
124 | sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME); |
125 | current_screen = 0; |
126 | } |
127 | |
128 | void psp_msleep(int ms) |
129 | { |
130 | sceKernelDelayThread(ms * 1000); |
131 | } |
132 | |
7d4906bf |
133 | unsigned int psp_pad_read(int blocking) |
1820b5a7 |
134 | { |
135 | SceCtrlData pad; |
7d4906bf |
136 | if (blocking) |
137 | sceCtrlReadBufferPositive(&pad, 1); |
138 | else sceCtrlPeekBufferPositive(&pad, 1); |
1820b5a7 |
139 | |
140 | return pad.Buttons; |
141 | } |
142 | |
70357ce5 |
143 | int psp_get_cpu_clock(void) |
144 | { |
145 | return scePowerGetCpuClockFrequencyInt(); |
146 | } |
147 | |
148 | int 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 | |
7d4906bf |
156 | /* alt logging */ |
157 | #define LOG_FILE "log.log" |
158 | |
159 | void 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); |
177 | sceIoClose(logfd); |
178 | logfd = -1; |
179 | } |
180 | |
181 | |