59 const std::string& name);
77 static inline FreqIndex fnumToIncrement(
unsigned block_fnum)
81 unsigned block = (block_fnum & 0x1C00) >> 10;
82 return FreqIndex(block_fnum & 0x03FF) >> (11 - block);
89 inline int op_calc(
unsigned phase,
unsigned lfo_am)
const;
104 template<
typename Archive>
158 template<
typename Archive>
159 void serialize(Archive& ar,
unsigned version);
196 template<
typename Archive>
197 void serialize(Archive& ar,
unsigned version);
201 virtual int getAmplificationFactor()
const;
202 virtual void generateChannels(
int** bufs,
unsigned num);
204 void callback(
byte flag);
208 void setStatus(
byte flag);
209 void resetStatus(
byte flag);
210 void changeStatusMask(
byte flag);
213 inline int genPhaseHighHat();
214 inline int genPhaseSnare();
215 inline int genPhaseCymbal();
217 void chan_calc_rhythm(
unsigned lfo_am);
218 void set_mul(
unsigned sl,
byte v);
219 void set_ksl_tl(
unsigned sl,
byte v);
220 void set_ar_dr(
unsigned sl,
byte v);
221 void set_sl_rr(
unsigned sl,
byte v);
222 bool checkMuteHelper();
224 inline bool isExtended(
unsigned ch)
const;
228 const std::unique_ptr<YMF262Debuggable> debuggable;
231 static const int R04_ST1 = 0x01;
232 static const int R04_ST2 = 0x02;
233 static const int R04_MASK_T2 = 0x20;
234 static const int R04_MASK_T1 = 0x40;
235 static const int R04_IRQ_RESET = 0x80;
238 static const int STATUS_T2 = R04_MASK_T2;
239 static const int STATUS_T1 = R04_MASK_T1;
241 const std::unique_ptr<EmuTimer> timer1;
242 const std::unique_ptr<EmuTimer> timer2;
251 unsigned pan[18 * 4];
262 byte lfo_pm_depth_range;
272 bool alreadySignaledNEW2;
279 static const int ENV_BITS = 10;
280 static const int ENV_LEN = 1 << ENV_BITS;
281 static const double ENV_STEP = 128.0 / ENV_LEN;
283 static const int MAX_ATT_INDEX = (1 << (ENV_BITS - 1)) - 1;
284 static const int MIN_ATT_INDEX = 0;
287 static const int SIN_BITS = 10;
288 static const int SIN_LEN = 1 << SIN_BITS;
289 static const int SIN_MASK = SIN_LEN - 1;
291 static const int TL_RES_LEN = 256;
294 static const byte MOD = 0;
295 static const byte CAR = 1;
299 static const int slot_array[32] = {
300 0, 2, 4, 1, 3, 5, -1, -1,
301 6, 8, 10, 7, 9, 11, -1, -1,
302 12, 14, 16, 13, 15, 17, -1, -1,
303 -1, -1, -1, -1, -1, -1, -1, -1
311 #define DV(x) int(x / (0.1875 / 2.0))
312 static const unsigned ksl_tab[8 * 16] = {
314 DV( 0.000),
DV( 0.000),
DV( 0.000),
DV( 0.000),
315 DV( 0.000),
DV( 0.000),
DV( 0.000),
DV( 0.000),
316 DV( 0.000),
DV( 0.000),
DV( 0.000),
DV( 0.000),
317 DV( 0.000),
DV( 0.000),
DV( 0.000),
DV( 0.000),
319 DV( 0.000),
DV( 0.000),
DV( 0.000),
DV( 0.000),
320 DV( 0.000),
DV( 0.000),
DV( 0.000),
DV( 0.000),
321 DV( 0.000),
DV( 0.750),
DV( 1.125),
DV( 1.500),
322 DV( 1.875),
DV( 2.250),
DV( 2.625),
DV( 3.000),
324 DV( 0.000),
DV( 0.000),
DV( 0.000),
DV( 0.000),
325 DV( 0.000),
DV( 1.125),
DV( 1.875),
DV( 2.625),
326 DV( 3.000),
DV( 3.750),
DV( 4.125),
DV( 4.500),
327 DV( 4.875),
DV( 5.250),
DV( 5.625),
DV( 6.000),
329 DV( 0.000),
DV( 0.000),
DV( 0.000),
DV( 1.875),
330 DV( 3.000),
DV( 4.125),
DV( 4.875),
DV( 5.625),
331 DV( 6.000),
DV( 6.750),
DV( 7.125),
DV( 7.500),
332 DV( 7.875),
DV( 8.250),
DV( 8.625),
DV( 9.000),
334 DV( 0.000),
DV( 0.000),
DV( 3.000),
DV( 4.875),
335 DV( 6.000),
DV( 7.125),
DV( 7.875),
DV( 8.625),
336 DV( 9.000),
DV( 9.750),
DV(10.125),
DV(10.500),
337 DV(10.875),
DV(11.250),
DV(11.625),
DV(12.000),
339 DV( 0.000),
DV( 3.000),
DV( 6.000),
DV( 7.875),
340 DV( 9.000),
DV(10.125),
DV(10.875),
DV(11.625),
341 DV(12.000),
DV(12.750),
DV(13.125),
DV(13.500),
342 DV(13.875),
DV(14.250),
DV(14.625),
DV(15.000),
344 DV( 0.000),
DV( 6.000),
DV( 9.000),
DV(10.875),
345 DV(12.000),
DV(13.125),
DV(13.875),
DV(14.625),
346 DV(15.000),
DV(15.750),
DV(16.125),
DV(16.500),
347 DV(16.875),
DV(17.250),
DV(17.625),
DV(18.000),
349 DV( 0.000),
DV( 9.000),
DV(12.000),
DV(13.875),
350 DV(15.000),
DV(16.125),
DV(16.875),
DV(17.625),
351 DV(18.000),
DV(18.750),
DV(19.125),
DV(19.500),
352 DV(19.875),
DV(20.250),
DV(20.625),
DV(21.000)
358 #define SC(db) unsigned(db * (2.0 / ENV_STEP))
359 static const unsigned sl_tab[16] = {
366 static const byte RATE_STEPS = 8;
367 static const byte eg_inc[15 * RATE_STEPS] = {
390 #define O(a) (a * RATE_STEPS)
392 static const byte eg_rate_select[16 + 64 + 16] = {
395 O(14),
O(14),
O(14),
O(14),
O(14),
O(14),
O(14),
O(14),
396 O(14),
O(14),
O(14),
O(14),
O(14),
O(14),
O(14),
O(14),
399 O( 0),
O( 1),
O( 2),
O( 3),
400 O( 0),
O( 1),
O( 2),
O( 3),
401 O( 0),
O( 1),
O( 2),
O( 3),
402 O( 0),
O( 1),
O( 2),
O( 3),
403 O( 0),
O( 1),
O( 2),
O( 3),
404 O( 0),
O( 1),
O( 2),
O( 3),
405 O( 0),
O( 1),
O( 2),
O( 3),
406 O( 0),
O( 1),
O( 2),
O( 3),
407 O( 0),
O( 1),
O( 2),
O( 3),
408 O( 0),
O( 1),
O( 2),
O( 3),
409 O( 0),
O( 1),
O( 2),
O( 3),
410 O( 0),
O( 1),
O( 2),
O( 3),
411 O( 0),
O( 1),
O( 2),
O( 3),
414 O( 4),
O( 5),
O( 6),
O( 7),
417 O( 8),
O( 9),
O(10),
O(11),
420 O(12),
O(12),
O(12),
O(12),
423 O(12),
O(12),
O(12),
O(12),
O(12),
O(12),
O(12),
O(12),
424 O(12),
O(12),
O(12),
O(12),
O(12),
O(12),
O(12),
O(12),
432 static const byte eg_rate_shift[16 + 64 + 16] =
436 O( 0),
O( 0),
O( 0),
O( 0),
O( 0),
O( 0),
O( 0),
O( 0),
437 O( 0),
O( 0),
O( 0),
O( 0),
O( 0),
O( 0),
O( 0),
O( 0),
440 O(12),
O(12),
O(12),
O(12),
441 O(11),
O(11),
O(11),
O(11),
442 O(10),
O(10),
O(10),
O(10),
443 O( 9),
O( 9),
O( 9),
O( 9),
444 O( 8),
O( 8),
O( 8),
O( 8),
445 O( 7),
O( 7),
O( 7),
O( 7),
446 O( 6),
O( 6),
O( 6),
O( 6),
447 O( 5),
O( 5),
O( 5),
O( 5),
448 O( 4),
O( 4),
O( 4),
O( 4),
449 O( 3),
O( 3),
O( 3),
O( 3),
450 O( 2),
O( 2),
O( 2),
O( 2),
451 O( 1),
O( 1),
O( 1),
O( 1),
452 O( 0),
O( 0),
O( 0),
O( 0),
453 O( 0),
O( 0),
O( 0),
O( 0),
454 O( 0),
O( 0),
O( 0),
O( 0),
455 O( 0),
O( 0),
O( 0),
O( 0),
458 O( 0),
O( 0),
O( 0),
O( 0),
O( 0),
O( 0),
O( 0),
O( 0),
459 O( 0),
O( 0),
O( 0),
O( 0),
O( 0),
O( 0),
O( 0),
O( 0),
465 #define ML(x) byte(2 * x)
466 static const byte mul_tab[16] = {
468 ML( 0.5),
ML( 1.0),
ML( 2.0),
ML( 3.0),
469 ML( 4.0),
ML( 5.0),
ML( 6.0),
ML( 7.0),
470 ML( 8.0),
ML( 9.0),
ML(10.0),
ML(10.0),
471 ML(12.0),
ML(12.0),
ML(15.0),
ML(15.0)
483 static const int TL_TAB_LEN = 13 * 2 * TL_RES_LEN;
489 static unsigned sin_tab[SIN_LEN * 8];
503 static const unsigned LFO_AM_TAB_ELEMENTS = 210;
504 static const byte lfo_am_table[LFO_AM_TAB_ELEMENTS] = {
561 static const signed char lfo_pm_table[8 * 8 * 2] = {
563 0, 0, 0, 0, 0, 0, 0, 0,
564 0, 0, 0, 0, 0, 0, 0, 0,
567 0, 0, 0, 0, 0, 0, 0, 0,
568 1, 0, 0, 0,-1, 0, 0, 0,
571 1, 0, 0, 0,-1, 0, 0, 0,
572 2, 1, 0,-1,-2,-1, 0, 1,
575 1, 0, 0, 0,-1, 0, 0, 0,
576 3, 1, 0,-1,-3,-1, 0, 1,
579 2, 1, 0,-1,-2,-1, 0, 1,
580 4, 2, 0,-2,-4,-2, 0, 2,
583 2, 1, 0,-1,-2,-1, 0, 1,
584 5, 2, 0,-2,-5,-2, 0, 2,
587 3, 1, 0,-1,-3,-1, 0, 1,
588 6, 3, 0,-3,-6,-3, 0, 3,
591 3, 1, 0,-1,-3,-1, 0, 1,
592 7, 3, 0,-3,-7,-3, 0, 3
596 static int phase_modulation;
597 static int phase_modulation2;
624 void YMF262::Impl::callback(
byte flag)
630 void YMF262::Impl::setStatus(
byte flag)
634 if (status & statusMask) {
641 void YMF262::Impl::resetStatus(
byte flag)
645 if (!(status & statusMask)) {
652 void YMF262::Impl::changeStatusMask(
byte flag)
655 status &= statusMask;
670 if (!(eg_cnt & eg_m_ar)) {
671 volume += (~volume * eg_inc[eg_sel_ar + ((eg_cnt >> eg_sh_ar) & 7)]) >> 3;
672 if (volume <= MIN_ATT_INDEX) {
673 volume = MIN_ATT_INDEX;
680 if (!(eg_cnt & eg_m_dr)) {
681 volume += eg_inc[eg_sel_dr + ((eg_cnt >> eg_sh_dr) & 7)];
699 if (!(eg_cnt & eg_m_rr)) {
700 volume += eg_inc[eg_sel_rr + ((eg_cnt >> eg_sh_rr) & 7)];
701 if (volume >= MAX_ATT_INDEX) {
702 volume = MAX_ATT_INDEX;
711 if (!(eg_cnt & eg_m_rr)) {
712 volume += eg_inc[eg_sel_rr + ((eg_cnt >> eg_sh_rr) & 7)];
713 if (volume >= MAX_ATT_INDEX) {
714 volume = MAX_ATT_INDEX;
730 unsigned fnum_lfo = (block_fnum & 0x0380) >> 7;
731 int lfo_fn_table_index_offset = lfo_pm_table[lfo_pm + 16 * fnum_lfo];
732 Cnt += fnumToIncrement(block_fnum + lfo_fn_table_index_offset) * mul;
744 lfo_pm_cnt.addQuantum();
745 unsigned lfo_pm = (lfo_pm_cnt.toInt() & 7) | lfo_pm_depth_range;
748 for (
int c = 0; c < 18; ++c) {
750 for (
int s = 0; s < 2; ++s) {
777 noise_rng ^= 0x800302;
785 unsigned env = (TLL + volume + (lfo_am & AMmask)) << 4;
786 int p = env + wavetable[phase & SIN_MASK];
787 return (p < TL_TAB_LEN) ? tl_tab[p] : 0;
815 phase_modulation = 0;
816 phase_modulation2 = 0;
843 phase_modulation = 0;
887 inline int YMF262::Impl::genPhaseHighHat()
894 int op71phase = channel[7].slot[MOD].Cnt.toInt();
895 bool bit7 = (op71phase & 0x80) != 0;
896 bool bit3 = (op71phase & 0x08) != 0;
897 bool bit2 = (op71phase & 0x04) != 0;
898 bool res1 = (bit2 ^ bit7) | bit3;
901 unsigned phase = res1 ? (0x200 | (0xd0 >> 2)) : 0xd0;
904 int op82phase = channel[8].slot[CAR].Cnt.toInt();
905 bool bit5e= (op82phase & 0x20) != 0;
906 bool bit3e= (op82phase & 0x08) != 0;
907 bool res2 = (bit3e ^ bit5e);
911 phase = (0x200 | (0xd0 >> 2));
918 phase = 0x200 | 0xd0;
930 inline int YMF262::Impl::genPhaseSnare()
935 return ((channel[7].slot[MOD].Cnt.toInt() & 0x100) + 0x100)
936 ^ ((noise_rng & 1) << 8);
939 inline int YMF262::Impl::genPhaseCymbal()
945 int op82phase = channel[8].slot[CAR].Cnt.toInt();
946 if ((op82phase ^ (op82phase << 2)) & 0x20) {
950 int op71phase = channel[7].slot[MOD].Cnt.toInt();
951 bool bit7 = (op71phase & 0x80) != 0;
952 bool bit3 = (op71phase & 0x08) != 0;
953 bool bit2 = (op71phase & 0x04) != 0;
954 return ((bit2 != bit7) || bit3) ? 0x300 : 0x100;
959 void YMF262::Impl::chan_calc_rhythm(
unsigned lfo_am)
968 YMF262Slot& mod6 = channel[6].slot[MOD];
969 int out = mod6.fb_shift ? mod6.op1_out[0] + mod6.op1_out[1] : 0;
970 mod6.op1_out[0] = mod6.op1_out[1];
971 int pm = mod6.CON ? 0 : mod6.op1_out[0];
972 mod6.op1_out[1] = mod6.op_calc(mod6.Cnt.toInt() + (out >> mod6.fb_shift), lfo_am);
973 YMF262Slot& car6 = channel[6].slot[CAR];
974 chanout[6] += 2 * car6.op_calc(car6.Cnt.toInt() + pm, lfo_am);
989 YMF262Slot& mod7 = channel[7].slot[MOD];
990 chanout[7] += 2 * mod7.op_calc(genPhaseHighHat(), lfo_am);
991 YMF262Slot& car7 = channel[7].slot[CAR];
992 chanout[7] += 2 * car7.op_calc(genPhaseSnare(), lfo_am);
993 YMF262Slot& mod8 = channel[8].slot[MOD];
994 chanout[8] += 2 * mod8.op_calc(mod8.Cnt.toInt(), lfo_am);
995 YMF262Slot& car8 = channel[8].slot[CAR];
996 chanout[8] += 2 * car8.op_calc(genPhaseCymbal(), lfo_am);
1001 void YMF262::Impl::init_tables()
1003 static bool alreadyInit =
false;
1004 if (alreadyInit)
return;
1007 for (
int x = 0; x < TL_RES_LEN; x++) {
1008 double m = (1 << 16) / pow(2, (x + 1) * (ENV_STEP / 4.0) / 8.0);
1015 n = (n >> 1) + (n & 1);
1018 tl_tab[x * 2 + 0] = n;
1019 tl_tab[x * 2 + 1] = ~tl_tab[x * 2 + 0];
1021 for (
int i = 1; i < 13; i++) {
1022 tl_tab[x * 2 + 0 + i * 2 * TL_RES_LEN] = tl_tab[x * 2 + 0] >> i;
1023 tl_tab[x * 2 + 1 + i * 2 * TL_RES_LEN] = ~tl_tab[x * 2 + 0 + i * 2 * TL_RES_LEN];
1027 const double LOG2 = ::log(2.0);
1028 for (
int i = 0; i < SIN_LEN; i++) {
1030 double m = sin(((i * 2) + 1) * M_PI / SIN_LEN);
1032 double o = (m > 0.0) ?
1033 8 * ::log( 1.0 / m) / LOG2:
1034 8 * ::log(-1.0 / m) / LOG2;
1035 o = o / (ENV_STEP / 4);
1038 n = (n >> 1) + (n & 1);
1039 sin_tab[i] = n * 2 + (m >= 0.0 ? 0 : 1);
1042 for (
int i = 0; i < SIN_LEN; ++i) {
1047 sin_tab[1 * SIN_LEN + i] = (i & (1 << (SIN_BITS - 1)))
1054 sin_tab[2 * SIN_LEN + i] = sin_tab[i & (SIN_MASK >> 1)];
1059 sin_tab[3 * SIN_LEN + i] = (i & (1 << (SIN_BITS - 2)))
1061 : sin_tab[i & (SIN_MASK>>2)];
1067 sin_tab[4 * SIN_LEN + i] = (i & (1 << (SIN_BITS - 1)))
1075 sin_tab[5 * SIN_LEN + i] = (i & (1 << (SIN_BITS - 1)))
1077 : sin_tab[(i * 2) & (SIN_MASK >> 1)];
1083 sin_tab[6 * SIN_LEN + i] = (i & (1 << (SIN_BITS - 1)))
1090 int x = (i & (1 << (SIN_BITS - 1)))
1091 ? ((SIN_LEN - 1) - i) * 16 + 1
1093 x = std::min(x, TL_TAB_LEN);
1094 sin_tab[7 * SIN_LEN + i] = x;
1116 if (state != EG_OFF) {
1125 if ((ar + ksr) < 16 + 60) {
1127 eg_sh_ar = eg_rate_shift [ar + ksr];
1128 eg_sel_ar = eg_rate_select[ar + ksr];
1131 eg_sel_ar = 13 * RATE_STEPS;
1133 eg_m_ar = (1 << eg_sh_ar) - 1;
1134 eg_sh_dr = eg_rate_shift [dr + ksr];
1135 eg_sel_dr = eg_rate_select[dr + ksr];
1136 eg_m_dr = (1 << eg_sh_dr) - 1;
1140 eg_sh_rr = eg_rate_shift [rr + ksr];
1141 eg_sel_rr = eg_rate_select[rr + ksr];
1142 eg_m_rr = (1 << eg_sh_rr) - 1;
1151 int newKsr = ch.
kcode >> KSR;
1152 if (ksr == newKsr)
return;
1160 static const unsigned channelPairTab[18] = {
1161 0, 1, 2, 0, 1, 2, unsigned(~0), unsigned(~0), unsigned(~0),
1162 9, 10, 11, 9, 10, 11, unsigned(~0), unsigned(~0), unsigned(~0),
1164 inline bool YMF262::Impl::isExtended(
unsigned ch)
const
1167 if (!OPL3_mode)
return false;
1168 if (channelPairTab[ch] ==
unsigned(~0))
return false;
1169 return channel[channelPairTab[ch]].extended;
1171 static inline unsigned getFirstOfPairNum(
unsigned ch)
1173 assert((ch < 18) && (channelPairTab[ch] !=
unsigned(~0)));
1174 return channelPairTab[ch];
1176 inline YMF262Channel& YMF262::Impl::getFirstOfPair(
unsigned ch)
1178 return channel[getFirstOfPairNum(ch) + 0];
1180 inline YMF262Channel& YMF262::Impl::getSecondOfPair(
unsigned ch)
1182 return channel[getFirstOfPairNum(ch) + 3];
1186 void YMF262::Impl::set_mul(
unsigned sl,
byte v)
1188 unsigned chan_no = sl / 2;
1189 YMF262Channel& ch = channel[chan_no];
1190 YMF262Slot& slot = ch.slot[sl & 1];
1192 slot.mul = mul_tab[v & 0x0f];
1193 slot.KSR = (v & 0x10) ? 0 : 2;
1194 slot.eg_type = (v & 0x20) != 0;
1195 slot.vib = (v & 0x40) != 0;
1196 slot.AMmask = (v & 0x80) ? ~0 : 0;
1198 if (isExtended(chan_no)) {
1201 slot.calc_fc(getFirstOfPair(chan_no));
1209 void YMF262::Impl::set_ksl_tl(
unsigned sl,
byte v)
1211 unsigned chan_no = sl / 2;
1212 YMF262Channel& ch = channel[chan_no];
1213 YMF262Slot& slot = ch.slot[sl & 1];
1216 slot.ksl = ksl ? 3 - ksl : 31;
1217 slot.TL = (v & 0x3F) << (ENV_BITS - 1 - 7);
1219 if (isExtended(chan_no)) {
1221 YMF262Channel& ch0 = getFirstOfPair(chan_no);
1222 slot.TLL = slot.TL + (ch0.ksl_base >> slot.ksl);
1225 slot.TLL = slot.TL + (ch.ksl_base >> slot.ksl);
1230 void YMF262::Impl::set_ar_dr(
unsigned sl,
byte v)
1232 YMF262Channel& ch = channel[sl / 2];
1233 YMF262Slot& slot = ch.slot[sl & 1];
1235 slot.ar = (v >> 4) ? 16 + ((v >> 4) << 2) : 0;
1236 slot.dr = (v & 0x0F) ? 16 + ((v & 0x0F) << 2) : 0;
1237 slot.update_ar_dr();
1241 void YMF262::Impl::set_sl_rr(
unsigned sl,
byte v)
1243 YMF262Channel& ch = channel[sl / 2];
1244 YMF262Slot& slot = ch.slot[sl & 1];
1246 slot.sl = sl_tab[v >> 4];
1247 slot.rr = (v & 0x0F) ? 16 + ((v & 0x0F) << 2) : 0;
1264 if (!OPL3_mode && (r != 0x105)) {
1273 writeRegDirect(r, v, time);
1282 channel[ 0].extended = (v & 0x01) != 0;
1283 channel[ 1].extended = (v & 0x02) != 0;
1284 channel[ 2].extended = (v & 0x04) != 0;
1285 channel[ 9].extended = (v & 0x08) != 0;
1286 channel[10].extended = (v & 0x10) != 0;
1287 channel[11].extended = (v & 0x20) != 0;
1292 OPL3_mode = v & 0x01;
1301 if ((v & 0x02) && !alreadySignaledNEW2 && isYMF278) {
1303 alreadySignaledNEW2 =
true;
1318 unsigned ch_offset = (r & 0x100) ? 9 : 0;
1326 timer1->setValue(v);
1330 timer2->setValue(v);
1338 changeStatusMask((~v) & 0x60);
1339 timer1->setStart((v & R04_ST1) != 0, time);
1340 timer2->setStart((v & R04_ST2) != 0, time);
1345 nts = (v & 0x40) != 0;
1354 int slot = slot_array[r & 0x1F];
1355 if (slot < 0)
return;
1356 set_mul(slot + ch_offset * 2, v);
1360 int slot = slot_array[r & 0x1F];
1361 if (slot < 0)
return;
1362 set_ksl_tl(slot + ch_offset * 2, v);
1366 int slot = slot_array[r & 0x1F];
1367 if (slot < 0)
return;
1368 set_ar_dr(slot + ch_offset * 2, v);
1372 int slot = slot_array[r & 0x1F];
1373 if (slot < 0)
return;
1374 set_sl_rr(slot + ch_offset * 2, v);
1381 lfo_am_depth = (v & 0x80) != 0;
1382 lfo_pm_depth_range = (v & 0x40) ? 8 : 0;
1385 if (rhythm & 0x20) {
1388 channel[6].slot[MOD].FM_KEYON (2);
1389 channel[6].slot[CAR].FM_KEYON (2);
1391 channel[6].slot[MOD].FM_KEYOFF(2);
1392 channel[6].slot[CAR].FM_KEYOFF(2);
1396 channel[7].slot[MOD].FM_KEYON (2);
1398 channel[7].slot[MOD].FM_KEYOFF(2);
1402 channel[7].slot[CAR].FM_KEYON (2);
1404 channel[7].slot[CAR].FM_KEYOFF(2);
1408 channel[8].slot[MOD].FM_KEYON (2);
1410 channel[8].slot[MOD].FM_KEYOFF(2);
1414 channel[8].slot[CAR].FM_KEYON (2);
1416 channel[8].slot[CAR].FM_KEYOFF(2);
1420 channel[6].slot[MOD].FM_KEYOFF(2);
1421 channel[6].slot[CAR].FM_KEYOFF(2);
1423 channel[7].slot[MOD].FM_KEYOFF(2);
1425 channel[7].slot[CAR].FM_KEYOFF(2);
1427 channel[8].slot[MOD].FM_KEYOFF(2);
1429 channel[8].slot[CAR].FM_KEYOFF(2);
1435 if ((r & 0x0F) > 8) {
1438 unsigned chan_no = (r & 0x0F) + ch_offset;
1439 YMF262Channel& ch = channel[chan_no];
1443 block_fnum = (ch.block_fnum & 0x1F00) | v;
1446 block_fnum = ((v & 0x1F) << 8) | (ch.block_fnum & 0xFF);
1447 if (isExtended(chan_no)) {
1448 if (getFirstOfPairNum(chan_no) == chan_no) {
1451 YMF262Channel& ch0 = getFirstOfPair(chan_no);
1452 YMF262Channel& ch3 = getSecondOfPair(chan_no);
1454 ch0.slot[MOD].FM_KEYON(1);
1455 ch0.slot[CAR].FM_KEYON(1);
1456 ch3.slot[MOD].FM_KEYON(1);
1457 ch3.slot[CAR].FM_KEYON(1);
1459 ch0.slot[MOD].FM_KEYOFF(1);
1460 ch0.slot[CAR].FM_KEYOFF(1);
1461 ch3.slot[MOD].FM_KEYOFF(1);
1462 ch3.slot[CAR].FM_KEYOFF(1);
1470 ch.slot[MOD].FM_KEYON (1);
1471 ch.slot[CAR].FM_KEYON (1);
1473 ch.slot[MOD].FM_KEYOFF(1);
1474 ch.slot[CAR].FM_KEYOFF(1);
1479 if (ch.block_fnum != block_fnum) {
1480 ch.block_fnum = block_fnum;
1481 ch.ksl_base = ksl_tab[block_fnum >> 6];
1482 ch.fc = fnumToIncrement(block_fnum);
1485 ch.kcode = (ch.block_fnum & 0x1C00) >> 9;
1492 ch.kcode |= (ch.block_fnum & 0x100) >> 8;
1494 ch.kcode |= (ch.block_fnum & 0x200) >> 9;
1496 if (isExtended(chan_no)) {
1497 if (getFirstOfPairNum(chan_no) == chan_no) {
1501 YMF262Channel& ch0 = getFirstOfPair(chan_no);
1502 YMF262Channel& ch3 = getSecondOfPair(chan_no);
1503 ch0.slot[MOD].TLL = ch0.slot[MOD].TL + (ch.ksl_base >> ch0.slot[MOD].ksl);
1504 ch0.slot[CAR].TLL = ch0.slot[CAR].TL + (ch.ksl_base >> ch0.slot[CAR].ksl);
1505 ch3.slot[MOD].TLL = ch3.slot[MOD].TL + (ch.ksl_base >> ch3.slot[MOD].ksl);
1506 ch3.slot[CAR].TLL = ch3.slot[CAR].TL + (ch.ksl_base >> ch3.slot[CAR].ksl);
1509 ch0.slot[MOD].calc_fc(ch);
1510 ch0.slot[CAR].calc_fc(ch);
1511 ch3.slot[MOD].calc_fc(ch);
1512 ch3.slot[CAR].calc_fc(ch);
1518 ch.slot[MOD].TLL = ch.slot[MOD].TL + (ch.ksl_base >> ch.slot[MOD].ksl);
1519 ch.slot[CAR].TLL = ch.slot[CAR].TL + (ch.ksl_base >> ch.slot[CAR].ksl);
1522 ch.slot[MOD].calc_fc(ch);
1523 ch.slot[CAR].calc_fc(ch);
1530 if ((r & 0xF) > 8) {
1533 unsigned chan_no = (r & 0x0F) + ch_offset;
1534 YMF262Channel& ch = channel[chan_no];
1536 unsigned base = chan_no * 4;
1539 pan[base + 0] = (v & 0x10) ?
unsigned(~0) : 0;
1540 pan[base + 1] = (v & 0x20) ?
unsigned(~0) : 0;
1541 pan[base + 2] = (v & 0x40) ?
unsigned(~0) : 0;
1542 pan[base + 3] = (v & 0x80) ?
unsigned(~0) : 0;
1545 pan[base + 0] = unsigned(~0);
1546 pan[base + 1] = unsigned(~0);
1547 pan[base + 2] = unsigned(~0);
1548 pan[base + 3] = unsigned(~0);
1551 ch.slot[MOD].setFeedbackShift((v >> 1) & 7);
1552 ch.slot[MOD].CON = v & 1;
1554 if (isExtended(chan_no)) {
1555 unsigned chan_no0 = getFirstOfPairNum(chan_no);
1556 unsigned chan_no3 = chan_no0 + 3;
1557 YMF262Channel& ch0 = getFirstOfPair(chan_no);
1558 YMF262Channel& ch3 = getSecondOfPair(chan_no);
1559 switch ((ch0.slot[MOD].CON ? 2:0) | (ch3.slot[MOD].CON ? 1:0)) {
1562 ch0.slot[MOD].connect = &phase_modulation;
1563 ch0.slot[CAR].connect = &phase_modulation2;
1564 ch3.slot[MOD].connect = &phase_modulation;
1565 ch3.slot[CAR].connect = &chanout[chan_no3];
1570 ch0.slot[MOD].connect = &phase_modulation;
1571 ch0.slot[CAR].connect = &chanout[chan_no0];
1572 ch3.slot[MOD].connect = &phase_modulation;
1573 ch3.slot[CAR].connect = &chanout[chan_no3];
1578 ch0.slot[MOD].connect = &chanout[chan_no0];
1579 ch0.slot[CAR].connect = &phase_modulation2;
1580 ch3.slot[MOD].connect = &phase_modulation;
1581 ch3.slot[CAR].connect = &chanout[chan_no3];
1587 ch0.slot[MOD].connect = &chanout[chan_no0];
1588 ch0.slot[CAR].connect = &phase_modulation2;
1589 ch3.slot[MOD].connect = &chanout[chan_no3];
1590 ch3.slot[CAR].connect = &chanout[chan_no3];
1595 ch.slot[MOD].connect = ch.slot[MOD].CON
1597 : &phase_modulation;
1598 ch.slot[CAR].connect = &chanout[chan_no];
1604 int slot = slot_array[r & 0x1f];
1605 if (slot < 0)
return;
1606 slot += ch_offset * 2;
1607 YMF262Channel& ch = channel[slot / 2];
1616 ch.slot[slot & 1].wavetable = &sin_tab[v * SIN_LEN];
1629 alreadySignaledNEW2 =
false;
1633 writeRegDirect(0x01, 0, time);
1634 writeRegDirect(0x02, 0, time);
1635 writeRegDirect(0x03, 0, time);
1636 writeRegDirect(0x04, 0, time);
1640 for (
int c = 0xFF; c >= 0x20; c--) {
1641 writeRegDirect(c, 0, time);
1644 for (
int c = 0x1FF; c >= 0x120; c--) {
1645 writeRegDirect(c, 0, time);
1649 for (
int c = 0; c < 9 * 2; c++) {
1651 for (
int s = 0; s < 2; s++) {
1663 config.getMotherBoard(), self,
getName()))
1665 ?
EmuTimer::createOPL4_1(config.getScheduler(), *this)
1666 :
EmuTimer::createOPL3_1(config.getScheduler(), *this))
1668 ?
EmuTimer::createOPL4_2(config.getScheduler(), *this)
1669 :
EmuTimer::createOPL3_2(config.getScheduler(), *this))
1670 , irq(config.getMotherBoard(),
getName() +
".IRQ")
1671 , lfo_am_cnt(0), lfo_pm_cnt(0)
1672 , isYMF278(isYMF278_)
1674 lfo_am_depth =
false;
1675 lfo_pm_depth_range = 0;
1678 status = status2 = statusMask = 0;
1681 memset(chanout, 0,
sizeof(chanout));
1682 memset(reg, 0,
sizeof(reg));
1686 double input = isYMF278
1687 ? 33868800.0 / (19 * 36)
1688 : 4 * 3579545.0 / ( 8 * 36);
1703 byte result = status | status2;
1710 return status | status2;
1713 bool YMF262::Impl::checkMuteHelper()
1716 for (
int i = 0; i < 18; i++) {
1717 for (
int j = 0; j < 2; j++) {
1719 if (!((sl.
state == EG_OFF) ||
1721 ((sl.
TLL + sl.
volume) >= ENV_QUIET)))) {
1729 int YMF262::Impl::getAmplificationFactor()
const
1734 void YMF262::Impl::generateChannels(
int** bufs,
unsigned num)
1738 if (checkMuteHelper()) {
1740 for (
int i = 0; i < 18; ++i) {
1746 bool rhythmEnabled = (rhythm & 0x20) != 0;
1748 for (
unsigned j = 0; j < num; ++j) {
1752 lfo_am_cnt.addQuantum();
1753 if (lfo_am_cnt == LFOAMIndex(LFO_AM_TAB_ELEMENTS)) {
1755 lfo_am_cnt = LFOAMIndex(0);
1757 unsigned tmp = lfo_am_table[lfo_am_cnt.toInt()];
1758 unsigned lfo_am = lfo_am_depth ? tmp : tmp / 4;
1761 memset(chanout, 0,
sizeof(chanout));
1765 for (
int k = 0; k <= 9; k += 9) {
1766 for (
int i = 0; i < 3; ++i) {
1767 YMF262Channel& ch0 = channel[k + i + 0];
1768 YMF262Channel& ch3 = channel[k + i + 3];
1770 ch0.chan_calc(lfo_am);
1773 ch3.chan_calc_ext(lfo_am);
1776 ch3.chan_calc(lfo_am);
1782 if (!rhythmEnabled) {
1783 channel[6].chan_calc(lfo_am);
1784 channel[7].chan_calc(lfo_am);
1785 channel[8].chan_calc(lfo_am);
1788 chan_calc_rhythm(lfo_am);
1792 channel[15].chan_calc(lfo_am);
1793 channel[16].chan_calc(lfo_am);
1794 channel[17].chan_calc(lfo_am);
1796 for (
int i = 0; i < 18; ++i) {
1797 bufs[i][2 * j + 0] += chanout[i] & pan[4 * i + 0];
1798 bufs[i][2 * j + 1] += chanout[i] & pan[4 * i + 1];
1808 static enum_string<EnvelopeState> envelopeStateInfo[]= {
1817 template<
typename Archive>
1821 unsigned waveform = unsigned((wavetable - sin_tab) / SIN_LEN);
1822 ar.serialize(
"waveform", waveform);
1823 if (ar.isLoader()) {
1824 wavetable = &sin_tab[waveform * SIN_LEN];
1831 ar.serialize(
"Cnt", Cnt);
1832 ar.serialize(
"Incr", Incr);
1833 ar.serialize(
"op1_out", op1_out);
1834 ar.serialize(
"TL", TL);
1835 ar.serialize(
"TLL", TLL);
1836 ar.serialize(
"volume", volume);
1837 ar.serialize(
"sl", sl);
1838 ar.serialize(
"state", state);
1839 ar.serialize(
"eg_m_ar", eg_m_ar);
1840 ar.serialize(
"eg_m_dr", eg_m_dr);
1841 ar.serialize(
"eg_m_rr", eg_m_rr);
1842 ar.serialize(
"eg_sh_ar", eg_sh_ar);
1843 ar.serialize(
"eg_sel_ar", eg_sel_ar);
1844 ar.serialize(
"eg_sh_dr", eg_sh_dr);
1845 ar.serialize(
"eg_sel_dr", eg_sel_dr);
1846 ar.serialize(
"eg_sh_rr", eg_sh_rr);
1847 ar.serialize(
"eg_sel_rr", eg_sel_rr);
1848 ar.serialize(
"key", key);
1849 ar.serialize(
"eg_type", eg_type);
1850 ar.serialize(
"AMmask", AMmask);
1851 ar.serialize(
"vib", vib);
1852 ar.serialize(
"ar", this->ar);
1853 ar.serialize(
"dr", dr);
1854 ar.serialize(
"rr", rr);
1855 ar.serialize(
"KSR", KSR);
1856 ar.serialize(
"ksl", ksl);
1857 ar.serialize(
"ksr", ksr);
1858 ar.serialize(
"mul", mul);
1861 template<
typename Archive>
1864 ar.serialize(
"slots", slot);
1865 ar.serialize(
"block_fnum", block_fnum);
1866 ar.serialize(
"fc", fc);
1867 ar.serialize(
"ksl_base", ksl_base);
1868 ar.serialize(
"kcode", kcode);
1869 ar.serialize(
"extended", extended);
1874 template<
typename Archive>
1877 ar.serialize(
"timer1", *timer1);
1878 ar.serialize(
"timer2", *timer2);
1879 ar.serialize(
"irq", irq);
1880 ar.serialize(
"chanout", chanout);
1881 ar.serialize_blob(
"registers", reg,
sizeof(reg));
1882 ar.serialize(
"channels", channel);
1883 ar.serialize(
"eg_cnt", eg_cnt);
1884 ar.serialize(
"noise_rng", noise_rng);
1885 ar.serialize(
"lfo_am_cnt", lfo_am_cnt);
1886 ar.serialize(
"lfo_pm_cnt", lfo_pm_cnt);
1887 ar.serialize(
"lfo_am_depth", lfo_am_depth);
1888 ar.serialize(
"lfo_pm_depth_range", lfo_pm_depth_range);
1889 ar.serialize(
"rhythm", rhythm);
1890 ar.serialize(
"nts", nts);
1891 ar.serialize(
"OPL3_mode", OPL3_mode);
1892 ar.serialize(
"status", status);
1893 ar.serialize(
"status2", status2);
1894 ar.serialize(
"statusMask", statusMask);
1895 if (ar.versionAtLeast(version, 2)) {
1896 ar.serialize(
"alreadySignaledNEW2", alreadySignaledNEW2);
1902 for (
int i = 0xC0; i <= 0xC8; ++i) {
1903 writeRegDirect(i + 0x000, reg[i + 0x000], time);
1904 writeRegDirect(i + 0x100, reg[i + 0x100], time);
1912 const std::string& name)
1914 "MoonSound FM-part registers", 0x200)
1921 return ymf262.
peekReg(address);
1947 pimpl->writeReg(r, v, time);
1952 pimpl->writeReg512(r, v, time);
1957 return pimpl->readReg(reg);
1962 return pimpl->peekReg(reg);
1967 return pimpl->readStatus();
1972 return pimpl->peekStatus();
1975 template<
typename Archive>
1978 pimpl->serialize(ar, version);