| 1 |
/* |
| 2 |
License: Zlib |
| 3 |
see https://gitlab.com/cam900/vgsound_emu/-/blob/main/LICENSE for more details |
| 4 |
|
| 5 |
Copyright holder(s): cam900 |
| 6 |
Namco 163 Sound emulation core |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "n163.hpp" |
| 10 |
|
| 11 |
void n163_core::tick() |
| 12 |
{ |
| 13 |
if (m_multiplex) |
| 14 |
{ |
| 15 |
m_out = 0; |
| 16 |
} |
| 17 |
// 0xe000-0xe7ff Disable sound bits (bit 6, bit 0 to 5 are CPU ROM Bank |
| 18 |
// 0x8000-0x9fff select.) |
| 19 |
if (m_disable) |
| 20 |
{ |
| 21 |
if (!m_multiplex) |
| 22 |
{ |
| 23 |
m_out = 0; |
| 24 |
} |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
// tick per each clock |
| 29 |
const u32 freq = m_ram[m_voice_cycle + 0] | (u32(m_ram[m_voice_cycle + 2]) << 8) | |
| 30 |
(bitfield<u32>(m_ram[m_voice_cycle + 4], 0, 2) << 16); // 18 bit frequency |
| 31 |
|
| 32 |
u32 accum = m_ram[m_voice_cycle + 1] | (u32(m_ram[m_voice_cycle + 3]) << 8) | |
| 33 |
(u32(m_ram[m_voice_cycle + 5]) << 16); // 24 bit accumulator |
| 34 |
|
| 35 |
const u16 length = 256 - (m_ram[m_voice_cycle + 4] & 0xfc); |
| 36 |
const u8 addr = m_ram[m_voice_cycle + 6] + bitfield(accum, 16, 8); |
| 37 |
const s16 wave = (bitfield(m_ram[bitfield(addr, 1, 7)], bitfield(addr, 0) << 2, 4) - 8); |
| 38 |
const s16 volume = bitfield(m_ram[m_voice_cycle + 7], 0, 4); |
| 39 |
|
| 40 |
// get per-voice output |
| 41 |
const s16 voice_out = (wave * volume); |
| 42 |
m_voice_out[(m_voice_cycle >> 3) & 7] = voice_out; |
| 43 |
|
| 44 |
// accumulate address |
| 45 |
accum = bitfield(accum + freq, 0, 24); |
| 46 |
if (bitfield(accum, 16, 8) >= length) |
| 47 |
{ |
| 48 |
accum = bitfield(accum, 0, 18); |
| 49 |
} |
| 50 |
|
| 51 |
// writeback to register |
| 52 |
m_ram[m_voice_cycle + 1] = bitfield(accum, 0, 8); |
| 53 |
m_ram[m_voice_cycle + 3] = bitfield(accum, 8, 8); |
| 54 |
m_ram[m_voice_cycle + 5] = bitfield(accum, 16, 8); |
| 55 |
|
| 56 |
// update voice cycle |
| 57 |
bool flush = m_multiplex ? true : false; |
| 58 |
m_voice_cycle -= 0x8; |
| 59 |
if (m_voice_cycle < (0x78 - (bitfield(m_ram[0x7f], 4, 3) << 3))) |
| 60 |
{ |
| 61 |
if (!m_multiplex) |
| 62 |
{ |
| 63 |
flush = true; |
| 64 |
} |
| 65 |
m_voice_cycle = 0x78; |
| 66 |
} |
| 67 |
|
| 68 |
// output 4 bit waveform and volume, multiplexed |
| 69 |
m_acc += voice_out; |
| 70 |
if (flush) |
| 71 |
{ |
| 72 |
m_out = m_acc / (m_multiplex ? 1 : (bitfield(m_ram[0x7f], 4, 3) + 1)); |
| 73 |
m_acc = 0; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
void n163_core::reset() |
| 78 |
{ |
| 79 |
// reset this chip |
| 80 |
m_disable = false; |
| 81 |
m_multiplex = true; |
| 82 |
std::fill(m_ram.begin(), m_ram.end(), 0); |
| 83 |
m_voice_cycle = 0x78; |
| 84 |
m_addr_latch.reset(); |
| 85 |
m_out = 0; |
| 86 |
m_acc = 0; |
| 87 |
} |
| 88 |
|
| 89 |
// accessor |
| 90 |
void n163_core::addr_w(u8 data) |
| 91 |
{ |
| 92 |
// 0xf800-0xffff Sound address, increment |
| 93 |
m_addr_latch.write(data); |
| 94 |
} |
| 95 |
|
| 96 |
void n163_core::data_w(u8 data, bool cpu_access) |
| 97 |
{ |
| 98 |
// 0x4800-0x4fff Sound data write |
| 99 |
m_ram[m_addr_latch.addr()] = data; |
| 100 |
|
| 101 |
// address latch increment |
| 102 |
if (cpu_access && m_addr_latch.incr()) |
| 103 |
{ |
| 104 |
m_addr_latch.addr_inc(); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
u8 n163_core::data_r(bool cpu_access) |
| 109 |
{ |
| 110 |
// 0x4800-0x4fff Sound data read |
| 111 |
const u8 ret = m_ram[m_addr_latch.addr()]; |
| 112 |
|
| 113 |
// address latch increment |
| 114 |
if (cpu_access && m_addr_latch.incr()) |
| 115 |
{ |
| 116 |
m_addr_latch.addr_inc(); |
| 117 |
} |
| 118 |
|
| 119 |
return ret; |
| 120 |
} |