1 // license:GPL-2.0+ 2 // copyright-holders:Jarek Burczynski,Ernesto Corvi 3 /* 4 ** File: ym2151.h - header file for software implementation of YM2151 5 ** FM Operator Type-M(OPM) 6 ** 7 ** (c) 1997-2002 Jarek Burczynski (s0246@poczta.onet.pl, bujar@mame.net) 8 ** Some of the optimizing ideas by Tatsuyuki Satoh 9 ** 10 ** Version 2.150 final beta May, 11th 2002 11 ** 12 ** 13 ** I would like to thank following people for making this project possible: 14 ** 15 ** Beauty Planets - for making a lot of real YM2151 samples and providing 16 ** additional informations about the chip. Also for the time spent making 17 ** the samples and the speed of replying to my endless requests. 18 ** 19 ** Shigeharu Isoda - for general help, for taking time to scan his YM2151 20 ** Japanese Manual first of all, and answering MANY of my questions. 21 ** 22 ** Nao - for giving me some info about YM2151 and pointing me to Shigeharu. 23 ** Also for creating fmemu (which I still use to test the emulator). 24 ** 25 ** Aaron Giles and Chris Hardy - they made some samples of one of my favourite 26 ** arcade games so I could compare it to my emulator. 27 ** 28 ** Bryan McPhail and Tim (powerjaw) - for making some samples. 29 ** 30 ** Ishmair - for the datasheet and motivation. 31 */ 32 33 #ifndef MAME_SOUND_YM2151_H 34 #define MAME_SOUND_YM2151_H 35 36 #pragma once 37 38 39 //************************************************************************** 40 // TYPE DEFINITIONS 41 //************************************************************************** 42 43 44 // ======================> ym2151_device 45 46 class ym2151_device : public device_t, 47 public device_sound_interface 48 { 49 public: 50 // construction/destruction 51 ym2151_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock); 52 53 // configuration helpers irq_handler()54 auto irq_handler() { return m_irqhandler.bind(); } port_write_handler()55 auto port_write_handler() { return m_portwritehandler.bind(); } 56 57 // read/write 58 u8 read(offs_t offset); 59 void write(offs_t offset, u8 data); 60 61 u8 status_r(); 62 void register_w(u8 data); 63 void data_w(u8 data); 64 65 DECLARE_WRITE_LINE_MEMBER(reset_w); 66 67 protected: 68 // device-level overrides 69 ym2151_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock); 70 71 virtual void device_start() override; 72 virtual void device_reset() override; 73 virtual void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr) override; 74 virtual void device_post_load() override; 75 virtual void device_clock_changed() override; 76 77 // sound stream update overrides 78 virtual void sound_stream_update(sound_stream &stream, std::vector<read_stream_view> const &inputs, std::vector<write_stream_view> &outputs) override; 79 80 virtual void calculate_timers(); 81 virtual void write_reg(int r, int v); 82 83 private: 84 enum { 85 TIMER_IRQ_A_OFF, 86 TIMER_IRQ_B_OFF, 87 TIMER_A, 88 TIMER_B 89 }; 90 91 enum { 92 RATE_STEPS = 8, 93 TL_RES_LEN = 256, /* 8 bits addressing (real chip) */ 94 95 /* TL_TAB_LEN is calculated as: 96 * 13 - sinus amplitude bits (Y axis) 97 * 2 - sinus sign bit (Y axis) 98 * TL_RES_LEN - sinus resolution (X axis) 99 */ 100 TL_TAB_LEN = 13*2*TL_RES_LEN, 101 102 SIN_BITS = 10, 103 SIN_LEN = 1 << SIN_BITS, 104 SIN_MASK = SIN_LEN - 1 105 }; 106 107 int tl_tab[TL_TAB_LEN]; 108 unsigned int sin_tab[SIN_LEN]; 109 uint32_t d1l_tab[16]; 110 111 static const uint8_t eg_inc[19*RATE_STEPS]; 112 static const uint8_t eg_rate_select[32+64+32]; 113 static const uint8_t eg_rate_shift[32+64+32]; 114 static const uint32_t dt2_tab[4]; 115 static const uint8_t dt1_tab[4*32]; 116 static const uint16_t phaseinc_rom[768]; 117 static const uint8_t lfo_noise_waveform[256]; 118 119 /* struct describing a single operator */ 120 struct YM2151Operator 121 { 122 uint32_t phase; /* accumulated operator phase */ 123 uint32_t freq; /* operator frequency count */ 124 int32_t dt1; /* current DT1 (detune 1 phase inc/decrement) value */ 125 uint32_t mul; /* frequency count multiply */ 126 uint32_t dt1_i; /* DT1 index * 32 */ 127 uint32_t dt2; /* current DT2 (detune 2) value */ 128 129 signed int *connect; /* operator output 'direction' */ 130 131 /* only M1 (operator 0) is filled with this data: */ 132 signed int *mem_connect; /* where to put the delayed sample (MEM) */ 133 int32_t mem_value; /* delayed sample (MEM) value */ 134 135 /* channel specific data; note: each operator number 0 contains channel specific data */ 136 uint32_t fb_shift; /* feedback shift value for operators 0 in each channel */ 137 int32_t fb_out_curr; /* operator feedback value (used only by operators 0) */ 138 int32_t fb_out_prev; /* previous feedback value (used only by operators 0) */ 139 uint32_t kc; /* channel KC (copied to all operators) */ 140 uint32_t kc_i; /* just for speedup */ 141 uint32_t pms; /* channel PMS */ 142 uint32_t ams; /* channel AMS */ 143 /* end of channel specific data */ 144 145 uint32_t AMmask; /* LFO Amplitude Modulation enable mask */ 146 uint32_t state; /* Envelope state: 4-attack(AR) 3-decay(D1R) 2-sustain(D2R) 1-release(RR) 0-off */ 147 uint8_t eg_sh_ar; /* (attack state) */ 148 uint8_t eg_sel_ar; /* (attack state) */ 149 uint32_t tl; /* Total attenuation Level */ 150 int32_t volume; /* current envelope attenuation level */ 151 uint8_t eg_sh_d1r; /* (decay state) */ 152 uint8_t eg_sel_d1r; /* (decay state) */ 153 uint32_t d1l; /* envelope switches to sustain state after reaching this level */ 154 uint8_t eg_sh_d2r; /* (sustain state) */ 155 uint8_t eg_sel_d2r; /* (sustain state) */ 156 uint8_t eg_sh_rr; /* (release state) */ 157 uint8_t eg_sel_rr; /* (release state) */ 158 159 uint32_t key; /* 0=last key was KEY OFF, 1=last key was KEY ON */ 160 161 uint32_t ks; /* key scale */ 162 uint32_t ar; /* attack rate */ 163 uint32_t d1r; /* decay rate */ 164 uint32_t d2r; /* sustain rate */ 165 uint32_t rr; /* release rate */ 166 167 uint32_t reserved0; /**/ 168 uint32_t reserved1; /**/ 169 170 void key_on(uint32_t key_set, uint32_t eg_cnt); 171 void key_off(uint32_t key_set); 172 }; 173 174 signed int chanout[8]; 175 signed int m2,c1,c2; /* Phase Modulation input for operators 2,3,4 */ 176 signed int mem; /* one sample delay memory */ 177 178 YM2151Operator oper[32]; /* the 32 operators */ 179 180 uint32_t pan[16]; /* channels output masks (0xffffffff = enable) */ 181 182 uint32_t eg_cnt; /* global envelope generator counter */ 183 uint32_t eg_timer; /* global envelope generator counter works at frequency = chipclock/64/3 */ 184 uint32_t eg_timer_add; /* step of eg_timer */ 185 uint32_t eg_timer_overflow; /* envelope generator timer overflows every 3 samples (on real chip) */ 186 187 uint32_t lfo_phase; /* accumulated LFO phase (0 to 255) */ 188 uint32_t lfo_timer; /* LFO timer */ 189 uint32_t lfo_timer_add; /* step of lfo_timer */ 190 uint32_t lfo_overflow; /* LFO generates new output when lfo_timer reaches this value */ 191 uint32_t lfo_counter; /* LFO phase increment counter */ 192 uint32_t lfo_counter_add; /* step of lfo_counter */ 193 uint8_t lfo_wsel; /* LFO waveform (0-saw, 1-square, 2-triangle, 3-random noise) */ 194 uint8_t amd; /* LFO Amplitude Modulation Depth */ 195 int8_t pmd; /* LFO Phase Modulation Depth */ 196 uint32_t lfa; /* LFO current AM output */ 197 int32_t lfp; /* LFO current PM output */ 198 199 uint8_t test; /* TEST register */ 200 uint8_t ct; /* output control pins (bit1-CT2, bit0-CT1) */ 201 202 uint32_t noise; /* noise enable/period register (bit 7 - noise enable, bits 4-0 - noise period */ 203 uint32_t noise_rng; /* 17 bit noise shift register */ 204 uint32_t noise_p; /* current noise 'phase'*/ 205 uint32_t noise_f; /* current noise period */ 206 207 uint32_t csm_req; /* CSM KEY ON / KEY OFF sequence request */ 208 209 uint32_t irq_enable; /* IRQ enable for timer B (bit 3) and timer A (bit 2); bit 7 - CSM mode (keyon to all slots, everytime timer A overflows) */ 210 uint32_t status; /* chip status (BUSY, IRQ Flags) */ 211 uint8_t connect[8]; /* channels connections */ 212 213 emu_timer *timer_A, *timer_A_irq_off; 214 emu_timer *timer_B, *timer_B_irq_off; 215 216 protected: 217 attotime timer_A_time[1024]; /* timer A times for MAME */ 218 attotime timer_B_time[256]; /* timer B times for MAME */ 219 220 private: 221 int irqlinestate; 222 223 uint32_t timer_A_index; /* timer A index */ 224 uint32_t timer_B_index; /* timer B index */ 225 uint32_t timer_A_index_old; /* timer A previous index */ 226 uint32_t timer_B_index_old; /* timer B previous index */ 227 228 /* Frequency-deltas to get the closest frequency possible. 229 * There are 11 octaves because of DT2 (max 950 cents over base frequency) 230 * and LFO phase modulation (max 800 cents below AND over base frequency) 231 * Summary: octave explanation 232 * 0 note code - LFO PM 233 * 1 note code 234 * 2 note code 235 * 3 note code 236 * 4 note code 237 * 5 note code 238 * 6 note code 239 * 7 note code 240 * 8 note code 241 * 9 note code + DT2 + LFO PM 242 * 10 note code + DT2 + LFO PM 243 */ 244 uint32_t freq[11*768]; /* 11 octaves, 768 'cents' per octave */ 245 246 /* Frequency deltas for DT1. These deltas alter operator frequency 247 * after it has been taken from frequency-deltas table. 248 */ 249 int32_t dt1_freq[8*32]; /* 8 DT1 levels, 32 KC values */ 250 251 // internal state 252 sound_stream * m_stream; 253 uint8_t m_lastreg; 254 devcb_write_line m_irqhandler; 255 devcb_write8 m_portwritehandler; 256 bool m_reset_active; 257 258 void init_tables(); 259 void envelope_KONKOFF(YM2151Operator * op, int v); 260 void set_connect(YM2151Operator *om1, int cha, int v); 261 void advance(); 262 void advance_eg(); 263 void chan_calc(unsigned int chan); 264 void chan7_calc(); 265 int op_calc(YM2151Operator * OP, unsigned int env, signed int pm); 266 int op_calc1(YM2151Operator * OP, unsigned int env, signed int pm); 267 void refresh_EG(YM2151Operator * op); 268 }; 269 270 // ======================> ym2164_device 271 272 class ym2164_device : public ym2151_device 273 { 274 public: 275 // construction/destruction 276 ym2164_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock); 277 278 protected: 279 virtual void calculate_timers() override; 280 virtual void write_reg(int r, int v) override; 281 }; 282 283 // ======================> ym2414_device 284 285 class ym2414_device : public ym2151_device 286 { 287 public: 288 // construction/destruction 289 ym2414_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock); 290 }; 291 292 // device type definition 293 DECLARE_DEVICE_TYPE(YM2151, ym2151_device) 294 DECLARE_DEVICE_TYPE(YM2164, ym2164_device) 295 DECLARE_DEVICE_TYPE(YM2414, ym2414_device) 296 297 298 #endif // MAME_SOUND_YM2151_H 299