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