src/emu/sound/sn76496.c
| r6375 | r6376 | |
| 44 | 44 | for the taps), and added R->OldNoise to simulate the extra 0 that is always |
| 45 | 45 | output before the noise LFSR contents are after an LFSR reset. |
| 46 | 46 | This fixes SN76489/A to match chips. Added SN94624. |
| 47 | |
| 48 | 14/11/2009 : Lord Nightmare |
| 49 | Removed STEP mess, vastly simplifying the code. Made output bipolar rather |
| 50 | than always above the 0 line. |
| 47 | 51 | |
| 48 | 52 | TODO: * Implement a function for setting stereo regs for the game gear. |
| 49 | 53 | Requires making the core support both mono and stereo, and have |
| r6375 | r6376 | |
| 65 | 69 | #include "sn76496.h" |
| 66 | 70 | |
| 67 | 71 | |
| 68 | | #define MAX_OUTPUT 0x7fff |
| 69 | | #define STEP 0x10000 |
| 72 | #define MAX_OUTPUT 0x3fff |
| 70 | 73 | #define NOISEMODE (R->Register[6]&4)?1:0 |
| 71 | 74 | |
| 72 | 75 | |
| r6375 | r6376 | |
| 149 | 152 | case 2: /* tone 1 : frequency */ |
| 150 | 153 | case 4: /* tone 2 : frequency */ |
| 151 | 154 | if ((data & 0x80) == 0) R->Register[r] = (R->Register[r] & 0x0f) | ((data & 0x3f) << 4); |
| 152 | | R->Period[c] = STEP * R->Register[r]; |
| 153 | | if (R->Period[c] == 0) R->Period[c] = STEP; |
| 155 | R->Period[c] = R->Register[r]; |
| 154 | 156 | if (r == 4) |
| 155 | 157 | { |
| 156 | 158 | /* update noise shift frequency */ |
| r6375 | r6376 | |
| 170 | 172 | if ((data & 0x80) == 0) R->Register[r] = (R->Register[r] & 0x3f0) | (data & 0x0f); |
| 171 | 173 | n = R->Register[6]; |
| 172 | 174 | /* N/512,N/1024,N/2048,Tone #3 output */ |
| 173 | | R->Period[3] = ((n&3) == 3) ? 2 * R->Period[2] : (STEP << (5+(n&3))); |
| 175 | R->Period[3] = ((n&3) == 3) ? 2 * R->Period[2] : (1 << (5+(n&3))); |
| 174 | 176 | /* Reset noise shifter */ |
| 175 | 177 | R->RNG = R->FeedbackMask; |
| 176 | 178 | R->OldNoise = 0; |
| r6375 | r6376 | |
| 181 | 183 | } |
| 182 | 184 | } |
| 183 | 185 | |
| 184 | | |
| 185 | | |
| 186 | 186 | static STREAM_UPDATE( SN76496Update ) |
| 187 | 187 | { |
| 188 | 188 | int i; |
| r6375 | r6376 | |
| 190 | 190 | stream_sample_t *buffer = outputs[0]; |
| 191 | 191 | |
| 192 | 192 | |
| 193 | | /* If the volume is 0, increase the counter; this is more or less |
| 194 | | a speedup hack for when silence is to be output */ |
| 195 | | for (i = 0;i < 4;i++) |
| 196 | | { |
| 197 | | if (R->Volume[i] == 0) |
| 198 | | { |
| 199 | | /* note that I do count += samples, NOT count = samples + 1. |
| 200 | | You might think it's the same since the volume is 0, but doing |
| 201 | | the latter could cause interferencies when the program is |
| 202 | | rapidly modulating the volume. */ |
| 203 | | if (R->Count[i] <= samples*STEP) R->Count[i] += samples*STEP; |
| 204 | | } |
| 205 | | } |
| 206 | | |
| 207 | 193 | while (samples > 0) |
| 208 | 194 | { |
| 209 | | int vol[4]; |
| 210 | | unsigned int out; |
| 211 | | int left; |
| 195 | // clock chip once |
| 196 | INT16 out; |
| 212 | 197 | |
| 213 | 198 | /* decrement Cycles to READY by one */ |
| 214 | 199 | if (R->CyclestoREADY >0) R->CyclestoREADY--; |
| 215 | 200 | |
| 216 | | /* vol[] keeps track of how long each square wave stays */ |
| 217 | | /* in the 1 position during the sample period. */ |
| 218 | | vol[0] = vol[1] = vol[2] = vol[3] = 0; |
| 219 | | |
| 201 | // handle channels 0,1,2 |
| 220 | 202 | for (i = 0;i < 3;i++) |
| 221 | 203 | { |
| 222 | | if (R->Output[i]) vol[i] += R->Count[i]; |
| 223 | | R->Count[i] -= STEP; |
| 224 | | /* Period[i] is the half period of the square wave. Here, in each |
| 225 | | loop I add Period[i] twice, so that at the end of the loop the |
| 226 | | square wave is in the same status (0 or 1) it was at the start. |
| 227 | | vol[i] is also incremented by Period[i], since the wave has been 1 |
| 228 | | exactly half of the time, regardless of the initial position. |
| 229 | | If we exit the loop in the middle, Output[i] has to be inverted |
| 230 | | and vol[i] incremented only if the exit status of the square |
| 231 | | wave is 1. */ |
| 232 | | while (R->Count[i] <= 0) |
| 204 | R->Count[i]--; |
| 205 | if (R->Count[i] < 0) |
| 233 | 206 | { |
| 234 | | R->Count[i] += R->Period[i]; |
| 235 | | if (R->Count[i] > 0) |
| 236 | | { |
| 237 | | R->Output[i] ^= 1; |
| 238 | | if (R->Output[i]) vol[i] += R->Period[i]; |
| 239 | | break; |
| 240 | | } |
| 241 | | R->Count[i] += R->Period[i]; |
| 242 | | vol[i] += R->Period[i]; |
| 207 | R->Output[i] ^= 1; |
| 208 | R->Count[i] = R->Period[i]; |
| 243 | 209 | } |
| 244 | | if (R->Output[i]) vol[i] -= R->Count[i]; |
| 245 | 210 | } |
| 246 | 211 | |
| 247 | | left = STEP; |
| 248 | | do |
| 212 | // handle channel 3 |
| 213 | R->Count[3]--; |
| 214 | if (R->Count[3] <= 0) |
| 249 | 215 | { |
| 250 | | int nextevent; |
| 251 | | |
| 252 | | |
| 253 | | if (R->Count[3] < left) nextevent = R->Count[3]; |
| 254 | | else nextevent = left; |
| 255 | | |
| 256 | | if (R->Output[3]) vol[3] += R->Count[3]; |
| 257 | | R->Count[3] -= nextevent; |
| 258 | | if (R->Count[3] <= 0) |
| 216 | if (NOISEMODE == 1) /* White Noise Mode */ |
| 259 | 217 | { |
| 260 | | if (NOISEMODE == 1) /* White Noise Mode */ |
| 261 | | { |
| 262 | | if ((((R->RNG & R->WhitenoiseTaps) != R->WhitenoiseTaps) && ((R->RNG & R->WhitenoiseTaps) != 0)) ^ R->FeedbackInvert ) /* XOR or XNOR */ |
| 263 | | { |
| 264 | | R->RNG >>= 1; |
| 265 | | R->RNG |= R->FeedbackMask; |
| 266 | | } |
| 267 | | else |
| 268 | | { |
| 269 | | R->RNG >>= 1; |
| 270 | | } |
| 218 | if ((((R->RNG & R->WhitenoiseTaps) != R->WhitenoiseTaps) && ((R->RNG & R->WhitenoiseTaps) != 0)) ^ R->FeedbackInvert ) /* XOR or XNOR */ |
| 219 | { |
| 220 | R->RNG >>= 1; |
| 221 | R->RNG |= R->FeedbackMask; |
| 271 | 222 | } |
| 272 | | else /* Periodic noise mode */ |
| 223 | else |
| 273 | 224 | { |
| 274 | | if (R->RNG & 1) |
| 275 | | { |
| 276 | | R->RNG >>= 1; |
| 277 | | R->RNG |= R->FeedbackMask; |
| 278 | | } |
| 279 | | else |
| 280 | | { |
| 281 | | R->RNG >>= 1; |
| 282 | | } |
| 225 | R->RNG >>= 1; |
| 283 | 226 | } |
| 284 | | R->Output[3] = R->OldNoise; |
| 285 | | R->OldNoise = R->RNG & 1; |
| 286 | | R->Count[3] += R->Period[3]; |
| 287 | | if (R->Output[3]) vol[3] += R->Period[3]; |
| 288 | 227 | } |
| 289 | | if (R->Output[3]) vol[3] -= R->Count[3]; |
| 228 | else /* Periodic noise mode */ |
| 229 | { |
| 230 | if (R->RNG & 1) |
| 231 | { |
| 232 | R->RNG >>= 1; |
| 233 | R->RNG |= R->FeedbackMask; |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | R->RNG >>= 1; |
| 238 | } |
| 239 | } |
| 240 | R->Output[3] = R->OldNoise; |
| 241 | R->OldNoise = R->RNG & 1; |
| 242 | R->Count[3] = R->Period[3]; |
| 243 | } |
| 290 | 244 | |
| 291 | | left -= nextevent; |
| 292 | | } while (left > 0); |
| 245 | out = (R->Output[0]?R->Volume[0]:(0-R->Volume[0])) |
| 246 | +(R->Output[1]?R->Volume[1]:(0-R->Volume[1])) |
| 247 | +(R->Output[2]?R->Volume[2]:(0-R->Volume[2])) |
| 248 | +(R->Output[3]?R->Volume[3]:(0-R->Volume[3])); |
| 293 | 249 | |
| 294 | | out = vol[0] * R->Volume[0] + vol[1] * R->Volume[1] + |
| 295 | | vol[2] * R->Volume[2] + vol[3] * R->Volume[3]; |
| 250 | *(buffer++) = out; |
| 296 | 251 | |
| 297 | | if (out > MAX_OUTPUT * STEP) out = MAX_OUTPUT * STEP; |
| 298 | | |
| 299 | | *(buffer++) = out / STEP; |
| 300 | | |
| 301 | 252 | samples--; |
| 302 | 253 | } |
| 303 | 254 | } |
| r6375 | r6376 | |
| 349 | 300 | |
| 350 | 301 | for (i = 0;i < 4;i++) |
| 351 | 302 | { |
| 352 | | R->Output[i] = 0; |
| 353 | | R->Period[i] = R->Count[i] = STEP; |
| 303 | R->Output[i] = R->Period[i] = R->Count[i] = 0; |
| 354 | 304 | } |
| 355 | 305 | |
| 356 | 306 | /* Default is SN76489 non-A */ |