| 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 |
Konami VRC VI sound emulation core |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "vrcvi.hpp" |
| 10 |
|
| 11 |
int vrcvi_core::predict() { |
| 12 |
const int p0=m_pulse[0].predict(); |
| 13 |
const int p1=m_pulse[1].predict(); |
| 14 |
const int s=m_sawtooth.predict(); |
| 15 |
|
| 16 |
int ret=p0; |
| 17 |
if (p1<ret) ret=p1; |
| 18 |
if (s<ret) ret=s; |
| 19 |
if (ret<1) ret=1; |
| 20 |
return ret; |
| 21 |
} |
| 22 |
|
| 23 |
void vrcvi_core::tick(int cycles) |
| 24 |
{ |
| 25 |
m_out = 0; |
| 26 |
if (!m_control.m_halt) // Halt flag |
| 27 |
{ |
| 28 |
// tick per each clock |
| 29 |
m_out += m_pulse[0].get_output(cycles); // add 4 bit pulse output |
| 30 |
m_out += m_pulse[1].get_output(cycles); |
| 31 |
m_out += m_sawtooth.get_output(cycles); // add 5 bit sawtooth output |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
void vrcvi_core::reset() |
| 36 |
{ |
| 37 |
m_pulse[0].reset(); |
| 38 |
m_pulse[1].reset(); |
| 39 |
m_sawtooth.reset(); |
| 40 |
m_timer.reset(); |
| 41 |
m_control.reset(); |
| 42 |
m_out = 0; |
| 43 |
} |
| 44 |
|
| 45 |
int vrcvi_core::alu_t::predict() { |
| 46 |
if (!m_divider.m_enable) return 65535; |
| 47 |
if (m_host.m_control.m_shift&2) { |
| 48 |
return bitfield(m_counter, 8, 4); |
| 49 |
} else if (m_host.m_control.m_shift&1) { |
| 50 |
return bitfield(m_counter, 4, 8); |
| 51 |
} |
| 52 |
|
| 53 |
return m_counter&0xfff; |
| 54 |
} |
| 55 |
|
| 56 |
bool vrcvi_core::alu_t::tick(int cycles) |
| 57 |
{ |
| 58 |
if (m_divider.m_enable) |
| 59 |
{ |
| 60 |
const u16 temp = m_counter; |
| 61 |
// post decrement |
| 62 |
if (m_host.m_control.m_shift&2) |
| 63 |
{ |
| 64 |
m_counter = (m_counter & 0x0ff) | (bitfield(bitfield(m_counter, 8, 4) - cycles, 0, 4) << 8); |
| 65 |
m_counter = (m_counter & 0xf00) | (bitfield(bitfield(m_counter, 0, 8) - cycles, 0, 8) << 0); |
| 66 |
|
| 67 |
if (bitfield(temp, 8, 4) == 0) |
| 68 |
{ |
| 69 |
m_counter = m_divider.m_divider; |
| 70 |
return true; |
| 71 |
} |
| 72 |
} |
| 73 |
else if (m_host.m_control.m_shift&1) |
| 74 |
{ |
| 75 |
m_counter = (m_counter & 0x00f) | (bitfield(bitfield(m_counter, 4, 8) - cycles, 0, 8) << 4); |
| 76 |
m_counter = (m_counter & 0xff0) | (bitfield(bitfield(m_counter, 0, 4) - cycles, 0, 4) << 0); |
| 77 |
|
| 78 |
if (bitfield(temp, 4, 8) == 0) |
| 79 |
{ |
| 80 |
m_counter = m_divider.m_divider; |
| 81 |
return true; |
| 82 |
} |
| 83 |
} |
| 84 |
else |
| 85 |
{ |
| 86 |
m_counter = (m_counter-cycles)&0xfff; //bitfield(bitfield(m_counter, 0, 12) - 1, 0, 12); |
| 87 |
if (!(temp&0xfff)) { |
| 88 |
m_counter = m_divider.m_divider; |
| 89 |
return true; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return false; |
| 94 |
} |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
bool vrcvi_core::pulse_t::tick(int cycles) |
| 99 |
{ |
| 100 |
if (!m_divider.m_enable) |
| 101 |
{ |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
if (vrcvi_core::alu_t::tick(cycles)) |
| 106 |
{ |
| 107 |
m_cycle = (m_cycle+1)&15; |
| 108 |
} |
| 109 |
|
| 110 |
return m_control.m_mode ? true : ((m_cycle > m_control.m_duty) ? true : false); |
| 111 |
} |
| 112 |
|
| 113 |
bool vrcvi_core::sawtooth_t::tick(int cycles) |
| 114 |
{ |
| 115 |
if (!m_divider.m_enable) |
| 116 |
{ |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
if (vrcvi_core::alu_t::tick(cycles)) |
| 121 |
{ |
| 122 |
if ((m_cycle++)&1) |
| 123 |
{ // Even step only |
| 124 |
m_accum += m_rate; |
| 125 |
} |
| 126 |
if (m_cycle >= 14) // Reset accumulator at every 14 cycles |
| 127 |
{ |
| 128 |
m_accum = 0; |
| 129 |
m_cycle = 0; |
| 130 |
} |
| 131 |
} |
| 132 |
return (m_accum != 0); |
| 133 |
} |
| 134 |
|
| 135 |
s8 vrcvi_core::pulse_t::get_output(int cycles) |
| 136 |
{ |
| 137 |
// add 4 bit pulse output |
| 138 |
m_out = tick(cycles) ? m_control.m_volume : 0; |
| 139 |
return m_out; |
| 140 |
} |
| 141 |
|
| 142 |
s8 vrcvi_core::sawtooth_t::get_output(int cycles) |
| 143 |
{ |
| 144 |
// add 5 bit sawtooth output |
| 145 |
m_out = tick(cycles) ? ((m_accum>>3)&31) : 0; |
| 146 |
return m_out; |
| 147 |
} |
| 148 |
|
| 149 |
void vrcvi_core::alu_t::reset() |
| 150 |
{ |
| 151 |
m_divider.reset(); |
| 152 |
m_divider.m_divider = 1023; |
| 153 |
m_counter = 0; |
| 154 |
m_cycle = 0; |
| 155 |
m_out = 0; |
| 156 |
} |
| 157 |
|
| 158 |
void vrcvi_core::pulse_t::reset() |
| 159 |
{ |
| 160 |
vrcvi_core::alu_t::reset(); |
| 161 |
m_control.reset(); |
| 162 |
m_divider.m_divider = 1023; |
| 163 |
} |
| 164 |
|
| 165 |
void vrcvi_core::sawtooth_t::reset() |
| 166 |
{ |
| 167 |
vrcvi_core::alu_t::reset(); |
| 168 |
m_rate = 0; |
| 169 |
m_accum = 0; |
| 170 |
m_divider.m_divider = 1023; |
| 171 |
} |
| 172 |
|
| 173 |
bool vrcvi_core::timer_t::tick() |
| 174 |
{ |
| 175 |
if (m_timer_control.enable()) |
| 176 |
{ |
| 177 |
if (!m_timer_control.sync()) // scanline sync mode |
| 178 |
{ |
| 179 |
m_prescaler -= 3; |
| 180 |
if (m_prescaler <= 0) |
| 181 |
{ |
| 182 |
m_prescaler += 341; |
| 183 |
return true; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
return (m_timer_control.enable() && m_timer_control.sync()) ? true : false; |
| 188 |
} |
| 189 |
|
| 190 |
void vrcvi_core::timer_t::counter_tick() |
| 191 |
{ |
| 192 |
if (bitfield(++m_counter, 0, 8) == 0) |
| 193 |
{ |
| 194 |
m_counter = m_counter_latch; |
| 195 |
irq_set(); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
void vrcvi_core::timer_t::reset() |
| 200 |
{ |
| 201 |
m_timer_control.reset(); |
| 202 |
m_prescaler = 341; |
| 203 |
m_counter = m_counter_latch = 0; |
| 204 |
irq_clear(); |
| 205 |
} |
| 206 |
|
| 207 |
// Accessors |
| 208 |
|
| 209 |
void vrcvi_core::alu_t::divider_t::write(bool msb, u8 data) |
| 210 |
{ |
| 211 |
if (msb) |
| 212 |
{ |
| 213 |
m_divider = (m_divider & ~0xf00) | (bitfield<u32>(data, 0, 4) << 8); |
| 214 |
m_enable = bitfield(data, 7); |
| 215 |
} |
| 216 |
else |
| 217 |
{ |
| 218 |
m_divider = (m_divider & ~0x0ff) | data; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
void vrcvi_core::pulse_w(u8 voice, u8 address, u8 data) |
| 223 |
{ |
| 224 |
pulse_t &v = m_pulse[voice]; |
| 225 |
switch (address) |
| 226 |
{ |
| 227 |
case 0x00: // Control - 0x9000 (Pulse 1), 0xa000 (Pulse 2) |
| 228 |
v.control().write(data); |
| 229 |
break; |
| 230 |
case 0x01: // Pitch LSB - 0x9001/0x9002 (Pulse 1), 0xa001/0xa002 (Pulse 2) |
| 231 |
v.divider().write(false, data); |
| 232 |
break; |
| 233 |
case 0x02: // Pitch MSB, Enable/Disable - 0x9002/0x9001 (Pulse 1), 0xa002/0xa001 (Pulse 2) |
| 234 |
v.divider().write(true, data); |
| 235 |
if (!v.divider().m_enable) |
| 236 |
{ // Reset duty cycle |
| 237 |
v.clear_cycle(); |
| 238 |
} |
| 239 |
break; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
void vrcvi_core::saw_w(u8 address, u8 data) |
| 244 |
{ |
| 245 |
switch (address) |
| 246 |
{ |
| 247 |
case 0x00: // Sawtooth Accumulate - 0xb000 |
| 248 |
m_sawtooth.set_rate(bitfield(data, 0, 6)); |
| 249 |
break; |
| 250 |
case 0x01: // Pitch LSB - 0xb001/0xb002 (Sawtooth) |
| 251 |
m_sawtooth.divider().write(false, data); |
| 252 |
break; |
| 253 |
case 0x02: // Pitch MSB, Enable/Disable - 0xb002/0xb001 (Sawtooth) |
| 254 |
m_sawtooth.divider().write(true, data); |
| 255 |
if (!m_sawtooth.divider().m_enable) |
| 256 |
{ // Reset accumulator |
| 257 |
m_sawtooth.clear_accum(); |
| 258 |
} |
| 259 |
break; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
void vrcvi_core::timer_w(u8 address, u8 data) |
| 264 |
{ |
| 265 |
switch (address) |
| 266 |
{ |
| 267 |
case 0x00: // Timer latch - 0xf000 |
| 268 |
m_timer.set_counter_latch(data); |
| 269 |
break; |
| 270 |
case 0x01: // Timer control - 0xf001/0xf002 |
| 271 |
m_timer.timer_control_w(data); |
| 272 |
break; |
| 273 |
case 0x02: // IRQ Acknowledge - 0xf002/0xf001 |
| 274 |
m_timer.irq_ack(); |
| 275 |
break; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
void vrcvi_core::control_w(u8 data) |
| 280 |
{ |
| 281 |
// Global control - 0x9003 |
| 282 |
m_control.write(data); |
| 283 |
} |