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