sound, fix psg audio
authorkub <derkub@gmail.com>
Fri, 24 Sep 2021 16:31:19 +0000 (18:31 +0200)
committerkub <derkub@gmail.com>
Fri, 24 Sep 2021 16:31:19 +0000 (18:31 +0200)
pico/sound/sn76496.c

index 4507507..b4a8847 100644 (file)
@@ -22,7 +22,7 @@
 \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
@@ -58,7 +51,7 @@ struct SN76496
        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
@@ -97,6 +90,7 @@ void SN76496Write(int data)
                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
@@ -115,10 +109,11 @@ void SN76496Write(int data)
                        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
@@ -138,18 +133,6 @@ void SN76496Update(short *buffer, int length, int stereo)
        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
@@ -173,13 +156,16 @@ void SN76496Update(short *buffer, int length, int stereo)
                        /* 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
@@ -189,12 +175,10 @@ void SN76496Update(short *buffer, int length, int stereo)
                                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
@@ -202,27 +186,29 @@ void SN76496Update(short *buffer, int length, int stereo)
                        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
@@ -254,7 +240,7 @@ static void SN76496_set_gain(struct SN76496 *R,int gain)
        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
@@ -262,7 +248,7 @@ static void SN76496_set_gain(struct SN76496 *R,int gain)
        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
@@ -294,10 +280,10 @@ int SN76496_init(int clock,int sample_rate)
 \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