sn76496: simplify writes
authornotaz <notasas@gmail.com>
Sat, 30 Sep 2017 15:14:20 +0000 (18:14 +0300)
committernotaz <notasas@gmail.com>
Fri, 6 Oct 2017 22:36:59 +0000 (01:36 +0300)
pico/sound/sn76496.c

index 8474c70..b212759 100644 (file)
@@ -73,96 +73,54 @@ int *sn76496_regs;
 void SN76496Write(int data)\r
 {\r
        struct SN76496 *R = &ono_sn;\r
-       int n;\r
-\r
+       int n, r, c;\r
 \r
        /* update the output buffer before changing the registers */\r
        //stream_update(R->Channel,0);\r
 \r
+       r = R->LastRegister;\r
        if (data & 0x80)\r
-       {\r
-               int r = (data & 0x70) >> 4;\r
-               int c = r/2;\r
+               r = R->LastRegister = (data & 0x70) >> 4;\r
+       c = r / 2;\r
 \r
-               R->LastRegister = r;\r
-               R->Register[r] = (R->Register[r] & 0x3f0) | (data & 0x0f);\r
-               switch (r)\r
-               {\r
-                       case 0: /* tone 0 : frequency */\r
-                       case 2: /* tone 1 : frequency */\r
-                       case 4: /* tone 2 : frequency */\r
-                               R->Period[c] = R->UpdateStep * R->Register[r];\r
-                               if (R->Period[c] == 0) R->Period[c] = R->UpdateStep;\r
-                               if (r == 4)\r
-                               {\r
-                                       /* update noise shift frequency */\r
-                                       if ((R->Register[6] & 0x03) == 0x03)\r
-                                               R->Period[3] = 2 * R->Period[2];\r
-                               }\r
-                               break;\r
-                       case 1: /* tone 0 : volume */\r
-                       case 3: /* tone 1 : volume */\r
-                       case 5: /* tone 2 : volume */\r
-                       case 7: /* noise  : volume */\r
-                               R->Volume[c] = R->VolTable[data & 0x0f];\r
-                               break;\r
-                       case 6: /* noise  : frequency, mode */\r
-                               {\r
-                                       int n = R->Register[6];\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) == 3) ? 2 * R->Period[2] : (R->UpdateStep << (5+(n&3)));\r
-\r
-                                       /* reset noise shifter */\r
-                                       R->RNG = NG_PRESET;\r
-                                       R->Output[3] = R->RNG & 1;\r
-                               }\r
-                               break;\r
-               }\r
-       }\r
+       if (!(data & 0x80) && (r == 0 || r == 2 || r == 4))\r
+               // data byte (tone only)\r
+               R->Register[r] = (R->Register[r] & 0x0f) | ((data & 0x3f) << 4);\r
        else\r
-       {\r
-               int r = R->LastRegister;\r
-               int c = r/2;\r
+               R->Register[r] = (R->Register[r] & 0x3f0) | (data & 0x0f);\r
 \r
-               switch (r)\r
-               {\r
-                       case 0: /* tone 0 : frequency */\r
-                       case 2: /* tone 1 : frequency */\r
-                       case 4: /* tone 2 : frequency */\r
-                               R->Register[r] = (R->Register[r] & 0x0f) | ((data & 0x3f) << 4);\r
-                               R->Period[c] = R->UpdateStep * R->Register[r];\r
-                               if (R->Period[c] == 0) R->Period[c] = R->UpdateStep;\r
-                               if (r == 4)\r
-                               {\r
-                                       /* update noise shift frequency */\r
-                                       if ((R->Register[6] & 0x03) == 0x03)\r
-                                               R->Period[3] = 2 * R->Period[2];\r
-                               }\r
-                               break;\r
-                       case 1: /* tone 0 : volume */\r
-                       case 3: /* tone 1 : volume */\r
-                       case 5: /* tone 2 : volume */\r
-                       case 7: /* noise  : volume */\r
-                               R->Volume[c] = R->VolTable[data & 0x0f];\r
-                               R->Register[r] = (R->Register[r] & 0x3f0) | (data & 0x0f);\r
-                               break;\r
-                       case 6: /* noise  : frequency, mode */\r
-                               {\r
-                                       R->Register[r] = (R->Register[r] & 0x3f0) | (data & 0x0f);\r
-                                       n = R->Register[6];\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) == 3) ? 2 * R->Period[2] : (R->UpdateStep << (5+(n&3)));\r
-\r
-                                       /* reset noise shifter */\r
-                                       R->RNG = NG_PRESET;\r
-                                       R->Output[3] = R->RNG & 1;\r
-                               }\r
-                               break;\r
-               }\r
+       data = R->Register[r];\r
+       switch (r)\r
+       {\r
+               case 0: /* tone 0 : frequency */\r
+               case 2: /* tone 1 : frequency */\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
+                       if (r == 4)\r
+                       {\r
+                               /* update noise shift frequency */\r
+                               if ((R->Register[6] & 0x03) == 0x03)\r
+                                       R->Period[3] = 2 * R->Period[2];\r
+                       }\r
+                       break;\r
+               case 1: /* tone 0 : volume */\r
+               case 3: /* tone 1 : volume */\r
+               case 5: /* tone 2 : volume */\r
+               case 7: /* noise  : volume */\r
+                       R->Volume[c] = R->VolTable[data & 0x0f];\r
+                       break;\r
+               case 6: /* noise  : frequency, mode */\r
+                       n = data;\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
+                       /* reset noise shifter */\r
+                       R->RNG = NG_PRESET;\r
+                       R->Output[3] = R->RNG & 1;\r
+                       break;\r
        }\r
 }\r
 \r