| 1 |
/* |
| 2 |
|
| 3 |
============================================================================ |
| 4 |
|
| 5 |
NDS sound emulator |
| 6 |
by cam900 |
| 7 |
|
| 8 |
MODIFIED BY TILDEARROW!!! |
| 9 |
MODIFIED BY TILDEARROW!!! |
| 10 |
MODIFIED BY TILDEARROW!!! |
| 11 |
MODIFIED BY TILDEARROW!!! |
| 12 |
MODIFIED BY TILDEARROW!!! |
| 13 |
MODIFIED BY TILDEARROW!!! |
| 14 |
|
| 15 |
making it SUPER CLEAR to comply with the license |
| 16 |
this is NOT the original version! for the original version, git checkout |
| 17 |
any commit from January 2025. |
| 18 |
|
| 19 |
This file is licensed under zlib license. |
| 20 |
|
| 21 |
============================================================================ |
| 22 |
|
| 23 |
zlib License |
| 24 |
|
| 25 |
(C) 2024-present cam900 and contributors |
| 26 |
|
| 27 |
This software is provided 'as-is', without any express or implied |
| 28 |
warranty. In no event will the authors be held liable for any damages |
| 29 |
arising from the use of this software. |
| 30 |
|
| 31 |
Permission is granted to anyone to use this software for any purpose, |
| 32 |
including commercial applications, and to alter it and redistribute it |
| 33 |
freely, subject to the following restrictions: |
| 34 |
|
| 35 |
1. The origin of this software must not be misrepresented; you must not |
| 36 |
claim that you wrote the original software. If you use this software |
| 37 |
in a product, an acknowledgment in the product documentation would be |
| 38 |
appreciated but is not required. |
| 39 |
2. Altered source versions must be plainly marked as such, and must not be |
| 40 |
misrepresented as being the original software. |
| 41 |
3. This notice may not be removed or altered from any source distribution. |
| 42 |
|
| 43 |
============================================================================ |
| 44 |
TODO: |
| 45 |
- needs to further verifications from real hardware |
| 46 |
|
| 47 |
Tech info: https://problemkaputt.de/gbatek.htm |
| 48 |
|
| 49 |
*/ |
| 50 |
|
| 51 |
#include "nds.hpp" |
| 52 |
#include <stdlib.h> |
| 53 |
|
| 54 |
namespace nds_sound_emu |
| 55 |
{ |
| 56 |
void nds_sound_t::reset() |
| 57 |
{ |
| 58 |
for (channel_t &elem : m_channel) |
| 59 |
elem.reset(); |
| 60 |
for (capture_t &elem : m_capture) |
| 61 |
elem.reset(); |
| 62 |
|
| 63 |
m_control = 0; |
| 64 |
m_bias = 0; |
| 65 |
} |
| 66 |
|
| 67 |
s32 nds_sound_t::predict() { |
| 68 |
s32 ret=INT32_MAX; |
| 69 |
for (u8 i = 0; i < 16; i++) { |
| 70 |
const s32 next=m_channel[i].predict(); |
| 71 |
if (next<ret) ret=next; |
| 72 |
} |
| 73 |
return ret; |
| 74 |
} |
| 75 |
|
| 76 |
void nds_sound_t::resetTS(u32 what) { |
| 77 |
m_lastts = what; |
| 78 |
for (u8 i = 0; i < 16; i++) { |
| 79 |
m_channel[i].resetTS(m_lastts); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
void nds_sound_t::set_bb(blip_buffer_t* bbLeft, blip_buffer_t* bbRight) { |
| 84 |
for (u8 i = 0; i < 16; i++) { |
| 85 |
m_channel[i].set_bb(bbLeft,bbRight); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
void nds_sound_t::set_oscbuf(DivDispatchOscBuffer** oscBuf) { |
| 90 |
for (u8 i = 0; i < 16; i++) { |
| 91 |
m_channel[i].set_oscbuf(oscBuf[i]); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
void nds_sound_t::tick(s32 cycle) |
| 96 |
{ |
| 97 |
if (!enable()) |
| 98 |
return; |
| 99 |
|
| 100 |
// mix outputs |
| 101 |
s32 lmix = 0, rmix = 0; |
| 102 |
for (u8 i = 0; i < 16; i++) |
| 103 |
{ |
| 104 |
channel_t &channel = m_channel[i]; |
| 105 |
channel.update(cycle); |
| 106 |
/* |
| 107 |
// bypass mixer |
| 108 |
if (((i == 1) && (mix_ch1())) || ((i == 3) && (mix_ch3()))) |
| 109 |
continue; |
| 110 |
|
| 111 |
lmix += channel.loutput(); |
| 112 |
rmix += channel.routput(); |
| 113 |
*/ |
| 114 |
} |
| 115 |
|
| 116 |
return; // don't care about the rest |
| 117 |
|
| 118 |
// send mixer output to capture |
| 119 |
m_capture[0].update(lmix, cycle); |
| 120 |
m_capture[1].update(rmix, cycle); |
| 121 |
|
| 122 |
// select left/right output source |
| 123 |
switch (lout_from()) |
| 124 |
{ |
| 125 |
case 0: // left mixer |
| 126 |
break; |
| 127 |
case 1: // channel 1 |
| 128 |
lmix = m_channel[1].loutput(); |
| 129 |
break; |
| 130 |
case 2: // channel 3 |
| 131 |
lmix = m_channel[3].loutput(); |
| 132 |
break; |
| 133 |
case 3: // channel 1 + 3 |
| 134 |
lmix = m_channel[1].loutput() + m_channel[3].loutput(); |
| 135 |
break; |
| 136 |
} |
| 137 |
|
| 138 |
switch (rout_from()) |
| 139 |
{ |
| 140 |
case 0: // right mixer |
| 141 |
break; |
| 142 |
case 1: // channel 1 |
| 143 |
rmix = m_channel[1].routput(); |
| 144 |
break; |
| 145 |
case 2: // channel 3 |
| 146 |
rmix = m_channel[3].routput(); |
| 147 |
break; |
| 148 |
case 3: // channel 1 + 3 |
| 149 |
rmix = m_channel[1].routput() + m_channel[3].routput(); |
| 150 |
break; |
| 151 |
} |
| 152 |
|
| 153 |
// adjust master volume |
| 154 |
lmix = (lmix * mvol()) >> 13; |
| 155 |
rmix = (rmix * mvol()) >> 13; |
| 156 |
} |
| 157 |
|
| 158 |
u8 nds_sound_t::read8(u32 addr) |
| 159 |
{ |
| 160 |
return bitfield(read32(addr >> 2), bitfield(addr, 0, 2) << 3, 8); |
| 161 |
} |
| 162 |
|
| 163 |
u16 nds_sound_t::read16(u32 addr) |
| 164 |
{ |
| 165 |
return bitfield(read32(addr >> 1), bitfield(addr, 0) << 4, 16); |
| 166 |
} |
| 167 |
|
| 168 |
u32 nds_sound_t::read32(u32 addr) |
| 169 |
{ |
| 170 |
addr <<= 2; // word address |
| 171 |
|
| 172 |
switch (addr & 0x100) |
| 173 |
{ |
| 174 |
case 0x000: |
| 175 |
if ((addr & 0xc) == 0) |
| 176 |
return m_channel[bitfield(addr, 4, 4)].control(); |
| 177 |
break; |
| 178 |
case 0x100: |
| 179 |
switch (addr & 0xff) |
| 180 |
{ |
| 181 |
case 0x00: |
| 182 |
return m_control; |
| 183 |
case 0x04: |
| 184 |
return m_bias; |
| 185 |
case 0x08: |
| 186 |
return m_capture[0].control() | (m_capture[1].control() << 8); |
| 187 |
case 0x10: |
| 188 |
case 0x18: |
| 189 |
return m_capture[bitfield(addr, 3)].dstaddr(); |
| 190 |
default: |
| 191 |
break; |
| 192 |
} |
| 193 |
break; |
| 194 |
} |
| 195 |
return 0; |
| 196 |
} |
| 197 |
|
| 198 |
void nds_sound_t::write8(u32 addr, u8 data) |
| 199 |
{ |
| 200 |
const u8 bit = bitfield(addr, 0, 2); |
| 201 |
const u32 in = u32(data) << (bit << 3); |
| 202 |
const u32 in_mask = 0xff << (bit << 3); |
| 203 |
write32(addr >> 2, in, in_mask); |
| 204 |
} |
| 205 |
|
| 206 |
void nds_sound_t::write16(u32 addr, u16 data, u16 mask) |
| 207 |
{ |
| 208 |
const u8 bit = bitfield(addr, 0); |
| 209 |
const u32 in = u32(data) << (bit << 4); |
| 210 |
const u32 in_mask = u32(mask) << (bit << 4); |
| 211 |
write32(addr >> 1, in, in_mask); |
| 212 |
} |
| 213 |
|
| 214 |
void nds_sound_t::write32(u32 addr, u32 data, u32 mask) |
| 215 |
{ |
| 216 |
addr <<= 2; // word address |
| 217 |
|
| 218 |
switch (addr & 0x100) |
| 219 |
{ |
| 220 |
case 0x000: |
| 221 |
m_channel[bitfield(addr, 4, 4)].write(bitfield(addr, 2, 2), data, mask); |
| 222 |
break; |
| 223 |
case 0x100: |
| 224 |
switch (addr & 0xff) |
| 225 |
{ |
| 226 |
case 0x00: |
| 227 |
m_control = (m_control & ~mask) | (data & mask); |
| 228 |
break; |
| 229 |
case 0x04: |
| 230 |
mask &= 0x3ff; |
| 231 |
m_bias = (m_bias & ~mask) | (data & mask); |
| 232 |
break; |
| 233 |
case 0x08: |
| 234 |
if (bitfield(mask, 0, 8)) |
| 235 |
m_capture[0].control_w(data & 0xff); |
| 236 |
if (bitfield(mask, 8, 8)) |
| 237 |
m_capture[1].control_w((data >> 8) & 0xff); |
| 238 |
break; |
| 239 |
case 0x10: |
| 240 |
case 0x14: |
| 241 |
case 0x18: |
| 242 |
case 0x1c: |
| 243 |
m_capture[bitfield(addr, 3)].addrlen_w(bitfield(addr, 2), data, mask); |
| 244 |
break; |
| 245 |
default: |
| 246 |
break; |
| 247 |
} |
| 248 |
break; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
// channels |
| 253 |
void nds_sound_t::channel_t::reset() |
| 254 |
{ |
| 255 |
m_control = 0; |
| 256 |
m_sourceaddr = 0; |
| 257 |
m_freq = 0; |
| 258 |
m_loopstart = 0; |
| 259 |
m_length = 0; |
| 260 |
|
| 261 |
m_ctl_volume = 0; |
| 262 |
m_ctl_voldiv = 0; |
| 263 |
m_ctl_hold = 0; |
| 264 |
m_ctl_pan = 0; |
| 265 |
m_ctl_duty = 0; |
| 266 |
m_ctl_repeat = 0; |
| 267 |
m_ctl_format = 0; |
| 268 |
m_ctl_busy = 0; |
| 269 |
|
| 270 |
m_playing = false; |
| 271 |
m_adpcm_out = 0; |
| 272 |
m_adpcm_index = 0; |
| 273 |
m_prev_adpcm_out = 0; |
| 274 |
m_prev_adpcm_index = 0; |
| 275 |
m_cur_addr = 0; |
| 276 |
m_cur_state = 0; |
| 277 |
m_cur_bitaddr = 0; |
| 278 |
m_delay = 0; |
| 279 |
m_sample = 0; |
| 280 |
m_lfsr = 0x7fff; |
| 281 |
m_lfsr_out = 0x7fff; |
| 282 |
m_counter = 0x10000; |
| 283 |
m_output = 0; |
| 284 |
m_loutput = 0; |
| 285 |
m_routput = 0; |
| 286 |
} |
| 287 |
|
| 288 |
void nds_sound_t::channel_t::write(u32 offset, u32 data, u32 mask) |
| 289 |
{ |
| 290 |
const u32 old = m_control; |
| 291 |
switch (offset & 3) |
| 292 |
{ |
| 293 |
case 0: // Control/Status |
| 294 |
m_control = (m_control & ~mask) | (data & mask); |
| 295 |
|
| 296 |
// explode this register |
| 297 |
m_ctl_volume = bitfield(m_control, 0, 7); |
| 298 |
m_ctl_voldiv = m_voldiv_shift[bitfield(m_control, 8, 2)]; |
| 299 |
m_ctl_hold = bitfield(m_control, 15); |
| 300 |
m_ctl_pan = bitfield(m_control, 16, 7); |
| 301 |
m_ctl_duty = bitfield(m_control, 24, 3); |
| 302 |
m_ctl_repeat = bitfield(m_control, 27, 2); |
| 303 |
m_ctl_format = bitfield(m_control, 29, 2); |
| 304 |
m_ctl_busy = bitfield(m_control, 31); |
| 305 |
|
| 306 |
if (bitfield(old ^ m_control, 31)) |
| 307 |
{ |
| 308 |
if (m_ctl_busy) |
| 309 |
keyon(); |
| 310 |
else if (!m_ctl_busy) |
| 311 |
keyoff(); |
| 312 |
} |
| 313 |
// reset hold flag |
| 314 |
if (!m_playing && !m_ctl_hold) |
| 315 |
{ |
| 316 |
m_sample = m_lfsr_out = 0; |
| 317 |
m_oscBuf->putSample(m_lastts,0); |
| 318 |
blip_add_delta(m_bb[0],m_lastts,-m_loutput); |
| 319 |
blip_add_delta(m_bb[1],m_lastts,-m_routput); |
| 320 |
m_output = m_loutput = m_routput = 0; |
| 321 |
} |
| 322 |
break; |
| 323 |
case 1: // Source address |
| 324 |
mask &= 0x7ffffff; |
| 325 |
m_sourceaddr = (m_sourceaddr & ~mask) | (data & mask); |
| 326 |
break; |
| 327 |
case 2: // Frequency, Loopstart |
| 328 |
if (bitfield(mask, 0, 16)) |
| 329 |
m_freq = (m_freq & bitfield(~mask, 0, 16)) | (bitfield(data & mask, 0, 16)); |
| 330 |
if (bitfield(mask, 16, 16)) |
| 331 |
m_loopstart = (m_loopstart & bitfield(~mask, 16, 16)) | (bitfield(data & mask, 16, 16)); |
| 332 |
break; |
| 333 |
case 3: // Length |
| 334 |
mask &= 0x3fffff; |
| 335 |
m_length = (m_length & ~mask) | (data & mask); |
| 336 |
break; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
void nds_sound_t::channel_t::keyon() |
| 341 |
{ |
| 342 |
if (!m_playing) |
| 343 |
{ |
| 344 |
m_playing = true; |
| 345 |
m_delay = m_ctl_format == 2 ? 11 : 3; // 3 (11 for ADPCM) delay for playing sample |
| 346 |
m_cur_bitaddr = m_cur_addr = 0; |
| 347 |
m_cur_state = (m_ctl_format == 2) ? STATE_ADPCM_LOAD : ((m_loopstart == 0) ? STATE_POST_LOOP : STATE_PRE_LOOP); |
| 348 |
m_counter = 0x10000; |
| 349 |
m_sample = 0; |
| 350 |
m_lfsr_out = 0x7fff; |
| 351 |
m_lfsr = 0x7fff; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
void nds_sound_t::channel_t::keyoff() |
| 357 |
{ |
| 358 |
if (m_playing) |
| 359 |
{ |
| 360 |
if (m_ctl_busy) { |
| 361 |
m_control &= ~(1 << 31); |
| 362 |
m_ctl_busy = false; |
| 363 |
} |
| 364 |
if (!m_ctl_hold) |
| 365 |
{ |
| 366 |
m_sample = m_lfsr_out = 0; |
| 367 |
m_oscBuf->putSample(m_lastts,0); |
| 368 |
blip_add_delta(m_bb[0],m_lastts,-m_loutput); |
| 369 |
blip_add_delta(m_bb[1],m_lastts,-m_routput); |
| 370 |
m_output = m_loutput = m_routput = 0; |
| 371 |
} |
| 372 |
|
| 373 |
m_playing = false; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
// sorry. I need my spaces back. |
| 378 |
void nds_sound_t::channel_t::update(s32 timestamp) |
| 379 |
{ |
| 380 |
if (m_playing) |
| 381 |
{ |
| 382 |
for (s32 i=m_lastts; i<timestamp; i++) { |
| 383 |
int cycle = m_counter - m_freq; |
| 384 |
if (cycle>timestamp-i) cycle=timestamp-i; |
| 385 |
if (cycle<1) cycle=1; |
| 386 |
|
| 387 |
// get output |
| 388 |
m_counter -= cycle; |
| 389 |
while (m_counter <= m_freq) |
| 390 |
{ |
| 391 |
// advance |
| 392 |
fetch(); |
| 393 |
advance(); |
| 394 |
m_counter += 0x10000 - m_freq; |
| 395 |
} |
| 396 |
m_output = (m_sample * m_ctl_volume) >> (7 + m_ctl_voldiv); |
| 397 |
const s32 loutput = (m_output * lvol()) >> 7; |
| 398 |
const s32 routput = (m_output * rvol()) >> 7; |
| 399 |
|
| 400 |
i+=cycle-1; |
| 401 |
|
| 402 |
if (m_loutput!=loutput || m_routput!=routput) { |
| 403 |
m_oscBuf->putSample(i,(loutput+routput)>>1); |
| 404 |
} |
| 405 |
if (m_loutput!=loutput) { |
| 406 |
blip_add_delta(m_bb[0],i,loutput-m_loutput); |
| 407 |
m_loutput=loutput; |
| 408 |
} |
| 409 |
if (m_routput!=routput) { |
| 410 |
blip_add_delta(m_bb[1],i,routput-m_routput); |
| 411 |
m_routput=routput; |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
m_lastts = timestamp; |
| 416 |
} |
| 417 |
|
| 418 |
s32 nds_sound_t::channel_t::predict() { |
| 419 |
if (!m_playing) return INT32_MAX; |
| 420 |
if (!(m_ctl_volume)) return INT32_MAX; |
| 421 |
return m_counter-m_freq; |
| 422 |
} |
| 423 |
|
| 424 |
void nds_sound_t::channel_t::fetch() |
| 425 |
{ |
| 426 |
if (m_playing) |
| 427 |
{ |
| 428 |
// fetch samples |
| 429 |
switch (m_ctl_format) |
| 430 |
{ |
| 431 |
case 0: // PCM8 |
| 432 |
m_sample = s16(m_host.m_intf.read_byte(addr()) << 8); |
| 433 |
break; |
| 434 |
case 1: // PCM16 |
| 435 |
m_sample = m_host.m_intf.read_word(addr()); |
| 436 |
break; |
| 437 |
case 2: // ADPCM |
| 438 |
m_sample = m_cur_state == STATE_ADPCM_LOAD ? 0 : m_adpcm_out; |
| 439 |
break; |
| 440 |
case 3: // PSG or Noise |
| 441 |
m_sample = 0; |
| 442 |
if (m_psg) // psg |
| 443 |
m_sample = (m_ctl_duty == 7) ? -0x7fff : ((m_cur_bitaddr < s32(u32(7) - m_ctl_duty)) ? -0x7fff : 0x7fff); |
| 444 |
else if (m_noise) // noise |
| 445 |
m_sample = m_lfsr_out; |
| 446 |
break; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
// apply delay |
| 451 |
if (m_ctl_format != 3 && m_delay > 0) |
| 452 |
m_sample = 0; |
| 453 |
} |
| 454 |
|
| 455 |
void nds_sound_t::channel_t::advance() |
| 456 |
{ |
| 457 |
if (m_playing) |
| 458 |
{ |
| 459 |
// advance bit address |
| 460 |
switch (m_ctl_format) |
| 461 |
{ |
| 462 |
case 0: // PCM8 |
| 463 |
m_cur_bitaddr += 8; |
| 464 |
break; |
| 465 |
case 1: // PCM16 |
| 466 |
m_cur_bitaddr += 16; |
| 467 |
break; |
| 468 |
case 2: // ADPCM |
| 469 |
if (m_cur_state == STATE_ADPCM_LOAD) // load ADPCM data |
| 470 |
{ |
| 471 |
if (m_cur_bitaddr == 0) |
| 472 |
m_prev_adpcm_out = m_adpcm_out = m_host.m_intf.read_word(addr()); |
| 473 |
if (m_cur_bitaddr == 16) |
| 474 |
m_prev_adpcm_index = m_adpcm_index = clamp<s32>(m_host.m_intf.read_byte(addr()) & 0x7f, 0, 88); |
| 475 |
} |
| 476 |
else // decode ADPCM |
| 477 |
{ |
| 478 |
const u8 input = bitfield(m_host.m_intf.read_byte(addr()), m_cur_bitaddr & 4, 4); |
| 479 |
s32 diff = ((bitfield(input, 0, 3) * 2 + 1) * m_host.adpcm_diff_table[m_adpcm_index] / 8); |
| 480 |
if (bitfield(input, 3)) diff = -diff; |
| 481 |
m_adpcm_out = clamp<s32>(m_adpcm_out + diff, -0x8000, 0x7fff); |
| 482 |
m_adpcm_index = clamp<s32>(m_adpcm_index + m_host.adpcm_index_table[bitfield(input, 0, 3)], 0, 88); |
| 483 |
} |
| 484 |
m_cur_bitaddr += 4; |
| 485 |
break; |
| 486 |
case 3: // PSG or Noise |
| 487 |
if (m_psg) // psg |
| 488 |
m_cur_bitaddr = (m_cur_bitaddr + 1) & 7; |
| 489 |
else if (m_noise) // noise |
| 490 |
{ |
| 491 |
if (bitfield(m_lfsr, 0)) |
| 492 |
{ |
| 493 |
m_lfsr = (m_lfsr >> 1) ^ 0x6000; |
| 494 |
m_lfsr_out = -0x7fff; |
| 495 |
} |
| 496 |
else |
| 497 |
{ |
| 498 |
m_lfsr >>= 1; |
| 499 |
m_lfsr_out = 0x7fff; |
| 500 |
} |
| 501 |
} |
| 502 |
break; |
| 503 |
} |
| 504 |
|
| 505 |
// address update |
| 506 |
if (m_ctl_format != 3) |
| 507 |
{ |
| 508 |
// adjust delay |
| 509 |
m_delay--; |
| 510 |
|
| 511 |
// update address, loop |
| 512 |
while (m_cur_bitaddr >= 32) |
| 513 |
{ |
| 514 |
// already loaded? |
| 515 |
if (m_ctl_format == 2 && m_cur_state == STATE_ADPCM_LOAD) |
| 516 |
{ |
| 517 |
m_cur_state = m_loopstart == 0 ? STATE_POST_LOOP : STATE_PRE_LOOP; |
| 518 |
} |
| 519 |
m_cur_addr++; |
| 520 |
if (m_cur_state == STATE_PRE_LOOP && m_cur_addr >= m_loopstart) |
| 521 |
{ |
| 522 |
m_cur_state = STATE_POST_LOOP; |
| 523 |
m_cur_addr = 0; |
| 524 |
if (m_ctl_format == 2) |
| 525 |
{ |
| 526 |
m_prev_adpcm_out = m_adpcm_out; |
| 527 |
m_prev_adpcm_index = m_adpcm_index; |
| 528 |
} |
| 529 |
} |
| 530 |
else if (m_cur_state == STATE_POST_LOOP && m_cur_addr >= m_length) |
| 531 |
{ |
| 532 |
switch (m_ctl_repeat) |
| 533 |
{ |
| 534 |
case 0: // manual; not correct? |
| 535 |
case 2: // one-shot |
| 536 |
case 3: // prohibited |
| 537 |
keyoff(); |
| 538 |
break; |
| 539 |
case 1: // loop infinitely |
| 540 |
if (m_ctl_format == 2) |
| 541 |
{ |
| 542 |
if (m_loopstart == 0) // reload ADPCM |
| 543 |
{ |
| 544 |
m_cur_state = STATE_ADPCM_LOAD; |
| 545 |
} |
| 546 |
else // restore |
| 547 |
{ |
| 548 |
m_adpcm_out = m_prev_adpcm_out; |
| 549 |
m_adpcm_index = m_prev_adpcm_index; |
| 550 |
} |
| 551 |
} |
| 552 |
m_cur_addr = 0; |
| 553 |
break; |
| 554 |
} |
| 555 |
} |
| 556 |
m_cur_bitaddr -= 32; |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
// capture |
| 563 |
void nds_sound_t::capture_t::reset() |
| 564 |
{ |
| 565 |
m_control = 0; |
| 566 |
m_dstaddr = 0; |
| 567 |
m_length = 0; |
| 568 |
|
| 569 |
m_counter = 0x10000; |
| 570 |
m_cur_addr = 0; |
| 571 |
m_cur_waddr = 0; |
| 572 |
m_cur_bitaddr = 0; |
| 573 |
m_enable = false; |
| 574 |
} |
| 575 |
|
| 576 |
void nds_sound_t::capture_t::control_w(u8 data) |
| 577 |
{ |
| 578 |
const u8 old = m_control; |
| 579 |
m_control = data; |
| 580 |
if (bitfield(old ^ m_control, 7)) |
| 581 |
{ |
| 582 |
if (busy()) |
| 583 |
capture_on(); |
| 584 |
else if (!busy()) |
| 585 |
capture_off(); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
void nds_sound_t::capture_t::addrlen_w(u32 offset, u32 data, u32 mask) |
| 590 |
{ |
| 591 |
switch (offset & 1) |
| 592 |
{ |
| 593 |
case 0: // Destination Address |
| 594 |
mask &= 0x7ffffff; |
| 595 |
m_dstaddr = (m_dstaddr & ~mask) | (data & mask); |
| 596 |
break; |
| 597 |
case 1: // Buffer Length |
| 598 |
mask &= 0xffff; |
| 599 |
m_length = (m_length & ~mask) | (data & mask); |
| 600 |
break; |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
void nds_sound_t::capture_t::update(s32 mix, s32 cycle) |
| 605 |
{ |
| 606 |
if (m_enable) |
| 607 |
{ |
| 608 |
s32 inval = 0; |
| 609 |
// get inputs |
| 610 |
// TODO: hardware bugs aren't emulated, add mode behavior not verified |
| 611 |
if (addmode()) |
| 612 |
inval = get_source() ? m_input.output() + m_output.output() : mix; |
| 613 |
else |
| 614 |
inval = get_source() ? m_input.output() : mix; |
| 615 |
|
| 616 |
// clip output |
| 617 |
inval = clamp<s32>(inval, -0x8000, 0x7fff); |
| 618 |
|
| 619 |
// update counter |
| 620 |
m_counter -= cycle; |
| 621 |
while (m_counter <= m_output.freq()) |
| 622 |
{ |
| 623 |
// write to memory; TODO: verify write behavior |
| 624 |
if (format()) // 8 bit output |
| 625 |
{ |
| 626 |
m_fifo[m_fifo_head & 7].write_byte(m_cur_bitaddr & 0x18, (inval >> 8) & 0xff); |
| 627 |
m_cur_bitaddr += 8; |
| 628 |
} |
| 629 |
else |
| 630 |
{ |
| 631 |
m_fifo[m_fifo_head & 7].write_word(m_cur_bitaddr & 0x10, inval & 0xffff); |
| 632 |
m_cur_bitaddr += 16; |
| 633 |
} |
| 634 |
|
| 635 |
// update address |
| 636 |
while (m_cur_bitaddr >= 32) |
| 637 |
{ |
| 638 |
// clear FIFO empty flag |
| 639 |
m_fifo_empty = false; |
| 640 |
|
| 641 |
// advance FIFO head position |
| 642 |
m_fifo_head = (m_fifo_head + 1) & 7; |
| 643 |
if ((m_fifo_head & fifo_mask()) == (m_fifo_tail & fifo_mask())) |
| 644 |
m_fifo_full = true; |
| 645 |
|
| 646 |
// update loop |
| 647 |
if (++m_cur_addr >= m_length) |
| 648 |
{ |
| 649 |
if (repeat()) |
| 650 |
m_cur_addr = 0; |
| 651 |
else |
| 652 |
capture_off(); |
| 653 |
} |
| 654 |
|
| 655 |
if (m_fifo_full) |
| 656 |
{ |
| 657 |
// execute FIFO |
| 658 |
fifo_write(); |
| 659 |
|
| 660 |
// check repeat |
| 661 |
if (m_cur_waddr >= m_length && repeat()) |
| 662 |
m_cur_waddr = 0; |
| 663 |
} |
| 664 |
|
| 665 |
m_cur_bitaddr -= 32; |
| 666 |
} |
| 667 |
m_counter += 0x10000 - m_output.freq(); |
| 668 |
} |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
bool nds_sound_t::capture_t::fifo_write() |
| 673 |
{ |
| 674 |
if (m_fifo_empty) |
| 675 |
return true; |
| 676 |
|
| 677 |
// clear FIFO full flag |
| 678 |
m_fifo_full = false; |
| 679 |
|
| 680 |
// write FIFO data to memory |
| 681 |
m_host.m_intf.write_dword(waddr(), m_fifo[m_fifo_tail].data()); |
| 682 |
m_cur_waddr++; |
| 683 |
|
| 684 |
// advance FIFO tail position |
| 685 |
m_fifo_tail = (m_fifo_tail + 1) & 7; |
| 686 |
if ((m_fifo_head & fifo_mask()) == (m_fifo_tail & fifo_mask())) |
| 687 |
m_fifo_empty = true; |
| 688 |
|
| 689 |
return m_fifo_empty; |
| 690 |
} |
| 691 |
|
| 692 |
void nds_sound_t::capture_t::capture_on() |
| 693 |
{ |
| 694 |
if (!m_enable) |
| 695 |
{ |
| 696 |
m_enable = true; |
| 697 |
|
| 698 |
// reset address |
| 699 |
m_cur_bitaddr = 0; |
| 700 |
m_cur_addr = m_cur_waddr = 0; |
| 701 |
m_counter = 0x10000; |
| 702 |
|
| 703 |
// reset FIFO |
| 704 |
m_fifo_head = m_fifo_tail = 0; |
| 705 |
m_fifo_empty = true; |
| 706 |
m_fifo_full = false; |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
void nds_sound_t::capture_t::capture_off() |
| 711 |
{ |
| 712 |
if (m_enable) |
| 713 |
{ |
| 714 |
// flush FIFO |
| 715 |
while (m_cur_waddr < m_length) |
| 716 |
{ |
| 717 |
if (fifo_write()) |
| 718 |
break; |
| 719 |
} |
| 720 |
|
| 721 |
m_enable = false; |
| 722 |
if (busy()) |
| 723 |
m_control &= ~(1 << 7); |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
}; // namespace nds_sound_emu |