| 1 |
/******************************************************************************/ |
| 2 |
/* Mednafen - Multi-system Emulator */ |
| 3 |
/******************************************************************************/ |
| 4 |
/* sound.cpp - WonderSwan Sound Emulation |
| 5 |
** Copyright (C) 2007-2017 Mednafen Team |
| 6 |
** Copyright (C) 2016 Alex 'trap15' Marshall - http://daifukkat.su/ |
| 7 |
** |
| 8 |
** This program is free software; you can redistribute it and/or |
| 9 |
** modify it under the terms of the GNU General Public License |
| 10 |
** as published by the Free Software Foundation; either version 2 |
| 11 |
** of the License, or (at your option) any later version. |
| 12 |
** |
| 13 |
** This program is distributed in the hope that it will be useful, |
| 14 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
** GNU General Public License for more details. |
| 17 |
** |
| 18 |
** You should have received a copy of the GNU General Public License |
| 19 |
** along with this program; if not, write to the Free Software Foundation, Inc., |
| 20 |
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "swan_mdfn.h" |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#define MK_SAMPLE_CACHE \ |
| 27 |
{ \ |
| 28 |
int sample; \ |
| 29 |
sample = (((wsRAM[(/*(SampleRAMPos << 6) + */(sample_pos[ch] >> 1) + (ch << 4)) ] >> ((sample_pos[ch] & 1) ? 4 : 0)) & 0x0F)); \ |
| 30 |
sample_cache[ch][0] = sample * ((volume[ch] >> 4) & 0x0F); \ |
| 31 |
sample_cache[ch][1] = sample * ((volume[ch] >> 0) & 0x0F); \ |
| 32 |
} |
| 33 |
|
| 34 |
#define MK_SAMPLE_CACHE_NOISE \ |
| 35 |
{ \ |
| 36 |
int sample; \ |
| 37 |
sample = ((nreg & 1) ? 0xF : 0x0); \ |
| 38 |
sample_cache[ch][0] = sample * ((volume[ch] >> 4) & 0x0F); \ |
| 39 |
sample_cache[ch][1] = sample * ((volume[ch] >> 0) & 0x0F); \ |
| 40 |
} |
| 41 |
|
| 42 |
#define MK_SAMPLE_CACHE_VOICE \ |
| 43 |
{ \ |
| 44 |
int sample, half; \ |
| 45 |
sample = volume[ch]; \ |
| 46 |
half = sample >> 1; \ |
| 47 |
sample_cache[ch][0] = (voice_volume & 4) ? sample : (voice_volume & 8) ? half : 0; \ |
| 48 |
sample_cache[ch][1] = (voice_volume & 1) ? sample : (voice_volume & 2) ? half : 0; \ |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
#define SYNCSAMPLE(wt) \ |
| 53 |
{ \ |
| 54 |
int32_t left = sample_cache[ch][0] << 5, right = sample_cache[ch][1] << 5; \ |
| 55 |
if (left!=last_val[ch][0]) { \ |
| 56 |
blip_add_delta(sbuf[0], wt, left - last_val[ch][0]); \ |
| 57 |
last_val[ch][0] = left; \ |
| 58 |
} \ |
| 59 |
if (right!=last_val[ch][1]) { \ |
| 60 |
blip_add_delta(sbuf[1], wt, right - last_val[ch][1]); \ |
| 61 |
last_val[ch][1] = right; \ |
| 62 |
} \ |
| 63 |
oscBuf[ch]->putSample(wt,(left+right)<<1); \ |
| 64 |
} |
| 65 |
|
| 66 |
#define SYNCSAMPLE_NOISE(wt) SYNCSAMPLE(wt) |
| 67 |
|
| 68 |
void WSwan::SoundUpdate(void) |
| 69 |
{ |
| 70 |
int32_t run_time; |
| 71 |
|
| 72 |
//printf("%d\n", v30mz_timestamp); |
| 73 |
//printf("%02x %02x\n", control, noise_control); |
| 74 |
run_time = v30mz_timestamp - last_ts; |
| 75 |
|
| 76 |
for(unsigned int ch = 0; ch < 4; ch++) |
| 77 |
{ |
| 78 |
// Channel is disabled? |
| 79 |
if(!(control & (1 << ch))) |
| 80 |
continue; |
| 81 |
|
| 82 |
if(ch == 1 && (control & 0x20)) // Direct D/A mode? |
| 83 |
{ |
| 84 |
MK_SAMPLE_CACHE_VOICE; |
| 85 |
SYNCSAMPLE(v30mz_timestamp); |
| 86 |
} |
| 87 |
else if(ch == 2 && (control & 0x40) && sweep_value) // Sweep |
| 88 |
{ |
| 89 |
uint32_t tmp_pt = 2048 - period[ch]; |
| 90 |
uint32_t meow_timestamp = v30mz_timestamp - run_time; |
| 91 |
uint32_t tmp_run_time = run_time; |
| 92 |
|
| 93 |
while(tmp_run_time) |
| 94 |
{ |
| 95 |
int32_t sub_run_time = tmp_run_time; |
| 96 |
|
| 97 |
if(sub_run_time > sweep_8192_divider) |
| 98 |
sub_run_time = sweep_8192_divider; |
| 99 |
|
| 100 |
sweep_8192_divider -= sub_run_time; |
| 101 |
if(sweep_8192_divider <= 0) |
| 102 |
{ |
| 103 |
sweep_8192_divider += 8192; |
| 104 |
sweep_counter--; |
| 105 |
if(sweep_counter <= 0) |
| 106 |
{ |
| 107 |
sweep_counter = sweep_step + 1; |
| 108 |
period[ch] = (period[ch] + (int8_t)sweep_value) & 0x7FF; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
meow_timestamp += sub_run_time; |
| 113 |
if(tmp_pt > 4) |
| 114 |
{ |
| 115 |
period_counter[ch] -= sub_run_time; |
| 116 |
while(period_counter[ch] <= 0) |
| 117 |
{ |
| 118 |
sample_pos[ch] = (sample_pos[ch] + 1) & 0x1F; |
| 119 |
|
| 120 |
MK_SAMPLE_CACHE; |
| 121 |
SYNCSAMPLE(meow_timestamp + period_counter[ch]); |
| 122 |
period_counter[ch] += tmp_pt; |
| 123 |
} |
| 124 |
} |
| 125 |
tmp_run_time -= sub_run_time; |
| 126 |
} |
| 127 |
} |
| 128 |
else if(ch == 3 && (control & 0x80) && (noise_control & 0x10)) // Noise |
| 129 |
{ |
| 130 |
uint32_t tmp_pt = 2048 - period[ch]; |
| 131 |
|
| 132 |
period_counter[ch] -= run_time; |
| 133 |
while(period_counter[ch] <= 0) |
| 134 |
{ |
| 135 |
static const uint8_t stab[8] = { 14, 10, 13, 4, 8, 6, 9, 11 }; |
| 136 |
|
| 137 |
nreg = ((nreg << 1) | ((1 ^ (nreg >> 7) ^ (nreg >> stab[noise_control & 0x7])) & 1)) & 0x7FFF; |
| 138 |
|
| 139 |
if(control & 0x80) |
| 140 |
{ |
| 141 |
MK_SAMPLE_CACHE_NOISE; |
| 142 |
SYNCSAMPLE_NOISE(v30mz_timestamp + period_counter[ch]); |
| 143 |
} |
| 144 |
else if(tmp_pt > 4) |
| 145 |
{ |
| 146 |
sample_pos[ch] = (sample_pos[ch] + 1) & 0x1F; |
| 147 |
MK_SAMPLE_CACHE; |
| 148 |
SYNCSAMPLE(v30mz_timestamp + period_counter[ch]); |
| 149 |
} |
| 150 |
period_counter[ch] += tmp_pt; |
| 151 |
} |
| 152 |
} |
| 153 |
else |
| 154 |
{ |
| 155 |
uint32_t tmp_pt = 2048 - period[ch]; |
| 156 |
|
| 157 |
if(tmp_pt > 4) |
| 158 |
{ |
| 159 |
period_counter[ch] -= run_time; |
| 160 |
while(period_counter[ch] <= 0) |
| 161 |
{ |
| 162 |
sample_pos[ch] = (sample_pos[ch] + 1) & 0x1F; |
| 163 |
|
| 164 |
MK_SAMPLE_CACHE; |
| 165 |
SYNCSAMPLE(v30mz_timestamp + period_counter[ch]); // - period_counter[ch]); |
| 166 |
period_counter[ch] += tmp_pt; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if(HVoiceCtrl & 0x80) |
| 173 |
{ |
| 174 |
int16_t sample = (uint8_t)HyperVoice; |
| 175 |
|
| 176 |
switch(HVoiceCtrl & 0xC) |
| 177 |
{ |
| 178 |
case 0x0: sample = (uint16_t)sample << (8 - (HVoiceCtrl & 3)); break; |
| 179 |
case 0x4: sample = (uint16_t)(sample | -0x100) << (8 - (HVoiceCtrl & 3)); break; |
| 180 |
case 0x8: sample = (uint16_t)((int8_t)sample) << (8 - (HVoiceCtrl & 3)); break; |
| 181 |
case 0xC: sample = (uint16_t)sample << 8; break; |
| 182 |
} |
| 183 |
// bring back to 11bit, keeping signedness |
| 184 |
sample >>= 5; |
| 185 |
|
| 186 |
int32_t left, right; |
| 187 |
left = (HVoiceChanCtrl & 0x40) ? (sample << 5) : 0; |
| 188 |
right = (HVoiceChanCtrl & 0x20) ? (sample << 5) : 0; |
| 189 |
|
| 190 |
if (left!=last_hv_val[0]) { |
| 191 |
blip_add_delta(sbuf[0], v30mz_timestamp, left - last_hv_val[0]); |
| 192 |
last_hv_val[0] = left; |
| 193 |
} |
| 194 |
if (right!=last_hv_val[1]) { |
| 195 |
blip_add_delta(sbuf[1], v30mz_timestamp, right - last_hv_val[1]); |
| 196 |
last_hv_val[1] = right; |
| 197 |
} |
| 198 |
} |
| 199 |
last_ts = v30mz_timestamp; |
| 200 |
} |
| 201 |
|
| 202 |
void WSwan::SoundWrite(uint32_t A, uint8_t V) |
| 203 |
{ |
| 204 |
SoundUpdate(); |
| 205 |
|
| 206 |
if(A >= 0x80 && A <= 0x87) |
| 207 |
{ |
| 208 |
int ch = (A - 0x80) >> 1; |
| 209 |
|
| 210 |
if(A & 1) |
| 211 |
period[ch] = (period[ch] & 0x00FF) | ((V & 0x07) << 8); |
| 212 |
else |
| 213 |
period[ch] = (period[ch] & 0x0700) | ((V & 0xFF) << 0); |
| 214 |
|
| 215 |
//printf("Period %d: 0x%04x --- %f\n", ch, period[ch], 3072000.0 / (2048 - period[ch])); |
| 216 |
} |
| 217 |
else if(A >= 0x88 && A <= 0x8B) |
| 218 |
{ |
| 219 |
volume[A - 0x88] = V; |
| 220 |
} |
| 221 |
else if(A == 0x8C) |
| 222 |
sweep_value = V; |
| 223 |
else if(A == 0x8D) |
| 224 |
{ |
| 225 |
sweep_step = V; |
| 226 |
sweep_counter = sweep_step + 1; |
| 227 |
sweep_8192_divider = 8192; |
| 228 |
} |
| 229 |
else if(A == 0x8E) |
| 230 |
{ |
| 231 |
//printf("NOISECONTROL: %02x\n", V); |
| 232 |
if(V & 0x8) |
| 233 |
nreg = 0; |
| 234 |
|
| 235 |
noise_control = V & 0x17; |
| 236 |
} |
| 237 |
else if(A == 0x90) |
| 238 |
{ |
| 239 |
for(int n = 0; n < 4; n++) |
| 240 |
{ |
| 241 |
if(!(control & (1 << n)) && (V & (1 << n))) |
| 242 |
{ |
| 243 |
period_counter[n] = 1; |
| 244 |
sample_pos[n] = 0x1F; |
| 245 |
} |
| 246 |
} |
| 247 |
control = V; |
| 248 |
//printf("Sound Control: %02x\n", V); |
| 249 |
} |
| 250 |
else if(A == 0x91) |
| 251 |
{ |
| 252 |
output_control = V & 0xF; |
| 253 |
//printf("%02x, %02x\n", V, (V >> 1) & 3); |
| 254 |
} |
| 255 |
else if(A == 0x92) |
| 256 |
nreg = (nreg & 0xFF00) | (V << 0); |
| 257 |
else if(A == 0x93) |
| 258 |
nreg = (nreg & 0x00FF) | ((V & 0x7F) << 8); |
| 259 |
else if(A == 0x94) |
| 260 |
{ |
| 261 |
voice_volume = V & 0xF; |
| 262 |
//printf("%02x\n", V); |
| 263 |
} |
| 264 |
else switch(A) |
| 265 |
{ |
| 266 |
case 0x6A: HVoiceCtrl = V; break; |
| 267 |
case 0x6B: HVoiceChanCtrl = V & 0x6F; break; |
| 268 |
case 0x8F: SampleRAMPos = V; break; |
| 269 |
case 0x95: HyperVoice = V; break; // Pick a port, any port?! |
| 270 |
//default: printf("%04x:%02x\n", A, V); break; |
| 271 |
} |
| 272 |
SoundUpdate(); |
| 273 |
} |
| 274 |
|
| 275 |
uint8_t WSwan::SoundRead(uint32_t A) |
| 276 |
{ |
| 277 |
SoundUpdate(); |
| 278 |
|
| 279 |
if(A >= 0x80 && A <= 0x87) |
| 280 |
{ |
| 281 |
int ch = (A - 0x80) >> 1; |
| 282 |
|
| 283 |
if(A & 1) |
| 284 |
return(period[ch] >> 8); |
| 285 |
else |
| 286 |
return(period[ch]); |
| 287 |
} |
| 288 |
else if(A >= 0x88 && A <= 0x8B) |
| 289 |
return(volume[A - 0x88]); |
| 290 |
else switch(A) |
| 291 |
{ |
| 292 |
default: /*printf("SoundRead: %04x\n", A);*/ return(0); |
| 293 |
case 0x6A: return(HVoiceCtrl); |
| 294 |
case 0x6B: return(HVoiceChanCtrl); |
| 295 |
case 0x8C: return(sweep_value); |
| 296 |
case 0x8D: return(sweep_step); |
| 297 |
case 0x8E: return(noise_control); |
| 298 |
case 0x8F: return(SampleRAMPos); |
| 299 |
case 0x90: return(control); |
| 300 |
case 0x91: return(output_control | 0x80); |
| 301 |
case 0x92: return((nreg >> 0) & 0xFF); |
| 302 |
case 0x93: return((nreg >> 8) & 0xFF); |
| 303 |
case 0x94: return(voice_volume); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
void WSwan::RAMWrite(uint32_t A, uint8_t V) |
| 308 |
{ |
| 309 |
wsRAM[A & 0x3F] = V; |
| 310 |
} |
| 311 |
|
| 312 |
int32_t WSwan::SoundFlush(int16_t *SoundBuf, const int32_t MaxSoundFrames) |
| 313 |
{ |
| 314 |
int32_t FrameCount = 0; |
| 315 |
|
| 316 |
SoundUpdate(); |
| 317 |
|
| 318 |
|
| 319 |
last_ts = 0; |
| 320 |
|
| 321 |
return(FrameCount); |
| 322 |
} |
| 323 |
|
| 324 |
// Call before wsRAM is updated |
| 325 |
void WSwan::SoundCheckRAMWrite(uint32_t A) |
| 326 |
{ |
| 327 |
if((A >> 6) == SampleRAMPos) |
| 328 |
SoundUpdate(); |
| 329 |
} |
| 330 |
|
| 331 |
void WSwan::SoundReset(void) |
| 332 |
{ |
| 333 |
memset(period, 0, sizeof(period)); |
| 334 |
memset(volume, 0, sizeof(volume)); |
| 335 |
voice_volume = 0; |
| 336 |
sweep_step = 0; |
| 337 |
sweep_value = 0; |
| 338 |
noise_control = 0; |
| 339 |
control = 0; |
| 340 |
output_control = 0; |
| 341 |
|
| 342 |
sweep_8192_divider = 8192; |
| 343 |
sweep_counter = 1; |
| 344 |
SampleRAMPos = 0; |
| 345 |
|
| 346 |
for(unsigned ch = 0; ch < 4; ch++) |
| 347 |
period_counter[ch] = 1; |
| 348 |
|
| 349 |
memset(sample_pos, 0, sizeof(sample_pos)); |
| 350 |
nreg = 0; |
| 351 |
|
| 352 |
memset(sample_cache, 0, sizeof(sample_cache)); |
| 353 |
memset(last_val, 0, sizeof(last_val)); |
| 354 |
last_v_val = 0; |
| 355 |
|
| 356 |
HyperVoice = 0; |
| 357 |
last_hv_val[0] = last_hv_val[1] = 0; |
| 358 |
HVoiceCtrl = 0; |
| 359 |
HVoiceChanCtrl = 0; |
| 360 |
|
| 361 |
last_ts = 0; |
| 362 |
v30mz_timestamp = 0; |
| 363 |
} |