| 1 | /* Mednafen - Multi-system Emulator |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | */ |
| 17 | |
| 18 | #include <string.h> |
| 19 | #include <stdlib.h> |
| 20 | |
| 21 | #include "wswan.h" |
| 22 | #include "sound.h" |
| 23 | #include "v30mz.h" |
| 24 | #include "wswan-memory.h" |
| 25 | #include "../state_inline.h" |
| 26 | |
| 27 | #include "../include/blip/Blip_Buffer.h" |
| 28 | |
| 29 | static Blip_Synth WaveSynth; |
| 30 | |
| 31 | static Blip_Buffer sbuf[2]; |
| 32 | |
| 33 | static uint16 period[4]; |
| 34 | static uint8 volume[4]; /* left volume in upper 4 bits, right in lower 4 bits */ |
| 35 | static uint8 voice_volume; |
| 36 | |
| 37 | static uint8 sweep_step, sweep_value; |
| 38 | static uint8 noise_control; |
| 39 | static uint8 control; |
| 40 | static uint8 output_control; |
| 41 | |
| 42 | static int32 sweep_8192_divider; |
| 43 | static uint8 sweep_counter; |
| 44 | static uint8 SampleRAMPos; |
| 45 | |
| 46 | static int32 sample_cache[4][2]; |
| 47 | |
| 48 | static int32 last_v_val; |
| 49 | |
| 50 | static uint8 HyperVoice; |
| 51 | static int32 last_hv_val[2]; |
| 52 | static uint8 HVoiceCtrl, HVoiceChanCtrl; |
| 53 | |
| 54 | static int32 period_counter[4]; |
| 55 | static int32 last_val[4][2]; /* Last outputted value, l&r */ |
| 56 | static uint8 sample_pos[4]; |
| 57 | static uint16 nreg; |
| 58 | static uint32 last_ts; |
| 59 | |
| 60 | |
| 61 | #define MK_SAMPLE_CACHE \ |
| 62 | { \ |
| 63 | int sample = (((wsRAM[((SampleRAMPos << 6) + (sample_pos[ch] >> 1) + (ch << 4)) ] >> ((sample_pos[ch] & 1) ? 4 : 0)) & 0x0F)); \ |
| 64 | sample_cache[ch][0] = sample * ((volume[ch] >> 4) & 0x0F); \ |
| 65 | sample_cache[ch][1] = sample * ((volume[ch] >> 0) & 0x0F); \ |
| 66 | } |
| 67 | |
| 68 | #define MK_SAMPLE_CACHE_NOISE \ |
| 69 | { \ |
| 70 | int sample = ((nreg & 1) ? 0xF : 0x0); \ |
| 71 | sample_cache[ch][0] = sample * ((volume[ch] >> 4) & 0x0F); \ |
| 72 | sample_cache[ch][1] = sample * ((volume[ch] >> 0) & 0x0F); \ |
| 73 | } |
| 74 | |
| 75 | #define MK_SAMPLE_CACHE_VOICE \ |
| 76 | { \ |
| 77 | int sample = volume[ch]; \ |
| 78 | int half = sample >> 1; \ |
| 79 | sample_cache[ch][0] = (voice_volume & 4) ? sample : (voice_volume & 8) ? half : 0; \ |
| 80 | sample_cache[ch][1] = (voice_volume & 1) ? sample : (voice_volume & 2) ? half : 0; \ |
| 81 | } |
| 82 | |
| 83 | #define SYNCSAMPLE(wt) \ |
| 84 | { \ |
| 85 | int32 left = sample_cache[ch][0], right = sample_cache[ch][1]; \ |
| 86 | Blip_Synth_offset(&WaveSynth, wt, left - last_val[ch][0], &sbuf[0]); \ |
| 87 | Blip_Synth_offset(&WaveSynth, wt, right - last_val[ch][1], &sbuf[1]); \ |
| 88 | last_val[ch][0] = left; \ |
| 89 | last_val[ch][1] = right; \ |
| 90 | } |
| 91 | |
| 92 | #define SYNCSAMPLE_NOISE(wt) SYNCSAMPLE(wt) |
| 93 | |
| 94 | void WSwan_SoundUpdate(void) |
| 95 | { |
| 96 | unsigned int ch; |
| 97 | int32 run_time = v30mz_timestamp - last_ts; |
| 98 | |
| 99 | for(ch = 0; ch < 4; ch++) |
| 100 | { |
| 101 | /* Channel is disabled? */ |
| 102 | if(!(control & (1 << ch))) |
| 103 | continue; |
| 104 | |
| 105 | if(ch == 1 && (control & 0x20)) /* Direct D/A mode? */ |
| 106 | { |
| 107 | MK_SAMPLE_CACHE_VOICE; |
| 108 | SYNCSAMPLE(v30mz_timestamp); |
| 109 | } |
| 110 | else if(ch == 2 && (control & 0x40) && sweep_value) /* Sweep */ |
| 111 | { |
| 112 | uint32 tmp_pt = 2048 - period[ch]; |
| 113 | uint32 meow_timestamp = v30mz_timestamp - run_time; |
| 114 | uint32 tmp_run_time = run_time; |
| 115 | |
| 116 | while(tmp_run_time) |
| 117 | { |
| 118 | int32 sub_run_time = tmp_run_time; |
| 119 | |
| 120 | if(sub_run_time > sweep_8192_divider) |
| 121 | sub_run_time = sweep_8192_divider; |
| 122 | |
| 123 | sweep_8192_divider -= sub_run_time; |
| 124 | if(sweep_8192_divider <= 0) |
| 125 | { |
| 126 | sweep_8192_divider += 8192; |
| 127 | sweep_counter--; |
| 128 | if(sweep_counter <= 0) |
| 129 | { |
| 130 | sweep_counter = sweep_step + 1; |
| 131 | period[ch] = (period[ch] + (int8)sweep_value) & 0x7FF; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | meow_timestamp += sub_run_time; |
| 136 | if(tmp_pt > 4) |
| 137 | { |
| 138 | period_counter[ch] -= sub_run_time; |
| 139 | while(period_counter[ch] <= 0) |
| 140 | { |
| 141 | sample_pos[ch] = (sample_pos[ch] + 1) & 0x1F; |
| 142 | |
| 143 | MK_SAMPLE_CACHE; |
| 144 | SYNCSAMPLE(meow_timestamp + period_counter[ch]); |
| 145 | period_counter[ch] += tmp_pt; |
| 146 | } |
| 147 | } |
| 148 | tmp_run_time -= sub_run_time; |
| 149 | } |
| 150 | } |
| 151 | else if(ch == 3 && (control & 0x80) && (noise_control & 0x10)) /* Noise */ |
| 152 | { |
| 153 | uint32 tmp_pt = 2048 - period[ch]; |
| 154 | |
| 155 | period_counter[ch] -= run_time; |
| 156 | while(period_counter[ch] <= 0) |
| 157 | { |
| 158 | static const uint8 stab[8] = { 14, 10, 13, 4, 8, 6, 9, 11 }; |
| 159 | |
| 160 | nreg = ((nreg << 1) | ((1 ^ (nreg >> 7) ^ (nreg >> stab[noise_control & 0x7])) & 1)) & 0x7FFF; |
| 161 | |
| 162 | if(control & 0x80) |
| 163 | { |
| 164 | MK_SAMPLE_CACHE_NOISE; |
| 165 | SYNCSAMPLE_NOISE(v30mz_timestamp + period_counter[ch]); |
| 166 | } |
| 167 | else if(tmp_pt > 4) |
| 168 | { |
| 169 | sample_pos[ch] = (sample_pos[ch] + 1) & 0x1F; |
| 170 | MK_SAMPLE_CACHE; |
| 171 | SYNCSAMPLE(v30mz_timestamp + period_counter[ch]); |
| 172 | } |
| 173 | period_counter[ch] += tmp_pt; |
| 174 | } |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | uint32 tmp_pt = 2048 - period[ch]; |
| 179 | |
| 180 | if(tmp_pt > 4) |
| 181 | { |
| 182 | period_counter[ch] -= run_time; |
| 183 | while(period_counter[ch] <= 0) |
| 184 | { |
| 185 | sample_pos[ch] = (sample_pos[ch] + 1) & 0x1F; |
| 186 | |
| 187 | MK_SAMPLE_CACHE; |
| 188 | SYNCSAMPLE(v30mz_timestamp + period_counter[ch]); |
| 189 | period_counter[ch] += tmp_pt; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if(HVoiceCtrl & 0x80) |
| 196 | { |
| 197 | int32 left, right; |
| 198 | int16 sample = (uint8)HyperVoice; |
| 199 | |
| 200 | switch(HVoiceCtrl & 0xC) |
| 201 | { |
| 202 | case 0x0: sample = (uint16)sample << (8 - (HVoiceCtrl & 3)); break; |
| 203 | case 0x4: sample = (uint16)(sample | -0x100) << (8 - (HVoiceCtrl & 3)); break; |
| 204 | case 0x8: sample = (uint16)((int8)sample) << (8 - (HVoiceCtrl & 3)); break; |
| 205 | case 0xC: sample = (uint16)sample << 8; break; |
| 206 | } |
| 207 | /* bring back to 11bit, keeping signedness */ |
| 208 | sample >>= 5; |
| 209 | |
| 210 | left = (HVoiceChanCtrl & 0x40) ? sample : 0; |
| 211 | right = (HVoiceChanCtrl & 0x20) ? sample : 0; |
| 212 | |
| 213 | Blip_Synth_offset(&WaveSynth, v30mz_timestamp, left - last_hv_val[0], &sbuf[0]); |
| 214 | Blip_Synth_offset(&WaveSynth, v30mz_timestamp, right - last_hv_val[0], &sbuf[1]); |
| 215 | last_hv_val[0] = left; |
| 216 | last_hv_val[1] = right; |
| 217 | } |
| 218 | last_ts = v30mz_timestamp; |
| 219 | } |
| 220 | |
| 221 | void WSwan_SoundWrite(uint32 A, uint8 V) |
| 222 | { |
| 223 | WSwan_SoundUpdate(); |
| 224 | |
| 225 | if(A >= 0x80 && A <= 0x87) |
| 226 | { |
| 227 | int ch = (A - 0x80) >> 1; |
| 228 | |
| 229 | if(A & 1) |
| 230 | period[ch] = (period[ch] & 0x00FF) | ((V & 0x07) << 8); |
| 231 | else |
| 232 | period[ch] = (period[ch] & 0x0700) | ((V & 0xFF) << 0); |
| 233 | } |
| 234 | else if(A >= 0x88 && A <= 0x8B) |
| 235 | volume[A - 0x88] = V; |
| 236 | else if(A == 0x8C) |
| 237 | sweep_value = V; |
| 238 | else if(A == 0x8D) |
| 239 | { |
| 240 | sweep_step = V; |
| 241 | sweep_counter = sweep_step + 1; |
| 242 | sweep_8192_divider = 8192; |
| 243 | } |
| 244 | else if(A == 0x8E) |
| 245 | { |
| 246 | if(V & 0x8) |
| 247 | nreg = 0; |
| 248 | |
| 249 | noise_control = V & 0x17; |
| 250 | } |
| 251 | else if(A == 0x90) |
| 252 | { |
| 253 | int n; |
| 254 | for(n = 0; n < 4; n++) |
| 255 | { |
| 256 | if(!(control & (1 << n)) && (V & (1 << n))) |
| 257 | { |
| 258 | period_counter[n] = 1; |
| 259 | sample_pos[n] = 0x1F; |
| 260 | } |
| 261 | } |
| 262 | control = V; |
| 263 | } |
| 264 | else if(A == 0x91) |
| 265 | output_control = V & 0xF; |
| 266 | else if(A == 0x92) |
| 267 | nreg = (nreg & 0xFF00) | (V << 0); |
| 268 | else if(A == 0x93) |
| 269 | nreg = (nreg & 0x00FF) | ((V & 0x7F) << 8); |
| 270 | else if(A == 0x94) |
| 271 | voice_volume = V & 0xF; |
| 272 | else switch(A) |
| 273 | { |
| 274 | case 0x6A: HVoiceCtrl = V; break; |
| 275 | case 0x6B: HVoiceChanCtrl = V & 0x6F; break; |
| 276 | case 0x8F: SampleRAMPos = V; break; |
| 277 | case 0x95: HyperVoice = V; break; /* Pick a port, any port?! */ |
| 278 | } |
| 279 | WSwan_SoundUpdate(); |
| 280 | } |
| 281 | |
| 282 | uint8 WSwan_SoundRead(uint32 A) |
| 283 | { |
| 284 | WSwan_SoundUpdate(); |
| 285 | |
| 286 | if(A >= 0x80 && A <= 0x87) |
| 287 | { |
| 288 | int ch = (A - 0x80) >> 1; |
| 289 | |
| 290 | if(A & 1) |
| 291 | return(period[ch] >> 8); |
| 292 | return(period[ch]); |
| 293 | } |
| 294 | else if(A >= 0x88 && A <= 0x8B) |
| 295 | return(volume[A - 0x88]); |
| 296 | else switch(A) |
| 297 | { |
| 298 | case 0x6A: return(HVoiceCtrl); |
| 299 | case 0x6B: return(HVoiceChanCtrl); |
| 300 | case 0x8C: return(sweep_value); |
| 301 | case 0x8D: return(sweep_step); |
| 302 | case 0x8E: return(noise_control); |
| 303 | case 0x8F: return(SampleRAMPos); |
| 304 | case 0x90: return(control); |
| 305 | case 0x91: return(output_control | 0x80); |
| 306 | case 0x92: return((nreg >> 0) & 0xFF); |
| 307 | case 0x93: return((nreg >> 8) & 0xFF); |
| 308 | case 0x94: return(voice_volume); |
| 309 | default: |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | int32 WSwan_SoundFlush(int16 **SoundBuf, int32 *SoundBufSize) |
| 317 | { |
| 318 | int32 RequiredSize = 0; |
| 319 | int32 FrameCount = 0; |
| 320 | |
| 321 | WSwan_SoundUpdate(); |
| 322 | |
| 323 | Blip_Buffer_end_frame(&sbuf[0], v30mz_timestamp); |
| 324 | Blip_Buffer_end_frame(&sbuf[1], v30mz_timestamp); |
| 325 | |
| 326 | RequiredSize = Blip_Buffer_samples_avail(&sbuf[0]) << 1; |
| 327 | RequiredSize = (RequiredSize + 1) & ~1; |
| 328 | |
| 329 | if (SoundBuf && *SoundBuf) |
| 330 | { |
| 331 | /* Check if sound buffer needs to be resized */ |
| 332 | if (*SoundBufSize < RequiredSize) |
| 333 | { |
| 334 | int16 *newBuf = (int16*)realloc(*SoundBuf, |
| 335 | RequiredSize * sizeof(int16)); |
| 336 | |
| 337 | if (newBuf) |
| 338 | { |
| 339 | *SoundBuf = newBuf; |
| 340 | *SoundBufSize = RequiredSize; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | FrameCount = Blip_Buffer_read_samples(&sbuf[0], *SoundBuf , *SoundBufSize); |
| 345 | FrameCount = Blip_Buffer_read_samples(&sbuf[1], *SoundBuf + 1, *SoundBufSize); |
| 346 | } |
| 347 | |
| 348 | last_ts = 0; |
| 349 | |
| 350 | return(FrameCount); |
| 351 | } |
| 352 | |
| 353 | /* Call before wsRAM is updated */ |
| 354 | void WSwan_SoundCheckRAMWrite(uint32 A) |
| 355 | { |
| 356 | if((A >> 6) == SampleRAMPos) |
| 357 | WSwan_SoundUpdate(); |
| 358 | } |
| 359 | |
| 360 | static void RedoVolume(void) |
| 361 | { |
| 362 | double eff_volume = 1.0 / 4; |
| 363 | |
| 364 | Blip_Synth_set_volume(&WaveSynth, eff_volume, 256); |
| 365 | } |
| 366 | |
| 367 | void WSwan_SoundInit(void) |
| 368 | { |
| 369 | unsigned i; |
| 370 | for(i = 0; i < 2; i++) |
| 371 | { |
| 372 | Blip_Buffer_init(&sbuf[i]); |
| 373 | Blip_Buffer_set_sample_rate(&sbuf[i], 44100, 60); |
| 374 | Blip_Buffer_set_clock_rate(&sbuf[i], (long)(3072000)); |
| 375 | Blip_Buffer_bass_freq(&sbuf[i], 20); |
| 376 | } |
| 377 | |
| 378 | RedoVolume(); |
| 379 | } |
| 380 | |
| 381 | void WSwan_SoundKill(void) |
| 382 | { |
| 383 | int i; |
| 384 | for(i = 0; i < 2; i++) |
| 385 | { |
| 386 | Blip_Buffer_deinit(&sbuf[i]); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | bool WSwan_SetSoundRate(uint32 rate) |
| 391 | { |
| 392 | unsigned i; |
| 393 | for(i = 0; i < 2; i++) |
| 394 | Blip_Buffer_set_sample_rate(&sbuf[i], rate ? rate : 44100, 60); |
| 395 | |
| 396 | return(true); |
| 397 | } |
| 398 | |
| 399 | int WSwan_SoundStateAction(StateMem *sm, int load, int data_only) |
| 400 | { |
| 401 | SFORMAT StateRegs[] = |
| 402 | { |
| 403 | SFARRAY16(period, 4), |
| 404 | SFARRAY(volume, 4), |
| 405 | SFVAR(voice_volume), |
| 406 | SFVAR(sweep_step), |
| 407 | SFVAR(sweep_value), |
| 408 | SFVAR(noise_control), |
| 409 | SFVAR(control), |
| 410 | SFVAR(output_control), |
| 411 | SFVAR(HVoiceCtrl), |
| 412 | SFVAR(HVoiceChanCtrl), |
| 413 | |
| 414 | SFVAR(sweep_8192_divider), |
| 415 | SFVAR(sweep_counter), |
| 416 | SFVAR(SampleRAMPos), |
| 417 | SFARRAY32(period_counter, 4), |
| 418 | SFARRAY(sample_pos, 4), |
| 419 | SFVAR(nreg), |
| 420 | SFEND |
| 421 | }; |
| 422 | |
| 423 | if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "PSG", false)) |
| 424 | return 0; |
| 425 | |
| 426 | if(load) |
| 427 | { |
| 428 | unsigned ch; |
| 429 | if(sweep_8192_divider < 1) |
| 430 | sweep_8192_divider = 1; |
| 431 | |
| 432 | for(ch = 0; ch < 4; ch++) |
| 433 | { |
| 434 | period[ch] &= 0x7FF; |
| 435 | |
| 436 | if(period_counter[ch] < 1) |
| 437 | period_counter[ch] = 1; |
| 438 | |
| 439 | sample_pos[ch] &= 0x1F; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | return 1; |
| 444 | } |
| 445 | |
| 446 | void WSwan_SoundReset(void) |
| 447 | { |
| 448 | unsigned y,ch; |
| 449 | |
| 450 | memset(period, 0, sizeof(period)); |
| 451 | memset(volume, 0, sizeof(volume)); |
| 452 | voice_volume = 0; |
| 453 | sweep_step = 0; |
| 454 | sweep_value = 0; |
| 455 | noise_control = 0; |
| 456 | control = 0; |
| 457 | output_control = 0; |
| 458 | |
| 459 | sweep_8192_divider = 8192; |
| 460 | sweep_counter = 0; |
| 461 | SampleRAMPos = 0; |
| 462 | |
| 463 | for(ch = 0; ch < 4; ch++) |
| 464 | period_counter[ch] = 1; |
| 465 | |
| 466 | memset(sample_pos, 0, sizeof(sample_pos)); |
| 467 | nreg = 0; |
| 468 | |
| 469 | memset(sample_cache, 0, sizeof(sample_cache)); |
| 470 | memset(last_val, 0, sizeof(last_val)); |
| 471 | last_v_val = 0; |
| 472 | |
| 473 | HyperVoice = 0; |
| 474 | last_hv_val[0] = last_hv_val[1] = 0; |
| 475 | HVoiceCtrl = 0; |
| 476 | HVoiceChanCtrl = 0; |
| 477 | |
| 478 | for(y = 0; y < 2; y++) |
| 479 | Blip_Buffer_clear(&sbuf[y], 1); |
| 480 | } |