| 1 |
// license:BSD-3-Clause |
| 2 |
// copyright-holders:Nicola Salmoria |
| 3 |
/*************************************************************************** |
| 4 |
|
| 5 |
sn76496.c |
| 6 |
by Nicola Salmoria |
| 7 |
with contributions by others |
| 8 |
|
| 9 |
Routines to emulate the: |
| 10 |
Texas Instruments SN76489, SN76489A, SN76494/SN76496 |
| 11 |
( Also known as, or at least compatible with, the TMS9919 and SN94624.) |
| 12 |
and the Sega 'PSG' used on the Master System, Game Gear, and Megadrive/Genesis |
| 13 |
This chip is known as the Programmable Sound Generator, or PSG, and is a 4 |
| 14 |
channel sound generator, with three squarewave channels and a noise/arbitrary |
| 15 |
duty cycle channel. |
| 16 |
|
| 17 |
Noise emulation for all verified chips should be accurate: |
| 18 |
|
| 19 |
** SN76489 uses a 15-bit shift register with taps on bits D and E, output on E, |
| 20 |
XOR function. |
| 21 |
It uses a 15-bit ring buffer for periodic noise/arbitrary duty cycle. |
| 22 |
Its output is inverted. |
| 23 |
** SN94624 is the same as SN76489 but lacks the /8 divider on its clock input. |
| 24 |
** SN76489A uses a 15-bit shift register with taps on bits D and E, output on F, |
| 25 |
XOR function. |
| 26 |
It uses a 15-bit ring buffer for periodic noise/arbitrary duty cycle. |
| 27 |
Its output is not inverted. |
| 28 |
** SN76494 is the same as SN76489A but lacks the /8 divider on its clock input. |
| 29 |
** SN76496 is identical in operation to the SN76489A, but the audio input on pin 9 is |
| 30 |
documented. |
| 31 |
All the TI-made PSG chips have an audio input line which is mixed with the 4 channels |
| 32 |
of output. (It is undocumented and may not function properly on the sn76489, 76489a |
| 33 |
and 76494; the sn76489a input is mentioned in datasheets for the tms5200) |
| 34 |
All the TI-made PSG chips act as if the frequency was set to 0x400 if 0 is |
| 35 |
written to the frequency register. |
| 36 |
** Sega Master System III/MD/Genesis PSG uses a 16-bit shift register with taps |
| 37 |
on bits C and F, output on F |
| 38 |
It uses a 16-bit ring buffer for periodic noise/arbitrary duty cycle. |
| 39 |
(whether it uses an XOR or XNOR needs to be verified, assumed XOR) |
| 40 |
(whether output is inverted or not needs to be verified, assumed to be inverted) |
| 41 |
** Sega Game Gear PSG is identical to the SMS3/MD/Genesis one except it has an |
| 42 |
extra register for mapping which channels go to which speaker. |
| 43 |
The register, connected to a z80 port, means: |
| 44 |
for bits 7 6 5 4 3 2 1 0 |
| 45 |
L3 L2 L1 L0 R3 R2 R1 R0 |
| 46 |
Noise is an XOR function, and audio output is negated before being output. |
| 47 |
All the Sega-made PSG chips act as if the frequency was set to 0 if 0 is written |
| 48 |
to the frequency register. |
| 49 |
** NCR8496 (as used on the Tandy 1000TX) is similar to the SN76489 but with a |
| 50 |
different noise LFSR pattern: taps on bits A and E, output on E, XNOR function |
| 51 |
It uses a 15-bit ring buffer for periodic noise/arbitrary duty cycle. |
| 52 |
Its output is inverted. |
| 53 |
** PSSJ-3 (as used on the later Tandy 1000 series computers) is the same as the |
| 54 |
NCR8496 with the exception that its output is not inverted. |
| 55 |
|
| 56 |
28/03/2005 : Sebastien Chevalier |
| 57 |
Update th SN76496Write func, according to SN76489 doc found on SMSPower. |
| 58 |
- On write with 0x80 set to 0, when LastRegister is other then TONE, |
| 59 |
the function is similar than update with 0x80 set to 1 |
| 60 |
|
| 61 |
23/04/2007 : Lord Nightmare |
| 62 |
Major update, implement all three different noise generation algorithms and a |
| 63 |
set_variant call to discern among them. |
| 64 |
|
| 65 |
28/04/2009 : Lord Nightmare |
| 66 |
Add READY line readback; cleaned up struct a bit. Cleaned up comments. |
| 67 |
Add more TODOs. Fixed some unsaved savestate related stuff. |
| 68 |
|
| 69 |
04/11/2009 : Lord Nightmare |
| 70 |
Changed the way that the invert works (it now selects between XOR and XNOR |
| 71 |
for the taps), and added R->OldNoise to simulate the extra 0 that is always |
| 72 |
output before the noise LFSR contents are after an LFSR reset. |
| 73 |
This fixes SN76489/A to match chips. Added SN94624. |
| 74 |
|
| 75 |
14/11/2009 : Lord Nightmare |
| 76 |
Removed STEP mess, vastly simplifying the code. Made output bipolar rather |
| 77 |
than always above the 0 line, but disabled that code due to pending issues. |
| 78 |
|
| 79 |
16/11/2009 : Lord Nightmare |
| 80 |
Fix screeching in regulus: When summing together four equal channels, the |
| 81 |
size of the max amplitude per channel should be 1/4 of the max range, not |
| 82 |
1/3. Added NCR8496. |
| 83 |
|
| 84 |
18/11/2009 : Lord Nightmare |
| 85 |
Modify Init functions to support negating the audio output. The gamegear |
| 86 |
psg does this. Change gamegear and sega psgs to use XOR rather than XNOR |
| 87 |
based on testing. Got rid of R->OldNoise and fixed taps accordingly. |
| 88 |
Added stereo support for game gear. |
| 89 |
|
| 90 |
15/01/2010 : Lord Nightmare |
| 91 |
Fix an issue with SN76489 and SN76489A having the wrong periodic noise periods. |
| 92 |
Note that properly emulating the noise cycle bit timing accurately may require |
| 93 |
extensive rewriting. |
| 94 |
|
| 95 |
24/01/2010: Lord Nightmare |
| 96 |
Implement periodic noise as forcing one of the XNOR or XOR taps to 1 or 0 respectively. |
| 97 |
Thanks to PlgDavid for providing samples which helped immensely here. |
| 98 |
Added true clock divider emulation, so sn94624 and sn76494 run 8x faster than |
| 99 |
the others, as in real life. |
| 100 |
|
| 101 |
15/02/2010: Lord Nightmare & Michael Zapf (additional testing by PlgDavid) |
| 102 |
Fix noise period when set to mirror channel 3 and channel 3 period is set to 0 (tested on hardware for noise, wave needs tests) - MZ |
| 103 |
Fix phase of noise on sn94624 and sn76489; all chips use a standard XOR, the only inversion is the output itself - LN, Plgdavid |
| 104 |
Thanks to PlgDavid and Michael Zapf for providing samples which helped immensely here. |
| 105 |
|
| 106 |
23/02/2011: Lord Nightmare & Enik |
| 107 |
Made it so the Sega PSG chips have a frequency of 0 if 0 is written to the |
| 108 |
frequency register, while the others have 0x400 as before. Should fix a bug |
| 109 |
or two on sega games, particularly Vigilante on Sega Master System. Verified |
| 110 |
on SMS hardware. |
| 111 |
|
| 112 |
27/06/2012: Michael Zapf |
| 113 |
Converted to modern device, legacy devices were gradually removed afterwards. |
| 114 |
|
| 115 |
16/09/2015: Lord Nightmare |
| 116 |
Fix PSG chips to have volume reg inited on reset to 0x0 based on tests by |
| 117 |
ValleyBell. Made Sega PSG chips start up with register 0x3 selected (volume |
| 118 |
for channel 2) based on hardware tests by Nemesis. |
| 119 |
|
| 120 |
03/09/2018: Lord Nightmare, Qbix, ValleyBell, NewRisingSun |
| 121 |
* renamed the NCR8496 to its correct name, based on chip pictures on VGMPF |
| 122 |
* fixed NCR8496's noise LFSR behavior so it is only reset if the mode bit in |
| 123 |
register 6 is changed. |
| 124 |
* NCR8496's LFSR feedback function is an XNOR, which is now supported. |
| 125 |
* add PSSJ-3 support for the later Tandy 1000 series computers. |
| 126 |
* NCR8496's output is inverted, PSSJ-3's output is not. |
| 127 |
|
| 128 |
10/12/2019: Michael Zapf |
| 129 |
* READY line handling by own emu_timer, not depending on sound_stream_update |
| 130 |
|
| 131 |
additional modifications by tildearrow, cam900 for furnace |
| 132 |
|
| 133 |
TODO: * Implement the TMS9919 - any difference to sn94624? |
| 134 |
* Implement the T6W28; has registers in a weird order, needs writes |
| 135 |
to be 'sanitized' first. Also is stereo, similar to game gear. |
| 136 |
* Factor out common code so that the SAA1099 can share some code. |
| 137 |
* verify NCR8496/PSSJ-3 behavior on write to mirrored registers; unlike the |
| 138 |
other variants, the NCR-derived variants are implied to ignore writes to |
| 139 |
regs 1,3,5,6,7 if 0x80 is not set. This needs to be verified on real hardware. |
| 140 |
|
| 141 |
***************************************************************************/ |
| 142 |
|
| 143 |
#include "sn76496.h" |
| 144 |
#include <stdio.h> |
| 145 |
|
| 146 |
#define MAX_OUTPUT 0x7fff |
| 147 |
|
| 148 |
//#define NOISE_START 0x8000 |
| 149 |
//#define NOISE_START 0x0f35 |
| 150 |
|
| 151 |
sn76496_base_device::sn76496_base_device( |
| 152 |
int feedbackmask, |
| 153 |
int noise_start, |
| 154 |
int noisetap1, |
| 155 |
int noisetap2, |
| 156 |
bool negate, |
| 157 |
bool stereo, |
| 158 |
bool ncr, |
| 159 |
bool sega) |
| 160 |
: m_feedback_mask(feedbackmask) |
| 161 |
, m_noise_start(noise_start) |
| 162 |
, m_whitenoise_tap1(noisetap1) |
| 163 |
, m_whitenoise_tap2(noisetap2) |
| 164 |
, m_negate(negate) |
| 165 |
, m_stereo(stereo) |
| 166 |
, m_ncr_style_psg(ncr) |
| 167 |
, m_sega_style_psg(sega) |
| 168 |
{ |
| 169 |
} |
| 170 |
|
| 171 |
sn76496_device::sn76496_device() |
| 172 |
: sn76496_base_device(0x10000, 0x04, 0x08, false, false, false, true) |
| 173 |
{ |
| 174 |
} |
| 175 |
|
| 176 |
y2404_device::y2404_device() |
| 177 |
: sn76496_base_device(0x10000, 0x04, 0x08, false, false, false, true) |
| 178 |
{ |
| 179 |
} |
| 180 |
|
| 181 |
sn76489_device::sn76489_device() |
| 182 |
: sn76496_base_device(0x4000, 0x01, 0x02, true, false, false, true) |
| 183 |
{ |
| 184 |
} |
| 185 |
|
| 186 |
sn76489a_device::sn76489a_device() |
| 187 |
: sn76496_base_device(0x10000, 0x04, 0x08, false, false, false, true) |
| 188 |
{ |
| 189 |
} |
| 190 |
|
| 191 |
sn76494_device::sn76494_device() |
| 192 |
: sn76496_base_device(0x10000, 0x04, 0x08, false, false, false, true) |
| 193 |
{ |
| 194 |
} |
| 195 |
|
| 196 |
sn94624_device::sn94624_device() |
| 197 |
: sn76496_base_device(0x4000, 0x01, 0x02, true, false, false, true) |
| 198 |
{ |
| 199 |
} |
| 200 |
|
| 201 |
ncr8496_device::ncr8496_device() |
| 202 |
: sn76496_base_device(0x8000, 0x02, 0x20, true, false, true, true) |
| 203 |
{ |
| 204 |
} |
| 205 |
|
| 206 |
pssj3_device::pssj3_device() |
| 207 |
: sn76496_base_device(0x8000, 0x02, 0x20, false, false, true, true) |
| 208 |
{ |
| 209 |
} |
| 210 |
|
| 211 |
gamegear_device::gamegear_device() |
| 212 |
: sn76496_base_device(0x8000, 0x01, 0x08, true, true, false, false) |
| 213 |
{ |
| 214 |
} |
| 215 |
|
| 216 |
segapsg_device::segapsg_device() |
| 217 |
: sn76496_base_device(0x8000, 0x01, 0x08, true, false, false, false) |
| 218 |
{ |
| 219 |
} |
| 220 |
|
| 221 |
void sn76496_base_device::device_start() |
| 222 |
{ |
| 223 |
int i; |
| 224 |
double out; |
| 225 |
int gain; |
| 226 |
|
| 227 |
for (i = 0; i < 4; i++) m_volume[i] = 0; |
| 228 |
|
| 229 |
m_last_register = m_sega_style_psg?3:0; // Sega VDP PSG defaults to selected period reg for 2nd channel |
| 230 |
for (i = 0; i < 8; i+=2) |
| 231 |
{ |
| 232 |
m_register[i] = 0; |
| 233 |
m_register[i + 1] = 0x0; // volume = 0x0 (max volume) on reset; this needs testing on chips other than SN76489A and Sega VDP PSG |
| 234 |
} |
| 235 |
|
| 236 |
for (i = 0; i < 4; i++) |
| 237 |
{ |
| 238 |
m_output[i] = 0; |
| 239 |
m_period[i] = 0; |
| 240 |
m_count[i] = 0; |
| 241 |
} |
| 242 |
|
| 243 |
m_RNG = m_feedback_mask; |
| 244 |
m_output[3] = m_RNG & 1; |
| 245 |
|
| 246 |
m_stereo_mask = 0xFF; // all channels enabled |
| 247 |
|
| 248 |
// set gain |
| 249 |
gain = 0; |
| 250 |
|
| 251 |
gain &= 0xff; |
| 252 |
|
| 253 |
// increase max output basing on gain (0.2 dB per step) |
| 254 |
out = MAX_OUTPUT / 4; // four channels, each gets 1/4 of the total range |
| 255 |
while (gain-- > 0) |
| 256 |
out *= 1.023292992; // = (10 ^ (0.2/20)) |
| 257 |
|
| 258 |
// build volume table (2dB per step) |
| 259 |
for (i = 0; i < 15; i++) |
| 260 |
{ |
| 261 |
// limit volume to avoid clipping |
| 262 |
if (out > MAX_OUTPUT / 4) m_vol_table[i] = MAX_OUTPUT / 4; |
| 263 |
else m_vol_table[i] = out; |
| 264 |
|
| 265 |
out /= 1.258925412; /* = 10 ^ (2/20) = 2dB */ |
| 266 |
} |
| 267 |
m_vol_table[15] = 0; |
| 268 |
|
| 269 |
m_ready_state = true; |
| 270 |
} |
| 271 |
|
| 272 |
void sn76496_base_device::stereo_w(u8 data) |
| 273 |
{ |
| 274 |
if (m_stereo) m_stereo_mask = data; |
| 275 |
} |
| 276 |
|
| 277 |
void sn76496_base_device::write(u8 data) |
| 278 |
{ |
| 279 |
int n, r, c; |
| 280 |
|
| 281 |
if (data & 0x80) |
| 282 |
{ |
| 283 |
r = (data & 0x70) >> 4; |
| 284 |
m_last_register = r; |
| 285 |
if (((m_ncr_style_psg) && (r == 6)) && ((data&0x04) != (m_register[6]&0x04))) m_RNG = m_noise_start; // NCR-style PSG resets the LFSR only on a mode write which actually changes the state of bit 2 of register 6 |
| 286 |
m_register[r] = (m_register[r] & 0x3f0) | (data & 0x0f); |
| 287 |
} |
| 288 |
else |
| 289 |
{ |
| 290 |
r = m_last_register; |
| 291 |
//if ((m_ncr_style_psg) && ((r & 1) || (r == 6))) return; // NCR-style PSG ignores writes to regs 1, 3, 5, 6 and 7 with bit 7 clear; this behavior is not verified on hardware yet, uncomment it once verified. |
| 292 |
} |
| 293 |
|
| 294 |
c = r >> 1; |
| 295 |
switch (r) |
| 296 |
{ |
| 297 |
case 0: // tone 0: frequency |
| 298 |
case 2: // tone 1: frequency |
| 299 |
case 4: // tone 2: frequency |
| 300 |
if ((data & 0x80) == 0) m_register[r] = (m_register[r] & 0x0f) | ((data & 0x3f) << 4); |
| 301 |
if ((m_register[r] != 0) || (!m_sega_style_psg)) m_period[c] = m_register[r]; |
| 302 |
else m_period[c] = 0x400; |
| 303 |
|
| 304 |
if (r == 4) |
| 305 |
{ |
| 306 |
// update noise shift frequency |
| 307 |
if ((m_register[6] & 0x03) == 0x03) m_period[3] = m_period[2]<<1; |
| 308 |
} |
| 309 |
break; |
| 310 |
case 1: // tone 0: volume |
| 311 |
case 3: // tone 1: volume |
| 312 |
case 5: // tone 2: volume |
| 313 |
case 7: // noise: volume |
| 314 |
m_volume[c] = m_vol_table[data & 0x0f]; |
| 315 |
if ((data & 0x80) == 0) m_register[r] = (m_register[r] & 0x3f0) | (data & 0x0f); |
| 316 |
break; |
| 317 |
case 6: // noise: frequency, mode |
| 318 |
{ |
| 319 |
if ((data & 0x80) == 0) printf("sn76496_base_device: write to reg 6 with bit 7 clear; data was %03x, new write is %02x! report this to LN!\n", m_register[6], data); |
| 320 |
if ((data & 0x80) == 0) m_register[r] = (m_register[r] & 0x3f0) | (data & 0x0f); |
| 321 |
n = m_register[6]; |
| 322 |
// N/512,N/1024,N/2048,Tone #3 output |
| 323 |
m_period[3] = ((n&3) == 3)? (m_period[2]<<1) : (1 << (5+(n&3))); |
| 324 |
if (!(m_ncr_style_psg)) m_RNG = m_noise_start; |
| 325 |
} |
| 326 |
break; |
| 327 |
} |
| 328 |
|
| 329 |
//m_ready_state = false; |
| 330 |
} |
| 331 |
|
| 332 |
inline bool sn76496_base_device::in_noise_mode() |
| 333 |
{ |
| 334 |
return ((m_register[6] & 4)!=0); |
| 335 |
} |
| 336 |
|
| 337 |
void sn76496_base_device::sound_stream_update(short* outputs, int advance) |
| 338 |
{ |
| 339 |
int i; |
| 340 |
|
| 341 |
int16_t out; |
| 342 |
int16_t out2 = 0; |
| 343 |
|
| 344 |
// handle channels 0,1,2 |
| 345 |
for (i = 0; i < 3; i++) |
| 346 |
{ |
| 347 |
m_count[i]-=advance; |
| 348 |
if (m_count[i] <= 0) |
| 349 |
{ |
| 350 |
m_output[i] ^= 1; |
| 351 |
m_count[i] = m_period[i]; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
// handle channel 3 |
| 356 |
m_count[3]-=advance; |
| 357 |
if (m_count[3] <= 0) |
| 358 |
{ |
| 359 |
// if noisemode is 1, both taps are enabled |
| 360 |
// if noisemode is 0, the lower tap, whitenoisetap2, is held at 0 |
| 361 |
// The != was a bit-XOR (^) before |
| 362 |
if (((m_RNG & m_whitenoise_tap1)!=0) != (((int32_t)(m_RNG & m_whitenoise_tap2)!=(m_ncr_style_psg?m_whitenoise_tap2:0)) && in_noise_mode())) |
| 363 |
{ |
| 364 |
m_RNG >>= 1; |
| 365 |
m_RNG |= m_feedback_mask; |
| 366 |
} |
| 367 |
else |
| 368 |
{ |
| 369 |
m_RNG >>= 1; |
| 370 |
} |
| 371 |
m_output[3] = m_RNG & 1; |
| 372 |
|
| 373 |
m_count[3] = m_period[3]; |
| 374 |
} |
| 375 |
|
| 376 |
if (m_stereo) |
| 377 |
{ |
| 378 |
out = ((((m_stereo_mask & 0x10)!=0) && (m_output[0]!=0))? m_volume[0] : 0) |
| 379 |
+ ((((m_stereo_mask & 0x20)!=0) && (m_output[1]!=0))? m_volume[1] : 0) |
| 380 |
+ ((((m_stereo_mask & 0x40)!=0) && (m_output[2]!=0))? m_volume[2] : 0) |
| 381 |
+ ((((m_stereo_mask & 0x80)!=0) && (m_output[3]!=0))? m_volume[3] : 0); |
| 382 |
|
| 383 |
out2= ((((m_stereo_mask & 0x1)!=0) && (m_output[0]!=0))? m_volume[0] : 0) |
| 384 |
+ ((((m_stereo_mask & 0x2)!=0) && (m_output[1]!=0))? m_volume[1] : 0) |
| 385 |
+ ((((m_stereo_mask & 0x4)!=0) && (m_output[2]!=0))? m_volume[2] : 0) |
| 386 |
+ ((((m_stereo_mask & 0x8)!=0) && (m_output[3]!=0))? m_volume[3] : 0); |
| 387 |
} |
| 388 |
else |
| 389 |
{ |
| 390 |
out= ((m_output[0]!=0)? m_volume[0]:0) |
| 391 |
+((m_output[1]!=0)? m_volume[1]:0) |
| 392 |
+((m_output[2]!=0)? m_volume[2]:0) |
| 393 |
+((m_output[3]!=0)? m_volume[3]:0); |
| 394 |
} |
| 395 |
|
| 396 |
if (m_negate) { out = -out; out2 = -out2; } |
| 397 |
|
| 398 |
outputs[0]=out; |
| 399 |
if (m_stereo) |
| 400 |
outputs[1]=out2; |
| 401 |
} |