| 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 |
Ensoniq ES5506 emulation core |
| 7 |
*/ |
| 8 |
|
| 9 |
#ifndef _VGSOUND_EMU_SRC_ES5506_HPP |
| 10 |
#define _VGSOUND_EMU_SRC_ES5506_HPP |
| 11 |
|
| 12 |
#pragma once |
| 13 |
|
| 14 |
#include "es550x.hpp" |
| 15 |
|
| 16 |
// ES5506 specific |
| 17 |
class es5506_core : public es550x_shared_core |
| 18 |
{ |
| 19 |
private: |
| 20 |
class output_t : public vgsound_emu_core |
| 21 |
{ |
| 22 |
public: |
| 23 |
output_t(s32 left = 0, s32 right = 0) |
| 24 |
: vgsound_emu_core("es5506_output") |
| 25 |
, m_left(left) |
| 26 |
, m_right(right) |
| 27 |
{ |
| 28 |
} |
| 29 |
|
| 30 |
inline void reset() |
| 31 |
{ |
| 32 |
m_left = 0; |
| 33 |
m_right = 0; |
| 34 |
} |
| 35 |
|
| 36 |
inline void copy_output(output_t &src) |
| 37 |
{ |
| 38 |
m_left = src.left(); |
| 39 |
m_right = src.right(); |
| 40 |
} |
| 41 |
|
| 42 |
inline s32 clamp20(s32 in) { return VGS_CLAMP(in, -0x80000, 0x7ffff); } |
| 43 |
|
| 44 |
inline void clamp20(output_t &src) |
| 45 |
{ |
| 46 |
m_left = clamp20(src.left()); |
| 47 |
m_right = clamp20(src.right()); |
| 48 |
} |
| 49 |
|
| 50 |
inline void clamp20() |
| 51 |
{ |
| 52 |
m_left = clamp20(m_left); |
| 53 |
m_right = clamp20(m_right); |
| 54 |
} |
| 55 |
|
| 56 |
// setters |
| 57 |
inline void set_left(s32 left) { m_left = clamp20(left); } |
| 58 |
|
| 59 |
inline void set_right(s32 right) { m_right = clamp20(right); } |
| 60 |
|
| 61 |
void serial_in(bool ch, u8 in) |
| 62 |
{ |
| 63 |
if (ch) // Right output |
| 64 |
{ |
| 65 |
m_right = (m_right << 1) | (in ? 1 : 0); |
| 66 |
} |
| 67 |
else // Left output |
| 68 |
{ |
| 69 |
m_left = (m_left << 1) | (in ? 1 : 0); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// getters |
| 74 |
inline s32 left() { return m_left; } |
| 75 |
|
| 76 |
inline s32 right() { return m_right; } |
| 77 |
|
| 78 |
output_t &operator+=(output_t &src) |
| 79 |
{ |
| 80 |
m_left = clamp20(m_left + src.left()); |
| 81 |
m_right = clamp20(m_right + src.right()); |
| 82 |
return *this; |
| 83 |
} |
| 84 |
|
| 85 |
output_t &operator>>(s32 shift) |
| 86 |
{ |
| 87 |
m_left >>= shift; |
| 88 |
m_right >>= shift; |
| 89 |
return *this; |
| 90 |
} |
| 91 |
|
| 92 |
public: // oh my... |
| 93 |
s32 m_left = 0; |
| 94 |
s32 m_right = 0; |
| 95 |
}; |
| 96 |
|
| 97 |
// es5506 voice classes |
| 98 |
class voice_t : public es550x_voice_t |
| 99 |
{ |
| 100 |
private: |
| 101 |
// es5506 Filter ramp class |
| 102 |
class filter_ramp_t : public vgsound_emu_core |
| 103 |
{ |
| 104 |
public: |
| 105 |
filter_ramp_t() |
| 106 |
: vgsound_emu_core("es5506_filter_ramp") |
| 107 |
, m_slow(0) |
| 108 |
, m_ramp(0) |
| 109 |
{ |
| 110 |
} |
| 111 |
|
| 112 |
void reset() |
| 113 |
{ |
| 114 |
m_slow = 0; |
| 115 |
m_ramp = 0; |
| 116 |
}; |
| 117 |
|
| 118 |
// Setters |
| 119 |
inline void write(u16 data) |
| 120 |
{ |
| 121 |
m_slow = data & 1; |
| 122 |
m_ramp = (data >> 8) & 0xff; |
| 123 |
} |
| 124 |
|
| 125 |
// Getters |
| 126 |
inline bool slow() { return m_slow; } |
| 127 |
|
| 128 |
inline u16 ramp() { return m_ramp; } |
| 129 |
|
| 130 |
private: |
| 131 |
u16 m_slow : 1; // Slow mode flag |
| 132 |
u16 m_ramp = 8; // Ramp value |
| 133 |
}; |
| 134 |
|
| 135 |
public: |
| 136 |
// constructor |
| 137 |
voice_t(es5506_core &host) |
| 138 |
: es550x_voice_t("es5506_voice", 21, 11, true) |
| 139 |
, m_host(host) |
| 140 |
, m_lvol(0) |
| 141 |
, m_rvol(0) |
| 142 |
, m_lvramp(0) |
| 143 |
, m_rvramp(0) |
| 144 |
, m_ecount(0) |
| 145 |
, m_k2ramp(filter_ramp_t()) |
| 146 |
, m_k1ramp(filter_ramp_t()) |
| 147 |
, m_filtcount(0) |
| 148 |
, m_mute(false) |
| 149 |
, m_output{0,0} |
| 150 |
{ |
| 151 |
} |
| 152 |
|
| 153 |
// internal state |
| 154 |
virtual void reset() override; |
| 155 |
virtual void fetch(u8 cycle) override; |
| 156 |
virtual void tick(u8 voice) override; |
| 157 |
|
| 158 |
// Setters |
| 159 |
inline void set_lvol(s32 lvol) { m_lvol = lvol; } |
| 160 |
|
| 161 |
inline void set_rvol(s32 rvol) { m_rvol = rvol; } |
| 162 |
|
| 163 |
inline void set_lvramp(s32 lvramp) { m_lvramp = lvramp; } |
| 164 |
|
| 165 |
inline void set_rvramp(s32 rvramp) { m_rvramp = rvramp; } |
| 166 |
|
| 167 |
inline void set_ecount(s16 ecount) { m_ecount = ecount; } |
| 168 |
|
| 169 |
// Getters |
| 170 |
inline s32 lvol() { return m_lvol; } |
| 171 |
|
| 172 |
inline s32 rvol() { return m_rvol; } |
| 173 |
|
| 174 |
inline s32 lvramp() { return m_lvramp; } |
| 175 |
|
| 176 |
inline s32 rvramp() { return m_rvramp; } |
| 177 |
|
| 178 |
inline s16 ecount() { return m_ecount; } |
| 179 |
|
| 180 |
inline filter_ramp_t &k2ramp() { return m_k2ramp; } |
| 181 |
|
| 182 |
inline filter_ramp_t &k1ramp() { return m_k1ramp; } |
| 183 |
|
| 184 |
inline bool muted() { return m_mute; } |
| 185 |
|
| 186 |
output_t &ch() { return m_ch; } |
| 187 |
|
| 188 |
// for debug/preview only |
| 189 |
inline void set_mute(bool mute) { m_mute = mute; } |
| 190 |
|
| 191 |
inline s32 left_out() { return m_mute ? 0 : m_output[0]; } |
| 192 |
|
| 193 |
inline s32 right_out() { return m_mute ? 0 : m_output[1]; } |
| 194 |
|
| 195 |
private: |
| 196 |
// accessors, getters, setters |
| 197 |
s16 decompress(u8 sample); |
| 198 |
s32 volume_calc(u16 volume, s32 in); |
| 199 |
|
| 200 |
// registers |
| 201 |
es5506_core &m_host; |
| 202 |
// Volume register: 4 bit exponent, 8 bit mantissa |
| 203 |
// 4 LSBs are used for fine control of ramp increment for hardware envelope |
| 204 |
s32 m_lvol = 0; // Left volume |
| 205 |
s32 m_rvol = 0; // Right volume |
| 206 |
// Envelope |
| 207 |
s32 m_lvramp = 0; // Left volume ramp |
| 208 |
s32 m_rvramp = 0; // Righr volume ramp |
| 209 |
s16 m_ecount = 0; // Envelope counter |
| 210 |
filter_ramp_t m_k2ramp; // Filter coefficient 2 Ramp |
| 211 |
filter_ramp_t m_k1ramp; // Filter coefficient 1 Ramp |
| 212 |
u8 m_filtcount = 0; // Internal counter for slow mode |
| 213 |
output_t m_ch; // channel output |
| 214 |
bool m_mute = false; // mute flag (for debug purpose) |
| 215 |
s32 m_output[2]; // output preview (for debug purpose) |
| 216 |
}; |
| 217 |
|
| 218 |
// 5 bit mode |
| 219 |
class mode_t : public vgsound_emu_core |
| 220 |
{ |
| 221 |
public: |
| 222 |
mode_t() |
| 223 |
: vgsound_emu_core("es5506_mode") |
| 224 |
, m_lrclk_en(1) |
| 225 |
, m_wclk_en(1) |
| 226 |
, m_bclk_en(1) |
| 227 |
, m_master(0) |
| 228 |
, m_dual(0) |
| 229 |
{ |
| 230 |
} |
| 231 |
|
| 232 |
// internal states |
| 233 |
void reset() |
| 234 |
{ |
| 235 |
m_lrclk_en = 1; |
| 236 |
m_wclk_en = 1; |
| 237 |
m_bclk_en = 1; |
| 238 |
m_master = 0; |
| 239 |
m_dual = 0; |
| 240 |
} |
| 241 |
|
| 242 |
// accessors |
| 243 |
void write(u8 data) |
| 244 |
{ |
| 245 |
m_lrclk_en = (data >> 0) & 1; |
| 246 |
m_wclk_en = (data >> 1) & 1; |
| 247 |
m_bclk_en = (data >> 2) & 1; |
| 248 |
m_master = (data >> 3) & 1; |
| 249 |
m_dual = (data >> 4) & 1; |
| 250 |
} |
| 251 |
|
| 252 |
// getters |
| 253 |
bool lrclk_en() { return m_lrclk_en; } |
| 254 |
|
| 255 |
bool wclk_en() { return m_wclk_en; } |
| 256 |
|
| 257 |
bool bclk_en() { return m_bclk_en; } |
| 258 |
|
| 259 |
bool master() { return m_master; } |
| 260 |
|
| 261 |
bool dual() { return m_dual; } |
| 262 |
|
| 263 |
private: |
| 264 |
u8 m_lrclk_en : 1; // Set LRCLK to output |
| 265 |
u8 m_wclk_en : 1; // Set WCLK to output |
| 266 |
u8 m_bclk_en : 1; // Set BCLK to output |
| 267 |
u8 m_master : 1; // Set memory mode to master |
| 268 |
u8 m_dual : 1; // Set dual chip config |
| 269 |
}; |
| 270 |
|
| 271 |
public: |
| 272 |
// constructor |
| 273 |
es5506_core(es550x_intf &intf) |
| 274 |
: es550x_shared_core("es5506", 32, intf) |
| 275 |
, m_voice{*this, *this, *this, *this, *this, *this, *this, *this, *this, *this, *this, |
| 276 |
*this, *this, *this, *this, *this, *this, *this, *this, *this, *this, *this, |
| 277 |
*this, *this, *this, *this, *this, *this, *this, *this, *this, *this} |
| 278 |
, m_read_latch(0) |
| 279 |
, m_write_latch(0) |
| 280 |
, m_w_st(0) |
| 281 |
, m_w_end(0) |
| 282 |
, m_lr_end(0) |
| 283 |
, m_mode(mode_t()) |
| 284 |
, m_w_st_curr(0) |
| 285 |
, m_w_end_curr(0) |
| 286 |
, m_bclk(clock_pulse_t<s8>(4, 0)) |
| 287 |
, m_lrclk(clock_pulse_t<s8>(32, 1)) |
| 288 |
, m_wclk(0) |
| 289 |
, m_wclk_lr(false) |
| 290 |
, m_output_bit(0) |
| 291 |
, m_ch{output_t()} |
| 292 |
, m_output{output_t()} |
| 293 |
, m_output_temp{output_t()} |
| 294 |
, m_output_latch{output_t()} |
| 295 |
{ |
| 296 |
} |
| 297 |
|
| 298 |
// host interface |
| 299 |
u8 host_r(u8 address); |
| 300 |
void host_w(u8 address, u8 data); |
| 301 |
|
| 302 |
// internal state |
| 303 |
virtual void reset() override; |
| 304 |
virtual void tick() override; |
| 305 |
|
| 306 |
// less cycle accurate, but also less cpu heavy update routine |
| 307 |
void tick_perf(); |
| 308 |
|
| 309 |
// clock outputs |
| 310 |
inline bool bclk() { return m_bclk.current_edge(); } |
| 311 |
|
| 312 |
inline bool bclk_rising_edge() { return m_bclk.rising_edge(); } |
| 313 |
|
| 314 |
inline bool bclk_falling_edge() { return m_bclk.falling_edge(); } |
| 315 |
|
| 316 |
// 6 stereo output channels |
| 317 |
inline s32 lout(u8 ch) { return m_output[ch&7].left(); } |
| 318 |
|
| 319 |
inline s32 rout(u8 ch) { return m_output[ch&7].right(); } |
| 320 |
|
| 321 |
//----------------------------------------------------------------- |
| 322 |
// |
| 323 |
// for preview/debug purpose only, not for serious emulators |
| 324 |
// |
| 325 |
//----------------------------------------------------------------- |
| 326 |
|
| 327 |
// bypass chips host interface for debug purpose only |
| 328 |
u8 read(u8 address, bool cpu_access = false); |
| 329 |
void write(u8 address, u8 data); |
| 330 |
|
| 331 |
u32 regs_r(u8 page, u8 address, bool cpu_access = false); |
| 332 |
void regs_w(u8 page, u8 address, u32 data); |
| 333 |
|
| 334 |
u8 regs8_r(u8 page, u8 address) |
| 335 |
{ |
| 336 |
u8 prev = m_page; |
| 337 |
m_page = page; |
| 338 |
u8 ret = read(address, false); |
| 339 |
m_page = prev; |
| 340 |
return ret; |
| 341 |
} |
| 342 |
|
| 343 |
inline void set_mute(u8 ch, bool mute) { m_voice[ch & 0x1f].set_mute(mute); } |
| 344 |
|
| 345 |
// per-voice outputs |
| 346 |
inline s32 voice_lout(u8 voice) { return (voice < 32) ? m_voice[voice].left_out() : 0; } |
| 347 |
|
| 348 |
inline s32 voice_rout(u8 voice) { return (voice < 32) ? m_voice[voice].right_out() : 0; } |
| 349 |
|
| 350 |
protected: |
| 351 |
virtual inline u8 max_voices() override { return 32; } |
| 352 |
|
| 353 |
virtual void voice_tick() override; |
| 354 |
|
| 355 |
private: |
| 356 |
voice_t m_voice[32]; // 32 voices |
| 357 |
|
| 358 |
// Host interfaces |
| 359 |
u32 m_read_latch = 0; // 32 bit register latch for host read |
| 360 |
u32 m_write_latch = 0; // 32 bit register latch for host write |
| 361 |
|
| 362 |
// Serial register |
| 363 |
u8 m_w_st = 0; // Word clock start register |
| 364 |
u8 m_w_end = 0; // Word clock end register |
| 365 |
u8 m_lr_end = 0; // Left/Right clock end register |
| 366 |
mode_t m_mode; // Global mode |
| 367 |
|
| 368 |
// Serial related stuffs |
| 369 |
u8 m_w_st_curr = 0; // Word clock start, current status |
| 370 |
u8 m_w_end_curr = 0; // Word clock end register |
| 371 |
clock_pulse_t<s8> m_bclk; // BCLK clock (CLKIN / 4), freely running clock |
| 372 |
clock_pulse_t<s8> m_lrclk; // LRCLK |
| 373 |
s16 m_wclk = 0; // WCLK |
| 374 |
bool m_wclk_lr = false; // WCLK, L/R output select |
| 375 |
s8 m_output_bit = 0; // Bit position in output |
| 376 |
output_t m_ch[6]; // 6 stereo output channels |
| 377 |
output_t m_output[8]; // Serial outputs |
| 378 |
output_t m_output_temp[6]; // temporary signal for serial output |
| 379 |
output_t m_output_latch[6]; // output latch |
| 380 |
}; |
| 381 |
|
| 382 |
#endif |