| 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 K053260 core |
| 7 |
*/ |
| 8 |
|
| 9 |
#ifndef _VGSOUND_EMU_SRC_K053260_HPP |
| 10 |
#define _VGSOUND_EMU_SRC_K053260_HPP |
| 11 |
|
| 12 |
#pragma once |
| 13 |
|
| 14 |
#include "../core/core.hpp" |
| 15 |
|
| 16 |
using namespace vgsound_emu; |
| 17 |
|
| 18 |
class k053260_intf : public vgsound_emu_core |
| 19 |
{ |
| 20 |
public: |
| 21 |
k053260_intf() |
| 22 |
: vgsound_emu_core("k053260_intf") |
| 23 |
{ |
| 24 |
} |
| 25 |
|
| 26 |
virtual u8 read_sample(u32 address) { return 0; } // sample fetch |
| 27 |
|
| 28 |
//virtual void write_int(u8 out) {} // timer interrupt |
| 29 |
}; |
| 30 |
|
| 31 |
class k053260_core : public vgsound_emu_core |
| 32 |
{ |
| 33 |
friend class k053260_intf; // k053260 specific interface |
| 34 |
|
| 35 |
private: |
| 36 |
const s32 m_pan_lut[8][2] = { |
| 37 |
{0x00, 0x00}, |
| 38 |
{0x7f, 0x00}, |
| 39 |
{0x74, 0x34}, |
| 40 |
{0x68, 0x49}, |
| 41 |
{0x5a, 0x5a}, |
| 42 |
{0x49, 0x68}, |
| 43 |
{0x34, 0x74}, |
| 44 |
{0x00, 0x7f} |
| 45 |
}; // pan LUT |
| 46 |
|
| 47 |
const s8 m_adpcm_lut[16] = |
| 48 |
{0, 1, 2, 4, 8, 16, 32, 64, -128, -64, -32, -16, -8, -4, -2, -1}; // ADPCM LUT |
| 49 |
|
| 50 |
class voice_t : public vgsound_emu_core |
| 51 |
{ |
| 52 |
public: |
| 53 |
// constructor |
| 54 |
voice_t(k053260_core &host) |
| 55 |
: vgsound_emu_core("k053260_voice") |
| 56 |
, m_host(host) |
| 57 |
, m_enable(0) |
| 58 |
, m_busy(0) |
| 59 |
, m_loop(0) |
| 60 |
, m_adpcm(0) |
| 61 |
, m_pitch(0) |
| 62 |
, m_reverse(0) |
| 63 |
, m_start(0) |
| 64 |
, m_length(0) |
| 65 |
, m_volume(0) |
| 66 |
, m_pan(4) |
| 67 |
, m_counter(0) |
| 68 |
, m_addr(0) |
| 69 |
, m_remain(0) |
| 70 |
, m_bitpos(4) |
| 71 |
, m_data(0) |
| 72 |
, m_output(0) |
| 73 |
{ |
| 74 |
std::fill_n(m_out, 2, 0); |
| 75 |
} |
| 76 |
|
| 77 |
// internal state |
| 78 |
void reset(); |
| 79 |
void tick(u32 cycle); |
| 80 |
|
| 81 |
// accessors |
| 82 |
void write(u8 address, u8 data); |
| 83 |
void keyon(); |
| 84 |
void keyoff(); |
| 85 |
|
| 86 |
// setters |
| 87 |
inline void set_enable(bool enable) { m_enable = enable ? 1 : 0; } |
| 88 |
|
| 89 |
inline void set_busy(bool busy) { m_busy = busy ? 1 : 0; } |
| 90 |
|
| 91 |
inline void set_loop(bool loop) { m_loop = loop ? 1 : 0; } |
| 92 |
|
| 93 |
inline void set_adpcm(bool adpcm) { m_adpcm = adpcm ? 1 : 0; } |
| 94 |
|
| 95 |
inline void set_reverse(const bool reverse) |
| 96 |
{ |
| 97 |
m_reverse = reverse ? 1 : 0; |
| 98 |
} |
| 99 |
|
| 100 |
inline void length_inc() { m_length = (m_length + 1) & 0xffff; } |
| 101 |
|
| 102 |
inline void set_pan(u8 pan) { m_pan = pan & 7; } |
| 103 |
|
| 104 |
// getters |
| 105 |
inline bool enable() { return m_enable; } |
| 106 |
|
| 107 |
inline bool busy() { return m_busy; } |
| 108 |
|
| 109 |
inline u32 start() { return m_start; } |
| 110 |
|
| 111 |
inline u16 length() { return m_length; } |
| 112 |
|
| 113 |
inline s32 out(u8 ch) { return m_out[ch & 1]; } |
| 114 |
|
| 115 |
private: |
| 116 |
// registers |
| 117 |
k053260_core &m_host; |
| 118 |
u16 m_enable : 1; // enable flag |
| 119 |
u16 m_busy : 1; // busy status |
| 120 |
u16 m_loop : 1; // loop flag |
| 121 |
u16 m_adpcm : 1; // ADPCM flag |
| 122 |
u16 m_pitch : 12; // pitch |
| 123 |
u8 m_reverse : 1; // reverse playback |
| 124 |
u32 m_start = 0; // start position |
| 125 |
u16 m_length = 0; // source length |
| 126 |
u8 m_volume = 0; // master volume |
| 127 |
s32 m_pan = 4; // master pan |
| 128 |
u16 m_counter = 0; // frequency counter |
| 129 |
u32 m_addr = 0; // current address |
| 130 |
s32 m_remain = 0; // remain for end sample |
| 131 |
u8 m_bitpos = 4; // bit position for ADPCM decoding |
| 132 |
u8 m_data = 0; // current data |
| 133 |
s8 m_output = 0; // ADPCM buffer |
| 134 |
s32 m_out[2]; // current output |
| 135 |
}; |
| 136 |
|
| 137 |
class ctrl_t |
| 138 |
{ |
| 139 |
public: |
| 140 |
ctrl_t() |
| 141 |
: m_rom_read(0) |
| 142 |
, m_sound_en(0) |
| 143 |
, m_input_en(0) |
| 144 |
{ |
| 145 |
} |
| 146 |
|
| 147 |
void reset() |
| 148 |
{ |
| 149 |
m_rom_read = 0; |
| 150 |
m_sound_en = 0; |
| 151 |
m_input_en = 0; |
| 152 |
} |
| 153 |
|
| 154 |
void write(u8 data) |
| 155 |
{ |
| 156 |
m_rom_read = (data >> 0) & 1; |
| 157 |
m_sound_en = (data >> 1) & 1; |
| 158 |
m_input_en = (data >> 2) & 3; |
| 159 |
} |
| 160 |
|
| 161 |
// getters |
| 162 |
inline bool rom_read() { return m_rom_read; } |
| 163 |
|
| 164 |
inline bool sound_en() { return m_sound_en; } |
| 165 |
|
| 166 |
inline u8 input_en() { return m_input_en; } |
| 167 |
|
| 168 |
private: |
| 169 |
u8 m_rom_read : 1; // ROM readback |
| 170 |
u8 m_sound_en : 1; // Sound enable |
| 171 |
u8 m_input_en : 2; // Input enable |
| 172 |
}; |
| 173 |
|
| 174 |
/* |
| 175 |
class ym3012_t |
| 176 |
{ |
| 177 |
public: |
| 178 |
ym3012_t() |
| 179 |
{ |
| 180 |
m_in.fill(0); |
| 181 |
m_out.fill(0); |
| 182 |
} |
| 183 |
|
| 184 |
void reset() |
| 185 |
{ |
| 186 |
m_in.fill(0); |
| 187 |
m_out.fill(0); |
| 188 |
} |
| 189 |
|
| 190 |
void tick(u8 ch, s32 in) |
| 191 |
{ |
| 192 |
m_out[(ch & 1)] = m_in[(ch & 1)]; |
| 193 |
m_in[(ch & 1) ^ 1] = in; |
| 194 |
} |
| 195 |
|
| 196 |
private: |
| 197 |
std::array<s32, 2> m_in; |
| 198 |
std::array<s32, 2> m_out; |
| 199 |
}; |
| 200 |
*/ |
| 201 |
|
| 202 |
/* |
| 203 |
class dac_t |
| 204 |
{ |
| 205 |
public: |
| 206 |
dac_t() |
| 207 |
: m_clock(0) |
| 208 |
, m_state(0) |
| 209 |
{ |
| 210 |
} |
| 211 |
|
| 212 |
void reset() |
| 213 |
{ |
| 214 |
m_clock = 0; |
| 215 |
m_state = 0; |
| 216 |
} |
| 217 |
|
| 218 |
inline void set_clock(u8 clock) { m_clock = clock; } |
| 219 |
|
| 220 |
inline void set_state(u8 state) { m_state = state; } |
| 221 |
|
| 222 |
inline u8 clock() { return m_clock; } |
| 223 |
|
| 224 |
inline u8 state() { return m_state; } |
| 225 |
|
| 226 |
private: |
| 227 |
u8 m_clock : 4; // DAC clock (16 clock) |
| 228 |
u8 m_state : 2; // DAC state (4 state - SAM1, SAM2) |
| 229 |
}; |
| 230 |
*/ |
| 231 |
|
| 232 |
public: |
| 233 |
// constructor |
| 234 |
k053260_core(k053260_intf &intf) |
| 235 |
: vgsound_emu_core("k053260") |
| 236 |
, m_voice{*this, *this, *this, *this} |
| 237 |
, m_intf(intf) |
| 238 |
, m_ctrl(ctrl_t()) |
| 239 |
//, m_ym3012(ym3012_t()) |
| 240 |
//, m_dac(dac_t()) |
| 241 |
{ |
| 242 |
std::fill_n(m_host2snd, 2, 0); |
| 243 |
std::fill_n(m_snd2host, 2, 0); |
| 244 |
std::fill_n(m_reg, 64, 0); |
| 245 |
std::fill_n(m_out, 2, 0); |
| 246 |
} |
| 247 |
|
| 248 |
// communications |
| 249 |
inline u8 snd2host_r(u8 address) { return m_snd2host[address & 1]; } |
| 250 |
|
| 251 |
inline void host2snd_w(u8 address, u8 data) { m_host2snd[address & 1] = data; } |
| 252 |
|
| 253 |
// sound accessors |
| 254 |
u8 read(u8 address); |
| 255 |
void write(u8 address, u8 data); |
| 256 |
|
| 257 |
// internal state |
| 258 |
void reset(); |
| 259 |
void tick(u32 cycle); |
| 260 |
|
| 261 |
// getters for debug, trackers, etc |
| 262 |
inline s32 output(u8 ch) { return m_out[ch & 1]; } // output for each channels |
| 263 |
|
| 264 |
inline u8 reg_r(u8 address) { return m_reg[address & 0x3f]; } |
| 265 |
|
| 266 |
inline s32 voice_out(u8 voice, u8 ch) |
| 267 |
{ |
| 268 |
return (voice < 4) ? m_voice[voice].out(ch & 1) : 0; |
| 269 |
} |
| 270 |
|
| 271 |
protected: |
| 272 |
inline s32 pan_lut(const u8 pan, const u8 out) { return m_pan_lut[pan][out]; } |
| 273 |
|
| 274 |
inline s32 adpcm_lut(const u8 nibble) { return m_adpcm_lut[nibble]; } |
| 275 |
|
| 276 |
private: |
| 277 |
voice_t m_voice[4]; |
| 278 |
k053260_intf &m_intf; // common memory interface |
| 279 |
|
| 280 |
u8 m_host2snd[2]; |
| 281 |
u8 m_snd2host[2]; |
| 282 |
|
| 283 |
ctrl_t m_ctrl; // chip control |
| 284 |
|
| 285 |
//ym3012_t m_ym3012; // YM3012 output |
| 286 |
//dac_t m_dac; // YM3012 interface |
| 287 |
|
| 288 |
u8 m_reg[64]; // register pool |
| 289 |
s32 m_out[2]; // stereo output |
| 290 |
}; |
| 291 |
|
| 292 |
#endif |