| 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 |
OKI MSM6295 emulation core |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "msm6295.hpp" |
| 10 |
|
| 11 |
void msm6295_core::tick() |
| 12 |
{ |
| 13 |
if (m_counter < 4) |
| 14 |
{ |
| 15 |
m_voice[m_counter].tick(); |
| 16 |
m_out_temp += m_voice[m_counter].out(); |
| 17 |
} |
| 18 |
if ((++m_counter) >= (m_ss ? 5 : 4)) |
| 19 |
{ |
| 20 |
// command handler |
| 21 |
if (m_command_pending) |
| 22 |
{ |
| 23 |
if (bitfield(m_command, 7)) // play voice |
| 24 |
{ |
| 25 |
if ((++m_clock) >= 15) |
| 26 |
{ |
| 27 |
if (bitfield(m_next_command, 4, 4) != 0) |
| 28 |
{ |
| 29 |
for (int i = 0; i < 4; i++) |
| 30 |
{ |
| 31 |
if (bitfield(m_next_command, 4 + i)) |
| 32 |
{ |
| 33 |
if (!m_voice[i].busy()) |
| 34 |
{ |
| 35 |
m_voice[i].set_command(m_command); |
| 36 |
m_voice[i].set_volume(bitfield(m_next_command, 0, 4)); |
| 37 |
} |
| 38 |
// voices aren't be playable simultaneously at once |
| 39 |
break; |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
m_command = 0; |
| 44 |
m_command_pending = false; |
| 45 |
m_clock = 0; |
| 46 |
} |
| 47 |
} |
| 48 |
else if (bitfield(m_next_command, 7)) // select phrase |
| 49 |
{ |
| 50 |
if ((++m_clock) >= 15) |
| 51 |
{ |
| 52 |
m_command = m_next_command; |
| 53 |
m_command_pending = false; |
| 54 |
m_clock = 0; |
| 55 |
} |
| 56 |
} |
| 57 |
else |
| 58 |
{ |
| 59 |
if (bitfield(m_next_command, 3, 4) != 0) // suspend voices |
| 60 |
{ |
| 61 |
for (int i = 0; i < 4; i++) |
| 62 |
{ |
| 63 |
if (bitfield(m_next_command, 3 + i)) |
| 64 |
{ |
| 65 |
if (m_voice[i].busy()) |
| 66 |
{ |
| 67 |
m_voice[i].set_command(m_next_command); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
m_next_command &= ~0x78; |
| 72 |
} |
| 73 |
m_command_pending = false; |
| 74 |
} |
| 75 |
} |
| 76 |
m_out = m_out_temp; |
| 77 |
m_out_temp = 0; |
| 78 |
m_counter = 0; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
void msm6295_core::reset() |
| 83 |
{ |
| 84 |
for (auto &elem : m_voice) |
| 85 |
{ |
| 86 |
elem.reset(); |
| 87 |
} |
| 88 |
|
| 89 |
m_command = 0; |
| 90 |
m_next_command = 0; |
| 91 |
m_command_pending = false; |
| 92 |
m_clock = 0; |
| 93 |
m_counter = 0; |
| 94 |
m_out = 0; |
| 95 |
m_out_temp = 0; |
| 96 |
} |
| 97 |
|
| 98 |
void msm6295_core::voice_t::tick() |
| 99 |
{ |
| 100 |
if (!m_busy) |
| 101 |
{ |
| 102 |
if (bitfield(m_command, 7)) |
| 103 |
{ |
| 104 |
// get phrase header (stored in data memory) |
| 105 |
const u32 phrase = bitfield(m_command, 0, 7) << 3; |
| 106 |
// Start address |
| 107 |
m_addr = (bitfield(m_host.m_intf.read_byte(phrase | 0), 0, 2) << 16) | |
| 108 |
(m_host.m_intf.read_byte(phrase | 1) << 8) | |
| 109 |
(m_host.m_intf.read_byte(phrase | 2) << 0); |
| 110 |
// End address |
| 111 |
m_end = (bitfield(m_host.m_intf.read_byte(phrase | 3), 0, 2) << 16) | |
| 112 |
(m_host.m_intf.read_byte(phrase | 4) << 8) | |
| 113 |
(m_host.m_intf.read_byte(phrase | 5) << 0); |
| 114 |
m_nibble = 4; // MSB first, LSB second |
| 115 |
m_command = 0; |
| 116 |
m_busy = true; |
| 117 |
vox_decoder_t::reset(); |
| 118 |
} |
| 119 |
m_out = 0; |
| 120 |
} |
| 121 |
else |
| 122 |
{ |
| 123 |
// playback |
| 124 |
if ((++m_clock) >= 33) |
| 125 |
{ |
| 126 |
bool is_end = (m_command != 0); // suspend |
| 127 |
decode(bitfield(m_host.m_intf.read_byte(m_addr), m_nibble, 4)); |
| 128 |
if (m_nibble <= 0) |
| 129 |
{ |
| 130 |
m_nibble = 4; |
| 131 |
if (++m_addr > m_end) |
| 132 |
{ |
| 133 |
is_end = true; |
| 134 |
} |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
m_nibble -= 4; |
| 139 |
} |
| 140 |
if (is_end) |
| 141 |
{ |
| 142 |
m_command = 0; |
| 143 |
m_busy = false; |
| 144 |
} |
| 145 |
m_out = (step() * m_volume) >> 7; // scale out to 12 bit output |
| 146 |
m_clock = 0; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
void msm6295_core::voice_t::reset() |
| 152 |
{ |
| 153 |
vox_decoder_t::reset(); |
| 154 |
m_clock = 0; |
| 155 |
m_busy = false; |
| 156 |
m_command = 0; |
| 157 |
m_addr = 0; |
| 158 |
m_nibble = 0; |
| 159 |
m_end = 0; |
| 160 |
m_volume = 0; |
| 161 |
m_out = 0; |
| 162 |
m_mute = false; |
| 163 |
} |
| 164 |
|
| 165 |
// accessors |
| 166 |
u8 msm6295_core::busy_r() |
| 167 |
{ |
| 168 |
return (m_voice[0].busy() ? 0x01 : 0x00) | (m_voice[1].busy() ? 0x02 : 0x00) | |
| 169 |
(m_voice[2].busy() ? 0x04 : 0x00) | (m_voice[3].busy() ? 0x08 : 0x00); |
| 170 |
} |
| 171 |
|
| 172 |
void msm6295_core::command_w(u8 data) |
| 173 |
{ |
| 174 |
if (!m_command_pending) |
| 175 |
{ |
| 176 |
m_next_command = data; |
| 177 |
m_command_pending = true; |
| 178 |
} |
| 179 |
} |