GSaha567/seq_level_training_data
052
1text,length,is_long_context,metric_val,label_metric2"//3// Copyright (C) 2013-2016 Alexey Khokholov (Nuke.YKT)4//5// This program is free software; you can redistribute it and/or6// modify it under the terms of the GNU General Public License7// as published by the Free Software Foundation; either version 28// of the License, or (at your option) any later version.9//10// This program is distributed in the hope that it will be useful,11// but WITHOUT ANY WARRANTY; without even the implied warranty of12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13// GNU General Public License for more details.14//15//16// Nuked OPL3 emulator.17// Thanks:18// MAME Development Team(Jarek Burczynski, Tatsuyuki Satoh):19// Feedback and Rhythm part calculation information.20// forums.submarine.org.uk(carbon14, opl3):21// Tremolo and phase generator calculation information.22// OPLx decapsulated(Matthew Gambrell, Olli Niemitalo):23// OPL2 ROMs.24//25// version: 1.7.426//27 28#include <stdio.h>29#include <stdlib.h>30#include <string.h>31#include ""opl3.h""32 33#define RSM_FRAC 1034 35// Channel types36 37enum {38 ch_2op = 0,39 ch_4op = 1,40 ch_4op2 = 2,41 ch_drum = 342};43 44// Envelope key types45 46enum {47 egk_norm = 0x01,48 egk_drum = 0x0249};50 51 52//53// logsin table54//55 56static const Bit16u logsinrom[256] = {57 0x859, 0x6c3, 0x607, 0x58b, 0x52e, 0x4e4, 0x4a6, 0x471,58 0x443, 0x41a, 0x3f5, 0x3d3, 0x3b5, 0x398, 0x37e, 0x365,59 0x34e, 0x339, 0x324, 0x311, 0x2ff, 0x2ed, 0x2dc, 0x2cd,60 0x2bd, 0x2af, 0x2a0, 0x293, 0x286, 0x279, 0x26d, 0x261,61 0x256, 0x24b, 0x240, 0x236, 0x22c, 0x222, 0x218, 0x20f,62 0x206, 0x1fd, 0x1f5, 0x1ec, 0x1e4, 0x1dc, 0x1d4, 0x1cd,63 0x1c5, 0x1be, 0x1b7, 0x1b0, 0x1a9, 0x1a2, 0x19b, 0x195,64 0x18f, 0x188, 0x182, 0x17c, 0x177, 0x171, 0x16b, 0x166,65 0x160, 0x15b, 0x155, 0x150, 0x14b, 0x146, 0x141, 0x13c,66 0x137, 0x133, 0x12e, 0x129, 0x125, 0x121, 0x11c, 0x118,67 0x114, 0x10f, 0x10b, 0x107, 0x103, 0x0ff, 0x0fb, 0x0f8,68 0x0f4, 0x0f0, 0x0ec, 0x0e9, 0x0e5, 0x0e2, 0x0de, 0x0db,69 0x0d7, 0x0d4, 0x0d1, 0x0cd, 0x0ca, 0x0c7, 0x0c4, 0x0c1,70 0x0be, 0x0bb, 0x0b8, 0x0b5, 0x0b2, 0x0af, 0x0ac, 0x0a9,71 0x0a7, 0x0a4, 0x0a1, 0x09f, 0x09c, 0x099, 0x097, 0x094,72 0x092, 0x08f, 0x08d, 0x08a, 0x088, 0x086, 0x083, 0x081,73 0x07f, 0x07d, 0x07a, 0x078, 0x076, 0x074, 0x072, 0x070,74 0x06e, 0x06c, 0x06a, 0x068, 0x066, 0x064, 0x062, 0x060,75 0x05e, 0x05c, 0x05b, 0x059, 0x057, 0x055, 0x053, 0x052,76 0x050, 0x04e, 0x04d, 0x04b, 0x04a, 0x048, 0x046, 0x045,77 0x043, 0x042, 0x040, 0x03f, 0x03e, 0x03c, 0x03b, 0x039,78 0x038, 0x037, 0x035, 0x034, 0x033, 0x031, 0x030, 0x02f,79 0x02e, 0x02d, 0x02b, 0x02a, 0x029, 0x028, 0x027, 0x026,80 0x025, 0x024, 0x023, 0x022, 0x021, 0x020, 0x01f, 0x01e,81 0x01d, 0x01c, 0x01b, 0x01a, 0x019, 0x018, 0x017, 0x017,82 0x016, 0x015, 0x014, 0x014, 0x013, 0x012, 0x011, 0x011,83 0x010, 0x00f, 0x00f, 0x00e, 0x00d, 0x00d, 0x00c, 0x00c,84 0x00b, 0x00a, 0x00a, 0x009, 0x009, 0x008, 0x008, 0x007,85 0x007, 0x007, 0x006, 0x006, 0x005, 0x005, 0x005, 0x004,86 0x004, 0x004, 0x003, 0x003, 0x003, 0x002, 0x002, 0x002,87 0x002, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,88 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x00089};90 91//92// exp table93//94 95static const Bit16u exprom[256] = {96 0x000, 0x003, 0x006, 0x008, 0x00b, 0x00e, 0x011, 0x014,97 0x016, 0x019, 0x01c, 0x01f, 0x022, 0x025, 0x028, 0x02a,98 0x02d, 0x030, 0x033, 0x036, 0x039, 0x03c, 0x03f, 0x042,99 0x045, 0x048, 0x04b, 0x04e, 0x051, 0x054, 0x057, 0x05a,100 0x05d, 0x060, 0x063, 0x066, 0x069, 0x06c, 0x06f, 0x072,101 0x075, 0x078, 0x07b, 0x07e, 0x082, 0x085, 0x088, 0x08b,102 0x08e, 0x091, 0x094, 0x098, 0x09b, 0x09e, 0x0a1, 0x0a4,103 0x0a8, 0x0ab, 0x0ae, 0x0b1, 0x0b5, 0x0b8, 0x0bb, 0x0be,104 0x0c2, 0x0c5, 0x0c8, 0x0cc, 0x0cf, 0x0d2, 0x0d6, 0x0d9,105 0x0dc, 0x0e0, 0x0e3, 0x0e7, 0x0ea, 0x0ed, 0x0f1, 0x0f4,106 0x0f8, 0x0fb, 0x0ff, 0x102, 0x106, 0x109, 0x10c, 0x110,107 0x114, 0x117, 0x11b, 0x11e, 0x122, 0x125, 0x129, 0x12c,108 0x130, 0x134, 0x137, 0x13b, 0x13e, 0x142, 0x146, 0x149,109 0x14d, 0x151, 0x154, 0x158, 0x15c, 0x160, 0x163, 0x167,110 0x16b, 0x16f, 0x172, 0x176, 0x17a, 0x17e, 0x181, 0x185,111 0x189, 0x18d, 0x191, 0x195, 0x199, 0x19c, 0x1a0, 0x1a4,112 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc, 0x1c0, 0x1c4,113 0x1c8, 0x1cc, 0x1d0, 0x1d4, 0x1d8, 0x1dc, 0x1e0, 0x1e4,114 0x1e8, 0x1ec, 0x1f0, 0x1f5, 0x1f9, 0x1fd, 0x201, 0x205,115 0x209, 0x20e, 0x212, 0x216, 0x21a, 0x21e, 0x223, 0x227,116 0x22b, 0x230, 0x234, 0x238, 0x23c, 0x241, 0x245, 0x249,117 0x24e, 0x252, 0x257, 0x25b, 0x25f, 0x264, 0x268, 0x26d,118 0x271, 0x276, 0x27a, 0x27f, 0x283, 0x288, 0x28c, 0x291,119 0x295, 0x29a, 0x29e, 0x2a3, 0x2a8, 0x2ac, 0x2b1, 0x2b5,120 0x2ba, 0x2bf, 0x2c4, 0x2c8, 0x2cd, 0x2d2, 0x2d6, 0x2db,121 0x2e0, 0x2e5, 0x2e9, 0x2ee, 0x2f3, 0x2f8, 0x2fd, 0x302,122 0x306, 0x30b, 0x310, 0x315, 0x31a, 0x31f, 0x324, 0x329,123 0x32e, 0x333, 0x338, 0x33d, 0x342, 0x347, 0x34c, 0x351,124 0x356, 0x35b, 0x360, 0x365, 0x36a, 0x370, 0x375, 0x37a,125 0x37f, 0x384, 0x38a, 0x38f, 0x394, 0x399, 0x39f, 0x3a4,126 0x3a9, 0x3ae, 0x3b4, 0x3b9, 0x3bf, 0x3c4, 0x3c9, 0x3cf,127 0x3d4, 0x3da, 0x3df, 0x3e4, 0x3ea, 0x3ef, 0x3f5, 0x3fa128};129 130//131// freq mult table multiplied by 2132//133// 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 12, 12, 15, 15134//135 136static const Bit8u mt[16] = {137 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 24, 24, 30, 30138};139 140//141// ksl table142//143 144static const Bit8u kslrom[16] = {145 0, 32, 40, 45, 48, 51, 53, 55, 56, 58, 59, 60, 61, 62, 63, 64146};147 148static const Bit8u kslshift[4] = {149 8, 1, 2, 0150};151 152//153// envelope generator constants154//155 156static const Bit8u eg_incstep[3][4][8] = {157 {158 { 0, 0, 0, 0, 0, 0, 0, 0 },159 { 0, 0, 0, 0, 0, 0, 0, 0 },160 { 0, 0, 0, 0, 0, 0, 0, 0 },161 { 0, 0, 0, 0, 0, 0, 0, 0 }162 },163 {164 { 0, 1, 0, 1, 0, 1, 0, 1 },165 { 0, 1, 0, 1, 1, 1, 0, 1 },166 { 0, 1, 1, 1, 0, 1, 1, 1 },167 { 0, 1, 1, 1, 1, 1, 1, 1 }168 },169 {170 { 1, 1, 1, 1, 1, 1, 1, 1 },171 { 2, 2, 1, 1, 1, 1, 1, 1 },172 { 2, 2, 1, 1, 2, 2, 1, 1 },173 { 2, 2, 2, 2, 2, 2, 1, 1 }174 }175};176 177static const Bit8u eg_incdesc[16] = {178 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2179};180 181static const Bit8s eg_incsh[16] = {182 0, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, -1, -2183};184 185//186// address decoding187//188 189static const Bit8s ad_slot[0x20] = {190 0, 1, 2, 3, 4, 5, -1, -1, 6, 7, 8, 9, 10, 11, -1, -1,191 12, 13, 14, 15, 16, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1192};193 194static const Bit8u ch_slot[18] = {195 0, 1, 2, 6, 7, 8, 12, 13, 14, 18, 19, 20, 24, 25, 26, 30, 31, 32196};197 198//199// Envelope generator200//201 202typedef Bit16s(*envelope_sinfunc)(Bit16u phase, Bit16u envelope);203typedef void(*envelope_genfunc)(opl3_slot *slott);204 205static Bit16s OPL3_EnvelopeCalcExp(Bit32u level)206{207 if (level > 0x1fff)208 {209 level = 0x1fff;210 }211 return ((exprom[(level & 0xff) ^ 0xff] | 0x400) << 1) >> (level >> 8);212}213 214static Bit16s OPL3_EnvelopeCalcSin0(Bit16u phase, Bit16u envelope)215{216 Bit16u out = 0;217 Bit16u neg = 0;218 phase &= 0x3ff;219 if (phase & 0x200)220 {221 neg = ~0;222 }223 if (phase & 0x100)224 {225 out = logsinrom[(phase & 0xff) ^ 0xff];226 }227 else228 {229 out = logsinrom[phase & 0xff];230 }231 return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg;232}233 234static Bit16s OPL3_EnvelopeCalcSin1(Bit16u phase, Bit16u envelope)235{236 Bit16u out = 0;237 phase &= 0x3ff;238 if (phase & 0x200)239 {240 out = 0x1000;241 }242 else if (phase & 0x100)243 {244 out = logsinrom[(phase & 0xff) ^ 0xff];245 }246 else247 {248 out = logsinrom[phase & 0xff];249 }250 return OPL3_EnvelopeCalcExp(out + (envelope << 3));251}252 253static Bit16s OPL3_EnvelopeCalcSin2(Bit16u phase, Bit16u envelope)254{255 Bit16u out = 0;256 phase &= 0x3ff;257 if (phase & 0x100)258 {259 out = logsinrom[(phase & 0xff) ^ 0xff];260 }261 else262 {263 out = logsinrom[phase & 0xff];264 }265 return OPL3_EnvelopeCalcExp(out + (envelope << 3));266}267 268static Bit16s OPL3_EnvelopeCalcSin3(Bit16u phase, Bit16u envelope)269{270 Bit16u out = 0;271 phase &= 0x3ff;272 if (phase & 0x100)273 {274 out = 0x1000;275 }276 else277 {278 out = logsinrom[phase & 0xff];279 }280 return OPL3_EnvelopeCalcExp(out + (envelope << 3));281}282 283static Bit16s OPL3_EnvelopeCalcSin4(Bit16u phase, Bit16u envelope)284{285 Bit16u out = 0;286 Bit16u neg = 0;287 phase &= 0x3ff;288 if ((phase & 0x300) == 0x100)289 {290 neg = ~0;291 }292 if (phase & 0x200)293 {294 out = 0x1000;295 }296 else if (phase & 0x80)297 {298 out = logsinrom[((phase ^ 0xff) << 1) & 0xff];299 }300 else301 {302 out = logsinrom[(phase << 1) & 0xff];303 }304 return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg;305}306 307static Bit16s OPL3_EnvelopeCalcSin5(Bit16u phase, Bit16u envelope)308{309 Bit16u out = 0;310 phase &= 0x3ff;311 if (phase & 0x200)312 {313 out = 0x1000;314 }315 else if (phase & 0x80)316 {317 out = logsinrom[((phase ^ 0xff) << 1) & 0xff];318 }319 else320 {321 out = logsinrom[(phase << 1) & 0xff];322 }323 return OPL3_EnvelopeCalcExp(out + (envelope << 3));324}325 326static Bit16s OPL3_EnvelopeCalcSin6(Bit16u phase, Bit16u envelope)327{328 Bit16u neg = 0;329 phase &= 0x3ff;330 if (phase & 0x200)331 {332 neg = ~0;333 }334 return OPL3_EnvelopeCalcExp(envelope << 3) ^ neg;335}336 337static Bit16s OPL3_EnvelopeCalcSin7(Bit16u phase, Bit16u envelope)338{339 Bit16u out = 0;340 Bit16u neg = 0;341 phase &= 0x3ff;342 if (phase & 0x200)343 {344 neg = ~0;345 phase = (phase & 0x1ff) ^ 0x1ff;346 }347 out = phase << 3;348 return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg;349}350 351static const envelope_sinfunc envelope_sin[8] = {352 OPL3_EnvelopeCalcSin0,353 OPL3_EnvelopeCalcSin1,354 OPL3_EnvelopeCalcSin2,355 OPL3_EnvelopeCalcSin3,356 OPL3_EnvelopeCalcSin4,357 OPL3_EnvelopeCalcSin5,358 OPL3_EnvelopeCalcSin6,359 OPL3_EnvelopeCalcSin7360};361 362static void OPL3_EnvelopeGenOff(opl3_slot *slot);363static void OPL3_EnvelopeGenAttack(opl3_slot *slot);364static void OPL3_EnvelopeGenDecay(opl3_slot *slot);365static void OPL3_EnvelopeGenSustain(opl3_slot *slot);366static void OPL3_EnvelopeGenRelease(opl3_slot *slot);367 368envelope_genfunc envelope_gen[5] = {369 OPL3_EnvelopeGenOff,370 OPL3_EnvelopeGenAttack,371 OPL3_EnvelopeGenDecay,372 OPL3_EnvelopeGenSustain,373 OPL3_EnvelopeGenRelease374};375 376enum envelope_gen_num377{378 envelope_gen_num_off = 0,379 envelope_gen_num_attack = 1,380 envelope_gen_num_decay = 2,381 envelope_gen_num_sustain = 3,382 envelope_gen_num_release = 4383};384 385static Bit8u OPL3_EnvelopeCalcRate(opl3_slot *slot, Bit8u reg_rate)386{387 Bit8u rate;388 if (reg_rate == 0x00)389 {390 return 0x00;391 }392 rate = (reg_rate << 2)393 + (slot->reg_ksr ? slot->channel->ksv : (slot->channel->ksv >> 2));394 if (rate > 0x3c)395 {396 rate = 0x3c;397 }398 return rate;399}400 401static void OPL3_EnvelopeUpdateKSL(opl3_slot *slot)402{403 Bit16s ksl = (kslrom[slot->channel->f_num >> 6] << 2)404 - ((0x08 - slot->channel->block) << 5);405 if (ksl < 0)406 {407 ksl = 0;408 }409 slot->eg_ksl = (Bit8u)ksl;410}411 412static void OPL3_EnvelopeUpdateRate(opl3_slot *slot)413{414 switch (slot->eg_gen)415 {416 case envelope_gen_num_off:417 case envelope_gen_num_attack:418 slot->eg_rate = OPL3_EnvelopeCalcRate(slot, slot->reg_ar);419 break;420 case envelope_gen_num_decay:421 slot->eg_rate = OPL3_EnvelopeCalcRate(slot, slot->reg_dr);422 break;423 case envelope_gen_num_sustain:424 case envelope_gen_num_release:425 slot->eg_rate = OPL3_EnvelopeCalcRate(slot, slot->reg_rr);426 break;427 }428}429 430static void OPL3_EnvelopeGenOff(opl3_slot *slot)431{432 slot->eg_rout = 0x1ff;433}434 435static void OPL3_EnvelopeGenAttack(opl3_slot *slot)436{437 if (slot->eg_rout == 0x00)438 {439 slot->eg_gen = envelope_gen_num_decay;440 OPL3_EnvelopeUpdateRate(slot);441 return;442 }443 slot->eg_rout += ((~slot->eg_rout) * slot->eg_inc) >> 3;444 if (slot->eg_rout < 0x00)445 {446 slot->eg_rout = 0x00;447 }448}449 450static void OPL3_EnvelopeGenDecay(opl3_slot *slot)451{452 if (slot->eg_rout >= slot->reg_sl << 4)453 {454 slot->eg_gen = envelope_gen_num_sustain;455 OPL3_EnvelopeUpdateRate(slot);456 return;457 }458 slot->eg_rout += slot->eg_inc;459}460 461static void OPL3_EnvelopeGenSustain(opl3_slot *slot)462{463 if (!slot->reg_type)464 {465 OPL3_EnvelopeGenRelease(slot);466 }467}468 469static void OPL3_EnvelopeGenRelease(opl3_slot *slot)470{471 if (slot->eg_rout >= 0x1ff)472 {473 slot->eg_gen = envelope_gen_num_off;474 slot->eg_rout = 0x1ff;475 OPL3_EnvelopeUpdateRate(slot);476 return;477 }478 slot->eg_rout += slot->eg_inc;479}480 481static void OPL3_EnvelopeCalc(opl3_slot *slot)482{483 Bit8u rate_h, rate_l;484 Bit8u inc = 0;485 rate_h = slot->eg_rate >> 2;486 rate_l = slot->eg_rate & 3;487 if (eg_incsh[rate_h] > 0)488 {489 if ((slot->chip->timer & ((1 << eg_incsh[rate_h]) - 1)) == 0)490 {491 inc = eg_incstep[eg_incdesc[rate_h]][rate_l]492 [((slot->chip->timer) >> eg_incsh[rate_h]) & 0x07];493 }494 }495 else496 {497 inc = eg_incstep[eg_incdesc[rate_h]][rate_l]498 [slot->chip->timer & 0x07] << (-eg_incsh[rate_h]);499 }500 slot->eg_inc = inc;501 slot->eg_out = slot->eg_rout + (slot->reg_tl << 2)502 + (slot->eg_ksl >> kslshift[slot->reg_ksl]) + *slot->trem;503 envelope_gen[slot->eg_gen](slot);504}505 506static void OPL3_EnvelopeKeyOn(opl3_slot *slot, Bit8u type)507{508 if (!slot->key)509 {510 slot->eg_gen = envelope_gen_num_attack;511 OPL3_EnvelopeUpdateRate(slot);512 if ((slot->eg_rate >> 2) == 0x0f)513 {514 slot->eg_gen = envelope_gen_num_decay;515 OPL3_EnvelopeUpdateRate(slot);516 slot->eg_rout = 0x00;517 }518 slot->pg_phase = 0x00;519 }520 slot->key |= type;521}522 523static void OPL3_EnvelopeKeyOff(opl3_slot *slot, Bit8u type)524{525 if (slot->key)526 {527 slot->key &= (~type);528 if (!slot->key)529 {530 slot->eg_gen = envelope_gen_num_release;531 OPL3_EnvelopeUpdateRate(slot);532 }533 }534}535 536//537// Phase Generator538//539 540static void OPL3_PhaseGenerate(opl3_slot *slot)541{542 Bit16u f_num;543 Bit32u basefreq;544 545 f_num = slot->channel->f_num;546 if (slot->reg_vib)547 {548 Bit8s range;549 Bit8u vibpos;550 551 range = (f_num >> 7) & 7;552 vibpos = slot->chip->vibpos;553 554 if (!(vibpos & 3))555 {556 range = 0;557 }558 else if (vibpos & 1)559 {560 range >>= 1;561 }562 range >>= slot->chip->vibshift;563 564 if (vibpos & 4)565 {566 range = -range;567 }568 f_num += range;569 }570 basefreq = (f_num << slot->channel->block) >> 1;571 slot->pg_phase += (basefreq * mt[slot->reg_mult]) >> 1;572}573 574//575// Noise Generator576//577 578static void OPL3_NoiseGenerate(opl3_chip *chip)579{580 if (chip->noise & 0x01)581 {582 chip->noise ^= 0x800302;583 }584 chip->noise >>= 1;585}586 587//588// Slot589//590 591static void OPL3_SlotWrite20(opl3_slot *slot, Bit8u data)592{593 if ((data >> 7) & 0x01)594 {595 slot->trem = &slot->chip->tremolo;596 }597 else598 {599 slot->trem = (Bit8u*)&slot->chip->zeromod;600 }601 slot->reg_vib = (data >> 6) & 0x01;602 slot->reg_type = (data >> 5) & 0x01;603 slot->reg_ksr = (data >> 4) & 0x01;604 slot->reg_mult = data & 0x0f;605 OPL3_EnvelopeUpdateRate(slot);606}607 608static void OPL3_SlotWrite40(opl3_slot *slot, Bit8u data)609{610 slot->reg_ksl = (data >> 6) & 0x03;611 slot->reg_tl = data & 0x3f;612 OPL3_EnvelopeUpdateKSL(slot);613}614 615static void OPL3_SlotWrite60(opl3_slot *slot, Bit8u data)616{617 slot->reg_ar = (data >> 4) & 0x0f;618 slot->reg_dr = data & 0x0f;619 OPL3_EnvelopeUpdateRate(slot);620}621 622static void OPL3_SlotWrite80(opl3_slot *slot, Bit8u data)623{624 slot->reg_sl = (data >> 4) & 0x0f;625 if (slot->reg_sl == 0x0f)626 {627 slot->reg_sl = 0x1f;628 }629 slot->reg_rr = data & 0x0f;630 OPL3_EnvelopeUpdateRate(slot);631}632 633static void OPL3_SlotWriteE0(opl3_slot *slot, Bit8u data)634{635 slot->reg_wf = data & 0x07;636 if (slot->chip->newm == 0x00)637 {638 slot->reg_wf &= 0x03;639 }640}641 642static void OPL3_SlotGeneratePhase(opl3_slot *slot, Bit16u phase)643{644 slot->out = envelope_sin[slot->reg_wf](phase, slot->eg_out);645}646 647static void OPL3_SlotGenerate(opl3_slot *slot)648{649 OPL3_SlotGeneratePhase(slot, (Bit16u)(slot->pg_phase >> 9) + *slot->mod);650}651 652static void OPL3_SlotGenerateZM(opl3_slot *slot)653{654 OPL3_SlotGeneratePhase(slot, (Bit16u)(slot->pg_phase >> 9));655}656 657static void OPL3_SlotCalcFB(opl3_slot *slot)658{659 if (slot->channel->fb != 0x00)660 {661 slot->fbmod = (slot->prout + slot->out) >> (0x09 - slot->channel->fb);662 }663 else664 {665 slot->fbmod = 0;666 }667 slot->prout = slot->out;668}669 670//671// Channel672//673 674static void OPL3_ChannelSetupAlg(opl3_channel *channel);675 676static void OPL3_ChannelUpdateRhythm(opl3_chip *chip, Bit8u data)677{678 opl3_channel *channel6;679 opl3_channel *channel7;680 opl3_channel *channel8;681 Bit8u chnum;682 683 chip->rhy = data & 0x3f;684 if (chip->rhy & 0x20)685 {686 channel6 = &chip->channel[6];687 channel7 = &chip->channel[7];688 channel8 = &chip->channel[8];689 channel6->out[0] = &channel6->slots[1]->out;690 channel6->out[1] = &channel6->slots[1]->out;691 channel6->out[2] = &chip->zeromod;692 channel6->out[3] = &chip->zeromod;693 channel7->out[0] = &channel7->slots[0]->out;694 channel7->out[1] = &channel7->slots[0]->out;695 channel7->out[2] = &channel7->slots[1]->out;696 channel7->out[3] = &channel7->slots[1]->out;697 channel8->out[0] = &channel8->slots[0]->out;698 channel8->out[1] = &channel8->slots[0]->out;699 channel8->out[2] = &channel8->slots[1]->out;700 channel8->out[3] = &channel8->slots[1]->out;701 for (chnum = 6; chnum < 9; chnum++)702 {703 chip->channel[chnum].chtype = ch_drum;704 }705 OPL3_ChannelSetupAlg(channel6);706 //hh707 if (chip->rhy & 0x01)708 {709 OPL3_EnvelopeKeyOn(channel7->slots[0], egk_drum);710 }711 else712 {713 OPL3_EnvelopeKeyOff(channel7->slots[0], egk_drum);714 }715 //tc716 if (chip->rhy & 0x02)717 {718 OPL3_EnvelopeKeyOn(channel8->slots[1], egk_drum);719 }720 else721 {722 OPL3_EnvelopeKeyOff(channel8->slots[1], egk_drum);723 }724 //tom725 if (chip->rhy & 0x04)726 {727 OPL3_EnvelopeKeyOn(channel8->slots[0], egk_drum);728 }729 else730 {731 OPL3_EnvelopeKeyOff(channel8->slots[0], egk_drum);732 }733 //sd734 if (chip->rhy & 0x08)735 {736 OPL3_EnvelopeKeyOn(channel7->slots[1], egk_drum);737 }738 else739 {740 OPL3_EnvelopeKeyOff(channel7->slots[1], egk_drum);741 }742 //bd743 if (chip->rhy & 0x10)744 {745 OPL3_EnvelopeKeyOn(channel6->slots[0], egk_drum);746 OPL3_EnvelopeKeyOn(channel6->slots[1], egk_drum);747 }748 else749 {750 OPL3_EnvelopeKeyOff(channel6->slots[0], egk_drum);751 OPL3_EnvelopeKeyOff(channel6->slots[1], egk_drum);752 }753 }754 else755 {756 for (chnum = 6; chnum < 9; chnum++)757 {758 chip->channel[chnum].chtype = ch_2op;759 OPL3_ChannelSetupAlg(&chip->channel[chnum]);760 OPL3_EnvelopeKeyOff(chip->channel[chnum].slots[0], egk_drum);761 OPL3_EnvelopeKeyOff(chip->channel[chnum].slots[1], egk_drum);762 }763 }764}765 766static void OPL3_ChannelWriteA0(opl3_channel *channel, Bit8u data)767{768 if (channel->chip->newm && channel->chtype == ch_4op2)769 {770 return;771 }772 channel->f_num = (channel->f_num & 0x300) | data;773 channel->ksv = (channel->block << 1)774 | ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01);775 OPL3_EnvelopeUpdateKSL(channel->slots[0]);776 OPL3_EnvelopeUpdateKSL(channel->slots[1]);777 OPL3_EnvelopeUpdateRate(channel->slots[0]);778 OPL3_EnvelopeUpdateRate(channel->slots[1]);779 if (channel->chip->newm && channel->chtype == ch_4op)780 {781 channel->pair->f_num = channel->f_num;782 channel->pair->ksv = channel->ksv;783 OPL3_EnvelopeUpdateKSL(channel->pair->slots[0]);784 OPL3_EnvelopeUpdateKSL(channel->pair->slots[1]);785 OPL3_EnvelopeUpdateRate(channel->pair->slots[0]);786 OPL3_EnvelopeUpdateRate(channel->pair->slots[1]);787 }788}789 790static void OPL3_ChannelWriteB0(opl3_channel *channel, Bit8u data)791{792 if (channel->chip->newm && channel->chtype == ch_4op2)793 {794 return;795 }796 channel->f_num = (channel->f_num & 0xff) | ((data & 0x03) << 8);797 channel->block = (data >> 2) & 0x07;798 channel->ksv = (channel->block << 1)799 | ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01);800 OPL3_EnvelopeUpdateKSL(channel->slots[0]);801 OPL3_EnvelopeUpdateKSL(channel->slots[1]);802 OPL3_EnvelopeUpdateRate(channel->slots[0]);803 OPL3_EnvelopeUpdateRate(channel->slots[1]);804 if (channel->chip->newm && channel->chtype == ch_4op)805 {806 channel->pair->f_num = channel->f_num;807 channel->pair->block = channel->block;808 channel->pair->ksv = channel->ksv;809 OPL3_EnvelopeUpdateKSL(channel->pair->slots[0]);810 OPL3_EnvelopeUpdateKSL(channel->pair->slots[1]);811 OPL3_EnvelopeUpdateRate(channel->pair->slots[0]);812 OPL3_EnvelopeUpdateRate(channel->pair->slots[1]);813 }814}815 816static void OPL3_ChannelSetupAlg(opl3_channel *channel)817{818 if (channel->chtype == ch_drum)819 {820 switch (channel->alg & 0x01)821 {822 case 0x00:823 channel->slots[0]->mod = &channel->slots[0]->fbmod;824 channel->slots[1]->mod = &channel->slots[0]->out;825 break;826 case 0x01:827 channel->slots[0]->mod = &channel->slots[0]->fbmod;828 channel->slots[1]->mod = &channel->chip->zeromod;829 break;830 }831 return;832 }833 if (channel->alg & 0x08)834 {835 return;836 }837 if (channel->alg & 0x04)838 {839 channel->pair->out[0] = &channel->chip->zeromod;840 channel->pair->out[1] = &channel->chip->zeromod;841 channel->pair->out[2] = &channel->chip->zeromod;842 channel->pair->out[3] = &channel->chip->zeromod;843 switch (channel->alg & 0x03)844 {845 case 0x00:846 channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;847 channel->pair->slots[1]->mod = &channel->pair->slots[0]->out;848 channel->slots[0]->mod = &channel->pair->slots[1]->out;849 channel->slots[1]->mod = &channel->slots[0]->out;850 channel->out[0] = &channel->slots[1]->out;851 channel->out[1] = &channel->chip->zeromod;852 channel->out[2] = &channel->chip->zeromod;853 channel->out[3] = &channel->chip->zeromod;854 break;855 case 0x01:856 channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;857 channel->pair->slots[1]->mod = &channel->pair->slots[0]->out;858 channel->slots[0]->mod = &channel->chip->zeromod;859 channel->slots[1]->mod = &channel->slots[0]->out;860 channel->out[0] = &channel->pair->slots[1]->out;861 channel->out[1] = &channel->slots[1]->out;862 channel->out[2] = &channel->chip->zeromod;863 channel->out[3] = &channel->chip->zeromod;864 break;865 case 0x02:866 channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;867 channel->pair->slots[1]->mod = &channel->chip->zeromod;868 channel->slots[0]->mod = &channel->pair->slots[1]->out;869 channel->slots[1]->mod = &channel->slots[0]->out;870 channel->out[0] = &channel->pair->slots[0]->out;871 channel->out[1] = &channel->slots[1]->out;872 channel->out[2] = &channel->chip->zeromod;873 channel->out[3] = &channel->chip->zeromod;874 break;875 case 0x03:876 channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;877 channel->pair->slots[1]->mod = &channel->chip->zeromod;878 channel->slots[0]->mod = &channel->pair->slots[1]->out;879 channel->slots[1]->mod = &channel->chip->zeromod;880 channel->out[0] = &channel->pair->slots[0]->out;881 channel->out[1] = &channel->slots[0]->out;882 channel->out[2] = &channel->slots[1]->out;883 channel->out[3] = &channel->chip->zeromod;884 break;885 }886 }887 else888 {889 switch (channel->alg & 0x01)890 {891 case 0x00:892 channel->slots[0]->mod = &channel->slots[0]->fbmod;893 channel->slots[1]->mod = &channel->slots[0]->out;894 channel->out[0] = &channel->slots[1]->out;895 channel->out[1] = &channel->chip->zeromod;896 channel->out[2] = &channel->chip->zeromod;897 channel->out[3] = &channel->chip->zeromod;898 break;899 case 0x01:900 channel->slots[0]->mod = &channel->slots[0]->fbmod;901 channel->slots[1]->mod = &channel->chip->zeromod;902 channel->out[0] = &channel->slots[0]->out;903 channel->out[1] = &channel->slots[1]->out;904 channel->out[2] = &channel->chip->zeromod;905 channel->out[3] = &channel->chip->zeromod;906 break;907 }908 }909}910 911static void OPL3_ChannelWriteC0(opl3_channel *channel, Bit8u data)912{913 channel->fb = (data & 0x0e) >> 1;914 channel->con = data & 0x01;915 channel->alg = channel->con;916 if (channel->chip->newm)917 {918 if (channel->chtype == ch_4op)919 {920 channel->pair->alg = 0x04 | (channel->con << 1) | (channel->pair->con);921 channel->alg = 0x08;922 OPL3_ChannelSetupAlg(channel->pair);923 }924 else if (channel->chtype == ch_4op2)925 {926 channel->alg = 0x04 | (channel->pair->con << 1) | (channel->con);927 channel->pair->alg = 0x08;928 OPL3_ChannelSetupAlg(channel);929 }930 else931 {932 OPL3_ChannelSetupAlg(channel);933 }934 }935 else936 {937 OPL3_ChannelSetupAlg(channel);938 }939 if (channel->chip->newm)940 {941 channel->cha = ((data >> 4) & 0x01) ? ~0 : 0;942 channel->chb = ((data >> 5) & 0x01) ? ~0 : 0;943 }944 else945 {946 channel->cha = channel->chb = ~0;947 }948}949 950static void OPL3_ChannelKeyOn(opl3_channel *channel)951{952 if (channel->chip->newm)953 {954 if (channel->chtype == ch_4op)955 {956 OPL3_EnvelopeKeyOn(channel->slots[0], egk_norm);957 OPL3_EnvelopeKeyOn(channel->slots[1], egk_norm);958 OPL3_EnvelopeKeyOn(channel->pair->slots[0], egk_norm);959 OPL3_EnvelopeKeyOn(channel->pair->slots[1], egk_norm);960 }961 else if (channel->chtype == ch_2op || channel->chtype == ch_drum)962 {963 OPL3_EnvelopeKeyOn(channel->slots[0], egk_norm);964 OPL3_EnvelopeKeyOn(channel->slots[1], egk_norm);965 }966 }967 else968 {969 OPL3_EnvelopeKeyOn(channel->slots[0], egk_norm);970 OPL3_EnvelopeKeyOn(channel->slots[1], egk_norm);971 }972}973 974static void OPL3_ChannelKeyOff(opl3_channel *channel)975{976 if (channel->chip->newm)977 {978 if (channel->chtype == ch_4op)979 {980 OPL3_EnvelopeKeyOff(channel->slots[0], egk_norm);981 OPL3_EnvelopeKeyOff(channel->slots[1], egk_norm);982 OPL3_EnvelopeKeyOff(channel->pair->slots[0], egk_norm);983 OPL3_EnvelopeKeyOff(channel->pair->slots[1], egk_norm);984 }985 else if (channel->chtype == ch_2op || channel->chtype == ch_drum)986 {987 OPL3_EnvelopeKeyOff(channel->slots[0], egk_norm);988 OPL3_EnvelopeKeyOff(channel->slots[1], egk_norm);989 }990 }991 else992 {993 OPL3_EnvelopeKeyOff(channel->slots[0], egk_norm);994 OPL3_EnvelopeKeyOff(channel->slots[1], egk_norm);995 }996}997 998static void OPL3_ChannelSet4Op(opl3_chip *chip, Bit8u data)999{1000 Bit8u bit;1001 Bit8u chnum;1002 for (bit = 0; bit < 6; bit++)1003 {1004 chnum = bit;1005 if (bit >= 3)1006 {1007 chnum += 9 - 3;1008 }1009 if ((data >> bit) & 0x01)1010 {1011 chip->channel[chnum].chtype = ch_4op;1012 chip->channel[chnum + 3].chtype = ch_4op2;1013 }1014 else1015 {1016 chip->channel[chnum].chtype = ch_2op;1017 chip->channel[chnum + 3].chtype = ch_2op;1018 }1019 }1020}1021 1022static Bit16s OPL3_ClipSample(Bit32s sample)1023{1024 if (sample > 32767)1025 {1026 sample = 32767;1027 }1028 else if (sample < -32768)1029 {1030 sample = -32768;1031 }1032 return (Bit16s)sample;1033}1034 1035static void OPL3_GenerateRhythm1(opl3_chip *chip)1036{1037 opl3_channel *channel6;1038 opl3_channel *channel7;1039 opl3_channel *channel8;1040 Bit16u phase14;1041 Bit16u phase17;1042 Bit16u phase;1043 Bit16u phasebit;1044 1045 channel6 = &chip->channel[6];1046 channel7 = &chip->channel[7];1047 channel8 = &chip->channel[8];1048 OPL3_SlotGenerate(channel6->slots[0]);1049 phase14 = (channel7->slots[0]->pg_phase >> 9) & 0x3ff;1050 phase17 = (channel8->slots[1]->pg_phase >> 9) & 0x3ff;1051 phase = 0x00;1052 //hh tc phase bit1053 phasebit = ((phase14 & 0x08) | (((phase14 >> 5) ^ phase14) & 0x04)1054 | (((phase17 >> 2) ^ phase17) & 0x08)) ? 0x01 : 0x00;1055 //hh1056 phase = (phasebit << 9)1057 | (0x34 << ((phasebit ^ (chip->noise & 0x01)) << 1));1058 OPL3_SlotGeneratePhase(channel7->slots[0], phase);1059 //tt1060 OPL3_SlotGenerateZM(channel8->slots[0]);1061}1062 1063static void OPL3_GenerateRhythm2(opl3_chip *chip)1064{1065 opl3_channel *channel6;1066 opl3_channel *channel7;1067 opl3_channel *channel8;1068 Bit16u phase14;1069 Bit16u phase17;1070 Bit16u phase;1071 Bit16u phasebit;1072 1073 channel6 = &chip->channel[6];1074 channel7 = &chip->channel[7];1075 channel8 = &chip->channel[8];1076 OPL3_SlotGenerate(channel6->slots[1]);1077 phase14 = (channel7->slots[0]->pg_phase >> 9) & 0x3ff;1078 phase17 = (channel8->slots[1]->pg_phase >> 9) & 0x3ff;1079 phase = 0x00;1080 //hh tc phase bit1081 phasebit = ((phase14 & 0x08) | (((phase14 >> 5) ^ phase14) & 0x04)1082 | (((phase17 >> 2) ^ phase17) & 0x08)) ? 0x01 : 0x00;1083 //sd1084 phase = (0x100 << ((phase14 >> 8) & 0x01)) ^ ((chip->noise & 0x01) << 8);1085 OPL3_SlotGeneratePhase(channel7->slots[1], phase);1086 //tc1087 phase = 0x100 | (phasebit << 9);1088 OPL3_SlotGeneratePhase(channel8->slots[1], phase);1089}1090 1091void OPL3_Generate(opl3_chip *chip, Bit16s *buf)1092{1093 Bit8u ii;1094 Bit8u jj;1095 Bit16s accm;1096 1097 buf[1] = OPL3_ClipSample(chip->mixbuff[1]);1098 1099 for (ii = 0; ii < 12; ii++)1100 {1101 OPL3_SlotCalcFB(&chip->slot[ii]);1102 OPL3_PhaseGenerate(&chip->slot[ii]);1103 OPL3_EnvelopeCalc(&chip->slot[ii]);1104 OPL3_SlotGenerate(&chip->slot[ii]);1105 }1106 1107 for (ii = 12; ii < 15; ii++)1108 {1109 OPL3_SlotCalcFB(&chip->slot[ii]);1110 OPL3_PhaseGenerate(&chip->slot[ii]);1111 OPL3_EnvelopeCalc(&chip->slot[ii]);1112 }1113 1114 if (chip->rhy & 0x20)1115 {1116 OPL3_GenerateRhythm1(chip);1117 }1118 else1119 {1120 OPL3_SlotGenerate(&chip->slot[12]);1121 OPL3_SlotGenerate(&chip->slot[13]);1122 OPL3_SlotGenerate(&chip->slot[14]);1123 }1124 1125 chip->mixbuff[0] = 0;1126 for (ii = 0; ii < 18; ii++)1127 {1128 accm = 0;1129 for (jj = 0; jj < 4; jj++)1130 {1131 accm += *chip->channel[ii].out[jj];1132 }1133 chip->mixbuff[0] += (Bit16s)(accm & chip->channel[ii].cha);1134 }1135 1136 for (ii = 15; ii < 18; ii++)1137 {1138 OPL3_SlotCalcFB(&chip->slot[ii]);1139 OPL3_PhaseGenerate(&chip->slot[ii]);1140 OPL3_EnvelopeCalc(&chip->slot[ii]);1141 }1142 1143 if (chip->rhy & 0x20)1144 {1145 OPL3_GenerateRhythm2(chip);1146 }1147 else1148 {1149 OPL3_SlotGenerate(&chip->slot[15]);1150 OPL3_SlotGenerate(&chip->slot[16]);1151 OPL3_SlotGenerate(&chip->slot[17]);1152 }1153 1154 buf[0] = OPL3_ClipSample(chip->mixbuff[0]);1155 1156 for (ii = 18; ii < 33; ii++)1157 {1158 OPL3_SlotCalcFB(&chip->slot[ii]);1159 OPL3_PhaseGenerate(&chip->slot[ii]);1160 OPL3_EnvelopeCalc(&chip->slot[ii]);1161 OPL3_SlotGenerate(&chip->slot[ii]);1162 }1163 1164 chip->mixbuff[1] = 0;1165 for (ii = 0; ii < 18; ii++)1166 {1167 accm = 0;1168 for (jj = 0; jj < 4; jj++)1169 {1170 accm += *chip->channel[ii].out[jj];1171 }1172 chip->mixbuff[1] += (Bit16s)(accm & chip->channel[ii].chb);1173 }1174 1175 for (ii = 33; ii < 36; ii++)1176 {1177 OPL3_SlotCalcFB(&chip->slot[ii]);1178 OPL3_PhaseGenerate(&chip->slot[ii]);1179 OPL3_EnvelopeCalc(&chip->slot[ii]);1180 OPL3_SlotGenerate(&chip->slot[ii]);1181 }1182 1183 OPL3_NoiseGenerate(chip);1184 1185 if ((chip->timer & 0x3f) == 0x3f)1186 {1187 chip->tremolopos = (chip->tremolopos + 1) % 210;1188 }1189 if (chip->tremolopos < 105)1190 {1191 chip->tremolo = chip->tremolopos >> chip->tremoloshift;1192 }1193 else1194 {1195 chip->tremolo = (210 - chip->tremolopos) >> chip->tremoloshift;1196 }1197 1198 if ((chip->timer & 0x3ff) == 0x3ff)1199 {1200 chip->vibpos = (chip->vibpos + 1) & 7;