basic psp snd + cz80
[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, priority %i\n",
36                 sceKernelGetThreadId(), sceKernelGetThreadCurrentPriority());
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 {
48         SceUID thid;
49
50         lprintf("entered psp_init, threadId %i, priority %i\n", sceKernelGetThreadId(),
51                 sceKernelGetThreadCurrentPriority());
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);
61         sceDisplaySetFrameBuf(VRAM_FB1, 512, PSP_DISPLAY_PIXEL_FORMAT_565, PSP_DISPLAY_SETBUF_NEXTFRAME);
62         current_screen = 1;
63         psp_screen = VRAM_FB0;
64
65         /* gu */
66         sceGuInit();
67
68         sceGuStart(GU_DIRECT, guCmdList);
69         sceGuDrawBuffer(GU_PSM_5650, (void *)VRAMOFFS_FB0, 512);
70         sceGuDispBuffer(480, 272, (void *)VRAMOFFS_FB1, 512); // don't care
71         sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT);
72         sceGuDepthBuffer((void *)VRAMOFFS_DEPTH, 512);
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);
78
79         sceGuDepthMask(0xffff);
80         sceGuDisable(GU_DEPTH_TEST);
81
82         sceGuFrontFace(GU_CW);
83         sceGuEnable(GU_TEXTURE_2D);
84         sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
85         sceGuAmbientColor(0xffffffff);
86         sceGuColor(0xffffffff);
87         sceGuFinish();
88         sceGuSync(0, 0);
89
90         sceDisplayWaitVblankStart();
91         sceGuDisplay(GU_TRUE);
92
93
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
107 void psp_video_flip(int wait_vsync)
108 {
109         if (wait_vsync) sceDisplayWaitVblankStart();
110         sceDisplaySetFrameBuf(psp_screen, 512, PSP_DISPLAY_PIXEL_FORMAT_565,
111                 wait_vsync ? PSP_DISPLAY_SETBUF_IMMEDIATE : PSP_DISPLAY_SETBUF_NEXTFRAME);
112         current_screen ^= 1;
113         psp_screen = current_screen ? VRAM_FB0 : VRAM_FB1;
114 }
115
116 void *psp_video_get_active_fb(void)
117 {
118         return current_screen ? VRAM_FB1 : VRAM_FB0;
119 }
120
121 void psp_video_switch_to_single(void)
122 {
123         psp_screen = VRAM_FB0;
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
133 unsigned int psp_pad_read(int blocking)
134 {
135         SceCtrlData pad;
136         if (blocking)
137              sceCtrlReadBufferPositive(&pad, 1);
138         else sceCtrlPeekBufferPositive(&pad, 1);
139
140         return pad.Buttons;
141 }
142
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
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