| 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 |
#ifndef _VGSOUND_EMU_SRC_VRCVI_HPP |
| 10 |
#define _VGSOUND_EMU_SRC_VRCVI_HPP |
| 11 |
|
| 12 |
#pragma once |
| 13 |
|
| 14 |
#include "../core/core.hpp" |
| 15 |
|
| 16 |
using namespace vgsound_emu; |
| 17 |
|
| 18 |
class vrcvi_intf : public vgsound_emu_core |
| 19 |
{ |
| 20 |
public: |
| 21 |
vrcvi_intf() |
| 22 |
: vgsound_emu_core("vrc_vi_intf") |
| 23 |
{ |
| 24 |
} |
| 25 |
|
| 26 |
virtual void irq_w(bool irq) {} |
| 27 |
}; |
| 28 |
|
| 29 |
class vrcvi_core : public vgsound_emu_core |
| 30 |
{ |
| 31 |
friend class vrcvi_intf; |
| 32 |
|
| 33 |
private: |
| 34 |
// Common ALU for sound channels |
| 35 |
class alu_t : public vgsound_emu_core |
| 36 |
{ |
| 37 |
private: |
| 38 |
class divider_t : public vgsound_emu_core |
| 39 |
{ |
| 40 |
public: |
| 41 |
divider_t() |
| 42 |
: vgsound_emu_core("vrc_vi_frequency_divider") |
| 43 |
, m_divider(0) |
| 44 |
, m_enable(0) |
| 45 |
{ |
| 46 |
} |
| 47 |
|
| 48 |
void reset() |
| 49 |
{ |
| 50 |
m_divider = 1023; |
| 51 |
m_enable = 0; |
| 52 |
} |
| 53 |
|
| 54 |
void write(bool msb, u8 data); |
| 55 |
|
| 56 |
// getters |
| 57 |
inline u16 divider() { return m_divider; } |
| 58 |
|
| 59 |
inline bool enable() { return m_enable; } |
| 60 |
|
| 61 |
public: |
| 62 |
u16 m_divider : 12; // divider (pitch) |
| 63 |
u16 m_enable : 1; // channel disable flag |
| 64 |
}; |
| 65 |
|
| 66 |
public: |
| 67 |
alu_t(std::string tag, vrcvi_core &host) |
| 68 |
: vgsound_emu_core(tag) |
| 69 |
, m_host(host) |
| 70 |
, m_divider(divider_t()) |
| 71 |
, m_counter(0) |
| 72 |
, m_cycle(0) |
| 73 |
, m_out(0) |
| 74 |
{ |
| 75 |
} |
| 76 |
|
| 77 |
virtual void reset(); |
| 78 |
virtual bool tick(int cycles); |
| 79 |
virtual int predict(); |
| 80 |
|
| 81 |
virtual s8 get_output(int cycles) |
| 82 |
{ |
| 83 |
m_out = 0; |
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
// accessors |
| 88 |
inline void clear_cycle() { m_cycle = 0; } |
| 89 |
|
| 90 |
// getters |
| 91 |
divider_t ÷r() { return m_divider; } |
| 92 |
|
| 93 |
inline u16 counter() { return m_counter; } |
| 94 |
|
| 95 |
inline u8 cycle() { return m_cycle; } |
| 96 |
|
| 97 |
// for previwe/debug only |
| 98 |
inline s8 out() { return m_out; } |
| 99 |
|
| 100 |
protected: |
| 101 |
vrcvi_core &m_host; |
| 102 |
divider_t m_divider; |
| 103 |
u16 m_counter = 0; // clock counter |
| 104 |
u8 m_cycle = 0; // clock cycle |
| 105 |
s8 m_out = 0; // output per channel |
| 106 |
}; |
| 107 |
|
| 108 |
// 2 Pulse channels |
| 109 |
class pulse_t : public alu_t |
| 110 |
{ |
| 111 |
private: |
| 112 |
// Control bits |
| 113 |
class pulse_control_t |
| 114 |
{ |
| 115 |
public: |
| 116 |
pulse_control_t() |
| 117 |
: m_mode(0) |
| 118 |
, m_duty(0) |
| 119 |
, m_volume(0) |
| 120 |
{ |
| 121 |
} |
| 122 |
|
| 123 |
void reset() |
| 124 |
{ |
| 125 |
m_mode = 0; |
| 126 |
m_duty = 0; |
| 127 |
m_volume = 0; |
| 128 |
} |
| 129 |
|
| 130 |
// accessors |
| 131 |
inline void write(u8 data) |
| 132 |
{ |
| 133 |
m_mode = (data >> 7) & 0x1; |
| 134 |
m_duty = (data >> 4) & 0x7; |
| 135 |
m_volume = (data >> 0) & 0xf; |
| 136 |
} |
| 137 |
|
| 138 |
// getters |
| 139 |
inline bool mode() { return m_mode; } |
| 140 |
|
| 141 |
inline u8 duty() { return m_duty; } |
| 142 |
|
| 143 |
inline u8 volume() { return m_volume; } |
| 144 |
|
| 145 |
public: |
| 146 |
u8 m_mode : 1; // duty toggle flag |
| 147 |
u8 m_duty : 3; // 3 bit duty cycle |
| 148 |
u8 m_volume : 4; // 4 bit volume |
| 149 |
}; |
| 150 |
|
| 151 |
public: |
| 152 |
pulse_t(vrcvi_core &host) |
| 153 |
: alu_t("vrc_vi_pulse", host) |
| 154 |
, m_control(pulse_control_t()) |
| 155 |
{ |
| 156 |
} |
| 157 |
|
| 158 |
virtual void reset() override; |
| 159 |
virtual bool tick(int cycles) override; |
| 160 |
virtual s8 get_output(int cycles) override; |
| 161 |
|
| 162 |
// getters |
| 163 |
pulse_control_t &control() { return m_control; } |
| 164 |
|
| 165 |
private: |
| 166 |
pulse_control_t m_control; |
| 167 |
}; |
| 168 |
|
| 169 |
// 1 Sawtooth channel |
| 170 |
class sawtooth_t : public alu_t |
| 171 |
{ |
| 172 |
public: |
| 173 |
sawtooth_t(vrcvi_core &host) |
| 174 |
: alu_t("vrc_vi_sawtooth", host) |
| 175 |
, m_rate(0) |
| 176 |
, m_accum(0) |
| 177 |
{ |
| 178 |
} |
| 179 |
|
| 180 |
virtual void reset() override; |
| 181 |
virtual bool tick(int cycles) override; |
| 182 |
virtual s8 get_output(int cycles) override; |
| 183 |
|
| 184 |
// accessors |
| 185 |
inline void clear_accum() { m_accum = 0; } |
| 186 |
|
| 187 |
// setters |
| 188 |
inline void set_rate(u8 rate) { m_rate = rate; } |
| 189 |
|
| 190 |
// getters |
| 191 |
inline u8 rate() { return m_rate; } |
| 192 |
|
| 193 |
inline u8 accum() { return m_accum; } |
| 194 |
|
| 195 |
private: |
| 196 |
u8 m_rate = 0; // sawtooth accumulate rate |
| 197 |
u8 m_accum = 0; // sawtooth accumulator, high 5 bit is accumulated to output |
| 198 |
}; |
| 199 |
|
| 200 |
// Internal timer |
| 201 |
class timer_t : public vgsound_emu_core |
| 202 |
{ |
| 203 |
private: |
| 204 |
// Control bits |
| 205 |
class timer_control_t : public vgsound_emu_core |
| 206 |
{ |
| 207 |
public: |
| 208 |
timer_control_t() |
| 209 |
: vgsound_emu_core("vrc_vi_timer_control") |
| 210 |
, m_irq_trigger(0) |
| 211 |
, m_enable_ack(0) |
| 212 |
, m_enable(0) |
| 213 |
, m_sync(0) |
| 214 |
{ |
| 215 |
} |
| 216 |
|
| 217 |
void reset() |
| 218 |
{ |
| 219 |
m_irq_trigger = 0; |
| 220 |
m_enable_ack = 0; |
| 221 |
m_enable = 0; |
| 222 |
m_sync = 0; |
| 223 |
} |
| 224 |
|
| 225 |
// accessors |
| 226 |
inline void irq_set(bool irq) { m_irq_trigger = irq ? 1 : 0; } |
| 227 |
|
| 228 |
// setters |
| 229 |
inline void set_enable_ack(bool enable_ack) |
| 230 |
{ |
| 231 |
m_enable_ack = enable_ack ? 1 : 0; |
| 232 |
} |
| 233 |
|
| 234 |
inline void set_enable(bool enable) { m_enable = enable ? 1 : 0; } |
| 235 |
|
| 236 |
inline void set_sync(bool sync) { m_sync = sync ? 1 : 0; } |
| 237 |
|
| 238 |
// getters |
| 239 |
inline bool irq_trigger() { return m_irq_trigger; } |
| 240 |
|
| 241 |
inline bool enable_ack() { return m_enable_ack; } |
| 242 |
|
| 243 |
inline bool enable() { return m_enable; } |
| 244 |
|
| 245 |
inline bool sync() { return m_sync; } |
| 246 |
|
| 247 |
private: |
| 248 |
u8 m_irq_trigger : 1; |
| 249 |
u8 m_enable_ack : 1; |
| 250 |
u8 m_enable : 1; |
| 251 |
u8 m_sync : 1; |
| 252 |
}; |
| 253 |
|
| 254 |
public: |
| 255 |
timer_t(vrcvi_core &host) |
| 256 |
: vgsound_emu_core("vrc_vi_timer") |
| 257 |
, m_host(host) |
| 258 |
, m_timer_control(timer_control_t()) |
| 259 |
, m_prescaler(341) |
| 260 |
, m_counter(0) |
| 261 |
, m_counter_latch(0) |
| 262 |
{ |
| 263 |
} |
| 264 |
|
| 265 |
void reset(); |
| 266 |
bool tick(); |
| 267 |
void counter_tick(); |
| 268 |
|
| 269 |
// IRQ update |
| 270 |
void update() { m_host.m_intf.irq_w(m_timer_control.irq_trigger()); } |
| 271 |
|
| 272 |
void irq_set() |
| 273 |
{ |
| 274 |
if (!m_timer_control.irq_trigger()) |
| 275 |
{ |
| 276 |
m_timer_control.irq_set(true); |
| 277 |
update(); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
void irq_clear() |
| 282 |
{ |
| 283 |
if (m_timer_control.irq_trigger()) |
| 284 |
{ |
| 285 |
m_timer_control.irq_set(false); |
| 286 |
update(); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
// accessors |
| 291 |
void reset_counter() |
| 292 |
{ |
| 293 |
m_counter = m_counter_latch; |
| 294 |
m_prescaler = 341; |
| 295 |
} |
| 296 |
|
| 297 |
void timer_control_w(u8 data) |
| 298 |
{ |
| 299 |
m_timer_control.set_enable_ack((data >> 0) & 1); |
| 300 |
m_timer_control.set_enable((data >> 1) & 1); |
| 301 |
m_timer_control.set_sync((data >> 2) & 1); |
| 302 |
if (m_timer_control.enable()) |
| 303 |
{ |
| 304 |
reset_counter(); |
| 305 |
} |
| 306 |
irq_clear(); |
| 307 |
} |
| 308 |
|
| 309 |
void irq_ack() |
| 310 |
{ |
| 311 |
irq_clear(); |
| 312 |
m_timer_control.set_enable(m_timer_control.enable_ack()); |
| 313 |
} |
| 314 |
|
| 315 |
// setters |
| 316 |
inline void set_counter_latch(u8 counter_latch) { m_counter_latch = counter_latch; } |
| 317 |
|
| 318 |
// getters |
| 319 |
timer_control_t &timer_control() { return m_timer_control; } |
| 320 |
|
| 321 |
inline s16 prescaler() { return m_prescaler; } |
| 322 |
|
| 323 |
inline u8 counter() { return m_counter; } |
| 324 |
|
| 325 |
inline u8 counter_latch() { return m_counter_latch; } |
| 326 |
|
| 327 |
private: |
| 328 |
vrcvi_core &m_host; // host core |
| 329 |
timer_control_t m_timer_control; // timer control bits |
| 330 |
s16 m_prescaler = 341; // prescaler |
| 331 |
u8 m_counter = 0; // clock counter |
| 332 |
u8 m_counter_latch = 0; // clock counter latch |
| 333 |
}; |
| 334 |
|
| 335 |
class global_control_t : public vgsound_emu_core |
| 336 |
{ |
| 337 |
public: |
| 338 |
global_control_t() |
| 339 |
: vgsound_emu_core("vrc_vi_global_control") |
| 340 |
, m_halt(0) |
| 341 |
, m_shift(0) |
| 342 |
{ |
| 343 |
} |
| 344 |
|
| 345 |
void reset() |
| 346 |
{ |
| 347 |
m_halt = 0; |
| 348 |
m_shift = 0; |
| 349 |
} |
| 350 |
|
| 351 |
// accessors |
| 352 |
inline void write(u8 data) |
| 353 |
{ |
| 354 |
m_halt = (data >> 0) & 1; |
| 355 |
m_shift = (data >> 1) & 3; |
| 356 |
} |
| 357 |
|
| 358 |
// getters |
| 359 |
inline bool halt() { return m_halt; } |
| 360 |
|
| 361 |
inline u8 shift() { return m_shift; } |
| 362 |
|
| 363 |
public: |
| 364 |
u8 m_halt; // halt sound |
| 365 |
u8 m_shift; // 4/8 bit right shift |
| 366 |
}; |
| 367 |
|
| 368 |
public: |
| 369 |
// constructor |
| 370 |
vrcvi_core(vrcvi_intf &intf) |
| 371 |
: vgsound_emu_core("vrc_vi") |
| 372 |
, m_intf(intf) |
| 373 |
, m_pulse{*this, *this} |
| 374 |
, m_sawtooth(*this) |
| 375 |
, m_timer(*this) |
| 376 |
, m_control(global_control_t()) |
| 377 |
, m_out(0) |
| 378 |
{ |
| 379 |
} |
| 380 |
|
| 381 |
// accessors, getters, setters |
| 382 |
void pulse_w(u8 voice, u8 address, u8 data); |
| 383 |
void saw_w(u8 address, u8 data); |
| 384 |
void timer_w(u8 address, u8 data); |
| 385 |
void control_w(u8 data); |
| 386 |
|
| 387 |
// internal state |
| 388 |
void reset(); |
| 389 |
int predict(); |
| 390 |
void tick(int cycles=1); |
| 391 |
|
| 392 |
// 6 bit output |
| 393 |
inline s8 out() { return m_out; } |
| 394 |
|
| 395 |
// for debug/preview only |
| 396 |
inline s8 pulse_out(u8 pulse) { return (pulse < 2) ? m_pulse[pulse].out() : 0; } |
| 397 |
|
| 398 |
inline s8 sawtooth_out() { return m_sawtooth.out(); } |
| 399 |
|
| 400 |
private: |
| 401 |
vrcvi_intf &m_intf; |
| 402 |
|
| 403 |
pulse_t m_pulse[2]; // 2 pulse channels |
| 404 |
sawtooth_t m_sawtooth; // sawtooth channel |
| 405 |
timer_t m_timer; // internal timer |
| 406 |
global_control_t m_control; // control |
| 407 |
|
| 408 |
s8 m_out = 0; // 6 bit output |
| 409 |
}; |
| 410 |
|
| 411 |
#endif |