8474c7036991c01b58fe0943a79b31e73d315b5f
[picodrive.git] / pico / sound / sn76496.c
1 /***************************************************************************\r
2 \r
3   sn76496.c\r
4 \r
5   Routines to emulate the Texas Instruments SN76489 / SN76496 programmable\r
6   tone /noise generator. Also known as (or at least compatible with) TMS9919.\r
7 \r
8   Noise emulation is not accurate due to lack of documentation. The noise\r
9   generator uses a shift register with a XOR-feedback network, but the exact\r
10   layout is unknown. It can be set for either period or white noise; again,\r
11   the details are unknown.\r
12 \r
13   28/03/2005 : Sebastien Chevalier\r
14   Update th SN76496Write func, according to SN76489 doc found on SMSPower.\r
15    - On write with 0x80 set to 0, when LastRegister is other then TONE,\r
16    the function is similar than update with 0x80 set to 1\r
17 ***************************************************************************/\r
18 \r
19 #ifndef __GNUC__\r
20 #pragma warning (disable:4244)\r
21 #endif\r
22 \r
23 #include "sn76496.h"\r
24 \r
25 #define MAX_OUTPUT 0x47ff // was 0x7fff\r
26 \r
27 #define STEP 0x10000\r
28 \r
29 \r
30 /* Formulas for noise generator */\r
31 /* bit0 = output */\r
32 \r
33 /* noise feedback for white noise mode (verified on real SN76489 by John Kortink) */\r
34 #define FB_WNOISE 0x14002       /* (16bits) bit16 = bit0(out) ^ bit2 ^ bit15 */\r
35 \r
36 /* noise feedback for periodic noise mode */\r
37 //#define FB_PNOISE 0x10000 /* 16bit rorate */\r
38 #define FB_PNOISE 0x08000   /* JH 981127 - fixes Do Run Run */\r
39 \r
40 /*\r
41 0x08000 is definitely wrong. The Master System conversion of Marble Madness\r
42 uses periodic noise as a baseline. With a 15-bit rotate, the bassline is\r
43 out of tune.\r
44 The 16-bit rotate has been confirmed against a real PAL Sega Master System 2.\r
45 Hope that helps the System E stuff, more news on the PSG as and when!\r
46 */\r
47 \r
48 /* noise generator start preset (for periodic noise) */\r
49 #define NG_PRESET 0x0f35\r
50 \r
51 \r
52 struct SN76496\r
53 {\r
54         //sound_stream * Channel;\r
55         int SampleRate;\r
56         unsigned int UpdateStep;\r
57         int VolTable[16];       /* volume table         */\r
58         int Register[8];        /* registers */\r
59         int LastRegister;       /* last register written */\r
60         int Volume[4];          /* volume of voice 0-2 and noise */\r
61         unsigned int RNG;               /* noise generator      */\r
62         int NoiseFB;            /* noise feedback mask */\r
63         int Period[4];\r
64         int Count[4];\r
65         int Output[4];\r
66         int pad[1];\r
67 };\r
68 \r
69 static struct SN76496 ono_sn; // one and only SN76496\r
70 int *sn76496_regs;\r
71 \r
72 //static\r
73 void SN76496Write(int data)\r
74 {\r
75         struct SN76496 *R = &ono_sn;\r
76         int n;\r
77 \r
78 \r
79         /* update the output buffer before changing the registers */\r
80         //stream_update(R->Channel,0);\r
81 \r
82         if (data & 0x80)\r
83         {\r
84                 int r = (data & 0x70) >> 4;\r
85                 int c = r/2;\r
86 \r
87                 R->LastRegister = r;\r
88                 R->Register[r] = (R->Register[r] & 0x3f0) | (data & 0x0f);\r
89                 switch (r)\r
90                 {\r
91                         case 0: /* tone 0 : frequency */\r
92                         case 2: /* tone 1 : frequency */\r
93                         case 4: /* tone 2 : frequency */\r
94                                 R->Period[c] = R->UpdateStep * R->Register[r];\r
95                                 if (R->Period[c] == 0) R->Period[c] = R->UpdateStep;\r
96                                 if (r == 4)\r
97                                 {\r
98                                         /* update noise shift frequency */\r
99                                         if ((R->Register[6] & 0x03) == 0x03)\r
100                                                 R->Period[3] = 2 * R->Period[2];\r
101                                 }\r
102                                 break;\r
103                         case 1: /* tone 0 : volume */\r
104                         case 3: /* tone 1 : volume */\r
105                         case 5: /* tone 2 : volume */\r
106                         case 7: /* noise  : volume */\r
107                                 R->Volume[c] = R->VolTable[data & 0x0f];\r
108                                 break;\r
109                         case 6: /* noise  : frequency, mode */\r
110                                 {\r
111                                         int n = R->Register[6];\r
112                                         R->NoiseFB = (n & 4) ? FB_WNOISE : FB_PNOISE;\r
113                                         n &= 3;\r
114                                         /* N/512,N/1024,N/2048,Tone #3 output */\r
115                                         R->Period[3] = ((n&3) == 3) ? 2 * R->Period[2] : (R->UpdateStep << (5+(n&3)));\r
116 \r
117                                         /* reset noise shifter */\r
118                                         R->RNG = NG_PRESET;\r
119                                         R->Output[3] = R->RNG & 1;\r
120                                 }\r
121                                 break;\r
122                 }\r
123         }\r
124         else\r
125         {\r
126                 int r = R->LastRegister;\r
127                 int c = r/2;\r
128 \r
129                 switch (r)\r
130                 {\r
131                         case 0: /* tone 0 : frequency */\r
132                         case 2: /* tone 1 : frequency */\r
133                         case 4: /* tone 2 : frequency */\r
134                                 R->Register[r] = (R->Register[r] & 0x0f) | ((data & 0x3f) << 4);\r
135                                 R->Period[c] = R->UpdateStep * R->Register[r];\r
136                                 if (R->Period[c] == 0) R->Period[c] = R->UpdateStep;\r
137                                 if (r == 4)\r
138                                 {\r
139                                         /* update noise shift frequency */\r
140                                         if ((R->Register[6] & 0x03) == 0x03)\r
141                                                 R->Period[3] = 2 * R->Period[2];\r
142                                 }\r
143                                 break;\r
144                         case 1: /* tone 0 : volume */\r
145                         case 3: /* tone 1 : volume */\r
146                         case 5: /* tone 2 : volume */\r
147                         case 7: /* noise  : volume */\r
148                                 R->Volume[c] = R->VolTable[data & 0x0f];\r
149                                 R->Register[r] = (R->Register[r] & 0x3f0) | (data & 0x0f);\r
150                                 break;\r
151                         case 6: /* noise  : frequency, mode */\r
152                                 {\r
153                                         R->Register[r] = (R->Register[r] & 0x3f0) | (data & 0x0f);\r
154                                         n = R->Register[6];\r
155                                         R->NoiseFB = (n & 4) ? FB_WNOISE : FB_PNOISE;\r
156                                         n &= 3;\r
157                                         /* N/512,N/1024,N/2048,Tone #3 output */\r
158                                         R->Period[3] = ((n&3) == 3) ? 2 * R->Period[2] : (R->UpdateStep << (5+(n&3)));\r
159 \r
160                                         /* reset noise shifter */\r
161                                         R->RNG = NG_PRESET;\r
162                                         R->Output[3] = R->RNG & 1;\r
163                                 }\r
164                                 break;\r
165                 }\r
166         }\r
167 }\r
168 \r
169 /*\r
170 WRITE8_HANDLER( SN76496_0_w ) { SN76496Write(0,data); }\r
171 WRITE8_HANDLER( SN76496_1_w ) { SN76496Write(1,data); }\r
172 WRITE8_HANDLER( SN76496_2_w ) { SN76496Write(2,data); }\r
173 WRITE8_HANDLER( SN76496_3_w ) { SN76496Write(3,data); }\r
174 WRITE8_HANDLER( SN76496_4_w ) { SN76496Write(4,data); }\r
175 */\r
176 \r
177 //static\r
178 void SN76496Update(short *buffer, int length, int stereo)\r
179 {\r
180         int i;\r
181         struct SN76496 *R = &ono_sn;\r
182 \r
183         /* If the volume is 0, increase the counter */\r
184         for (i = 0;i < 4;i++)\r
185         {\r
186                 if (R->Volume[i] == 0)\r
187                 {\r
188                         /* note that I do count += length, NOT count = length + 1. You might think */\r
189                         /* it's the same since the volume is 0, but doing the latter could cause */\r
190                         /* interferencies when the program is rapidly modulating the volume. */\r
191                         if (R->Count[i] <= length*STEP) R->Count[i] += length*STEP;\r
192                 }\r
193         }\r
194 \r
195         while (length > 0)\r
196         {\r
197                 int vol[4];\r
198                 unsigned int out;\r
199                 int left;\r
200 \r
201 \r
202                 /* vol[] keeps track of how long each square wave stays */\r
203                 /* in the 1 position during the sample period. */\r
204                 vol[0] = vol[1] = vol[2] = vol[3] = 0;\r
205 \r
206                 for (i = 0;i < 3;i++)\r
207                 {\r
208                         if (R->Output[i]) vol[i] += R->Count[i];\r
209                         R->Count[i] -= STEP;\r
210                         /* Period[i] is the half period of the square wave. Here, in each */\r
211                         /* loop I add Period[i] twice, so that at the end of the loop the */\r
212                         /* square wave is in the same status (0 or 1) it was at the start. */\r
213                         /* vol[i] is also incremented by Period[i], since the wave has been 1 */\r
214                         /* exactly half of the time, regardless of the initial position. */\r
215                         /* If we exit the loop in the middle, Output[i] has to be inverted */\r
216                         /* and vol[i] incremented only if the exit status of the square */\r
217                         /* wave is 1. */\r
218                         while (R->Count[i] <= 0)\r
219                         {\r
220                                 R->Count[i] += R->Period[i];\r
221                                 if (R->Count[i] > 0)\r
222                                 {\r
223                                         R->Output[i] ^= 1;\r
224                                         if (R->Output[i]) vol[i] += R->Period[i];\r
225                                         break;\r
226                                 }\r
227                                 R->Count[i] += R->Period[i];\r
228                                 vol[i] += R->Period[i];\r
229                         }\r
230                         if (R->Output[i]) vol[i] -= R->Count[i];\r
231                 }\r
232 \r
233                 left = STEP;\r
234                 do\r
235                 {\r
236                         int nextevent;\r
237 \r
238                         if (R->Count[3] < left) nextevent = R->Count[3];\r
239                         else nextevent = left;\r
240 \r
241                         if (R->Output[3]) vol[3] += R->Count[3];\r
242                         R->Count[3] -= nextevent;\r
243                         if (R->Count[3] <= 0)\r
244                         {\r
245                                 if (R->RNG & 1) R->RNG ^= R->NoiseFB;\r
246                                 R->RNG >>= 1;\r
247                                 R->Output[3] = R->RNG & 1;\r
248                                 R->Count[3] += R->Period[3];\r
249                                 if (R->Output[3]) vol[3] += R->Period[3];\r
250                         }\r
251                         if (R->Output[3]) vol[3] -= R->Count[3];\r
252 \r
253                         left -= nextevent;\r
254                 } while (left > 0);\r
255 \r
256                 out = vol[0] * R->Volume[0] + vol[1] * R->Volume[1] +\r
257                                 vol[2] * R->Volume[2] + vol[3] * R->Volume[3];\r
258 \r
259                 if (out > MAX_OUTPUT * STEP) out = MAX_OUTPUT * STEP;\r
260 \r
261                 if ((out /= STEP)) // will be optimized to shift; max 0x47ff = 18431\r
262                         *buffer += out;\r
263                 if(stereo) buffer+=2; // only left for stereo, to be mixed to right later\r
264                 else buffer++;\r
265 \r
266                 length--;\r
267         }\r
268 }\r
269 \r
270 \r
271 static void SN76496_set_clock(struct SN76496 *R,int clock)\r
272 {\r
273 \r
274         /* the base clock for the tone generators is the chip clock divided by 16; */\r
275         /* for the noise generator, it is clock / 256. */\r
276         /* Here we calculate the number of steps which happen during one sample */\r
277         /* at the given sample rate. No. of events = sample rate / (clock/16). */\r
278         /* STEP is a multiplier used to turn the fraction into a fixed point */\r
279         /* number. */\r
280         R->UpdateStep = ((double)STEP * R->SampleRate * 16) / clock;\r
281 }\r
282 \r
283 \r
284 static void SN76496_set_gain(struct SN76496 *R,int gain)\r
285 {\r
286         int i;\r
287         double out;\r
288 \r
289 \r
290         gain &= 0xff;\r
291 \r
292         /* increase max output basing on gain (0.2 dB per step) */\r
293         out = MAX_OUTPUT / 3;\r
294         while (gain-- > 0)\r
295                 out *= 1.023292992;     /* = (10 ^ (0.2/20)) */\r
296 \r
297         /* build volume table (2dB per step) */\r
298         for (i = 0;i < 15;i++)\r
299         {\r
300                 /* limit volume to avoid clipping */\r
301                 if (out > MAX_OUTPUT / 3) R->VolTable[i] = MAX_OUTPUT / 3;\r
302                 else R->VolTable[i] = out;\r
303 \r
304                 out /= 1.258925412;     /* = 10 ^ (2/20) = 2dB */\r
305         }\r
306         R->VolTable[15] = 0;\r
307 }\r
308 \r
309 \r
310 //static\r
311 int SN76496_init(int clock,int sample_rate)\r
312 {\r
313         struct SN76496 *R = &ono_sn;\r
314         int i;\r
315 \r
316         //R->Channel = stream_create(0,1, sample_rate,R,SN76496Update);\r
317         sn76496_regs = R->Register;\r
318 \r
319         R->SampleRate = sample_rate;\r
320         SN76496_set_clock(R,clock);\r
321 \r
322         for (i = 0;i < 4;i++) R->Volume[i] = 0;\r
323 \r
324         R->LastRegister = 0;\r
325         for (i = 0;i < 8;i+=2)\r
326         {\r
327                 R->Register[i] = 0;\r
328                 R->Register[i + 1] = 0x0f;      /* volume = 0 */\r
329         }\r
330 \r
331         for (i = 0;i < 4;i++)\r
332         {\r
333                 R->Output[i] = 0;\r
334                 R->Period[i] = R->Count[i] = R->UpdateStep;\r
335         }\r
336         R->RNG = NG_PRESET;\r
337         R->Output[3] = R->RNG & 1;\r
338 \r
339         // added\r
340         SN76496_set_gain(R, 0);\r
341 \r
342         return 0;\r
343 }\r
344 \r