lowercasing filenames, part3
[picodrive.git] / pico / sound / sn76496.c
diff --git a/pico/sound/sn76496.c b/pico/sound/sn76496.c
new file mode 100644 (file)
index 0000000..8474c70
--- /dev/null
@@ -0,0 +1,344 @@
+/***************************************************************************\r
+\r
+  sn76496.c\r
+\r
+  Routines to emulate the Texas Instruments SN76489 / SN76496 programmable\r
+  tone /noise generator. Also known as (or at least compatible with) TMS9919.\r
+\r
+  Noise emulation is not accurate due to lack of documentation. The noise\r
+  generator uses a shift register with a XOR-feedback network, but the exact\r
+  layout is unknown. It can be set for either period or white noise; again,\r
+  the details are unknown.\r
+\r
+  28/03/2005 : Sebastien Chevalier\r
+  Update th SN76496Write func, according to SN76489 doc found on SMSPower.\r
+   - On write with 0x80 set to 0, when LastRegister is other then TONE,\r
+   the function is similar than update with 0x80 set to 1\r
+***************************************************************************/\r
+\r
+#ifndef __GNUC__\r
+#pragma warning (disable:4244)\r
+#endif\r
+\r
+#include "sn76496.h"\r
+\r
+#define MAX_OUTPUT 0x47ff // was 0x7fff\r
+\r
+#define STEP 0x10000\r
+\r
+\r
+/* Formulas for noise generator */\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
+\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
+\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
+\r
+\r
+struct SN76496\r
+{\r
+       //sound_stream * Channel;\r
+       int SampleRate;\r
+       unsigned int UpdateStep;\r
+       int VolTable[16];       /* volume table         */\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
+       int NoiseFB;            /* noise feedback mask */\r
+       int Period[4];\r
+       int Count[4];\r
+       int Output[4];\r
+       int pad[1];\r
+};\r
+\r
+static struct SN76496 ono_sn; // one and only SN76496\r
+int *sn76496_regs;\r
+\r
+//static\r
+void SN76496Write(int data)\r
+{\r
+       struct SN76496 *R = &ono_sn;\r
+       int n;\r
+\r
+\r
+       /* update the output buffer before changing the registers */\r
+       //stream_update(R->Channel,0);\r
+\r
+       if (data & 0x80)\r
+       {\r
+               int r = (data & 0x70) >> 4;\r
+               int 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
+       else\r
+       {\r
+               int r = R->LastRegister;\r
+               int c = r/2;\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
+       }\r
+}\r
+\r
+/*\r
+WRITE8_HANDLER( SN76496_0_w ) {        SN76496Write(0,data); }\r
+WRITE8_HANDLER( SN76496_1_w ) {        SN76496Write(1,data); }\r
+WRITE8_HANDLER( SN76496_2_w ) {        SN76496Write(2,data); }\r
+WRITE8_HANDLER( SN76496_3_w ) {        SN76496Write(3,data); }\r
+WRITE8_HANDLER( SN76496_4_w ) {        SN76496Write(4,data); }\r
+*/\r
+\r
+//static\r
+void SN76496Update(short *buffer, int length, int stereo)\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
+               unsigned int out;\r
+               int left;\r
+\r
+\r
+               /* vol[] keeps track of how long each square wave stays */\r
+               /* in the 1 position during the sample period. */\r
+               vol[0] = vol[1] = vol[2] = vol[3] = 0;\r
+\r
+               for (i = 0;i < 3;i++)\r
+               {\r
+                       if (R->Output[i]) vol[i] += R->Count[i];\r
+                       R->Count[i] -= STEP;\r
+                       /* Period[i] is the half period of the square wave. Here, in each */\r
+                       /* loop I add Period[i] twice, so that at the end of the loop the */\r
+                       /* square wave is in the same status (0 or 1) it was at the start. */\r
+                       /* vol[i] is also incremented by Period[i], since the wave has been 1 */\r
+                       /* exactly half of the time, regardless of the initial position. */\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
+                       while (R->Count[i] <= 0)\r
+                       {\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
+                                       break;\r
+                               }\r
+                               R->Count[i] += R->Period[i];\r
+                               vol[i] += R->Period[i];\r
+                       }\r
+                       if (R->Output[i]) vol[i] -= R->Count[i];\r
+               }\r
+\r
+               left = STEP;\r
+               do\r
+               {\r
+                       int nextevent;\r
+\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->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
+\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
+                       *buffer += out;\r
+               if(stereo) buffer+=2; // only left for stereo, to be mixed to right later\r
+               else buffer++;\r
+\r
+               length--;\r
+       }\r
+}\r
+\r
+\r
+static void SN76496_set_clock(struct SN76496 *R,int clock)\r
+{\r
+\r
+       /* the base clock for the tone generators is the chip clock divided by 16; */\r
+       /* for the noise generator, it is clock / 256. */\r
+       /* Here we calculate the number of steps which happen during one sample */\r
+       /* at the given sample rate. No. of events = sample rate / (clock/16). */\r
+       /* STEP is a multiplier used to turn the fraction into a fixed point */\r
+       /* number. */\r
+       R->UpdateStep = ((double)STEP * R->SampleRate * 16) / clock;\r
+}\r
+\r
+\r
+static void SN76496_set_gain(struct SN76496 *R,int gain)\r
+{\r
+       int i;\r
+       double out;\r
+\r
+\r
+       gain &= 0xff;\r
+\r
+       /* increase max output basing on gain (0.2 dB per step) */\r
+       out = MAX_OUTPUT / 3;\r
+       while (gain-- > 0)\r
+               out *= 1.023292992;     /* = (10 ^ (0.2/20)) */\r
+\r
+       /* build volume table (2dB per step) */\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
+               else R->VolTable[i] = out;\r
+\r
+               out /= 1.258925412;     /* = 10 ^ (2/20) = 2dB */\r
+       }\r
+       R->VolTable[15] = 0;\r
+}\r
+\r
+\r
+//static\r
+int SN76496_init(int clock,int sample_rate)\r
+{\r
+       struct SN76496 *R = &ono_sn;\r
+       int i;\r
+\r
+       //R->Channel = stream_create(0,1, sample_rate,R,SN76496Update);\r
+       sn76496_regs = R->Register;\r
+\r
+       R->SampleRate = sample_rate;\r
+       SN76496_set_clock(R,clock);\r
+\r
+       for (i = 0;i < 4;i++) R->Volume[i] = 0;\r
+\r
+       R->LastRegister = 0;\r
+       for (i = 0;i < 8;i+=2)\r
+       {\r
+               R->Register[i] = 0;\r
+               R->Register[i + 1] = 0x0f;      /* volume = 0 */\r
+       }\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
+       R->RNG = NG_PRESET;\r
+       R->Output[3] = R->RNG & 1;\r
+\r
+       // added\r
+       SN76496_set_gain(R, 0);\r
+\r
+       return 0;\r
+}\r
+\r