src/emu/sound/t6w28.c
| r0 | r4532 | |
| 1 | /*************************************************************************** |
| 2 | |
| 3 | t6w28.c (based on sn74696.c) |
| 4 | |
| 5 | The t6w28 sound core is used in the SNK NeoGeo Pocket. It is a stereo |
| 6 | sound chip based on 2 partial sn76498a cores. |
| 7 | |
| 8 | The block diagram for this chip is as follows: |
| 9 | |
| 10 | Offset 0: |
| 11 | Tone 0 /----------> Att0 ---\ |
| 12 | | | |
| 13 | Tone 1 | /-------> Att1 ---+ |
| 14 | | | | Right |
| 15 | Tone 2 | | /----> Att2 ---+--------> |
| 16 | | | | | | |
| 17 | Noise -----+-------------> Att3 ---/ |
| 18 | | | | | |
| 19 | | | | | |
| 20 | Offset 1: | | | | |
| 21 | Tone 0 --------+----------> Att0 ---\ |
| 22 | | | | | |
| 23 | Tone 1 -----------+-------> Att1 ---+ |
| 24 | | | | Left |
| 25 | Tone 2 --------------+----> Att2 ---+--------> |
| 26 | | | |
| 27 | Noise \-------------> Att3 ---/ |
| 28 | |
| 29 | |
| 30 | ***************************************************************************/ |
| 31 | |
| 32 | #include "sndintrf.h" |
| 33 | #include "streams.h" |
| 34 | #include "t6w28.h" |
| 35 | |
| 36 | |
| 37 | #define MAX_OUTPUT 0x7fff |
| 38 | |
| 39 | #define STEP 0x10000 |
| 40 | |
| 41 | typedef struct _t6w28_state t6w28_state; |
| 42 | struct _t6w28_state |
| 43 | { |
| 44 | sound_stream * Channel; |
| 45 | int SampleRate; |
| 46 | int VolTable[16]; /* volume table */ |
| 47 | INT32 Register[16]; /* registers */ |
| 48 | INT32 LastRegister[2]; /* last register written */ |
| 49 | INT32 Volume[8]; /* volume of voice 0-2 and noise */ |
| 50 | UINT32 RNG[2]; /* noise generator */ |
| 51 | INT32 NoiseMode[2]; /* active noise mode */ |
| 52 | INT32 FeedbackMask; /* mask for feedback */ |
| 53 | INT32 WhitenoiseTaps; /* mask for white noise taps */ |
| 54 | INT32 WhitenoiseInvert; /* white noise invert flag */ |
| 55 | INT32 Period[8]; |
| 56 | INT32 Count[8]; |
| 57 | INT32 Output[8]; |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | INLINE t6w28_state *get_safe_token(const device_config *device) |
| 62 | { |
| 63 | assert(device != NULL); |
| 64 | assert(device->token != NULL); |
| 65 | assert(device->type == SOUND); |
| 66 | assert(sound_get_type(device) == SOUND_T6W28); |
| 67 | return (t6w28_state *)device->token; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | WRITE8_DEVICE_HANDLER( t6w28_w ) |
| 72 | { |
| 73 | t6w28_state *R = get_safe_token(device); |
| 74 | int n, r, c; |
| 75 | |
| 76 | |
| 77 | /* update the output buffer before changing the registers */ |
| 78 | stream_update(R->Channel); |
| 79 | |
| 80 | offset &= 1; |
| 81 | |
| 82 | if (data & 0x80) |
| 83 | { |
| 84 | r = (data & 0x70) >> 4; |
| 85 | R->LastRegister[offset] = r; |
| 86 | R->Register[offset * 8 + r] = (R->Register[offset * 8 + r] & 0x3f0) | (data & 0x0f); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | r = R->LastRegister[offset]; |
| 91 | } |
| 92 | c = r/2; |
| 93 | switch (r) |
| 94 | { |
| 95 | case 0: /* tone 0 : frequency */ |
| 96 | case 2: /* tone 1 : frequency */ |
| 97 | case 4: /* tone 2 : frequency */ |
| 98 | if ((data & 0x80) == 0) R->Register[offset * 8 + r] = (R->Register[offset * 8 + r] & 0x0f) | ((data & 0x3f) << 4); |
| 99 | R->Period[offset * 4 + c] = STEP * R->Register[offset * 8 + r]; |
| 100 | if (R->Period[offset * 4 + c] == 0) R->Period[offset * 4 + c] = STEP; |
| 101 | if (r == 4) |
| 102 | { |
| 103 | /* update noise shift frequency */ |
| 104 | if ((R->Register[offset * 8 + 6] & 0x03) == 0x03) |
| 105 | R->Period[offset * 4 + 3] = 2 * R->Period[offset * 4 + 2]; |
| 106 | } |
| 107 | break; |
| 108 | case 1: /* tone 0 : volume */ |
| 109 | case 3: /* tone 1 : volume */ |
| 110 | case 5: /* tone 2 : volume */ |
| 111 | case 7: /* noise : volume */ |
| 112 | R->Volume[offset * 4 + c] = R->VolTable[data & 0x0f]; |
| 113 | if ((data & 0x80) == 0) R->Register[offset * 8 + r] = (R->Register[offset * 8 + r] & 0x3f0) | (data & 0x0f); |
| 114 | break; |
| 115 | case 6: /* noise : frequency, mode */ |
| 116 | { |
| 117 | if ((data & 0x80) == 0) R->Register[offset * 8 + r] = (R->Register[offset * 8 + r] & 0x3f0) | (data & 0x0f); |
| 118 | n = R->Register[offset * 8 + 6]; |
| 119 | R->NoiseMode[offset] = (n & 4) ? 1 : 0; |
| 120 | /* N/512,N/1024,N/2048,Tone #3 output */ |
| 121 | R->Period[offset * 4 + 3] = ((n&3) == 3) ? 2 * R->Period[offset * 4 + 2] : (STEP << (5+(n&3))); |
| 122 | /* Reset noise shifter */ |
| 123 | R->RNG[offset] = R->FeedbackMask; /* this is correct according to the smspower document */ |
| 124 | //R->RNG = 0xF35; /* this is not, but sounds better in do run run */ |
| 125 | R->Output[offset * 4 + 3] = R->RNG[offset] & 1; |
| 126 | } |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | |
| 132 | |
| 133 | static STREAM_UPDATE( t6w28_update ) |
| 134 | { |
| 135 | int i; |
| 136 | t6w28_state *R = (t6w28_state *)param; |
| 137 | stream_sample_t *buffer0 = outputs[0]; |
| 138 | stream_sample_t *buffer1 = outputs[1]; |
| 139 | |
| 140 | |
| 141 | /* If the volume is 0, increase the counter */ |
| 142 | for (i = 0;i < 8;i++) |
| 143 | { |
| 144 | if (R->Volume[i] == 0) |
| 145 | { |
| 146 | /* note that I do count += samples, NOT count = samples + 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] <= samples*STEP) R->Count[i] += samples*STEP; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | while (samples > 0) |
| 154 | { |
| 155 | int vol[8]; |
| 156 | unsigned int out0, out1; |
| 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] = vol[4] = vol[5] = vol[6] = vol[7] = 0; |
| 163 | |
| 164 | for (i = 2;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 | for (i = 4;i < 7;i++) |
| 192 | { |
| 193 | if (R->Output[i]) vol[i] += R->Count[i]; |
| 194 | R->Count[i] -= STEP; |
| 195 | /* Period[i] is the half period of the square wave. Here, in each */ |
| 196 | /* loop I add Period[i] twice, so that at the end of the loop the */ |
| 197 | /* square wave is in the same status (0 or 1) it was at the start. */ |
| 198 | /* vol[i] is also incremented by Period[i], since the wave has been 1 */ |
| 199 | /* exactly half of the time, regardless of the initial position. */ |
| 200 | /* If we exit the loop in the middle, Output[i] has to be inverted */ |
| 201 | /* and vol[i] incremented only if the exit status of the square */ |
| 202 | /* wave is 1. */ |
| 203 | while (R->Count[i] <= 0) |
| 204 | { |
| 205 | R->Count[i] += R->Period[i]; |
| 206 | if (R->Count[i] > 0) |
| 207 | { |
| 208 | R->Output[i] ^= 1; |
| 209 | if (R->Output[i]) vol[i] += R->Period[i]; |
| 210 | break; |
| 211 | } |
| 212 | R->Count[i] += R->Period[i]; |
| 213 | vol[i] += R->Period[i]; |
| 214 | } |
| 215 | if (R->Output[i]) vol[i] -= R->Count[i]; |
| 216 | } |
| 217 | |
| 218 | left = STEP; |
| 219 | do |
| 220 | { |
| 221 | int nextevent; |
| 222 | |
| 223 | |
| 224 | if (R->Count[3] < left) nextevent = R->Count[3]; |
| 225 | else nextevent = left; |
| 226 | |
| 227 | if (R->Output[3]) vol[3] += R->Count[3]; |
| 228 | R->Count[3] -= nextevent; |
| 229 | if (R->Count[3] <= 0) |
| 230 | { |
| 231 | if (R->NoiseMode[0] == 1) /* White Noise Mode */ |
| 232 | { |
| 233 | if (((R->RNG[0] & R->WhitenoiseTaps) != R->WhitenoiseTaps) && ((R->RNG[0] & R->WhitenoiseTaps) != 0)) /* crappy xor! */ |
| 234 | { |
| 235 | R->RNG[0] >>= 1; |
| 236 | R->RNG[0] |= R->FeedbackMask; |
| 237 | } |
| 238 | else |
| 239 | { |
| 240 | R->RNG[0] >>= 1; |
| 241 | } |
| 242 | R->Output[3] = R->WhitenoiseInvert ? !(R->RNG[0] & 1) : R->RNG[0] & 1; |
| 243 | } |
| 244 | else /* Periodic noise mode */ |
| 245 | { |
| 246 | if (R->RNG[0] & 1) |
| 247 | { |
| 248 | R->RNG[0] >>= 1; |
| 249 | R->RNG[0] |= R->FeedbackMask; |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | R->RNG[0] >>= 1; |
| 254 | } |
| 255 | R->Output[3] = R->RNG[0] & 1; |
| 256 | } |
| 257 | R->Count[3] += R->Period[3]; |
| 258 | if (R->Output[3]) vol[3] += R->Period[3]; |
| 259 | } |
| 260 | if (R->Output[3]) vol[3] -= R->Count[3]; |
| 261 | |
| 262 | left -= nextevent; |
| 263 | } while (left > 0); |
| 264 | |
| 265 | out0 = vol[4] * R->Volume[4] + vol[5] * R->Volume[5] + |
| 266 | vol[6] * R->Volume[6] + vol[3] * R->Volume[7]; |
| 267 | |
| 268 | out1 = vol[4] * R->Volume[0] + vol[5] * R->Volume[1] + |
| 269 | vol[6] * R->Volume[2] + vol[3] * R->Volume[3]; |
| 270 | |
| 271 | if (out0 > MAX_OUTPUT * STEP) out0 = MAX_OUTPUT * STEP; |
| 272 | if (out1 > MAX_OUTPUT * STEP) out1 = MAX_OUTPUT * STEP; |
| 273 | |
| 274 | *(buffer0++) = out0 / STEP; |
| 275 | *(buffer1++) = out1 / STEP; |
| 276 | |
| 277 | samples--; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | |
| 282 | |
| 283 | static void t6w28_set_gain(t6w28_state *R,int gain) |
| 284 | { |
| 285 | int i; |
| 286 | double out; |
| 287 | |
| 288 | |
| 289 | gain &= 0xff; |
| 290 | |
| 291 | /* increase max output basing on gain (0.2 dB per step) */ |
| 292 | out = MAX_OUTPUT / 3; |
| 293 | while (gain-- > 0) |
| 294 | out *= 1.023292992; /* = (10 ^ (0.2/20)) */ |
| 295 | |
| 296 | /* build volume table (2dB per step) */ |
| 297 | for (i = 0;i < 15;i++) |
| 298 | { |
| 299 | /* limit volume to avoid clipping */ |
| 300 | if (out > MAX_OUTPUT / 3) R->VolTable[i] = MAX_OUTPUT / 3; |
| 301 | else R->VolTable[i] = out; |
| 302 | |
| 303 | out /= 1.258925412; /* = 10 ^ (2/20) = 2dB */ |
| 304 | } |
| 305 | R->VolTable[15] = 0; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | |
| 310 | static int t6w28_init(const device_config *device, t6w28_state *R) |
| 311 | { |
| 312 | int sample_rate = device->clock/16; |
| 313 | int i; |
| 314 | |
| 315 | R->Channel = stream_create(device,0,2,sample_rate,R,t6w28_update); |
| 316 | |
| 317 | R->SampleRate = sample_rate; |
| 318 | |
| 319 | for (i = 0;i < 8;i++) R->Volume[i] = 0; |
| 320 | |
| 321 | R->LastRegister[0] = 0; |
| 322 | R->LastRegister[1] = 0; |
| 323 | for (i = 0;i < 8;i+=2) |
| 324 | { |
| 325 | R->Register[i] = 0; |
| 326 | R->Register[i + 1] = 0x0f; /* volume = 0 */ |
| 327 | } |
| 328 | |
| 329 | for (i = 0;i < 8;i++) |
| 330 | { |
| 331 | R->Output[i] = 0; |
| 332 | R->Period[i] = R->Count[i] = STEP; |
| 333 | } |
| 334 | |
| 335 | /* Default is SN76489 non-A */ |
| 336 | R->FeedbackMask = 0x4000; /* mask for feedback */ |
| 337 | R->WhitenoiseTaps = 0x03; /* mask for white noise taps */ |
| 338 | R->WhitenoiseInvert = 1; /* white noise invert flag */ |
| 339 | |
| 340 | R->RNG[0] = R->FeedbackMask; |
| 341 | R->RNG[1] = R->FeedbackMask; |
| 342 | R->Output[3] = R->RNG[0] & 1; |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | |
| 348 | static DEVICE_START( t6w28 ) |
| 349 | { |
| 350 | t6w28_state *chip = get_safe_token(device); |
| 351 | |
| 352 | if (t6w28_init(device,chip) != 0) |
| 353 | fatalerror("Error creating t6w28 chip"); |
| 354 | t6w28_set_gain(chip, 0); |
| 355 | |
| 356 | /* values from sn76489a */ |
| 357 | chip->FeedbackMask = 0x8000; |
| 358 | chip->WhitenoiseTaps = 0x06; |
| 359 | chip->WhitenoiseInvert = FALSE; |
| 360 | |
| 361 | state_save_register_device_item_array(device, 0, chip->Register); |
| 362 | state_save_register_device_item_array(device, 0, chip->LastRegister); |
| 363 | state_save_register_device_item_array(device, 0, chip->Volume); |
| 364 | state_save_register_device_item_array(device, 0, chip->RNG); |
| 365 | state_save_register_device_item_array(device, 0, chip->NoiseMode); |
| 366 | state_save_register_device_item_array(device, 0, chip->Period); |
| 367 | state_save_register_device_item_array(device, 0, chip->Count); |
| 368 | state_save_register_device_item_array(device, 0, chip->Output); |
| 369 | } |
| 370 | |
| 371 | |
| 372 | /************************************************************************** |
| 373 | * Generic get_info |
| 374 | **************************************************************************/ |
| 375 | |
| 376 | DEVICE_GET_INFO( t6w28 ) |
| 377 | { |
| 378 | switch (state) |
| 379 | { |
| 380 | /* --- the following bits of info are returned as 64-bit signed integers --- */ |
| 381 | case DEVINFO_INT_TOKEN_BYTES: info->i = sizeof(t6w28_state); break; |
| 382 | |
| 383 | /* --- the following bits of info are returned as pointers to data or functions --- */ |
| 384 | case DEVINFO_FCT_START: info->start = DEVICE_START_NAME( t6w28 ); break; |
| 385 | case DEVINFO_FCT_STOP: /* Nothing */ break; |
| 386 | case DEVINFO_FCT_RESET: /* Nothing */ break; |
| 387 | |
| 388 | /* --- the following bits of info are returned as NULL-terminated strings --- */ |
| 389 | case DEVINFO_STR_NAME: strcpy(info->s, "T6W28"); break; |
| 390 | case DEVINFO_STR_FAMILY: strcpy(info->s, "PSG"); break; |
| 391 | case DEVINFO_STR_VERSION: strcpy(info->s, "1.0"); break; |
| 392 | case DEVINFO_STR_SOURCE_FILE: strcpy(info->s, __FILE__); break; |
| 393 | case DEVINFO_STR_CREDITS: strcpy(info->s, "Copyright Nicola Salmoria and the MAME Team"); break; |
| 394 | } |
| 395 | } |
| 396 | |
src/mess/drivers/ngp.c
| r4531 | r4532 | |
| 98 | 98 | #include "cpu/tlcs900/tlcs900.h" |
| 99 | 99 | #include "cpu/z80/z80.h" |
| 100 | 100 | #include "devices/cartslot.h" |
| 101 | | #include "sound/sn76496.h" |
| 101 | #include "sound/t6w28.h" |
| 102 | 102 | #include "sound/dac.h" |
| 103 | 103 | #include "video/k1ge.h" |
| 104 | 104 | |
| r4531 | r4532 | |
| 187 | 187 | { |
| 188 | 188 | switch( offset ) |
| 189 | 189 | { |
| 190 | | case 0x20: /* sn76489 right */ |
| 191 | | case 0x21: /* sn76489 left */ |
| 192 | | sn76496_w( devtag_get_device( space->machine, "sn76489a" ), 0, data ); |
| 190 | case 0x20: /* t6w28 "right" */ |
| 191 | case 0x21: /* t6w28 "left" */ |
| 192 | if ( io_reg[0x38] == 0x55 && io_reg[0x39] == 0xAA ) |
| 193 | { |
| 194 | t6w28_w( devtag_get_device( space->machine, "t6w28" ), 0, data ); |
| 195 | } |
| 193 | 196 | break; |
| 194 | 197 | |
| 195 | 198 | case 0x22: /* DAC right */ |
| r4531 | r4532 | |
| 490 | 493 | |
| 491 | 494 | static ADDRESS_MAP_START( z80_mem, ADDRESS_SPACE_PROGRAM, 8 ) |
| 492 | 495 | AM_RANGE( 0x0000, 0x0FFF ) AM_RAM AM_SHARE(1) /* shared with tlcs900 */ |
| 493 | | AM_RANGE( 0x4000, 0x4000 ) AM_DEVWRITE( "sn76489a", sn76496_w ) /* sound chip (right) */ |
| 494 | | AM_RANGE( 0x4001, 0x4001 ) AM_DEVWRITE( "sn76489a", sn76496_w ) /* sound chip (left) */ |
| 496 | AM_RANGE( 0x4000, 0x4001 ) AM_DEVWRITE( "t6w28", t6w28_w ) /* sound chip (right, left) */ |
| 495 | 497 | AM_RANGE( 0x8000, 0x8000 ) AM_READWRITE( ngp_z80_comm_r, ngp_z80_comm_w ) /* main-sound communication */ |
| 496 | 498 | AM_RANGE( 0xc000, 0xc000 ) AM_WRITE( ngp_z80_signal_main_w ) /* signal irq to main cpu */ |
| 497 | 499 | ADDRESS_MAP_END |
| r4531 | r4532 | |
| 720 | 722 | /* sound hardware */ |
| 721 | 723 | MDRV_SPEAKER_STANDARD_STEREO( "lspeaker","rspeaker" ) |
| 722 | 724 | |
| 723 | | MDRV_SOUND_ADD( "sn76489a", SN76489A, XTAL_6_144MHz/2 ) |
| 724 | | MDRV_SOUND_ROUTE( ALL_OUTPUTS, "lspeaker", 0.50 ) |
| 725 | | MDRV_SOUND_ROUTE( ALL_OUTPUTS, "rspeaker", 0.50 ) |
| 725 | MDRV_SOUND_ADD( "t6w28", T6W28, XTAL_6_144MHz/2 ) |
| 726 | MDRV_SOUND_ROUTE( 0, "lspeaker", 0.50 ) |
| 727 | MDRV_SOUND_ROUTE( 1, "rspeaker", 0.50 ) |
| 726 | 728 | |
| 727 | 729 | MDRV_SOUND_ADD( "dac_l", DAC, 0 ) |
| 728 | 730 | MDRV_SOUND_ROUTE( ALL_OUTPUTS, "lspeaker", 0.50 ) |
| r4531 | r4532 | |
| 778 | 780 | ROM_END |
| 779 | 781 | |
| 780 | 782 | |
| 781 | | CONS( 1998, ngp, 0, 0, ngp, ngp, 0, 0, "SNK", "NeoGeo Pocket", GAME_IMPERFECT_SOUND ) |
| 782 | | CONS( 1999, ngpc, ngp, 0, ngpc, ngp, 0, 0, "SNK", "NeoGeo Pocket Color", GAME_IMPERFECT_SOUND ) |
| 783 | CONS( 1998, ngp, 0, 0, ngp, ngp, 0, 0, "SNK", "NeoGeo Pocket", 0 ) |
| 784 | CONS( 1999, ngpc, ngp, 0, ngpc, ngp, 0, 0, "SNK", "NeoGeo Pocket Color", 0 ) |
| 783 | 785 | |