Previous 15994 Revisions Next

r7157 Sunday 24th January, 2010 at 07:43:31 UTC by Jonathan Gevaryahu
Fixed sn76489a periodic noise once again (and simplified a bit of noise code), now should exactly match real chips as sampled. Added proper emulation of clock divider, so may be slightly slower than before. [Lord Nightmare, PlgDavid, Kold666]
[src/emu/sound]sn76496.c

src/emu/sound/sn76496.c
r7156r7157
1919  ** SN76489A uses a 15-bit shift register with taps on bits D and E, output on F,
2020  XNOR function
2121  It uses a 15-bit ring buffer for periodic noise/arbitrary duty cycle.
22  ** SN76494 and SN76496 are PROBABLY identical in operation to the SN76489A
23  They have an audio input line which is mixed with the 4 channels of output.
22  ** SN76494 is the same as SN76489A but lacks the /8 divider on its clock input.
23  ** SN76496 is PROBABLY identical in operation to the SN76489A, need more info.
24  All the SN7xxxx chips have an audio input line which is mixed with the 4 channels
25  of output.
2426  ** Sega Master System III/MD/Genesis PSG uses a 16-bit shift register with taps
2527  on bits C and F, output on F
2628  It uses a 16-bit ring buffer for periodic noise/arbitrary duty cycle.
r7156r7157
7072  based on testing. Got rid of R->OldNoise and fixed taps accordingly.
7173  Added stereo support for game gear.
7274
73  11/15/2010 : Lord Nightmare
75  15/01/2010 : Lord Nightmare
7476  Fix an issue with SN76489 and SN76489A having the wrong periodic noise periods.
7577  Note that properly emulating the noise cycle bit timing accurately may require
7678  extensive rewriting.
79 
80  17/01/2010: Lord Nightmare
81  Implement periodic noise as forcing one of the XNOR or XOR taps to 1 or 0 respectively.
82  Thanks to PlgDavid for providing samples which helped immensely here.
83  Added true clock divider emulation, so sn94624 and sn76494 run 8x faster than
84  the others, as in real life.
7785
78  TODO: * Implement the TMS9919 which is an earlier version, possibly
79          lacking the /8 clock divider, of the SN76489, and hence would
80          have a max clock of 500Khz and 4 clocks per sample, as opposed to
81          max of 4Mhz and 32 clocks per sample on the SN76489A.
86  TODO: * Implement the TMS9919 - any difference to sn94624?
8287        * Implement the T6W28; has registers in a weird order, needs writes
8388          to be 'sanitized' first. Also is stereo, similar to game gear.
8489        * Test the NCR7496; Smspower says the whitenoise taps are A and E,
8590          but this needs verification on real hardware.
8691        * Factor out common code so that the SAA1099 can share some code.
87        * Implement periodic noise as using a pointer to a table rather than a
88          self feeding lfsr. Kold666's recording of dorunrun proves this is the
89          right way.
90        * Fix the whitenoise bit timing so that dorunrun matches Kold666's PCB sample.
9192***************************************************************************/
9293
9394#include "emu.h"
r7156r7157
9697
9798
9899#define MAX_OUTPUT 0x7fff
99// make the above 0x3fff if using bipolar output
100100#define NOISEMODE (R->Register[6]&4)?1:0
101101
102102
r7156r7157
109109   INT32 LastRegister;   /* last register written */
110110   INT32 Volume[4];   /* db volume of voice 0-2 and noise */
111111   UINT32 RNG;         /* noise generator LFSR*/
112   INT32 ClockDivider;   /* clock divider */
113   INT32 CurrentClock;
112114   INT32 FeedbackMask;   /* mask for feedback */
113   INT32 WhitenoiseTaps;   /* mask for white noise taps */
115   INT32 WhitenoiseTap1;   /* mask for white noise tap 1 (higher one, usually bit 14) */
116   INT32 WhitenoiseTap2;   /* mask for white noise tap 2 (lower one, usually bit 13)*/
114117   INT32 FeedbackInvert;   /* feedback invert flag (xor vs xnor) */
115118   INT32 Negate;      /* output negate flag */
116119   INT32 Stereo;      /* whether we're dealing with stereo or not */
r7156r7157
207210         break;
208211      case 6:   /* noise  : frequency, mode */
209212         {
210                 if ((data & 0x80) == 0) R->Register[r] = (R->Register[r] & 0x3f0) | (data & 0x0f);
213            if ((data & 0x80) == 0) logerror("sn76489: write to reg 6 with bit 7 clear; data was %03x, new write is %02x! report this to LN!\n", R->Register[6], data);
214            if ((data & 0x80) == 0) R->Register[r] = (R->Register[r] & 0x3f0) | (data & 0x0f);
211215            n = R->Register[6];
212216            /* N/512,N/1024,N/2048,Tone #3 output */
213217            R->Period[3] = ((n&3) == 3) ? 2 * R->Period[2] : (1 << (5+(n&3)));
214                 /* Reset noise shifter */
215218            R->RNG = R->FeedbackMask;
216219            R->Output[3] = R->RNG & 1;
217220         }
r7156r7157
225228   sn76496_state *R = (sn76496_state *)param;
226229   stream_sample_t *lbuffer = outputs[0];
227230   stream_sample_t *rbuffer = (R->Stereo)?outputs[1]:NULL;
231   INT16 out = 0;
232   INT16 out2 = 0;
228233
229234   while (samples > 0)
230235   {
231   // clock chip once
232      INT16 out = 0;
233      INT16 out2 = 0;
234
235      /* decrement Cycles to READY by one */
236      if (R->CyclestoREADY >0) R->CyclestoREADY--;
237
238      // handle channels 0,1,2
239      for (i = 0;i < 3;i++)
236      // clock chip once
237      if (R->CurrentClock > 0) // not ready for new divided clock
240238      {
241         R->Count[i]--;
242         if (R->Count[i] < 0)
243         {
244            R->Output[i] ^= 1;
245            R->Count[i] = R->Period[i];
246         }
239         R->CurrentClock--;
247240      }
241      else // ready for new divided clock, make a new sample
242      {
243         R->CurrentClock = R->ClockDivider-1;
244         /* decrement Cycles to READY by one */
245         if (R->CyclestoREADY >0) R->CyclestoREADY--;
248246
249      // handle channel 3
250      R->Count[3]--;
251      if (R->Count[3] <= 0)
252      {
253         if (NOISEMODE == 1) /* White Noise Mode */
247         // handle channels 0,1,2
248         for (i = 0;i < 3;i++)
254249         {
255            if ((((R->RNG & R->WhitenoiseTaps) != R->WhitenoiseTaps) && ((R->RNG & R->WhitenoiseTaps) != 0)) ^ R->FeedbackInvert ) /* XOR or XNOR */
250            R->Count[i]--;
251            if (R->Count[i] < 0)
256252            {
257               R->RNG >>= 1;
258               R->RNG |= R->FeedbackMask;
253               R->Output[i] ^= 1;
254               R->Count[i] = R->Period[i];
259255            }
260            else
261            {
262               R->RNG >>= 1;
263            }
264256         }
265         else /* Periodic noise mode */
257
258         // handle channel 3
259         R->Count[3]--;
260         if (R->Count[3] <= 0)
266261         {
267            if (R->RNG & 1)
262         // if noisemode is 1, both taps are enabled
263         // if noisemode is 0, the lower tap, whitenoisetap2, is held at 1 or 0 depending on whether FeedbackInvert is set or clear
264            if (((R->RNG & R->WhitenoiseTap1)?1:0) ^ (((((R->RNG & R->WhitenoiseTap2)?0:1))*(NOISEMODE))^R->FeedbackInvert) ^ R->FeedbackInvert )
268265            {
269266               R->RNG >>= 1;
270267               R->RNG |= R->FeedbackMask;
r7156r7157
273270            {
274271               R->RNG >>= 1;
275272            }
273            R->Output[3] = R->RNG & 1;
274
275            R->Count[3] = R->Period[3];
276276         }
277         R->Output[3] = R->RNG & 1;
278         R->Count[3] = R->Period[3];
279277      }
280/* //bipolar output, doesn't seem to work right with sonic 2 on gamegear at least
281        out = (R->Output[0]?R->Volume[0]:(0-R->Volume[0]))
282            +(R->Output[1]?R->Volume[1]:(0-R->Volume[1]))
283            +(R->Output[2]?R->Volume[2]:(0-R->Volume[2]))
284            +(R->Output[3]?R->Volume[3]:(0-R->Volume[3]));
285*/
278
279
286280      if (R->Stereo)
287281      {
288282         out = (((R->StereoMask&0x10)&&R->Output[0])?R->Volume[0]:0)
r7156r7157
342336
343337static int SN76496_init(const device_config *device, sn76496_state *R, int stereo)
344338{
345   int sample_rate = device->clock/16;
339   int sample_rate = device->clock/2;
346340   int i;
347341
348342   R->Channel = stream_create(device,0,(stereo?2:1),sample_rate,R,SN76496Update);
r7156r7157
361355      R->Output[i] = R->Period[i] = R->Count[i] = 0;
362356   }
363357
364   /* Default is SN76489 non-A */
365   R->FeedbackMask = 0x4000;     /* mask for feedback */
366   R->WhitenoiseTaps = 0x03;   /* mask for white noise taps */
367   R->FeedbackInvert = 0; /* feedback invert flag */
368   R->CyclestoREADY = 1; /* assume ready is not active immediately on init. is this correct?*/
358   /* Default is SN76489A */
359   R->ClockDivider = 8;
360   R->FeedbackMask = 0x10000;     /* mask for feedback */
361   R->WhitenoiseTap1 = 0x04;   /* mask for white noise tap 1*/
362   R->WhitenoiseTap2 = 0x08;   /* mask for white noise tap 2*/
363   R->FeedbackInvert = 1; /* feedback invert flag */
369364   R->Negate = 0; /* channels are not negated */
370365   R->Stereo = stereo; /* depends on init */
366   R->CyclestoREADY = 1; /* assume ready is not active immediately on init. is this correct?*/
371367   R->StereoMask = 0xFF; /* all channels enabled */
372368
373369   R->RNG = R->FeedbackMask;
r7156r7157
377373}
378374
379375
380static void generic_start(const device_config *device, int feedbackmask, int noisetaps, int feedbackinvert, int negate, int stereo)
376static void generic_start(const device_config *device, int feedbackmask, int noisetap1, int noisetap2, int feedbackinvert, int negate, int stereo, int clockdivider)
381377{
382378   sn76496_state *chip = get_safe_token(device);
383379
r7156r7157
386382   SN76496_set_gain(chip, 0);
387383
388384   chip->FeedbackMask = feedbackmask;
389   chip->WhitenoiseTaps = noisetaps;
385   chip->WhitenoiseTap1 = noisetap1;
386   chip->WhitenoiseTap2 = noisetap2;
390387   chip->FeedbackInvert = feedbackinvert;
391388   chip->Negate = negate;
392389   chip->Stereo = stereo;
390   chip->ClockDivider = clockdivider;
391   chip->CurrentClock = clockdivider-1;
393392
394393   state_save_register_device_item_array(device, 0, chip->VolTable);
395394   state_save_register_device_item_array(device, 0, chip->Register);
396395   state_save_register_device_item(device, 0, chip->LastRegister);
397396   state_save_register_device_item_array(device, 0, chip->Volume);
398397   state_save_register_device_item(device, 0, chip->RNG);
398   state_save_register_device_item(device, 0, chip->ClockDivider);
399   state_save_register_device_item(device, 0, chip->CurrentClock);
399400   state_save_register_device_item(device, 0, chip->FeedbackMask);
400   state_save_register_device_item(device, 0, chip->WhitenoiseTaps);
401   state_save_register_device_item(device, 0, chip->WhitenoiseTap1);
402   state_save_register_device_item(device, 0, chip->WhitenoiseTap2);
401403   state_save_register_device_item(device, 0, chip->FeedbackInvert);
402404   state_save_register_device_item(device, 0, chip->Negate);
403405   state_save_register_device_item(device, 0, chip->Stereo);
r7156r7157
408410   state_save_register_device_item(device, 0, chip->CyclestoREADY);
409411}
410412
411// function parameters: device, feedback destination tap, feedback source taps, xor(false)/xnor(true), normal(false)/invert(true), mono(false)/stereo(true)
413// function parameters: device, feedback destination tap, feedback source taps, xor(false)/xnor(true), normal(false)/invert(true), mono(false)/stereo(true), clock divider factor
412414
413415static DEVICE_START( sn76489 )
414416{
415   generic_start(device, 0x4000, 0x03, FALSE, FALSE, FALSE); // todo: verify; assumed to be the same as sn94624
417   generic_start(device, 0x10000, 0x04, 0x08, FALSE, FALSE, FALSE, 8); // SN76489 not verified yet. todo: verify;
416418}
417419
418420static DEVICE_START( sn76489a )
419421{
420   generic_start(device, 0x4000, 0x03, TRUE, FALSE, FALSE); // verified by plgdavid
422   generic_start(device, 0x10000, 0x04, 0x08, TRUE, FALSE, FALSE, 8); // SN76489A: whitenoise verified, phase verified, periodic verified (by plgdavid)
421423}
422424
423425static DEVICE_START( sn76494 )
424426{
425   generic_start(device, 0x4000, 0x03, TRUE, FALSE, FALSE); // todo: verify; assumed to be the same as sn76489a
427   generic_start(device, 0x10000, 0x04, 0x08, TRUE, FALSE, FALSE, 1); // SN76494 not verified, (according to datasheet: same as sn76489a but without the /8 divider)
426428}
427429
428430static DEVICE_START( sn76496 )
429431{
430   generic_start(device, 0x4000, 0x03, TRUE, FALSE, FALSE); // todo: verify; assumed to be the same as sn76489a
432   generic_start(device, 0x10000, 0x04, 0x08, TRUE, FALSE, FALSE, 8); // SN76496 not verified; assumed to be the same as sn76489a
431433}
432434
433435static DEVICE_START( sn94624 )
434436{
435   generic_start(device, 0x4000, 0x03, FALSE, FALSE, FALSE); // verified by plgdavid
437   generic_start(device, 0x4000, 0x01, 0x02, FALSE, TRUE, FALSE, 1); // SN94624 whitenoise verified, phase verified, period verified; verified by PlgDavid
436438}
437439
438440static DEVICE_START( ncr7496 )
439441{
440   generic_start(device, 0x8000, 0x22, FALSE, FALSE, FALSE); // todo: verify; from smspower wiki
442   generic_start(device, 0x8000, 0x02, 0x20, FALSE, FALSE, FALSE, 8); // NCR7496 not verified; info from smspower wiki
441443}
442444
443445static DEVICE_START( gamegear )
444446{
445   generic_start(device, 0x8000, 0x09, FALSE, TRUE, TRUE); // Verified by Justin Kerk
447   generic_start(device, 0x8000, 0x01, 0x08, FALSE, TRUE, TRUE, 8); // Verified by Justin Kerk
446448}
447449
448450static DEVICE_START( smsiii )
449451{
450   generic_start(device, 0x8000, 0x09, FALSE, TRUE, FALSE); // todo: verify; from smspower wiki, assumed to have same invert as gamegear
452   generic_start(device, 0x8000, 0x01, 0x08, FALSE, TRUE, FALSE, 8); // todo: verify; from smspower wiki, assumed to have same invert as gamegear
451453}
452454
453455

Previous 15994 Revisions Next


© 1998-2018 The MESS Team