93c18cb4 |
1 | // (c) Copyright 2006-2009 notaz, All rights reserved.\r |
492d353a |
2 | // Free for non-commercial use.\r |
3 | \r |
4 | // For commercial use, separate licencing terms must be obtained.\r |
5 | \r |
6 | #include <stdio.h>\r |
b188c2b6 |
7 | #include <unistd.h>\r |
492d353a |
8 | \r |
492d353a |
9 | #include "../common/emu.h"\r |
7fd581f4 |
10 | #include "../common/menu.h"\r |
d2f29611 |
11 | #include "../common/plat.h"\r |
12 | #include "../common/arm_utils.h"\r |
b3972d82 |
13 | #include "../linux/sndout_oss.h"\r |
b188c2b6 |
14 | #include "../linux/fbdev.h"\r |
de9b1274 |
15 | #include "asm_utils.h"\r |
d2f29611 |
16 | #include "version.h"\r |
492d353a |
17 | \r |
f11bad75 |
18 | #include <pico/pico_int.h>\r |
492d353a |
19 | \r |
492d353a |
20 | \r |
492d353a |
21 | static short __attribute__((aligned(4))) sndBuffer[2*44100/50];\r |
d2f29611 |
22 | static unsigned char temp_frame[g_screen_width * g_screen_height * 2];\r |
23 | unsigned char *PicoDraw2FB = temp_frame;\r |
0c9ae592 |
24 | const char *renderer_names[] = { NULL };\r |
25 | const char *renderer_names32x[] = { NULL };\r |
f6eaae4f |
26 | char cpu_clk_name[] = "Max CPU clock";\r |
27 | \r |
28 | enum {\r |
29 | SCALE_1x1,\r |
30 | SCALE_2x2_3x2,\r |
31 | SCALE_2x2_2x2,\r |
32 | };\r |
492d353a |
33 | \r |
f6eaae4f |
34 | static int get_cpu_clock(void)\r |
35 | {\r |
36 | FILE *f;\r |
37 | int ret = 0;\r |
38 | f = fopen("/proc/pandora/cpu_mhz_max", "r");\r |
39 | if (f) {\r |
40 | fscanf(f, "%d", &ret);\r |
41 | fclose(f);\r |
42 | }\r |
43 | return ret;\r |
44 | }\r |
492d353a |
45 | \r |
93c18cb4 |
46 | void pemu_prep_defconfig(void)\r |
492d353a |
47 | {\r |
d2f29611 |
48 | // XXX: move elsewhere\r |
49 | g_menubg_ptr = temp_frame;\r |
b188c2b6 |
50 | \r |
51 | defaultConfig.EmuOpt |= EOPT_VSYNC;\r |
f6eaae4f |
52 | defaultConfig.scaling = SCALE_2x2_3x2;\r |
492d353a |
53 | }\r |
54 | \r |
d2f29611 |
55 | void pemu_validate_config(void)\r |
492d353a |
56 | {\r |
f6eaae4f |
57 | currentConfig.CPUclock = get_cpu_clock();\r |
492d353a |
58 | }\r |
59 | \r |
d2f29611 |
60 | // FIXME: cleanup\r |
046c4540 |
61 | static void osd_text(int x, int y, const char *text)\r |
492d353a |
62 | {\r |
63 | int len = strlen(text)*8;\r |
64 | \r |
662e622b |
65 | if (0) {\r |
492d353a |
66 | int *p, i, h;\r |
67 | x &= ~3; // align x\r |
68 | len = (len+3) >> 2;\r |
69 | for (h = 0; h < 8; h++) {\r |
7fd581f4 |
70 | p = (int *) ((unsigned char *) g_screen_ptr+x+g_screen_width*(y+h));\r |
492d353a |
71 | for (i = len; i; i--, p++) *p = 0xe0e0e0e0;\r |
72 | }\r |
b7911801 |
73 | emu_text_out8(x, y, text);\r |
492d353a |
74 | } else {\r |
75 | int *p, i, h;\r |
76 | x &= ~1; // align x\r |
77 | len++;\r |
78 | for (h = 0; h < 16; h++) {\r |
7fd581f4 |
79 | p = (int *) ((unsigned short *) g_screen_ptr+x+g_screen_width*(y+h));\r |
de9b1274 |
80 | for (i = len; i; i--, p++) *p = 0;//(*p>>2)&0x39e7;\r |
492d353a |
81 | }\r |
b7911801 |
82 | text_out16(x, y, text);\r |
492d353a |
83 | }\r |
84 | }\r |
85 | \r |
86 | static void draw_cd_leds(void)\r |
87 | {\r |
88 | // static\r |
89 | int old_reg;\r |
90 | // if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change // mmu hack problems?\r |
91 | old_reg = Pico_mcd->s68k_regs[0];\r |
92 | \r |
662e622b |
93 | if (0) {\r |
492d353a |
94 | // 8-bit modes\r |
95 | unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;\r |
96 | unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;\r |
7fd581f4 |
97 | *(unsigned int *)((char *)g_screen_ptr + g_screen_width*2+ 4) =\r |
98 | *(unsigned int *)((char *)g_screen_ptr + g_screen_width*3+ 4) =\r |
99 | *(unsigned int *)((char *)g_screen_ptr + g_screen_width*4+ 4) = col_g;\r |
100 | *(unsigned int *)((char *)g_screen_ptr + g_screen_width*2+12) =\r |
101 | *(unsigned int *)((char *)g_screen_ptr + g_screen_width*3+12) =\r |
102 | *(unsigned int *)((char *)g_screen_ptr + g_screen_width*4+12) = col_r;\r |
492d353a |
103 | } else {\r |
104 | // 16-bit modes\r |
7fd581f4 |
105 | unsigned int *p = (unsigned int *)((short *)g_screen_ptr + g_screen_width*2+4);\r |
492d353a |
106 | unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;\r |
107 | unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;\r |
e31266dd |
108 | *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += g_screen_width/2 - 12/2;\r |
109 | *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += g_screen_width/2 - 12/2;\r |
492d353a |
110 | *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;\r |
111 | }\r |
112 | }\r |
113 | \r |
f6eaae4f |
114 | static int emuscan_1x1(unsigned int num)\r |
de9b1274 |
115 | {\r |
7fd581f4 |
116 | DrawLineDest = (unsigned short *)g_screen_ptr + num*800 + 800/2 - 320/2;\r |
de9b1274 |
117 | //int w = (Pico.video.reg[12]&1) ? 320 : 256;\r |
7fd581f4 |
118 | //DrawLineDest = (unsigned short *)g_screen_ptr + num*w;\r |
de9b1274 |
119 | \r |
120 | return 0;\r |
121 | }\r |
122 | \r |
f6eaae4f |
123 | #define MAKE_EMUSCAN(name_, clut_name_, offs_, len_) \\r |
124 | static int name_(unsigned int num) \\r |
125 | { \\r |
126 | unsigned char *ps = HighCol+8; \\r |
127 | unsigned short *pd, *pal = HighPal; \\r |
128 | int sh = Pico.video.reg[0xC] & 8; \\r |
129 | int mask = 0xff; \\r |
130 | \\r |
131 | pd = (unsigned short *)g_screen_ptr + num*800*2 + offs_;\\r |
132 | \\r |
133 | if (Pico.m.dirtyPal) \\r |
134 | PicoDoHighPal555(sh); \\r |
135 | \\r |
136 | if (!sh && (rendstatus & PDRAW_SPR_LO_ON_HI)) \\r |
137 | mask = 0x3f; /* upper bits are priority stuff */\\r |
138 | \\r |
139 | clut_line##clut_name_(pd, ps, pal, (mask<<16) | len_); \\r |
140 | \\r |
141 | return 0; \\r |
492d353a |
142 | }\r |
143 | \r |
f6eaae4f |
144 | MAKE_EMUSCAN(emuscan_2x2_40, 2x2, 800/2 - 320*2/2, 320)\r |
145 | MAKE_EMUSCAN(emuscan_2x2_32, 2x2, 800/2 - 256*2/2, 256)\r |
146 | MAKE_EMUSCAN(emuscan_3x2_32, 3x2, 800/2 - 256*3/2, 256)\r |
147 | \r |
148 | #if 0 /* FIXME */\r |
b188c2b6 |
149 | static int EmuScanEnd16_32x(unsigned int num)\r |
150 | {\r |
151 | unsigned int *ps;\r |
152 | unsigned int *pd;\r |
153 | int len;\r |
154 | \r |
155 | ps = (unsigned int *)temp_frame;\r |
156 | pd = (unsigned int *)g_screen_ptr + (num*800*2 + 800/2 - 320*2/2) / 2;\r |
157 | \r |
158 | for (len = 320/2; len > 0; len--, ps++) {\r |
159 | unsigned int p, p1;\r |
160 | p1 = *ps;\r |
161 | p = p1 << 16;\r |
162 | p |= p >> 16;\r |
163 | *pd = pd[800/2] = p;\r |
164 | pd++;\r |
165 | \r |
166 | p = p1 >> 16;\r |
167 | p |= p << 16;\r |
168 | *pd = pd[800/2] = p;\r |
169 | pd++;\r |
170 | }\r |
171 | \r |
172 | return 0;\r |
173 | }\r |
f6eaae4f |
174 | #endif\r |
de9b1274 |
175 | \r |
b188c2b6 |
176 | void pemu_finalize_frame(const char *fps, const char *notice)\r |
492d353a |
177 | {\r |
d2f29611 |
178 | if (notice || (currentConfig.EmuOpt & EOPT_SHOW_FPS)) {\r |
179 | if (notice)\r |
b188c2b6 |
180 | osd_text(4, 464, notice);\r |
d2f29611 |
181 | if (currentConfig.EmuOpt & EOPT_SHOW_FPS)\r |
b188c2b6 |
182 | osd_text(640, 464, fps);\r |
492d353a |
183 | }\r |
d2f29611 |
184 | if ((PicoAHW & PAHW_MCD) && (currentConfig.EmuOpt & EOPT_EN_CD_LEDS))\r |
492d353a |
185 | draw_cd_leds();\r |
d2f29611 |
186 | }\r |
492d353a |
187 | \r |
662e622b |
188 | void plat_video_toggle_renderer(int change, int is_menu)\r |
d2f29611 |
189 | {\r |
190 | // this will auto-select SMS/32X renderers\r |
662e622b |
191 | PicoDrawSetOutFormat(PDF_RGB555, 1);\r |
492d353a |
192 | }\r |
193 | \r |
d2f29611 |
194 | void plat_video_menu_enter(int is_rom_loaded)\r |
195 | {\r |
196 | }\r |
492d353a |
197 | \r |
d2f29611 |
198 | void plat_video_menu_begin(void)\r |
492d353a |
199 | {\r |
d2f29611 |
200 | memcpy32(g_screen_ptr, g_menubg_ptr, g_screen_width * g_screen_height * 2 / 4);\r |
492d353a |
201 | }\r |
202 | \r |
d2f29611 |
203 | void plat_video_menu_end(void)\r |
204 | {\r |
b188c2b6 |
205 | plat_video_flip();\r |
d2f29611 |
206 | }\r |
492d353a |
207 | \r |
d2f29611 |
208 | void plat_status_msg_clear(void)\r |
492d353a |
209 | {\r |
b188c2b6 |
210 | int s = g_screen_width * g_screen_height * 2;\r |
211 | int l = g_screen_width * 16 * 2;\r |
212 | int i;\r |
213 | \r |
214 | for (i = 0; i < fbdev_buffer_count; i++)\r |
215 | memset32((int *)((char *)fbdev_buffers[i] + s - l), 0, l / 4);\r |
492d353a |
216 | }\r |
217 | \r |
d2f29611 |
218 | void plat_status_msg_busy_next(const char *msg)\r |
219 | {\r |
220 | plat_status_msg_clear();\r |
b188c2b6 |
221 | pemu_finalize_frame("", msg);\r |
222 | plat_video_flip();\r |
d2f29611 |
223 | emu_status_msg("");\r |
224 | reset_timing = 1;\r |
225 | }\r |
492d353a |
226 | \r |
d2f29611 |
227 | void plat_status_msg_busy_first(const char *msg)\r |
228 | {\r |
229 | // memset32(g_screen_ptr, 0, g_screen_width * g_screen_height * 2 / 4);\r |
230 | plat_status_msg_busy_next(msg);\r |
231 | }\r |
232 | \r |
233 | void plat_update_volume(int has_changed, int is_up)\r |
492d353a |
234 | {\r |
235 | static int prev_frame = 0, wait_frames = 0;\r |
236 | int vol = currentConfig.volume;\r |
237 | \r |
238 | if (has_changed)\r |
239 | {\r |
d2f29611 |
240 | if (is_up) {\r |
241 | if (vol < 99) vol++;\r |
242 | } else {\r |
243 | if (vol > 0) vol--;\r |
492d353a |
244 | }\r |
d2f29611 |
245 | wait_frames = 0;\r |
246 | sndout_oss_setvol(vol, vol);\r |
247 | currentConfig.volume = vol;\r |
248 | emu_status_msg("VOL: %02i", vol);\r |
492d353a |
249 | prev_frame = Pico.m.frame_count;\r |
250 | }\r |
492d353a |
251 | }\r |
252 | \r |
93c18cb4 |
253 | void pemu_forced_frame(int opts)\r |
492d353a |
254 | {\r |
255 | int po_old = PicoOpt;\r |
492d353a |
256 | \r |
257 | PicoOpt &= ~0x10;\r |
258 | PicoOpt |= opts|POPT_ACC_SPRITES; // acc_sprites\r |
492d353a |
259 | \r |
492d353a |
260 | Pico.m.dirtyPal = 1;\r |
261 | PicoFrameDrawOnly();\r |
262 | \r |
492d353a |
263 | PicoOpt = po_old;\r |
492d353a |
264 | }\r |
265 | \r |
d2f29611 |
266 | static void updateSound(int len)\r |
492d353a |
267 | {\r |
b188c2b6 |
268 | unsigned int t;\r |
269 | \r |
d2f29611 |
270 | len <<= 1;\r |
271 | if (PicoOpt & POPT_EN_STEREO)\r |
272 | len <<= 1;\r |
492d353a |
273 | \r |
b188c2b6 |
274 | // sndout_oss_can_write() not reliable..\r |
d2f29611 |
275 | if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && !sndout_oss_can_write(len))\r |
276 | return;\r |
492d353a |
277 | \r |
d2f29611 |
278 | /* avoid writing audio when lagging behind to prevent audio lag */\r |
b188c2b6 |
279 | if (PicoSkipFrame == 2)\r |
280 | return;\r |
281 | \r |
282 | t = plat_get_ticks_ms();\r |
283 | sndout_oss_write(PsndOut, len);\r |
284 | t = plat_get_ticks_ms() - t;\r |
285 | if (t > 1)\r |
286 | printf("audio lag %u\n", t);\r |
492d353a |
287 | }\r |
288 | \r |
93c18cb4 |
289 | void pemu_sound_start(void)\r |
fce20e73 |
290 | {\r |
fce20e73 |
291 | int target_fps = Pico.m.pal ? 50 : 60;\r |
292 | \r |
293 | PsndOut = NULL;\r |
294 | \r |
b188c2b6 |
295 | if (currentConfig.EmuOpt & EOPT_EN_SOUND)\r |
fce20e73 |
296 | {\r |
b188c2b6 |
297 | int snd_excess_add, frame_samples;\r |
298 | int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0;\r |
299 | \r |
300 | PsndRerate(Pico.m.frame_count ? 1 : 0);\r |
301 | \r |
302 | frame_samples = PsndLen;\r |
303 | snd_excess_add = ((PsndRate - PsndLen * target_fps)<<16) / target_fps;\r |
304 | if (snd_excess_add != 0)\r |
305 | frame_samples++;\r |
fce20e73 |
306 | \r |
fce20e73 |
307 | printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r |
b188c2b6 |
308 | PsndRate, PsndLen, snd_excess_add, is_stereo, Pico.m.pal);\r |
309 | sndout_oss_start(PsndRate, frame_samples * 2, is_stereo);\r |
310 | //sndout_oss_setvol(currentConfig.volume, currentConfig.volume);\r |
fce20e73 |
311 | PicoWriteSound = updateSound;\r |
d2f29611 |
312 | plat_update_volume(0, 0);\r |
fce20e73 |
313 | memset(sndBuffer, 0, sizeof(sndBuffer));\r |
314 | PsndOut = sndBuffer;\r |
fce20e73 |
315 | }\r |
316 | }\r |
317 | \r |
93c18cb4 |
318 | void pemu_sound_stop(void)\r |
fce20e73 |
319 | {\r |
b188c2b6 |
320 | sndout_oss_stop();\r |
fce20e73 |
321 | }\r |
322 | \r |
93c18cb4 |
323 | void pemu_sound_wait(void)\r |
fce20e73 |
324 | {\r |
325 | // don't need to do anything, writes will block by themselves\r |
326 | }\r |
492d353a |
327 | \r |
d2f29611 |
328 | void plat_debug_cat(char *str)\r |
492d353a |
329 | {\r |
d2f29611 |
330 | }\r |
492d353a |
331 | \r |
d2f29611 |
332 | void emu_video_mode_change(int start_line, int line_count, int is_32cols)\r |
333 | {\r |
b188c2b6 |
334 | int i;\r |
492d353a |
335 | \r |
d2f29611 |
336 | // clear whole screen in all buffers\r |
b188c2b6 |
337 | for (i = 0; i < fbdev_buffer_count; i++)\r |
338 | memset32(fbdev_buffers[i], 0, g_screen_width * g_screen_height * 2 / 4);\r |
492d353a |
339 | \r |
f6eaae4f |
340 | PicoScanBegin = NULL;\r |
341 | PicoScanEnd = NULL;\r |
342 | \r |
343 | #if 0\r |
b188c2b6 |
344 | if (PicoAHW & PAHW_32X) {\r |
f6eaae4f |
345 | /* FIXME */\r |
b188c2b6 |
346 | DrawLineDest = (unsigned short *)temp_frame;\r |
662e622b |
347 | PicoDrawSetOutFormat(PDF_RGB555, 1);\r |
b188c2b6 |
348 | PicoScanEnd = EmuScanEnd16_32x;\r |
f6eaae4f |
349 | } else\r |
492d353a |
350 | #endif\r |
f6eaae4f |
351 | {\r |
352 | switch (currentConfig.scaling) {\r |
353 | case SCALE_1x1:\r |
354 | PicoDrawSetOutFormat(PDF_RGB555, 1);\r |
355 | PicoScanBegin = emuscan_1x1;\r |
356 | break;\r |
357 | case SCALE_2x2_3x2:\r |
358 | PicoDrawSetOutFormat(PDF_NONE, 0);\r |
359 | PicoScanEnd = is_32cols ? emuscan_3x2_32 : emuscan_2x2_40;\r |
360 | break;\r |
361 | case SCALE_2x2_2x2:\r |
362 | PicoDrawSetOutFormat(PDF_NONE, 0);\r |
363 | PicoScanEnd = is_32cols ? emuscan_2x2_32 : emuscan_2x2_40;\r |
364 | break;\r |
365 | }\r |
366 | }\r |
b188c2b6 |
367 | }\r |
492d353a |
368 | \r |
b188c2b6 |
369 | void pemu_loop_prep(void)\r |
370 | {\r |
371 | emu_video_mode_change(0, 0, 0);\r |
f6eaae4f |
372 | \r |
373 | if (currentConfig.CPUclock != get_cpu_clock()) {\r |
374 | FILE *f = fopen("/proc/pandora/cpu_mhz_max", "w");\r |
375 | if (f != NULL) {\r |
376 | fprintf(f, "%d\n", currentConfig.CPUclock);\r |
377 | fclose(f);\r |
378 | }\r |
379 | }\r |
380 | \r |
d2f29611 |
381 | pemu_sound_start();\r |
382 | }\r |
492d353a |
383 | \r |
d2f29611 |
384 | void pemu_loop_end(void)\r |
385 | {\r |
386 | int po_old = PicoOpt;\r |
387 | int eo_old = currentConfig.EmuOpt;\r |
492d353a |
388 | \r |
d2f29611 |
389 | pemu_sound_stop();\r |
390 | memset32(g_screen_ptr, 0, g_screen_width * g_screen_height * 2 / 4);\r |
492d353a |
391 | \r |
d2f29611 |
392 | /* do one more frame for menu bg */\r |
393 | PicoOpt &= ~POPT_ALT_RENDERER;\r |
394 | PicoOpt |= POPT_EN_SOFTSCALE|POPT_ACC_SPRITES;\r |
395 | currentConfig.EmuOpt |= EOPT_16BPP;\r |
492d353a |
396 | \r |
662e622b |
397 | PicoDrawSetOutFormat(PDF_RGB555, 1);\r |
d2f29611 |
398 | Pico.m.dirtyPal = 1;\r |
399 | PicoFrame();\r |
492d353a |
400 | \r |
d2f29611 |
401 | PicoOpt = po_old;\r |
402 | currentConfig.EmuOpt = eo_old;\r |
403 | }\r |
492d353a |
404 | \r |
d2f29611 |
405 | void plat_wait_till_us(unsigned int us_to)\r |
406 | {\r |
407 | unsigned int now;\r |
b188c2b6 |
408 | signed int diff;\r |
492d353a |
409 | \r |
d2f29611 |
410 | now = plat_get_ticks_us();\r |
492d353a |
411 | \r |
f6eaae4f |
412 | // XXX: need to check NOHZ\r |
b188c2b6 |
413 | diff = (signed int)(us_to - now);\r |
414 | if (diff > 10000) {\r |
415 | //printf("sleep %d\n", us_to - now);\r |
416 | usleep(diff * 15 / 16);\r |
417 | now = plat_get_ticks_us();\r |
418 | //printf(" wake %d\n", (signed)(us_to - now));\r |
419 | }\r |
420 | /*\r |
421 | while ((signed int)(us_to - now) > 512) {\r |
d2f29611 |
422 | spend_cycles(1024);\r |
423 | now = plat_get_ticks_us();\r |
492d353a |
424 | }\r |
b188c2b6 |
425 | */\r |
492d353a |
426 | }\r |
427 | \r |
d2f29611 |
428 | const char *plat_get_credits(void)\r |
429 | {\r |
0c9ae592 |
430 | return "PicoDrive v" VERSION " (c) notaz, 2006-2010\n\n\n"\r |
d2f29611 |
431 | "Credits:\n"\r |
432 | "fDave: Cyclone 68000 core,\n"\r |
433 | " base code of PicoDrive\n"\r |
434 | "Reesy & FluBBa: DrZ80 core\n"\r |
435 | "MAME devs: YM2612 and SN76496 cores\n"\r |
f6eaae4f |
436 | "Pandora team: Pandora\n"\r |
437 | "Inder: menu bg\n"\r |
d2f29611 |
438 | "\n"\r |
439 | "special thanks (for docs, ideas):\n"\r |
440 | " Charles MacDonald, Haze,\n"\r |
441 | " Stephane Dallongeville,\n"\r |
442 | " Lordus, Exophase, Rokas,\n"\r |
443 | " Nemesis, Tasco Deluxe";\r |
444 | }\r |