| 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 ES5504 emulation core |
| 7 |
*/ |
| 8 |
|
| 9 |
#ifndef _VGSOUND_EMU_SRC_ES5505_HPP |
| 10 |
#define _VGSOUND_EMU_SRC_ES5505_HPP |
| 11 |
|
| 12 |
#pragma once |
| 13 |
|
| 14 |
#include "es550x.hpp" |
| 15 |
|
| 16 |
// ES5505 specific |
| 17 |
class es5505_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("es5505_output") |
| 25 |
, m_left(left) |
| 26 |
, m_right(right) |
| 27 |
{ |
| 28 |
} |
| 29 |
|
| 30 |
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 clamp16(s32 in) { return clamp(in, -0x8000, 0x7fff); } |
| 43 |
|
| 44 |
inline void clamp16(output_t &src) |
| 45 |
{ |
| 46 |
m_left = clamp16(src.left()); |
| 47 |
m_right = clamp16(src.right()); |
| 48 |
} |
| 49 |
|
| 50 |
inline void clamp16() |
| 51 |
{ |
| 52 |
m_left = clamp16(m_left); |
| 53 |
m_right = clamp16(m_right); |
| 54 |
} |
| 55 |
|
| 56 |
// setters |
| 57 |
inline void set_left(s32 left) { m_left = left; } |
| 58 |
|
| 59 |
inline void set_right(s32 right) { m_right = right; } |
| 60 |
|
| 61 |
inline 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 = clamp16(m_left + src.left()); |
| 81 |
m_right = clamp16(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 |
private: |
| 93 |
s32 m_left = 0; |
| 94 |
s32 m_right = 0; |
| 95 |
}; |
| 96 |
|
| 97 |
// es5505 voice classes |
| 98 |
class voice_t : public es550x_voice_t |
| 99 |
{ |
| 100 |
public: |
| 101 |
// constructor |
| 102 |
voice_t(es5505_core &host) |
| 103 |
: es550x_voice_t("es5505_voice", 20, 9, false) |
| 104 |
, m_host(host) |
| 105 |
, m_lvol(0) |
| 106 |
, m_rvol(0) |
| 107 |
, m_ch(output_t()) |
| 108 |
, m_mute(false) |
| 109 |
{ |
| 110 |
m_output.fill(0); |
| 111 |
} |
| 112 |
|
| 113 |
// internal state |
| 114 |
virtual void reset() override; |
| 115 |
virtual void fetch(u8 cycle) override; |
| 116 |
virtual void tick(u8 voice) override; |
| 117 |
|
| 118 |
// setters |
| 119 |
inline void set_lvol(u8 lvol) { m_lvol = lvol; } |
| 120 |
|
| 121 |
inline void set_rvol(u8 rvol) { m_rvol = rvol; } |
| 122 |
|
| 123 |
// getters |
| 124 |
inline u8 lvol() { return m_lvol; } |
| 125 |
|
| 126 |
inline u8 rvol() { return m_rvol; } |
| 127 |
|
| 128 |
output_t &ch() { return m_ch; } |
| 129 |
|
| 130 |
// for debug/preview only |
| 131 |
inline void set_mute(bool mute) { m_mute = mute; } |
| 132 |
|
| 133 |
inline s32 left_out() { return m_mute ? 0 : m_output[0]; } |
| 134 |
|
| 135 |
inline s32 right_out() { return m_mute ? 0 : m_output[1]; } |
| 136 |
|
| 137 |
private: |
| 138 |
s32 volume_calc(u8 volume, s32 in); |
| 139 |
|
| 140 |
// registers |
| 141 |
es5505_core &m_host; |
| 142 |
u8 m_lvol = 0; // Left volume |
| 143 |
u8 m_rvol = 0; // Right volume |
| 144 |
output_t m_ch; // channel output |
| 145 |
bool m_mute = false; // mute flag (for debug purpose) |
| 146 |
std::array<s32, 2> m_output; // output preview (for debug purpose) |
| 147 |
}; |
| 148 |
|
| 149 |
class sermode_t : public vgsound_emu_core |
| 150 |
{ |
| 151 |
public: |
| 152 |
sermode_t() |
| 153 |
: vgsound_emu_core("es5505_sermode") |
| 154 |
, m_adc(0) |
| 155 |
, m_test(0) |
| 156 |
, m_sony_bb(0) |
| 157 |
, m_msb(0) |
| 158 |
{ |
| 159 |
} |
| 160 |
|
| 161 |
void reset() |
| 162 |
{ |
| 163 |
m_adc = 0; |
| 164 |
m_test = 0; |
| 165 |
m_sony_bb = 0; |
| 166 |
m_msb = 0; |
| 167 |
} |
| 168 |
|
| 169 |
// setters |
| 170 |
void write(u16 data) |
| 171 |
{ |
| 172 |
m_adc = (data >> 0) & 1; |
| 173 |
m_test = (data >> 1) & 1; |
| 174 |
m_sony_bb = (data >> 2) & 1; |
| 175 |
m_msb = (data >> 11) & 0x1f; |
| 176 |
} |
| 177 |
|
| 178 |
void set_adc(bool adc) { m_adc = adc ? 1 : 0; } |
| 179 |
|
| 180 |
void set_test(bool test) { m_test = test ? 1 : 0; } |
| 181 |
|
| 182 |
void set_sony_bb(bool sony_bb) { m_sony_bb = sony_bb ? 1 : 0; } |
| 183 |
|
| 184 |
void set_msb(u8 msb) { m_msb = msb & 0x1f; } |
| 185 |
|
| 186 |
// getters |
| 187 |
bool adc() { return m_adc; } |
| 188 |
|
| 189 |
bool test() { return m_test; } |
| 190 |
|
| 191 |
bool sony_bb() { return m_sony_bb; } |
| 192 |
|
| 193 |
u8 msb() { return m_msb; } |
| 194 |
|
| 195 |
private: |
| 196 |
u8 m_adc : 1; // A/D |
| 197 |
u8 m_test : 1; // Test |
| 198 |
u8 m_sony_bb : 1; // Sony/BB format serial output |
| 199 |
u8 m_msb : 5; // Serial output MSB |
| 200 |
}; |
| 201 |
|
| 202 |
public: |
| 203 |
// constructor |
| 204 |
es5505_core(es550x_intf &intf) |
| 205 |
: es550x_shared_core("es5505", 32, intf) |
| 206 |
, m_voice{*this, *this, *this, *this, *this, *this, *this, *this, *this, *this, *this, |
| 207 |
*this, *this, *this, *this, *this, *this, *this, *this, *this, *this, *this, |
| 208 |
*this, *this, *this, *this, *this, *this, *this, *this, *this, *this} |
| 209 |
, m_sermode(sermode_t()) |
| 210 |
, m_bclk(clock_pulse_t<s8>(4, 0)) |
| 211 |
, m_lrclk(clock_pulse_t<s8>(16, 1)) |
| 212 |
, m_wclk(0) |
| 213 |
, m_wclk_lr(false) |
| 214 |
, m_output_bit(0) |
| 215 |
, m_ch{output_t()} |
| 216 |
, m_output{output_t()} |
| 217 |
, m_output_temp{output_t()} |
| 218 |
, m_output_latch{output_t()} |
| 219 |
{ |
| 220 |
} |
| 221 |
|
| 222 |
// host interface |
| 223 |
u16 host_r(u8 address); |
| 224 |
void host_w(u8 address, u16 data); |
| 225 |
|
| 226 |
// internal state |
| 227 |
virtual void reset() override; |
| 228 |
virtual void tick() override; |
| 229 |
|
| 230 |
// less cycle accurate, but also less cpu heavy update routine |
| 231 |
void tick_perf(); |
| 232 |
|
| 233 |
// clock outputs |
| 234 |
inline bool bclk() { return m_bclk.current_edge(); } |
| 235 |
|
| 236 |
inline bool bclk_rising_edge() { return m_bclk.rising_edge(); } |
| 237 |
|
| 238 |
inline bool bclk_falling_edge() { return m_bclk.falling_edge(); } |
| 239 |
|
| 240 |
// Input mode for Channel 3 |
| 241 |
inline void lin(s32 in) |
| 242 |
{ |
| 243 |
if (m_sermode.adc()) |
| 244 |
{ |
| 245 |
m_ch[3].set_left(in); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
inline void rin(s32 in) |
| 250 |
{ |
| 251 |
if (m_sermode.adc()) |
| 252 |
{ |
| 253 |
m_ch[3].set_right(in); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
// 4 stereo output channels |
| 258 |
inline s32 lout(u8 ch) { return m_ch[ch & 0x3].left(); } |
| 259 |
|
| 260 |
inline s32 rout(u8 ch) { return m_ch[ch & 0x3].right(); } |
| 261 |
|
| 262 |
//----------------------------------------------------------------- |
| 263 |
// |
| 264 |
// for preview/debug purpose only, not for serious emulators |
| 265 |
// |
| 266 |
//----------------------------------------------------------------- |
| 267 |
|
| 268 |
// bypass chips host interface for debug purpose only |
| 269 |
u16 read(u8 address, bool cpu_access = false); |
| 270 |
void write(u8 address, u16 data); |
| 271 |
|
| 272 |
u16 regs_r(u8 page, u8 address, bool cpu_access = false); |
| 273 |
void regs_w(u8 page, u8 address, u16 data); |
| 274 |
|
| 275 |
u16 regs_r(u8 page, u8 address) |
| 276 |
{ |
| 277 |
u8 prev = m_page; |
| 278 |
m_page = page; |
| 279 |
u16 ret = read(address, false); |
| 280 |
m_page = prev; |
| 281 |
return ret; |
| 282 |
} |
| 283 |
|
| 284 |
inline void set_mute(u8 ch, bool mute) { m_voice[ch & 0x1f].set_mute(mute); } |
| 285 |
|
| 286 |
// per-voice outputs |
| 287 |
inline s32 voice_lout(u8 voice) { return (voice < 32) ? m_voice[voice].left_out() : 0; } |
| 288 |
|
| 289 |
inline s32 voice_rout(u8 voice) { return (voice < 32) ? m_voice[voice].right_out() : 0; } |
| 290 |
|
| 291 |
protected: |
| 292 |
virtual inline u8 max_voices() override { return 32; } |
| 293 |
|
| 294 |
virtual void voice_tick() override; |
| 295 |
|
| 296 |
private: |
| 297 |
std::array<voice_t, 32> m_voice; // 32 voices |
| 298 |
// Serial related stuffs |
| 299 |
sermode_t m_sermode; // Serial mode register |
| 300 |
clock_pulse_t<s8> m_bclk; // BCLK clock (CLKIN / 4), freely running clock |
| 301 |
clock_pulse_t<s8> m_lrclk; // LRCLK |
| 302 |
s16 m_wclk = 0; // WCLK |
| 303 |
bool m_wclk_lr = false; // WCLK, L/R output select |
| 304 |
s8 m_output_bit = 0; // Bit position in output |
| 305 |
std::array<output_t, 4> m_ch; // 4 stereo output channels |
| 306 |
std::array<output_t, 4> m_output; // Serial outputs |
| 307 |
std::array<output_t, 4> m_output_temp; // temporary signal for serial output |
| 308 |
std::array<output_t, 4> m_output_latch; // output latch |
| 309 |
}; |
| 310 |
|
| 311 |
#endif |