src/mame/audio/snes.c
| r7592 | r7593 | |
| 1 | | /*************************************************************************** |
| 2 | | |
| 3 | | snes.c |
| 4 | | |
| 5 | | File to handle the sound emulation of the Nintendo Super NES. |
| 6 | | |
| 7 | | By R. Belmont, adapted from OpenSPC 0.3.99 by Brad Martin with permission. |
| 8 | | Thanks to Brad and also to Charles Bilyu? of SNeESe. |
| 9 | | |
| 10 | | OpenSPC's license terms (the LGPL) follow: |
| 11 | | |
| 12 | | --------------------------------------------------------------------------- |
| 13 | | |
| 14 | | Copyright Brad Martin. |
| 15 | | |
| 16 | | OpenSPC is free software; you can redistribute it and/or modify |
| 17 | | it under the terms of the GNU Lesser General Public License as published by |
| 18 | | the Free Software Foundation; either version 2 of the License, or |
| 19 | | (at your option) any later version. |
| 20 | | |
| 21 | | OpenSPC is distributed in the hope that it will be useful, |
| 22 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | | GNU Lesser General Public License for more details. |
| 25 | | |
| 26 | | ***************************************************************************/ |
| 27 | | |
| 28 | | #include "emu.h" |
| 29 | | #include "streams.h" |
| 30 | | #include "includes/snes.h" |
| 31 | | |
| 32 | | static struct |
| 33 | | { |
| 34 | | UINT8 enabled; |
| 35 | | UINT16 counter; |
| 36 | | emu_timer *timer; |
| 37 | | } timers[3]; |
| 38 | | static sound_stream *channel; |
| 39 | | static UINT8 DSPregs[256]; /* DSP registers */ |
| 40 | | static UINT8 snes_ipl_region[64]; /* SPC top 64 bytes */ |
| 41 | | |
| 42 | | static const int gauss[]= |
| 43 | | { |
| 44 | | 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 45 | | 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 46 | | 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, |
| 47 | | 0x001, 0x001, 0x001, 0x002, 0x002, 0x002, 0x002, 0x002, |
| 48 | | 0x002, 0x002, 0x003, 0x003, 0x003, 0x003, 0x003, 0x004, |
| 49 | | 0x004, 0x004, 0x004, 0x004, 0x005, 0x005, 0x005, 0x005, |
| 50 | | 0x006, 0x006, 0x006, 0x006, 0x007, 0x007, 0x007, 0x008, |
| 51 | | 0x008, 0x008, 0x009, 0x009, 0x009, 0x00A, 0x00A, 0x00A, |
| 52 | | 0x00B, 0x00B, 0x00B, 0x00C, 0x00C, 0x00D, 0x00D, 0x00E, |
| 53 | | 0x00E, 0x00F, 0x00F, 0x00F, 0x010, 0x010, 0x011, 0x011, |
| 54 | | 0x012, 0x013, 0x013, 0x014, 0x014, 0x015, 0x015, 0x016, |
| 55 | | 0x017, 0x017, 0x018, 0x018, 0x019, 0x01A, 0x01B, 0x01B, |
| 56 | | 0x01C, 0x01D, 0x01D, 0x01E, 0x01F, 0x020, 0x020, 0x021, |
| 57 | | 0x022, 0x023, 0x024, 0x024, 0x025, 0x026, 0x027, 0x028, |
| 58 | | 0x029, 0x02A, 0x02B, 0x02C, 0x02D, 0x02E, 0x02F, 0x030, |
| 59 | | 0x031, 0x032, 0x033, 0x034, 0x035, 0x036, 0x037, 0x038, |
| 60 | | 0x03A, 0x03B, 0x03C, 0x03D, 0x03E, 0x040, 0x041, 0x042, |
| 61 | | 0x043, 0x045, 0x046, 0x047, 0x049, 0x04A, 0x04C, 0x04D, |
| 62 | | 0x04E, 0x050, 0x051, 0x053, 0x054, 0x056, 0x057, 0x059, |
| 63 | | 0x05A, 0x05C, 0x05E, 0x05F, 0x061, 0x063, 0x064, 0x066, |
| 64 | | 0x068, 0x06A, 0x06B, 0x06D, 0x06F, 0x071, 0x073, 0x075, |
| 65 | | 0x076, 0x078, 0x07A, 0x07C, 0x07E, 0x080, 0x082, 0x084, |
| 66 | | 0x086, 0x089, 0x08B, 0x08D, 0x08F, 0x091, 0x093, 0x096, |
| 67 | | 0x098, 0x09A, 0x09C, 0x09F, 0x0A1, 0x0A3, 0x0A6, 0x0A8, |
| 68 | | 0x0AB, 0x0AD, 0x0AF, 0x0B2, 0x0B4, 0x0B7, 0x0BA, 0x0BC, |
| 69 | | 0x0BF, 0x0C1, 0x0C4, 0x0C7, 0x0C9, 0x0CC, 0x0CF, 0x0D2, |
| 70 | | 0x0D4, 0x0D7, 0x0DA, 0x0DD, 0x0E0, 0x0E3, 0x0E6, 0x0E9, |
| 71 | | 0x0EC, 0x0EF, 0x0F2, 0x0F5, 0x0F8, 0x0FB, 0x0FE, 0x101, |
| 72 | | 0x104, 0x107, 0x10B, 0x10E, 0x111, 0x114, 0x118, 0x11B, |
| 73 | | 0x11E, 0x122, 0x125, 0x129, 0x12C, 0x130, 0x133, 0x137, |
| 74 | | 0x13A, 0x13E, 0x141, 0x145, 0x148, 0x14C, 0x150, 0x153, |
| 75 | | 0x157, 0x15B, 0x15F, 0x162, 0x166, 0x16A, 0x16E, 0x172, |
| 76 | | 0x176, 0x17A, 0x17D, 0x181, 0x185, 0x189, 0x18D, 0x191, |
| 77 | | 0x195, 0x19A, 0x19E, 0x1A2, 0x1A6, 0x1AA, 0x1AE, 0x1B2, |
| 78 | | 0x1B7, 0x1BB, 0x1BF, 0x1C3, 0x1C8, 0x1CC, 0x1D0, 0x1D5, |
| 79 | | 0x1D9, 0x1DD, 0x1E2, 0x1E6, 0x1EB, 0x1EF, 0x1F3, 0x1F8, |
| 80 | | 0x1FC, 0x201, 0x205, 0x20A, 0x20F, 0x213, 0x218, 0x21C, |
| 81 | | 0x221, 0x226, 0x22A, 0x22F, 0x233, 0x238, 0x23D, 0x241, |
| 82 | | 0x246, 0x24B, 0x250, 0x254, 0x259, 0x25E, 0x263, 0x267, |
| 83 | | 0x26C, 0x271, 0x276, 0x27B, 0x280, 0x284, 0x289, 0x28E, |
| 84 | | 0x293, 0x298, 0x29D, 0x2A2, 0x2A6, 0x2AB, 0x2B0, 0x2B5, |
| 85 | | 0x2BA, 0x2BF, 0x2C4, 0x2C9, 0x2CE, 0x2D3, 0x2D8, 0x2DC, |
| 86 | | 0x2E1, 0x2E6, 0x2EB, 0x2F0, 0x2F5, 0x2FA, 0x2FF, 0x304, |
| 87 | | 0x309, 0x30E, 0x313, 0x318, 0x31D, 0x322, 0x326, 0x32B, |
| 88 | | 0x330, 0x335, 0x33A, 0x33F, 0x344, 0x349, 0x34E, 0x353, |
| 89 | | 0x357, 0x35C, 0x361, 0x366, 0x36B, 0x370, 0x374, 0x379, |
| 90 | | 0x37E, 0x383, 0x388, 0x38C, 0x391, 0x396, 0x39B, 0x39F, |
| 91 | | 0x3A4, 0x3A9, 0x3AD, 0x3B2, 0x3B7, 0x3BB, 0x3C0, 0x3C5, |
| 92 | | 0x3C9, 0x3CE, 0x3D2, 0x3D7, 0x3DC, 0x3E0, 0x3E5, 0x3E9, |
| 93 | | 0x3ED, 0x3F2, 0x3F6, 0x3FB, 0x3FF, 0x403, 0x408, 0x40C, |
| 94 | | 0x410, 0x415, 0x419, 0x41D, 0x421, 0x425, 0x42A, 0x42E, |
| 95 | | 0x432, 0x436, 0x43A, 0x43E, 0x442, 0x446, 0x44A, 0x44E, |
| 96 | | 0x452, 0x455, 0x459, 0x45D, 0x461, 0x465, 0x468, 0x46C, |
| 97 | | 0x470, 0x473, 0x477, 0x47A, 0x47E, 0x481, 0x485, 0x488, |
| 98 | | 0x48C, 0x48F, 0x492, 0x496, 0x499, 0x49C, 0x49F, 0x4A2, |
| 99 | | 0x4A6, 0x4A9, 0x4AC, 0x4AF, 0x4B2, 0x4B5, 0x4B7, 0x4BA, |
| 100 | | 0x4BD, 0x4C0, 0x4C3, 0x4C5, 0x4C8, 0x4CB, 0x4CD, 0x4D0, |
| 101 | | 0x4D2, 0x4D5, 0x4D7, 0x4D9, 0x4DC, 0x4DE, 0x4E0, 0x4E3, |
| 102 | | 0x4E5, 0x4E7, 0x4E9, 0x4EB, 0x4ED, 0x4EF, 0x4F1, 0x4F3, |
| 103 | | 0x4F5, 0x4F6, 0x4F8, 0x4FA, 0x4FB, 0x4FD, 0x4FF, 0x500, |
| 104 | | 0x502, 0x503, 0x504, 0x506, 0x507, 0x508, 0x50A, 0x50B, |
| 105 | | 0x50C, 0x50D, 0x50E, 0x50F, 0x510, 0x511, 0x511, 0x512, |
| 106 | | 0x513, 0x514, 0x514, 0x515, 0x516, 0x516, 0x517, 0x517, |
| 107 | | 0x517, 0x518, 0x518, 0x518, 0x518, 0x518, 0x519, 0x519 |
| 108 | | }; |
| 109 | | |
| 110 | | /*========== DEFINES ==========*/ |
| 111 | | |
| 112 | | #undef DEBUG |
| 113 | | #undef DBG_KEY |
| 114 | | #undef DBG_ENV |
| 115 | | #undef DBG_PMOD |
| 116 | | #undef DBG_BRR |
| 117 | | #undef DBG_ECHO |
| 118 | | #undef DBG_INTRP |
| 119 | | |
| 120 | | #undef NO_PMOD |
| 121 | | #undef NO_ECHO |
| 122 | | |
| 123 | | #define CPU_RATE ( 1024000 ) |
| 124 | | #define SAMP_FREQ ( 32000 ) |
| 125 | | |
| 126 | | /*========== VARIABLES ==========*/ |
| 127 | | |
| 128 | | static int keyed_on; |
| 129 | | static int keys; /* 8-bits for 8 voices */ |
| 130 | | static voice_state_type voice_state[ 8 ]; |
| 131 | | |
| 132 | | /* Noise stuff */ |
| 133 | | static int noise_cnt; |
| 134 | | static int noise_lev; |
| 135 | | |
| 136 | | /* These are for the FIR echo filter */ |
| 137 | | #ifndef NO_ECHO |
| 138 | | static short FIRlbuf[ 8 ]; |
| 139 | | static short FIRrbuf[ 8 ]; |
| 140 | | static int FIRptr; |
| 141 | | static int echo_ptr; |
| 142 | | #endif |
| 143 | | |
| 144 | | /*========== CONSTANTS ==========*/ |
| 145 | | |
| 146 | | /* Original SPC DSP took samples 32000 times a second, which is once every |
| 147 | | (1024000/32000 = 32) cycles. */ |
| 148 | | #ifdef UNUSED_DEFINITION |
| 149 | | static const int TS_CYC = CPU_RATE / SAMP_FREQ; |
| 150 | | #endif |
| 151 | | |
| 152 | | /* Ptrs to Gaussian table */ |
| 153 | | static const int *const G1 = &gauss[ 256 ]; |
| 154 | | static const int *const G2 = &gauss[ 512 ]; |
| 155 | | static const int *const G3 = &gauss[ 255 ]; |
| 156 | | static const int *const G4 = &gauss[ -1 ]; |
| 157 | | |
| 158 | | static const int mask = 0xFF; |
| 159 | | |
| 160 | | /* This table is for envelope timing. It represents the number of counts |
| 161 | | that should be subtracted from the counter each sample period (32kHz). |
| 162 | | The counter starts at 30720 (0x7800). */ |
| 163 | | static const int CNT_INIT = 0x7800; |
| 164 | | static const int ENVCNT[ 0x20 ] |
| 165 | | = { |
| 166 | | 0x0000, 0x000F, 0x0014, 0x0018, 0x001E, 0x0028, 0x0030, 0x003C, |
| 167 | | 0x0050, 0x0060, 0x0078, 0x00A0, 0x00C0, 0x00F0, 0x0140, 0x0180, |
| 168 | | 0x01E0, 0x0280, 0x0300, 0x03C0, 0x0500, 0x0600, 0x0780, 0x0A00, |
| 169 | | 0x0C00, 0x0F00, 0x1400, 0x1800, 0x1E00, 0x2800, 0x3C00, 0x7800 |
| 170 | | }; |
| 171 | | |
| 172 | | /*========== MACROS ==========*/ |
| 173 | | |
| 174 | | /* Make reading the ADSR code easier */ |
| 175 | | #define SL( v ) ( DSPregs[ ( ( v ) << 4 ) + 6 ] >> 5 ) |
| 176 | | /* Returns SUSTAIN level */ |
| 177 | | #define SR( v ) ( DSPregs[ ( ( v ) << 4 ) + 6 ] & 0x1F ) |
| 178 | | /* Returns SUSTAIN rate */ |
| 179 | | |
| 180 | | /* Handle endianness */ |
| 181 | | #define LEtoME16( x ) LITTLE_ENDIANIZE_INT16(x) |
| 182 | | #define MEtoLE16( x ) LITTLE_ENDIANIZE_INT16(x) |
| 183 | | |
| 184 | | /*========== PROCEDURES ==========*/ |
| 185 | | |
| 186 | | static int AdvanceEnvelope /* Run envelope step & retn ENVX*/ |
| 187 | | ( |
| 188 | | int v /* Voice to process envelope for*/ |
| 189 | | ); |
| 190 | | |
| 191 | | static STREAM_UPDATE( snes_sh_update ); |
| 192 | | |
| 193 | | /* Privately shared functions (for internal library use only) */ |
| 194 | | |
| 195 | | /***** DSP_Reset *****/ |
| 196 | | |
| 197 | | static void DSP_Reset /* Reset emulated DSP */ |
| 198 | | ( void ) |
| 199 | | { |
| 200 | | int i; |
| 201 | | |
| 202 | | #ifdef DEBUG |
| 203 | | fprintf( stderr,"DSP_Reset\n" ); |
| 204 | | #endif |
| 205 | | |
| 206 | | for( i = 0; i < 8; i++ ) |
| 207 | | { |
| 208 | | #ifndef NO_ECHO |
| 209 | | FIRlbuf[ i ] = 0; |
| 210 | | FIRrbuf[ i ] = 0; |
| 211 | | #endif |
| 212 | | voice_state[ i ].on_cnt = 0; |
| 213 | | } |
| 214 | | |
| 215 | | #ifndef NO_ECHO |
| 216 | | FIRptr = 0; |
| 217 | | echo_ptr = 0; |
| 218 | | #endif |
| 219 | | keys = 0; |
| 220 | | keyed_on = 0; |
| 221 | | noise_cnt = 0; |
| 222 | | noise_lev = 0x4000; |
| 223 | | DSPregs[ 0x6C ] |= 0xE0; |
| 224 | | DSPregs[ 0x4C ] = 0; |
| 225 | | DSPregs[ 0x5C ] = 0; |
| 226 | | |
| 227 | | } /* DSP_Reset() */ |
| 228 | | |
| 229 | | |
| 230 | | /***** DSP_Update *****/ |
| 231 | | |
| 232 | | static void DSP_Update /* Mix one sample of audio */ |
| 233 | | ( |
| 234 | | short * sound_ptr /* Pointer to mix audio into */ |
| 235 | | ) |
| 236 | | { |
| 237 | | int V; |
| 238 | | #ifndef NO_ECHO |
| 239 | | int echo_base; |
| 240 | | int echol; |
| 241 | | int echor; |
| 242 | | #endif |
| 243 | | int envx; |
| 244 | | int m; |
| 245 | | int outl; |
| 246 | | int outr; |
| 247 | | signed long outx; /* Smpl height (must be signed) */ |
| 248 | | src_dir_type * sd; |
| 249 | | int v; |
| 250 | | int vl; |
| 251 | | voice_state_type * vp; |
| 252 | | int vr; |
| 253 | | |
| 254 | | sd = ( src_dir_type * )&spc_ram[ ( int )DSPregs[ 0x5D ] << 8 ]; |
| 255 | | |
| 256 | | /* Check for reset */ |
| 257 | | if( DSPregs[ 0x6C ] & 0x80 ) |
| 258 | | { |
| 259 | | DSP_Reset(); |
| 260 | | } |
| 261 | | |
| 262 | | /* Here we check for keys on/off. Docs say that successive writes to KON/KOF |
| 263 | | must be separated by at least 2 Ts periods or risk being neglected. |
| 264 | | Therefore DSP only looks at these during an update, and not at the time of |
| 265 | | the write. Only need to do this once however, since the regs haven't |
| 266 | | changed over the whole period we need to catch up with. */ |
| 267 | | #ifdef DBG_KEY |
| 268 | | DSPregs[ 0x4C ] &= mask; |
| 269 | | #endif |
| 270 | | |
| 271 | | /* Keying on a voice resets that bit in ENDX */ |
| 272 | | DSPregs[ 0x7C ] &= ~DSPregs[ 0x4C ]; |
| 273 | | |
| 274 | | /* Question: what is the expected behavior when pitch modulation is enabled on |
| 275 | | voice 0? Jurassic Park 2 does this. For now, using outx of zero for first |
| 276 | | voice. */ |
| 277 | | outx = 0; |
| 278 | | /* Same table for noise and envelope */ |
| 279 | | noise_cnt -= ENVCNT[ DSPregs[ 0x6C ] & 0x1F ]; |
| 280 | | if( noise_cnt <= 0 ) |
| 281 | | { |
| 282 | | noise_cnt = CNT_INIT; |
| 283 | | noise_lev = ( ( ( noise_lev << 13 ) ^ ( noise_lev << 14 ) ) & 0x4000 ) |
| 284 | | | ( noise_lev >> 1 ); |
| 285 | | } |
| 286 | | outl = 0; |
| 287 | | outr = 0; |
| 288 | | #ifndef NO_ECHO |
| 289 | | echol = 0; |
| 290 | | echor = 0; |
| 291 | | #endif |
| 292 | | for( v = 0, m = 1, V = 0; v < 8; v++, V += 16, m <<= 1 ) |
| 293 | | { |
| 294 | | vp = &voice_state[ v ]; |
| 295 | | if( vp->on_cnt && ( --vp->on_cnt == 0 ) ) |
| 296 | | { |
| 297 | | /* Voice was keyed on */ |
| 298 | | keys |= m; |
| 299 | | keyed_on |= m; |
| 300 | | vl = DSPregs[ ( v<<4 ) + 4 ]; |
| 301 | | vp->samp_id = *( UINT32 * )&sd[ vl ]; |
| 302 | | vp->mem_ptr = LEtoME16( sd[ vl ].vptr ); |
| 303 | | |
| 304 | | #ifdef DBG_KEY |
| 305 | | fprintf( |
| 306 | | stderr, |
| 307 | | "Keying on voice %d, samp=0x%04X (0x%02X)\n", |
| 308 | | v, |
| 309 | | vp->mem_ptr, |
| 310 | | vl |
| 311 | | ); |
| 312 | | #endif |
| 313 | | |
| 314 | | vp->header_cnt = 0; |
| 315 | | vp->half = 0; |
| 316 | | vp->envx = 0; |
| 317 | | vp->end = 0; |
| 318 | | vp->sampptr = 0; |
| 319 | | vp->mixfrac = 3 * 4096; |
| 320 | | |
| 321 | | /* NOTE: Real SNES does *not* appear to initialize the envelope |
| 322 | | counter to anything in particular. The first cycle always seems to |
| 323 | | come at a random time sooner than expected; as yet, I have been |
| 324 | | unable to find any pattern. I doubt it will matter though, so |
| 325 | | we'll go ahead and do the full time for now. */ |
| 326 | | vp->envcnt = CNT_INIT; |
| 327 | | vp->envstate = ATTACK; |
| 328 | | } |
| 329 | | |
| 330 | | if( DSPregs[ 0x4C ] & m & ~DSPregs[ 0x5C ] ) |
| 331 | | { |
| 332 | | /* Voice doesn't come on if key off is set */ |
| 333 | | DSPregs[ 0x4C ] &= ~m; |
| 334 | | vp->on_cnt = 8; |
| 335 | | |
| 336 | | #ifdef DBG_KEY |
| 337 | | fprintf( stderr, "Key on set for voice %d\n", v ); |
| 338 | | #endif |
| 339 | | } |
| 340 | | |
| 341 | | if( keys & DSPregs[ 0x5C ] & m ) |
| 342 | | { |
| 343 | | /* Voice was keyed off */ |
| 344 | | vp->envstate = RELEASE; |
| 345 | | vp->on_cnt = 0; |
| 346 | | |
| 347 | | #ifdef DBG_KEY |
| 348 | | fprintf( stderr, "Keying off voice %d\n", v ); |
| 349 | | #endif |
| 350 | | } |
| 351 | | |
| 352 | | if( !( keys & m & mask ) || ( ( envx = AdvanceEnvelope( v ) ) < 0 ) ) |
| 353 | | { |
| 354 | | DSPregs[ V + 8 ] = 0; |
| 355 | | DSPregs[ V + 9 ] = 0; |
| 356 | | outx = 0; |
| 357 | | continue; |
| 358 | | } |
| 359 | | |
| 360 | | vp->pitch = LEtoME16( *( ( UINT16 * )&DSPregs[ V + 2 ] ) ) |
| 361 | | & 0x3FFF; |
| 362 | | |
| 363 | | #ifndef NO_PMOD |
| 364 | | /* Pitch mod uses OUTX from last voice for this one. Luckily we haven't |
| 365 | | modified OUTX since it was used for last voice. */ |
| 366 | | if(DSPregs[ 0x2D ] & m ) |
| 367 | | { |
| 368 | | #ifdef DBG_PMOD |
| 369 | | fprintf( |
| 370 | | stderr, |
| 371 | | "Pitch Modulating voice %d, outx=%ld, old pitch=%d, ", |
| 372 | | v, |
| 373 | | outx, |
| 374 | | vp->pitch |
| 375 | | ); |
| 376 | | #endif |
| 377 | | vp->pitch = ( vp->pitch * ( outx + 32768 ) ) >> 15; |
| 378 | | } |
| 379 | | #endif |
| 380 | | |
| 381 | | #ifdef DBG_PMOD |
| 382 | | fprintf( stderr, "pitch=%d\n", vp->pitch ); |
| 383 | | #endif |
| 384 | | |
| 385 | | for( ; vp->mixfrac >= 0; vp->mixfrac -= 4096 ) |
| 386 | | { |
| 387 | | /* This part performs the BRR decode 'on-the-fly'. This is more |
| 388 | | correct than the old way, which could be fooled if the data and/or |
| 389 | | the loop point changed while the sample was playing, or if the BRR |
| 390 | | decode didn't produce the same result every loop because of the |
| 391 | | filters. The event interface still has no chance of keeping up |
| 392 | | with those kinds of tricks, though. */ |
| 393 | | if( !vp->header_cnt ) |
| 394 | | { |
| 395 | | if( vp->end & 1 ) |
| 396 | | { |
| 397 | | /* Docs say ENDX bit is set when decode of block with source |
| 398 | | end flag set is done. Does this apply to looping samples? |
| 399 | | Some info I've seen suggests yes. */ |
| 400 | | DSPregs[ 0x7C ] |= m; |
| 401 | | if( vp->end & 2 ) |
| 402 | | { |
| 403 | | vp->mem_ptr = LEtoME16( sd[ DSPregs[ V + 4 ] ].lptr ); |
| 404 | | |
| 405 | | #ifdef DBG_BRR |
| 406 | | fprintf( |
| 407 | | stderr, |
| 408 | | "BRR looping to 0x%04X\n", |
| 409 | | vp->mem_ptr |
| 410 | | ); |
| 411 | | #endif |
| 412 | | } |
| 413 | | else |
| 414 | | { |
| 415 | | #ifdef DBG_KEY |
| 416 | | fprintf( stderr, "BRR decode end, voice %d\n", v ); |
| 417 | | #endif |
| 418 | | |
| 419 | | keys &= ~m; |
| 420 | | DSPregs[ V + 8 ] = 0; |
| 421 | | vp->envx = 0; |
| 422 | | while( vp->mixfrac >= 0 ) |
| 423 | | { |
| 424 | | vp->sampbuf[ vp->sampptr ] = 0; |
| 425 | | outx = 0; |
| 426 | | vp->sampptr = ( vp->sampptr + 1 ) & 3; |
| 427 | | vp->mixfrac -= 4096; |
| 428 | | } |
| 429 | | break; |
| 430 | | } |
| 431 | | } |
| 432 | | vp->header_cnt = 8; |
| 433 | | vl = ( UINT8 )spc_ram[ vp->mem_ptr++ ]; |
| 434 | | vp->range = vl >> 4; |
| 435 | | vp->end = vl & 3; |
| 436 | | vp->filter = ( vl & 12 ) >> 2; |
| 437 | | |
| 438 | | #ifdef DBG_BRR |
| 439 | | fprintf( |
| 440 | | stderr, |
| 441 | | "V%d: header read, range=%d, end=%d, filter=%d\n", |
| 442 | | v, |
| 443 | | vp->range, |
| 444 | | vp->end, |
| 445 | | vp->filter |
| 446 | | ); |
| 447 | | #endif |
| 448 | | } |
| 449 | | if( vp->half == 0 ) |
| 450 | | { |
| 451 | | vp->half = 1; |
| 452 | | outx = ( ( signed char )spc_ram[ vp->mem_ptr ] ) >> 4; |
| 453 | | } |
| 454 | | else |
| 455 | | { |
| 456 | | vp->half = 0; |
| 457 | | /* Funkiness to get 4-bit signed to carry through */ |
| 458 | | outx = ( signed char )( spc_ram[ vp->mem_ptr++ ] << 4 ); |
| 459 | | outx >>= 4; |
| 460 | | vp->header_cnt--; |
| 461 | | } |
| 462 | | |
| 463 | | #ifdef DBG_BRR |
| 464 | | fprintf( |
| 465 | | stderr, |
| 466 | | "V%d: nybble=%X, ptr=%04X, smp1=%d, smp2=%d\n", |
| 467 | | v, |
| 468 | | outx & 0xF, |
| 469 | | vp->mem_ptr, |
| 470 | | vp->smp1, |
| 471 | | vp->smp2 |
| 472 | | ); |
| 473 | | #endif |
| 474 | | |
| 475 | | /* For invalid ranges (D,E,F): if the nybble is negative, the result |
| 476 | | is F000. If positive, 0000. Nothing else like previous range, |
| 477 | | etc. seems to have any effect. If range is valid, do the shift |
| 478 | | normally. Note these are both shifted right once to do the filters |
| 479 | | properly, but the output will be shifted back again at the end. */ |
| 480 | | if( vp->range <= 0xC ) |
| 481 | | { |
| 482 | | outx = ( outx << vp->range ) >> 1; |
| 483 | | } |
| 484 | | else |
| 485 | | { |
| 486 | | outx &= ~0x7FF; |
| 487 | | |
| 488 | | #ifdef DBG_BRR |
| 489 | | fprintf( stderr, "V%d: invalid range! (%X)\n", v, vp->range ); |
| 490 | | #endif |
| 491 | | } |
| 492 | | |
| 493 | | #ifdef DBG_BRR |
| 494 | | fprintf( |
| 495 | | stderr, |
| 496 | | "V%d: shifted delta=%04X\n", |
| 497 | | v, |
| 498 | | ( UINT16 )outx |
| 499 | | ); |
| 500 | | #endif |
| 501 | | |
| 502 | | switch( vp->filter ) |
| 503 | | { |
| 504 | | case 0: |
| 505 | | break; |
| 506 | | |
| 507 | | case 1: |
| 508 | | outx += ( vp->smp1 >> 1 ) + ( ( -vp->smp1 ) >> 5 ); |
| 509 | | break; |
| 510 | | |
| 511 | | case 2: |
| 512 | | outx += vp->smp1 |
| 513 | | + ( ( -( vp->smp1 + ( vp->smp1 >> 1 ) ) ) >> 5 ) |
| 514 | | - ( vp->smp2 >> 1 ) + ( vp->smp2 >> 5 ); |
| 515 | | break; |
| 516 | | |
| 517 | | case 3: |
| 518 | | outx += vp->smp1 |
| 519 | | + ( ( -( vp->smp1 + ( vp->smp1 << 2 ) |
| 520 | | + ( vp->smp1 << 3 ) ) ) >> 7 ) |
| 521 | | - ( vp->smp2 >> 1 ) |
| 522 | | + ( ( vp->smp2 + ( vp->smp2 >> 1 ) ) >> 4 ); |
| 523 | | break; |
| 524 | | } |
| 525 | | |
| 526 | | if( outx < ( signed short )0x8000 ) |
| 527 | | { |
| 528 | | outx = ( signed short )0x8000; |
| 529 | | } |
| 530 | | else if( outx > ( signed short )0x7FFF ) |
| 531 | | { |
| 532 | | outx = ( signed short )0x7FFF; |
| 533 | | } |
| 534 | | |
| 535 | | #ifdef DBG_BRR |
| 536 | | fprintf( |
| 537 | | stderr, |
| 538 | | "V%d: filter + delta=%04X\n", |
| 539 | | v, |
| 540 | | ( UINT16 )outx |
| 541 | | ); |
| 542 | | #endif |
| 543 | | |
| 544 | | vp->smp2 = ( signed short )vp->smp1; |
| 545 | | vp->smp1 = ( signed short )( outx << 1 ); |
| 546 | | vp->sampbuf[ vp->sampptr ] = vp->smp1; |
| 547 | | |
| 548 | | #ifdef DBG_BRR |
| 549 | | fprintf( |
| 550 | | stderr, |
| 551 | | "V%d: final output: %04X\n", |
| 552 | | v, |
| 553 | | vp->sampbuf[ vp->sampptr ] |
| 554 | | ); |
| 555 | | #endif |
| 556 | | |
| 557 | | vp->sampptr = ( vp->sampptr + 1 ) & 3; |
| 558 | | } |
| 559 | | |
| 560 | | if(DSPregs[ 0x3D ] & m ) |
| 561 | | { |
| 562 | | #ifdef DBG_PMOD |
| 563 | | fprintf( stderr, "Noise enabled, voice %d\n", v ); |
| 564 | | #endif |
| 565 | | outx = ( signed short )( noise_lev << 1 ); |
| 566 | | } |
| 567 | | else |
| 568 | | { |
| 569 | | /* Perform 4-Point Gaussian interpolation. Take an approximation of a |
| 570 | | Gaussian bell-curve, and move it through the sample data at a rate |
| 571 | | determined by the pitch. The sample output at any given time is |
| 572 | | the sum of the products of each input sample point with the value |
| 573 | | of the bell-curve corresponding to that point. */ |
| 574 | | vl = vp->mixfrac >> 4; |
| 575 | | vr = ( ( G4[ -vl ] * vp->sampbuf[ vp->sampptr ] ) >> 11 ) & ~1; |
| 576 | | vr += ( ( G3[ -vl ] |
| 577 | | * vp->sampbuf[ ( vp->sampptr + 1 ) & 3 ] ) >> 11 ) & ~1; |
| 578 | | vr += ( ( G2[ vl ] |
| 579 | | * vp->sampbuf[ ( vp->sampptr + 2 ) & 3 ] ) >> 11 ) & ~1; |
| 580 | | |
| 581 | | /* This is to do the wrapping properly. Based on my tests with the |
| 582 | | SNES, it appears clipping is done only if it is the fourth addition |
| 583 | | that would cause a wrap. If it has already wrapped before the |
| 584 | | fourth addition, it is not clipped. */ |
| 585 | | vr = ( signed short )vr; |
| 586 | | vr += ( ( G1[ vl ] |
| 587 | | * vp->sampbuf[ ( vp->sampptr + 3 ) & 3 ] ) >> 11 ) & ~1; |
| 588 | | if( vr > 32767 ) |
| 589 | | { |
| 590 | | vr = 32767; |
| 591 | | } |
| 592 | | else if( vr < -32768 ) |
| 593 | | { |
| 594 | | vr = -32768; |
| 595 | | } |
| 596 | | outx = ( signed short )vr; |
| 597 | | |
| 598 | | #ifdef DBG_INTRP |
| 599 | | fprintf( |
| 600 | | stderr, |
| 601 | | "V%d: mixfrac=%d: [%d]*%d + [%d]*%d + [%d]*%d + [%d]*%d = %d\n", |
| 602 | | v, |
| 603 | | vl, |
| 604 | | G1[ vl ], |
| 605 | | vp->sampbuf[ ( vp->sampptr + 3 ) & 3 ], |
| 606 | | G2[ vl ], |
| 607 | | vp->sampbuf[ ( vp->sampptr + 2 ) & 3 ], |
| 608 | | G3[ -vl ], |
| 609 | | vp->sampbuf[ ( vp->sampptr + 1 ) & 3 ], |
| 610 | | G4[ -vl ], |
| 611 | | vp->sampbuf[ vp->sampptr ], |
| 612 | | outx |
| 613 | | ); |
| 614 | | #endif |
| 615 | | } |
| 616 | | |
| 617 | | /* Advance the sample position for next update. */ |
| 618 | | vp->mixfrac += vp->pitch; |
| 619 | | |
| 620 | | outx = ( ( outx * envx ) >> 11 ) & ~1; |
| 621 | | DSPregs[ V + 9 ] = outx >> 8; |
| 622 | | |
| 623 | | vl = ( ( ( int )( signed char )DSPregs[ V ] ) * outx ) >> 7; |
| 624 | | vr = ( ( ( int )( signed char )DSPregs[ V + 1 ] ) * outx ) >> 7; |
| 625 | | outl += vl; |
| 626 | | outr += vr; |
| 627 | | if( DSPregs[ 0x4D ] & m ) |
| 628 | | { |
| 629 | | #ifndef NO_ECHO |
| 630 | | echol += vl; |
| 631 | | echor += vr; |
| 632 | | #endif |
| 633 | | } |
| 634 | | } |
| 635 | | outl = ( outl * ( signed char )DSPregs[ 0x0C ] ) >> 7; |
| 636 | | outr = ( outr * ( signed char )DSPregs[ 0x1C ] ) >> 7; |
| 637 | | |
| 638 | | #ifndef NO_ECHO |
| 639 | | /* Perform echo. First, read mem at current location, and put those samples |
| 640 | | into the FIR filter queue. */ |
| 641 | | #ifdef DBG_ECHO |
| 642 | | fprintf( |
| 643 | | stderr, |
| 644 | | "Echo delay=%dms, feedback=%d%%\n", |
| 645 | | DSPregs[ 0x7D ] * 16, |
| 646 | | ( ( signed char )DSPregs[ 0x0D ] * 100 ) / 0x7F |
| 647 | | ); |
| 648 | | #endif |
| 649 | | |
| 650 | | echo_base = ( ( DSPregs[ 0x6D ] << 8 ) + echo_ptr ) & 0xFFFF; |
| 651 | | FIRlbuf[ FIRptr ] |
| 652 | | = ( signed short )LEtoME16( |
| 653 | | *( UINT16 * ) |
| 654 | | &spc_ram[ echo_base ] |
| 655 | | ); |
| 656 | | FIRrbuf[ FIRptr ] |
| 657 | | = ( signed short )LEtoME16( |
| 658 | | *( UINT16 * ) |
| 659 | | &spc_ram[ echo_base + sizeof( short ) ] |
| 660 | | ); |
| 661 | | |
| 662 | | /* Now, evaluate the FIR filter, and add the results into the final output. */ |
| 663 | | vl = FIRlbuf[ FIRptr ] * ( signed char )DSPregs[ 0x7F ]; |
| 664 | | vr = FIRrbuf[ FIRptr ] * ( signed char )DSPregs[ 0x7F ]; |
| 665 | | FIRptr = ( FIRptr + 1 ) & 7; |
| 666 | | vl += FIRlbuf[ FIRptr ] * ( signed char )DSPregs[ 0x6F ]; |
| 667 | | vr += FIRrbuf[ FIRptr ] * ( signed char )DSPregs[ 0x6F ]; |
| 668 | | FIRptr = ( FIRptr + 1 ) & 7; |
| 669 | | vl += FIRlbuf[ FIRptr ] * ( signed char )DSPregs[ 0x5F ]; |
| 670 | | vr += FIRrbuf[ FIRptr ] * ( signed char )DSPregs[ 0x5F ]; |
| 671 | | FIRptr = ( FIRptr + 1 ) & 7; |
| 672 | | vl += FIRlbuf[ FIRptr ] * ( signed char )DSPregs[ 0x4F ]; |
| 673 | | vr += FIRrbuf[ FIRptr ] * ( signed char )DSPregs[ 0x4F ]; |
| 674 | | FIRptr = ( FIRptr + 1 ) & 7; |
| 675 | | vl += FIRlbuf[ FIRptr ] * ( signed char )DSPregs[ 0x3F ]; |
| 676 | | vr += FIRrbuf[ FIRptr ] * ( signed char )DSPregs[ 0x3F ]; |
| 677 | | FIRptr = ( FIRptr + 1 ) & 7; |
| 678 | | vl += FIRlbuf[ FIRptr ] * ( signed char )DSPregs[ 0x2F ]; |
| 679 | | vr += FIRrbuf[ FIRptr ] * ( signed char )DSPregs[ 0x2F ]; |
| 680 | | FIRptr = ( FIRptr + 1 ) & 7; |
| 681 | | vl += FIRlbuf[ FIRptr ] * ( signed char )DSPregs[ 0x1F ]; |
| 682 | | vr += FIRrbuf[ FIRptr ] * ( signed char )DSPregs[ 0x1F ]; |
| 683 | | FIRptr = ( FIRptr + 1 ) & 7; |
| 684 | | vl += FIRlbuf[ FIRptr ] * ( signed char )DSPregs[ 0x0F ]; |
| 685 | | vr += FIRrbuf[ FIRptr ] * ( signed char )DSPregs[ 0x0F ]; |
| 686 | | |
| 687 | | #ifdef DBG_ECHO |
| 688 | | fprintf( |
| 689 | | stderr, |
| 690 | | "FIR Coefficients: %02X %02X %02X %02X %02X %02X %02X %02X\n", |
| 691 | | DSPregs[ 0x0F ], |
| 692 | | DSPregs[ 0x1F ], |
| 693 | | DSPregs[ 0x2F ], |
| 694 | | DSPregs[ 0x3F ], |
| 695 | | DSPregs[ 0x4F ], |
| 696 | | DSPregs[ 0x5F ], |
| 697 | | DSPregs[ 0x6F ], |
| 698 | | DSPregs[ 0x7F ] |
| 699 | | ); |
| 700 | | #endif |
| 701 | | |
| 702 | | /* FIRptr is left in the position of the oldest sample, the one that will be |
| 703 | | replaced next update. */ |
| 704 | | outl += vl * ( signed char )DSPregs[ 0x2C ] >> 14; |
| 705 | | outr += vr * ( signed char )DSPregs[ 0x3C ] >> 14; |
| 706 | | |
| 707 | | if( !( DSPregs[ 0x6C ] & 0x20 ) ) |
| 708 | | { |
| 709 | | /* Add the echo feedback back into the original result, and save that into |
| 710 | | memory for use later. */ |
| 711 | | echol += vl * ( signed char )DSPregs[ 0x0D ] >> 14; |
| 712 | | if( echol > 32767 ) |
| 713 | | { |
| 714 | | echol = 32767; |
| 715 | | } |
| 716 | | else if( echol < -32768 ) |
| 717 | | { |
| 718 | | echol = -32768; |
| 719 | | } |
| 720 | | echor += vr * ( signed char )DSPregs[ 0x0D ] >> 14; |
| 721 | | if( echor > 32767 ) |
| 722 | | { |
| 723 | | echor = 32767; |
| 724 | | } |
| 725 | | else if( echor < -32768 ) |
| 726 | | { |
| 727 | | echor = -32768; |
| 728 | | } |
| 729 | | |
| 730 | | #ifdef DBG_ECHO |
| 731 | | fprintf( |
| 732 | | stderr, |
| 733 | | "Echo: Writing %04X,%04X at location %04X\n", |
| 734 | | ( UINT16 )echol, |
| 735 | | ( UINT16 )echor, |
| 736 | | echo_base |
| 737 | | ); |
| 738 | | #endif |
| 739 | | |
| 740 | | *( UINT16 * )&spc_ram[ echo_base ] |
| 741 | | = MEtoLE16( ( UINT16 )echol ); |
| 742 | | *( UINT16 * )&spc_ram[ echo_base + sizeof( short ) ] |
| 743 | | = MEtoLE16( ( UINT16 )echor ); |
| 744 | | } |
| 745 | | |
| 746 | | echo_ptr += 2 * sizeof( short ); |
| 747 | | if( echo_ptr >= ( ( DSPregs[ 0x7D ] & 0xF ) << 11 ) ) |
| 748 | | { |
| 749 | | echo_ptr = 0; |
| 750 | | } |
| 751 | | #endif /* !defined( NO_ECHO ) */ |
| 752 | | |
| 753 | | if( sound_ptr ) |
| 754 | | { |
| 755 | | if( DSPregs[ 0x6C ] & 0x40 ) |
| 756 | | { |
| 757 | | /* MUTE */ |
| 758 | | #ifdef DEBUG |
| 759 | | fprintf( stderr, "MUTED!\n" ); |
| 760 | | #endif |
| 761 | | |
| 762 | | *sound_ptr = 0; |
| 763 | | sound_ptr++; |
| 764 | | *sound_ptr = 0; |
| 765 | | sound_ptr++; |
| 766 | | } |
| 767 | | else |
| 768 | | { |
| 769 | | if( outl > 32767 ) |
| 770 | | { |
| 771 | | *sound_ptr = 32767; |
| 772 | | } |
| 773 | | else if( outl < -32768 ) |
| 774 | | { |
| 775 | | *sound_ptr = -32768; |
| 776 | | } |
| 777 | | else |
| 778 | | { |
| 779 | | *sound_ptr = outl; |
| 780 | | } |
| 781 | | sound_ptr++; |
| 782 | | if( outr > 32767 ) |
| 783 | | { |
| 784 | | *sound_ptr = 32767; |
| 785 | | } |
| 786 | | else if( outr < -32768 ) |
| 787 | | { |
| 788 | | *sound_ptr = -32768; |
| 789 | | } |
| 790 | | else |
| 791 | | { |
| 792 | | *sound_ptr = outr; |
| 793 | | } |
| 794 | | sound_ptr++; |
| 795 | | } |
| 796 | | } |
| 797 | | |
| 798 | | } /* DSP_Update() */ |
| 799 | | |
| 800 | | |
| 801 | | /***** AdvanceEnvelope *****/ |
| 802 | | |
| 803 | | static int AdvanceEnvelope /* Run envelope step & retn ENVX*/ |
| 804 | | ( |
| 805 | | int v /* Voice to process envelope for*/ |
| 806 | | ) |
| 807 | | { |
| 808 | | int envx; |
| 809 | | int cnt; |
| 810 | | int adsr1; |
| 811 | | int t; |
| 812 | | |
| 813 | | envx = voice_state[ v ].envx; |
| 814 | | |
| 815 | | if( voice_state[ v ].envstate == RELEASE ) |
| 816 | | { |
| 817 | | /* Docs: "When in the state of "key off". the "click" sound is prevented |
| 818 | | by the addition of the fixed value 1/256" WTF??? Alright, I'm going |
| 819 | | to choose to interpret that this way: When a note is keyed off, start |
| 820 | | the RELEASE state, which subtracts 1/256th each sample period (32kHz). |
| 821 | | Note there's no need for a count because it always happens every |
| 822 | | update. */ |
| 823 | | envx -= 0x8; /* 0x8 / 0x800 = 1/256th */ |
| 824 | | if( envx <= 0 ) |
| 825 | | { |
| 826 | | envx = 0; |
| 827 | | keys &= ~( 1 << v ); |
| 828 | | return -1; |
| 829 | | } |
| 830 | | voice_state[ v ].envx = envx; |
| 831 | | DSPregs[ ( v << 4 ) + 8 ] = envx >> 8; |
| 832 | | |
| 833 | | #ifdef DBG_ENV |
| 834 | | fprintf( |
| 835 | | stderr, |
| 836 | | "ENV voice %d: envx=%03X, state=RELEASE\n", |
| 837 | | v, |
| 838 | | envx |
| 839 | | ); |
| 840 | | #endif |
| 841 | | |
| 842 | | return( envx ); |
| 843 | | } |
| 844 | | |
| 845 | | cnt = voice_state[ v ].envcnt; |
| 846 | | adsr1 = DSPregs[ ( v << 4 ) + 5 ]; |
| 847 | | if( adsr1 & 0x80 ) |
| 848 | | { |
| 849 | | switch( voice_state[ v ].envstate ) |
| 850 | | { |
| 851 | | case ATTACK: |
| 852 | | /* Docs are very confusing. "AR is multiplied by the fixed value |
| 853 | | 1/64..." I believe it means to add 1/64th to ENVX once every |
| 854 | | time ATTACK is updated, and that's what I'm going to implement. |
| 855 | | */ |
| 856 | | t = adsr1 & 0xF; |
| 857 | | if( t == 0xF ) |
| 858 | | { |
| 859 | | #ifdef DBG_ENV |
| 860 | | fprintf( stderr, "ENV voice %d: instant attack\n", v ); |
| 861 | | #endif |
| 862 | | |
| 863 | | envx += 0x400; |
| 864 | | } |
| 865 | | else |
| 866 | | { |
| 867 | | cnt -= ENVCNT[ ( t << 1 ) + 1 ]; |
| 868 | | if( cnt > 0 ) |
| 869 | | { |
| 870 | | break; |
| 871 | | } |
| 872 | | envx += 0x20; /* 0x020 / 0x800 = 1/64 */ |
| 873 | | cnt = CNT_INIT; |
| 874 | | } |
| 875 | | |
| 876 | | if( envx > 0x7FF ) |
| 877 | | { |
| 878 | | envx = 0x7FF; |
| 879 | | voice_state[ v ].envstate = DECAY; |
| 880 | | } |
| 881 | | |
| 882 | | #ifdef DBG_ENV |
| 883 | | fprintf( |
| 884 | | stderr, |
| 885 | | "ENV voice %d: envx=%03X, state=ATTACK\n", |
| 886 | | v, |
| 887 | | envx |
| 888 | | ); |
| 889 | | #endif |
| 890 | | |
| 891 | | voice_state[ v ].envx = envx; |
| 892 | | |
| 893 | | break; |
| 894 | | |
| 895 | | case DECAY: |
| 896 | | /* Docs: "DR... [is multiplied] by the fixed value 1-1/256." |
| 897 | | Well, at least that makes some sense. Multiplying ENVX by |
| 898 | | 255/256 every time DECAY is updated. */ |
| 899 | | cnt -= ENVCNT[ ( ( adsr1 >> 3 ) & 0xE ) + 0x10 ]; |
| 900 | | if( cnt <= 0 ) |
| 901 | | { |
| 902 | | cnt = CNT_INIT; |
| 903 | | envx -= ( ( envx - 1 ) >> 8 ) + 1; |
| 904 | | voice_state[ v ].envx = envx; |
| 905 | | } |
| 906 | | |
| 907 | | if( envx <= 0x100 * ( SL( v ) + 1 ) ) |
| 908 | | { |
| 909 | | voice_state[ v ].envstate = SUSTAIN; |
| 910 | | } |
| 911 | | |
| 912 | | #ifdef DBG_ENV |
| 913 | | fprintf( |
| 914 | | stderr, |
| 915 | | "ENV voice %d: envx=%03X, state=DECAY\n", |
| 916 | | v, |
| 917 | | envx |
| 918 | | ); |
| 919 | | #endif |
| 920 | | |
| 921 | | break; |
| 922 | | |
| 923 | | case SUSTAIN: |
| 924 | | /* Docs: "SR [is multiplied] by the fixed value 1-1/256." |
| 925 | | Multiplying ENVX by 255/256 every time SUSTAIN is updated. */ |
| 926 | | #ifdef DBG_ENV |
| 927 | | if( ENVCNT[ SR( v ) ] == 0 ) |
| 928 | | { |
| 929 | | fprintf( |
| 930 | | stderr, |
| 931 | | "ENV voice %d: envx=%03X, state=SUSTAIN, zero rate\n", |
| 932 | | v, |
| 933 | | envx |
| 934 | | ); |
| 935 | | } |
| 936 | | #endif |
| 937 | | |
| 938 | | cnt -= ENVCNT[ SR( v ) ]; |
| 939 | | if( cnt > 0 ) |
| 940 | | { |
| 941 | | break; |
| 942 | | } |
| 943 | | cnt = CNT_INIT; |
| 944 | | envx -= ( ( envx - 1 ) >> 8 ) + 1; |
| 945 | | |
| 946 | | #ifdef DBG_ENV |
| 947 | | fprintf( |
| 948 | | stderr, |
| 949 | | "ENV voice %d: envx=%03X, state=SUSTAIN\n", |
| 950 | | v, |
| 951 | | envx |
| 952 | | ); |
| 953 | | #endif |
| 954 | | |
| 955 | | voice_state[ v ].envx = envx; |
| 956 | | |
| 957 | | /* Note: no way out of this state except by explicit KEY OFF (or |
| 958 | | switch to GAIN). */ |
| 959 | | break; |
| 960 | | |
| 961 | | case RELEASE: /* Handled earlier to prevent GAIN mode from stopping |
| 962 | | KEY OFF events */ |
| 963 | | break; |
| 964 | | } |
| 965 | | } |
| 966 | | else |
| 967 | | { |
| 968 | | /* GAIN mode is set |
| 969 | | Note: if the game switches between ADSR and GAIN modes partway |
| 970 | | through, should the count be reset, or should it continue from |
| 971 | | where it was? Does the DSP actually watch for that bit to |
| 972 | | change, or does it just go along with whatever it sees when it |
| 973 | | performs the update? I'm going to assume the latter and not |
| 974 | | update the count, unless I see a game that obviously wants the |
| 975 | | other behavior. The effect would be pretty subtle, in any case. |
| 976 | | */ |
| 977 | | t = DSPregs[ ( v << 4 ) + 7 ]; |
| 978 | | if( t < 0x80 ) |
| 979 | | { |
| 980 | | envx = t << 4; |
| 981 | | voice_state[ v ].envx = envx; |
| 982 | | |
| 983 | | #ifdef DBG_ENV |
| 984 | | fprintf( |
| 985 | | stderr, |
| 986 | | "ENV voice %d: envx=%03X, state=DIRECT\n", |
| 987 | | v, |
| 988 | | envx |
| 989 | | ); |
| 990 | | #endif |
| 991 | | } |
| 992 | | else |
| 993 | | { |
| 994 | | switch( t >> 5 ) |
| 995 | | { |
| 996 | | case 4: |
| 997 | | /* Docs: "Decrease (linear): Subtraction of the fixed value |
| 998 | | 1/64." */ |
| 999 | | cnt -= ENVCNT[ t & 0x1F ]; |
| 1000 | | if( cnt > 0 ) |
| 1001 | | { |
| 1002 | | break; |
| 1003 | | } |
| 1004 | | cnt = CNT_INIT; |
| 1005 | | envx -= 0x020; /* 0x020 / 0x800 = 1/64th */ |
| 1006 | | if( envx < 0 ) |
| 1007 | | { |
| 1008 | | envx = 0; |
| 1009 | | } |
| 1010 | | |
| 1011 | | #ifdef DBG_ENV |
| 1012 | | fprintf( |
| 1013 | | stderr, |
| 1014 | | "ENV voice %d: envx=%03X, state=DECREASE\n", |
| 1015 | | v, |
| 1016 | | envx |
| 1017 | | ); |
| 1018 | | #endif |
| 1019 | | |
| 1020 | | voice_state[ v ].envx = envx; |
| 1021 | | |
| 1022 | | break; |
| 1023 | | |
| 1024 | | case 5: |
| 1025 | | /* Docs: "Drecrease <sic> (exponential): Multiplication by |
| 1026 | | the fixed value 1-1/256." */ |
| 1027 | | cnt -= ENVCNT[ t & 0x1F ]; |
| 1028 | | if( cnt > 0 ) |
| 1029 | | { |
| 1030 | | break; |
| 1031 | | } |
| 1032 | | cnt = CNT_INIT; |
| 1033 | | envx -= ( ( envx - 1 ) >> 8 ) + 1; |
| 1034 | | |
| 1035 | | #ifdef DBG_ENV |
| 1036 | | fprintf( |
| 1037 | | stderr, |
| 1038 | | "ENV voice %d: envx=%03X, state=EXP\n", |
| 1039 | | v, |
| 1040 | | envx |
| 1041 | | ); |
| 1042 | | #endif |
| 1043 | | |
| 1044 | | voice_state[ v ].envx = envx; |
| 1045 | | |
| 1046 | | break; |
| 1047 | | |
| 1048 | | case 6: |
| 1049 | | /* Docs: "Increase (linear): Addition of the fixed value |
| 1050 | | 1/64." */ |
| 1051 | | cnt -= ENVCNT[ t & 0x1F ]; |
| 1052 | | if( cnt > 0 ) |
| 1053 | | { |
| 1054 | | break; |
| 1055 | | } |
| 1056 | | cnt = CNT_INIT; |
| 1057 | | envx += 0x020; /* 0x020 / 0x800 = 1/64th */ |
| 1058 | | if( envx > 0x7FF ) |
| 1059 | | { |
| 1060 | | envx = 0x7FF; |
| 1061 | | } |
| 1062 | | |
| 1063 | | #ifdef DBG_ENV |
| 1064 | | fprintf( |
| 1065 | | stderr, |
| 1066 | | "ENV voice %d: envx=%03X, state=INCREASE\n", |
| 1067 | | v, |
| 1068 | | envx |
| 1069 | | ); |
| 1070 | | #endif |
| 1071 | | |
| 1072 | | voice_state[ v ].envx = envx; |
| 1073 | | |
| 1074 | | break; |
| 1075 | | |
| 1076 | | case 7: |
| 1077 | | /* Docs: "Increase (bent line): Addition of the constant |
| 1078 | | 1/64 up to .75 of the constaint <sic> 1/256 from .75 to |
| 1079 | | 1." */ |
| 1080 | | cnt -= ENVCNT[ t & 0x1F ]; |
| 1081 | | if( cnt > 0 ) |
| 1082 | | { |
| 1083 | | break; |
| 1084 | | } |
| 1085 | | cnt = CNT_INIT; |
| 1086 | | if( envx < 0x600 ) /* 0x600 / 0x800 = .75 */ |
| 1087 | | { |
| 1088 | | envx += 0x020; /* 0x020 / 0x800 = 1/64 */ |
| 1089 | | } |
| 1090 | | else |
| 1091 | | { |
| 1092 | | envx += 0x008; /* 0x008 / 0x800 = 1/256 */ |
| 1093 | | } |
| 1094 | | if( envx > 0x7FF ) |
| 1095 | | { |
| 1096 | | envx=0x7FF; |
| 1097 | | } |
| 1098 | | |
| 1099 | | #ifdef DBG_ENV |
| 1100 | | fprintf( |
| 1101 | | stderr, |
| 1102 | | "ENV voice %d: envx=%03X, state=INCREASE\n", |
| 1103 | | v, |
| 1104 | | envx |
| 1105 | | ); |
| 1106 | | #endif |
| 1107 | | |
| 1108 | | voice_state[ v ].envx = envx; |
| 1109 | | |
| 1110 | | break; |
| 1111 | | } |
| 1112 | | } |
| 1113 | | } |
| 1114 | | |
| 1115 | | voice_state[ v ].envcnt = cnt; |
| 1116 | | DSPregs[ ( v << 4 ) + 8 ] = envx >> 4; |
| 1117 | | |
| 1118 | | return( envx ); |
| 1119 | | |
| 1120 | | } /* AdvanceEnvelope() */ |
| 1121 | | |
| 1122 | | static TIMER_CALLBACK( snes_spc_timer ) |
| 1123 | | { |
| 1124 | | int which = param; |
| 1125 | | |
| 1126 | | timers[which].counter++; |
| 1127 | | if( timers[which].counter >= spc_ram[0xfa + which] ) // minus = |
| 1128 | | { |
| 1129 | | timers[which].counter = 0; |
| 1130 | | spc_ram[0xfd + which]++; |
| 1131 | | spc_ram[0xfd + which]&= 0xf; |
| 1132 | | } |
| 1133 | | } |
| 1134 | | |
| 1135 | | static DEVICE_START( snes_sound ) |
| 1136 | | { |
| 1137 | | running_machine *machine = device->machine; |
| 1138 | | UINT8 ii; |
| 1139 | | |
| 1140 | | /* put IPL image at the top of RAM */ |
| 1141 | | memcpy(snes_ipl_region, memory_region(machine, "user5"), 64); |
| 1142 | | |
| 1143 | | /* default to ROM visible */ |
| 1144 | | spc_ram[0xf1] = 0x80; |
| 1145 | | |
| 1146 | | /* Sort out the ports */ |
| 1147 | | for( ii = 0; ii < 4; ii++ ) |
| 1148 | | { |
| 1149 | | spc_port_in[ii] = 0; |
| 1150 | | spc_port_out[ii] = 0; |
| 1151 | | } |
| 1152 | | |
| 1153 | | channel = stream_create( device, 0, 2, 32000, NULL, snes_sh_update ); |
| 1154 | | |
| 1155 | | /* Initialize the timers */ |
| 1156 | | timers[0].timer = timer_alloc(machine, snes_spc_timer , NULL); |
| 1157 | | timer_adjust_periodic( timers[0].timer, ATTOTIME_IN_HZ(8000), 0, ATTOTIME_IN_HZ(8000) ); |
| 1158 | | timer_enable( timers[0].timer, 0 ); |
| 1159 | | timers[1].timer = timer_alloc(machine, snes_spc_timer , NULL); |
| 1160 | | timer_adjust_periodic( timers[1].timer, ATTOTIME_IN_HZ(8000), 1, ATTOTIME_IN_HZ(8000) ); |
| 1161 | | timer_enable( timers[1].timer, 0 ); |
| 1162 | | timers[2].timer = timer_alloc(machine, snes_spc_timer , NULL); |
| 1163 | | timer_adjust_periodic( timers[2].timer, ATTOTIME_IN_HZ(64000), 2, ATTOTIME_IN_HZ(64000) ); |
| 1164 | | timer_enable( timers[2].timer, 0 ); |
| 1165 | | |
| 1166 | | DSP_Reset(); |
| 1167 | | } |
| 1168 | | |
| 1169 | | DEVICE_GET_INFO( snes_sound ) |
| 1170 | | { |
| 1171 | | switch (state) |
| 1172 | | { |
| 1173 | | /* --- the following bits of info are returned as pointers to data or functions --- */ |
| 1174 | | case DEVINFO_FCT_START: info->start = DEVICE_START_NAME(snes_sound); break; |
| 1175 | | |
| 1176 | | /* --- the following bits of info are returned as NULL-terminated strings --- */ |
| 1177 | | case DEVINFO_STR_NAME: strcpy(info->s, "SNES Custom"); break; |
| 1178 | | case DEVINFO_STR_SOURCE_FILE: strcpy(info->s, __FILE__); break; |
| 1179 | | } |
| 1180 | | } |
| 1181 | | |
| 1182 | | |
| 1183 | | static STREAM_UPDATE( snes_sh_update ) |
| 1184 | | { |
| 1185 | | int i; |
| 1186 | | short mix[2]; |
| 1187 | | |
| 1188 | | for (i = 0; i < samples; i++) |
| 1189 | | { |
| 1190 | | mix[0] = mix[1] = 0; |
| 1191 | | DSP_Update(mix); |
| 1192 | | |
| 1193 | | /* Update the buffers */ |
| 1194 | | outputs[0][i] = (stream_sample_t)mix[0]; |
| 1195 | | outputs[1][i] = (stream_sample_t)mix[1]; |
| 1196 | | } |
| 1197 | | } |
| 1198 | | |
| 1199 | | /*************************** |
| 1200 | | * I/O for DSP * |
| 1201 | | ***************************/ |
| 1202 | | |
| 1203 | | static READ8_HANDLER( snes_dsp_io_r ) |
| 1204 | | { |
| 1205 | | stream_update(channel); |
| 1206 | | |
| 1207 | | #ifdef NO_ENVX |
| 1208 | | if( 8 == ( SPCRAM[ 0xF2 ] & 0xF ) ) |
| 1209 | | { |
| 1210 | | DSPregs[ SPCRAM[ 0xF2 ] ] = 0; |
| 1211 | | } |
| 1212 | | #endif |
| 1213 | | |
| 1214 | | return DSPregs[offset & 0x7f]; |
| 1215 | | } |
| 1216 | | |
| 1217 | | static WRITE8_HANDLER( snes_dsp_io_w ) |
| 1218 | | { |
| 1219 | | stream_update(channel); |
| 1220 | | |
| 1221 | | if( 0x7C == offset ) |
| 1222 | | { |
| 1223 | | DSP_WRITE_7C(data); |
| 1224 | | } |
| 1225 | | else |
| 1226 | | { |
| 1227 | | DSPregs[ offset ] = data; |
| 1228 | | } |
| 1229 | | } |
| 1230 | | |
| 1231 | | /*************************** |
| 1232 | | * I/O for SPC700 * |
| 1233 | | ***************************/ |
| 1234 | | READ8_HANDLER( spc_io_r ) |
| 1235 | | { |
| 1236 | | switch( offset ) /* Offset is from 0x00f0 */ |
| 1237 | | { |
| 1238 | | case 0x0: //FIXME: Super Bomberman PBW reads from there, is it really write-only? |
| 1239 | | return 0; |
| 1240 | | case 0x1: |
| 1241 | | return 0; //Super Kick Boxing reads port 1 and wants it to be zero. |
| 1242 | | case 0x2: /* Register address */ |
| 1243 | | return spc_ram[0xf2]; |
| 1244 | | case 0x3: /* Register data */ |
| 1245 | | return snes_dsp_io_r( space, spc_ram[0xf2] ); |
| 1246 | | case 0x4: /* Port 0 */ |
| 1247 | | case 0x5: /* Port 1 */ |
| 1248 | | case 0x6: /* Port 2 */ |
| 1249 | | case 0x7: /* Port 3 */ |
| 1250 | | // mame_printf_debug("SPC: rd %02x @ %d, PC=%x\n", spc_port_in[offset-4], offset-4, cpu_get_pc(space->cpu)); |
| 1251 | | return spc_port_in[offset - 4]; |
| 1252 | | case 0x8: //normal RAM, can be read even if the ram disabled flag ($f0 bit 1) is active |
| 1253 | | case 0x9: |
| 1254 | | return spc_ram[0xf0 + offset]; |
| 1255 | | case 0xA: /* Timer 0 */ |
| 1256 | | case 0xB: /* Timer 1 */ |
| 1257 | | case 0xC: /* Timer 2 */ |
| 1258 | | break; |
| 1259 | | case 0xD: /* Counter 0 */ |
| 1260 | | case 0xE: /* Counter 1 */ |
| 1261 | | case 0xF: /* Counter 2 */ |
| 1262 | | { |
| 1263 | | UINT8 value = spc_ram[0xf0 + offset] & 0xf; |
| 1264 | | spc_ram[0xf0 + offset] = 0; |
| 1265 | | return value; |
| 1266 | | } |
| 1267 | | } |
| 1268 | | |
| 1269 | | return 0; |
| 1270 | | } |
| 1271 | | |
| 1272 | | WRITE8_HANDLER( spc_io_w ) |
| 1273 | | { |
| 1274 | | switch( offset ) /* Offset is from 0x00f0 */ |
| 1275 | | { |
| 1276 | | case 0x0: |
| 1277 | | printf("Warning: write to SOUND TEST register with data %02x!\n",data); |
| 1278 | | break; |
| 1279 | | case 0x1: /* Control */ |
| 1280 | | if( data & 0x1 && !timers[0].enabled ) |
| 1281 | | { |
| 1282 | | timers[0].counter = 0; |
| 1283 | | spc_ram[0xfd] = 0; |
| 1284 | | } |
| 1285 | | if( data & 0x2 && !timers[1].enabled ) |
| 1286 | | { |
| 1287 | | timers[1].counter = 0; |
| 1288 | | spc_ram[0xfe] = 0; |
| 1289 | | } |
| 1290 | | if( data & 0x4 && !timers[2].enabled ) |
| 1291 | | { |
| 1292 | | timers[2].counter = 0; |
| 1293 | | spc_ram[0xff] = 0; |
| 1294 | | } |
| 1295 | | timers[0].enabled = data & 0x1; |
| 1296 | | timer_enable( timers[0].timer, timers[0].enabled ); |
| 1297 | | timers[1].enabled = (data & 0x2) >> 1; |
| 1298 | | timer_enable( timers[1].timer, timers[1].enabled ); |
| 1299 | | timers[2].enabled = (data & 0x4) >> 2; |
| 1300 | | timer_enable( timers[2].timer, timers[2].enabled ); |
| 1301 | | if( data & 0x10 ) |
| 1302 | | { |
| 1303 | | spc_port_in[0] = 0; |
| 1304 | | spc_port_in[1] = 0; |
| 1305 | | } |
| 1306 | | if( data & 0x20 ) |
| 1307 | | { |
| 1308 | | spc_port_in[2] = 0; |
| 1309 | | spc_port_in[3] = 0; |
| 1310 | | } |
| 1311 | | |
| 1312 | | if ((data & 0x80) != (spc_ram[0xf1] & 0x80)) |
| 1313 | | { |
| 1314 | | if (data & 0x80) |
| 1315 | | { |
| 1316 | | memcpy(snes_ipl_region, memory_region(space->machine, "user5"), 64); |
| 1317 | | } |
| 1318 | | else |
| 1319 | | { |
| 1320 | | memcpy(snes_ipl_region, &spc_ram[0xffc0], 64); |
| 1321 | | } |
| 1322 | | } |
| 1323 | | break; |
| 1324 | | case 0x2: /* Register address */ |
| 1325 | | break; |
| 1326 | | case 0x3: /* Register data */ |
| 1327 | | snes_dsp_io_w( space, spc_ram[0xf2], data ); |
| 1328 | | break; |
| 1329 | | case 0x4: /* Port 0 */ |
| 1330 | | case 0x5: /* Port 1 */ |
| 1331 | | case 0x6: /* Port 2 */ |
| 1332 | | case 0x7: /* Port 3 */ |
| 1333 | | // mame_printf_debug("SPC: %02x to APU @ %d (PC=%x)\n", data, offset&3, cpu_get_pc(space->cpu)); |
| 1334 | | spc_port_out[offset - 4] = data; |
| 1335 | | cpuexec_boost_interleave(space->machine, attotime_zero, ATTOTIME_IN_USEC(20)); |
| 1336 | | break; |
| 1337 | | case 0xA: /* Timer 0 */ |
| 1338 | | case 0xB: /* Timer 1 */ |
| 1339 | | case 0xC: /* Timer 2 */ |
| 1340 | | if (data == 0) data = 255; |
| 1341 | | break; |
| 1342 | | case 0xD: /* Counter 0 */ |
| 1343 | | case 0xE: /* Counter 1 */ |
| 1344 | | case 0xF: /* Counter 2 */ |
| 1345 | | return; |
| 1346 | | } |
| 1347 | | |
| 1348 | | spc_ram[0xf0 + offset] = data; |
| 1349 | | } |
| 1350 | | |
| 1351 | | READ8_HANDLER( spc_ram_r ) |
| 1352 | | { |
| 1353 | | return spc_ram[offset]; |
| 1354 | | } |
| 1355 | | |
| 1356 | | WRITE8_HANDLER( spc_ram_w ) |
| 1357 | | { |
| 1358 | | spc_ram[offset] = data; |
| 1359 | | |
| 1360 | | // if RAM is mapped in, mirror accordingly |
| 1361 | | if ((!(spc_ram[0xf1] & 0x80)) && (offset >= 0xffc0)) |
| 1362 | | { |
| 1363 | | snes_ipl_region[offset-0xffc0] = data; |
| 1364 | | } |
| 1365 | | } |
| 1366 | | |
| 1367 | | READ8_HANDLER( spc_ipl_r ) |
| 1368 | | { |
| 1369 | | return snes_ipl_region[offset]; |
| 1370 | | } |
| 1371 | | |
src/mame/audio/snes_snd.c
| r0 | r7593 | |
| 1 | /*************************************************************************** |
| 2 | |
| 3 | snes_snd.c |
| 4 | |
| 5 | File to handle the sound emulation of the Nintendo Super NES. |
| 6 | |
| 7 | By R. Belmont, adapted from OpenSPC 0.3.99 by Brad Martin with permission. |
| 8 | Thanks to Brad and also to Charles Bilyu? of SNeESe. |
| 9 | |
| 10 | OpenSPC's license terms (the LGPL) follow: |
| 11 | |
| 12 | --------------------------------------------------------------------------- |
| 13 | |
| 14 | Copyright Brad Martin. |
| 15 | |
| 16 | OpenSPC is free software; you can redistribute it and/or modify |
| 17 | it under the terms of the GNU Lesser General Public License as published by |
| 18 | the Free Software Foundation; either version 2 of the License, or |
| 19 | (at your option) any later version. |
| 20 | |
| 21 | OpenSPC is distributed in the hope that it will be useful, |
| 22 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | GNU Lesser General Public License for more details. |
| 25 | |
| 26 | ***************************************************************************/ |
| 27 | |
| 28 | #include "emu.h" |
| 29 | #include "streams.h" |
| 30 | #include "audio/snes_snd.h" |
| 31 | |
| 32 | /*************************************************************************** |
| 33 | CONSTANTS AND MACROS |
| 34 | ***************************************************************************/ |
| 35 | |
| 36 | static const int gauss[]= |
| 37 | { |
| 38 | 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 39 | 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 40 | 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, |
| 41 | 0x001, 0x001, 0x001, 0x002, 0x002, 0x002, 0x002, 0x002, |
| 42 | 0x002, 0x002, 0x003, 0x003, 0x003, 0x003, 0x003, 0x004, |
| 43 | 0x004, 0x004, 0x004, 0x004, 0x005, 0x005, 0x005, 0x005, |
| 44 | 0x006, 0x006, 0x006, 0x006, 0x007, 0x007, 0x007, 0x008, |
| 45 | 0x008, 0x008, 0x009, 0x009, 0x009, 0x00A, 0x00A, 0x00A, |
| 46 | 0x00B, 0x00B, 0x00B, 0x00C, 0x00C, 0x00D, 0x00D, 0x00E, |
| 47 | 0x00E, 0x00F, 0x00F, 0x00F, 0x010, 0x010, 0x011, 0x011, |
| 48 | 0x012, 0x013, 0x013, 0x014, 0x014, 0x015, 0x015, 0x016, |
| 49 | 0x017, 0x017, 0x018, 0x018, 0x019, 0x01A, 0x01B, 0x01B, |
| 50 | 0x01C, 0x01D, 0x01D, 0x01E, 0x01F, 0x020, 0x020, 0x021, |
| 51 | 0x022, 0x023, 0x024, 0x024, 0x025, 0x026, 0x027, 0x028, |
| 52 | 0x029, 0x02A, 0x02B, 0x02C, 0x02D, 0x02E, 0x02F, 0x030, |
| 53 | 0x031, 0x032, 0x033, 0x034, 0x035, 0x036, 0x037, 0x038, |
| 54 | 0x03A, 0x03B, 0x03C, 0x03D, 0x03E, 0x040, 0x041, 0x042, |
| 55 | 0x043, 0x045, 0x046, 0x047, 0x049, 0x04A, 0x04C, 0x04D, |
| 56 | 0x04E, 0x050, 0x051, 0x053, 0x054, 0x056, 0x057, 0x059, |
| 57 | 0x05A, 0x05C, 0x05E, 0x05F, 0x061, 0x063, 0x064, 0x066, |
| 58 | 0x068, 0x06A, 0x06B, 0x06D, 0x06F, 0x071, 0x073, 0x075, |
| 59 | 0x076, 0x078, 0x07A, 0x07C, 0x07E, 0x080, 0x082, 0x084, |
| 60 | 0x086, 0x089, 0x08B, 0x08D, 0x08F, 0x091, 0x093, 0x096, |
| 61 | 0x098, 0x09A, 0x09C, 0x09F, 0x0A1, 0x0A3, 0x0A6, 0x0A8, |
| 62 | 0x0AB, 0x0AD, 0x0AF, 0x0B2, 0x0B4, 0x0B7, 0x0BA, 0x0BC, |
| 63 | 0x0BF, 0x0C1, 0x0C4, 0x0C7, 0x0C9, 0x0CC, 0x0CF, 0x0D2, |
| 64 | 0x0D4, 0x0D7, 0x0DA, 0x0DD, 0x0E0, 0x0E3, 0x0E6, 0x0E9, |
| 65 | 0x0EC, 0x0EF, 0x0F2, 0x0F5, 0x0F8, 0x0FB, 0x0FE, 0x101, |
| 66 | 0x104, 0x107, 0x10B, 0x10E, 0x111, 0x114, 0x118, 0x11B, |
| 67 | 0x11E, 0x122, 0x125, 0x129, 0x12C, 0x130, 0x133, 0x137, |
| 68 | 0x13A, 0x13E, 0x141, 0x145, 0x148, 0x14C, 0x150, 0x153, |
| 69 | 0x157, 0x15B, 0x15F, 0x162, 0x166, 0x16A, 0x16E, 0x172, |
| 70 | 0x176, 0x17A, 0x17D, 0x181, 0x185, 0x189, 0x18D, 0x191, |
| 71 | 0x195, 0x19A, 0x19E, 0x1A2, 0x1A6, 0x1AA, 0x1AE, 0x1B2, |
| 72 | 0x1B7, 0x1BB, 0x1BF, 0x1C3, 0x1C8, 0x1CC, 0x1D0, 0x1D5, |
| 73 | 0x1D9, 0x1DD, 0x1E2, 0x1E6, 0x1EB, 0x1EF, 0x1F3, 0x1F8, |
| 74 | 0x1FC, 0x201, 0x205, 0x20A, 0x20F, 0x213, 0x218, 0x21C, |
| 75 | 0x221, 0x226, 0x22A, 0x22F, 0x233, 0x238, 0x23D, 0x241, |
| 76 | 0x246, 0x24B, 0x250, 0x254, 0x259, 0x25E, 0x263, 0x267, |
| 77 | 0x26C, 0x271, 0x276, 0x27B, 0x280, 0x284, 0x289, 0x28E, |
| 78 | 0x293, 0x298, 0x29D, 0x2A2, 0x2A6, 0x2AB, 0x2B0, 0x2B5, |
| 79 | 0x2BA, 0x2BF, 0x2C4, 0x2C9, 0x2CE, 0x2D3, 0x2D8, 0x2DC, |
| 80 | 0x2E1, 0x2E6, 0x2EB, 0x2F0, 0x2F5, 0x2FA, 0x2FF, 0x304, |
| 81 | 0x309, 0x30E, 0x313, 0x318, 0x31D, 0x322, 0x326, 0x32B, |
| 82 | 0x330, 0x335, 0x33A, 0x33F, 0x344, 0x349, 0x34E, 0x353, |
| 83 | 0x357, 0x35C, 0x361, 0x366, 0x36B, 0x370, 0x374, 0x379, |
| 84 | 0x37E, 0x383, 0x388, 0x38C, 0x391, 0x396, 0x39B, 0x39F, |
| 85 | 0x3A4, 0x3A9, 0x3AD, 0x3B2, 0x3B7, 0x3BB, 0x3C0, 0x3C5, |
| 86 | 0x3C9, 0x3CE, 0x3D2, 0x3D7, 0x3DC, 0x3E0, 0x3E5, 0x3E9, |
| 87 | 0x3ED, 0x3F2, 0x3F6, 0x3FB, 0x3FF, 0x403, 0x408, 0x40C, |
| 88 | 0x410, 0x415, 0x419, 0x41D, 0x421, 0x425, 0x42A, 0x42E, |
| 89 | 0x432, 0x436, 0x43A, 0x43E, 0x442, 0x446, 0x44A, 0x44E, |
| 90 | 0x452, 0x455, 0x459, 0x45D, 0x461, 0x465, 0x468, 0x46C, |
| 91 | 0x470, 0x473, 0x477, 0x47A, 0x47E, 0x481, 0x485, 0x488, |
| 92 | 0x48C, 0x48F, 0x492, 0x496, 0x499, 0x49C, 0x49F, 0x4A2, |
| 93 | 0x4A6, 0x4A9, 0x4AC, 0x4AF, 0x4B2, 0x4B5, 0x4B7, 0x4BA, |
| 94 | 0x4BD, 0x4C0, 0x4C3, 0x4C5, 0x4C8, 0x4CB, 0x4CD, 0x4D0, |
| 95 | 0x4D2, 0x4D5, 0x4D7, 0x4D9, 0x4DC, 0x4DE, 0x4E0, 0x4E3, |
| 96 | 0x4E5, 0x4E7, 0x4E9, 0x4EB, 0x4ED, 0x4EF, 0x4F1, 0x4F3, |
| 97 | 0x4F5, 0x4F6, 0x4F8, 0x4FA, 0x4FB, 0x4FD, 0x4FF, 0x500, |
| 98 | 0x502, 0x503, 0x504, 0x506, 0x507, 0x508, 0x50A, 0x50B, |
| 99 | 0x50C, 0x50D, 0x50E, 0x50F, 0x510, 0x511, 0x511, 0x512, |
| 100 | 0x513, 0x514, 0x514, 0x515, 0x516, 0x516, 0x517, 0x517, |
| 101 | 0x517, 0x518, 0x518, 0x518, 0x518, 0x518, 0x519, 0x519 |
| 102 | }; |
| 103 | |
| 104 | #undef DEBUG |
| 105 | #undef DBG_KEY |
| 106 | #undef DBG_ENV |
| 107 | #undef DBG_PMOD |
| 108 | #undef DBG_BRR |
| 109 | #undef DBG_ECHO |
| 110 | #undef DBG_INTRP |
| 111 | |
| 112 | #undef NO_PMOD |
| 113 | #undef NO_ECHO |
| 114 | |
| 115 | #define CPU_RATE 1024000 |
| 116 | #define SAMP_FREQ 32000 |
| 117 | |
| 118 | |
| 119 | /* Original SPC DSP took samples 32000 times a second, which is once every (1024000/32000 = 32) cycles. */ |
| 120 | #ifdef UNUSED_DEFINITION |
| 121 | static const int TS_CYC = CPU_RATE / SAMP_FREQ; |
| 122 | #endif |
| 123 | |
| 124 | /* Ptrs to Gaussian table */ |
| 125 | static const int *const G1 = &gauss[256]; |
| 126 | static const int *const G2 = &gauss[512]; |
| 127 | static const int *const G3 = &gauss[255]; |
| 128 | static const int *const G4 = &gauss[-1]; |
| 129 | |
| 130 | static const int mask = 0xFF; |
| 131 | |
| 132 | /* This table is for envelope timing. It represents the number of counts |
| 133 | that should be subtracted from the counter each sample period (32kHz). |
| 134 | The counter starts at 30720 (0x7800). */ |
| 135 | static const int CNT_INIT = 0x7800; |
| 136 | static const int ENVCNT[0x20] |
| 137 | = { |
| 138 | 0x0000, 0x000F, 0x0014, 0x0018, 0x001E, 0x0028, 0x0030, 0x003C, |
| 139 | 0x0050, 0x0060, 0x0078, 0x00A0, 0x00C0, 0x00F0, 0x0140, 0x0180, |
| 140 | 0x01E0, 0x0280, 0x0300, 0x03C0, 0x0500, 0x0600, 0x0780, 0x0A00, |
| 141 | 0x0C00, 0x0F00, 0x1400, 0x1800, 0x1E00, 0x2800, 0x3C00, 0x7800 |
| 142 | }; |
| 143 | |
| 144 | |
| 145 | /* Make reading the ADSR code easier */ |
| 146 | #define SL( v ) (spc700->dsp_regs[((v) << 4) + 6] >> 5) /* Returns SUSTAIN level */ |
| 147 | #define SR( v ) (spc700->dsp_regs[((v) << 4) + 6] & 0x1f) /* Returns SUSTAIN rate */ |
| 148 | |
| 149 | /* Handle endianness */ |
| 150 | #define LEtoME16( x ) LITTLE_ENDIANIZE_INT16(x) |
| 151 | #define MEtoLE16( x ) LITTLE_ENDIANIZE_INT16(x) |
| 152 | |
| 153 | |
| 154 | static int advance_envelope( running_device *device, int v); |
| 155 | |
| 156 | /*************************************************************************** |
| 157 | TYPE DEFINITIONS |
| 158 | ***************************************************************************/ |
| 159 | |
| 160 | typedef enum /* ADSR state type */ |
| 161 | { |
| 162 | ATTACK, |
| 163 | DECAY, |
| 164 | SUSTAIN, |
| 165 | RELEASE |
| 166 | } env_state_t32; |
| 167 | |
| 168 | typedef struct /* Voice state type */ |
| 169 | { |
| 170 | UINT16 mem_ptr; /* Sample data memory pointer */ |
| 171 | int end; /* End or loop after block */ |
| 172 | int envcnt; /* Counts to envelope update */ |
| 173 | env_state_t32 envstate; /* Current envelope state */ |
| 174 | int envx; /* Last env height (0-0x7FFF) */ |
| 175 | int filter; /* Last header's filter */ |
| 176 | int half; /* Active nybble of BRR */ |
| 177 | int header_cnt; /* Bytes before new header (0-8)*/ |
| 178 | int mixfrac; /* Fractional part of smpl pstn */ |
| 179 | int on_cnt; /* Is it time to turn on yet? */ |
| 180 | int pitch; /* Sample pitch (4096->32000Hz) */ |
| 181 | int range; /* Last header's range */ |
| 182 | UINT32 samp_id; /* Sample ID# */ |
| 183 | int sampptr; /* Where in sampbuf we are */ |
| 184 | signed long smp1; /* Last sample (for BRR filter) */ |
| 185 | signed long smp2; /* Second-to-last sample decoded*/ |
| 186 | short sampbuf[4]; /* Buffer for Gaussian interp */ |
| 187 | } voice_state_type; |
| 188 | |
| 189 | typedef struct /* Source directory entry */ |
| 190 | { |
| 191 | UINT16 vptr; /* Ptr to start of sample data */ |
| 192 | UINT16 lptr; /* Loop pointer in sample data */ |
| 193 | } src_dir_type; |
| 194 | |
| 195 | |
| 196 | typedef struct _snes_sound_state snes_sound_state; |
| 197 | struct _snes_sound_state |
| 198 | { |
| 199 | UINT8 *ram; |
| 200 | sound_stream *channel; |
| 201 | UINT8 dsp_regs[256]; /* DSP registers */ |
| 202 | UINT8 ipl_region[64]; /* SPC top 64 bytes */ |
| 203 | |
| 204 | int keyed_on; |
| 205 | int keys; /* 8-bits for 8 voices */ |
| 206 | voice_state_type voice_state[8]; |
| 207 | |
| 208 | /* Noise stuff */ |
| 209 | int noise_cnt; |
| 210 | int noise_lev; |
| 211 | |
| 212 | /* These are for the FIR echo filter */ |
| 213 | #ifndef NO_ECHO |
| 214 | short fir_lbuf[8]; |
| 215 | short fir_rbuf[8]; |
| 216 | int fir_ptr; |
| 217 | int echo_ptr; |
| 218 | #endif |
| 219 | |
| 220 | /* timers */ |
| 221 | emu_timer *timer[3]; |
| 222 | UINT8 enabled[3]; |
| 223 | UINT16 counter[3]; |
| 224 | |
| 225 | /* IO ports */ |
| 226 | UINT8 port_in[4]; /* SPC input ports */ |
| 227 | UINT8 port_out[4]; /* SPC output ports */ |
| 228 | }; |
| 229 | |
| 230 | /***************************************************************************** |
| 231 | INLINE FUNCTIONS |
| 232 | *****************************************************************************/ |
| 233 | |
| 234 | INLINE snes_sound_state *get_safe_token( running_device *device ) |
| 235 | { |
| 236 | assert(device != NULL); |
| 237 | assert(device->token != NULL); |
| 238 | assert(device->type == SNES_SPC); |
| 239 | |
| 240 | return (snes_sound_state *)device->token; |
| 241 | } |
| 242 | |
| 243 | /***************************************************************************** |
| 244 | IMPLEMENTATION |
| 245 | *****************************************************************************/ |
| 246 | |
| 247 | |
| 248 | /*------------------------------------------------- |
| 249 | dsp_reset |
| 250 | |
| 251 | Reset emulated DSP |
| 252 | -------------------------------------------------*/ |
| 253 | |
| 254 | static void dsp_reset( running_device *device ) |
| 255 | { |
| 256 | snes_sound_state *spc700 = get_safe_token(device); |
| 257 | int i; |
| 258 | |
| 259 | #ifdef DEBUG |
| 260 | logerror("dsp_reset\n"); |
| 261 | #endif |
| 262 | |
| 263 | for (i = 0; i < 8; i++) |
| 264 | { |
| 265 | #ifndef NO_ECHO |
| 266 | spc700->fir_lbuf[i] = 0; |
| 267 | spc700->fir_rbuf[i] = 0; |
| 268 | #endif |
| 269 | spc700->voice_state[i].on_cnt = 0; |
| 270 | } |
| 271 | |
| 272 | #ifndef NO_ECHO |
| 273 | spc700->fir_ptr = 0; |
| 274 | spc700->echo_ptr = 0; |
| 275 | #endif |
| 276 | |
| 277 | spc700->keys = 0; |
| 278 | spc700->keyed_on = 0; |
| 279 | spc700->noise_cnt = 0; |
| 280 | spc700->noise_lev = 0x4000; |
| 281 | spc700->dsp_regs[0x6c] |= 0xe0; |
| 282 | spc700->dsp_regs[0x4c] = 0; |
| 283 | spc700->dsp_regs[0x5c] = 0; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | /*------------------------------------------------- |
| 288 | dsp_update |
| 289 | |
| 290 | Mix one sample of audio. sound_ptr is a pointer |
| 291 | to mix audio into |
| 292 | -------------------------------------------------*/ |
| 293 | |
| 294 | static void dsp_update( running_device *device, short *sound_ptr ) |
| 295 | { |
| 296 | snes_sound_state *spc700 = get_safe_token(device); |
| 297 | int V; |
| 298 | |
| 299 | #ifndef NO_ECHO |
| 300 | int echo_base; |
| 301 | int echol; |
| 302 | int echor; |
| 303 | #endif |
| 304 | |
| 305 | int envx; |
| 306 | int m; |
| 307 | int outl; |
| 308 | int outr; |
| 309 | signed long outx; /* Smpl height (must be signed) */ |
| 310 | src_dir_type * sd; |
| 311 | int v; |
| 312 | int vl; |
| 313 | voice_state_type * vp; |
| 314 | int vr; |
| 315 | |
| 316 | sd = (src_dir_type *) &spc700->ram[(int) spc700->dsp_regs[0x5d] << 8]; |
| 317 | |
| 318 | /* Check for reset */ |
| 319 | if (spc700->dsp_regs[0x6c] & 0x80) |
| 320 | dsp_reset(device); |
| 321 | |
| 322 | /* Here we check for keys on/off. Docs say that successive writes to KON/KOF |
| 323 | must be separated by at least 2 Ts periods or risk being neglected. |
| 324 | Therefore DSP only looks at these during an update, and not at the time of |
| 325 | the write. Only need to do this once however, since the regs haven't |
| 326 | changed over the whole period we need to catch up with. */ |
| 327 | #ifdef DBG_KEY |
| 328 | spc700->dsp_regs[0x4c] &= mask; |
| 329 | #endif |
| 330 | |
| 331 | /* Keying on a voice resets that bit in ENDX */ |
| 332 | spc700->dsp_regs[0x7c] &= ~spc700->dsp_regs[0x4c]; |
| 333 | |
| 334 | /* Question: what is the expected behavior when pitch modulation is enabled on |
| 335 | voice 0? Jurassic Park 2 does this. For now, using outx of zero for first |
| 336 | voice. */ |
| 337 | outx = 0; |
| 338 | |
| 339 | /* Same table for noise and envelope */ |
| 340 | spc700->noise_cnt -= ENVCNT[spc700->dsp_regs[0x6c] & 0x1f]; |
| 341 | if (spc700->noise_cnt <= 0) |
| 342 | { |
| 343 | spc700->noise_cnt = CNT_INIT; |
| 344 | spc700->noise_lev = (((spc700->noise_lev << 13) ^ (spc700->noise_lev << 14)) & 0x4000) | (spc700->noise_lev >> 1); |
| 345 | } |
| 346 | |
| 347 | outl = 0; |
| 348 | outr = 0; |
| 349 | |
| 350 | #ifndef NO_ECHO |
| 351 | echol = 0; |
| 352 | echor = 0; |
| 353 | #endif |
| 354 | |
| 355 | for (v = 0, m = 1, V = 0; v < 8; v++, V += 16, m <<= 1) |
| 356 | { |
| 357 | vp = &spc700->voice_state[v]; |
| 358 | |
| 359 | if (vp->on_cnt && (--vp->on_cnt == 0)) |
| 360 | { |
| 361 | /* Voice was keyed on */ |
| 362 | spc700->keys |= m; |
| 363 | spc700->keyed_on |= m; |
| 364 | vl = spc700->dsp_regs[(v << 4) + 4]; |
| 365 | vp->samp_id = *( UINT32 * )&sd[vl]; |
| 366 | vp->mem_ptr = LEtoME16(sd[vl].vptr); |
| 367 | |
| 368 | #ifdef DBG_KEY |
| 369 | logerror("Keying on voice %d, samp=0x%04X (0x%02X)\n", v, vp->mem_ptr, vl); |
| 370 | #endif |
| 371 | |
| 372 | vp->header_cnt = 0; |
| 373 | vp->half = 0; |
| 374 | vp->envx = 0; |
| 375 | vp->end = 0; |
| 376 | vp->sampptr = 0; |
| 377 | vp->mixfrac = 3 * 4096; |
| 378 | |
| 379 | /* NOTE: Real SNES does *not* appear to initialize the envelope |
| 380 | counter to anything in particular. The first cycle always seems to |
| 381 | come at a random time sooner than expected; as yet, I have been |
| 382 | unable to find any pattern. I doubt it will matter though, so |
| 383 | we'll go ahead and do the full time for now. */ |
| 384 | vp->envcnt = CNT_INIT; |
| 385 | vp->envstate = ATTACK; |
| 386 | } |
| 387 | |
| 388 | if (spc700->dsp_regs[0x4c] & m & ~spc700->dsp_regs[0x5c]) |
| 389 | { |
| 390 | /* Voice doesn't come on if key off is set */ |
| 391 | spc700->dsp_regs[0x4c] &= ~m; |
| 392 | vp->on_cnt = 8; |
| 393 | |
| 394 | #ifdef DBG_KEY |
| 395 | logerror("Key on set for voice %d\n", v); |
| 396 | #endif |
| 397 | } |
| 398 | |
| 399 | if (spc700->keys & spc700->dsp_regs[0x5c] & m) |
| 400 | { |
| 401 | /* Voice was keyed off */ |
| 402 | vp->envstate = RELEASE; |
| 403 | vp->on_cnt = 0; |
| 404 | |
| 405 | #ifdef DBG_KEY |
| 406 | logerror("Keying off voice %d\n", v); |
| 407 | #endif |
| 408 | } |
| 409 | |
| 410 | if (!(spc700->keys & m & mask) || ((envx = advance_envelope(device, v)) < 0)) |
| 411 | { |
| 412 | spc700->dsp_regs[V + 8] = 0; |
| 413 | spc700->dsp_regs[V + 9] = 0; |
| 414 | outx = 0; |
| 415 | continue; |
| 416 | } |
| 417 | |
| 418 | vp->pitch = LEtoME16(*((UINT16 *)&spc700->dsp_regs[V + 2])) & 0x3fff; |
| 419 | |
| 420 | #ifndef NO_PMOD |
| 421 | /* Pitch mod uses OUTX from last voice for this one. Luckily we haven't |
| 422 | modified OUTX since it was used for last voice. */ |
| 423 | if (spc700->dsp_regs[0x2d] & m) |
| 424 | { |
| 425 | #ifdef DBG_PMOD |
| 426 | logerror("Pitch Modulating voice %d, outx=%ld, old pitch=%d, ", v, outx, vp->pitch); |
| 427 | #endif |
| 428 | vp->pitch = (vp->pitch * (outx + 32768)) >> 15; |
| 429 | } |
| 430 | #endif |
| 431 | |
| 432 | #ifdef DBG_PMOD |
| 433 | logerror("pitch=%d\n", vp->pitch); |
| 434 | #endif |
| 435 | |
| 436 | for ( ; vp->mixfrac >= 0; vp->mixfrac -= 4096) |
| 437 | { |
| 438 | /* This part performs the BRR decode 'on-the-fly'. This is more |
| 439 | correct than the old way, which could be fooled if the data and/or |
| 440 | the loop point changed while the sample was playing, or if the BRR |
| 441 | decode didn't produce the same result every loop because of the |
| 442 | filters. The event interface still has no chance of keeping up |
| 443 | with those kinds of tricks, though. */ |
| 444 | if (!vp->header_cnt) |
| 445 | { |
| 446 | if (vp->end & 1) |
| 447 | { |
| 448 | /* Docs say ENDX bit is set when decode of block with source |
| 449 | end flag set is done. Does this apply to looping samples? |
| 450 | Some info I've seen suggests yes. */ |
| 451 | spc700->dsp_regs[0x7c] |= m; |
| 452 | if (vp->end & 2) |
| 453 | { |
| 454 | vp->mem_ptr = LEtoME16(sd[spc700->dsp_regs[V + 4]].lptr); |
| 455 | |
| 456 | #ifdef DBG_BRR |
| 457 | logerror("BRR looping to 0x%04X\n", vp->mem_ptr); |
| 458 | #endif |
| 459 | } |
| 460 | else |
| 461 | { |
| 462 | #ifdef DBG_KEY |
| 463 | logerror("BRR decode end, voice %d\n", v); |
| 464 | #endif |
| 465 | |
| 466 | spc700->keys &= ~m; |
| 467 | spc700->dsp_regs[V + 8] = 0; |
| 468 | vp->envx = 0; |
| 469 | while (vp->mixfrac >= 0) |
| 470 | { |
| 471 | vp->sampbuf[vp->sampptr] = 0; |
| 472 | outx = 0; |
| 473 | vp->sampptr = (vp->sampptr + 1) & 3; |
| 474 | vp->mixfrac -= 4096; |
| 475 | } |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | vp->header_cnt = 8; |
| 481 | vl = (UINT8)spc700->ram[vp->mem_ptr++]; |
| 482 | vp->range = vl >> 4; |
| 483 | vp->end = vl & 3; |
| 484 | vp->filter = (vl & 12) >> 2; |
| 485 | |
| 486 | #ifdef DBG_BRR |
| 487 | logerror("V%d: header read, range=%d, end=%d, filter=%d\n", v, vp->range, vp->end, vp->filter); |
| 488 | #endif |
| 489 | } |
| 490 | |
| 491 | if (vp->half == 0) |
| 492 | { |
| 493 | vp->half = 1; |
| 494 | outx = ((signed char)spc700->ram[vp->mem_ptr]) >> 4; |
| 495 | } |
| 496 | else |
| 497 | { |
| 498 | vp->half = 0; |
| 499 | /* Funkiness to get 4-bit signed to carry through */ |
| 500 | outx = (signed char)(spc700->ram[vp->mem_ptr++] << 4); |
| 501 | outx >>= 4; |
| 502 | vp->header_cnt--; |
| 503 | } |
| 504 | |
| 505 | #ifdef DBG_BRR |
| 506 | logerror("V%d: nybble=%X, ptr=%04X, smp1=%d, smp2=%d\n", v, outx & 0x0f, vp->mem_ptr, vp->smp1, vp->smp2); |
| 507 | #endif |
| 508 | |
| 509 | /* For invalid ranges (D,E,F): if the nybble is negative, the result |
| 510 | is F000. If positive, 0000. Nothing else like previous range, |
| 511 | etc. seems to have any effect. If range is valid, do the shift |
| 512 | normally. Note these are both shifted right once to do the filters |
| 513 | properly, but the output will be shifted back again at the end. */ |
| 514 | if (vp->range <= 0xc) |
| 515 | { |
| 516 | outx = (outx << vp->range) >> 1; |
| 517 | } |
| 518 | else |
| 519 | { |
| 520 | outx &= ~0x7ff; |
| 521 | |
| 522 | #ifdef DBG_BRR |
| 523 | logerror("V%d: invalid range! (%X)\n", v, vp->range); |
| 524 | #endif |
| 525 | } |
| 526 | |
| 527 | #ifdef DBG_BRR |
| 528 | logerror("V%d: shifted delta=%04X\n", v, (UINT16)outx); |
| 529 | #endif |
| 530 | |
| 531 | switch (vp->filter) |
| 532 | { |
| 533 | case 0: |
| 534 | break; |
| 535 | |
| 536 | case 1: |
| 537 | outx += (vp->smp1 >> 1) + ((-vp->smp1) >> 5); |
| 538 | break; |
| 539 | |
| 540 | case 2: |
| 541 | outx += vp->smp1 + ((-(vp->smp1 + (vp->smp1 >> 1))) >> 5) - (vp->smp2 >> 1) + (vp->smp2 >> 5); |
| 542 | break; |
| 543 | |
| 544 | case 3: |
| 545 | outx += vp->smp1 + ((-(vp->smp1 + (vp->smp1 << 2) + (vp->smp1 << 3))) >> 7) |
| 546 | - (vp->smp2 >> 1) + ((vp->smp2 + (vp->smp2 >> 1)) >> 4); |
| 547 | break; |
| 548 | } |
| 549 | |
| 550 | if (outx < (signed short)0x8000) |
| 551 | { |
| 552 | outx = (signed short)0x8000; |
| 553 | } |
| 554 | else if (outx > (signed short)0x7fff) |
| 555 | { |
| 556 | outx = (signed short)0x7fff; |
| 557 | } |
| 558 | |
| 559 | #ifdef DBG_BRR |
| 560 | logerror("V%d: filter + delta=%04X\n", v, (UINT16)outx); |
| 561 | #endif |
| 562 | |
| 563 | vp->smp2 = (signed short)vp->smp1; |
| 564 | vp->smp1 = (signed short)(outx << 1); |
| 565 | vp->sampbuf[vp->sampptr] = vp->smp1; |
| 566 | |
| 567 | #ifdef DBG_BRR |
| 568 | logerror("V%d: final output: %04X\n", v, vp->sampbuf[vp->sampptr]); |
| 569 | #endif |
| 570 | |
| 571 | vp->sampptr = (vp->sampptr + 1) & 3; |
| 572 | } |
| 573 | |
| 574 | if (spc700->dsp_regs[0x3d] & m) |
| 575 | { |
| 576 | #ifdef DBG_PMOD |
| 577 | logerror("Noise enabled, voice %d\n", v); |
| 578 | #endif |
| 579 | outx = (signed short)(spc700->noise_lev << 1); |
| 580 | } |
| 581 | else |
| 582 | { |
| 583 | /* Perform 4-Point Gaussian interpolation. Take an approximation of a |
| 584 | Gaussian bell-curve, and move it through the sample data at a rate |
| 585 | determined by the pitch. The sample output at any given time is |
| 586 | the sum of the products of each input sample point with the value |
| 587 | of the bell-curve corresponding to that point. */ |
| 588 | vl = vp->mixfrac >> 4; |
| 589 | vr = ((G4[-vl] * vp->sampbuf[vp->sampptr]) >> 11 ) & ~1; |
| 590 | vr += ((G3[-vl] * vp->sampbuf[(vp->sampptr + 1) & 3]) >> 11) & ~1; |
| 591 | vr += ((G2[vl] * vp->sampbuf[(vp->sampptr + 2) & 3]) >> 11 ) & ~1; |
| 592 | |
| 593 | /* This is to do the wrapping properly. Based on my tests with the |
| 594 | SNES, it appears clipping is done only if it is the fourth addition |
| 595 | that would cause a wrap. If it has already wrapped before the |
| 596 | fourth addition, it is not clipped. */ |
| 597 | vr = (signed short)vr; |
| 598 | vr += ((G1[vl] * vp->sampbuf[(vp->sampptr + 3) & 3]) >> 11) & ~1; |
| 599 | |
| 600 | if (vr > 32767) |
| 601 | vr = 32767; |
| 602 | else if (vr < -32768) |
| 603 | vr = -32768; |
| 604 | |
| 605 | outx = (signed short)vr; |
| 606 | |
| 607 | #ifdef DBG_INTRP |
| 608 | logerror("V%d: mixfrac=%d: [%d]*%d + [%d]*%d + [%d]*%d + [%d]*%d = %d\n", v, vl, |
| 609 | G1[vl], |
| 610 | vp->sampbuf[(vp->sampptr + 3) & 3], |
| 611 | G2[vl], |
| 612 | vp->sampbuf[(vp->sampptr + 2) & 3], |
| 613 | G3[-vl], |
| 614 | vp->sampbuf[(vp->sampptr + 1) & 3], |
| 615 | G4[-vl], |
| 616 | vp->sampbuf[vp->sampptr], |
| 617 | outx); |
| 618 | #endif |
| 619 | } |
| 620 | |
| 621 | /* Advance the sample position for next update. */ |
| 622 | vp->mixfrac += vp->pitch; |
| 623 | |
| 624 | outx = ((outx * envx) >> 11) & ~1; |
| 625 | spc700->dsp_regs[V + 9] = outx >> 8; |
| 626 | |
| 627 | vl = (((int)(signed char)spc700->dsp_regs[V ]) * outx) >> 7; |
| 628 | vr = (((int)(signed char)spc700->dsp_regs[V + 1]) * outx) >> 7; |
| 629 | outl += vl; |
| 630 | outr += vr; |
| 631 | |
| 632 | if (spc700->dsp_regs[0x4d] & m) |
| 633 | { |
| 634 | #ifndef NO_ECHO |
| 635 | echol += vl; |
| 636 | echor += vr; |
| 637 | #endif |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | outl = (outl * (signed char)spc700->dsp_regs[0x0c]) >> 7; |
| 642 | outr = (outr * (signed char)spc700->dsp_regs[0x1c]) >> 7; |
| 643 | |
| 644 | #ifndef NO_ECHO |
| 645 | /* Perform echo. First, read mem at current location, and put those samples |
| 646 | into the FIR filter queue. */ |
| 647 | #ifdef DBG_ECHO |
| 648 | logerror("Echo delay=%dms, feedback=%d%%\n", spc700->dsp_regs[0x7d] * 16, |
| 649 | ((signed char)spc700->dsp_regs[0x0d] * 100) / 0x7f); |
| 650 | #endif |
| 651 | |
| 652 | echo_base = ((spc700->dsp_regs[0x6d] << 8) + spc700->echo_ptr) & 0xffff; |
| 653 | spc700->fir_lbuf[spc700->fir_ptr] = (signed short)LEtoME16(*(UINT16 *)&spc700->ram[echo_base]); |
| 654 | spc700->fir_rbuf[spc700->fir_ptr] = (signed short)LEtoME16(*(UINT16 *)&spc700->ram[echo_base + sizeof(short)]); |
| 655 | |
| 656 | /* Now, evaluate the FIR filter, and add the results into the final output. */ |
| 657 | vl = spc700->fir_lbuf[spc700->fir_ptr] * (signed char)spc700->dsp_regs[0x7f]; |
| 658 | vr = spc700->fir_rbuf[spc700->fir_ptr] * (signed char)spc700->dsp_regs[0x7f]; |
| 659 | spc700->fir_ptr = (spc700->fir_ptr + 1) & 7; |
| 660 | vl += spc700->fir_lbuf[spc700->fir_ptr] * (signed char)spc700->dsp_regs[0x6f]; |
| 661 | vr += spc700->fir_rbuf[spc700->fir_ptr] * (signed char)spc700->dsp_regs[0x6f]; |
| 662 | spc700->fir_ptr = (spc700->fir_ptr + 1) & 7; |
| 663 | vl += spc700->fir_lbuf[spc700->fir_ptr] * (signed char)spc700->dsp_regs[0x5f]; |
| 664 | vr += spc700->fir_rbuf[spc700->fir_ptr] * (signed char)spc700->dsp_regs[0x5f]; |
| 665 | spc700->fir_ptr = (spc700->fir_ptr + 1) & 7; |
| 666 | vl += spc700->fir_lbuf[spc700->fir_ptr] * (signed char)spc700->dsp_regs[0x4f]; |
| 667 | vr += spc700->fir_rbuf[spc700->fir_ptr] * (signed char)spc700->dsp_regs[0x4f]; |
| 668 | spc700->fir_ptr = (spc700->fir_ptr + 1) & 7; |
| 669 | vl += spc700->fir_lbuf[spc700->fir_ptr] * (signed char)spc700->dsp_regs[0x3f]; |
| 670 | vr += spc700->fir_rbuf[spc700->fir_ptr] * (signed char)spc700->dsp_regs[0x3f]; |
| 671 | spc700->fir_ptr = (spc700->fir_ptr + 1) & 7; |
| 672 | vl += spc700->fir_lbuf[spc700->fir_ptr] * (signed char)spc700->dsp_regs[0x2f]; |
| 673 | vr += spc700->fir_rbuf[spc700->fir_ptr] * (signed char)spc700->dsp_regs[0x2f]; |
| 674 | spc700->fir_ptr = (spc700->fir_ptr + 1) & 7; |
| 675 | vl += spc700->fir_lbuf[spc700->fir_ptr] * (signed char)spc700->dsp_regs[0x1f]; |
| 676 | vr += spc700->fir_rbuf[spc700->fir_ptr] * (signed char)spc700->dsp_regs[0x1f]; |
| 677 | spc700->fir_ptr = (spc700->fir_ptr + 1) & 7; |
| 678 | vl += spc700->fir_lbuf[spc700->fir_ptr] * (signed char)spc700->dsp_regs[0x0f]; |
| 679 | vr += spc700->fir_rbuf[spc700->fir_ptr] * (signed char)spc700->dsp_regs[0x0f]; |
| 680 | |
| 681 | #ifdef DBG_ECHO |
| 682 | logerror("FIR Coefficients: %02X %02X %02X %02X %02X %02X %02X %02X\n", |
| 683 | spc700->dsp_regs[0x0f], |
| 684 | spc700->dsp_regs[0x1f], |
| 685 | spc700->dsp_regs[0x2f], |
| 686 | spc700->dsp_regs[0x3f], |
| 687 | spc700->dsp_regs[0x4f], |
| 688 | spc700->dsp_regs[0x5f], |
| 689 | spc700->dsp_regs[0x6f], |
| 690 | spc700->dsp_regs[0x7f]); |
| 691 | #endif |
| 692 | |
| 693 | /* FIR_ptr is left in the position of the oldest sample, the one that will be replaced next update. */ |
| 694 | outl += vl * (signed char)spc700->dsp_regs[0x2c] >> 14; |
| 695 | outr += vr * (signed char)spc700->dsp_regs[0x3c] >> 14; |
| 696 | |
| 697 | if (!(spc700->dsp_regs[0x6c] & 0x20)) |
| 698 | { |
| 699 | /* Add the echo feedback back into the original result, and save that into memory for use later. */ |
| 700 | echol += vl * (signed char)spc700->dsp_regs[0x0d] >> 14; |
| 701 | |
| 702 | if (echol > 32767) |
| 703 | echol = 32767; |
| 704 | else if (echol < -32768) |
| 705 | echol = -32768; |
| 706 | |
| 707 | echor += vr * (signed char)spc700->dsp_regs[0x0D ] >> 14; |
| 708 | |
| 709 | if (echor > 32767) |
| 710 | echor = 32767; |
| 711 | else if (echor < -32768) |
| 712 | echor = -32768; |
| 713 | |
| 714 | #ifdef DBG_ECHO |
| 715 | logerror("Echo: Writing %04X,%04X at location %04X\n", (UINT16)echol, (UINT16)echor, echo_base); |
| 716 | #endif |
| 717 | |
| 718 | *(UINT16 *)&spc700->ram[echo_base] = MEtoLE16((UINT16)echol); |
| 719 | *(UINT16 *)&spc700->ram[echo_base + sizeof(short)] = MEtoLE16((UINT16)echor); |
| 720 | } |
| 721 | |
| 722 | spc700->echo_ptr += 2 * sizeof(short); |
| 723 | |
| 724 | if (spc700->echo_ptr >= ((spc700->dsp_regs[0x7d] & 0x0f) << 11)) |
| 725 | { |
| 726 | spc700->echo_ptr = 0; |
| 727 | } |
| 728 | #endif /* !defined( NO_ECHO ) */ |
| 729 | |
| 730 | if (sound_ptr != NULL) |
| 731 | { |
| 732 | if (spc700->dsp_regs[0x6c] & 0x40) |
| 733 | { |
| 734 | /* MUTE */ |
| 735 | #ifdef DEBUG |
| 736 | logerror("MUTED!\n"); |
| 737 | #endif |
| 738 | |
| 739 | *sound_ptr = 0; |
| 740 | sound_ptr++; |
| 741 | *sound_ptr = 0; |
| 742 | sound_ptr++; |
| 743 | } |
| 744 | else |
| 745 | { |
| 746 | if (outl > 32767) |
| 747 | *sound_ptr = 32767; |
| 748 | else if (outl < -32768) |
| 749 | *sound_ptr = -32768; |
| 750 | else |
| 751 | *sound_ptr = outl; |
| 752 | |
| 753 | sound_ptr++; |
| 754 | |
| 755 | if (outr > 32767) |
| 756 | *sound_ptr = 32767; |
| 757 | else if (outr < -32768) |
| 758 | *sound_ptr = -32768; |
| 759 | else |
| 760 | *sound_ptr = outr; |
| 761 | |
| 762 | sound_ptr++; |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | |
| 768 | /*------------------------------------------------- |
| 769 | advance_envelope |
| 770 | |
| 771 | Run envelope step & return ENVX. v is the voice |
| 772 | to process envelope for. |
| 773 | -------------------------------------------------*/ |
| 774 | |
| 775 | static int advance_envelope( running_device *device, int v ) |
| 776 | { |
| 777 | snes_sound_state *spc700 = get_safe_token(device); |
| 778 | int envx; |
| 779 | int cnt; |
| 780 | int adsr1; |
| 781 | int t; |
| 782 | |
| 783 | envx = spc700->voice_state[v].envx; |
| 784 | |
| 785 | if (spc700->voice_state[v].envstate == RELEASE) |
| 786 | { |
| 787 | /* Docs: "When in the state of "key off". the "click" sound is prevented |
| 788 | by the addition of the fixed value 1/256" WTF??? Alright, I'm going |
| 789 | to choose to interpret that this way: When a note is keyed off, start |
| 790 | the RELEASE state, which subtracts 1/256th each sample period (32kHz). |
| 791 | Note there's no need for a count because it always happens every |
| 792 | update. */ |
| 793 | envx -= 0x8; /* 0x8 / 0x800 = 1/256th */ |
| 794 | if (envx <= 0) |
| 795 | { |
| 796 | envx = 0; |
| 797 | spc700->keys &= ~(1 << v); |
| 798 | return -1; |
| 799 | } |
| 800 | |
| 801 | spc700->voice_state[v].envx = envx; |
| 802 | spc700->dsp_regs[(v << 4) + 8] = envx >> 8; |
| 803 | |
| 804 | #ifdef DBG_ENV |
| 805 | logerror("ENV voice %d: envx=%03X, state=RELEASE\n", v, envx); |
| 806 | #endif |
| 807 | |
| 808 | return envx; |
| 809 | } |
| 810 | |
| 811 | cnt = spc700->voice_state[v].envcnt; |
| 812 | adsr1 = spc700->dsp_regs[(v << 4) + 5]; |
| 813 | |
| 814 | if (adsr1 & 0x80) |
| 815 | { |
| 816 | switch (spc700->voice_state[v].envstate) |
| 817 | { |
| 818 | case ATTACK: |
| 819 | /* Docs are very confusing. "AR is multiplied by the fixed value |
| 820 | 1/64..." I believe it means to add 1/64th to ENVX once every |
| 821 | time ATTACK is updated, and that's what I'm going to implement. */ |
| 822 | t = adsr1 & 0x0f; |
| 823 | |
| 824 | if (t == 0x0f) |
| 825 | { |
| 826 | #ifdef DBG_ENV |
| 827 | logerror("ENV voice %d: instant attack\n", v); |
| 828 | #endif |
| 829 | |
| 830 | envx += 0x400; |
| 831 | } |
| 832 | else |
| 833 | { |
| 834 | cnt -= ENVCNT[(t << 1) + 1]; |
| 835 | |
| 836 | if (cnt > 0) |
| 837 | break; |
| 838 | |
| 839 | envx += 0x20; /* 0x020 / 0x800 = 1/64 */ |
| 840 | cnt = CNT_INIT; |
| 841 | } |
| 842 | |
| 843 | if (envx > 0x7ff) |
| 844 | { |
| 845 | envx = 0x7ff; |
| 846 | spc700->voice_state[v].envstate = DECAY; |
| 847 | } |
| 848 | |
| 849 | #ifdef DBG_ENV |
| 850 | logerror("ENV voice %d: envx=%03X, state=ATTACK\n", v, envx); |
| 851 | #endif |
| 852 | |
| 853 | spc700->voice_state[v].envx = envx; |
| 854 | break; |
| 855 | |
| 856 | case DECAY: |
| 857 | /* Docs: "DR... [is multiplied] by the fixed value 1-1/256." |
| 858 | Well, at least that makes some sense. Multiplying ENVX by |
| 859 | 255/256 every time DECAY is updated. */ |
| 860 | cnt -= ENVCNT[((adsr1 >> 3) & 0x0e) + 0x10]; |
| 861 | |
| 862 | if (cnt <= 0) |
| 863 | { |
| 864 | cnt = CNT_INIT; |
| 865 | envx -= ((envx - 1) >> 8) + 1; |
| 866 | spc700->voice_state[v].envx = envx; |
| 867 | } |
| 868 | |
| 869 | if (envx <= 0x100 * (SL(v) + 1)) |
| 870 | spc700->voice_state[v].envstate = SUSTAIN; |
| 871 | |
| 872 | #ifdef DBG_ENV |
| 873 | logerror("ENV voice %d: envx=%03X, state=DECAY\n", v, envx); |
| 874 | #endif |
| 875 | |
| 876 | break; |
| 877 | |
| 878 | case SUSTAIN: |
| 879 | /* Docs: "SR [is multiplied] by the fixed value 1-1/256." |
| 880 | Multiplying ENVX by 255/256 every time SUSTAIN is updated. */ |
| 881 | #ifdef DBG_ENV |
| 882 | if (ENVCNT[SR(v)] == 0) |
| 883 | logerror("ENV voice %d: envx=%03X, state=SUSTAIN, zero rate\n", v, envx); |
| 884 | #endif |
| 885 | |
| 886 | cnt -= ENVCNT[SR(v)]; |
| 887 | if (cnt > 0) |
| 888 | break; |
| 889 | |
| 890 | cnt = CNT_INIT; |
| 891 | envx -= ((envx - 1) >> 8) + 1; |
| 892 | |
| 893 | #ifdef DBG_ENV |
| 894 | logerror("ENV voice %d: envx=%03X, state=SUSTAIN\n", v, envx); |
| 895 | #endif |
| 896 | |
| 897 | spc700->voice_state[v].envx = envx; |
| 898 | |
| 899 | /* Note: no way out of this state except by explicit KEY OFF (or switch to GAIN). */ |
| 900 | break; |
| 901 | |
| 902 | case RELEASE: /* Handled earlier to prevent GAIN mode from stopping KEY OFF events */ |
| 903 | break; |
| 904 | } |
| 905 | } |
| 906 | else |
| 907 | { |
| 908 | /* GAIN mode is set |
| 909 | Note: if the game switches between ADSR and GAIN modes partway |
| 910 | through, should the count be reset, or should it continue from |
| 911 | where it was? Does the DSP actually watch for that bit to |
| 912 | change, or does it just go along with whatever it sees when it |
| 913 | performs the update? I'm going to assume the latter and not |
| 914 | update the count, unless I see a game that obviously wants the |
| 915 | other behavior. The effect would be pretty subtle, in any case. |
| 916 | */ |
| 917 | t = spc700->dsp_regs[(v << 4) + 7]; |
| 918 | |
| 919 | if (t < 0x80) |
| 920 | { |
| 921 | envx = t << 4; |
| 922 | spc700->voice_state[v].envx = envx; |
| 923 | |
| 924 | #ifdef DBG_ENV |
| 925 | logerror("ENV voice %d: envx=%03X, state=DIRECT\n", v, envx); |
| 926 | #endif |
| 927 | } |
| 928 | else |
| 929 | { |
| 930 | switch (t >> 5) |
| 931 | { |
| 932 | case 4: |
| 933 | /* Docs: "Decrease (linear): Subtraction of the fixed value 1/64." */ |
| 934 | cnt -= ENVCNT[t & 0x1f]; |
| 935 | |
| 936 | if (cnt > 0) |
| 937 | break; |
| 938 | |
| 939 | cnt = CNT_INIT; |
| 940 | envx -= 0x020; /* 0x020 / 0x800 = 1/64th */ |
| 941 | |
| 942 | if (envx < 0) |
| 943 | envx = 0; |
| 944 | |
| 945 | #ifdef DBG_ENV |
| 946 | logerror("ENV voice %d: envx=%03X, state=DECREASE\n", v, envx); |
| 947 | #endif |
| 948 | |
| 949 | spc700->voice_state[v].envx = envx; |
| 950 | break; |
| 951 | |
| 952 | case 5: |
| 953 | /* Docs: "Drecrease <sic> (exponential): Multiplication by the fixed value 1-1/256." */ |
| 954 | cnt -= ENVCNT[t & 0x1f]; |
| 955 | |
| 956 | if (cnt > 0) |
| 957 | break; |
| 958 | |
| 959 | cnt = CNT_INIT; |
| 960 | envx -= ((envx - 1) >> 8) + 1; |
| 961 | |
| 962 | #ifdef DBG_ENV |
| 963 | logerror("ENV voice %d: envx=%03X, state=EXP\n", v, envx); |
| 964 | #endif |
| 965 | |
| 966 | spc700->voice_state[v].envx = envx; |
| 967 | break; |
| 968 | |
| 969 | case 6: |
| 970 | /* Docs: "Increase (linear): Addition of the fixed value 1/64." */ |
| 971 | cnt -= ENVCNT[t & 0x1f]; |
| 972 | |
| 973 | if (cnt > 0) |
| 974 | break; |
| 975 | |
| 976 | cnt = CNT_INIT; |
| 977 | envx += 0x020; /* 0x020 / 0x800 = 1/64th */ |
| 978 | if (envx > 0x7ff) |
| 979 | envx = 0x7ff; |
| 980 | |
| 981 | #ifdef DBG_ENV |
| 982 | logerror("ENV voice %d: envx=%03X, state=INCREASE\n", v, envx); |
| 983 | #endif |
| 984 | |
| 985 | spc700->voice_state[v].envx = envx; |
| 986 | break; |
| 987 | |
| 988 | case 7: |
| 989 | /* Docs: "Increase (bent line): Addition of the constant |
| 990 | 1/64 up to .75 of the constaint <sic> 1/256 from .75 to 1." */ |
| 991 | cnt -= ENVCNT[t & 0x1f]; |
| 992 | |
| 993 | if (cnt > 0) |
| 994 | break; |
| 995 | |
| 996 | cnt = CNT_INIT; |
| 997 | |
| 998 | if (envx < 0x600) /* 0x600 / 0x800 = .75 */ |
| 999 | envx += 0x020; /* 0x020 / 0x800 = 1/64 */ |
| 1000 | else |
| 1001 | envx += 0x008; /* 0x008 / 0x800 = 1/256 */ |
| 1002 | |
| 1003 | if (envx > 0x7ff) |
| 1004 | envx=0x7ff; |
| 1005 | |
| 1006 | #ifdef DBG_ENV |
| 1007 | logerror("ENV voice %d: envx=%03X, state=INCREASE\n", v, envx); |
| 1008 | #endif |
| 1009 | |
| 1010 | spc700->voice_state[v].envx = envx; |
| 1011 | break; |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | spc700->voice_state[v].envcnt = cnt; |
| 1017 | spc700->dsp_regs[(v << 4) + 8] = envx >> 4; |
| 1018 | |
| 1019 | return envx; |
| 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | static TIMER_CALLBACK( snes_spc_timer ) |
| 1024 | { |
| 1025 | snes_sound_state *spc700 = (snes_sound_state *)ptr; |
| 1026 | int which = param; |
| 1027 | |
| 1028 | spc700->counter[which]++; |
| 1029 | if (spc700->counter[which] >= spc700->ram[0xfa + which] ) // minus = |
| 1030 | { |
| 1031 | spc700->counter[which] = 0; |
| 1032 | spc700->ram[0xfd + which]++; |
| 1033 | spc700->ram[0xfd + which] &= 0x0f; |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | static STREAM_UPDATE( snes_sh_update ) |
| 1038 | { |
| 1039 | int i; |
| 1040 | short mix[2]; |
| 1041 | |
| 1042 | for (i = 0; i < samples; i++) |
| 1043 | { |
| 1044 | mix[0] = mix[1] = 0; |
| 1045 | dsp_update(device, mix); |
| 1046 | |
| 1047 | /* Update the buffers */ |
| 1048 | outputs[0][i] = (stream_sample_t)mix[0]; |
| 1049 | outputs[1][i] = (stream_sample_t)mix[1]; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | |
| 1054 | /*************************** |
| 1055 | I/O for DSP |
| 1056 | ***************************/ |
| 1057 | |
| 1058 | static READ8_DEVICE_HANDLER( snes_dsp_io_r ) |
| 1059 | { |
| 1060 | snes_sound_state *spc700 = get_safe_token(device); |
| 1061 | |
| 1062 | stream_update(spc700->channel); |
| 1063 | |
| 1064 | #ifdef NO_ENVX |
| 1065 | if (8 == (spc700->ram[0xf2] & 0x0f)) |
| 1066 | spc700->dsp_regs[spc700->ram[0xf2]] = 0; |
| 1067 | #endif |
| 1068 | |
| 1069 | /* All reads simply return the contents of the addressed register. */ |
| 1070 | return spc700->dsp_regs[offset & 0x7f]; |
| 1071 | } |
| 1072 | |
| 1073 | static WRITE8_DEVICE_HANDLER( snes_dsp_io_w ) |
| 1074 | { |
| 1075 | snes_sound_state *spc700 = get_safe_token(device); |
| 1076 | |
| 1077 | stream_update(spc700->channel); |
| 1078 | |
| 1079 | if (offset == 0x7c) |
| 1080 | { |
| 1081 | /* Writes to register 0x7c (ENDX) works as follows */ |
| 1082 | spc700->dsp_regs[offset] = 0; |
| 1083 | } |
| 1084 | else |
| 1085 | { |
| 1086 | /* All other writes store the value in the addressed register as expected. */ |
| 1087 | spc700->dsp_regs[offset] = data; |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | /*************************** |
| 1092 | I/O for SPC700 |
| 1093 | ***************************/ |
| 1094 | |
| 1095 | READ8_DEVICE_HANDLER( spc_io_r ) |
| 1096 | { |
| 1097 | snes_sound_state *spc700 = get_safe_token(device); |
| 1098 | |
| 1099 | switch (offset) /* Offset is from 0x00f0 */ |
| 1100 | { |
| 1101 | case 0x0: //FIXME: Super Bomberman PBW reads from there, is it really write-only? |
| 1102 | return 0; |
| 1103 | case 0x1: |
| 1104 | return 0; //Super Kick Boxing reads port 1 and wants it to be zero. |
| 1105 | case 0x2: /* Register address */ |
| 1106 | return spc700->ram[0xf2]; |
| 1107 | case 0x3: /* Register data */ |
| 1108 | return snes_dsp_io_r(device, spc700->ram[0xf2]); |
| 1109 | case 0x4: /* Port 0 */ |
| 1110 | case 0x5: /* Port 1 */ |
| 1111 | case 0x6: /* Port 2 */ |
| 1112 | case 0x7: /* Port 3 */ |
| 1113 | // mame_printf_debug("SPC: rd %02x @ %d, PC=%x\n", spc700->port_in[offset - 4], offset - 4, cpu_get_pc(space->cpu)); |
| 1114 | return spc700->port_in[offset - 4]; |
| 1115 | case 0x8: //normal RAM, can be read even if the ram disabled flag ($f0 bit 1) is active |
| 1116 | case 0x9: |
| 1117 | return spc700->ram[0xf0 + offset]; |
| 1118 | case 0xa: /* Timer 0 */ |
| 1119 | case 0xb: /* Timer 1 */ |
| 1120 | case 0xc: /* Timer 2 */ |
| 1121 | break; |
| 1122 | case 0xd: /* Counter 0 */ |
| 1123 | case 0xe: /* Counter 1 */ |
| 1124 | case 0xf: /* Counter 2 */ |
| 1125 | { |
| 1126 | UINT8 value = spc700->ram[0xf0 + offset] & 0x0f; |
| 1127 | spc700->ram[0xf0 + offset] = 0; |
| 1128 | return value; |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
| 1135 | WRITE8_DEVICE_HANDLER( spc_io_w ) |
| 1136 | { |
| 1137 | snes_sound_state *spc700 = get_safe_token(device); |
| 1138 | int i; |
| 1139 | |
| 1140 | switch (offset) /* Offset is from 0x00f0 */ |
| 1141 | { |
| 1142 | case 0x0: |
| 1143 | printf("Warning: write to SOUND TEST register with data %02x!\n", data); |
| 1144 | break; |
| 1145 | case 0x1: /* Control */ |
| 1146 | for (i = 0; i < 3; i++) |
| 1147 | { |
| 1148 | if (BIT(data, i) && !spc700->enabled[i]) |
| 1149 | { |
| 1150 | spc700->counter[i] = 0; |
| 1151 | spc700->ram[0xfd + i] = 0; |
| 1152 | } |
| 1153 | |
| 1154 | spc700->enabled[i] = BIT(data, i); |
| 1155 | timer_enable(spc700->timer[i], spc700->enabled[i]); |
| 1156 | } |
| 1157 | |
| 1158 | if (BIT(data, 4)) |
| 1159 | { |
| 1160 | spc700->port_in[0] = 0; |
| 1161 | spc700->port_in[1] = 0; |
| 1162 | } |
| 1163 | |
| 1164 | if (BIT(data, 5)) |
| 1165 | { |
| 1166 | spc700->port_in[2] = 0; |
| 1167 | spc700->port_in[3] = 0; |
| 1168 | } |
| 1169 | |
| 1170 | if ((data & 0x80) != (spc700->ram[0xf1] & 0x80)) |
| 1171 | { |
| 1172 | if (data & 0x80) |
| 1173 | memcpy(spc700->ipl_region, memory_region(device->machine, "user5"), 64); |
| 1174 | else |
| 1175 | memcpy(spc700->ipl_region, &spc700->ram[0xffc0], 64); |
| 1176 | } |
| 1177 | break; |
| 1178 | case 0x2: /* Register address */ |
| 1179 | break; |
| 1180 | case 0x3: /* Register data */ |
| 1181 | snes_dsp_io_w(device, spc700->ram[0xf2], data); |
| 1182 | break; |
| 1183 | case 0x4: /* Port 0 */ |
| 1184 | case 0x5: /* Port 1 */ |
| 1185 | case 0x6: /* Port 2 */ |
| 1186 | case 0x7: /* Port 3 */ |
| 1187 | // mame_printf_debug("SPC: %02x to APU @ %d (PC=%x)\n", data, offset & 3, cpu_get_pc(space->cpu)); |
| 1188 | spc700->port_out[offset - 4] = data; |
| 1189 | cpuexec_boost_interleave(device->machine, attotime_zero, ATTOTIME_IN_USEC(20)); |
| 1190 | break; |
| 1191 | case 0xa: /* Timer 0 */ |
| 1192 | case 0xb: /* Timer 1 */ |
| 1193 | case 0xc: /* Timer 2 */ |
| 1194 | if (data == 0) |
| 1195 | data = 255; |
| 1196 | break; |
| 1197 | case 0xd: /* Counter 0 */ |
| 1198 | case 0xe: /* Counter 1 */ |
| 1199 | case 0xf: /* Counter 2 */ |
| 1200 | return; |
| 1201 | } |
| 1202 | |
| 1203 | spc700->ram[0xf0 + offset] = data; |
| 1204 | } |
| 1205 | |
| 1206 | READ8_DEVICE_HANDLER( spc_ram_r ) |
| 1207 | { |
| 1208 | snes_sound_state *spc700 = get_safe_token(device); |
| 1209 | return spc700->ram[offset]; |
| 1210 | } |
| 1211 | |
| 1212 | WRITE8_DEVICE_HANDLER( spc_ram_w ) |
| 1213 | { |
| 1214 | snes_sound_state *spc700 = get_safe_token(device); |
| 1215 | |
| 1216 | spc700->ram[offset] = data; |
| 1217 | |
| 1218 | /* if RAM is mapped in, mirror accordingly */ |
| 1219 | if ((!(spc700->ram[0xf1] & 0x80)) && (offset >= 0xffc0)) |
| 1220 | spc700->ipl_region[offset - 0xffc0] = data; |
| 1221 | } |
| 1222 | |
| 1223 | READ8_DEVICE_HANDLER( spc_ipl_r ) |
| 1224 | { |
| 1225 | snes_sound_state *spc700 = get_safe_token(device); |
| 1226 | return spc700->ipl_region[offset]; |
| 1227 | } |
| 1228 | |
| 1229 | |
| 1230 | READ8_DEVICE_HANDLER( spc_port_out ) |
| 1231 | { |
| 1232 | snes_sound_state *spc700 = get_safe_token(device); |
| 1233 | |
| 1234 | assert(offset < 4); |
| 1235 | |
| 1236 | return spc700->port_out[offset]; |
| 1237 | } |
| 1238 | |
| 1239 | WRITE8_DEVICE_HANDLER( spc_port_in ) |
| 1240 | { |
| 1241 | snes_sound_state *spc700 = get_safe_token(device); |
| 1242 | |
| 1243 | assert(offset < 4); |
| 1244 | |
| 1245 | spc700->port_in[offset] = data; |
| 1246 | } |
| 1247 | |
| 1248 | UINT8 *spc_get_ram( running_device *device ) |
| 1249 | { |
| 1250 | snes_sound_state *spc700 = get_safe_token(device); |
| 1251 | |
| 1252 | return spc700->ram; |
| 1253 | } |
| 1254 | |
| 1255 | /***************************************************************************** |
| 1256 | DEVICE INTERFACE |
| 1257 | *****************************************************************************/ |
| 1258 | |
| 1259 | static void state_register( running_device *device ) |
| 1260 | { |
| 1261 | snes_sound_state *spc700 = get_safe_token(device); |
| 1262 | int v; |
| 1263 | |
| 1264 | state_save_register_device_item_array(device, 0, spc700->dsp_regs); |
| 1265 | state_save_register_device_item_array(device, 0, spc700->ipl_region); |
| 1266 | |
| 1267 | state_save_register_device_item(device, 0, spc700->keyed_on); |
| 1268 | state_save_register_device_item(device, 0, spc700->keys); |
| 1269 | |
| 1270 | state_save_register_device_item(device, 0, spc700->noise_cnt); |
| 1271 | state_save_register_device_item(device, 0, spc700->noise_lev); |
| 1272 | |
| 1273 | #ifndef NO_ECHO |
| 1274 | state_save_register_device_item_array(device, 0, spc700->fir_lbuf); |
| 1275 | state_save_register_device_item_array(device, 0, spc700->fir_rbuf); |
| 1276 | state_save_register_device_item(device, 0, spc700->fir_ptr); |
| 1277 | state_save_register_device_item(device, 0, spc700->echo_ptr); |
| 1278 | #endif |
| 1279 | |
| 1280 | state_save_register_device_item_array(device, 0, spc700->enabled); |
| 1281 | state_save_register_device_item_array(device, 0, spc700->counter); |
| 1282 | state_save_register_device_item_array(device, 0, spc700->port_in); |
| 1283 | state_save_register_device_item_array(device, 0, spc700->port_out); |
| 1284 | |
| 1285 | for (v = 0; v < 8; v++) |
| 1286 | { |
| 1287 | state_save_register_device_item(device, v, spc700->voice_state[v].mem_ptr); |
| 1288 | state_save_register_device_item(device, v, spc700->voice_state[v].end); |
| 1289 | state_save_register_device_item(device, v, spc700->voice_state[v].envcnt); |
| 1290 | state_save_register_device_item(device, v, spc700->voice_state[v].envstate); |
| 1291 | state_save_register_device_item(device, v, spc700->voice_state[v].envx); |
| 1292 | state_save_register_device_item(device, v, spc700->voice_state[v].filter); |
| 1293 | state_save_register_device_item(device, v, spc700->voice_state[v].half); |
| 1294 | state_save_register_device_item(device, v, spc700->voice_state[v].header_cnt); |
| 1295 | state_save_register_device_item(device, v, spc700->voice_state[v].mixfrac); |
| 1296 | state_save_register_device_item(device, v, spc700->voice_state[v].on_cnt); |
| 1297 | state_save_register_device_item(device, v, spc700->voice_state[v].pitch); |
| 1298 | state_save_register_device_item(device, v, spc700->voice_state[v].range); |
| 1299 | state_save_register_device_item(device, v, spc700->voice_state[v].samp_id); |
| 1300 | state_save_register_device_item(device, v, spc700->voice_state[v].sampptr); |
| 1301 | state_save_register_device_item(device, v, spc700->voice_state[v].smp1); |
| 1302 | state_save_register_device_item(device, v, spc700->voice_state[v].smp2); |
| 1303 | state_save_register_device_item_array(device, v, spc700->voice_state[v].sampbuf); |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | static DEVICE_START( snes_sound ) |
| 1308 | { |
| 1309 | snes_sound_state *spc700 = get_safe_token(device); |
| 1310 | running_machine *machine = device->machine; |
| 1311 | |
| 1312 | state_register(device); |
| 1313 | |
| 1314 | spc700->channel = stream_create(device, 0, 2, 32000, 0, snes_sh_update); |
| 1315 | |
| 1316 | spc700->ram = auto_alloc_array_clear(device->machine, UINT8, SNES_SPCRAM_SIZE); |
| 1317 | |
| 1318 | /* default to ROM visible */ |
| 1319 | spc700->ram[0xf1] = 0x80; |
| 1320 | |
| 1321 | /* Initialize the timers */ |
| 1322 | spc700->timer[0] = timer_alloc(machine, snes_spc_timer, spc700); |
| 1323 | timer_adjust_periodic(spc700->timer[0], ATTOTIME_IN_HZ(8000), 0, ATTOTIME_IN_HZ(8000)); |
| 1324 | timer_enable(spc700->timer[0], 0); |
| 1325 | spc700->timer[1] = timer_alloc(machine, snes_spc_timer, spc700); |
| 1326 | timer_adjust_periodic(spc700->timer[1], ATTOTIME_IN_HZ(8000), 1, ATTOTIME_IN_HZ(8000)); |
| 1327 | timer_enable(spc700->timer[1], 0); |
| 1328 | spc700->timer[2] = timer_alloc(machine, snes_spc_timer, spc700); |
| 1329 | timer_adjust_periodic(spc700->timer[2], ATTOTIME_IN_HZ(64000), 2, ATTOTIME_IN_HZ(64000)); |
| 1330 | timer_enable(spc700->timer[2], 0); |
| 1331 | } |
| 1332 | |
| 1333 | static DEVICE_RESET( snes_sound ) |
| 1334 | { |
| 1335 | snes_sound_state *spc700 = get_safe_token(device); |
| 1336 | running_machine *machine = device->machine; |
| 1337 | int ii; |
| 1338 | |
| 1339 | /* put IPL image at the top of RAM */ |
| 1340 | memcpy(spc700->ipl_region, memory_region(machine, "user5"), 64); |
| 1341 | |
| 1342 | /* Sort out the ports */ |
| 1343 | for (ii = 0; ii < 4; ii++) |
| 1344 | { |
| 1345 | spc700->port_in[ii] = 0; |
| 1346 | spc700->port_out[ii] = 0; |
| 1347 | } |
| 1348 | |
| 1349 | dsp_reset(device); |
| 1350 | } |
| 1351 | |
| 1352 | /*------------------------------------------------- |
| 1353 | Device definition |
| 1354 | -------------------------------------------------*/ |
| 1355 | |
| 1356 | static const char DEVTEMPLATE_SOURCE[] = __FILE__; |
| 1357 | |
| 1358 | #define DEVTEMPLATE_ID(p,s) p##snes_sound##s |
| 1359 | #define DEVTEMPLATE_FEATURES DT_HAS_START | DT_HAS_RESET |
| 1360 | #define DEVTEMPLATE_NAME "SNES Custom DSP (SPC700)" |
| 1361 | #define DEVTEMPLATE_FAMILY "SNES Custom" |
| 1362 | #include "devtempl.h" |
src/mame/machine/snes.c
| r7592 | r7593 | |
| 15 | 15 | #define __MACHINE_SNES_C |
| 16 | 16 | |
| 17 | 17 | #include "emu.h" |
| 18 | | #include "includes/snes.h" |
| 19 | 18 | #include "cpu/superfx/superfx.h" |
| 20 | 19 | #include "cpu/g65816/g65816.h" |
| 20 | #include "includes/snes.h" |
| 21 | #include "audio/snes_snd.h" |
| 21 | 22 | |
| 22 | 23 | |
| 23 | 24 | /* -- Globals -- */ |
| 24 | 25 | UINT8 *snes_ram = NULL; /* 65816 ram */ |
| 25 | | UINT8 *spc_ram = NULL; /* spc700 ram */ |
| 26 | 26 | UINT8 *snes_vram = NULL; /* Video RAM (Should be 16-bit, but it's easier this way) */ |
| 27 | 27 | UINT16 *snes_cgram = NULL; /* Colour RAM */ |
| 28 | 28 | UINT16 *snes_oam = NULL; /* Object Attribute Memory */ |
| 29 | | UINT8 spc_port_in[4]; /* Port for sending data to the SPC700 */ |
| 30 | | UINT8 spc_port_out[4]; /* Port for receiving data from the SPC700 */ |
| 31 | 29 | |
| 32 | 30 | UINT8 snes_has_addon_chip; |
| 33 | 31 | UINT32 snes_rom_size; |
| r7592 | r7593 | |
| 80 | 78 | snes_state *state = (snes_state *)machine->driver_data; |
| 81 | 79 | |
| 82 | 80 | // pull NMI |
| 83 | | cputag_set_input_line(machine, "maincpu", G65816_LINE_NMI, ASSERT_LINE); |
| 81 | cpu_set_input_line(state->maincpu, G65816_LINE_NMI, ASSERT_LINE); |
| 84 | 82 | |
| 85 | 83 | // don't happen again |
| 86 | 84 | timer_adjust_oneshot(state->nmi_timer, attotime_never, 0); |
| r7592 | r7593 | |
| 94 | 92 | // (don't need to switch to the 65816 context, we don't do anything dependant on it) |
| 95 | 93 | snes_latch_counters(machine); |
| 96 | 94 | snes_ram[TIMEUP] = 0x80; /* Indicate that irq occured */ |
| 97 | | cputag_set_input_line(machine, "maincpu", G65816_LINE_IRQ, ASSERT_LINE); |
| 95 | cpu_set_input_line(state->maincpu, G65816_LINE_IRQ, ASSERT_LINE); |
| 98 | 96 | |
| 99 | 97 | // don't happen again |
| 100 | 98 | timer_adjust_oneshot(state->hirq_timer, attotime_never, 0); |
| r7592 | r7593 | |
| 107 | 105 | |
| 108 | 106 | static TIMER_CALLBACK( snes_reset_oam_address ) |
| 109 | 107 | { |
| 108 | snes_state *state = (snes_state *)machine->driver_data; |
| 110 | 109 | // make sure we're in the 65816's context since we're messing with the OAM and stuff |
| 111 | | const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM); |
| 110 | const address_space *space = cpu_get_address_space(state->maincpu, ADDRESS_SPACE_PROGRAM); |
| 112 | 111 | |
| 113 | 112 | if (!(snes_ppu.screen_disabled)) //Reset OAM address, byuu says it happens at H=10 |
| 114 | 113 | { |
| 115 | | memory_write_byte(space, OAMADDL, snes_ppu.oam.saved_address_low ); /* Reset oam address */ |
| 116 | | memory_write_byte(space, OAMADDH, snes_ppu.oam.saved_address_high ); |
| 114 | memory_write_byte(space, OAMADDL, snes_ppu.oam.saved_address_low); /* Reset oam address */ |
| 115 | memory_write_byte(space, OAMADDH, snes_ppu.oam.saved_address_high); |
| 117 | 116 | snes_ppu.oam.first_sprite = snes_ppu.oam.priority_rotation ? (snes_ppu.oam.address >> 1) & 127 : 0; |
| 118 | 117 | } |
| 119 | 118 | } |
| r7592 | r7593 | |
| 141 | 140 | snes_ram[TIMEUP] = 0x80; /* Indicate that irq occured */ |
| 142 | 141 | // IRQ latches the counters, do it now |
| 143 | 142 | snes_latch_counters(machine); |
| 144 | | cputag_set_input_line(machine, "maincpu", G65816_LINE_IRQ, ASSERT_LINE ); |
| 143 | cpu_set_input_line(state->maincpu, G65816_LINE_IRQ, ASSERT_LINE ); |
| 145 | 144 | } |
| 146 | 145 | } |
| 147 | 146 | /* Horizontal IRQ timer */ |
| r7592 | r7593 | |
| 184 | 183 | if (snes_ram[NMITIMEN] & 0x80) /* NMI only signaled if this bit set */ |
| 185 | 184 | { |
| 186 | 185 | // NMI goes off about 12 cycles after this (otherwise Chrono Trigger, NFL QB Club, etc. lock up) |
| 187 | | timer_adjust_oneshot(state->nmi_timer, cputag_clocks_to_attotime(machine, "maincpu", 12), 0); |
| 186 | timer_adjust_oneshot(state->nmi_timer, cpu_clocks_to_attotime(state->maincpu, 12), 0); |
| 188 | 187 | } |
| 189 | 188 | } |
| 190 | 189 | |
| r7592 | r7593 | |
| 208 | 207 | snes_ram[STAT78] ^= 0x80; /* Toggle field flag */ |
| 209 | 208 | snes_ppu.stat77_flags &= 0x3f; /* Clear Time Over and Range Over bits */ |
| 210 | 209 | |
| 211 | | cputag_set_input_line(machine, "maincpu", G65816_LINE_NMI, CLEAR_LINE ); |
| 210 | cpu_set_input_line(state->maincpu, G65816_LINE_NMI, CLEAR_LINE ); |
| 212 | 211 | } |
| 213 | 212 | |
| 214 | 213 | timer_adjust_oneshot(state->scanline_timer, attotime_never, 0); |
| r7592 | r7593 | |
| 219 | 218 | static TIMER_CALLBACK( snes_hblank_tick ) |
| 220 | 219 | { |
| 221 | 220 | snes_state *state = (snes_state *)machine->driver_data; |
| 222 | | const address_space *cpu0space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM); |
| 221 | const address_space *cpu0space = cpu_get_address_space(state->maincpu, ADDRESS_SPACE_PROGRAM); |
| 223 | 222 | int nextscan; |
| 224 | 223 | |
| 225 | 224 | snes_ppu.beam.current_vert = video_screen_get_vpos(machine->primary_screen); |
| r7592 | r7593 | |
| 352 | 351 | // APU is mirrored from 2140 to 217f |
| 353 | 352 | if (offset >= APU00 && offset < WMDATA) |
| 354 | 353 | { |
| 355 | | return spc_port_out[offset & 0x3]; |
| 354 | return spc_port_out(state->spc700, offset & 0x3); |
| 356 | 355 | } |
| 357 | 356 | |
| 358 | | if (snes_has_addon_chip == HAS_SUPERFX && devtag_get_device(space->machine, "superfx") != NULL) |
| 357 | if (snes_has_addon_chip == HAS_SUPERFX && state->superfx != NULL) |
| 359 | 358 | { |
| 360 | 359 | if (offset >= 0x3000 && offset < 0x3300) |
| 361 | 360 | { |
| 362 | | return superfx_mmio_read(devtag_get_device(space->machine, "superfx"), offset); |
| 361 | return superfx_mmio_read(state->superfx, offset); |
| 363 | 362 | } |
| 364 | 363 | } |
| 365 | 364 | else if (snes_has_addon_chip == HAS_RTC) |
| r7592 | r7593 | |
| 624 | 623 | return value; |
| 625 | 624 | case TIMEUP: /* IRQ flag by H/V count timer */ |
| 626 | 625 | value = (snes_open_bus_r(space,0) & 0x7f) | (snes_ram[TIMEUP] & 0x80); |
| 627 | | cputag_set_input_line(space->machine, "maincpu", G65816_LINE_IRQ, CLEAR_LINE ); |
| 626 | cpu_set_input_line(state->maincpu, G65816_LINE_IRQ, CLEAR_LINE ); |
| 628 | 627 | snes_ram[TIMEUP] = 0; |
| 629 | 628 | return value; |
| 630 | 629 | case HVBJOY: /* H/V blank and joypad controller enable */ |
| r7592 | r7593 | |
| 714 | 713 | // APU is mirrored from 2140 to 217f |
| 715 | 714 | if (offset >= APU00 && offset < WMDATA) |
| 716 | 715 | { |
| 717 | | // printf("816: %02x to APU @ %d\n", data, offset&3); |
| 718 | | spc_port_in[offset & 0x3] = data; |
| 716 | // printf("816: %02x to APU @ %d\n", data, offset & 3); |
| 717 | spc_port_in(state->spc700, offset & 0x3, data); |
| 719 | 718 | cpuexec_boost_interleave(space->machine, attotime_zero, ATTOTIME_IN_USEC(20)); |
| 720 | 719 | return; |
| 721 | 720 | } |
| 722 | 721 | |
| 723 | | if (snes_has_addon_chip == HAS_SUPERFX && devtag_get_device(space->machine, "superfx") != NULL) |
| 722 | if (snes_has_addon_chip == HAS_SUPERFX && state->superfx != NULL) |
| 724 | 723 | { |
| 725 | 724 | if (offset >= 0x3000 && offset < 0x3300) |
| 726 | 725 | { |
| 727 | | superfx_mmio_write(devtag_get_device(space->machine, "superfx"), offset, data); |
| 726 | superfx_mmio_write(state->superfx, offset, data); |
| 728 | 727 | return; |
| 729 | 728 | } |
| 730 | 729 | } |
| r7592 | r7593 | |
| 1242 | 1241 | break; |
| 1243 | 1242 | case WRMPYB: /* Multiplier B */ |
| 1244 | 1243 | snes_ram[WRMPYB] = data; |
| 1245 | | // timer_adjust_oneshot(state->mult_timer, cputag_clocks_to_attotime(space->machine, "maincpu", 8), 0); |
| 1244 | // timer_adjust_oneshot(state->mult_timer, cpu_clocks_to_attotime(state->maincpu, 8), 0); |
| 1246 | 1245 | { |
| 1247 | 1246 | UINT32 c = snes_ram[WRMPYA] * snes_ram[WRMPYB]; |
| 1248 | 1247 | snes_ram[RDMPYL] = c & 0xff; |
| r7592 | r7593 | |
| 1254 | 1253 | break; |
| 1255 | 1254 | case WRDVDD: /* Divisor */ |
| 1256 | 1255 | snes_ram[WRDVDD] = data; |
| 1257 | | // timer_adjust_oneshot(state->div_timer, cputag_clocks_to_attotime(space->machine, "maincpu", 16), 0); |
| 1256 | // timer_adjust_oneshot(state->div_timer, cpu_clocks_to_attotime(state->maincpu, 16), 0); |
| 1258 | 1257 | { |
| 1259 | 1258 | UINT16 value, dividend, remainder; |
| 1260 | 1259 | dividend = remainder = 0; |
| r7592 | r7593 | |
| 1291 | 1290 | case MEMSEL: /* Access cycle designation in memory (2) area */ |
| 1292 | 1291 | /* FIXME: Need to adjust the speed only during access of banks 0x80+ |
| 1293 | 1292 | * Currently we are just increasing it no matter what */ |
| 1294 | | // cpu_set_clockscale(devtag_get_device(space->machine, "maincpu"), (data & 0x1) ? 1.335820896 : 1.0 ); |
| 1293 | // cpu_set_clockscale(state->maincpu, (data & 0x1) ? 1.335820896 : 1.0 ); |
| 1295 | 1294 | #ifdef SNES_DBG_REG_W |
| 1296 | 1295 | if ((data & 0x1) != (snes_ram[MEMSEL] & 0x1)) |
| 1297 | 1296 | mame_printf_debug( "CPU speed: %f Mhz\n", (data & 0x1) ? 3.58 : 2.68 ); |
| r7592 | r7593 | |
| 1347 | 1346 | |
| 1348 | 1347 | WRITE_LINE_DEVICE_HANDLER( snes_extern_irq_w ) |
| 1349 | 1348 | { |
| 1350 | | cputag_set_input_line(device->machine, "maincpu", G65816_LINE_IRQ, state); |
| 1349 | snes_state *driver_state = (snes_state *)device->machine->driver_data; |
| 1350 | cpu_set_input_line(driver_state->maincpu, G65816_LINE_IRQ, state); |
| 1351 | 1351 | } |
| 1352 | 1352 | |
| 1353 | 1353 | /************************************* |
| r7592 | r7593 | |
| 1428 | 1428 | /* 0x000000 - 0x2fffff */ |
| 1429 | 1429 | READ8_HANDLER( snes_r_bank1 ) |
| 1430 | 1430 | { |
| 1431 | snes_state *state = (snes_state *)space->machine->driver_data; |
| 1431 | 1432 | UINT8 value; |
| 1432 | 1433 | UINT16 address = offset & 0xffff; |
| 1433 | 1434 | |
| r7592 | r7593 | |
| 1437 | 1438 | value = snes_r_io(space, address); |
| 1438 | 1439 | else if (address < 0x8000) |
| 1439 | 1440 | { |
| 1440 | | if (snes_has_addon_chip == HAS_SUPERFX && devtag_get_device(space->machine, "superfx") != NULL) |
| 1441 | if (snes_has_addon_chip == HAS_SUPERFX && state->superfx != NULL) |
| 1441 | 1442 | value = snes_ram[0x700000 + (address & 0x1fff)]; |
| 1442 | 1443 | else if (snes_has_addon_chip == HAS_OBC1) |
| 1443 | 1444 | value = obc1_read(space, offset); |
| r7592 | r7593 | |
| 1546 | 1547 | /* 0x600000 - 0x6fffff */ |
| 1547 | 1548 | READ8_HANDLER( snes_r_bank4 ) |
| 1548 | 1549 | { |
| 1550 | snes_state *state = (snes_state *)space->machine->driver_data; |
| 1549 | 1551 | UINT8 value = 0xff; |
| 1550 | 1552 | UINT16 address = offset & 0xffff; |
| 1551 | 1553 | |
| 1552 | | if (snes_has_addon_chip == HAS_SUPERFX && devtag_get_device(space->machine, "superfx") != NULL) |
| 1554 | if (snes_has_addon_chip == HAS_SUPERFX && state->superfx != NULL) |
| 1553 | 1555 | { |
| 1554 | 1556 | value = snes_ram[0x600000 + offset]; |
| 1555 | 1557 | } |
| r7592 | r7593 | |
| 1579 | 1581 | /* 0x700000 - 0x7dffff */ |
| 1580 | 1582 | READ8_HANDLER( snes_r_bank5 ) |
| 1581 | 1583 | { |
| 1584 | snes_state *state = (snes_state *)space->machine->driver_data; |
| 1582 | 1585 | UINT8 value; |
| 1583 | 1586 | UINT16 address = offset & 0xffff; |
| 1584 | 1587 | |
| 1585 | | if (snes_has_addon_chip == HAS_SUPERFX && devtag_get_device(space->machine, "superfx") != NULL) |
| 1588 | if (snes_has_addon_chip == HAS_SUPERFX && state->superfx != NULL) |
| 1586 | 1589 | { |
| 1587 | 1590 | value = snes_ram[0x700000 + offset]; |
| 1588 | 1591 | } |
| r7592 | r7593 | |
| 1610 | 1613 | /* 0x800000 - 0xbfffff */ |
| 1611 | 1614 | READ8_HANDLER( snes_r_bank6 ) |
| 1612 | 1615 | { |
| 1616 | snes_state *state = (snes_state *)space->machine->driver_data; |
| 1613 | 1617 | UINT8 value = 0; |
| 1614 | 1618 | UINT16 address = offset & 0xffff; |
| 1615 | 1619 | |
| 1616 | 1620 | if (address < 0x8000) |
| 1617 | 1621 | { |
| 1618 | | if (address >= 0x6000 && snes_has_addon_chip == HAS_SUPERFX && devtag_get_device(space->machine, "superfx") != NULL) |
| 1622 | if (address >= 0x6000 && snes_has_addon_chip == HAS_SUPERFX && state->superfx != NULL) |
| 1619 | 1623 | logerror( "snes_r_bank6 hit in Super FX mode, please fix me\n" ); |
| 1620 | 1624 | else if (snes_cart.mode != SNES_MODE_25) |
| 1621 | 1625 | value = memory_read_byte(space, offset); |
| r7592 | r7593 | |
| 1657 | 1661 | /* 0xc00000 - 0xffffff */ |
| 1658 | 1662 | READ8_HANDLER( snes_r_bank7 ) |
| 1659 | 1663 | { |
| 1664 | snes_state *state = (snes_state *)space->machine->driver_data; |
| 1660 | 1665 | UINT8 value = 0; |
| 1661 | 1666 | UINT16 address = offset & 0xffff; |
| 1662 | 1667 | |
| r7592 | r7593 | |
| 1664 | 1669 | { |
| 1665 | 1670 | return sdd1_read(space->machine, offset); |
| 1666 | 1671 | } |
| 1667 | | else if (snes_has_addon_chip == HAS_SUPERFX && devtag_get_device(space->machine, "superfx") != NULL) |
| 1672 | else if (snes_has_addon_chip == HAS_SUPERFX && state->superfx != NULL) |
| 1668 | 1673 | { |
| 1669 | 1674 | logerror( "snes_r_bank7 hit in Super FX mode, please fix me\n" ); |
| 1670 | 1675 | } |
| r7592 | r7593 | |
| 1771 | 1776 | /* 0x600000 - 0x6fffff */ |
| 1772 | 1777 | WRITE8_HANDLER( snes_w_bank4 ) |
| 1773 | 1778 | { |
| 1779 | snes_state *state = (snes_state *)space->machine->driver_data; |
| 1774 | 1780 | UINT16 address = offset & 0xffff; |
| 1775 | 1781 | |
| 1776 | | if (snes_has_addon_chip == HAS_SUPERFX && devtag_get_device(space->machine, "superfx") != NULL) |
| 1782 | if (snes_has_addon_chip == HAS_SUPERFX && state->superfx != NULL) |
| 1777 | 1783 | { |
| 1778 | 1784 | snes_ram[0x600000 + offset] = data; |
| 1779 | 1785 | } |
| r7592 | r7593 | |
| 1805 | 1811 | /* 0x700000 - 0x7dffff */ |
| 1806 | 1812 | WRITE8_HANDLER( snes_w_bank5 ) |
| 1807 | 1813 | { |
| 1814 | snes_state *state = (snes_state *)space->machine->driver_data; |
| 1808 | 1815 | UINT16 address = offset & 0xffff; |
| 1809 | 1816 | |
| 1810 | | if (snes_has_addon_chip == HAS_SUPERFX && devtag_get_device(space->machine, "superfx") != NULL) |
| 1817 | if (snes_has_addon_chip == HAS_SUPERFX && state->superfx != NULL) |
| 1811 | 1818 | { |
| 1812 | 1819 | snes_ram[0x700000 + offset] = data; |
| 1813 | 1820 | } |
| r7592 | r7593 | |
| 1833 | 1840 | /* 0x800000 - 0xbfffff */ |
| 1834 | 1841 | WRITE8_HANDLER( snes_w_bank6 ) |
| 1835 | 1842 | { |
| 1843 | snes_state *state = (snes_state *)space->machine->driver_data; |
| 1836 | 1844 | UINT16 address = offset & 0xffff; |
| 1837 | 1845 | |
| 1838 | 1846 | if (address < 0x8000) |
| 1839 | 1847 | { |
| 1840 | | if (address >= 0x6000 && snes_has_addon_chip == HAS_SUPERFX && devtag_get_device(space->machine, "superfx") != NULL) |
| 1848 | if (address >= 0x6000 && snes_has_addon_chip == HAS_SUPERFX && state->superfx != NULL) |
| 1841 | 1849 | { |
| 1842 | 1850 | logerror( "snes_w_bank6 hit (RAM) in Super FX mode, please fix me\n" ); |
| 1843 | 1851 | } |
| r7592 | r7593 | |
| 1879 | 1887 | DSP3_write(address, data); |
| 1880 | 1888 | else if ((snes_has_addon_chip == HAS_DSP4) && (offset >= 0x300000) && (address < 0xc000)) |
| 1881 | 1889 | DSP4_write(data); |
| 1882 | | else if (snes_has_addon_chip == HAS_SUPERFX && devtag_get_device(space->machine, "superfx") != NULL) |
| 1890 | else if (snes_has_addon_chip == HAS_SUPERFX && state->superfx != NULL) |
| 1883 | 1891 | logerror( "snes_w_bank6 hit (ROM) in Super FX mode, please fix me\n" ); |
| 1884 | 1892 | else |
| 1885 | 1893 | logerror("Attempt to write to ROM address: %X\n", offset + 0x800000); |
| r7592 | r7593 | |
| 1889 | 1897 | /* 0xc00000 - 0xffffff */ |
| 1890 | 1898 | WRITE8_HANDLER( snes_w_bank7 ) |
| 1891 | 1899 | { |
| 1900 | snes_state *state = (snes_state *)space->machine->driver_data; |
| 1892 | 1901 | UINT16 address = offset & 0xffff; |
| 1893 | 1902 | |
| 1894 | | if (snes_has_addon_chip == HAS_SUPERFX && devtag_get_device(space->machine, "superfx") != NULL) |
| 1903 | if (snes_has_addon_chip == HAS_SUPERFX && state->superfx != NULL) |
| 1895 | 1904 | { |
| 1896 | 1905 | if (offset >= 0x200000) |
| 1897 | 1906 | { |
| r7592 | r7593 | |
| 2033 | 2042 | timer_adjust_oneshot(state->hblank_timer, video_screen_get_time_until_pos(machine->primary_screen, ((snes_ram[STAT78] & 0x10) == SNES_NTSC) ? SNES_VTOTAL_NTSC - 1 : SNES_VTOTAL_PAL - 1, state->hblank_offset), 0); |
| 2034 | 2043 | } |
| 2035 | 2044 | |
| 2036 | | static void snes_init_ram(running_machine *machine) |
| 2045 | static void snes_init_ram( running_machine *machine ) |
| 2037 | 2046 | { |
| 2038 | 2047 | snes_state *state = (snes_state *)machine->driver_data; |
| 2039 | 2048 | const address_space *cpu0space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM); |
| r7592 | r7593 | |
| 2141 | 2150 | } |
| 2142 | 2151 | |
| 2143 | 2152 | |
| 2144 | | /* should we treat this as nvram in MAME? */ |
| 2145 | 2153 | static DIRECT_UPDATE_HANDLER( spc_direct ) |
| 2146 | 2154 | { |
| 2147 | | direct->raw = direct->decrypted = spc_ram; |
| 2155 | direct->raw = direct->decrypted = spc_get_ram(devtag_get_device(space->machine, "spc700")); |
| 2148 | 2156 | return ~0; |
| 2149 | 2157 | } |
| 2150 | 2158 | |
| r7592 | r7593 | |
| 2156 | 2164 | |
| 2157 | 2165 | MACHINE_START( snes ) |
| 2158 | 2166 | { |
| 2167 | snes_state *state = (snes_state *)machine->driver_data; |
| 2159 | 2168 | snes_vram = auto_alloc_array(machine, UINT8, SNES_VRAM_SIZE); |
| 2160 | 2169 | snes_cgram = auto_alloc_array(machine, UINT16, SNES_CGRAM_SIZE/2); |
| 2161 | 2170 | snes_oam = auto_alloc_array(machine, UINT16, SNES_OAM_SIZE/2); |
| 2162 | 2171 | memory_set_direct_update_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), snes_direct); |
| 2163 | 2172 | memory_set_direct_update_handler(cputag_get_address_space(machine, "soundcpu", ADDRESS_SPACE_PROGRAM), spc_direct); |
| 2164 | 2173 | |
| 2174 | state->maincpu = devtag_get_device(machine, "maincpu"); |
| 2175 | state->soundcpu = devtag_get_device(machine, "soundcpu"); |
| 2176 | state->spc700 = devtag_get_device(machine, "spc700"); |
| 2177 | state->superfx = devtag_get_device(machine, "superfx"); |
| 2178 | |
| 2165 | 2179 | // power-on sets these registers like this |
| 2166 | 2180 | snes_ram[WRIO] = 0xff; |
| 2167 | 2181 | snes_ram[WRMPYA] = 0xff; |
src/mame/drivers/snesb.c
| r7592 | r7593 | |
| 140 | 140 | #include "cpu/spc700/spc700.h" |
| 141 | 141 | #include "cpu/g65816/g65816.h" |
| 142 | 142 | #include "includes/snes.h" |
| 143 | #include "audio/snes_snd.h" |
| 143 | 144 | |
| 144 | 145 | static INT8 *shared_ram; |
| 145 | 146 | static UINT8 ffight2b_coins; |
| r7592 | r7593 | |
| 271 | 272 | ADDRESS_MAP_END |
| 272 | 273 | |
| 273 | 274 | |
| 274 | | static READ8_HANDLER( spc_ram_100_r ) |
| 275 | static READ8_DEVICE_HANDLER( spc_ram_100_r ) |
| 275 | 276 | { |
| 276 | | return spc_ram_r(space, offset + 0x100); |
| 277 | return spc_ram_r(device, offset + 0x100); |
| 277 | 278 | } |
| 278 | 279 | |
| 279 | | static WRITE8_HANDLER( spc_ram_100_w ) |
| 280 | static WRITE8_DEVICE_HANDLER( spc_ram_100_w ) |
| 280 | 281 | { |
| 281 | | spc_ram_w(space, offset + 0x100, data); |
| 282 | spc_ram_w(device, offset + 0x100, data); |
| 282 | 283 | } |
| 283 | 284 | |
| 284 | 285 | static ADDRESS_MAP_START( spc_mem, ADDRESS_SPACE_PROGRAM, 8 ) |
| 285 | | AM_RANGE(0x0000, 0x00ef) AM_READWRITE(spc_ram_r, spc_ram_w) AM_BASE(&spc_ram) /* lower 32k ram */ |
| 286 | | AM_RANGE(0x00f0, 0x00ff) AM_READWRITE(spc_io_r, spc_io_w) /* spc io */ |
| 287 | | AM_RANGE(0x0100, 0xffff) AM_WRITE(spc_ram_100_w) |
| 288 | | AM_RANGE(0x0100, 0xffbf) AM_READ(spc_ram_100_r) |
| 289 | | AM_RANGE(0xffc0, 0xffff) AM_READ(spc_ipl_r) |
| 286 | AM_RANGE(0x0000, 0x00ef) AM_DEVREADWRITE("spc700", spc_ram_r, spc_ram_w) /* lower 32k ram */ |
| 287 | AM_RANGE(0x00f0, 0x00ff) AM_DEVREADWRITE("spc700", spc_io_r, spc_io_w) /* spc io */ |
| 288 | AM_RANGE(0x0100, 0xffff) AM_DEVWRITE("spc700", spc_ram_100_w) |
| 289 | AM_RANGE(0x0100, 0xffbf) AM_DEVREAD("spc700", spc_ram_100_r) |
| 290 | AM_RANGE(0xffc0, 0xffff) AM_DEVREAD("spc700", spc_ipl_r) |
| 290 | 291 | ADDRESS_MAP_END |
| 291 | 292 | |
| 292 | 293 | static INPUT_PORTS_START( snes_common ) |
| r7592 | r7593 | |
| 331 | 332 | PORT_BIT( 0xff, IP_ACTIVE_HIGH, IPT_UNUSED ) |
| 332 | 333 | |
| 333 | 334 | #ifdef SNES_LAYER_DEBUG |
| 334 | | PORT_START("INTERNAL") /* IN 8 : Internal switches */ |
| 335 | | PORT_CONFNAME( 0x1, 0x1, "Enforce 32 sprites/line" ) |
| 336 | | PORT_CONFSETTING( 0x0, DEF_STR( No ) ) |
| 337 | | PORT_CONFSETTING( 0x1, DEF_STR( Yes ) ) |
| 335 | PORT_START("DEBUG1") |
| 336 | PORT_CONFNAME( 0x70, 0x00, "Select OAM" ) |
| 337 | PORT_CONFSETTING( 0x00, "All" ) |
| 338 | PORT_CONFSETTING( 0x10, "OAM1 only" ) |
| 339 | PORT_CONFSETTING( 0x20, "OAM2 only" ) |
| 340 | PORT_CONFSETTING( 0x30, "OAM3 only" ) |
| 341 | PORT_CONFSETTING( 0x40, "OAM4 only" ) |
| 342 | PORT_CONFNAME( 0x80, 0x00, "Draw sprite in reverse order" ) |
| 343 | PORT_CONFSETTING( 0x00, DEF_STR( Off ) ) |
| 344 | PORT_CONFSETTING( 0x80, DEF_STR( On ) ) |
| 338 | 345 | |
| 339 | | PORT_START("DEBUG1") /* IN 9 : debug switches */ |
| 340 | | PORT_CONFNAME( 0x3, 0x0, "Browse tiles" ) |
| 341 | | PORT_CONFSETTING( 0x0, DEF_STR( Off ) ) |
| 342 | | PORT_CONFSETTING( 0x1, "2bpl" ) |
| 343 | | PORT_CONFSETTING( 0x2, "4bpl" ) |
| 344 | | PORT_CONFSETTING( 0x3, "8bpl" ) |
| 345 | | PORT_CONFNAME( 0xc, 0x0, "Browse maps" ) |
| 346 | | PORT_CONFSETTING( 0x0, DEF_STR( Off ) ) |
| 347 | | PORT_CONFSETTING( 0x4, "2bpl" ) |
| 348 | | PORT_CONFSETTING( 0x8, "4bpl" ) |
| 349 | | PORT_CONFSETTING( 0xc, "8bpl" ) |
| 346 | PORT_START("DEBUG2") |
| 347 | PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Toggle BG 1") PORT_CODE(KEYCODE_1_PAD) |
| 348 | PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Toggle BG 2") PORT_CODE(KEYCODE_2_PAD) |
| 349 | PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Toggle BG 3") PORT_CODE(KEYCODE_3_PAD) |
| 350 | PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Toggle BG 4") PORT_CODE(KEYCODE_4_PAD) |
| 351 | PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Toggle Objects") PORT_CODE(KEYCODE_5_PAD) |
| 352 | PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Toggle Main/Sub") PORT_CODE(KEYCODE_6_PAD) |
| 353 | PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Toggle Back col") PORT_CODE(KEYCODE_7_PAD) |
| 354 | PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Toggle Windows") PORT_CODE(KEYCODE_8_PAD) |
| 350 | 355 | |
| 351 | | PORT_START("DEBUG2") /* IN 10 : debug switches */ |
| 352 | | PORT_BIT( 0x1, IP_ACTIVE_HIGH, IPT_BUTTON7 ) PORT_NAME("Toggle BG 1") PORT_PLAYER(2) |
| 353 | | PORT_BIT( 0x2, IP_ACTIVE_HIGH, IPT_BUTTON8 ) PORT_NAME("Toggle BG 2") PORT_PLAYER(2) |
| 354 | | PORT_BIT( 0x4, IP_ACTIVE_HIGH, IPT_BUTTON9 ) PORT_NAME("Toggle BG 3") PORT_PLAYER(2) |
| 355 | | PORT_BIT( 0x8, IP_ACTIVE_HIGH, IPT_BUTTON10 ) PORT_NAME("Toggle BG 4") PORT_PLAYER(2) |
| 356 | | PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_BUTTON7 ) PORT_NAME("Toggle Objects") PORT_PLAYER(3) |
| 357 | | PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_BUTTON8 ) PORT_NAME("Toggle Main/Sub") PORT_PLAYER(3) |
| 358 | | PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_BUTTON9 ) PORT_NAME("Toggle Back col") PORT_PLAYER(3) |
| 359 | | PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_BUTTON10 ) PORT_NAME("Toggle Windows") PORT_PLAYER(3) |
| 356 | PORT_START("DEBUG3") |
| 357 | PORT_BIT( 0x1, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Pal prev") PORT_CODE(KEYCODE_B) |
| 358 | PORT_BIT( 0x2, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Pal next") PORT_CODE(KEYCODE_N) |
| 359 | PORT_BIT( 0x4, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Toggle Transparency") PORT_CODE(KEYCODE_9_PAD) |
| 360 | 360 | |
| 361 | | PORT_START("DEBUG3") /* IN 11 : debug input */ |
| 362 | | PORT_BIT( 0x1, IP_ACTIVE_HIGH, IPT_BUTTON9 ) PORT_NAME("Pal prev") |
| 363 | | PORT_BIT( 0x2, IP_ACTIVE_HIGH, IPT_BUTTON10 ) PORT_NAME("Pal next") |
| 364 | | PORT_BIT( 0x4, IP_ACTIVE_HIGH, IPT_BUTTON7 ) PORT_NAME("Toggle Transparency") PORT_PLAYER(4) |
| 361 | PORT_START("DEBUG4") |
| 362 | PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Toggle Mode 0 draw") |
| 363 | PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Toggle Mode 1 draw") |
| 364 | PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Toggle Mode 2 draw") |
| 365 | PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Toggle Mode 3 draw") |
| 366 | PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Toggle Mode 4 draw") |
| 367 | PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Toggle Mode 5 draw") |
| 368 | PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Toggle Mode 6 draw") |
| 369 | PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Toggle Mode 7 draw") |
| 365 | 370 | #endif |
| 366 | 371 | INPUT_PORTS_END |
| 367 | 372 | |
| r7592 | r7593 | |
| 506 | 511 | |
| 507 | 512 | /* sound hardware */ |
| 508 | 513 | MDRV_SPEAKER_STANDARD_STEREO("lspeaker", "rspeaker") |
| 509 | | |
| 510 | | MDRV_SOUND_ADD("snes", SNES, 0) |
| 514 | MDRV_SOUND_ADD("spc700", SNES, 0) |
| 511 | 515 | MDRV_SOUND_ROUTE(0, "lspeaker", 1.00) |
| 512 | 516 | MDRV_SOUND_ROUTE(1, "rspeaker", 1.00) |
| 513 | 517 | MACHINE_DRIVER_END |
| r7592 | r7593 | |
| 815 | 819 | ROM_END |
| 816 | 820 | |
| 817 | 821 | |
| 818 | | GAME( 199?, kinstb, 0, kinstb, kinstb, kinstb, ROT0, "bootleg", "Killer Instinct (SNES bootleg)", GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS ) |
| 819 | | GAME( 1996, ffight2b, 0, kinstb, ffight2b, ffight2b, ROT0, "bootleg", "Final Fight 2 (SNES bootleg)", GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS ) |
| 820 | | GAME( 1997, sblast2b, 0, kinstb, sblast2b, sblast2b, ROT0, "bootleg", "Sonic Blast Man 2 Special Turbo (SNES bootleg)", GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS) |
| 821 | | GAME( 1996, iron, 0, kinstb, iron, iron, ROT0, "bootleg", "Iron (SNES bootleg)", GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS ) |
| 822 | | GAME( 1996, denseib, 0, kinstb, denseib, denseib, ROT0, "bootleg", "Ghost Chaser Densei (SNES bootleg)", GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS ) |
| 823 | | GAME( 1996, endless, 0, kinstb, iron, endless, ROT0, "bootleg", "Gundam Wing: Endless Duel (SNES bootleg)", GAME_NOT_WORKING | GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS ) |
| 822 | GAME( 199?, kinstb, 0, kinstb, kinstb, kinstb, ROT0, "bootleg", "Killer Instinct (SNES bootleg)", GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS ) |
| 823 | GAME( 1996, ffight2b, 0, kinstb, ffight2b, ffight2b, ROT0, "bootleg", "Final Fight 2 (SNES bootleg)", GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS ) |
| 824 | GAME( 1997, sblast2b, 0, kinstb, sblast2b, sblast2b, ROT0, "bootleg", "Sonic Blast Man 2 Special Turbo (SNES bootleg)", GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS) |
| 825 | GAME( 1996, iron, 0, kinstb, iron, iron, ROT0, "bootleg", "Iron (SNES bootleg)", GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS ) |
| 826 | GAME( 1996, denseib, 0, kinstb, denseib, denseib, ROT0, "bootleg", "Ghost Chaser Densei (SNES bootleg)", GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS ) |
| 827 | GAME( 1996, endless, 0, kinstb, iron, endless, ROT0, "bootleg", "Gundam Wing: Endless Duel (SNES bootleg)", GAME_NOT_WORKING | GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS ) |
src/mame/drivers/nss.c
| r7592 | r7593 | |
| 292 | 292 | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
| 293 | 293 | |
| 294 | 294 | ***************************************************************************/ |
| 295 | |
| 295 | 296 | #include "emu.h" |
| 296 | 297 | #include "cpu/spc700/spc700.h" |
| 297 | 298 | #include "cpu/g65816/g65816.h" |
| 298 | 299 | #include "includes/snes.h" |
| 300 | #include "audio/snes_snd.h" |
| 299 | 301 | |
| 300 | 302 | static ADDRESS_MAP_START( snes_map, ADDRESS_SPACE_PROGRAM, 8) |
| 301 | 303 | AM_RANGE(0x000000, 0x2fffff) AM_READWRITE(snes_r_bank1, snes_w_bank1) /* I/O and ROM (repeats for each bank) */ |
| r7592 | r7593 | |
| 308 | 310 | AM_RANGE(0xc00000, 0xffffff) AM_READWRITE(snes_r_bank7, snes_w_bank7) /* Mirror and ROM */ |
| 309 | 311 | ADDRESS_MAP_END |
| 310 | 312 | |
| 311 | | static READ8_HANDLER( spc_ram_100_r ) |
| 313 | static READ8_DEVICE_HANDLER( spc_ram_100_r ) |
| 312 | 314 | { |
| 313 | | return spc_ram_r(space, offset + 0x100); |
| 315 | return spc_ram_r(device, offset + 0x100); |
| 314 | 316 | } |
| 315 | 317 | |
| 316 | | static WRITE8_HANDLER( spc_ram_100_w ) |
| 318 | static WRITE8_DEVICE_HANDLER( spc_ram_100_w ) |
| 317 | 319 | { |
| 318 | | spc_ram_w(space, offset + 0x100, data); |
| 320 | spc_ram_w(device, offset + 0x100, data); |
| 319 | 321 | } |
| 320 | 322 | |
| 321 | 323 | static ADDRESS_MAP_START( spc_mem, ADDRESS_SPACE_PROGRAM, 8 ) |
| 322 | | AM_RANGE(0x0000, 0x00ef) AM_READWRITE(spc_ram_r, spc_ram_w) AM_BASE(&spc_ram) /* lower 32k ram */ |
| 323 | | AM_RANGE(0x00f0, 0x00ff) AM_READWRITE(spc_io_r, spc_io_w) /* spc io */ |
| 324 | | AM_RANGE(0x0100, 0xffff) AM_WRITE(spc_ram_100_w) |
| 325 | | AM_RANGE(0x0100, 0xffbf) AM_READ(spc_ram_100_r) |
| 326 | | AM_RANGE(0xffc0, 0xffff) AM_READ(spc_ipl_r) |
| 324 | AM_RANGE(0x0000, 0x00ef) AM_DEVREADWRITE("spc700", spc_ram_r, spc_ram_w) /* lower 32k ram */ |
| 325 | AM_RANGE(0x00f0, 0x00ff) AM_DEVREADWRITE("spc700", spc_io_r, spc_io_w) /* spc io */ |
| 326 | AM_RANGE(0x0100, 0xffff) AM_DEVWRITE("spc700", spc_ram_100_w) |
| 327 | AM_RANGE(0x0100, 0xffbf) AM_DEVREAD("spc700", spc_ram_100_r) |
| 328 | AM_RANGE(0xffc0, 0xffff) AM_DEVREAD("spc700", spc_ipl_r) |
| 327 | 329 | ADDRESS_MAP_END |
| 328 | 330 | |
| 329 | 331 | static INPUT_PORTS_START( snes ) |
| r7592 | r7593 | |
| 416 | 418 | |
| 417 | 419 | /* sound hardware */ |
| 418 | 420 | MDRV_SPEAKER_STANDARD_STEREO("lspeaker", "rspeaker") |
| 419 | | |
| 420 | | MDRV_SOUND_ADD("snes", SNES, 0) |
| 421 | MDRV_SOUND_ADD("spc700", SNES, 0) |
| 421 | 422 | MDRV_SOUND_ROUTE(0, "lspeaker", 1.00) |
| 422 | 423 | MDRV_SOUND_ROUTE(1, "rspeaker", 1.00) |
| 423 | 424 | MACHINE_DRIVER_END |
| r7592 | r7593 | |
| 574 | 575 | ROM_LOAD( "st.ic3", 0x0000, 0x8000, CRC(8880596e) SHA1(ec6d68fc2f51f7d94f496cd72cf898db65324542) ) |
| 575 | 576 | ROM_END |
| 576 | 577 | |
| 577 | | GAME( 199?, nss, 0, snes, snes, snes, ROT0, "Nintendo", "Nintendo Super System BIOS", GAME_IS_BIOS_ROOT ) |
| 578 | | GAME( 1992, nss_actr, nss, snes, snes, snes, ROT0, "Enix", "Act Raiser (Nintendo Super System)", GAME_NOT_WORKING | GAME_IMPERFECT_SOUND ) // time broken (?) |
| 579 | | GAME( 1992, nss_adam, nss, snes, snes, snes, ROT0, "Ocean", "The Addams Family (Nintendo Super System)", GAME_NOT_WORKING | GAME_IMPERFECT_SOUND ) |
| 580 | | GAME( 1992, nss_aten, nss, snes, snes, snes, ROT0, "Absolute Entertainment Inc.","David Crane's Amazing Tennis (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) // gfx problems with net |
| 581 | | GAME( 1992, nss_con3, nss, snes, snes, snes, ROT0, "Konami", "Contra 3: The Alien Wars (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) |
| 582 | | GAME( 1992, nss_lwep, nss, snes, snes, snes, ROT0, "Ocean", "Lethal Weapon (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) |
| 583 | | GAME( 1992, nss_ncaa, nss, snes, snes, snes, ROT0, "Sculptured Software Inc.", "NCAA Basketball (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) |
| 584 | | GAME( 1992, nss_rob3, nss, snes, snes, snes, ROT0, "Ocean", "Robocop 3 (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) // invisible enemy? gameplay prob? |
| 585 | | GAME( 1992, nss_skin, nss, snes, snes, snes, ROT0, "Irem", "Skins Game (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) // no controls |
| 586 | | GAME( 1992, nss_ssoc, nss, snes, snes, snes, ROT0, "Human Inc.", "Super Soccer (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) // some gfx issues |
| 587 | | GAME( 1991, nss_smw, nss, snes, snes, snes, ROT0, "Nintendo", "Super Mario World (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) |
| 588 | | GAME( 1991, nss_fzer, nss, snes, snes, snes, ROT0, "Nintendo", "F-Zero (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) |
| 589 | | GAME( 1991, nss_sten, nss, snes, snes, snes, ROT0, "Nintendo", "Super Tennis (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) |
| 578 | GAME( 199?, nss, 0, snes, snes, snes, ROT0, "Nintendo", "Nintendo Super System BIOS", GAME_IS_BIOS_ROOT ) |
| 579 | GAME( 1992, nss_actr, nss, snes, snes, snes, ROT0, "Enix", "Act Raiser (Nintendo Super System)", GAME_NOT_WORKING | GAME_IMPERFECT_SOUND ) // time broken (?) |
| 580 | GAME( 1992, nss_adam, nss, snes, snes, snes, ROT0, "Ocean", "The Addams Family (Nintendo Super System)", GAME_NOT_WORKING | GAME_IMPERFECT_SOUND ) |
| 581 | GAME( 1992, nss_aten, nss, snes, snes, snes, ROT0, "Absolute Entertainment Inc.", "David Crane's Amazing Tennis (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) // gfx problems with net |
| 582 | GAME( 1992, nss_con3, nss, snes, snes, snes, ROT0, "Konami", "Contra 3: The Alien Wars (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) |
| 583 | GAME( 1992, nss_lwep, nss, snes, snes, snes, ROT0, "Ocean", "Lethal Weapon (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) |
| 584 | GAME( 1992, nss_ncaa, nss, snes, snes, snes, ROT0, "Sculptured Software Inc.", "NCAA Basketball (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) |
| 585 | GAME( 1992, nss_rob3, nss, snes, snes, snes, ROT0, "Ocean", "Robocop 3 (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) // invisible enemy? gameplay prob? |
| 586 | GAME( 1992, nss_skin, nss, snes, snes, snes, ROT0, "Irem", "Skins Game (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) // no controls |
| 587 | GAME( 1992, nss_ssoc, nss, snes, snes, snes, ROT0, "Human Inc.", "Super Soccer (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) // some gfx issues |
| 588 | GAME( 1991, nss_smw, nss, snes, snes, snes, ROT0, "Nintendo", "Super Mario World (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) |
| 589 | GAME( 1991, nss_fzer, nss, snes, snes, snes, ROT0, "Nintendo", "F-Zero (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) |
| 590 | GAME( 1991, nss_sten, nss, snes, snes, snes, ROT0, "Nintendo", "Super Tennis (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) |