Previous 15994 Revisions Next

r7593 Wednesday 17th March, 2010 at 09:35:28 UTC by Fabio Priuli
[SNES] Updated the snes sound device to use device handlers and to store its internals in a struct
[src/emu/cpu/spc700]spc700.c
[src/mame]mame.mak
[src/mame/audio]snes.c snes_snd.c* snes_snd.h*
[src/mame/drivers]nss.c snesb.c
[src/mame/includes]snes.h
[src/mame/machine]snes.c
[src/mess]mess.mak
[src/mess/drivers]snes.c

src/mame/audio/snes.c
r7592r7593
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
32static struct
33{
34   UINT8 enabled;
35   UINT16 counter;
36   emu_timer *timer;
37} timers[3];
38static sound_stream *channel;
39static UINT8 DSPregs[256];      /* DSP registers */
40static UINT8 snes_ipl_region[64];   /* SPC top 64 bytes */
41
42static 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
128static int                     keyed_on;
129static int                     keys;       /* 8-bits for 8 voices          */
130static voice_state_type        voice_state[ 8 ];
131
132/* Noise stuff */
133static int                      noise_cnt;
134static int                     noise_lev;
135
136/* These are for the FIR echo filter */
137#ifndef NO_ECHO
138static short            FIRlbuf[ 8 ];
139static short            FIRrbuf[ 8 ];
140static int              FIRptr;
141static 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
149static const int               TS_CYC = CPU_RATE / SAMP_FREQ;
150#endif
151
152/* Ptrs to Gaussian table */
153static const int *const G1 = &gauss[ 256 ];
154static const int *const G2 = &gauss[ 512 ];
155static const int *const G3 = &gauss[ 255 ];
156static const int *const G4 = &gauss[ -1  ];
157
158static 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). */
163static const int        CNT_INIT = 0x7800;
164static 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
186static int AdvanceEnvelope          /* Run envelope step & retn ENVX*/
187    (
188    int                 v           /* Voice to process envelope for*/
189    );
190
191static STREAM_UPDATE( snes_sh_update );
192
193/* Privately shared functions (for internal library use only) */
194
195/***** DSP_Reset *****/
196
197static void DSP_Reset                      /* Reset emulated DSP           */
198    ( void )
199{
200int                     i;
201
202#ifdef DEBUG
203fprintf( stderr,"DSP_Reset\n" );
204#endif
205
206for( 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
216FIRptr   = 0;
217echo_ptr = 0;
218#endif
219keys             = 0;
220keyed_on         = 0;
221noise_cnt        = 0;
222noise_lev        = 0x4000;
223DSPregs[ 0x6C ] |= 0xE0;
224DSPregs[ 0x4C ]  = 0;
225DSPregs[ 0x5C ]  = 0;
226
227}   /* DSP_Reset() */
228
229
230/***** DSP_Update *****/
231
232static void DSP_Update                     /* Mix one sample of audio      */
233    (
234    short *             sound_ptr   /* Pointer to mix audio into    */
235    )
236{
237int                     V;
238#ifndef NO_ECHO
239int                     echo_base;
240int                     echol;
241int                     echor;
242#endif
243int                     envx;
244int                     m;
245int                     outl;
246int                     outr;
247signed long             outx;       /* Smpl height (must be signed) */
248src_dir_type *          sd;
249int                     v;
250int                     vl;
251voice_state_type *      vp;
252int                     vr;
253
254sd = ( src_dir_type * )&spc_ram[ ( int )DSPregs[ 0x5D ] << 8 ];
255
256/* Check for reset */
257if( 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
268DSPregs[ 0x4C ] &= mask;
269#endif
270
271/* Keying on a voice resets that bit in ENDX */
272DSPregs[ 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. */
277outx = 0;
278/* Same table for noise and envelope */
279noise_cnt -= ENVCNT[ DSPregs[ 0x6C ] & 0x1F ];
280if( noise_cnt <= 0 )
281    {
282    noise_cnt = CNT_INIT;
283    noise_lev = ( ( ( noise_lev << 13 ) ^ ( noise_lev << 14 ) ) & 0x4000 )
284              | ( noise_lev >> 1                                         );
285    }
286outl  = 0;
287outr  = 0;
288#ifndef NO_ECHO
289echol = 0;
290echor = 0;
291#endif
292for( 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    }
635outl = ( outl * ( signed char )DSPregs[ 0x0C ] ) >> 7;
636outr = ( 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
642fprintf(
643       stderr,
644       "Echo delay=%dms, feedback=%d%%\n",
645       DSPregs[ 0x7D ] * 16,
646       ( ( signed char )DSPregs[ 0x0D ] * 100 ) / 0x7F
647       );
648#endif
649
650echo_base = ( ( DSPregs[ 0x6D ] << 8 ) + echo_ptr ) & 0xFFFF;
651FIRlbuf[ FIRptr ]
652  = ( signed short )LEtoME16(
653                            *( UINT16 * )
654                              &spc_ram[ echo_base ]
655                            );
656FIRrbuf[ 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. */
663vl     = FIRlbuf[ FIRptr ] * ( signed char )DSPregs[ 0x7F ];
664vr     = FIRrbuf[ FIRptr ] * ( signed char )DSPregs[ 0x7F ];
665FIRptr = ( FIRptr + 1 ) & 7;
666vl    += FIRlbuf[ FIRptr ] * ( signed char )DSPregs[ 0x6F ];
667vr    += FIRrbuf[ FIRptr ] * ( signed char )DSPregs[ 0x6F ];
668FIRptr = ( FIRptr + 1 ) & 7;
669vl    += FIRlbuf[ FIRptr ] * ( signed char )DSPregs[ 0x5F ];
670vr    += FIRrbuf[ FIRptr ] * ( signed char )DSPregs[ 0x5F ];
671FIRptr = ( FIRptr + 1 ) & 7;
672vl    += FIRlbuf[ FIRptr ] * ( signed char )DSPregs[ 0x4F ];
673vr    += FIRrbuf[ FIRptr ] * ( signed char )DSPregs[ 0x4F ];
674FIRptr = ( FIRptr + 1 ) & 7;
675vl    += FIRlbuf[ FIRptr ] * ( signed char )DSPregs[ 0x3F ];
676vr    += FIRrbuf[ FIRptr ] * ( signed char )DSPregs[ 0x3F ];
677FIRptr = ( FIRptr + 1 ) & 7;
678vl    += FIRlbuf[ FIRptr ] * ( signed char )DSPregs[ 0x2F ];
679vr    += FIRrbuf[ FIRptr ] * ( signed char )DSPregs[ 0x2F ];
680FIRptr = ( FIRptr + 1 ) & 7;
681vl    += FIRlbuf[ FIRptr ] * ( signed char )DSPregs[ 0x1F ];
682vr    += FIRrbuf[ FIRptr ] * ( signed char )DSPregs[ 0x1F ];
683FIRptr = ( FIRptr + 1 ) & 7;
684vl    += FIRlbuf[ FIRptr ] * ( signed char )DSPregs[ 0x0F ];
685vr    += FIRrbuf[ FIRptr ] * ( signed char )DSPregs[ 0x0F ];
686
687#ifdef DBG_ECHO
688fprintf(
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. */
704outl += vl * ( signed char )DSPregs[ 0x2C ] >> 14;
705outr += vr * ( signed char )DSPregs[ 0x3C ] >> 14;
706
707if( !( 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
746echo_ptr += 2 * sizeof( short );
747if( echo_ptr >= ( ( DSPregs[ 0x7D ] & 0xF ) << 11 ) )
748    {
749    echo_ptr = 0;
750    }
751#endif                              /* !defined( NO_ECHO )          */
752
753if( 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
803static int AdvanceEnvelope          /* Run envelope step & retn ENVX*/
804    (
805    int                 v           /* Voice to process envelope for*/
806    )
807{
808int                     envx;
809int                     cnt;
810int                     adsr1;
811int                     t;
812
813envx = voice_state[ v ].envx;
814
815if( 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
845cnt   = voice_state[ v ].envcnt;
846adsr1 = DSPregs[ ( v << 4 ) + 5 ];
847if( 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    }
966else
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
1115voice_state[ v ].envcnt   = cnt;
1116DSPregs[ ( v << 4 ) + 8 ] = envx >> 4;
1117
1118return( envx );
1119
1120}    /* AdvanceEnvelope() */
1121
1122static 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
1135static 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
1169DEVICE_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
1183static 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
1203static 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
1217static 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 ***************************/
1234READ8_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
1272WRITE8_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
1351READ8_HANDLER( spc_ram_r )
1352{
1353   return spc_ram[offset];
1354}
1355
1356WRITE8_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
1367READ8_HANDLER( spc_ipl_r )
1368{
1369   return snes_ipl_region[offset];
1370}
1371
src/mame/audio/snes_snd.c
r0r7593
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
36static 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 */
125static const int *const G1 = &gauss[256];
126static const int *const G2 = &gauss[512];
127static const int *const G3 = &gauss[255];
128static const int *const G4 = &gauss[-1];
129
130static 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). */
135static const int CNT_INIT = 0x7800;
136static 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
154static int advance_envelope( running_device *device, int v);
155
156/***************************************************************************
157 TYPE DEFINITIONS
158***************************************************************************/
159
160typedef enum                        /* ADSR state type              */
161{
162   ATTACK,
163   DECAY,
164   SUSTAIN,
165   RELEASE
166} env_state_t32;
167
168typedef 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
189typedef 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
196typedef struct _snes_sound_state  snes_sound_state;
197struct _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
234INLINE 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
254static 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
294static 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
775static 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
1023static 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
1037static 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
1058static 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
1073static 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
1095READ8_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
1135WRITE8_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
1206READ8_DEVICE_HANDLER( spc_ram_r )
1207{
1208   snes_sound_state *spc700 = get_safe_token(device);
1209   return spc700->ram[offset];
1210}
1211
1212WRITE8_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
1223READ8_DEVICE_HANDLER( spc_ipl_r )
1224{
1225   snes_sound_state *spc700 = get_safe_token(device);
1226   return spc700->ipl_region[offset];
1227}
1228
1229
1230READ8_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
1239WRITE8_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
1248UINT8 *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
1259static 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
1307static 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
1333static 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
1356static 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/audio/snes_snd.h
r0r7593
1/*****************************************************************************
2 *
3 * audio/snes_spc.h
4 *
5 ****************************************************************************/
6
7#ifndef __SNES_SPC_H__
8#define __SNES_SPC_H__
9
10#include "devcb.h"
11
12#define SNES_SPCRAM_SIZE      0x10000
13
14
15/***************************************************************************
16    INFO PROTOTYPES
17***************************************************************************/
18
19DEVICE_GET_INFO( snes_sound );
20
21/***************************************************************************
22    DEVICE CONFIGURATION MACROS
23***************************************************************************/
24
25#define SNES_SPC DEVICE_GET_INFO_NAME( snes_sound )
26#define SOUND_SNES SNES_SPC
27
28/***************************************************************************
29    I/O PROTOTYPES
30***************************************************************************/
31
32READ8_DEVICE_HANDLER( spc_io_r );
33WRITE8_DEVICE_HANDLER( spc_io_w );
34READ8_DEVICE_HANDLER( spc_ram_r );
35WRITE8_DEVICE_HANDLER( spc_ram_w );
36READ8_DEVICE_HANDLER( spc_ipl_r );
37READ8_DEVICE_HANDLER( spc_port_out );
38WRITE8_DEVICE_HANDLER( spc_port_in );
39
40UINT8 *spc_get_ram(running_device *device);
41
42#endif /* __SNES_SPC_H__ */
src/mame/machine/snes.c
r7592r7593
1515#define __MACHINE_SNES_C
1616
1717#include "emu.h"
18#include "includes/snes.h"
1918#include "cpu/superfx/superfx.h"
2019#include "cpu/g65816/g65816.h"
20#include "includes/snes.h"
21#include "audio/snes_snd.h"
2122
2223
2324/* -- Globals -- */
2425UINT8  *snes_ram = NULL;      /* 65816 ram */
25UINT8  *spc_ram = NULL;         /* spc700 ram */
2626UINT8  *snes_vram = NULL;      /* Video RAM (Should be 16-bit, but it's easier this way) */
2727UINT16 *snes_cgram = NULL;      /* Colour RAM */
2828UINT16 *snes_oam = NULL;      /* Object Attribute Memory */
29UINT8  spc_port_in[4];   /* Port for sending data to the SPC700 */
30UINT8  spc_port_out[4];   /* Port for receiving data from the SPC700 */
3129
3230UINT8 snes_has_addon_chip;
3331UINT32 snes_rom_size;
r7592r7593
8078   snes_state *state = (snes_state *)machine->driver_data;
8179
8280   // 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);
8482
8583   // don't happen again
8684   timer_adjust_oneshot(state->nmi_timer, attotime_never, 0);
r7592r7593
9492   // (don't need to switch to the 65816 context, we don't do anything dependant on it)
9593   snes_latch_counters(machine);
9694   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);
9896
9997   // don't happen again
10098   timer_adjust_oneshot(state->hirq_timer, attotime_never, 0);
r7592r7593
107105
108106static TIMER_CALLBACK( snes_reset_oam_address )
109107{
108   snes_state *state = (snes_state *)machine->driver_data;
110109   // 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);
112111
113112   if (!(snes_ppu.screen_disabled)) //Reset OAM address, byuu says it happens at H=10
114113   {
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);
117116      snes_ppu.oam.first_sprite = snes_ppu.oam.priority_rotation ? (snes_ppu.oam.address >> 1) & 127 : 0;
118117   }
119118}
r7592r7593
141140         snes_ram[TIMEUP] = 0x80;   /* Indicate that irq occured */
142141         // IRQ latches the counters, do it now
143142         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 );
145144      }
146145   }
147146   /* Horizontal IRQ timer */
r7592r7593
184183      if (snes_ram[NMITIMEN] & 0x80)   /* NMI only signaled if this bit set */
185184      {
186185         // 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);
188187      }
189188   }
190189
r7592r7593
208207      snes_ram[STAT78] ^= 0x80;      /* Toggle field flag */
209208      snes_ppu.stat77_flags &= 0x3f;   /* Clear Time Over and Range Over bits */
210209
211      cputag_set_input_line(machine, "maincpu", G65816_LINE_NMI, CLEAR_LINE );
210      cpu_set_input_line(state->maincpu, G65816_LINE_NMI, CLEAR_LINE );
212211   }
213212
214213   timer_adjust_oneshot(state->scanline_timer, attotime_never, 0);
r7592r7593
219218static TIMER_CALLBACK( snes_hblank_tick )
220219{
221220   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);
223222   int nextscan;
224223
225224   snes_ppu.beam.current_vert = video_screen_get_vpos(machine->primary_screen);
r7592r7593
352351   // APU is mirrored from 2140 to 217f
353352   if (offset >= APU00 && offset < WMDATA)
354353   {
355      return spc_port_out[offset & 0x3];
354      return spc_port_out(state->spc700, offset & 0x3);
356355   }
357356
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)
359358   {
360359      if (offset >= 0x3000 && offset < 0x3300)
361360      {
362         return superfx_mmio_read(devtag_get_device(space->machine, "superfx"), offset);
361         return superfx_mmio_read(state->superfx, offset);
363362      }
364363   }
365364   else if (snes_has_addon_chip == HAS_RTC)
r7592r7593
624623         return value;
625624      case TIMEUP:      /* IRQ flag by H/V count timer */
626625         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 );
628627         snes_ram[TIMEUP] = 0;
629628         return value;
630629      case HVBJOY:      /* H/V blank and joypad controller enable */
r7592r7593
714713   // APU is mirrored from 2140 to 217f
715714   if (offset >= APU00 && offset < WMDATA)
716715   {
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);
719718      cpuexec_boost_interleave(space->machine, attotime_zero, ATTOTIME_IN_USEC(20));
720719      return;
721720   }
722721
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)
724723   {
725724      if (offset >= 0x3000 && offset < 0x3300)
726725      {
727         superfx_mmio_write(devtag_get_device(space->machine, "superfx"), offset, data);
726         superfx_mmio_write(state->superfx, offset, data);
728727         return;
729728      }
730729   }
r7592r7593
12421241         break;
12431242      case WRMPYB:   /* Multiplier B */
12441243         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);
12461245         {
12471246            UINT32 c = snes_ram[WRMPYA] * snes_ram[WRMPYB];
12481247            snes_ram[RDMPYL] = c & 0xff;
r7592r7593
12541253         break;
12551254      case WRDVDD:   /* Divisor */
12561255         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);
12581257         {
12591258            UINT16 value, dividend, remainder;
12601259            dividend = remainder = 0;
r7592r7593
12911290      case MEMSEL:   /* Access cycle designation in memory (2) area */
12921291         /* FIXME: Need to adjust the speed only during access of banks 0x80+
12931292             * 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 );
12951294#ifdef SNES_DBG_REG_W
12961295         if ((data & 0x1) != (snes_ram[MEMSEL] & 0x1))
12971296            mame_printf_debug( "CPU speed: %f Mhz\n", (data & 0x1) ? 3.58 : 2.68 );
r7592r7593
13471346
13481347WRITE_LINE_DEVICE_HANDLER( snes_extern_irq_w )
13491348{
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);
13511351}
13521352
13531353/*************************************
r7592r7593
14281428/* 0x000000 - 0x2fffff */
14291429READ8_HANDLER( snes_r_bank1 )
14301430{
1431   snes_state *state = (snes_state *)space->machine->driver_data;
14311432   UINT8 value;
14321433   UINT16 address = offset & 0xffff;
14331434
r7592r7593
14371438      value = snes_r_io(space, address);
14381439   else if (address < 0x8000)
14391440   {
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)
14411442         value = snes_ram[0x700000 + (address & 0x1fff)];
14421443      else if (snes_has_addon_chip == HAS_OBC1)
14431444         value = obc1_read(space, offset);
r7592r7593
15461547/* 0x600000 - 0x6fffff */
15471548READ8_HANDLER( snes_r_bank4 )
15481549{
1550   snes_state *state = (snes_state *)space->machine->driver_data;
15491551   UINT8 value = 0xff;
15501552   UINT16 address = offset & 0xffff;
15511553
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)
15531555   {
15541556      value = snes_ram[0x600000 + offset];
15551557   }
r7592r7593
15791581/* 0x700000 - 0x7dffff */
15801582READ8_HANDLER( snes_r_bank5 )
15811583{
1584   snes_state *state = (snes_state *)space->machine->driver_data;
15821585   UINT8 value;
15831586   UINT16 address = offset & 0xffff;
15841587
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)
15861589   {
15871590      value = snes_ram[0x700000 + offset];
15881591   }
r7592r7593
16101613/* 0x800000 - 0xbfffff */
16111614READ8_HANDLER( snes_r_bank6 )
16121615{
1616   snes_state *state = (snes_state *)space->machine->driver_data;
16131617   UINT8 value = 0;
16141618   UINT16 address = offset & 0xffff;
16151619
16161620   if (address < 0x8000)
16171621   {
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)
16191623         logerror( "snes_r_bank6 hit in Super FX mode, please fix me\n" );
16201624      else if (snes_cart.mode != SNES_MODE_25)
16211625         value = memory_read_byte(space, offset);
r7592r7593
16571661/* 0xc00000 - 0xffffff */
16581662READ8_HANDLER( snes_r_bank7 )
16591663{
1664   snes_state *state = (snes_state *)space->machine->driver_data;
16601665   UINT8 value = 0;
16611666   UINT16 address = offset & 0xffff;
16621667
r7592r7593
16641669   {
16651670      return sdd1_read(space->machine, offset);
16661671   }
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)
16681673   {
16691674      logerror( "snes_r_bank7 hit in Super FX mode, please fix me\n" );
16701675   }
r7592r7593
17711776/* 0x600000 - 0x6fffff */
17721777WRITE8_HANDLER( snes_w_bank4 )
17731778{
1779   snes_state *state = (snes_state *)space->machine->driver_data;
17741780   UINT16 address = offset & 0xffff;
17751781
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)
17771783   {
17781784      snes_ram[0x600000 + offset] = data;
17791785   }
r7592r7593
18051811/* 0x700000 - 0x7dffff */
18061812WRITE8_HANDLER( snes_w_bank5 )
18071813{
1814   snes_state *state = (snes_state *)space->machine->driver_data;
18081815   UINT16 address = offset & 0xffff;
18091816
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)
18111818   {
18121819      snes_ram[0x700000 + offset] = data;
18131820   }
r7592r7593
18331840/* 0x800000 - 0xbfffff */
18341841WRITE8_HANDLER( snes_w_bank6 )
18351842{
1843   snes_state *state = (snes_state *)space->machine->driver_data;
18361844   UINT16 address = offset & 0xffff;
18371845
18381846   if (address < 0x8000)
18391847   {
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)
18411849      {
18421850         logerror( "snes_w_bank6 hit (RAM) in Super FX mode, please fix me\n" );
18431851      }
r7592r7593
18791887      DSP3_write(address, data);
18801888   else if ((snes_has_addon_chip == HAS_DSP4) && (offset >= 0x300000) && (address < 0xc000))
18811889      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)
18831891      logerror( "snes_w_bank6 hit (ROM) in Super FX mode, please fix me\n" );
18841892   else
18851893      logerror("Attempt to write to ROM address: %X\n", offset + 0x800000);
r7592r7593
18891897/* 0xc00000 - 0xffffff */
18901898WRITE8_HANDLER( snes_w_bank7 )
18911899{
1900   snes_state *state = (snes_state *)space->machine->driver_data;
18921901   UINT16 address = offset & 0xffff;
18931902
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)
18951904   {
18961905      if (offset >= 0x200000)
18971906      {
r7592r7593
20332042   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);
20342043}
20352044
2036static void snes_init_ram(running_machine *machine)
2045static void snes_init_ram( running_machine *machine )
20372046{
20382047   snes_state *state = (snes_state *)machine->driver_data;
20392048   const address_space *cpu0space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
r7592r7593
21412150}
21422151
21432152
2144/* should we treat this as nvram in MAME? */
21452153static DIRECT_UPDATE_HANDLER( spc_direct )
21462154{
2147   direct->raw = direct->decrypted = spc_ram;
2155   direct->raw = direct->decrypted = spc_get_ram(devtag_get_device(space->machine, "spc700"));
21482156   return ~0;
21492157}
21502158
r7592r7593
21562164
21572165MACHINE_START( snes )
21582166{
2167   snes_state *state = (snes_state *)machine->driver_data;
21592168   snes_vram = auto_alloc_array(machine, UINT8, SNES_VRAM_SIZE);
21602169   snes_cgram = auto_alloc_array(machine, UINT16, SNES_CGRAM_SIZE/2);
21612170   snes_oam = auto_alloc_array(machine, UINT16, SNES_OAM_SIZE/2);
21622171   memory_set_direct_update_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), snes_direct);
21632172   memory_set_direct_update_handler(cputag_get_address_space(machine, "soundcpu", ADDRESS_SPACE_PROGRAM), spc_direct);
21642173
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
21652179   // power-on sets these registers like this
21662180   snes_ram[WRIO] = 0xff;
21672181   snes_ram[WRMPYA] = 0xff;
src/mame/includes/snes.h
r7592r7593
2323#define DOTCLK_NTSC   (MCLK_NTSC/4)
2424#define DOTCLK_PAL   (MCLK_PAL/4)
2525
26#define SNES_LAYER_DEBUG 
26#define SNES_LAYER_DEBUG  1
2727
2828/* Debug definitions */
2929#ifdef MAME_DEBUG
r7592r7593
5252#define SNES_VRAM_SIZE        0x20000   /* 128kb of video ram */
5353#define SNES_CGRAM_SIZE       0x202      /* 256 16-bit colours + 1 tacked on 16-bit colour for fixed colour */
5454#define SNES_OAM_SIZE         0x440      /* 1088 bytes of Object Attribute Memory */
55#define SNES_SPCRAM_SIZE      0x10000   /* 64kb of spc700 ram */
5655#define SNES_EXROM_START      0x1000000
5756#define FIXED_COLOUR          256      /* Position in cgram for fixed colour */
5857/* Definitions for PPU Memory-Mapped registers */
r7592r7593
417416   /* input callbacks (to allow MESS to have its own input handlers) */
418417   snes_io_read          io_read;
419418   snes_oldjoy_read      oldjoy1_read, oldjoy2_read;
419
420   /* devices */
421   running_device *maincpu;
422   running_device *soundcpu;
423   running_device *spc700;
424   running_device *superfx;
420425};
421426
422427/* Special chips, checked at init and used in memory handlers */
r7592r7593
506511extern UINT16 *snes_cgram;         /* Colour RAM */
507512extern UINT16 *snes_oam;         /* Object Attribute Memory */
508513extern UINT8  *snes_ram;         /* Main memory */
509extern UINT8  *spc_ram;            /* SPC main memory */
510extern UINT8  spc_port_in[4];      /* SPC input ports */
511extern UINT8  spc_port_out[4];      /* SPC output ports */
512514struct SNES_PPU_STRUCT   /* once all the regs are saved in this structure, it would be better to reorganize it a bit... */
513515{
514516   struct
r7592r7593
624626
625627extern struct snes_cart_info snes_cart;
626628
627/*----------- defined in audio/snes.c -----------*/
628
629/* (APU) Sound related */
630extern READ8_HANDLER( spc_io_r );
631extern WRITE8_HANDLER( spc_io_w );
632extern READ8_HANDLER( spc_ram_r );
633extern WRITE8_HANDLER( spc_ram_w );
634extern READ8_HANDLER( spc_ipl_r );
635extern DEVICE_GET_INFO( snes_sound );
636#define SOUND_SNES DEVICE_GET_INFO_NAME( snes_sound )
637
638/* Stuff from OpenSPC 0.3.99 by Brad Martin */
639
640/*========== TYPES ==========*/
641
642typedef enum                        /* ADSR state type              */
643    {
644    ATTACK,
645    DECAY,
646    SUSTAIN,
647    RELEASE
648    } env_state_t32;
649
650typedef struct                      /* Voice state type             */
651    {
652    UINT16  mem_ptr;        /* Sample data memory pointer   */
653    int             end;            /* End or loop after block      */
654    int             envcnt;         /* Counts to envelope update    */
655    env_state_t32   envstate;       /* Current envelope state       */
656    int             envx;           /* Last env height (0-0x7FFF)   */
657    int             filter;         /* Last header's filter         */
658    int             half;           /* Active nybble of BRR         */
659    int             header_cnt;     /* Bytes before new header (0-8)*/
660    int             mixfrac;        /* Fractional part of smpl pstn */
661    int             on_cnt;         /* Is it time to turn on yet?   */
662    int             pitch;          /* Sample pitch (4096->32000Hz) */
663    int             range;          /* Last header's range          */
664    UINT32   samp_id;        /* Sample ID#                   */
665    int             sampptr;        /* Where in sampbuf we are      */
666    signed long     smp1;           /* Last sample (for BRR filter) */
667    signed long     smp2;           /* Second-to-last sample decoded*/
668    short           sampbuf[ 4 ];   /* Buffer for Gaussian interp   */
669    } voice_state_type;
670
671typedef struct                      /* Source directory entry       */
672    {
673    UINT16  vptr;           /* Ptr to start of sample data  */
674    UINT16  lptr;           /* Loop pointer in sample data  */
675    } src_dir_type;
676
677/*========== MACROS ==========*/
678
679/* The functions to actually read and write to the DSP registers must be
680   implemented by the specific SPC core implementation, as this is too
681   specific to generalize.  However, by defining these macros, we can
682   generalize the DSP's behavior while staying out of the SPC's internals,
683   by requiring that the SPC core must use these macros at the appropriate
684   times. */
685
686/* All reads simply return the contents of the addressed register. */
687
688/* This macro must be used INSTEAD OF a normal write to register 0x7C
689   (ENDX) */
690#define DSP_WRITE_7C( x )   ( DSPregs[ 0x7C ] = 0 )
691
692/* All other writes should store the value in the addressed register as
693   expected. */
694
695629/*----------- defined in video/snes.c -----------*/
696630
697631extern struct SNES_PPU_STRUCT snes_ppu;
src/mame/mame.mak
r7592r7593
10021002   $(DRIVERS)/mario.o $(AUDIO)/mario.o $(VIDEO)/mario.o \
10031003   $(DRIVERS)/multigam.o \
10041004   $(DRIVERS)/n8080.o $(AUDIO)/n8080.o $(VIDEO)/n8080.o \
1005   $(DRIVERS)/nss.o $(MACHINE)/snes.o $(AUDIO)/snes.o $(VIDEO)/snes.o \
1005   $(DRIVERS)/nss.o $(MACHINE)/snes.o $(AUDIO)/snes_snd.o $(VIDEO)/snes.o \
10061006   $(DRIVERS)/playch10.o $(MACHINE)/playch10.o $(VIDEO)/playch10.o \
10071007   $(DRIVERS)/popeye.o $(VIDEO)/popeye.o \
10081008   $(DRIVERS)/punchout.o $(VIDEO)/punchout.o \
r7592r7593
17211721
17221722$(DRIVERS)/firebeat.o:   $(LAYOUT)/firebeat.lh
17231723
1724$(DRIVERS)/funworld.o:   $(LAYOUT)/funworld.lh
1724$(DRIVERS)/funworld.o:   $(LAYOUT)/jollycrd.lh \
1725                  $(LAYOUT)/bigdeal.lh \
1726                  $(LAYOUT)/royalcrd.lh
17251727
17261728$(DRIVERS)/galaxi.o:   $(LAYOUT)/galaxi.lh
17271729
r7592r7593
17671769
17681770$(DRIVERS)/mil4000.o:   $(LAYOUT)/mil4000.lh
17691771
1772$(DRIVERS)/namcofl.o:    $(LAYOUT)/namcofl.lh
1773
17701774$(DRIVERS)/nbmj8688.o:   $(LAYOUT)/nbmj8688.lh
17711775
17721776$(DRIVERS)/namcos2.o:   $(LAYOUT)/finallap.lh
src/mame/drivers/snesb.c
r7592r7593
140140#include "cpu/spc700/spc700.h"
141141#include "cpu/g65816/g65816.h"
142142#include "includes/snes.h"
143#include "audio/snes_snd.h"
143144
144145static INT8 *shared_ram;
145146static UINT8 ffight2b_coins;
r7592r7593
271272ADDRESS_MAP_END
272273
273274
274static READ8_HANDLER( spc_ram_100_r )
275static READ8_DEVICE_HANDLER( spc_ram_100_r )
275276{
276   return spc_ram_r(space, offset + 0x100);
277   return spc_ram_r(device, offset + 0x100);
277278}
278279
279static WRITE8_HANDLER( spc_ram_100_w )
280static WRITE8_DEVICE_HANDLER( spc_ram_100_w )
280281{
281   spc_ram_w(space, offset + 0x100, data);
282   spc_ram_w(device, offset + 0x100, data);
282283}
283284
284285static 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)
290291ADDRESS_MAP_END
291292
292293static INPUT_PORTS_START( snes_common )
r7592r7593
331332   PORT_BIT( 0xff, IP_ACTIVE_HIGH, IPT_UNUSED )
332333
333334#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 ) )
338345
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)
350355
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)
360360
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")
365370#endif
366371INPUT_PORTS_END
367372
r7592r7593
506511
507512   /* sound hardware */
508513   MDRV_SPEAKER_STANDARD_STEREO("lspeaker", "rspeaker")
509
510   MDRV_SOUND_ADD("snes", SNES, 0)
514   MDRV_SOUND_ADD("spc700", SNES, 0)
511515   MDRV_SOUND_ROUTE(0, "lspeaker", 1.00)
512516   MDRV_SOUND_ROUTE(1, "rspeaker", 1.00)
513517MACHINE_DRIVER_END
r7592r7593
815819ROM_END
816820
817821
818GAME( 199?, kinstb,       0,     kinstb,        kinstb,    kinstb,      ROT0, "bootleg",   "Killer Instinct (SNES bootleg)", GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS )
819GAME( 1996, ffight2b,     0,     kinstb,        ffight2b,  ffight2b,   ROT0, "bootleg",   "Final Fight 2 (SNES bootleg)", GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS )
820GAME( 1997, sblast2b,     0,     kinstb,        sblast2b,  sblast2b,   ROT0, "bootleg",   "Sonic Blast Man 2 Special Turbo (SNES bootleg)", GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS)
821GAME( 1996, iron,         0,     kinstb,        iron,      iron,      ROT0, "bootleg",   "Iron (SNES bootleg)", GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS )
822GAME( 1996, denseib,      0,     kinstb,        denseib,   denseib,   ROT0, "bootleg",   "Ghost Chaser Densei (SNES bootleg)", GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS )
823GAME( 1996, endless,      0,     kinstb,        iron,      endless,    ROT0, "bootleg",   "Gundam Wing: Endless Duel (SNES bootleg)", GAME_NOT_WORKING | GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS )
822GAME( 199?, kinstb,       0,     kinstb,        kinstb,    kinstb,       ROT0, "bootleg",  "Killer Instinct (SNES bootleg)",                 GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS )
823GAME( 1996, ffight2b,     0,     kinstb,        ffight2b,  ffight2b,     ROT0, "bootleg",  "Final Fight 2 (SNES bootleg)",                   GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS )
824GAME( 1997, sblast2b,     0,     kinstb,        sblast2b,  sblast2b,     ROT0, "bootleg",  "Sonic Blast Man 2 Special Turbo (SNES bootleg)", GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS)
825GAME( 1996, iron,         0,     kinstb,        iron,      iron,         ROT0, "bootleg",  "Iron (SNES bootleg)",                            GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS )
826GAME( 1996, denseib,      0,     kinstb,        denseib,   denseib,      ROT0, "bootleg",  "Ghost Chaser Densei (SNES bootleg)",             GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS )
827GAME( 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
r7592r7593
292292-----------------------------------------------------------------------------------------------------------------------------------------------------
293293
294294***************************************************************************/
295
295296#include "emu.h"
296297#include "cpu/spc700/spc700.h"
297298#include "cpu/g65816/g65816.h"
298299#include "includes/snes.h"
300#include "audio/snes_snd.h"
299301
300302static ADDRESS_MAP_START( snes_map, ADDRESS_SPACE_PROGRAM, 8)
301303   AM_RANGE(0x000000, 0x2fffff) AM_READWRITE(snes_r_bank1, snes_w_bank1)   /* I/O and ROM (repeats for each bank) */
r7592r7593
308310   AM_RANGE(0xc00000, 0xffffff) AM_READWRITE(snes_r_bank7, snes_w_bank7)   /* Mirror and ROM */
309311ADDRESS_MAP_END
310312
311static READ8_HANDLER( spc_ram_100_r )
313static READ8_DEVICE_HANDLER( spc_ram_100_r )
312314{
313   return spc_ram_r(space, offset + 0x100);
315   return spc_ram_r(device, offset + 0x100);
314316}
315317
316static WRITE8_HANDLER( spc_ram_100_w )
318static WRITE8_DEVICE_HANDLER( spc_ram_100_w )
317319{
318   spc_ram_w(space, offset + 0x100, data);
320   spc_ram_w(device, offset + 0x100, data);
319321}
320322
321323static 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)
327329ADDRESS_MAP_END
328330
329331static INPUT_PORTS_START( snes )
r7592r7593
416418
417419   /* sound hardware */
418420   MDRV_SPEAKER_STANDARD_STEREO("lspeaker", "rspeaker")
419
420   MDRV_SOUND_ADD("snes", SNES, 0)
421   MDRV_SOUND_ADD("spc700", SNES, 0)
421422   MDRV_SOUND_ROUTE(0, "lspeaker", 1.00)
422423   MDRV_SOUND_ROUTE(1, "rspeaker", 1.00)
423424MACHINE_DRIVER_END
r7592r7593
574575   ROM_LOAD( "st.ic3", 0x0000, 0x8000, CRC(8880596e) SHA1(ec6d68fc2f51f7d94f496cd72cf898db65324542) )
575576ROM_END
576577
577GAME( 199?, nss,       0,     snes,        snes,    snes,      ROT0, "Nintendo",               "Nintendo Super System BIOS", GAME_IS_BIOS_ROOT )
578GAME( 1992, nss_actr,  nss,     snes,        snes,    snes,      ROT0, "Enix",                  "Act Raiser (Nintendo Super System)", GAME_NOT_WORKING | GAME_IMPERFECT_SOUND ) // time broken (?)
579GAME( 1992, nss_adam,  nss,     snes,        snes,    snes,      ROT0, "Ocean",                  "The Addams Family (Nintendo Super System)", GAME_NOT_WORKING | GAME_IMPERFECT_SOUND )
580GAME( 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
581GAME( 1992, nss_con3,  nss,     snes,        snes,    snes,      ROT0, "Konami",                  "Contra 3: The Alien Wars (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING )
582GAME( 1992, nss_lwep,  nss,     snes,        snes,    snes,      ROT0, "Ocean",                  "Lethal Weapon (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING )
583GAME( 1992, nss_ncaa,  nss,     snes,        snes,    snes,      ROT0, "Sculptured Software Inc.",   "NCAA Basketball (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING )
584GAME( 1992, nss_rob3,  nss,     snes,        snes,    snes,      ROT0, "Ocean",                  "Robocop 3 (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) // invisible enemy? gameplay prob?
585GAME( 1992, nss_skin,  nss,     snes,        snes,    snes,      ROT0, "Irem",                  "Skins Game (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) // no controls
586GAME( 1992, nss_ssoc,  nss,     snes,        snes,    snes,      ROT0, "Human Inc.",               "Super Soccer (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) // some gfx issues
587GAME( 1991, nss_smw,   nss,     snes,        snes,    snes,      ROT0, "Nintendo",               "Super Mario World (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING )
588GAME( 1991, nss_fzer,  nss,     snes,        snes,    snes,      ROT0, "Nintendo",               "F-Zero (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING )
589GAME( 1991, nss_sten,  nss,     snes,        snes,    snes,      ROT0, "Nintendo",               "Super Tennis (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING )
578GAME( 199?, nss,       0,     snes,      snes,    snes,    ROT0, "Nintendo",                    "Nintendo Super System BIOS", GAME_IS_BIOS_ROOT )
579GAME( 1992, nss_actr,  nss,   snes,      snes,    snes,    ROT0, "Enix",                        "Act Raiser (Nintendo Super System)", GAME_NOT_WORKING | GAME_IMPERFECT_SOUND ) // time broken (?)
580GAME( 1992, nss_adam,  nss,   snes,      snes,    snes,    ROT0, "Ocean",                       "The Addams Family (Nintendo Super System)", GAME_NOT_WORKING | GAME_IMPERFECT_SOUND )
581GAME( 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
582GAME( 1992, nss_con3,  nss,   snes,      snes,    snes,    ROT0, "Konami",                      "Contra 3: The Alien Wars (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING )
583GAME( 1992, nss_lwep,  nss,   snes,      snes,    snes,    ROT0, "Ocean",                       "Lethal Weapon (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING )
584GAME( 1992, nss_ncaa,  nss,   snes,      snes,    snes,    ROT0, "Sculptured Software Inc.",    "NCAA Basketball (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING )
585GAME( 1992, nss_rob3,  nss,   snes,      snes,    snes,    ROT0, "Ocean",                       "Robocop 3 (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) // invisible enemy? gameplay prob?
586GAME( 1992, nss_skin,  nss,   snes,      snes,    snes,    ROT0, "Irem",                        "Skins Game (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) // no controls
587GAME( 1992, nss_ssoc,  nss,   snes,      snes,    snes,    ROT0, "Human Inc.",                  "Super Soccer (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING ) // some gfx issues
588GAME( 1991, nss_smw,   nss,   snes,      snes,    snes,    ROT0, "Nintendo",                    "Super Mario World (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING )
589GAME( 1991, nss_fzer,  nss,   snes,      snes,    snes,    ROT0, "Nintendo",                    "F-Zero (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING )
590GAME( 1991, nss_sten,  nss,   snes,      snes,    snes,    ROT0, "Nintendo",                    "Super Tennis (Nintendo Super System)", GAME_IMPERFECT_SOUND | GAME_NOT_WORKING )
src/emu/cpu/spc700/spc700.c
r7592r7593
12471247/* ================================= API ================================== */
12481248/* ======================================================================== */
12491249
1250static void state_register( running_device *device )
1251{
1252   spc700i_cpu *cpustate = get_safe_token(device);
1253
1254   state_save_register_device_item(device, 0, cpustate->a);
1255   state_save_register_device_item(device, 0, cpustate->x);
1256   state_save_register_device_item(device, 0, cpustate->y);
1257   state_save_register_device_item(device, 0, cpustate->s);
1258   state_save_register_device_item(device, 0, cpustate->pc);
1259   state_save_register_device_item(device, 0, cpustate->ppc);
1260   state_save_register_device_item(device, 0, cpustate->flag_n);
1261   state_save_register_device_item(device, 0, cpustate->flag_z);
1262   state_save_register_device_item(device, 0, cpustate->flag_v);
1263   state_save_register_device_item(device, 0, cpustate->flag_p);
1264   state_save_register_device_item(device, 0, cpustate->flag_b);
1265   state_save_register_device_item(device, 0, cpustate->flag_h);
1266   state_save_register_device_item(device, 0, cpustate->flag_i);
1267   state_save_register_device_item(device, 0, cpustate->flag_c);
1268   state_save_register_device_item(device, 0, cpustate->line_irq);
1269   state_save_register_device_item(device, 0, cpustate->line_nmi);
1270   state_save_register_device_item(device, 0, cpustate->line_rst);
1271   state_save_register_device_item(device, 0, cpustate->ir);
1272   state_save_register_device_item(device, 0, cpustate->stopped);
1273   state_save_register_device_item(device, 0, cpustate->ICount);
1274   state_save_register_device_item(device, 0, cpustate->source);
1275   state_save_register_device_item(device, 0, cpustate->destination);
1276   state_save_register_device_item(device, 0, cpustate->temp1);
1277   state_save_register_device_item(device, 0, cpustate->temp2);
1278   state_save_register_device_item(device, 0, cpustate->temp3);
1279   state_save_register_device_item(device, 0, cpustate->spc_int16);
1280   state_save_register_device_item(device, 0, cpustate->spc_int32);
1281}
1282
12501283static CPU_INIT( spc700 )
12511284{
12521285   spc700i_cpu *cpustate = get_safe_token(device);
12531286
1287   state_register(device);
1288
12541289   INT_ACK = irqcallback;
12551290   cpustate->device = device;
12561291   cpustate->program = device->space(AS_PROGRAM);
src/mess/mess.mak
r7592r7593
11051105   $(MESS_FORMATS)/nes_dsk.o   \
11061106   $(MESS_MACHINE)/nes.o      \
11071107   $(MESS_DRIVERS)/nes.o      \
1108   $(MAME_AUDIO)/snes.o      \
1108   $(MAME_AUDIO)/snes_snd.o      \
11091109   $(MAME_MACHINE)/snes.o      \
11101110   $(MAME_VIDEO)/snes.o      \
11111111   $(MESS_MACHINE)/snescart.o   \
src/mess/drivers/snes.c
r7592r7593
2727***************************************************************************/
2828
2929#include "emu.h"
30#include "video/generic.h"
30#include "audio/snes_snd.h"
3131#include "cpu/spc700/spc700.h"
3232#include "cpu/superfx/superfx.h"
3333#include "cpu/g65816/g65816.h"
r7592r7593
4343 *
4444 *************************************/
4545
46static READ8_HANDLER( spc_ram_100_r )
46static READ8_DEVICE_HANDLER( spc_ram_100_r )
4747{
48   return spc_ram_r(space, offset + 0x100);
48   return spc_ram_r(device, offset + 0x100);
4949}
5050
51static WRITE8_HANDLER( spc_ram_100_w )
51static WRITE8_DEVICE_HANDLER( spc_ram_100_w )
5252{
53   spc_ram_w(space, offset + 0x100, data);
53   spc_ram_w(device, offset + 0x100, data);
5454}
5555
5656/*************************************
r7592r7593
8080ADDRESS_MAP_END
8181
8282static ADDRESS_MAP_START( spc_map, ADDRESS_SPACE_PROGRAM, 8)
83   AM_RANGE(0x0000, 0x00ef) AM_READWRITE(spc_ram_r, spc_ram_w) AM_BASE(&spc_ram)      /* lower 32k ram */
84   AM_RANGE(0x00f0, 0x00ff) AM_READWRITE(spc_io_r, spc_io_w)      /* spc io */
85   AM_RANGE(0x0100, 0xffff) AM_WRITE(spc_ram_100_w)
86   AM_RANGE(0x0100, 0xffbf) AM_READ(spc_ram_100_r)
87   AM_RANGE(0xffc0, 0xffff) AM_READ(spc_ipl_r)
83   AM_RANGE(0x0000, 0x00ef) AM_DEVREADWRITE("spc700", spc_ram_r, spc_ram_w)      /* lower 32k ram */
84   AM_RANGE(0x00f0, 0x00ff) AM_DEVREADWRITE("spc700", spc_io_r, spc_io_w)      /* spc io */
85   AM_RANGE(0x0100, 0xffff) AM_DEVWRITE("spc700", spc_ram_100_w)
86   AM_RANGE(0x0100, 0xffbf) AM_DEVREAD("spc700", spc_ram_100_r)
87   AM_RANGE(0xffc0, 0xffff) AM_DEVREAD("spc700", spc_ipl_r)
8888ADDRESS_MAP_END
8989
9090
r7592r7593
697697
698698   /* sound hardware */
699699   MDRV_SPEAKER_STANDARD_STEREO("lspeaker", "rspeaker")
700   MDRV_SOUND_ADD("custom", SNES, 0)
700   MDRV_SOUND_ADD("spc700", SNES, 0)
701701   MDRV_SOUND_ROUTE(0, "lspeaker", 1.00)
702702   MDRV_SOUND_ROUTE(1, "rspeaker", 1.00)
703703

Previous 15994 Revisions Next


© 1998-2018 The MESS Team