| 1 |
/* |
| 2 |
* Copyright (C) 2017-2018 Alexey Khokholov (Nuke.YKT) |
| 3 |
* Copyright (C) 2019 Jean Pierre Cimalando |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
* |
| 19 |
* |
| 20 |
* Nuked OPN2-MOD emulator, with OPNA functionality added. |
| 21 |
* Thanks: |
| 22 |
* Silicon Pr0n: |
| 23 |
* Yamaha YM3438 decap and die shot(digshadow). |
| 24 |
* OPLx decapsulated(Matthew Gambrell, Olli Niemitalo): |
| 25 |
* OPL2 ROMs. |
| 26 |
* |
| 27 |
* version: 1.0.9 |
| 28 |
* |
| 29 |
* OPN-MOD additions: |
| 30 |
* - Jean Pierre Cimalando 2019-04-06: add SSG control interface |
| 31 |
* - Jean Pierre Cimalando 2019-04-06: add 6-channel FM flag |
| 32 |
* - Jean Pierre Cimalando 2019-04-06: add ADPCM rhythm channels |
| 33 |
* - Jean Pierre Cimalando 2019-04-07: raise the channel clipping threshold |
| 34 |
* - Jean Pierre Cimalando 2020-02-23: add the ADPCM Delta-T |
| 35 |
*/ |
| 36 |
|
| 37 |
#include <string.h> |
| 38 |
#include <stdlib.h> |
| 39 |
#include "ym3438.h" |
| 40 |
|
| 41 |
/*OPN-MOD: define the quantization in bits at which channels clip*/ |
| 42 |
static const unsigned channel_clipbits = 16; /*YM2612 has 9 bits*/ |
| 43 |
|
| 44 |
enum { |
| 45 |
eg_num_attack = 0, |
| 46 |
eg_num_decay = 1, |
| 47 |
eg_num_sustain = 2, |
| 48 |
eg_num_release = 3 |
| 49 |
}; |
| 50 |
|
| 51 |
/* logsin table */ |
| 52 |
static const Bit16u logsinrom[256] = { |
| 53 |
0x859, 0x6c3, 0x607, 0x58b, 0x52e, 0x4e4, 0x4a6, 0x471, |
| 54 |
0x443, 0x41a, 0x3f5, 0x3d3, 0x3b5, 0x398, 0x37e, 0x365, |
| 55 |
0x34e, 0x339, 0x324, 0x311, 0x2ff, 0x2ed, 0x2dc, 0x2cd, |
| 56 |
0x2bd, 0x2af, 0x2a0, 0x293, 0x286, 0x279, 0x26d, 0x261, |
| 57 |
0x256, 0x24b, 0x240, 0x236, 0x22c, 0x222, 0x218, 0x20f, |
| 58 |
0x206, 0x1fd, 0x1f5, 0x1ec, 0x1e4, 0x1dc, 0x1d4, 0x1cd, |
| 59 |
0x1c5, 0x1be, 0x1b7, 0x1b0, 0x1a9, 0x1a2, 0x19b, 0x195, |
| 60 |
0x18f, 0x188, 0x182, 0x17c, 0x177, 0x171, 0x16b, 0x166, |
| 61 |
0x160, 0x15b, 0x155, 0x150, 0x14b, 0x146, 0x141, 0x13c, |
| 62 |
0x137, 0x133, 0x12e, 0x129, 0x125, 0x121, 0x11c, 0x118, |
| 63 |
0x114, 0x10f, 0x10b, 0x107, 0x103, 0x0ff, 0x0fb, 0x0f8, |
| 64 |
0x0f4, 0x0f0, 0x0ec, 0x0e9, 0x0e5, 0x0e2, 0x0de, 0x0db, |
| 65 |
0x0d7, 0x0d4, 0x0d1, 0x0cd, 0x0ca, 0x0c7, 0x0c4, 0x0c1, |
| 66 |
0x0be, 0x0bb, 0x0b8, 0x0b5, 0x0b2, 0x0af, 0x0ac, 0x0a9, |
| 67 |
0x0a7, 0x0a4, 0x0a1, 0x09f, 0x09c, 0x099, 0x097, 0x094, |
| 68 |
0x092, 0x08f, 0x08d, 0x08a, 0x088, 0x086, 0x083, 0x081, |
| 69 |
0x07f, 0x07d, 0x07a, 0x078, 0x076, 0x074, 0x072, 0x070, |
| 70 |
0x06e, 0x06c, 0x06a, 0x068, 0x066, 0x064, 0x062, 0x060, |
| 71 |
0x05e, 0x05c, 0x05b, 0x059, 0x057, 0x055, 0x053, 0x052, |
| 72 |
0x050, 0x04e, 0x04d, 0x04b, 0x04a, 0x048, 0x046, 0x045, |
| 73 |
0x043, 0x042, 0x040, 0x03f, 0x03e, 0x03c, 0x03b, 0x039, |
| 74 |
0x038, 0x037, 0x035, 0x034, 0x033, 0x031, 0x030, 0x02f, |
| 75 |
0x02e, 0x02d, 0x02b, 0x02a, 0x029, 0x028, 0x027, 0x026, |
| 76 |
0x025, 0x024, 0x023, 0x022, 0x021, 0x020, 0x01f, 0x01e, |
| 77 |
0x01d, 0x01c, 0x01b, 0x01a, 0x019, 0x018, 0x017, 0x017, |
| 78 |
0x016, 0x015, 0x014, 0x014, 0x013, 0x012, 0x011, 0x011, |
| 79 |
0x010, 0x00f, 0x00f, 0x00e, 0x00d, 0x00d, 0x00c, 0x00c, |
| 80 |
0x00b, 0x00a, 0x00a, 0x009, 0x009, 0x008, 0x008, 0x007, |
| 81 |
0x007, 0x007, 0x006, 0x006, 0x005, 0x005, 0x005, 0x004, |
| 82 |
0x004, 0x004, 0x003, 0x003, 0x003, 0x002, 0x002, 0x002, |
| 83 |
0x002, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, |
| 84 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000 |
| 85 |
}; |
| 86 |
|
| 87 |
/* exp table */ |
| 88 |
static const Bit16u exprom[256] = { |
| 89 |
0x000, 0x003, 0x006, 0x008, 0x00b, 0x00e, 0x011, 0x014, |
| 90 |
0x016, 0x019, 0x01c, 0x01f, 0x022, 0x025, 0x028, 0x02a, |
| 91 |
0x02d, 0x030, 0x033, 0x036, 0x039, 0x03c, 0x03f, 0x042, |
| 92 |
0x045, 0x048, 0x04b, 0x04e, 0x051, 0x054, 0x057, 0x05a, |
| 93 |
0x05d, 0x060, 0x063, 0x066, 0x069, 0x06c, 0x06f, 0x072, |
| 94 |
0x075, 0x078, 0x07b, 0x07e, 0x082, 0x085, 0x088, 0x08b, |
| 95 |
0x08e, 0x091, 0x094, 0x098, 0x09b, 0x09e, 0x0a1, 0x0a4, |
| 96 |
0x0a8, 0x0ab, 0x0ae, 0x0b1, 0x0b5, 0x0b8, 0x0bb, 0x0be, |
| 97 |
0x0c2, 0x0c5, 0x0c8, 0x0cc, 0x0cf, 0x0d2, 0x0d6, 0x0d9, |
| 98 |
0x0dc, 0x0e0, 0x0e3, 0x0e7, 0x0ea, 0x0ed, 0x0f1, 0x0f4, |
| 99 |
0x0f8, 0x0fb, 0x0ff, 0x102, 0x106, 0x109, 0x10c, 0x110, |
| 100 |
0x114, 0x117, 0x11b, 0x11e, 0x122, 0x125, 0x129, 0x12c, |
| 101 |
0x130, 0x134, 0x137, 0x13b, 0x13e, 0x142, 0x146, 0x149, |
| 102 |
0x14d, 0x151, 0x154, 0x158, 0x15c, 0x160, 0x163, 0x167, |
| 103 |
0x16b, 0x16f, 0x172, 0x176, 0x17a, 0x17e, 0x181, 0x185, |
| 104 |
0x189, 0x18d, 0x191, 0x195, 0x199, 0x19c, 0x1a0, 0x1a4, |
| 105 |
0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc, 0x1c0, 0x1c4, |
| 106 |
0x1c8, 0x1cc, 0x1d0, 0x1d4, 0x1d8, 0x1dc, 0x1e0, 0x1e4, |
| 107 |
0x1e8, 0x1ec, 0x1f0, 0x1f5, 0x1f9, 0x1fd, 0x201, 0x205, |
| 108 |
0x209, 0x20e, 0x212, 0x216, 0x21a, 0x21e, 0x223, 0x227, |
| 109 |
0x22b, 0x230, 0x234, 0x238, 0x23c, 0x241, 0x245, 0x249, |
| 110 |
0x24e, 0x252, 0x257, 0x25b, 0x25f, 0x264, 0x268, 0x26d, |
| 111 |
0x271, 0x276, 0x27a, 0x27f, 0x283, 0x288, 0x28c, 0x291, |
| 112 |
0x295, 0x29a, 0x29e, 0x2a3, 0x2a8, 0x2ac, 0x2b1, 0x2b5, |
| 113 |
0x2ba, 0x2bf, 0x2c4, 0x2c8, 0x2cd, 0x2d2, 0x2d6, 0x2db, |
| 114 |
0x2e0, 0x2e5, 0x2e9, 0x2ee, 0x2f3, 0x2f8, 0x2fd, 0x302, |
| 115 |
0x306, 0x30b, 0x310, 0x315, 0x31a, 0x31f, 0x324, 0x329, |
| 116 |
0x32e, 0x333, 0x338, 0x33d, 0x342, 0x347, 0x34c, 0x351, |
| 117 |
0x356, 0x35b, 0x360, 0x365, 0x36a, 0x370, 0x375, 0x37a, |
| 118 |
0x37f, 0x384, 0x38a, 0x38f, 0x394, 0x399, 0x39f, 0x3a4, |
| 119 |
0x3a9, 0x3ae, 0x3b4, 0x3b9, 0x3bf, 0x3c4, 0x3c9, 0x3cf, |
| 120 |
0x3d4, 0x3da, 0x3df, 0x3e4, 0x3ea, 0x3ef, 0x3f5, 0x3fa |
| 121 |
}; |
| 122 |
|
| 123 |
/* Note table */ |
| 124 |
static const Bit32u fn_note[16] = { |
| 125 |
0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3 |
| 126 |
}; |
| 127 |
|
| 128 |
/* Envelope generator */ |
| 129 |
static const Bit32u eg_stephi[4][4] = { |
| 130 |
{ 0, 0, 0, 0 }, |
| 131 |
{ 1, 0, 0, 0 }, |
| 132 |
{ 1, 0, 1, 0 }, |
| 133 |
{ 1, 1, 1, 0 } |
| 134 |
}; |
| 135 |
|
| 136 |
static const Bit8u eg_am_shift[4] = { |
| 137 |
7, 3, 1, 0 |
| 138 |
}; |
| 139 |
|
| 140 |
/* Phase generator */ |
| 141 |
static const Bit32u pg_detune[8] = { 16, 17, 19, 20, 22, 24, 27, 29 }; |
| 142 |
|
| 143 |
static const Bit32u pg_lfo_sh1[8][8] = { |
| 144 |
{ 7, 7, 7, 7, 7, 7, 7, 7 }, |
| 145 |
{ 7, 7, 7, 7, 7, 7, 7, 7 }, |
| 146 |
{ 7, 7, 7, 7, 7, 7, 1, 1 }, |
| 147 |
{ 7, 7, 7, 7, 1, 1, 1, 1 }, |
| 148 |
{ 7, 7, 7, 1, 1, 1, 1, 0 }, |
| 149 |
{ 7, 7, 1, 1, 0, 0, 0, 0 }, |
| 150 |
{ 7, 7, 1, 1, 0, 0, 0, 0 }, |
| 151 |
{ 7, 7, 1, 1, 0, 0, 0, 0 } |
| 152 |
}; |
| 153 |
|
| 154 |
static const Bit32u pg_lfo_sh2[8][8] = { |
| 155 |
{ 7, 7, 7, 7, 7, 7, 7, 7 }, |
| 156 |
{ 7, 7, 7, 7, 2, 2, 2, 2 }, |
| 157 |
{ 7, 7, 7, 2, 2, 2, 7, 7 }, |
| 158 |
{ 7, 7, 2, 2, 7, 7, 2, 2 }, |
| 159 |
{ 7, 7, 2, 7, 7, 7, 2, 7 }, |
| 160 |
{ 7, 7, 7, 2, 7, 7, 2, 1 }, |
| 161 |
{ 7, 7, 7, 2, 7, 7, 2, 1 }, |
| 162 |
{ 7, 7, 7, 2, 7, 7, 2, 1 } |
| 163 |
}; |
| 164 |
|
| 165 |
/* Address decoder */ |
| 166 |
static const Bit32u op_offset[12] = { |
| 167 |
0x000, /* Ch1 OP1/OP2 */ |
| 168 |
0x001, /* Ch2 OP1/OP2 */ |
| 169 |
0x002, /* Ch3 OP1/OP2 */ |
| 170 |
0x100, /* Ch4 OP1/OP2 */ |
| 171 |
0x101, /* Ch5 OP1/OP2 */ |
| 172 |
0x102, /* Ch6 OP1/OP2 */ |
| 173 |
0x004, /* Ch1 OP3/OP4 */ |
| 174 |
0x005, /* Ch2 OP3/OP4 */ |
| 175 |
0x006, /* Ch3 OP3/OP4 */ |
| 176 |
0x104, /* Ch4 OP3/OP4 */ |
| 177 |
0x105, /* Ch5 OP3/OP4 */ |
| 178 |
0x106 /* Ch6 OP3/OP4 */ |
| 179 |
}; |
| 180 |
|
| 181 |
static const Bit32u ch_offset[6] = { |
| 182 |
0x000, /* Ch1 */ |
| 183 |
0x001, /* Ch2 */ |
| 184 |
0x002, /* Ch3 */ |
| 185 |
0x100, /* Ch4 */ |
| 186 |
0x101, /* Ch5 */ |
| 187 |
0x102 /* Ch6 */ |
| 188 |
}; |
| 189 |
|
| 190 |
/* LFO */ |
| 191 |
static const Bit32u lfo_cycles[8] = { |
| 192 |
108, 77, 71, 67, 62, 44, 8, 5 |
| 193 |
}; |
| 194 |
|
| 195 |
/* FM algorithm */ |
| 196 |
static const Bit32u fm_algorithm[4][6][8] = { |
| 197 |
{ |
| 198 |
{ 1, 1, 1, 1, 1, 1, 1, 1 }, /* OP1_0 */ |
| 199 |
{ 1, 1, 1, 1, 1, 1, 1, 1 }, /* OP1_1 */ |
| 200 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* OP2 */ |
| 201 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */ |
| 202 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */ |
| 203 |
{ 0, 0, 0, 0, 0, 0, 0, 1 } /* Out */ |
| 204 |
}, |
| 205 |
{ |
| 206 |
{ 0, 1, 0, 0, 0, 1, 0, 0 }, /* OP1_0 */ |
| 207 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* OP1_1 */ |
| 208 |
{ 1, 1, 1, 0, 0, 0, 0, 0 }, /* OP2 */ |
| 209 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */ |
| 210 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */ |
| 211 |
{ 0, 0, 0, 0, 0, 1, 1, 1 } /* Out */ |
| 212 |
}, |
| 213 |
{ |
| 214 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* OP1_0 */ |
| 215 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* OP1_1 */ |
| 216 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* OP2 */ |
| 217 |
{ 1, 0, 0, 1, 1, 1, 1, 0 }, /* Last operator */ |
| 218 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */ |
| 219 |
{ 0, 0, 0, 0, 1, 1, 1, 1 } /* Out */ |
| 220 |
}, |
| 221 |
{ |
| 222 |
{ 0, 0, 1, 0, 0, 1, 0, 0 }, /* OP1_0 */ |
| 223 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* OP1_1 */ |
| 224 |
{ 0, 0, 0, 1, 0, 0, 0, 0 }, /* OP2 */ |
| 225 |
{ 1, 1, 0, 1, 1, 0, 0, 0 }, /* Last operator */ |
| 226 |
{ 0, 0, 1, 0, 0, 0, 0, 0 }, /* Last operator */ |
| 227 |
{ 1, 1, 1, 1, 1, 1, 1, 1 } /* Out */ |
| 228 |
} |
| 229 |
}; |
| 230 |
|
| 231 |
/*OPN-MOD: ADPCM jedi table*/ |
| 232 |
static const Bit16s adpcm_jedi_table[49 * 16] = |
| 233 |
{ |
| 234 |
2, 6, 10, 14, 18, 22, 26, 30, -2, -6, -10, -14, -18, -22, -26, -30, |
| 235 |
2, 6, 10, 14, 19, 23, 27, 31, -2, -6, -10, -14, -19, -23, -27, -31, |
| 236 |
2, 7, 11, 16, 21, 26, 30, 35, -2, -7, -11, -16, -21, -26, -30, -35, |
| 237 |
2, 7, 13, 18, 23, 28, 34, 39, -2, -7, -13, -18, -23, -28, -34, -39, |
| 238 |
2, 8, 14, 20, 25, 31, 37, 43, -2, -8, -14, -20, -25, -31, -37, -43, |
| 239 |
3, 9, 15, 21, 28, 34, 40, 46, -3, -9, -15, -21, -28, -34, -40, -46, |
| 240 |
3, 10, 17, 24, 31, 38, 45, 52, -3, -10, -17, -24, -31, -38, -45, -52, |
| 241 |
3, 11, 19, 27, 34, 42, 50, 58, -3, -11, -19, -27, -34, -42, -50, -58, |
| 242 |
4, 12, 21, 29, 38, 46, 55, 63, -4, -12, -21, -29, -38, -46, -55, -63, |
| 243 |
4, 13, 23, 32, 41, 50, 60, 69, -4, -13, -23, -32, -41, -50, -60, -69, |
| 244 |
5, 15, 25, 35, 46, 56, 66, 76, -5, -15, -25, -35, -46, -56, -66, -76, |
| 245 |
5, 16, 28, 39, 50, 61, 73, 84, -5, -16, -28, -39, -50, -61, -73, -84, |
| 246 |
6, 18, 31, 43, 56, 68, 81, 93, -6, -18, -31, -43, -56, -68, -81, -93, |
| 247 |
6, 20, 34, 48, 61, 75, 89, 103, -6, -20, -34, -48, -61, -75, -89, -103, |
| 248 |
7, 22, 37, 52, 67, 82, 97, 112, -7, -22, -37, -52, -67, -82, -97, -112, |
| 249 |
8, 24, 41, 57, 74, 90, 107, 123, -8, -24, -41, -57, -74, -90, -107, -123, |
| 250 |
9, 27, 45, 63, 82, 100, 118, 136, -9, -27, -45, -63, -82, -100, -118, -136, |
| 251 |
10, 30, 50, 70, 90, 110, 130, 150, -10, -30, -50, -70, -90, -110, -130, -150, |
| 252 |
11, 33, 55, 77, 99, 121, 143, 165, -11, -33, -55, -77, -99, -121, -143, -165, |
| 253 |
12, 36, 60, 84, 109, 133, 157, 181, -12, -36, -60, -84, -109, -133, -157, -181, |
| 254 |
13, 40, 66, 93, 120, 147, 173, 200, -13, -40, -66, -93, -120, -147, -173, -200, |
| 255 |
14, 44, 73, 103, 132, 162, 191, 221, -14, -44, -73, -103, -132, -162, -191, -221, |
| 256 |
16, 48, 81, 113, 146, 178, 211, 243, -16, -48, -81, -113, -146, -178, -211, -243, |
| 257 |
17, 53, 89, 125, 160, 196, 232, 268, -17, -53, -89, -125, -160, -196, -232, -268, |
| 258 |
19, 58, 98, 137, 176, 215, 255, 294, -19, -58, -98, -137, -176, -215, -255, -294, |
| 259 |
21, 64, 108, 151, 194, 237, 281, 324, -21, -64, -108, -151, -194, -237, -281, -324, |
| 260 |
23, 71, 118, 166, 213, 261, 308, 356, -23, -71, -118, -166, -213, -261, -308, -356, |
| 261 |
26, 78, 130, 182, 235, 287, 339, 391, -26, -78, -130, -182, -235, -287, -339, -391, |
| 262 |
28, 86, 143, 201, 258, 316, 373, 431, -28, -86, -143, -201, -258, -316, -373, -431, |
| 263 |
31, 94, 158, 221, 284, 347, 411, 474, -31, -94, -158, -221, -284, -347, -411, -474, |
| 264 |
34, 104, 174, 244, 313, 383, 453, 523, -34, -104, -174, -244, -313, -383, -453, -523, |
| 265 |
38, 115, 191, 268, 345, 422, 498, 575, -38, -115, -191, -268, -345, -422, -498, -575, |
| 266 |
42, 126, 210, 294, 379, 463, 547, 631, -42, -126, -210, -294, -379, -463, -547, -631, |
| 267 |
46, 139, 231, 324, 417, 510, 602, 695, -46, -139, -231, -324, -417, -510, -602, -695, |
| 268 |
51, 153, 255, 357, 459, 561, 663, 765, -51, -153, -255, -357, -459, -561, -663, -765, |
| 269 |
56, 168, 280, 392, 505, 617, 729, 841, -56, -168, -280, -392, -505, -617, -729, -841, |
| 270 |
61, 185, 308, 432, 555, 679, 802, 926, -61, -185, -308, -432, -555, -679, -802, -926, |
| 271 |
68, 204, 340, 476, 612, 748, 884, 1020, -68, -204, -340, -476, -612, -748, -884, -1020, |
| 272 |
74, 224, 373, 523, 672, 822, 971, 1121, -74, -224, -373, -523, -672, -822, -971, -1121, |
| 273 |
82, 246, 411, 575, 740, 904, 1069, 1233, -82, -246, -411, -575, -740, -904, -1069, -1233, |
| 274 |
90, 271, 452, 633, 814, 995, 1176, 1357, -90, -271, -452, -633, -814, -995, -1176, -1357, |
| 275 |
99, 298, 497, 696, 895, 1094, 1293, 1492, -99, -298, -497, -696, -895, -1094, -1293, -1492, |
| 276 |
109, 328, 547, 766, 985, 1204, 1423, 1642, -109, -328, -547, -766, -985, -1204, -1423, -1642, |
| 277 |
120, 361, 601, 842, 1083, 1324, 1564, 1805, -120, -361, -601, -842, -1083, -1324, -1564, -1805, |
| 278 |
132, 397, 662, 927, 1192, 1457, 1722, 1987, -132, -397, -662, -927, -1192, -1457, -1722, -1987, |
| 279 |
145, 437, 728, 1020, 1311, 1603, 1894, 2186, -145, -437, -728, -1020, -1311, -1603, -1894, -2186, |
| 280 |
160, 480, 801, 1121, 1442, 1762, 2083, 2403, -160, -480, -801, -1121, -1442, -1762, -2083, -2403, |
| 281 |
176, 529, 881, 1234, 1587, 1940, 2292, 2645, -176, -529, -881, -1234, -1587, -1940, -2292, -2645, |
| 282 |
194, 582, 970, 1358, 1746, 2134, 2522, 2910, -194, -582, -970, -1358, -1746, -2134, -2522, -2910 |
| 283 |
}; |
| 284 |
|
| 285 |
/*OPN-MOD: ADPCM table*/ |
| 286 |
extern const unsigned int YM2608_ADPCM_ROM_addr[2*6]; |
| 287 |
extern const unsigned char YM2608_ADPCM_ROM[0x2000]; |
| 288 |
|
| 289 |
/*OPN-MOD: ADPCM constants*/ |
| 290 |
static const Bitu adpcm_shift = 16; |
| 291 |
static const int adpcm_step_inc[8] = { -1*16, -1*16, -1*16, -1*16, 2*16, 5*16, 7*16, 9*16 }; |
| 292 |
|
| 293 |
static Bit32u chip_type = ym3438_mode_readmode; |
| 294 |
|
| 295 |
/*OPN-MOD: update ADPCM volume*/ |
| 296 |
void OPNmod_RhythmUpdateVolume(ym3438_t *chip, Bit32u channel) |
| 297 |
{ |
| 298 |
Bit8u volume = chip->rhythm_tl + chip->rhythm_level[channel]; |
| 299 |
|
| 300 |
/* volume formula from MAME OPN */ |
| 301 |
|
| 302 |
if (volume >= 63) /* This is correct, 63 = quiet */ |
| 303 |
{ |
| 304 |
chip->rhythm_vol_mul[channel] = 0; |
| 305 |
chip->rhythm_vol_shift[channel] = 0; |
| 306 |
} |
| 307 |
else |
| 308 |
{ |
| 309 |
chip->rhythm_vol_mul[channel] = 15 - (volume & 7); /* so called 0.75 dB */ |
| 310 |
chip->rhythm_vol_shift[channel] = 1 + (volume >> 3); /* Yamaha engineers used the approximation: each -6 dB is close to divide by two (shift right) */ |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
void OPN2_DoIO(ym3438_t *chip) |
| 315 |
{ |
| 316 |
/* Write signal check */ |
| 317 |
chip->write_a_en = (chip->write_a & 0x03) == 0x01; |
| 318 |
chip->write_d_en = (chip->write_d & 0x03) == 0x01; |
| 319 |
chip->write_a <<= 1; |
| 320 |
chip->write_d <<= 1; |
| 321 |
/* Busy counter */ |
| 322 |
chip->busy = chip->write_busy; |
| 323 |
chip->write_busy_cnt += chip->write_busy; |
| 324 |
chip->write_busy = (chip->write_busy && !(chip->write_busy_cnt >> 5)) || chip->write_d_en; |
| 325 |
chip->write_busy_cnt &= 0x1f; |
| 326 |
} |
| 327 |
|
| 328 |
void OPN2_DoRegWrite(ym3438_t *chip) |
| 329 |
{ |
| 330 |
Bit32u i; |
| 331 |
Bit32u slot = chip->cycles % 12; |
| 332 |
Bit32u address; |
| 333 |
Bit32u channel = chip->channel; |
| 334 |
/* Update registers */ |
| 335 |
if (chip->write_fm_data) |
| 336 |
{ |
| 337 |
/* Slot */ |
| 338 |
if (op_offset[slot] == (Bit32u)(chip->address & 0x107)) |
| 339 |
{ |
| 340 |
if (chip->address & 0x08) |
| 341 |
{ |
| 342 |
/* OP2, OP4 */ |
| 343 |
slot += 12; |
| 344 |
} |
| 345 |
address = chip->address & 0xf0; |
| 346 |
switch (address) |
| 347 |
{ |
| 348 |
case 0x30: /* DT, MULTI */ |
| 349 |
chip->multi[slot] = chip->data & 0x0f; |
| 350 |
if (!chip->multi[slot]) |
| 351 |
{ |
| 352 |
chip->multi[slot] = 1; |
| 353 |
} |
| 354 |
else |
| 355 |
{ |
| 356 |
chip->multi[slot] <<= 1; |
| 357 |
} |
| 358 |
chip->dt[slot] = (chip->data >> 4) & 0x07; |
| 359 |
break; |
| 360 |
case 0x40: /* TL */ |
| 361 |
chip->tl[slot] = chip->data & 0x7f; |
| 362 |
break; |
| 363 |
case 0x50: /* KS, AR */ |
| 364 |
chip->ar[slot] = chip->data & 0x1f; |
| 365 |
chip->ks[slot] = (chip->data >> 6) & 0x03; |
| 366 |
break; |
| 367 |
case 0x60: /* AM, DR */ |
| 368 |
chip->dr[slot] = chip->data & 0x1f; |
| 369 |
chip->am[slot] = (chip->data >> 7) & 0x01; |
| 370 |
break; |
| 371 |
case 0x70: /* SR */ |
| 372 |
chip->sr[slot] = chip->data & 0x1f; |
| 373 |
break; |
| 374 |
case 0x80: /* SL, RR */ |
| 375 |
chip->rr[slot] = chip->data & 0x0f; |
| 376 |
chip->sl[slot] = (chip->data >> 4) & 0x0f; |
| 377 |
chip->sl[slot] |= (chip->sl[slot] + 1) & 0x10; |
| 378 |
break; |
| 379 |
case 0x90: /* SSG-EG */ |
| 380 |
chip->ssg_eg[slot] = chip->data & 0x0f; |
| 381 |
break; |
| 382 |
default: |
| 383 |
break; |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
/* Channel */ |
| 388 |
if (ch_offset[channel] == (Bit32u)(chip->address & 0x103)) |
| 389 |
{ |
| 390 |
address = chip->address & 0xfc; |
| 391 |
switch (address) |
| 392 |
{ |
| 393 |
case 0xa0: |
| 394 |
chip->fnum[channel] = (chip->data & 0xff) | ((chip->reg_a4 & 0x07) << 8); |
| 395 |
chip->block[channel] = (chip->reg_a4 >> 3) & 0x07; |
| 396 |
chip->kcode[channel] = (Bit8u)((chip->block[channel] << 2) | fn_note[chip->fnum[channel] >> 7]); |
| 397 |
break; |
| 398 |
case 0xa4: |
| 399 |
chip->reg_a4 = chip->data & 0xff; |
| 400 |
break; |
| 401 |
case 0xa8: |
| 402 |
chip->fnum_3ch[channel] = (chip->data & 0xff) | ((chip->reg_ac & 0x07) << 8); |
| 403 |
chip->block_3ch[channel] = (chip->reg_ac >> 3) & 0x07; |
| 404 |
chip->kcode_3ch[channel] = (chip->block_3ch[channel] << 2) | fn_note[chip->fnum_3ch[channel] >> 7]; |
| 405 |
break; |
| 406 |
case 0xac: |
| 407 |
chip->reg_ac = chip->data & 0xff; |
| 408 |
break; |
| 409 |
case 0xb0: |
| 410 |
chip->connect[channel] = chip->data & 0x07; |
| 411 |
chip->fb[channel] = (chip->data >> 3) & 0x07; |
| 412 |
break; |
| 413 |
case 0xb4: |
| 414 |
chip->pms[channel] = chip->data & 0x07; |
| 415 |
chip->ams[channel] = (chip->data >> 4) & 0x03; |
| 416 |
chip->pan_l[channel] = (chip->data >> 7) & 0x01; |
| 417 |
chip->pan_r[channel] = (chip->data >> 6) & 0x01; |
| 418 |
break; |
| 419 |
default: |
| 420 |
break; |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
if (chip->write_a_en || chip->write_d_en) |
| 426 |
{ |
| 427 |
/* Data */ |
| 428 |
if (chip->write_a_en) |
| 429 |
{ |
| 430 |
chip->write_fm_data = 0; |
| 431 |
} |
| 432 |
|
| 433 |
if (chip->write_d_en) |
| 434 |
{ |
| 435 |
chip->write_fm_data = 1; |
| 436 |
} |
| 437 |
|
| 438 |
/* Address */ |
| 439 |
if (chip->write_a_en) |
| 440 |
{ |
| 441 |
/*OPN-MOD: store address for both FM/SSG modes (for PSG read) */ |
| 442 |
chip->address = chip->write_data; |
| 443 |
|
| 444 |
if ((chip->write_data & 0xf0) != 0x00) |
| 445 |
{ |
| 446 |
/* FM Write */ |
| 447 |
chip->write_fm_address = 1; |
| 448 |
} |
| 449 |
else |
| 450 |
{ |
| 451 |
/* SSG write */ |
| 452 |
chip->write_fm_address = 0; |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
/* FM Mode */ |
| 457 |
/* Data */ |
| 458 |
if (chip->write_d_en && (chip->write_data & 0x100) == 0) |
| 459 |
{ |
| 460 |
switch (chip->write_fm_mode_a) |
| 461 |
{ |
| 462 |
/*OPN-MOD: Rhythm key on/off*/ |
| 463 |
case 0x10: |
| 464 |
if ((chip->write_data & 0x80) == 0) |
| 465 |
{ |
| 466 |
for (i = 0; i < 6; ++i) |
| 467 |
{ |
| 468 |
if (chip->write_data & (1 << i)) |
| 469 |
{ |
| 470 |
chip->rhythm_key[i] = 1; |
| 471 |
chip->rhythm_addr[i] = YM2608_ADPCM_ROM_addr[2 * i] << 1; |
| 472 |
chip->rhythm_now_step[i] = 0; |
| 473 |
chip->rhythm_adpcm_step[i] = 0; |
| 474 |
chip->rhythm_adpcm_acc[i] = 0; |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
else |
| 479 |
{ |
| 480 |
for (i = 0; i < 6; ++i) |
| 481 |
{ |
| 482 |
if (chip->write_data & (1 << i)) |
| 483 |
{ |
| 484 |
chip->rhythm_key[i] = 0; |
| 485 |
chip->rhythml[i] = 0; |
| 486 |
chip->rhythmr[i] = 0; |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
break; |
| 491 |
/*OPN-MOD: Rhythm total level*/ |
| 492 |
case 0x11: |
| 493 |
chip->rhythm_tl = ~chip->write_data & 63; |
| 494 |
for (i = 0; i < 6; ++i) |
| 495 |
{ |
| 496 |
OPNmod_RhythmUpdateVolume(chip, i); |
| 497 |
} |
| 498 |
break; |
| 499 |
/*OPN-MOD: Rhythm instrument pan/level*/ |
| 500 |
case 0x18: case 0x19: case 0x1a: |
| 501 |
case 0x1b: case 0x1c: case 0x1d: |
| 502 |
i = chip->write_fm_mode_a - 0x18; |
| 503 |
chip->rhythm_pan[i] = (chip->write_data >> 6) & 3; |
| 504 |
chip->rhythm_level[i] = ~chip->write_data & 31; |
| 505 |
OPNmod_RhythmUpdateVolume(chip, i); |
| 506 |
break; |
| 507 |
case 0x21: /* LSI test 1 */ |
| 508 |
for (i = 0; i < 8; i++) |
| 509 |
{ |
| 510 |
chip->mode_test_21[i] = (chip->write_data >> i) & 0x01; |
| 511 |
} |
| 512 |
break; |
| 513 |
case 0x22: /* LFO control */ |
| 514 |
if ((chip->write_data >> 3) & 0x01) |
| 515 |
{ |
| 516 |
chip->lfo_en = 0x7f; |
| 517 |
} |
| 518 |
else |
| 519 |
{ |
| 520 |
chip->lfo_en = 0; |
| 521 |
} |
| 522 |
chip->lfo_freq = chip->write_data & 0x07; |
| 523 |
break; |
| 524 |
case 0x24: /* Timer A */ |
| 525 |
chip->timer_a_reg &= 0x03; |
| 526 |
chip->timer_a_reg |= (chip->write_data & 0xff) << 2; |
| 527 |
break; |
| 528 |
case 0x25: |
| 529 |
chip->timer_a_reg &= 0x3fc; |
| 530 |
chip->timer_a_reg |= chip->write_data & 0x03; |
| 531 |
break; |
| 532 |
case 0x26: /* Timer B */ |
| 533 |
chip->timer_b_reg = chip->write_data & 0xff; |
| 534 |
break; |
| 535 |
case 0x27: /* CSM, Timer control */ |
| 536 |
chip->mode_ch3 = (chip->write_data & 0xc0) >> 6; |
| 537 |
chip->mode_csm = chip->mode_ch3 == 2; |
| 538 |
chip->timer_a_load = chip->write_data & 0x01; |
| 539 |
chip->timer_a_enable = (chip->write_data >> 2) & 0x01; |
| 540 |
chip->timer_a_reset = (chip->write_data >> 4) & 0x01; |
| 541 |
chip->timer_b_load = (chip->write_data >> 1) & 0x01; |
| 542 |
chip->timer_b_enable = (chip->write_data >> 3) & 0x01; |
| 543 |
chip->timer_b_reset = (chip->write_data >> 5) & 0x01; |
| 544 |
break; |
| 545 |
case 0x28: /* Key on/off */ |
| 546 |
for (i = 0; i < 4; i++) |
| 547 |
{ |
| 548 |
chip->mode_kon_operator[i] = (chip->write_data >> (4 + i)) & 0x01; |
| 549 |
} |
| 550 |
if ((chip->write_data & 0x03) == 0x03) |
| 551 |
{ |
| 552 |
/* Invalid address */ |
| 553 |
chip->mode_kon_channel = 0xff; |
| 554 |
} |
| 555 |
else |
| 556 |
{ |
| 557 |
/*OPN-MOD: select according to 6 FM channel flag*/ |
| 558 |
chip->mode_kon_channel = chip->write_data & 0x03; |
| 559 |
if (chip->mode_fm6ch && (chip->write_data & 0x04)) |
| 560 |
{ |
| 561 |
chip->mode_kon_channel += 3; |
| 562 |
} |
| 563 |
} |
| 564 |
break; |
| 565 |
case 0x29: /*OPN-MOD: set 6 FM channel flag*/ |
| 566 |
chip->mode_fm6ch = chip->write_data & 0x80; |
| 567 |
break; |
| 568 |
case 0x2a: /* DAC data */ |
| 569 |
chip->dacdata &= 0x01; |
| 570 |
chip->dacdata |= (chip->write_data ^ 0x80) << 1; |
| 571 |
break; |
| 572 |
case 0x2b: /* DAC enable */ |
| 573 |
chip->dacen = chip->write_data >> 7; |
| 574 |
break; |
| 575 |
case 0x2c: /* LSI test 2 */ |
| 576 |
for (i = 0; i < 8; i++) |
| 577 |
{ |
| 578 |
chip->mode_test_2c[i] = (chip->write_data >> i) & 0x01; |
| 579 |
} |
| 580 |
chip->dacdata &= 0x1fe; |
| 581 |
chip->dacdata |= chip->mode_test_2c[3]; |
| 582 |
chip->eg_custom_timer = !chip->mode_test_2c[7] && chip->mode_test_2c[6]; |
| 583 |
break; |
| 584 |
default: |
| 585 |
/*OPN-MOD: write $00-$0F to PSG*/ |
| 586 |
if (chip->write_fm_mode_a < 0x10) |
| 587 |
{ |
| 588 |
chip->psg->Write(chip->psgdata, 0, chip->write_fm_mode_a); |
| 589 |
chip->psg->Write(chip->psgdata, 1, chip->write_data); |
| 590 |
} |
| 591 |
break; |
| 592 |
} |
| 593 |
} |
| 594 |
else if (chip->write_d_en && (chip->write_data & 0x100) == 0x100) |
| 595 |
{ |
| 596 |
/*OPN-Mod*/ |
| 597 |
switch (chip->write_fm_mode_a & 0xf0) |
| 598 |
{ |
| 599 |
case 0x00: |
| 600 |
if (chip->write_fm_mode_a == 0x0e) |
| 601 |
{ |
| 602 |
/* write to DAC data (unimplemented) */ |
| 603 |
} |
| 604 |
else if (chip->write_fm_mode_a != 0x0f) |
| 605 |
{ |
| 606 |
YM_DELTAT_ADPCM_Write(&chip->deltaT, chip->write_fm_mode_a, chip->write_data & 0xff); |
| 607 |
} |
| 608 |
break; |
| 609 |
case 0x10: |
| 610 |
/* IRQ flag control (unimplemented) */ |
| 611 |
break; |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
/* Address */ |
| 616 |
if (chip->write_a_en) |
| 617 |
{ |
| 618 |
chip->write_fm_mode_a = chip->write_data & 0x1ff; |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
if (chip->write_fm_data) |
| 623 |
{ |
| 624 |
chip->data = chip->write_data & 0xff; |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
void OPN2_PhaseCalcIncrement(ym3438_t *chip) |
| 629 |
{ |
| 630 |
Bit32u chan = chip->channel; |
| 631 |
Bit32u slot = chip->cycles; |
| 632 |
Bit32u fnum = chip->pg_fnum; |
| 633 |
Bit32u fnum_h = fnum >> 4; |
| 634 |
Bit32u fm; |
| 635 |
Bit32u basefreq; |
| 636 |
Bit8u lfo = chip->lfo_pm; |
| 637 |
Bit8u lfo_l = lfo & 0x0f; |
| 638 |
Bit8u pms = chip->pms[chan]; |
| 639 |
Bit8u dt = chip->dt[slot]; |
| 640 |
Bit8u dt_l = dt & 0x03; |
| 641 |
Bit8u detune = 0; |
| 642 |
Bit8u block, note; |
| 643 |
Bit8u sum, sum_h, sum_l; |
| 644 |
Bit8u kcode = chip->pg_kcode; |
| 645 |
|
| 646 |
fnum <<= 1; |
| 647 |
/* Apply LFO */ |
| 648 |
if (lfo_l & 0x08) |
| 649 |
{ |
| 650 |
lfo_l ^= 0x0f; |
| 651 |
} |
| 652 |
fm = (fnum_h >> pg_lfo_sh1[pms][lfo_l]) + (fnum_h >> pg_lfo_sh2[pms][lfo_l]); |
| 653 |
if (pms > 5) |
| 654 |
{ |
| 655 |
fm <<= pms - 5; |
| 656 |
} |
| 657 |
fm >>= 2; |
| 658 |
if (lfo & 0x10) |
| 659 |
{ |
| 660 |
fnum -= fm; |
| 661 |
} |
| 662 |
else |
| 663 |
{ |
| 664 |
fnum += fm; |
| 665 |
} |
| 666 |
fnum &= 0xfff; |
| 667 |
|
| 668 |
basefreq = (fnum << chip->pg_block) >> 2; |
| 669 |
|
| 670 |
/* Apply detune */ |
| 671 |
if (dt_l) |
| 672 |
{ |
| 673 |
if (kcode > 0x1c) |
| 674 |
{ |
| 675 |
kcode = 0x1c; |
| 676 |
} |
| 677 |
block = kcode >> 2; |
| 678 |
note = kcode & 0x03; |
| 679 |
sum = block + 9 + ((dt_l == 3) | (dt_l & 0x02)); |
| 680 |
sum_h = sum >> 1; |
| 681 |
sum_l = sum & 0x01; |
| 682 |
detune = pg_detune[(sum_l << 2) | note] >> (9 - sum_h); |
| 683 |
} |
| 684 |
if (dt & 0x04) |
| 685 |
{ |
| 686 |
basefreq -= detune; |
| 687 |
} |
| 688 |
else |
| 689 |
{ |
| 690 |
basefreq += detune; |
| 691 |
} |
| 692 |
basefreq &= 0x1ffff; |
| 693 |
chip->pg_inc[slot] = (basefreq * chip->multi[slot]) >> 1; |
| 694 |
chip->pg_inc[slot] &= 0xfffff; |
| 695 |
} |
| 696 |
|
| 697 |
void OPN2_PhaseGenerate(ym3438_t *chip) |
| 698 |
{ |
| 699 |
Bit32u slot; |
| 700 |
/* Mask increment */ |
| 701 |
slot = (chip->cycles + 20) % 24; |
| 702 |
if (chip->pg_reset[slot]) |
| 703 |
{ |
| 704 |
chip->pg_inc[slot] = 0; |
| 705 |
} |
| 706 |
/* Phase step */ |
| 707 |
slot = (chip->cycles + 19) % 24; |
| 708 |
chip->pg_phase[slot] += chip->pg_inc[slot]; |
| 709 |
chip->pg_phase[slot] &= 0xfffff; |
| 710 |
if (chip->pg_reset[slot] || chip->mode_test_21[3]) |
| 711 |
{ |
| 712 |
chip->pg_phase[slot] = 0; |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
void OPN2_EnvelopeSSGEG(ym3438_t *chip) |
| 717 |
{ |
| 718 |
Bit32u slot = chip->cycles; |
| 719 |
Bit8u direction = 0; |
| 720 |
chip->eg_ssg_pgrst_latch[slot] = 0; |
| 721 |
chip->eg_ssg_repeat_latch[slot] = 0; |
| 722 |
chip->eg_ssg_hold_up_latch[slot] = 0; |
| 723 |
chip->eg_ssg_inv[slot] = 0; |
| 724 |
if (chip->ssg_eg[slot] & 0x08) |
| 725 |
{ |
| 726 |
direction = chip->eg_ssg_dir[slot]; |
| 727 |
if (chip->eg_level[slot] & 0x200) |
| 728 |
{ |
| 729 |
/* Reset */ |
| 730 |
if ((chip->ssg_eg[slot] & 0x03) == 0x00) |
| 731 |
{ |
| 732 |
chip->eg_ssg_pgrst_latch[slot] = 1; |
| 733 |
} |
| 734 |
/* Repeat */ |
| 735 |
if ((chip->ssg_eg[slot] & 0x01) == 0x00) |
| 736 |
{ |
| 737 |
chip->eg_ssg_repeat_latch[slot] = 1; |
| 738 |
} |
| 739 |
/* Inverse */ |
| 740 |
if ((chip->ssg_eg[slot] & 0x03) == 0x02) |
| 741 |
{ |
| 742 |
direction ^= 1; |
| 743 |
} |
| 744 |
if ((chip->ssg_eg[slot] & 0x03) == 0x03) |
| 745 |
{ |
| 746 |
direction = 1; |
| 747 |
} |
| 748 |
} |
| 749 |
/* Hold up */ |
| 750 |
if (chip->eg_kon_latch[slot] |
| 751 |
&& ((chip->ssg_eg[slot] & 0x07) == 0x05 || (chip->ssg_eg[slot] & 0x07) == 0x03)) |
| 752 |
{ |
| 753 |
chip->eg_ssg_hold_up_latch[slot] = 1; |
| 754 |
} |
| 755 |
direction &= chip->eg_kon[slot]; |
| 756 |
chip->eg_ssg_inv[slot] = (chip->eg_ssg_dir[slot] ^ ((chip->ssg_eg[slot] >> 2) & 0x01)) |
| 757 |
& chip->eg_kon[slot]; |
| 758 |
} |
| 759 |
chip->eg_ssg_dir[slot] = direction; |
| 760 |
chip->eg_ssg_enable[slot] = (chip->ssg_eg[slot] >> 3) & 0x01; |
| 761 |
} |
| 762 |
|
| 763 |
void OPN2_EnvelopeADSR(ym3438_t *chip) |
| 764 |
{ |
| 765 |
Bit32u slot = (chip->cycles + 22) % 24; |
| 766 |
|
| 767 |
Bit8u nkon = chip->eg_kon_latch[slot]; |
| 768 |
Bit8u okon = chip->eg_kon[slot]; |
| 769 |
Bit8u kon_event; |
| 770 |
Bit8u koff_event; |
| 771 |
Bit8u eg_off; |
| 772 |
Bit16s level; |
| 773 |
Bit16s nextlevel = 0; |
| 774 |
Bit16s ssg_level; |
| 775 |
Bit8u nextstate = chip->eg_state[slot]; |
| 776 |
Bit16s inc = 0; |
| 777 |
chip->eg_read[0] = chip->eg_read_inc; |
| 778 |
chip->eg_read_inc = chip->eg_inc > 0; |
| 779 |
|
| 780 |
/* Reset phase generator */ |
| 781 |
chip->pg_reset[slot] = (nkon && !okon) || chip->eg_ssg_pgrst_latch[slot]; |
| 782 |
|
| 783 |
/* KeyOn/Off */ |
| 784 |
kon_event = (nkon && !okon) || (okon && chip->eg_ssg_repeat_latch[slot]); |
| 785 |
koff_event = okon && !nkon; |
| 786 |
|
| 787 |
ssg_level = level = (Bit16s)chip->eg_level[slot]; |
| 788 |
|
| 789 |
if (chip->eg_ssg_inv[slot]) |
| 790 |
{ |
| 791 |
/* Inverse */ |
| 792 |
ssg_level = 512 - level; |
| 793 |
ssg_level &= 0x3ff; |
| 794 |
} |
| 795 |
if (koff_event) |
| 796 |
{ |
| 797 |
level = ssg_level; |
| 798 |
} |
| 799 |
if (chip->eg_ssg_enable[slot]) |
| 800 |
{ |
| 801 |
eg_off = level >> 9; |
| 802 |
} |
| 803 |
else |
| 804 |
{ |
| 805 |
eg_off = (level & 0x3f0) == 0x3f0; |
| 806 |
} |
| 807 |
nextlevel = level; |
| 808 |
if (kon_event) |
| 809 |
{ |
| 810 |
nextstate = eg_num_attack; |
| 811 |
/* Instant attack */ |
| 812 |
if (chip->eg_ratemax) |
| 813 |
{ |
| 814 |
nextlevel = 0; |
| 815 |
} |
| 816 |
else if (chip->eg_state[slot] == eg_num_attack && level != 0 && chip->eg_inc && nkon) |
| 817 |
{ |
| 818 |
inc = (~level << chip->eg_inc) >> 5; |
| 819 |
} |
| 820 |
} |
| 821 |
else |
| 822 |
{ |
| 823 |
switch (chip->eg_state[slot]) |
| 824 |
{ |
| 825 |
case eg_num_attack: |
| 826 |
if (level == 0) |
| 827 |
{ |
| 828 |
nextstate = eg_num_decay; |
| 829 |
} |
| 830 |
else if(chip->eg_inc && !chip->eg_ratemax && nkon) |
| 831 |
{ |
| 832 |
inc = (~level << chip->eg_inc) >> 5; |
| 833 |
} |
| 834 |
break; |
| 835 |
case eg_num_decay: |
| 836 |
if ((level >> 5) == chip->eg_sl[1]) |
| 837 |
{ |
| 838 |
nextstate = eg_num_sustain; |
| 839 |
} |
| 840 |
else if (!eg_off && chip->eg_inc) |
| 841 |
{ |
| 842 |
inc = 1 << (chip->eg_inc - 1); |
| 843 |
if (chip->eg_ssg_enable[slot]) |
| 844 |
{ |
| 845 |
inc <<= 2; |
| 846 |
} |
| 847 |
} |
| 848 |
break; |
| 849 |
case eg_num_sustain: |
| 850 |
case eg_num_release: |
| 851 |
if (!eg_off && chip->eg_inc) |
| 852 |
{ |
| 853 |
inc = 1 << (chip->eg_inc - 1); |
| 854 |
if (chip->eg_ssg_enable[slot]) |
| 855 |
{ |
| 856 |
inc <<= 2; |
| 857 |
} |
| 858 |
} |
| 859 |
break; |
| 860 |
default: |
| 861 |
break; |
| 862 |
} |
| 863 |
if (!nkon) |
| 864 |
{ |
| 865 |
nextstate = eg_num_release; |
| 866 |
} |
| 867 |
} |
| 868 |
if (chip->eg_kon_csm[slot]) |
| 869 |
{ |
| 870 |
nextlevel |= chip->eg_tl[1] << 3; |
| 871 |
} |
| 872 |
|
| 873 |
/* Envelope off */ |
| 874 |
if (!kon_event && !chip->eg_ssg_hold_up_latch[slot] && chip->eg_state[slot] != eg_num_attack && eg_off) |
| 875 |
{ |
| 876 |
nextstate = eg_num_release; |
| 877 |
nextlevel = 0x3ff; |
| 878 |
} |
| 879 |
|
| 880 |
nextlevel += inc; |
| 881 |
|
| 882 |
chip->eg_kon[slot] = chip->eg_kon_latch[slot]; |
| 883 |
chip->eg_level[slot] = (Bit16u)nextlevel & 0x3ff; |
| 884 |
chip->eg_state[slot] = nextstate; |
| 885 |
} |
| 886 |
|
| 887 |
void OPN2_EnvelopePrepare(ym3438_t *chip) |
| 888 |
{ |
| 889 |
Bit8u rate; |
| 890 |
Bit8u sum; |
| 891 |
Bit8u inc = 0; |
| 892 |
Bit32u slot = chip->cycles; |
| 893 |
Bit8u rate_sel; |
| 894 |
|
| 895 |
/* Prepare increment */ |
| 896 |
rate = (chip->eg_rate << 1) + chip->eg_ksv; |
| 897 |
|
| 898 |
if (rate > 0x3f) |
| 899 |
{ |
| 900 |
rate = 0x3f; |
| 901 |
} |
| 902 |
|
| 903 |
sum = ((rate >> 2) + chip->eg_shift_lock) & 0x0f; |
| 904 |
if (chip->eg_rate != 0 && chip->eg_quotient == 2) |
| 905 |
{ |
| 906 |
if (rate < 48) |
| 907 |
{ |
| 908 |
switch (sum) |
| 909 |
{ |
| 910 |
case 12: |
| 911 |
inc = 1; |
| 912 |
break; |
| 913 |
case 13: |
| 914 |
inc = (rate >> 1) & 0x01; |
| 915 |
break; |
| 916 |
case 14: |
| 917 |
inc = rate & 0x01; |
| 918 |
break; |
| 919 |
default: |
| 920 |
break; |
| 921 |
} |
| 922 |
} |
| 923 |
else |
| 924 |
{ |
| 925 |
inc = eg_stephi[rate & 0x03][chip->eg_timer_low_lock] + (rate >> 2) - 11; |
| 926 |
if (inc > 4) |
| 927 |
{ |
| 928 |
inc = 4; |
| 929 |
} |
| 930 |
} |
| 931 |
} |
| 932 |
chip->eg_inc = inc; |
| 933 |
chip->eg_ratemax = (rate >> 1) == 0x1f; |
| 934 |
|
| 935 |
/* Prepare rate & ksv */ |
| 936 |
rate_sel = chip->eg_state[slot]; |
| 937 |
if ((chip->eg_kon[slot] && chip->eg_ssg_repeat_latch[slot]) |
| 938 |
|| (!chip->eg_kon[slot] && chip->eg_kon_latch[slot])) |
| 939 |
{ |
| 940 |
rate_sel = eg_num_attack; |
| 941 |
} |
| 942 |
switch (rate_sel) |
| 943 |
{ |
| 944 |
case eg_num_attack: |
| 945 |
chip->eg_rate = chip->ar[slot]; |
| 946 |
break; |
| 947 |
case eg_num_decay: |
| 948 |
chip->eg_rate = chip->dr[slot]; |
| 949 |
break; |
| 950 |
case eg_num_sustain: |
| 951 |
chip->eg_rate = chip->sr[slot]; |
| 952 |
break; |
| 953 |
case eg_num_release: |
| 954 |
chip->eg_rate = (chip->rr[slot] << 1) | 0x01; |
| 955 |
break; |
| 956 |
default: |
| 957 |
break; |
| 958 |
} |
| 959 |
chip->eg_ksv = chip->pg_kcode >> (chip->ks[slot] ^ 0x03); |
| 960 |
if (chip->am[slot]) |
| 961 |
{ |
| 962 |
chip->eg_lfo_am = chip->lfo_am >> eg_am_shift[chip->ams[chip->channel]]; |
| 963 |
} |
| 964 |
else |
| 965 |
{ |
| 966 |
chip->eg_lfo_am = 0; |
| 967 |
} |
| 968 |
/* Delay TL & SL value */ |
| 969 |
chip->eg_tl[1] = chip->eg_tl[0]; |
| 970 |
chip->eg_tl[0] = chip->tl[slot]; |
| 971 |
chip->eg_sl[1] = chip->eg_sl[0]; |
| 972 |
chip->eg_sl[0] = chip->sl[slot]; |
| 973 |
} |
| 974 |
|
| 975 |
void OPN2_EnvelopeGenerate(ym3438_t *chip) |
| 976 |
{ |
| 977 |
Bit32u slot = (chip->cycles + 23) % 24; |
| 978 |
Bit16u level; |
| 979 |
|
| 980 |
level = chip->eg_level[slot]; |
| 981 |
|
| 982 |
if (chip->eg_ssg_inv[slot]) |
| 983 |
{ |
| 984 |
/* Inverse */ |
| 985 |
level = 512 - level; |
| 986 |
} |
| 987 |
if (chip->mode_test_21[5]) |
| 988 |
{ |
| 989 |
level = 0; |
| 990 |
} |
| 991 |
level &= 0x3ff; |
| 992 |
|
| 993 |
/* Apply AM LFO */ |
| 994 |
level += chip->eg_lfo_am; |
| 995 |
|
| 996 |
/* Apply TL */ |
| 997 |
if (!(chip->mode_csm && chip->channel == 2 + 1)) |
| 998 |
{ |
| 999 |
level += chip->eg_tl[0] << 3; |
| 1000 |
} |
| 1001 |
if (level > 0x3ff) |
| 1002 |
{ |
| 1003 |
level = 0x3ff; |
| 1004 |
} |
| 1005 |
chip->eg_out[slot] = level; |
| 1006 |
} |
| 1007 |
|
| 1008 |
void OPN2_UpdateLFO(ym3438_t *chip) |
| 1009 |
{ |
| 1010 |
if ((chip->lfo_quotient & lfo_cycles[chip->lfo_freq]) == lfo_cycles[chip->lfo_freq]) |
| 1011 |
{ |
| 1012 |
chip->lfo_quotient = 0; |
| 1013 |
chip->lfo_cnt++; |
| 1014 |
} |
| 1015 |
else |
| 1016 |
{ |
| 1017 |
chip->lfo_quotient += chip->lfo_inc; |
| 1018 |
} |
| 1019 |
chip->lfo_cnt &= chip->lfo_en; |
| 1020 |
} |
| 1021 |
|
| 1022 |
void OPN2_FMPrepare(ym3438_t *chip) |
| 1023 |
{ |
| 1024 |
Bit32u slot = (chip->cycles + 6) % 24; |
| 1025 |
Bit32u channel = chip->channel; |
| 1026 |
Bit16s mod, mod1, mod2; |
| 1027 |
Bit32u op = slot / 6; |
| 1028 |
Bit8u connect = chip->connect[channel]; |
| 1029 |
Bit32u prevslot = (chip->cycles + 18) % 24; |
| 1030 |
|
| 1031 |
/* Calculate modulation */ |
| 1032 |
mod1 = mod2 = 0; |
| 1033 |
|
| 1034 |
if (fm_algorithm[op][0][connect]) |
| 1035 |
{ |
| 1036 |
mod2 |= chip->fm_op1[channel][0]; |
| 1037 |
} |
| 1038 |
if (fm_algorithm[op][1][connect]) |
| 1039 |
{ |
| 1040 |
mod1 |= chip->fm_op1[channel][1]; |
| 1041 |
} |
| 1042 |
if (fm_algorithm[op][2][connect]) |
| 1043 |
{ |
| 1044 |
mod1 |= chip->fm_op2[channel]; |
| 1045 |
} |
| 1046 |
if (fm_algorithm[op][3][connect]) |
| 1047 |
{ |
| 1048 |
mod2 |= chip->fm_out[prevslot]; |
| 1049 |
} |
| 1050 |
if (fm_algorithm[op][4][connect]) |
| 1051 |
{ |
| 1052 |
mod1 |= chip->fm_out[prevslot]; |
| 1053 |
} |
| 1054 |
mod = mod1 + mod2; |
| 1055 |
if (op == 0) |
| 1056 |
{ |
| 1057 |
/* Feedback */ |
| 1058 |
mod = mod >> (10 - chip->fb[channel]); |
| 1059 |
if (!chip->fb[channel]) |
| 1060 |
{ |
| 1061 |
mod = 0; |
| 1062 |
} |
| 1063 |
} |
| 1064 |
else |
| 1065 |
{ |
| 1066 |
mod >>= 1; |
| 1067 |
} |
| 1068 |
chip->fm_mod[slot] = mod; |
| 1069 |
|
| 1070 |
slot = (chip->cycles + 18) % 24; |
| 1071 |
/* OP1 */ |
| 1072 |
if (slot / 6 == 0) |
| 1073 |
{ |
| 1074 |
chip->fm_op1[channel][1] = chip->fm_op1[channel][0]; |
| 1075 |
chip->fm_op1[channel][0] = chip->fm_out[slot]; |
| 1076 |
} |
| 1077 |
/* OP2 */ |
| 1078 |
if (slot / 6 == 2) |
| 1079 |
{ |
| 1080 |
chip->fm_op2[channel] = chip->fm_out[slot]; |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
void OPN2_ChGenerate(ym3438_t *chip) |
| 1085 |
{ |
| 1086 |
Bit32u slot = (chip->cycles + 18) % 24; |
| 1087 |
Bit32u channel = chip->channel; |
| 1088 |
Bit32u op = slot / 6; |
| 1089 |
Bit32u test_dac = chip->mode_test_2c[5]; |
| 1090 |
Bit16s acc = chip->ch_acc[channel]; |
| 1091 |
Bit16s add = test_dac; |
| 1092 |
Bit16s sum = 0; |
| 1093 |
|
| 1094 |
Bit16s channel_maxsample = (1 << (channel_clipbits - 1)) - 1; |
| 1095 |
Bit16s channel_minsample = -(1 << (channel_clipbits - 1)); |
| 1096 |
|
| 1097 |
if (op == 0 && !test_dac) |
| 1098 |
{ |
| 1099 |
acc = 0; |
| 1100 |
} |
| 1101 |
if (fm_algorithm[op][5][chip->connect[channel]] && !test_dac) |
| 1102 |
{ |
| 1103 |
/*OPN-MOD: drop 1 bit (YM2608) instead of 5 (YM2612)*/ |
| 1104 |
add += chip->fm_out[slot] >> 1; |
| 1105 |
} |
| 1106 |
sum = acc + add; |
| 1107 |
/* Clamp */ |
| 1108 |
/*Bit16s channel_maxsample = (1 << (channel_clipbits - 1)) - 1; |
| 1109 |
Bit16s channel_minsample = -(1 << (channel_clipbits - 1));*/ |
| 1110 |
if (sum > channel_maxsample) |
| 1111 |
{ |
| 1112 |
sum = channel_maxsample; |
| 1113 |
} |
| 1114 |
else if(sum < channel_minsample) |
| 1115 |
{ |
| 1116 |
sum = channel_minsample; |
| 1117 |
} |
| 1118 |
|
| 1119 |
if (op == 0 || test_dac) |
| 1120 |
{ |
| 1121 |
chip->ch_out[channel] = chip->ch_acc[channel]; |
| 1122 |
} |
| 1123 |
chip->ch_acc[channel] = sum; |
| 1124 |
} |
| 1125 |
|
| 1126 |
void OPN2_ChOutput(ym3438_t *chip) |
| 1127 |
{ |
| 1128 |
Bit32u cycles = chip->cycles; |
| 1129 |
Bit32u slot = chip->cycles; |
| 1130 |
Bit32u channel = chip->channel; |
| 1131 |
Bit32u test_dac = chip->mode_test_2c[5]; |
| 1132 |
Bit16s out; |
| 1133 |
Bit16s sign; |
| 1134 |
Bit32u out_en; |
| 1135 |
chip->ch_read = chip->ch_lock; |
| 1136 |
if (slot < 12) |
| 1137 |
{ |
| 1138 |
/* Ch 4,5,6 */ |
| 1139 |
channel++; |
| 1140 |
} |
| 1141 |
if ((cycles & 3) == 0) |
| 1142 |
{ |
| 1143 |
if (!test_dac) |
| 1144 |
{ |
| 1145 |
/* Lock value */ |
| 1146 |
chip->ch_lock = chip->ch_out[channel]; |
| 1147 |
} |
| 1148 |
chip->ch_lock_l = chip->pan_l[channel]; |
| 1149 |
chip->ch_lock_r = chip->pan_r[channel]; |
| 1150 |
} |
| 1151 |
/* Ch 6 */ |
| 1152 |
if (((cycles >> 2) == 1 && chip->dacen) || test_dac) |
| 1153 |
{ |
| 1154 |
out = (Bit16s)chip->dacdata; |
| 1155 |
out <<= 7; |
| 1156 |
out >>= 7; |
| 1157 |
} |
| 1158 |
else |
| 1159 |
{ |
| 1160 |
out = chip->ch_lock; |
| 1161 |
} |
| 1162 |
chip->mol = 0; |
| 1163 |
chip->mor = 0; |
| 1164 |
|
| 1165 |
if (chip_type & ym3438_mode_ym2612) |
| 1166 |
{ |
| 1167 |
out_en = ((cycles & 3) == 3) || test_dac; |
| 1168 |
/* YM2612 DAC emulation(not verified) */ |
| 1169 |
sign = out >> 8; |
| 1170 |
if (out >= 0) |
| 1171 |
{ |
| 1172 |
out++; |
| 1173 |
sign++; |
| 1174 |
} |
| 1175 |
if (chip->ch_lock_l && out_en) |
| 1176 |
{ |
| 1177 |
chip->mol = out; |
| 1178 |
} |
| 1179 |
else |
| 1180 |
{ |
| 1181 |
chip->mol = sign; |
| 1182 |
} |
| 1183 |
if (chip->ch_lock_r && out_en) |
| 1184 |
{ |
| 1185 |
chip->mor = out; |
| 1186 |
} |
| 1187 |
else |
| 1188 |
{ |
| 1189 |
chip->mor = sign; |
| 1190 |
} |
| 1191 |
/* Amplify signal */ |
| 1192 |
chip->mol *= 3; |
| 1193 |
chip->mor *= 3; |
| 1194 |
} |
| 1195 |
else |
| 1196 |
{ |
| 1197 |
out_en = ((cycles & 3) != 0) || test_dac; |
| 1198 |
if (chip->ch_lock_l && out_en) |
| 1199 |
{ |
| 1200 |
chip->mol = out; |
| 1201 |
} |
| 1202 |
if (chip->ch_lock_r && out_en) |
| 1203 |
{ |
| 1204 |
chip->mor = out; |
| 1205 |
} |
| 1206 |
} |
| 1207 |
} |
| 1208 |
|
| 1209 |
void OPN2_FMGenerate(ym3438_t *chip) |
| 1210 |
{ |
| 1211 |
Bit32u slot = (chip->cycles + 19) % 24; |
| 1212 |
/* Calculate phase */ |
| 1213 |
Bit16u phase = (chip->fm_mod[slot] + (chip->pg_phase[slot] >> 10)) & 0x3ff; |
| 1214 |
Bit16u quarter; |
| 1215 |
Bit16u level; |
| 1216 |
Bit16s output; |
| 1217 |
if (phase & 0x100) |
| 1218 |
{ |
| 1219 |
quarter = (phase ^ 0xff) & 0xff; |
| 1220 |
} |
| 1221 |
else |
| 1222 |
{ |
| 1223 |
quarter = phase & 0xff; |
| 1224 |
} |
| 1225 |
level = logsinrom[quarter]; |
| 1226 |
/* Apply envelope */ |
| 1227 |
level += chip->eg_out[slot] << 2; |
| 1228 |
/* Transform */ |
| 1229 |
if (level > 0x1fff) |
| 1230 |
{ |
| 1231 |
level = 0x1fff; |
| 1232 |
} |
| 1233 |
output = ((exprom[(level & 0xff) ^ 0xff] | 0x400) << 2) >> (level >> 8); |
| 1234 |
if (phase & 0x200) |
| 1235 |
{ |
| 1236 |
output = ((~output) ^ (chip->mode_test_21[4] << 13)) + 1; |
| 1237 |
} |
| 1238 |
else |
| 1239 |
{ |
| 1240 |
output = output ^ (chip->mode_test_21[4] << 13); |
| 1241 |
} |
| 1242 |
output <<= 2; |
| 1243 |
output >>= 2; |
| 1244 |
chip->fm_out[slot] = output; |
| 1245 |
} |
| 1246 |
|
| 1247 |
/*OPN-MOD: generate ADPCM rhythm*/ |
| 1248 |
void OPNmod_RhythmGenerate(ym3438_t *chip) |
| 1249 |
{ |
| 1250 |
Bit32u channel = chip->channel; |
| 1251 |
Bit32s out = 0; |
| 1252 |
Bit8u panl = 0; |
| 1253 |
Bit8u panr = 0; |
| 1254 |
|
| 1255 |
Bit32u step; |
| 1256 |
Bit8u data; |
| 1257 |
Bit16u end = YM2608_ADPCM_ROM_addr[2 * channel + 1] << 1; |
| 1258 |
|
| 1259 |
if (chip->cycles < 6 && chip->rhythm_key[channel]) |
| 1260 |
{ |
| 1261 |
/*Bit32u step; |
| 1262 |
Bit8u data; |
| 1263 |
Bit16u end = YM2608_ADPCM_ROM_addr[2 * channel + 1] << 1;*/ |
| 1264 |
|
| 1265 |
panl = chip->rhythm_pan[channel] & 2; |
| 1266 |
panr = chip->rhythm_pan[channel] & 1; |
| 1267 |
|
| 1268 |
/*from MAME*/ |
| 1269 |
chip->rhythm_now_step[channel] += chip->rhythm_step[channel]; |
| 1270 |
if (chip->rhythm_now_step[channel] >= (1u << adpcm_shift)) |
| 1271 |
{ |
| 1272 |
step = chip->rhythm_now_step[channel] >> adpcm_shift; |
| 1273 |
chip->rhythm_now_step[channel] &= (1 << adpcm_shift) - 1; |
| 1274 |
do |
| 1275 |
{ |
| 1276 |
/* end check */ |
| 1277 |
/* 11-06-2001 JB: corrected comparison. Was > instead of == */ |
| 1278 |
/* YM2610 checks lower 20 bits only, the 4 MSB bits are sample bank */ |
| 1279 |
/* Here we use 1<<21 to compensate for nibble calculations */ |
| 1280 |
|
| 1281 |
if ((chip->rhythm_addr[channel] & ((1 << 21) - 1)) == (end & ((1 << 21) - 1))) |
| 1282 |
{ |
| 1283 |
chip->rhythm_key[channel] = 0; |
| 1284 |
goto end; |
| 1285 |
} |
| 1286 |
|
| 1287 |
if (chip->rhythm_addr[channel] & 1) |
| 1288 |
{ |
| 1289 |
data = chip->rhythm_data[channel] & 0x0f; |
| 1290 |
} |
| 1291 |
else |
| 1292 |
{ |
| 1293 |
chip->rhythm_data[channel] = YM2608_ADPCM_ROM[chip->rhythm_addr[channel] >> 1]; |
| 1294 |
data = (chip->rhythm_data[channel] >> 4) & 0x0f; |
| 1295 |
} |
| 1296 |
|
| 1297 |
chip->rhythm_addr[channel]++; |
| 1298 |
|
| 1299 |
chip->rhythm_adpcm_acc[channel] += adpcm_jedi_table[chip->rhythm_adpcm_step[channel] + data]; |
| 1300 |
|
| 1301 |
/* the 12-bit accumulator wraps on the ym2610 and ym2608 (like the msm5205), it does not saturate (like the msm5218) */ |
| 1302 |
chip->rhythm_adpcm_acc[channel] &= 0xfff; |
| 1303 |
|
| 1304 |
/* extend 12-bit signed int */ |
| 1305 |
if (chip->rhythm_adpcm_acc[channel] & 0x800) |
| 1306 |
{ |
| 1307 |
chip->rhythm_adpcm_acc[channel] |= ~0xfff; |
| 1308 |
} |
| 1309 |
|
| 1310 |
chip->rhythm_adpcm_step[channel] += adpcm_step_inc[data & 7]; |
| 1311 |
if (chip->rhythm_adpcm_step[channel] > 48 * 16) |
| 1312 |
{ |
| 1313 |
chip->rhythm_adpcm_step[channel] = 48 * 16; |
| 1314 |
} |
| 1315 |
if (chip->rhythm_adpcm_step[channel] < 0) |
| 1316 |
{ |
| 1317 |
chip->rhythm_adpcm_step[channel] = 0; |
| 1318 |
} |
| 1319 |
} while (--step); |
| 1320 |
|
| 1321 |
/* calc pcm * volume data */ |
| 1322 |
out = ((chip->rhythm_adpcm_acc[channel] * chip->rhythm_vol_mul[channel]) >> chip->rhythm_vol_shift[channel]) & ~3; /* multiply, shift and mask out 2 LSB bits */ |
| 1323 |
|
| 1324 |
end: |
| 1325 |
chip->rhythml[channel] = panl ? (out >> 1) : 0; |
| 1326 |
chip->rhythmr[channel] = panr ? (out >> 1) : 0; |
| 1327 |
} |
| 1328 |
} |
| 1329 |
} |
| 1330 |
|
| 1331 |
/*OPN-MOD: generate ADPCM DeltaT*/ |
| 1332 |
void OPNmod_DeltaTGenerate(ym3438_t *chip) |
| 1333 |
{ |
| 1334 |
Bit32u channel = chip->channel; |
| 1335 |
|
| 1336 |
Bit32s *out = chip->out_deltaT; |
| 1337 |
out[0] = 0; |
| 1338 |
out[1] = 0; |
| 1339 |
out[2] = 0; |
| 1340 |
out[3] = 0; |
| 1341 |
|
| 1342 |
if (chip->cycles == 0) |
| 1343 |
{ |
| 1344 |
if (chip->deltaT.portstate & 0x80 /* && !chip->mute_deltaT */) |
| 1345 |
YM_DELTAT_ADPCM_CALC(&chip->deltaT); |
| 1346 |
|
| 1347 |
chip->deltaTl[channel] = (out[2/*LEFT*/] + out[3/*CENTER*/]) / (1 << 8); |
| 1348 |
chip->deltaTr[channel] = (out[1/*RIGHT*/] + out[3/*CENTER*/]) / (1 << 8); |
| 1349 |
} |
| 1350 |
else |
| 1351 |
{ |
| 1352 |
chip->deltaTl[channel] = 0; |
| 1353 |
chip->deltaTr[channel] = 0; |
| 1354 |
} |
| 1355 |
} |
| 1356 |
|
| 1357 |
void OPN2_DoTimerA(ym3438_t *chip) |
| 1358 |
{ |
| 1359 |
Bit16u time; |
| 1360 |
Bit8u load; |
| 1361 |
load = chip->timer_a_overflow; |
| 1362 |
if (chip->cycles == 2) |
| 1363 |
{ |
| 1364 |
/* Lock load value */ |
| 1365 |
load |= (!chip->timer_a_load_lock && chip->timer_a_load); |
| 1366 |
chip->timer_a_load_lock = chip->timer_a_load; |
| 1367 |
if (chip->mode_csm) |
| 1368 |
{ |
| 1369 |
/* CSM KeyOn */ |
| 1370 |
chip->mode_kon_csm = load; |
| 1371 |
} |
| 1372 |
else |
| 1373 |
{ |
| 1374 |
chip->mode_kon_csm = 0; |
| 1375 |
} |
| 1376 |
} |
| 1377 |
/* Load counter */ |
| 1378 |
if (chip->timer_a_load_latch) |
| 1379 |
{ |
| 1380 |
time = chip->timer_a_reg; |
| 1381 |
} |
| 1382 |
else |
| 1383 |
{ |
| 1384 |
time = chip->timer_a_cnt; |
| 1385 |
} |
| 1386 |
chip->timer_a_load_latch = load; |
| 1387 |
/* Increase counter */ |
| 1388 |
if ((chip->cycles == 1 && chip->timer_a_load_lock) || chip->mode_test_21[2]) |
| 1389 |
{ |
| 1390 |
time++; |
| 1391 |
} |
| 1392 |
/* Set overflow flag */ |
| 1393 |
if (chip->timer_a_reset) |
| 1394 |
{ |
| 1395 |
chip->timer_a_reset = 0; |
| 1396 |
chip->timer_a_overflow_flag = 0; |
| 1397 |
} |
| 1398 |
else |
| 1399 |
{ |
| 1400 |
chip->timer_a_overflow_flag |= chip->timer_a_overflow & chip->timer_a_enable; |
| 1401 |
} |
| 1402 |
chip->timer_a_overflow = (time >> 10); |
| 1403 |
chip->timer_a_cnt = time & 0x3ff; |
| 1404 |
} |
| 1405 |
|
| 1406 |
void OPN2_DoTimerB(ym3438_t *chip) |
| 1407 |
{ |
| 1408 |
Bit16u time; |
| 1409 |
Bit8u load; |
| 1410 |
load = chip->timer_b_overflow; |
| 1411 |
if (chip->cycles == 2) |
| 1412 |
{ |
| 1413 |
/* Lock load value */ |
| 1414 |
load |= (!chip->timer_b_load_lock && chip->timer_b_load); |
| 1415 |
chip->timer_b_load_lock = chip->timer_b_load; |
| 1416 |
} |
| 1417 |
/* Load counter */ |
| 1418 |
if (chip->timer_b_load_latch) |
| 1419 |
{ |
| 1420 |
time = chip->timer_b_reg; |
| 1421 |
} |
| 1422 |
else |
| 1423 |
{ |
| 1424 |
time = chip->timer_b_cnt; |
| 1425 |
} |
| 1426 |
chip->timer_b_load_latch = load; |
| 1427 |
/* Increase counter */ |
| 1428 |
if (chip->cycles == 1) |
| 1429 |
{ |
| 1430 |
chip->timer_b_subcnt++; |
| 1431 |
} |
| 1432 |
if ((chip->timer_b_subcnt == 0x10 && chip->timer_b_load_lock) || chip->mode_test_21[2]) |
| 1433 |
{ |
| 1434 |
time++; |
| 1435 |
} |
| 1436 |
chip->timer_b_subcnt &= 0x0f; |
| 1437 |
/* Set overflow flag */ |
| 1438 |
if (chip->timer_b_reset) |
| 1439 |
{ |
| 1440 |
chip->timer_b_reset = 0; |
| 1441 |
chip->timer_b_overflow_flag = 0; |
| 1442 |
} |
| 1443 |
else |
| 1444 |
{ |
| 1445 |
chip->timer_b_overflow_flag |= chip->timer_b_overflow & chip->timer_b_enable; |
| 1446 |
} |
| 1447 |
chip->timer_b_overflow = (time >> 8); |
| 1448 |
chip->timer_b_cnt = time & 0xff; |
| 1449 |
} |
| 1450 |
|
| 1451 |
void OPN2_KeyOn(ym3438_t*chip) |
| 1452 |
{ |
| 1453 |
Bit32u slot = chip->cycles; |
| 1454 |
Bit32u chan = chip->channel; |
| 1455 |
/* Key On */ |
| 1456 |
chip->eg_kon_latch[slot] = chip->mode_kon[slot]; |
| 1457 |
chip->eg_kon_csm[slot] = 0; |
| 1458 |
if (chip->channel == 2 && chip->mode_kon_csm) |
| 1459 |
{ |
| 1460 |
/* CSM Key On */ |
| 1461 |
chip->eg_kon_latch[slot] = 1; |
| 1462 |
chip->eg_kon_csm[slot] = 1; |
| 1463 |
} |
| 1464 |
if (chip->cycles == chip->mode_kon_channel) |
| 1465 |
{ |
| 1466 |
/* OP1 */ |
| 1467 |
chip->mode_kon[chan] = chip->mode_kon_operator[0]; |
| 1468 |
/* OP2 */ |
| 1469 |
chip->mode_kon[chan + 12] = chip->mode_kon_operator[1]; |
| 1470 |
/* OP3 */ |
| 1471 |
chip->mode_kon[chan + 6] = chip->mode_kon_operator[2]; |
| 1472 |
/* OP4 */ |
| 1473 |
chip->mode_kon[chan + 18] = chip->mode_kon_operator[3]; |
| 1474 |
} |
| 1475 |
} |
| 1476 |
|
| 1477 |
static void OPNmod_deltat_status_set(void *userdata, Bit8u changebits) |
| 1478 |
{ |
| 1479 |
ym3438_t *chip = (ym3438_t *)userdata; |
| 1480 |
chip->status |= ~changebits; |
| 1481 |
} |
| 1482 |
|
| 1483 |
static void OPNmod_deltat_status_reset(void *userdata, Bit8u changebits) |
| 1484 |
{ |
| 1485 |
ym3438_t *chip = (ym3438_t *)userdata; |
| 1486 |
chip->status &= ~changebits; |
| 1487 |
} |
| 1488 |
|
| 1489 |
void OPN2_Reset(ym3438_t *chip, Bit32u clock, const struct OPN2mod_psg_callbacks *psg, void *psgdata, Bit32u dramsize) |
| 1490 |
{ |
| 1491 |
Bit32u i; |
| 1492 |
Bit8u *pcmmem = chip->deltaT.memory; |
| 1493 |
Bit32u pcmoldsize = chip->deltaT.memory_size; |
| 1494 |
memset(chip, 0, sizeof(ym3438_t)); |
| 1495 |
chip->deltaT.memory = pcmmem; |
| 1496 |
for (i = 0; i < 24; i++) |
| 1497 |
{ |
| 1498 |
chip->eg_out[i] = 0x3ff; |
| 1499 |
chip->eg_level[i] = 0x3ff; |
| 1500 |
chip->eg_state[i] = eg_num_release; |
| 1501 |
chip->multi[i] = 1; |
| 1502 |
} |
| 1503 |
for (i = 0; i < 6; i++) |
| 1504 |
{ |
| 1505 |
chip->pan_l[i] = 1; |
| 1506 |
chip->pan_r[i] = 1; |
| 1507 |
} |
| 1508 |
/*OPN-MOD: initialize and connect PSG*/ |
| 1509 |
chip->psg = psg; |
| 1510 |
chip->psgdata = psgdata; |
| 1511 |
psg->Reset(psgdata); |
| 1512 |
psg->SetClock(psgdata, clock / 4); |
| 1513 |
/*OPN-MOD: initialize rhythm*/ |
| 1514 |
for (i = 0; i < 6; i++) |
| 1515 |
{ |
| 1516 |
chip->rhythm_pan[i] = 3; |
| 1517 |
chip->rhythm_step[i] = (Bit32u)((1 << adpcm_shift) / ((i < 4) ? 3.0 : 6.0)); |
| 1518 |
OPNmod_RhythmUpdateVolume(chip, i); |
| 1519 |
} |
| 1520 |
|
| 1521 |
/*OPN-MOD: initialize ADPCM*/ |
| 1522 |
/*//chip->deltaT.memory = (UINT8 *)pcmrom; |
| 1523 |
//chip->deltaT.memory_size = pcmsize; |
| 1524 |
//chip->deltaT.memory = NULL; |
| 1525 |
//chip->deltaT.memory_size = 0x00; |
| 1526 |
//chip->deltaT.memory_mask = 0x00;*/ |
| 1527 |
chip->deltaT.memory = pcmmem = (Bit8u *)realloc(pcmmem, dramsize); |
| 1528 |
chip->deltaT.memory_size = dramsize; |
| 1529 |
if (!pcmmem) |
| 1530 |
abort(); |
| 1531 |
if (pcmoldsize < dramsize) |
| 1532 |
memset(pcmmem + pcmoldsize, 0, dramsize - pcmoldsize); |
| 1533 |
YM_DELTAT_calc_mem_mask(&chip->deltaT); |
| 1534 |
/*chip->deltaT.write_time = 20.0 / clock;*/ /* a single byte write takes 20 cycles of main clock */ |
| 1535 |
/*chip->deltaT.read_time = 18.0 / clock;*/ /* a single byte read takes 18 cycles of main clock */ |
| 1536 |
chip->deltaT.status_set_handler = &OPNmod_deltat_status_set; |
| 1537 |
chip->deltaT.status_reset_handler = &OPNmod_deltat_status_reset; |
| 1538 |
chip->deltaT.status_change_which_chip = chip; |
| 1539 |
chip->deltaT.status_change_EOS_bit = 0x04; /* status flag: set bit2 on End Of Sample */ |
| 1540 |
chip->deltaT.status_change_BRDY_bit = 0x08; /* status flag: set bit3 on BRDY */ |
| 1541 |
chip->deltaT.status_change_ZERO_bit = 0x10; /* status flag: set bit4 if silence continues for more than 290 miliseconds while recording the ADPCM */ |
| 1542 |
|
| 1543 |
YM_DELTAT_ADPCM_Init(&chip->deltaT,YM_DELTAT_EMULATION_MODE_NORMAL,5,chip->out_deltaT,1<<23); |
| 1544 |
chip->deltaT.freqbase = 1.0; /* TODO(jpc) set the freqbase value to fixed */ |
| 1545 |
YM_DELTAT_ADPCM_Reset(&chip->deltaT, 3/*CENTER*/); |
| 1546 |
} |
| 1547 |
|
| 1548 |
void OPN2_Destroy(ym3438_t *chip) |
| 1549 |
{ |
| 1550 |
if (!chip) |
| 1551 |
return; |
| 1552 |
|
| 1553 |
free(chip->deltaT.memory); |
| 1554 |
chip->deltaT.memory = NULL; |
| 1555 |
} |
| 1556 |
|
| 1557 |
void OPN2_SetChipType(Bit32u type) |
| 1558 |
{ |
| 1559 |
chip_type = type; |
| 1560 |
} |
| 1561 |
|
| 1562 |
void OPN2_Clock(ym3438_t *chip, Bit16s *buffer) |
| 1563 |
{ |
| 1564 |
Bit32u slot = chip->cycles; |
| 1565 |
Bit16s *rhythml = &chip->rhythml[chip->channel]; |
| 1566 |
Bit16s *rhythmr = &chip->rhythmr[chip->channel]; |
| 1567 |
Bit16s *deltaTl = &chip->deltaTl[chip->channel]; |
| 1568 |
Bit16s *deltaTr = &chip->deltaTr[chip->channel]; |
| 1569 |
chip->lfo_inc = chip->mode_test_21[1]; |
| 1570 |
chip->pg_read >>= 1; |
| 1571 |
chip->eg_read[1] >>= 1; |
| 1572 |
chip->eg_cycle++; |
| 1573 |
/* Lock envelope generator timer value */ |
| 1574 |
if (chip->cycles == 1 && chip->eg_quotient == 2) |
| 1575 |
{ |
| 1576 |
if (chip->eg_cycle_stop) |
| 1577 |
{ |
| 1578 |
chip->eg_shift_lock = 0; |
| 1579 |
} |
| 1580 |
else |
| 1581 |
{ |
| 1582 |
chip->eg_shift_lock = chip->eg_shift + 1; |
| 1583 |
} |
| 1584 |
chip->eg_timer_low_lock = chip->eg_timer & 0x03; |
| 1585 |
} |
| 1586 |
/* Cycle specific functions */ |
| 1587 |
switch (chip->cycles) |
| 1588 |
{ |
| 1589 |
case 0: |
| 1590 |
chip->lfo_pm = chip->lfo_cnt >> 2; |
| 1591 |
if (chip->lfo_cnt & 0x40) |
| 1592 |
{ |
| 1593 |
chip->lfo_am = chip->lfo_cnt & 0x3f; |
| 1594 |
} |
| 1595 |
else |
| 1596 |
{ |
| 1597 |
chip->lfo_am = chip->lfo_cnt ^ 0x3f; |
| 1598 |
} |
| 1599 |
chip->lfo_am <<= 1; |
| 1600 |
break; |
| 1601 |
case 1: |
| 1602 |
chip->eg_quotient++; |
| 1603 |
chip->eg_quotient %= 3; |
| 1604 |
chip->eg_cycle = 0; |
| 1605 |
chip->eg_cycle_stop = 1; |
| 1606 |
chip->eg_shift = 0; |
| 1607 |
chip->eg_timer_inc |= chip->eg_quotient >> 1; |
| 1608 |
chip->eg_timer = chip->eg_timer + chip->eg_timer_inc; |
| 1609 |
chip->eg_timer_inc = chip->eg_timer >> 12; |
| 1610 |
chip->eg_timer &= 0xfff; |
| 1611 |
break; |
| 1612 |
case 2: |
| 1613 |
chip->pg_read = chip->pg_phase[21] & 0x3ff; |
| 1614 |
chip->eg_read[1] = chip->eg_out[0]; |
| 1615 |
break; |
| 1616 |
case 13: |
| 1617 |
chip->eg_cycle = 0; |
| 1618 |
chip->eg_cycle_stop = 1; |
| 1619 |
chip->eg_shift = 0; |
| 1620 |
chip->eg_timer = chip->eg_timer + chip->eg_timer_inc; |
| 1621 |
chip->eg_timer_inc = chip->eg_timer >> 12; |
| 1622 |
chip->eg_timer &= 0xfff; |
| 1623 |
break; |
| 1624 |
case 23: |
| 1625 |
chip->lfo_inc |= 1; |
| 1626 |
break; |
| 1627 |
} |
| 1628 |
chip->eg_timer &= ~(chip->mode_test_21[5] << chip->eg_cycle); |
| 1629 |
if (((chip->eg_timer >> chip->eg_cycle) | (chip->pin_test_in & chip->eg_custom_timer)) & chip->eg_cycle_stop) |
| 1630 |
{ |
| 1631 |
chip->eg_shift = chip->eg_cycle; |
| 1632 |
chip->eg_cycle_stop = 0; |
| 1633 |
} |
| 1634 |
|
| 1635 |
OPN2_DoIO(chip); |
| 1636 |
|
| 1637 |
OPN2_DoTimerA(chip); |
| 1638 |
OPN2_DoTimerB(chip); |
| 1639 |
OPN2_KeyOn(chip); |
| 1640 |
|
| 1641 |
OPN2_ChOutput(chip); |
| 1642 |
OPN2_ChGenerate(chip); |
| 1643 |
|
| 1644 |
OPN2_FMPrepare(chip); |
| 1645 |
OPN2_FMGenerate(chip); |
| 1646 |
|
| 1647 |
OPNmod_RhythmGenerate(chip); |
| 1648 |
OPNmod_DeltaTGenerate(chip); |
| 1649 |
|
| 1650 |
OPN2_PhaseGenerate(chip); |
| 1651 |
OPN2_PhaseCalcIncrement(chip); |
| 1652 |
|
| 1653 |
OPN2_EnvelopeADSR(chip); |
| 1654 |
OPN2_EnvelopeGenerate(chip); |
| 1655 |
OPN2_EnvelopeSSGEG(chip); |
| 1656 |
OPN2_EnvelopePrepare(chip); |
| 1657 |
|
| 1658 |
/* Prepare fnum & block */ |
| 1659 |
if (chip->mode_ch3) |
| 1660 |
{ |
| 1661 |
/* Channel 3 special mode */ |
| 1662 |
switch (slot) |
| 1663 |
{ |
| 1664 |
case 1: /* OP1 */ |
| 1665 |
chip->pg_fnum = chip->fnum_3ch[1]; |
| 1666 |
chip->pg_block = chip->block_3ch[1]; |
| 1667 |
chip->pg_kcode = chip->kcode_3ch[1]; |
| 1668 |
break; |
| 1669 |
case 7: /* OP3 */ |
| 1670 |
chip->pg_fnum = chip->fnum_3ch[0]; |
| 1671 |
chip->pg_block = chip->block_3ch[0]; |
| 1672 |
chip->pg_kcode = chip->kcode_3ch[0]; |
| 1673 |
break; |
| 1674 |
case 13: /* OP2 */ |
| 1675 |
chip->pg_fnum = chip->fnum_3ch[2]; |
| 1676 |
chip->pg_block = chip->block_3ch[2]; |
| 1677 |
chip->pg_kcode = chip->kcode_3ch[2]; |
| 1678 |
break; |
| 1679 |
case 19: /* OP4 */ |
| 1680 |
default: |
| 1681 |
chip->pg_fnum = chip->fnum[(chip->channel + 1) % 6]; |
| 1682 |
chip->pg_block = chip->block[(chip->channel + 1) % 6]; |
| 1683 |
chip->pg_kcode = chip->kcode[(chip->channel + 1) % 6]; |
| 1684 |
break; |
| 1685 |
} |
| 1686 |
} |
| 1687 |
else |
| 1688 |
{ |
| 1689 |
chip->pg_fnum = chip->fnum[(chip->channel + 1) % 6]; |
| 1690 |
chip->pg_block = chip->block[(chip->channel + 1) % 6]; |
| 1691 |
chip->pg_kcode = chip->kcode[(chip->channel + 1) % 6]; |
| 1692 |
} |
| 1693 |
|
| 1694 |
OPN2_UpdateLFO(chip); |
| 1695 |
OPN2_DoRegWrite(chip); |
| 1696 |
chip->cycles = (chip->cycles + 1) % 24; |
| 1697 |
chip->channel = chip->cycles % 6; |
| 1698 |
|
| 1699 |
buffer[0] = chip->mol; |
| 1700 |
buffer[1] = chip->mor; |
| 1701 |
/*OPN-MOD: Rhythm output*/ |
| 1702 |
buffer[2] = *rhythml; |
| 1703 |
buffer[3] = *rhythmr; |
| 1704 |
/*OPN-MOD: DeltaT output*/ |
| 1705 |
buffer[4] = *deltaTl; |
| 1706 |
buffer[5] = *deltaTr; |
| 1707 |
|
| 1708 |
if (chip->status_time) |
| 1709 |
chip->status_time--; |
| 1710 |
} |
| 1711 |
|
| 1712 |
void OPN2_Write(ym3438_t *chip, Bit32u port, Bit8u data) |
| 1713 |
{ |
| 1714 |
port &= 3; |
| 1715 |
chip->write_data = ((port << 7) & 0x100) | data; |
| 1716 |
if (port & 1) |
| 1717 |
{ |
| 1718 |
/* Data */ |
| 1719 |
chip->write_d |= 1; |
| 1720 |
} |
| 1721 |
else |
| 1722 |
{ |
| 1723 |
/* Address */ |
| 1724 |
chip->write_a |= 1; |
| 1725 |
} |
| 1726 |
} |
| 1727 |
|
| 1728 |
void OPN2_SetTestPin(ym3438_t *chip, Bit32u value) |
| 1729 |
{ |
| 1730 |
chip->pin_test_in = value & 1; |
| 1731 |
} |
| 1732 |
|
| 1733 |
Bit32u OPN2_ReadTestPin(ym3438_t *chip) |
| 1734 |
{ |
| 1735 |
if (!chip->mode_test_2c[7]) |
| 1736 |
{ |
| 1737 |
return 0; |
| 1738 |
} |
| 1739 |
return chip->cycles == 23; |
| 1740 |
} |
| 1741 |
|
| 1742 |
Bit32u OPN2_ReadIRQPin(ym3438_t *chip) |
| 1743 |
{ |
| 1744 |
return chip->timer_a_overflow_flag | chip->timer_b_overflow_flag; |
| 1745 |
} |
| 1746 |
|
| 1747 |
Bit8u OPN2_Read(ym3438_t *chip, Bit32u port) |
| 1748 |
{ |
| 1749 |
Bit32u slot; |
| 1750 |
Bit16u testdata; |
| 1751 |
|
| 1752 |
if ((port & 3) == 0 || (chip_type & ym3438_mode_readmode)) |
| 1753 |
{ |
| 1754 |
if (chip->mode_test_21[6]) |
| 1755 |
{ |
| 1756 |
/* Read test data */ |
| 1757 |
/*Bit32u slot = (chip->cycles + 18) % 24; |
| 1758 |
Bit16u testdata = ((chip->pg_read & 0x01) << 15) |
| 1759 |
| ((chip->eg_read[chip->mode_test_21[0]] & 0x01) << 14);*/ |
| 1760 |
slot = (chip->cycles + 18) % 24; |
| 1761 |
testdata = ((chip->pg_read & 0x01) << 15) |
| 1762 |
| ((chip->eg_read[chip->mode_test_21[0]] & 0x01) << 14); |
| 1763 |
if (chip->mode_test_2c[4]) |
| 1764 |
{ |
| 1765 |
testdata |= chip->ch_read & 0x1ff; |
| 1766 |
} |
| 1767 |
else |
| 1768 |
{ |
| 1769 |
testdata |= chip->fm_out[slot] & 0x3fff; |
| 1770 |
} |
| 1771 |
if (chip->mode_test_21[7]) |
| 1772 |
{ |
| 1773 |
chip->status = testdata & 0xff; |
| 1774 |
} |
| 1775 |
else |
| 1776 |
{ |
| 1777 |
chip->status = testdata >> 8; |
| 1778 |
} |
| 1779 |
} |
| 1780 |
else |
| 1781 |
{ |
| 1782 |
chip->status = (chip->busy << 7) | (chip->timer_b_overflow_flag << 1) |
| 1783 |
| chip->timer_a_overflow_flag; |
| 1784 |
} |
| 1785 |
|
| 1786 |
/*OPN-MOD: status of DeltaT */ |
| 1787 |
chip->status |= (chip->deltaT.PCM_BSY & 1) << 5; |
| 1788 |
|
| 1789 |
if (chip_type & ym3438_mode_ym2612) |
| 1790 |
{ |
| 1791 |
chip->status_time = 300000; |
| 1792 |
} |
| 1793 |
else |
| 1794 |
{ |
| 1795 |
chip->status_time = 40000000; |
| 1796 |
} |
| 1797 |
} |
| 1798 |
/*OPN-MOD: read from PSG*/ |
| 1799 |
if ((port & 3) == 1) |
| 1800 |
{ |
| 1801 |
if (chip->address < 16) |
| 1802 |
{ |
| 1803 |
return chip->psg->Read(chip->psgdata); |
| 1804 |
} |
| 1805 |
else if(chip->address == 0xff) |
| 1806 |
{ |
| 1807 |
return 1; /* ID code */ |
| 1808 |
} |
| 1809 |
} |
| 1810 |
/*OPN-MOD: read from DeltaT*/ |
| 1811 |
if ((port & 3) == 3) |
| 1812 |
{ |
| 1813 |
if (chip->address == 0x08) |
| 1814 |
{ |
| 1815 |
return YM_DELTAT_ADPCM_Read(&chip->deltaT); |
| 1816 |
} |
| 1817 |
} |
| 1818 |
if (chip->status_time) |
| 1819 |
{ |
| 1820 |
return chip->status; |
| 1821 |
} |
| 1822 |
return 0; |
| 1823 |
} |
| 1824 |
|
| 1825 |
void OPN2_WriteBuffered(ym3438_t *chip, Bit32u port, Bit8u data) |
| 1826 |
{ |
| 1827 |
Bit64u time1, time2; |
| 1828 |
Bit16s buffer[6]; |
| 1829 |
Bit64u skip; |
| 1830 |
|
| 1831 |
if (chip->writebuf[chip->writebuf_last].port & 0x04) |
| 1832 |
{ |
| 1833 |
OPN2_Write(chip, chip->writebuf[chip->writebuf_last].port & 0X03, |
| 1834 |
chip->writebuf[chip->writebuf_last].data); |
| 1835 |
|
| 1836 |
chip->writebuf_cur = (chip->writebuf_last + 1) % OPN_WRITEBUF_SIZE; |
| 1837 |
skip = chip->writebuf[chip->writebuf_last].time - chip->writebuf_samplecnt; |
| 1838 |
chip->writebuf_samplecnt = chip->writebuf[chip->writebuf_last].time; |
| 1839 |
while (skip--) |
| 1840 |
{ |
| 1841 |
OPN2_Clock(chip, buffer); |
| 1842 |
} |
| 1843 |
} |
| 1844 |
|
| 1845 |
chip->writebuf[chip->writebuf_last].port = (port & 0x03) | 0x04; |
| 1846 |
chip->writebuf[chip->writebuf_last].data = data; |
| 1847 |
time1 = chip->writebuf_lasttime + OPN_WRITEBUF_DELAY; |
| 1848 |
time2 = chip->writebuf_samplecnt; |
| 1849 |
|
| 1850 |
if (time1 < time2) |
| 1851 |
{ |
| 1852 |
time1 = time2; |
| 1853 |
} |
| 1854 |
|
| 1855 |
chip->writebuf[chip->writebuf_last].time = time1; |
| 1856 |
chip->writebuf_lasttime = time1; |
| 1857 |
chip->writebuf_last = (chip->writebuf_last + 1) % OPN_WRITEBUF_SIZE; |
| 1858 |
} |
| 1859 |
|
| 1860 |
void OPN2_FlushBuffer(ym3438_t *chip) |
| 1861 |
{ |
| 1862 |
Bit16s buffer[6]; |
| 1863 |
Bit64u skip; |
| 1864 |
|
| 1865 |
while (chip->writebuf[chip->writebuf_cur].port & 0x04) |
| 1866 |
{ |
| 1867 |
chip->writebuf[chip->writebuf_cur].port &= 0x03; |
| 1868 |
|
| 1869 |
OPN2_Write(chip, chip->writebuf[chip->writebuf_cur].port, |
| 1870 |
chip->writebuf[chip->writebuf_cur].data); |
| 1871 |
|
| 1872 |
skip = chip->writebuf[chip->writebuf_cur].time - chip->writebuf_samplecnt; |
| 1873 |
chip->writebuf_samplecnt = chip->writebuf[chip->writebuf_cur].time; |
| 1874 |
chip->writebuf_cur = (chip->writebuf_cur + 1) % OPN_WRITEBUF_SIZE; |
| 1875 |
while (skip--) |
| 1876 |
{ |
| 1877 |
OPN2_Clock(chip, buffer); |
| 1878 |
} |
| 1879 |
} |
| 1880 |
} |
| 1881 |
|
| 1882 |
void OPN2_Generate(ym3438_t *chip, sample *samples) |
| 1883 |
{ |
| 1884 |
Bit32u i; |
| 1885 |
Bit16s buffer[6]; |
| 1886 |
|
| 1887 |
samples[0] = 0; |
| 1888 |
samples[1] = 0; |
| 1889 |
|
| 1890 |
for (i = 0; i < 24; i++) |
| 1891 |
{ |
| 1892 |
OPN2_Clock(chip, buffer); |
| 1893 |
|
| 1894 |
/*OPN-MOD: adjust FM output to mix*/ |
| 1895 |
samples[0] += (buffer[0] * 11) >> 4; |
| 1896 |
samples[1] += (buffer[1] * 11) >> 4; |
| 1897 |
|
| 1898 |
/*OPN-MOD: mix rhythm samples*/ |
| 1899 |
samples[0] += buffer[2]; |
| 1900 |
samples[1] += buffer[3]; |
| 1901 |
|
| 1902 |
/*OPN-MOD: mix deltaT samples*/ |
| 1903 |
samples[0] += buffer[4]; |
| 1904 |
samples[1] += buffer[5]; |
| 1905 |
|
| 1906 |
while (chip->writebuf[chip->writebuf_cur].time <= chip->writebuf_samplecnt) |
| 1907 |
{ |
| 1908 |
if (!(chip->writebuf[chip->writebuf_cur].port & 0x04)) |
| 1909 |
{ |
| 1910 |
break; |
| 1911 |
} |
| 1912 |
chip->writebuf[chip->writebuf_cur].port &= 0x03; |
| 1913 |
OPN2_Write(chip, chip->writebuf[chip->writebuf_cur].port, |
| 1914 |
chip->writebuf[chip->writebuf_cur].data); |
| 1915 |
chip->writebuf_cur = (chip->writebuf_cur + 1) % OPN_WRITEBUF_SIZE; |
| 1916 |
} |
| 1917 |
chip->writebuf_samplecnt++; |
| 1918 |
} |
| 1919 |
} |