\r
#include "sn76496.h"\r
\r
-#define MAX_OUTPUT 0x47ff // was 0x7fff\r
+#define MAX_OUTPUT 0x4800 // was 0x7fff\r
\r
#define STEP 0x10000\r
\r
/* bit0 = output */\r
\r
/* noise feedback for white noise mode (verified on real SN76489 by John Kortink) */\r
-#define FB_WNOISE 0x14002 /* (16bits) bit16 = bit0(out) ^ bit2 ^ bit15 */\r
+#define FB_WNOISE_T 0x3000 /* (15bits) bit15 = bit1 ^ bit2, TI */\r
+#define FB_WNOISE_S 0x9000 /* (16bits) bit16 = bit0 ^ bit3, Sega PSG */\r
\r
/* noise feedback for periodic noise mode */\r
-//#define FB_PNOISE 0x10000 /* 16bit rorate */\r
-#define FB_PNOISE 0x08000 /* JH 981127 - fixes Do Run Run */\r
+#define FB_PNOISE_T 0x4000 /* 15bit rotate for TI */\r
+#define FB_PNOISE_S 0x8000 /* 16bit rotate for Sega PSG */\r
\r
-/*\r
-0x08000 is definitely wrong. The Master System conversion of Marble Madness\r
-uses periodic noise as a baseline. With a 15-bit rotate, the bassline is\r
-out of tune.\r
-The 16-bit rotate has been confirmed against a real PAL Sega Master System 2.\r
-Hope that helps the System E stuff, more news on the PSG as and when!\r
-*/\r
-\r
-/* noise generator start preset (for periodic noise) */\r
-#define NG_PRESET 0x0f35\r
+#define FB_WNOISE FB_WNOISE_S /* Sega */\r
+#define FB_PNOISE FB_PNOISE_S\r
\r
\r
struct SN76496\r
int Register[8]; /* registers */\r
int LastRegister; /* last register written */\r
int Volume[4]; /* volume of voice 0-2 and noise */\r
- unsigned int RNG; /* noise generator */\r
+ unsigned int RNG; /* noise generator */\r
int NoiseFB; /* noise feedback mask */\r
int Period[4];\r
int Count[4];\r
case 4: /* tone 2 : frequency */\r
R->Period[c] = R->UpdateStep * data;\r
if (R->Period[c] == 0) R->Period[c] = R->UpdateStep;\r
+ R->Count[c] = 0;\r
if (r == 4)\r
{\r
/* update noise shift frequency */\r
R->NoiseFB = (n & 4) ? FB_WNOISE : FB_PNOISE;\r
n &= 3;\r
/* N/512,N/1024,N/2048,Tone #3 output */\r
- R->Period[3] = (n == 3) ? 2 * R->Period[2] : (R->UpdateStep << (5 + n));\r
+ R->Period[3] = (n == 3) ? 2 * R->Period[2] : (R->UpdateStep << (4 + n));\r
+ R->Count[3] = 0;\r
\r
/* reset noise shifter */\r
- R->RNG = NG_PRESET;\r
+ R->RNG = FB_PNOISE;\r
R->Output[3] = R->RNG & 1;\r
break;\r
}\r
int i;\r
struct SN76496 *R = &ono_sn;\r
\r
- /* If the volume is 0, increase the counter */\r
- for (i = 0;i < 4;i++)\r
- {\r
- if (R->Volume[i] == 0)\r
- {\r
- /* note that I do count += length, NOT count = length + 1. You might think */\r
- /* it's the same since the volume is 0, but doing the latter could cause */\r
- /* interferencies when the program is rapidly modulating the volume. */\r
- if (R->Count[i] <= length*STEP) R->Count[i] += length*STEP;\r
- }\r
- }\r
-\r
while (length > 0)\r
{\r
int vol[4];\r
/* If we exit the loop in the middle, Output[i] has to be inverted */\r
/* and vol[i] incremented only if the exit status of the square */\r
/* wave is 1. */\r
- left = 0;\r
- while (R->Count[i] <= 0)\r
+ if (R->Count[i] < -2*R->Period[i]) {\r
+ /* Cut of anything above the Nyquist freqency */\r
+ /* It will only create aliasing anyway */\r
+ vol[i] += STEP/2; // mean value\r
+ R->Count[i] = R->Output[i] = 0;\r
+ }\r
+ while (R->Count[i] < 0)\r
{\r
- if (R->Count[i] + R->Period[i]*4 < R->Period[i])\r
- left+= 4, R->Count[i] += R->Period[i]*4;\r
- else left++, R->Count[i] += R->Period[i];\r
- if (R->Count[i] > 0)\r
+ R->Count[i] += R->Period[i];\r
+ if (R->Count[i] >= 0)\r
{\r
R->Output[i] ^= 1;\r
if (R->Output[i]) vol[i] += R->Period[i];\r
vol[i] += R->Period[i];\r
}\r
if (R->Output[i]) vol[i] -= R->Count[i];\r
- /* Cut of anything above the sample freqency. It will only create */\r
- /* aliasing and hearable distortions anyway. */\r
- if (left > 1) vol[i] = STEP/2;\r
}\r
\r
left = STEP;\r
+ if (R->Output[3]) vol[3] += R->Count[3];\r
do\r
{\r
int nextevent;\r
if (R->Count[3] < left) nextevent = R->Count[3];\r
else nextevent = left;\r
\r
- if (R->Output[3]) vol[3] += R->Count[3];\r
R->Count[3] -= nextevent;\r
if (R->Count[3] <= 0)\r
{\r
- if (R->RNG & 1) R->RNG ^= R->NoiseFB;\r
- R->RNG >>= 1;\r
R->Output[3] = R->RNG & 1;\r
+ R->RNG >>= 1;\r
+ if (R->Output[3])\r
+ {\r
+ R->RNG ^= R->NoiseFB;\r
+ vol[3] += R->Period[3];\r
+ }\r
R->Count[3] += R->Period[3];\r
- if (R->Output[3]) vol[3] += R->Period[3];\r
}\r
- if (R->Output[3]) vol[3] -= R->Count[3];\r
\r
left -= nextevent;\r
} while (left > 0);\r
+ if (R->Output[3]) vol[3] -= R->Count[3];\r
\r
out = vol[0] * R->Volume[0] + vol[1] * R->Volume[1] +\r
vol[2] * R->Volume[2] + vol[3] * R->Volume[3];\r
\r
if (out > MAX_OUTPUT * STEP) out = MAX_OUTPUT * STEP;\r
\r
- if ((out /= STEP)) // will be optimized to shift; max 0x47ff = 18431\r
+ if ((out /= STEP)) // will be optimized to shift; max 0x4800 = 18432\r
*buffer += out;\r
if(stereo) buffer+=2; // only left for stereo, to be mixed to right later\r
else buffer++;\r
gain &= 0xff;\r
\r
/* increase max output basing on gain (0.2 dB per step) */\r
- out = MAX_OUTPUT / 3;\r
+ out = MAX_OUTPUT / 4.0;\r
while (gain-- > 0)\r
out *= 1.023292992; /* = (10 ^ (0.2/20)) */\r
\r
for (i = 0;i < 15;i++)\r
{\r
/* limit volume to avoid clipping */\r
- if (out > MAX_OUTPUT / 3) R->VolTable[i] = MAX_OUTPUT / 3;\r
+ if (out > MAX_OUTPUT / 4) R->VolTable[i] = MAX_OUTPUT / 4;\r
else R->VolTable[i] = out;\r
\r
out /= 1.258925412; /* = 10 ^ (2/20) = 2dB */\r
\r
for (i = 0;i < 4;i++)\r
{\r
- R->Output[i] = 0;\r
- R->Period[i] = R->Count[i] = R->UpdateStep;\r
+ R->Volume[i] = R->Output[i] = R->Count[i] = 0;\r
+ R->Period[i] = R->UpdateStep;\r
}\r
- R->RNG = NG_PRESET;\r
+ R->RNG = FB_PNOISE;\r
R->Output[3] = R->RNG & 1;\r
\r
// added\r