src/emu/sound/sn76496.c
| r7156 | r7157 | |
| 19 | 19 | ** SN76489A uses a 15-bit shift register with taps on bits D and E, output on F, |
| 20 | 20 | XNOR function |
| 21 | 21 | It uses a 15-bit ring buffer for periodic noise/arbitrary duty cycle. |
| 22 | | ** SN76494 and SN76496 are PROBABLY identical in operation to the SN76489A |
| 23 | | They have an audio input line which is mixed with the 4 channels of output. |
| 22 | ** SN76494 is the same as SN76489A but lacks the /8 divider on its clock input. |
| 23 | ** SN76496 is PROBABLY identical in operation to the SN76489A, need more info. |
| 24 | All the SN7xxxx chips have an audio input line which is mixed with the 4 channels |
| 25 | of output. |
| 24 | 26 | ** Sega Master System III/MD/Genesis PSG uses a 16-bit shift register with taps |
| 25 | 27 | on bits C and F, output on F |
| 26 | 28 | It uses a 16-bit ring buffer for periodic noise/arbitrary duty cycle. |
| r7156 | r7157 | |
| 70 | 72 | based on testing. Got rid of R->OldNoise and fixed taps accordingly. |
| 71 | 73 | Added stereo support for game gear. |
| 72 | 74 | |
| 73 | | 11/15/2010 : Lord Nightmare |
| 75 | 15/01/2010 : Lord Nightmare |
| 74 | 76 | Fix an issue with SN76489 and SN76489A having the wrong periodic noise periods. |
| 75 | 77 | Note that properly emulating the noise cycle bit timing accurately may require |
| 76 | 78 | extensive rewriting. |
| 79 | |
| 80 | 17/01/2010: Lord Nightmare |
| 81 | Implement periodic noise as forcing one of the XNOR or XOR taps to 1 or 0 respectively. |
| 82 | Thanks to PlgDavid for providing samples which helped immensely here. |
| 83 | Added true clock divider emulation, so sn94624 and sn76494 run 8x faster than |
| 84 | the others, as in real life. |
| 77 | 85 | |
| 78 | | TODO: * Implement the TMS9919 which is an earlier version, possibly |
| 79 | | lacking the /8 clock divider, of the SN76489, and hence would |
| 80 | | have a max clock of 500Khz and 4 clocks per sample, as opposed to |
| 81 | | max of 4Mhz and 32 clocks per sample on the SN76489A. |
| 86 | TODO: * Implement the TMS9919 - any difference to sn94624? |
| 82 | 87 | * Implement the T6W28; has registers in a weird order, needs writes |
| 83 | 88 | to be 'sanitized' first. Also is stereo, similar to game gear. |
| 84 | 89 | * Test the NCR7496; Smspower says the whitenoise taps are A and E, |
| 85 | 90 | but this needs verification on real hardware. |
| 86 | 91 | * Factor out common code so that the SAA1099 can share some code. |
| 87 | | * Implement periodic noise as using a pointer to a table rather than a |
| 88 | | self feeding lfsr. Kold666's recording of dorunrun proves this is the |
| 89 | | right way. |
| 90 | | * Fix the whitenoise bit timing so that dorunrun matches Kold666's PCB sample. |
| 91 | 92 | ***************************************************************************/ |
| 92 | 93 | |
| 93 | 94 | #include "emu.h" |
| r7156 | r7157 | |
| 96 | 97 | |
| 97 | 98 | |
| 98 | 99 | #define MAX_OUTPUT 0x7fff |
| 99 | | // make the above 0x3fff if using bipolar output |
| 100 | 100 | #define NOISEMODE (R->Register[6]&4)?1:0 |
| 101 | 101 | |
| 102 | 102 | |
| r7156 | r7157 | |
| 109 | 109 | INT32 LastRegister; /* last register written */ |
| 110 | 110 | INT32 Volume[4]; /* db volume of voice 0-2 and noise */ |
| 111 | 111 | UINT32 RNG; /* noise generator LFSR*/ |
| 112 | INT32 ClockDivider; /* clock divider */ |
| 113 | INT32 CurrentClock; |
| 112 | 114 | INT32 FeedbackMask; /* mask for feedback */ |
| 113 | | INT32 WhitenoiseTaps; /* mask for white noise taps */ |
| 115 | INT32 WhitenoiseTap1; /* mask for white noise tap 1 (higher one, usually bit 14) */ |
| 116 | INT32 WhitenoiseTap2; /* mask for white noise tap 2 (lower one, usually bit 13)*/ |
| 114 | 117 | INT32 FeedbackInvert; /* feedback invert flag (xor vs xnor) */ |
| 115 | 118 | INT32 Negate; /* output negate flag */ |
| 116 | 119 | INT32 Stereo; /* whether we're dealing with stereo or not */ |
| r7156 | r7157 | |
| 207 | 210 | break; |
| 208 | 211 | case 6: /* noise : frequency, mode */ |
| 209 | 212 | { |
| 210 | | if ((data & 0x80) == 0) R->Register[r] = (R->Register[r] & 0x3f0) | (data & 0x0f); |
| 213 | if ((data & 0x80) == 0) logerror("sn76489: write to reg 6 with bit 7 clear; data was %03x, new write is %02x! report this to LN!\n", R->Register[6], data); |
| 214 | if ((data & 0x80) == 0) R->Register[r] = (R->Register[r] & 0x3f0) | (data & 0x0f); |
| 211 | 215 | n = R->Register[6]; |
| 212 | 216 | /* N/512,N/1024,N/2048,Tone #3 output */ |
| 213 | 217 | R->Period[3] = ((n&3) == 3) ? 2 * R->Period[2] : (1 << (5+(n&3))); |
| 214 | | /* Reset noise shifter */ |
| 215 | 218 | R->RNG = R->FeedbackMask; |
| 216 | 219 | R->Output[3] = R->RNG & 1; |
| 217 | 220 | } |
| r7156 | r7157 | |
| 225 | 228 | sn76496_state *R = (sn76496_state *)param; |
| 226 | 229 | stream_sample_t *lbuffer = outputs[0]; |
| 227 | 230 | stream_sample_t *rbuffer = (R->Stereo)?outputs[1]:NULL; |
| 231 | INT16 out = 0; |
| 232 | INT16 out2 = 0; |
| 228 | 233 | |
| 229 | 234 | while (samples > 0) |
| 230 | 235 | { |
| 231 | | // clock chip once |
| 232 | | INT16 out = 0; |
| 233 | | INT16 out2 = 0; |
| 234 | | |
| 235 | | /* decrement Cycles to READY by one */ |
| 236 | | if (R->CyclestoREADY >0) R->CyclestoREADY--; |
| 237 | | |
| 238 | | // handle channels 0,1,2 |
| 239 | | for (i = 0;i < 3;i++) |
| 236 | // clock chip once |
| 237 | if (R->CurrentClock > 0) // not ready for new divided clock |
| 240 | 238 | { |
| 241 | | R->Count[i]--; |
| 242 | | if (R->Count[i] < 0) |
| 243 | | { |
| 244 | | R->Output[i] ^= 1; |
| 245 | | R->Count[i] = R->Period[i]; |
| 246 | | } |
| 239 | R->CurrentClock--; |
| 247 | 240 | } |
| 241 | else // ready for new divided clock, make a new sample |
| 242 | { |
| 243 | R->CurrentClock = R->ClockDivider-1; |
| 244 | /* decrement Cycles to READY by one */ |
| 245 | if (R->CyclestoREADY >0) R->CyclestoREADY--; |
| 248 | 246 | |
| 249 | | // handle channel 3 |
| 250 | | R->Count[3]--; |
| 251 | | if (R->Count[3] <= 0) |
| 252 | | { |
| 253 | | if (NOISEMODE == 1) /* White Noise Mode */ |
| 247 | // handle channels 0,1,2 |
| 248 | for (i = 0;i < 3;i++) |
| 254 | 249 | { |
| 255 | | if ((((R->RNG & R->WhitenoiseTaps) != R->WhitenoiseTaps) && ((R->RNG & R->WhitenoiseTaps) != 0)) ^ R->FeedbackInvert ) /* XOR or XNOR */ |
| 250 | R->Count[i]--; |
| 251 | if (R->Count[i] < 0) |
| 256 | 252 | { |
| 257 | | R->RNG >>= 1; |
| 258 | | R->RNG |= R->FeedbackMask; |
| 253 | R->Output[i] ^= 1; |
| 254 | R->Count[i] = R->Period[i]; |
| 259 | 255 | } |
| 260 | | else |
| 261 | | { |
| 262 | | R->RNG >>= 1; |
| 263 | | } |
| 264 | 256 | } |
| 265 | | else /* Periodic noise mode */ |
| 257 | |
| 258 | // handle channel 3 |
| 259 | R->Count[3]--; |
| 260 | if (R->Count[3] <= 0) |
| 266 | 261 | { |
| 267 | | if (R->RNG & 1) |
| 262 | // if noisemode is 1, both taps are enabled |
| 263 | // if noisemode is 0, the lower tap, whitenoisetap2, is held at 1 or 0 depending on whether FeedbackInvert is set or clear |
| 264 | if (((R->RNG & R->WhitenoiseTap1)?1:0) ^ (((((R->RNG & R->WhitenoiseTap2)?0:1))*(NOISEMODE))^R->FeedbackInvert) ^ R->FeedbackInvert ) |
| 268 | 265 | { |
| 269 | 266 | R->RNG >>= 1; |
| 270 | 267 | R->RNG |= R->FeedbackMask; |
| r7156 | r7157 | |
| 273 | 270 | { |
| 274 | 271 | R->RNG >>= 1; |
| 275 | 272 | } |
| 273 | R->Output[3] = R->RNG & 1; |
| 274 | |
| 275 | R->Count[3] = R->Period[3]; |
| 276 | 276 | } |
| 277 | | R->Output[3] = R->RNG & 1; |
| 278 | | R->Count[3] = R->Period[3]; |
| 279 | 277 | } |
| 280 | | /* //bipolar output, doesn't seem to work right with sonic 2 on gamegear at least |
| 281 | | out = (R->Output[0]?R->Volume[0]:(0-R->Volume[0])) |
| 282 | | +(R->Output[1]?R->Volume[1]:(0-R->Volume[1])) |
| 283 | | +(R->Output[2]?R->Volume[2]:(0-R->Volume[2])) |
| 284 | | +(R->Output[3]?R->Volume[3]:(0-R->Volume[3])); |
| 285 | | */ |
| 278 | |
| 279 | |
| 286 | 280 | if (R->Stereo) |
| 287 | 281 | { |
| 288 | 282 | out = (((R->StereoMask&0x10)&&R->Output[0])?R->Volume[0]:0) |
| r7156 | r7157 | |
| 342 | 336 | |
| 343 | 337 | static int SN76496_init(const device_config *device, sn76496_state *R, int stereo) |
| 344 | 338 | { |
| 345 | | int sample_rate = device->clock/16; |
| 339 | int sample_rate = device->clock/2; |
| 346 | 340 | int i; |
| 347 | 341 | |
| 348 | 342 | R->Channel = stream_create(device,0,(stereo?2:1),sample_rate,R,SN76496Update); |
| r7156 | r7157 | |
| 361 | 355 | R->Output[i] = R->Period[i] = R->Count[i] = 0; |
| 362 | 356 | } |
| 363 | 357 | |
| 364 | | /* Default is SN76489 non-A */ |
| 365 | | R->FeedbackMask = 0x4000; /* mask for feedback */ |
| 366 | | R->WhitenoiseTaps = 0x03; /* mask for white noise taps */ |
| 367 | | R->FeedbackInvert = 0; /* feedback invert flag */ |
| 368 | | R->CyclestoREADY = 1; /* assume ready is not active immediately on init. is this correct?*/ |
| 358 | /* Default is SN76489A */ |
| 359 | R->ClockDivider = 8; |
| 360 | R->FeedbackMask = 0x10000; /* mask for feedback */ |
| 361 | R->WhitenoiseTap1 = 0x04; /* mask for white noise tap 1*/ |
| 362 | R->WhitenoiseTap2 = 0x08; /* mask for white noise tap 2*/ |
| 363 | R->FeedbackInvert = 1; /* feedback invert flag */ |
| 369 | 364 | R->Negate = 0; /* channels are not negated */ |
| 370 | 365 | R->Stereo = stereo; /* depends on init */ |
| 366 | R->CyclestoREADY = 1; /* assume ready is not active immediately on init. is this correct?*/ |
| 371 | 367 | R->StereoMask = 0xFF; /* all channels enabled */ |
| 372 | 368 | |
| 373 | 369 | R->RNG = R->FeedbackMask; |
| r7156 | r7157 | |
| 377 | 373 | } |
| 378 | 374 | |
| 379 | 375 | |
| 380 | | static void generic_start(const device_config *device, int feedbackmask, int noisetaps, int feedbackinvert, int negate, int stereo) |
| 376 | static void generic_start(const device_config *device, int feedbackmask, int noisetap1, int noisetap2, int feedbackinvert, int negate, int stereo, int clockdivider) |
| 381 | 377 | { |
| 382 | 378 | sn76496_state *chip = get_safe_token(device); |
| 383 | 379 | |
| r7156 | r7157 | |
| 386 | 382 | SN76496_set_gain(chip, 0); |
| 387 | 383 | |
| 388 | 384 | chip->FeedbackMask = feedbackmask; |
| 389 | | chip->WhitenoiseTaps = noisetaps; |
| 385 | chip->WhitenoiseTap1 = noisetap1; |
| 386 | chip->WhitenoiseTap2 = noisetap2; |
| 390 | 387 | chip->FeedbackInvert = feedbackinvert; |
| 391 | 388 | chip->Negate = negate; |
| 392 | 389 | chip->Stereo = stereo; |
| 390 | chip->ClockDivider = clockdivider; |
| 391 | chip->CurrentClock = clockdivider-1; |
| 393 | 392 | |
| 394 | 393 | state_save_register_device_item_array(device, 0, chip->VolTable); |
| 395 | 394 | state_save_register_device_item_array(device, 0, chip->Register); |
| 396 | 395 | state_save_register_device_item(device, 0, chip->LastRegister); |
| 397 | 396 | state_save_register_device_item_array(device, 0, chip->Volume); |
| 398 | 397 | state_save_register_device_item(device, 0, chip->RNG); |
| 398 | state_save_register_device_item(device, 0, chip->ClockDivider); |
| 399 | state_save_register_device_item(device, 0, chip->CurrentClock); |
| 399 | 400 | state_save_register_device_item(device, 0, chip->FeedbackMask); |
| 400 | | state_save_register_device_item(device, 0, chip->WhitenoiseTaps); |
| 401 | state_save_register_device_item(device, 0, chip->WhitenoiseTap1); |
| 402 | state_save_register_device_item(device, 0, chip->WhitenoiseTap2); |
| 401 | 403 | state_save_register_device_item(device, 0, chip->FeedbackInvert); |
| 402 | 404 | state_save_register_device_item(device, 0, chip->Negate); |
| 403 | 405 | state_save_register_device_item(device, 0, chip->Stereo); |
| r7156 | r7157 | |
| 408 | 410 | state_save_register_device_item(device, 0, chip->CyclestoREADY); |
| 409 | 411 | } |
| 410 | 412 | |
| 411 | | // function parameters: device, feedback destination tap, feedback source taps, xor(false)/xnor(true), normal(false)/invert(true), mono(false)/stereo(true) |
| 413 | // function parameters: device, feedback destination tap, feedback source taps, xor(false)/xnor(true), normal(false)/invert(true), mono(false)/stereo(true), clock divider factor |
| 412 | 414 | |
| 413 | 415 | static DEVICE_START( sn76489 ) |
| 414 | 416 | { |
| 415 | | generic_start(device, 0x4000, 0x03, FALSE, FALSE, FALSE); // todo: verify; assumed to be the same as sn94624 |
| 417 | generic_start(device, 0x10000, 0x04, 0x08, FALSE, FALSE, FALSE, 8); // SN76489 not verified yet. todo: verify; |
| 416 | 418 | } |
| 417 | 419 | |
| 418 | 420 | static DEVICE_START( sn76489a ) |
| 419 | 421 | { |
| 420 | | generic_start(device, 0x4000, 0x03, TRUE, FALSE, FALSE); // verified by plgdavid |
| 422 | generic_start(device, 0x10000, 0x04, 0x08, TRUE, FALSE, FALSE, 8); // SN76489A: whitenoise verified, phase verified, periodic verified (by plgdavid) |
| 421 | 423 | } |
| 422 | 424 | |
| 423 | 425 | static DEVICE_START( sn76494 ) |
| 424 | 426 | { |
| 425 | | generic_start(device, 0x4000, 0x03, TRUE, FALSE, FALSE); // todo: verify; assumed to be the same as sn76489a |
| 427 | generic_start(device, 0x10000, 0x04, 0x08, TRUE, FALSE, FALSE, 1); // SN76494 not verified, (according to datasheet: same as sn76489a but without the /8 divider) |
| 426 | 428 | } |
| 427 | 429 | |
| 428 | 430 | static DEVICE_START( sn76496 ) |
| 429 | 431 | { |
| 430 | | generic_start(device, 0x4000, 0x03, TRUE, FALSE, FALSE); // todo: verify; assumed to be the same as sn76489a |
| 432 | generic_start(device, 0x10000, 0x04, 0x08, TRUE, FALSE, FALSE, 8); // SN76496 not verified; assumed to be the same as sn76489a |
| 431 | 433 | } |
| 432 | 434 | |
| 433 | 435 | static DEVICE_START( sn94624 ) |
| 434 | 436 | { |
| 435 | | generic_start(device, 0x4000, 0x03, FALSE, FALSE, FALSE); // verified by plgdavid |
| 437 | generic_start(device, 0x4000, 0x01, 0x02, FALSE, TRUE, FALSE, 1); // SN94624 whitenoise verified, phase verified, period verified; verified by PlgDavid |
| 436 | 438 | } |
| 437 | 439 | |
| 438 | 440 | static DEVICE_START( ncr7496 ) |
| 439 | 441 | { |
| 440 | | generic_start(device, 0x8000, 0x22, FALSE, FALSE, FALSE); // todo: verify; from smspower wiki |
| 442 | generic_start(device, 0x8000, 0x02, 0x20, FALSE, FALSE, FALSE, 8); // NCR7496 not verified; info from smspower wiki |
| 441 | 443 | } |
| 442 | 444 | |
| 443 | 445 | static DEVICE_START( gamegear ) |
| 444 | 446 | { |
| 445 | | generic_start(device, 0x8000, 0x09, FALSE, TRUE, TRUE); // Verified by Justin Kerk |
| 447 | generic_start(device, 0x8000, 0x01, 0x08, FALSE, TRUE, TRUE, 8); // Verified by Justin Kerk |
| 446 | 448 | } |
| 447 | 449 | |
| 448 | 450 | static DEVICE_START( smsiii ) |
| 449 | 451 | { |
| 450 | | generic_start(device, 0x8000, 0x09, FALSE, TRUE, FALSE); // todo: verify; from smspower wiki, assumed to have same invert as gamegear |
| 452 | generic_start(device, 0x8000, 0x01, 0x08, FALSE, TRUE, FALSE, 8); // todo: verify; from smspower wiki, assumed to have same invert as gamegear |
| 451 | 453 | } |
| 452 | 454 | |
| 453 | 455 | |