skip junk in mp3_helix, refactor find_sync_word
[picodrive.git] / platform / linux / emu.c
CommitLineData
697746df 1// (c) Copyright 2006-2009 notaz, All rights reserved.\r
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
7#include <unistd.h>\r
8\r
9#include "../common/emu.h"\r
10#include "../common/menu.h"\r
11#include "../common/plat.h"\r
12#include "../common/arm_utils.h"\r
13#include "../linux/sndout_oss.h"\r
14#include "version.h"\r
15\r
16#include <pico/pico_int.h>\r
17\r
18\r
19static short __attribute__((aligned(4))) sndBuffer[2*44100/50];\r
697746df 20char cpu_clk_name[] = "unused";\r
fcdefcf6 21const char *renderer_names[] = { "16bit accurate", " 8bit accurate", " 8bit fast", NULL };\r
22const char *renderer_names32x[] = { "accurate", "faster", "fastest", NULL };\r
5a681086 23enum renderer_types { RT_16BIT, RT_8BIT_ACC, RT_8BIT_FAST, RT_COUNT };\r
697746df 24\r
697746df 25\r
26void pemu_prep_defconfig(void)\r
27{\r
697746df 28}\r
29\r
30void pemu_validate_config(void)\r
31{\r
32 extern int PicoOpt;\r
33// PicoOpt &= ~POPT_EXT_FM;\r
34 PicoOpt &= ~POPT_EN_SVP_DRC;\r
35}\r
36\r
37// FIXME: dupes from GP2X, need cleanup\r
38static void (*osd_text)(int x, int y, const char *text);\r
39\r
d08e7326 40/*\r
697746df 41static void osd_text8(int x, int y, const char *text)\r
42{\r
43 int len = strlen(text)*8;\r
44 int *p, i, h, offs;\r
45\r
46 len = (len+3) >> 2;\r
47 for (h = 0; h < 8; h++) {\r
48 offs = (x + g_screen_width * (y+h)) & ~3;\r
49 p = (int *) ((char *)g_screen_ptr + offs);\r
50 for (i = len; i; i--, p++)\r
51 *p = 0xe0e0e0e0;\r
52 }\r
53 emu_text_out8(x, y, text);\r
54}\r
d08e7326 55*/\r
697746df 56\r
57static void osd_text16(int x, int y, const char *text)\r
58{\r
59 int len = strlen(text)*8;\r
60 int *p, i, h, offs;\r
61\r
62 len = (len+1) >> 1;\r
63 for (h = 0; h < 8; h++) {\r
64 offs = (x + g_screen_width * (y+h)) & ~1;\r
65 p = (int *) ((short *)g_screen_ptr + offs);\r
66 for (i = len; i; i--, p++)\r
67 *p = (*p >> 2) & 0x39e7;\r
68 }\r
69 emu_text_out16(x, y, text);\r
70}\r
71\r
72static void draw_cd_leds(void)\r
73{\r
74 int led_reg, pitch, scr_offs, led_offs;\r
75 led_reg = Pico_mcd->s68k_regs[0];\r
76\r
77 pitch = 320;\r
78 led_offs = 4;\r
79 scr_offs = pitch * 2 + 4;\r
80\r
5a681086 81 if (currentConfig.renderer != RT_16BIT) {\r
697746df 82 #define p(x) px[(x) >> 2]\r
83 // 8-bit modes\r
84 unsigned int *px = (unsigned int *)((char *)g_screen_ptr + scr_offs);\r
85 unsigned int col_g = (led_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;\r
86 unsigned int col_r = (led_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;\r
87 p(pitch*0) = p(pitch*1) = p(pitch*2) = col_g;\r
88 p(pitch*0 + led_offs) = p(pitch*1 + led_offs) = p(pitch*2 + led_offs) = col_r;\r
89 #undef p\r
90 } else {\r
91 #define p(x) px[(x)*2 >> 2] = px[((x)*2 >> 2) + 1]\r
92 // 16-bit modes\r
93 unsigned int *px = (unsigned int *)((short *)g_screen_ptr + scr_offs);\r
94 unsigned int col_g = (led_reg & 2) ? 0x06000600 : 0;\r
95 unsigned int col_r = (led_reg & 1) ? 0xc000c000 : 0;\r
96 p(pitch*0) = p(pitch*1) = p(pitch*2) = col_g;\r
97 p(pitch*0 + led_offs) = p(pitch*1 + led_offs) = p(pitch*2 + led_offs) = col_r;\r
98 #undef p\r
99 }\r
100}\r
101\r
d08e7326 102void pemu_finalize_frame(const char *fps, const char *notice)\r
697746df 103{\r
5a681086 104 if (currentConfig.renderer != RT_16BIT && !(PicoAHW & PAHW_32X)) {\r
105 unsigned short *pd = (unsigned short *)g_screen_ptr + 8 * g_screen_width;\r
106 unsigned char *ps = PicoDraw2FB + 328*8 + 8;\r
107 unsigned short *pal = HighPal;\r
108 int i, x;\r
109 if (Pico.m.dirtyPal)\r
110 PicoDrawUpdateHighPal();\r
111 for (i = 0; i < 224; i++, ps += 8)\r
112 for (x = 0; x < 320; x++)\r
113 *pd++ = pal[*ps++];\r
114 }\r
115\r
697746df 116 if (notice || (currentConfig.EmuOpt & EOPT_SHOW_FPS)) {\r
117 if (notice)\r
118 osd_text(4, g_screen_height - 8, notice);\r
119 if (currentConfig.EmuOpt & EOPT_SHOW_FPS)\r
d08e7326 120 osd_text(g_screen_width - 60, g_screen_height - 8, fps);\r
697746df 121 }\r
122 if ((PicoAHW & PAHW_MCD) && (currentConfig.EmuOpt & EOPT_EN_CD_LEDS))\r
123 draw_cd_leds();\r
697746df 124}\r
125\r
5a681086 126static void apply_renderer(void)\r
127{\r
128 PicoScanBegin = NULL;\r
129 PicoScanEnd = NULL;\r
130\r
131 switch (currentConfig.renderer) {\r
132 case RT_16BIT:\r
133 PicoOpt &= ~POPT_ALT_RENDERER;\r
134 PicoDrawSetOutFormat(PDF_RGB555, 0);\r
135 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
136 break;\r
137 case RT_8BIT_ACC:\r
138 PicoOpt &= ~POPT_ALT_RENDERER;\r
139 PicoDrawSetOutFormat(PDF_8BIT, 0);\r
140 PicoDrawSetOutBuf(PicoDraw2FB + 8, 328);\r
141 break;\r
142 case RT_8BIT_FAST:\r
143 PicoOpt |= POPT_ALT_RENDERER;\r
144 PicoDrawSetOutFormat(PDF_NONE, 0);\r
145 break;\r
146 }\r
147\r
148 if (PicoAHW & PAHW_32X) {\r
149 int only_32x = 0;\r
150 if (currentConfig.renderer == RT_16BIT)\r
151 only_32x = 1;\r
152 else\r
153 PicoDrawSetOutFormat(PDF_NONE, 0);\r
154 PicoDraw32xSetFrameMode(1, only_32x);\r
155 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
156 }\r
e51e5983 157 //PicoDraw32xSetFrameMode(0, 0);\r
158 //PicoDrawSetOutFormat(PDF_RGB555, 1);\r
5a681086 159}\r
160\r
161void plat_video_toggle_renderer(int change, int is_menu)\r
697746df 162{\r
5a681086 163 currentConfig.renderer += change;\r
164 if (currentConfig.renderer >= RT_COUNT)\r
165 currentConfig.renderer = 0;\r
166 else if (currentConfig.renderer < 0)\r
167 currentConfig.renderer = RT_COUNT - 1;\r
168\r
169 if (!is_menu)\r
170 apply_renderer();\r
171\r
172 emu_status_msg(renderer_names[currentConfig.renderer]);\r
697746df 173}\r
174\r
175void plat_video_menu_enter(int is_rom_loaded)\r
176{\r
177}\r
178\r
179void plat_video_menu_begin(void)\r
180{\r
181 memcpy32(g_screen_ptr, g_menubg_ptr, g_screen_width * g_screen_height * 2 / 4);\r
c7eb229a 182 g_menubg_ptr = g_screen_ptr;\r
697746df 183}\r
184\r
185void plat_video_menu_end(void)\r
186{\r
d08e7326 187 plat_video_flip();\r
697746df 188}\r
189\r
190void plat_status_msg_clear(void)\r
191{\r
192 unsigned short *d = (unsigned short *)g_screen_ptr + g_screen_width * g_screen_height;\r
193 int l = g_screen_width * 8;\r
194 memset32((int *)(d - l), 0, l * 2 / 4);\r
195}\r
196\r
197void plat_status_msg_busy_next(const char *msg)\r
198{\r
199 plat_status_msg_clear();\r
d08e7326 200 pemu_finalize_frame("", msg);\r
201 plat_video_flip();\r
697746df 202 emu_status_msg("");\r
203 reset_timing = 1;\r
204}\r
205\r
206void plat_status_msg_busy_first(const char *msg)\r
207{\r
208// memset32(g_screen_ptr, 0, g_screen_width * g_screen_height * 2 / 4);\r
209 plat_status_msg_busy_next(msg);\r
210}\r
211\r
212void plat_update_volume(int has_changed, int is_up)\r
213{\r
214}\r
215\r
74e1b42b 216void pemu_forced_frame(int opts, int no_scale)\r
697746df 217{\r
218 int po_old = PicoOpt;\r
219 int eo_old = currentConfig.EmuOpt;\r
220\r
221 PicoOpt &= ~POPT_ALT_RENDERER;\r
222 PicoOpt |= opts|POPT_ACC_SPRITES; // acc_sprites\r
697746df 223\r
e51e5983 224 PicoDrawSetOutFormat(PDF_RGB555, 1);\r
225 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
226 PicoDraw32xSetFrameMode(0, 0);\r
697746df 227\r
228 Pico.m.dirtyPal = 1;\r
229 PicoFrameDrawOnly();\r
230\r
231 PicoOpt = po_old;\r
232 currentConfig.EmuOpt = eo_old;\r
233}\r
234\r
235static void updateSound(int len)\r
236{\r
237 len <<= 1;\r
238 if (PicoOpt & POPT_EN_STEREO)\r
239 len <<= 1;\r
240\r
241 if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && !sndout_oss_can_write(len))\r
242 return;\r
243\r
244 /* avoid writing audio when lagging behind to prevent audio lag */\r
245 if (PicoSkipFrame != 2)\r
246 sndout_oss_write(PsndOut, len);\r
247}\r
248\r
249void pemu_sound_start(void)\r
250{\r
697746df 251 int target_fps = Pico.m.pal ? 50 : 60;\r
252\r
253 PsndOut = NULL;\r
254\r
255 if (currentConfig.EmuOpt & EOPT_EN_SOUND)\r
256 {\r
d08e7326 257 int snd_excess_add, frame_samples;\r
258 int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0;\r
259\r
260 PsndRerate(Pico.m.frame_count ? 1 : 0);\r
261\r
262 frame_samples = PsndLen;\r
263 snd_excess_add = ((PsndRate - PsndLen * target_fps)<<16) / target_fps;\r
264 if (snd_excess_add != 0)\r
265 frame_samples++;\r
697746df 266\r
697746df 267 printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r
d08e7326 268 PsndRate, PsndLen, snd_excess_add, is_stereo, Pico.m.pal);\r
269 sndout_oss_start(PsndRate, frame_samples, is_stereo);\r
697746df 270 sndout_oss_setvol(currentConfig.volume, currentConfig.volume);\r
271 PicoWriteSound = updateSound;\r
272 plat_update_volume(0, 0);\r
273 memset(sndBuffer, 0, sizeof(sndBuffer));\r
274 PsndOut = sndBuffer;\r
697746df 275 }\r
276}\r
277\r
278void pemu_sound_stop(void)\r
279{\r
280}\r
281\r
282void pemu_sound_wait(void)\r
283{\r
284 // don't need to do anything, writes will block by themselves\r
285}\r
286\r
287void plat_debug_cat(char *str)\r
288{\r
289}\r
290\r
291void emu_video_mode_change(int start_line, int line_count, int is_32cols)\r
292{\r
697746df 293 // clear whole screen in all buffers\r
294 memset32(g_screen_ptr, 0, g_screen_width * g_screen_height * 2 / 4);\r
295}\r
296\r
297void pemu_loop_prep(void)\r
298{\r
5a681086 299 apply_renderer();\r
697746df 300 osd_text = osd_text16;\r
301\r
302 pemu_sound_start();\r
303}\r
304\r
305void pemu_loop_end(void)\r
306{\r
307 int po_old = PicoOpt;\r
308 int eo_old = currentConfig.EmuOpt;\r
309\r
310 pemu_sound_stop();\r
311 memset32(g_screen_ptr, 0, g_screen_width * g_screen_height * 2 / 4);\r
312\r
313 /* do one more frame for menu bg */\r
314 PicoOpt &= ~POPT_ALT_RENDERER;\r
315 PicoOpt |= POPT_EN_SOFTSCALE|POPT_ACC_SPRITES;\r
697746df 316\r
e51e5983 317 PicoDrawSetOutFormat(PDF_RGB555, 1);\r
318 PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);\r
319 PicoDraw32xSetFrameMode(0, 0);\r
697746df 320 Pico.m.dirtyPal = 1;\r
321 PicoFrame();\r
322\r
323 PicoOpt = po_old;\r
324 currentConfig.EmuOpt = eo_old;\r
325}\r
326\r
327void plat_wait_till_us(unsigned int us_to)\r
328{\r
329 unsigned int now;\r
330\r
331 now = plat_get_ticks_us();\r
332\r
333 while ((signed int)(us_to - now) > 512)\r
334 {\r
335 usleep(1024);\r
336 now = plat_get_ticks_us();\r
337 }\r
338}\r
339\r
697746df 340const char *plat_get_credits(void)\r
341{\r
342 return "PicoDrive v" VERSION " (c) notaz, 2006-2009\n\n\n"\r
343 "Credits:\n"\r
344 "fDave: Cyclone 68000 core,\n"\r
345 " base code of PicoDrive\n"\r
346 "Reesy & FluBBa: DrZ80 core\n"\r
347 "MAME devs: YM2612 and SN76496 cores\n"\r
348 "rlyeh and others: minimal SDK\n"\r
349 "Squidge: mmuhack\n"\r
350 "Dzz: ARM940 sample\n"\r
351 "GnoStiC / Puck2099: USB joy code\n"\r
352 "craigix: GP2X hardware\n"\r
353 "ketchupgun: skin design\n"\r
354 "\n"\r
355 "special thanks (for docs, ideas):\n"\r
356 " Charles MacDonald, Haze,\n"\r
357 " Stephane Dallongeville,\n"\r
358 " Lordus, Exophase, Rokas,\n"\r
359 " Nemesis, Tasco Deluxe";\r
360}\r