Previous 15994 Revisions Next

r6376 Sunday 15th November, 2009 at 03:51:52 UTC by Jonathan Gevaryahu
Trashed the old pre-accurate-clocking STEP based sample averaging scheme from sn76489 and friends, vastly simplifying the code. Made the output bipolar rather than only above the 0 line.
[src/emu/sound]sn76496.c

src/emu/sound/sn76496.c
r6375r6376
4444  for the taps), and added R->OldNoise to simulate the extra 0 that is always
4545  output before the noise LFSR contents are after an LFSR reset.
4646  This fixes SN76489/A to match chips. Added SN94624.
47 
48  14/11/2009 : Lord Nightmare
49  Removed STEP mess, vastly simplifying the code. Made output bipolar rather
50  than always above the 0 line.
4751
4852  TODO: * Implement a function for setting stereo regs for the game gear.
4953          Requires making the core support both mono and stereo, and have
r6375r6376
6569#include "sn76496.h"
6670
6771
68#define MAX_OUTPUT 0x7fff
69#define STEP 0x10000
72#define MAX_OUTPUT 0x3fff
7073#define NOISEMODE (R->Register[6]&4)?1:0
7174
7275
r6375r6376
149152      case 2:   /* tone 1 : frequency */
150153      case 4:   /* tone 2 : frequency */
151154          if ((data & 0x80) == 0) R->Register[r] = (R->Register[r] & 0x0f) | ((data & 0x3f) << 4);
152         R->Period[c] = STEP * R->Register[r];
153         if (R->Period[c] == 0) R->Period[c] = STEP;
155         R->Period[c] = R->Register[r];
154156         if (r == 4)
155157         {
156158            /* update noise shift frequency */
r6375r6376
170172                 if ((data & 0x80) == 0) R->Register[r] = (R->Register[r] & 0x3f0) | (data & 0x0f);
171173            n = R->Register[6];
172174            /* N/512,N/1024,N/2048,Tone #3 output */
173            R->Period[3] = ((n&3) == 3) ? 2 * R->Period[2] : (STEP << (5+(n&3)));
175            R->Period[3] = ((n&3) == 3) ? 2 * R->Period[2] : (1 << (5+(n&3)));
174176                 /* Reset noise shifter */
175177            R->RNG = R->FeedbackMask;
176178            R->OldNoise = 0;
r6375r6376
181183   }
182184}
183185
184
185
186186static STREAM_UPDATE( SN76496Update )
187187{
188188   int i;
r6375r6376
190190   stream_sample_t *buffer = outputs[0];
191191
192192
193   /* If the volume is 0, increase the counter; this is more or less
194    a speedup hack for when silence is to be output */
195   for (i = 0;i < 4;i++)
196   {
197      if (R->Volume[i] == 0)
198      {
199         /* note that I do count += samples, NOT count = samples + 1.
200            You might think it's the same since the volume is 0, but doing
201            the latter could cause interferencies when the program is
202            rapidly modulating the volume. */
203         if (R->Count[i] <= samples*STEP) R->Count[i] += samples*STEP;
204      }
205   }
206
207193   while (samples > 0)
208194   {
209      int vol[4];
210      unsigned int out;
211      int left;
195   // clock chip once
196      INT16 out;
212197
213198      /* decrement Cycles to READY by one */
214199      if (R->CyclestoREADY >0) R->CyclestoREADY--;
215200
216      /* vol[] keeps track of how long each square wave stays */
217      /* in the 1 position during the sample period. */
218      vol[0] = vol[1] = vol[2] = vol[3] = 0;
219
201      // handle channels 0,1,2
220202      for (i = 0;i < 3;i++)
221203      {
222         if (R->Output[i]) vol[i] += R->Count[i];
223         R->Count[i] -= STEP;
224         /* Period[i] is the half period of the square wave. Here, in each
225            loop I add Period[i] twice, so that at the end of the loop the
226            square wave is in the same status (0 or 1) it was at the start.
227            vol[i] is also incremented by Period[i], since the wave has been 1
228            exactly half of the time, regardless of the initial position.
229            If we exit the loop in the middle, Output[i] has to be inverted
230            and vol[i] incremented only if the exit status of the square
231            wave is 1. */
232         while (R->Count[i] <= 0)
204         R->Count[i]--;
205         if (R->Count[i] < 0)
233206         {
234            R->Count[i] += R->Period[i];
235            if (R->Count[i] > 0)
236            {
237               R->Output[i] ^= 1;
238               if (R->Output[i]) vol[i] += R->Period[i];
239               break;
240            }
241            R->Count[i] += R->Period[i];
242            vol[i] += R->Period[i];
207            R->Output[i] ^= 1;
208            R->Count[i] = R->Period[i];
243209         }
244         if (R->Output[i]) vol[i] -= R->Count[i];
245210      }
246211
247      left = STEP;
248      do
212      // handle channel 3
213      R->Count[3]--;
214      if (R->Count[3] <= 0)
249215      {
250         int nextevent;
251
252
253         if (R->Count[3] < left) nextevent = R->Count[3];
254         else nextevent = left;
255
256         if (R->Output[3]) vol[3] += R->Count[3];
257         R->Count[3] -= nextevent;
258         if (R->Count[3] <= 0)
216         if (NOISEMODE == 1) /* White Noise Mode */
259217         {
260              if (NOISEMODE == 1) /* White Noise Mode */
261              {
262                 if ((((R->RNG & R->WhitenoiseTaps) != R->WhitenoiseTaps) && ((R->RNG & R->WhitenoiseTaps) != 0)) ^ R->FeedbackInvert ) /* XOR or XNOR */
263               {
264                    R->RNG >>= 1;
265                    R->RNG |= R->FeedbackMask;
266               }
267               else
268               {
269                    R->RNG >>= 1;
270               }
218            if ((((R->RNG & R->WhitenoiseTaps) != R->WhitenoiseTaps) && ((R->RNG & R->WhitenoiseTaps) != 0)) ^ R->FeedbackInvert ) /* XOR or XNOR */
219            {
220               R->RNG >>= 1;
221               R->RNG |= R->FeedbackMask;
271222            }
272            else /* Periodic noise mode */
223            else
273224            {
274                 if (R->RNG & 1)
275               {
276                    R->RNG >>= 1;
277                    R->RNG |= R->FeedbackMask;
278               }
279               else
280               {
281                    R->RNG >>= 1;
282               }
225               R->RNG >>= 1;
283226            }
284            R->Output[3] = R->OldNoise;
285            R->OldNoise = R->RNG & 1;
286            R->Count[3] += R->Period[3];
287            if (R->Output[3]) vol[3] += R->Period[3];
288227         }
289         if (R->Output[3]) vol[3] -= R->Count[3];
228         else /* Periodic noise mode */
229         {
230            if (R->RNG & 1)
231            {
232               R->RNG >>= 1;
233               R->RNG |= R->FeedbackMask;
234            }
235            else
236            {
237               R->RNG >>= 1;
238            }
239         }
240         R->Output[3] = R->OldNoise;
241         R->OldNoise = R->RNG & 1;
242         R->Count[3] = R->Period[3];
243      }
290244
291         left -= nextevent;
292      } while (left > 0);
245      out = (R->Output[0]?R->Volume[0]:(0-R->Volume[0]))
246         +(R->Output[1]?R->Volume[1]:(0-R->Volume[1]))
247         +(R->Output[2]?R->Volume[2]:(0-R->Volume[2]))
248         +(R->Output[3]?R->Volume[3]:(0-R->Volume[3]));
293249
294      out = vol[0] * R->Volume[0] + vol[1] * R->Volume[1] +
295            vol[2] * R->Volume[2] + vol[3] * R->Volume[3];
250      *(buffer++) = out;
296251
297      if (out > MAX_OUTPUT * STEP) out = MAX_OUTPUT * STEP;
298
299      *(buffer++) = out / STEP;
300
301252      samples--;
302253   }
303254}
r6375r6376
349300
350301   for (i = 0;i < 4;i++)
351302   {
352      R->Output[i] = 0;
353      R->Period[i] = R->Count[i] = STEP;
303      R->Output[i] = R->Period[i] = R->Count[i] = 0;
354304   }
355305
356306   /* Default is SN76489 non-A */

Previous 15994 Revisions Next


© 1998-2018 The MESS Team