| 1 |
/* |
| 2 |
* Copyright (C) 2017-2022 Alexey Khokholov (Nuke.YKT) |
| 3 |
* |
| 4 |
* This file is part of Nuked OPN2. |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
* |
| 20 |
* Nuked OPN2(Yamaha YM3438) emulator. |
| 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.11 |
| 28 |
*/ |
| 29 |
|
| 30 |
#include <string.h> |
| 31 |
#include "ym3438.h" |
| 32 |
|
| 33 |
#define SIGN_EXTEND(bit_index, value) (((value) & ((1u << (bit_index)) - 1u)) - ((value) & (1u << (bit_index)))) |
| 34 |
|
| 35 |
enum { |
| 36 |
eg_num_attack = 0, |
| 37 |
eg_num_decay = 1, |
| 38 |
eg_num_sustain = 2, |
| 39 |
eg_num_release = 3 |
| 40 |
}; |
| 41 |
|
| 42 |
/* msw table */ |
| 43 |
static const Bit16u mswrom[256] = { |
| 44 |
0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, |
| 45 |
0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, |
| 46 |
0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, |
| 47 |
0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, |
| 48 |
0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, |
| 49 |
0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, |
| 50 |
0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, |
| 51 |
0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, |
| 52 |
0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, |
| 53 |
0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, |
| 54 |
0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, |
| 55 |
0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, |
| 56 |
0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, |
| 57 |
0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, |
| 58 |
0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, |
| 59 |
0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, 0x600, |
| 60 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 61 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 62 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 63 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 64 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 65 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 66 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 67 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 68 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 69 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 70 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 71 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 72 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 73 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 74 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, |
| 75 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000 |
| 76 |
}; |
| 77 |
|
| 78 |
/* logsin table */ |
| 79 |
static const Bit16u logsinrom[256] = { |
| 80 |
0x859, 0x6c3, 0x607, 0x58b, 0x52e, 0x4e4, 0x4a6, 0x471, |
| 81 |
0x443, 0x41a, 0x3f5, 0x3d3, 0x3b5, 0x398, 0x37e, 0x365, |
| 82 |
0x34e, 0x339, 0x324, 0x311, 0x2ff, 0x2ed, 0x2dc, 0x2cd, |
| 83 |
0x2bd, 0x2af, 0x2a0, 0x293, 0x286, 0x279, 0x26d, 0x261, |
| 84 |
0x256, 0x24b, 0x240, 0x236, 0x22c, 0x222, 0x218, 0x20f, |
| 85 |
0x206, 0x1fd, 0x1f5, 0x1ec, 0x1e4, 0x1dc, 0x1d4, 0x1cd, |
| 86 |
0x1c5, 0x1be, 0x1b7, 0x1b0, 0x1a9, 0x1a2, 0x19b, 0x195, |
| 87 |
0x18f, 0x188, 0x182, 0x17c, 0x177, 0x171, 0x16b, 0x166, |
| 88 |
0x160, 0x15b, 0x155, 0x150, 0x14b, 0x146, 0x141, 0x13c, |
| 89 |
0x137, 0x133, 0x12e, 0x129, 0x125, 0x121, 0x11c, 0x118, |
| 90 |
0x114, 0x10f, 0x10b, 0x107, 0x103, 0x0ff, 0x0fb, 0x0f8, |
| 91 |
0x0f4, 0x0f0, 0x0ec, 0x0e9, 0x0e5, 0x0e2, 0x0de, 0x0db, |
| 92 |
0x0d7, 0x0d4, 0x0d1, 0x0cd, 0x0ca, 0x0c7, 0x0c4, 0x0c1, |
| 93 |
0x0be, 0x0bb, 0x0b8, 0x0b5, 0x0b2, 0x0af, 0x0ac, 0x0a9, |
| 94 |
0x0a7, 0x0a4, 0x0a1, 0x09f, 0x09c, 0x099, 0x097, 0x094, |
| 95 |
0x092, 0x08f, 0x08d, 0x08a, 0x088, 0x086, 0x083, 0x081, |
| 96 |
0x07f, 0x07d, 0x07a, 0x078, 0x076, 0x074, 0x072, 0x070, |
| 97 |
0x06e, 0x06c, 0x06a, 0x068, 0x066, 0x064, 0x062, 0x060, |
| 98 |
0x05e, 0x05c, 0x05b, 0x059, 0x057, 0x055, 0x053, 0x052, |
| 99 |
0x050, 0x04e, 0x04d, 0x04b, 0x04a, 0x048, 0x046, 0x045, |
| 100 |
0x043, 0x042, 0x040, 0x03f, 0x03e, 0x03c, 0x03b, 0x039, |
| 101 |
0x038, 0x037, 0x035, 0x034, 0x033, 0x031, 0x030, 0x02f, |
| 102 |
0x02e, 0x02d, 0x02b, 0x02a, 0x029, 0x028, 0x027, 0x026, |
| 103 |
0x025, 0x024, 0x023, 0x022, 0x021, 0x020, 0x01f, 0x01e, |
| 104 |
0x01d, 0x01c, 0x01b, 0x01a, 0x019, 0x018, 0x017, 0x017, |
| 105 |
0x016, 0x015, 0x014, 0x014, 0x013, 0x012, 0x011, 0x011, |
| 106 |
0x010, 0x00f, 0x00f, 0x00e, 0x00d, 0x00d, 0x00c, 0x00c, |
| 107 |
0x00b, 0x00a, 0x00a, 0x009, 0x009, 0x008, 0x008, 0x007, |
| 108 |
0x007, 0x007, 0x006, 0x006, 0x005, 0x005, 0x005, 0x004, |
| 109 |
0x004, 0x004, 0x003, 0x003, 0x003, 0x002, 0x002, 0x002, |
| 110 |
0x002, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, |
| 111 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000 |
| 112 |
}; |
| 113 |
|
| 114 |
/* exp table */ |
| 115 |
static const Bit16u exprom[256] = { |
| 116 |
0x000, 0x003, 0x006, 0x008, 0x00b, 0x00e, 0x011, 0x014, |
| 117 |
0x016, 0x019, 0x01c, 0x01f, 0x022, 0x025, 0x028, 0x02a, |
| 118 |
0x02d, 0x030, 0x033, 0x036, 0x039, 0x03c, 0x03f, 0x042, |
| 119 |
0x045, 0x048, 0x04b, 0x04e, 0x051, 0x054, 0x057, 0x05a, |
| 120 |
0x05d, 0x060, 0x063, 0x066, 0x069, 0x06c, 0x06f, 0x072, |
| 121 |
0x075, 0x078, 0x07b, 0x07e, 0x082, 0x085, 0x088, 0x08b, |
| 122 |
0x08e, 0x091, 0x094, 0x098, 0x09b, 0x09e, 0x0a1, 0x0a4, |
| 123 |
0x0a8, 0x0ab, 0x0ae, 0x0b1, 0x0b5, 0x0b8, 0x0bb, 0x0be, |
| 124 |
0x0c2, 0x0c5, 0x0c8, 0x0cc, 0x0cf, 0x0d2, 0x0d6, 0x0d9, |
| 125 |
0x0dc, 0x0e0, 0x0e3, 0x0e7, 0x0ea, 0x0ed, 0x0f1, 0x0f4, |
| 126 |
0x0f8, 0x0fb, 0x0ff, 0x102, 0x106, 0x109, 0x10c, 0x110, |
| 127 |
0x114, 0x117, 0x11b, 0x11e, 0x122, 0x125, 0x129, 0x12c, |
| 128 |
0x130, 0x134, 0x137, 0x13b, 0x13e, 0x142, 0x146, 0x149, |
| 129 |
0x14d, 0x151, 0x154, 0x158, 0x15c, 0x160, 0x163, 0x167, |
| 130 |
0x16b, 0x16f, 0x172, 0x176, 0x17a, 0x17e, 0x181, 0x185, |
| 131 |
0x189, 0x18d, 0x191, 0x195, 0x199, 0x19c, 0x1a0, 0x1a4, |
| 132 |
0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc, 0x1c0, 0x1c4, |
| 133 |
0x1c8, 0x1cc, 0x1d0, 0x1d4, 0x1d8, 0x1dc, 0x1e0, 0x1e4, |
| 134 |
0x1e8, 0x1ec, 0x1f0, 0x1f5, 0x1f9, 0x1fd, 0x201, 0x205, |
| 135 |
0x209, 0x20e, 0x212, 0x216, 0x21a, 0x21e, 0x223, 0x227, |
| 136 |
0x22b, 0x230, 0x234, 0x238, 0x23c, 0x241, 0x245, 0x249, |
| 137 |
0x24e, 0x252, 0x257, 0x25b, 0x25f, 0x264, 0x268, 0x26d, |
| 138 |
0x271, 0x276, 0x27a, 0x27f, 0x283, 0x288, 0x28c, 0x291, |
| 139 |
0x295, 0x29a, 0x29e, 0x2a3, 0x2a8, 0x2ac, 0x2b1, 0x2b5, |
| 140 |
0x2ba, 0x2bf, 0x2c4, 0x2c8, 0x2cd, 0x2d2, 0x2d6, 0x2db, |
| 141 |
0x2e0, 0x2e5, 0x2e9, 0x2ee, 0x2f3, 0x2f8, 0x2fd, 0x302, |
| 142 |
0x306, 0x30b, 0x310, 0x315, 0x31a, 0x31f, 0x324, 0x329, |
| 143 |
0x32e, 0x333, 0x338, 0x33d, 0x342, 0x347, 0x34c, 0x351, |
| 144 |
0x356, 0x35b, 0x360, 0x365, 0x36a, 0x370, 0x375, 0x37a, |
| 145 |
0x37f, 0x384, 0x38a, 0x38f, 0x394, 0x399, 0x39f, 0x3a4, |
| 146 |
0x3a9, 0x3ae, 0x3b4, 0x3b9, 0x3bf, 0x3c4, 0x3c9, 0x3cf, |
| 147 |
0x3d4, 0x3da, 0x3df, 0x3e4, 0x3ea, 0x3ef, 0x3f5, 0x3fa |
| 148 |
}; |
| 149 |
|
| 150 |
/* Note table */ |
| 151 |
static const Bit32u fn_note[16] = { |
| 152 |
0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3 |
| 153 |
}; |
| 154 |
|
| 155 |
/* Envelope generator */ |
| 156 |
static const Bit32u eg_stephi[4][4] = { |
| 157 |
{ 0, 0, 0, 0 }, |
| 158 |
{ 1, 0, 0, 0 }, |
| 159 |
{ 1, 0, 1, 0 }, |
| 160 |
{ 1, 1, 1, 0 } |
| 161 |
}; |
| 162 |
|
| 163 |
static const Bit8u eg_am_shift[4] = { |
| 164 |
7, 3, 1, 0 |
| 165 |
}; |
| 166 |
|
| 167 |
/* Phase generator */ |
| 168 |
static const Bit32u pg_detune[8] = { 16, 17, 19, 20, 22, 24, 27, 29 }; |
| 169 |
|
| 170 |
static const Bit32u pg_lfo_sh1[8][8] = { |
| 171 |
{ 7, 7, 7, 7, 7, 7, 7, 7 }, |
| 172 |
{ 7, 7, 7, 7, 7, 7, 7, 7 }, |
| 173 |
{ 7, 7, 7, 7, 7, 7, 1, 1 }, |
| 174 |
{ 7, 7, 7, 7, 1, 1, 1, 1 }, |
| 175 |
{ 7, 7, 7, 1, 1, 1, 1, 0 }, |
| 176 |
{ 7, 7, 1, 1, 0, 0, 0, 0 }, |
| 177 |
{ 7, 7, 1, 1, 0, 0, 0, 0 }, |
| 178 |
{ 7, 7, 1, 1, 0, 0, 0, 0 } |
| 179 |
}; |
| 180 |
|
| 181 |
static const Bit32u pg_lfo_sh2[8][8] = { |
| 182 |
{ 7, 7, 7, 7, 7, 7, 7, 7 }, |
| 183 |
{ 7, 7, 7, 7, 2, 2, 2, 2 }, |
| 184 |
{ 7, 7, 7, 2, 2, 2, 7, 7 }, |
| 185 |
{ 7, 7, 2, 2, 7, 7, 2, 2 }, |
| 186 |
{ 7, 7, 2, 7, 7, 7, 2, 7 }, |
| 187 |
{ 7, 7, 7, 2, 7, 7, 2, 1 }, |
| 188 |
{ 7, 7, 7, 2, 7, 7, 2, 1 }, |
| 189 |
{ 7, 7, 7, 2, 7, 7, 2, 1 } |
| 190 |
}; |
| 191 |
|
| 192 |
/* Address decoder */ |
| 193 |
static const Bit32u op_offset[12] = { |
| 194 |
0x000, /* Ch1 OP1/OP2 */ |
| 195 |
0x001, /* Ch2 OP1/OP2 */ |
| 196 |
0x002, /* Ch3 OP1/OP2 */ |
| 197 |
0x100, /* Ch4 OP1/OP2 */ |
| 198 |
0x101, /* Ch5 OP1/OP2 */ |
| 199 |
0x102, /* Ch6 OP1/OP2 */ |
| 200 |
0x004, /* Ch1 OP3/OP4 */ |
| 201 |
0x005, /* Ch2 OP3/OP4 */ |
| 202 |
0x006, /* Ch3 OP3/OP4 */ |
| 203 |
0x104, /* Ch4 OP3/OP4 */ |
| 204 |
0x105, /* Ch5 OP3/OP4 */ |
| 205 |
0x106 /* Ch6 OP3/OP4 */ |
| 206 |
}; |
| 207 |
|
| 208 |
static const Bit32u ch_offset[6] = { |
| 209 |
0x000, /* Ch1 */ |
| 210 |
0x001, /* Ch2 */ |
| 211 |
0x002, /* Ch3 */ |
| 212 |
0x100, /* Ch4 */ |
| 213 |
0x101, /* Ch5 */ |
| 214 |
0x102 /* Ch6 */ |
| 215 |
}; |
| 216 |
|
| 217 |
/* LFO */ |
| 218 |
static const Bit32u lfo_cycles[8] = { |
| 219 |
108, 77, 71, 67, 62, 44, 8, 5 |
| 220 |
}; |
| 221 |
|
| 222 |
/* FM algorithm */ |
| 223 |
static const Bit32u fm_algorithm[4][6][8] = { |
| 224 |
{ |
| 225 |
{ 1, 1, 1, 1, 1, 1, 1, 1 }, /* OP1_0 */ |
| 226 |
{ 1, 1, 1, 1, 1, 1, 1, 1 }, /* OP1_1 */ |
| 227 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* OP2 */ |
| 228 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */ |
| 229 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */ |
| 230 |
{ 0, 0, 0, 0, 0, 0, 0, 1 } /* Out */ |
| 231 |
}, |
| 232 |
{ |
| 233 |
{ 0, 1, 0, 0, 0, 1, 0, 0 }, /* OP1_0 */ |
| 234 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* OP1_1 */ |
| 235 |
{ 1, 1, 1, 0, 0, 0, 0, 0 }, /* OP2 */ |
| 236 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */ |
| 237 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */ |
| 238 |
{ 0, 0, 0, 0, 0, 1, 1, 1 } /* Out */ |
| 239 |
}, |
| 240 |
{ |
| 241 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* OP1_0 */ |
| 242 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* OP1_1 */ |
| 243 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* OP2 */ |
| 244 |
{ 1, 0, 0, 1, 1, 1, 1, 0 }, /* Last operator */ |
| 245 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */ |
| 246 |
{ 0, 0, 0, 0, 1, 1, 1, 1 } /* Out */ |
| 247 |
}, |
| 248 |
{ |
| 249 |
{ 0, 0, 1, 0, 0, 1, 0, 0 }, /* OP1_0 */ |
| 250 |
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* OP1_1 */ |
| 251 |
{ 0, 0, 0, 1, 0, 0, 0, 0 }, /* OP2 */ |
| 252 |
{ 1, 1, 0, 1, 1, 0, 0, 0 }, /* Last operator */ |
| 253 |
{ 0, 0, 1, 0, 0, 0, 0, 0 }, /* Last operator */ |
| 254 |
{ 1, 1, 1, 1, 1, 1, 1, 1 } /* Out */ |
| 255 |
} |
| 256 |
}; |
| 257 |
|
| 258 |
static void OPN2_DoIO(ym3438_t *chip) |
| 259 |
{ |
| 260 |
/* Write signal check */ |
| 261 |
chip->write_a_en = (chip->write_a & 0x03) == 0x01; |
| 262 |
chip->write_d_en = (chip->write_d & 0x03) == 0x01; |
| 263 |
chip->write_a <<= 1; |
| 264 |
chip->write_d <<= 1; |
| 265 |
/* Busy counter */ |
| 266 |
chip->busy = chip->write_busy; |
| 267 |
chip->write_busy_cnt += chip->write_busy; |
| 268 |
chip->write_busy = (chip->write_busy && !(chip->write_busy_cnt >> 5)) || chip->write_d_en; |
| 269 |
chip->write_busy_cnt &= 0x1f; |
| 270 |
} |
| 271 |
|
| 272 |
static void OPN2_DoRegWrite(ym3438_t *chip) |
| 273 |
{ |
| 274 |
Bit32u i; |
| 275 |
Bit32u slot = chip->cycles % 12; |
| 276 |
Bit32u address; |
| 277 |
Bit32u channel = chip->channel; |
| 278 |
/* Update registers */ |
| 279 |
if (chip->write_fm_data) |
| 280 |
{ |
| 281 |
/* Slot */ |
| 282 |
if (op_offset[slot] == (chip->address & 0x107)) |
| 283 |
{ |
| 284 |
if (chip->address & 0x08) |
| 285 |
{ |
| 286 |
/* OP2, OP4 */ |
| 287 |
slot += 12; |
| 288 |
} |
| 289 |
address = chip->address & 0xf0; |
| 290 |
switch (address) |
| 291 |
{ |
| 292 |
case 0x30: /* DT, MULTI */ |
| 293 |
chip->multi[slot] = chip->data & 0x0f; |
| 294 |
if (!chip->multi[slot]) |
| 295 |
{ |
| 296 |
chip->multi[slot] = 1; |
| 297 |
} |
| 298 |
else |
| 299 |
{ |
| 300 |
chip->multi[slot] <<= 1; |
| 301 |
} |
| 302 |
chip->dt[slot] = (chip->data >> 4) & 0x07; |
| 303 |
break; |
| 304 |
case 0x40: /* TL */ |
| 305 |
chip->tl[slot] = chip->data & 0x7f; |
| 306 |
break; |
| 307 |
case 0x50: /* KS, AR */ |
| 308 |
chip->ar[slot] = chip->data & 0x1f; |
| 309 |
chip->ks[slot] = (chip->data >> 6) & 0x03; |
| 310 |
break; |
| 311 |
case 0x60: /* AM, DR */ |
| 312 |
chip->dr[slot] = chip->data & 0x1f; |
| 313 |
chip->am[slot] = (chip->data >> 7) & 0x01; |
| 314 |
break; |
| 315 |
case 0x70: /* SR */ |
| 316 |
chip->sr[slot] = chip->data & 0x1f; |
| 317 |
break; |
| 318 |
case 0x80: /* SL, RR */ |
| 319 |
chip->rr[slot] = chip->data & 0x0f; |
| 320 |
chip->sl[slot] = (chip->data >> 4) & 0x0f; |
| 321 |
chip->sl[slot] |= (chip->sl[slot] + 1) & 0x10; |
| 322 |
break; |
| 323 |
case 0x90: /* SSG-EG */ |
| 324 |
chip->ssg_eg[slot] = chip->data & 0x0f; |
| 325 |
break; |
| 326 |
default: |
| 327 |
break; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
/* Channel */ |
| 332 |
if (ch_offset[channel] == (chip->address & 0x103)) |
| 333 |
{ |
| 334 |
address = chip->address & 0xfc; |
| 335 |
switch (address) |
| 336 |
{ |
| 337 |
case 0xa0: |
| 338 |
chip->fnum[channel] = (chip->data & 0xff) | ((chip->reg_a4 & 0x07) << 8); |
| 339 |
chip->block[channel] = (chip->reg_a4 >> 3) & 0x07; |
| 340 |
chip->kcode[channel] = (chip->block[channel] << 2) | fn_note[chip->fnum[channel] >> 7]; |
| 341 |
break; |
| 342 |
case 0xa4: |
| 343 |
chip->reg_a4 = chip->data & 0xff; |
| 344 |
break; |
| 345 |
case 0xa8: |
| 346 |
chip->fnum_3ch[channel] = (chip->data & 0xff) | ((chip->reg_ac & 0x07) << 8); |
| 347 |
chip->block_3ch[channel] = (chip->reg_ac >> 3) & 0x07; |
| 348 |
chip->kcode_3ch[channel] = (chip->block_3ch[channel] << 2) | fn_note[chip->fnum_3ch[channel] >> 7]; |
| 349 |
break; |
| 350 |
case 0xac: |
| 351 |
chip->reg_ac = chip->data & 0xff; |
| 352 |
break; |
| 353 |
case 0xb0: |
| 354 |
chip->connect[channel] = chip->data & 0x07; |
| 355 |
chip->fb[channel] = (chip->data >> 3) & 0x07; |
| 356 |
break; |
| 357 |
case 0xb4: |
| 358 |
chip->pms[channel] = chip->data & 0x07; |
| 359 |
chip->ams[channel] = (chip->data >> 4) & 0x03; |
| 360 |
chip->pan_l[channel] = (chip->data >> 7) & 0x01; |
| 361 |
chip->pan_r[channel] = (chip->data >> 6) & 0x01; |
| 362 |
break; |
| 363 |
default: |
| 364 |
break; |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
if (chip->write_a_en || chip->write_d_en) |
| 370 |
{ |
| 371 |
/* Data */ |
| 372 |
if (chip->write_a_en) |
| 373 |
{ |
| 374 |
chip->write_fm_data = 0; |
| 375 |
} |
| 376 |
|
| 377 |
if (chip->write_fm_address && chip->write_d_en) |
| 378 |
{ |
| 379 |
chip->write_fm_data = 1; |
| 380 |
} |
| 381 |
|
| 382 |
/* Address */ |
| 383 |
if (chip->write_a_en) |
| 384 |
{ |
| 385 |
if ((chip->write_data & 0xf0) != 0x00) |
| 386 |
{ |
| 387 |
/* FM Write */ |
| 388 |
chip->address = chip->write_data; |
| 389 |
chip->write_fm_address = 1; |
| 390 |
} |
| 391 |
else |
| 392 |
{ |
| 393 |
/* SSG write */ |
| 394 |
chip->write_fm_address = 0; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
/* FM Mode */ |
| 399 |
/* Data */ |
| 400 |
if (chip->write_d_en && (chip->write_data & 0x100) == 0) |
| 401 |
{ |
| 402 |
switch (chip->write_fm_mode_a) |
| 403 |
{ |
| 404 |
case 0x21: /* LSI test 1 */ |
| 405 |
for (i = 0; i < 8; i++) |
| 406 |
{ |
| 407 |
chip->mode_test_21[i] = (chip->write_data >> i) & 0x01; |
| 408 |
} |
| 409 |
break; |
| 410 |
case 0x22: /* LFO control */ |
| 411 |
if ((chip->write_data >> 3) & 0x01) |
| 412 |
{ |
| 413 |
chip->lfo_en = 0x7f; |
| 414 |
} |
| 415 |
else |
| 416 |
{ |
| 417 |
chip->lfo_en = 0; |
| 418 |
} |
| 419 |
chip->lfo_freq = chip->write_data & 0x07; |
| 420 |
break; |
| 421 |
case 0x24: /* Timer A */ |
| 422 |
chip->timer_a_reg &= 0x03; |
| 423 |
chip->timer_a_reg |= (chip->write_data & 0xff) << 2; |
| 424 |
break; |
| 425 |
case 0x25: |
| 426 |
chip->timer_a_reg &= 0x3fc; |
| 427 |
chip->timer_a_reg |= chip->write_data & 0x03; |
| 428 |
break; |
| 429 |
case 0x26: /* Timer B */ |
| 430 |
chip->timer_b_reg = chip->write_data & 0xff; |
| 431 |
break; |
| 432 |
case 0x27: /* CSM, Timer control */ |
| 433 |
chip->mode_ch3 = (chip->write_data & 0xc0) >> 6; |
| 434 |
chip->mode_csm = chip->mode_ch3 == 2; |
| 435 |
chip->timer_a_load = chip->write_data & 0x01; |
| 436 |
chip->timer_a_enable = (chip->write_data >> 2) & 0x01; |
| 437 |
chip->timer_a_reset = (chip->write_data >> 4) & 0x01; |
| 438 |
chip->timer_b_load = (chip->write_data >> 1) & 0x01; |
| 439 |
chip->timer_b_enable = (chip->write_data >> 3) & 0x01; |
| 440 |
chip->timer_b_reset = (chip->write_data >> 5) & 0x01; |
| 441 |
break; |
| 442 |
case 0x28: /* Key on/off */ |
| 443 |
for (i = 0; i < 4; i++) |
| 444 |
{ |
| 445 |
chip->mode_kon_operator[i] = (chip->write_data >> (4 + i)) & 0x01; |
| 446 |
} |
| 447 |
if ((chip->write_data & 0x03) == 0x03) |
| 448 |
{ |
| 449 |
/* Invalid address */ |
| 450 |
chip->mode_kon_channel = 0xff; |
| 451 |
} |
| 452 |
else |
| 453 |
{ |
| 454 |
chip->mode_kon_channel = (chip->write_data & 0x03) + ((chip->write_data >> 2) & 1) * 3; |
| 455 |
} |
| 456 |
break; |
| 457 |
case 0x2a: /* DAC data */ |
| 458 |
chip->dacdata &= 0x01; |
| 459 |
chip->dacdata |= (chip->write_data ^ 0x80) << 1; |
| 460 |
break; |
| 461 |
case 0x2b: /* DAC enable */ |
| 462 |
chip->dacen = chip->write_data >> 7; |
| 463 |
break; |
| 464 |
case 0x2c: /* LSI test 2 */ |
| 465 |
for (i = 0; i < 8; i++) |
| 466 |
{ |
| 467 |
chip->mode_test_2c[i] = (chip->write_data >> i) & 0x01; |
| 468 |
} |
| 469 |
chip->dacdata &= 0x1fe; |
| 470 |
chip->dacdata |= chip->mode_test_2c[3]; |
| 471 |
chip->eg_custom_timer = !chip->mode_test_2c[7] && chip->mode_test_2c[6]; |
| 472 |
break; |
| 473 |
default: |
| 474 |
break; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
/* Address */ |
| 479 |
if (chip->write_a_en) |
| 480 |
{ |
| 481 |
chip->write_fm_mode_a = chip->write_data & 0x1ff; |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
if (chip->write_fm_data) |
| 486 |
{ |
| 487 |
chip->data = chip->write_data & 0xff; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
static void OPN2_PhaseCalcIncrement(ym3438_t *chip) |
| 492 |
{ |
| 493 |
Bit32u chan = chip->channel; |
| 494 |
Bit32u slot = chip->cycles; |
| 495 |
Bit32u fnum = chip->pg_fnum; |
| 496 |
Bit32u fnum_h = fnum >> 4; |
| 497 |
Bit32u fm; |
| 498 |
Bit32u basefreq; |
| 499 |
Bit8u lfo = chip->lfo_pm; |
| 500 |
Bit8u lfo_l = lfo & 0x0f; |
| 501 |
Bit8u pms = chip->pms[chan]; |
| 502 |
Bit8u dt = chip->dt[slot]; |
| 503 |
Bit8u dt_l = dt & 0x03; |
| 504 |
Bit8u detune = 0; |
| 505 |
Bit8u block, note; |
| 506 |
Bit8u sum, sum_h, sum_l; |
| 507 |
Bit8u kcode = chip->pg_kcode; |
| 508 |
|
| 509 |
fnum <<= 1; |
| 510 |
/* Apply LFO */ |
| 511 |
if (lfo_l & 0x08) |
| 512 |
{ |
| 513 |
lfo_l ^= 0x0f; |
| 514 |
} |
| 515 |
fm = (fnum_h >> pg_lfo_sh1[pms][lfo_l]) + (fnum_h >> pg_lfo_sh2[pms][lfo_l]); |
| 516 |
if (pms > 5) |
| 517 |
{ |
| 518 |
fm <<= pms - 5; |
| 519 |
} |
| 520 |
fm >>= 2; |
| 521 |
if (lfo & 0x10) |
| 522 |
{ |
| 523 |
fnum -= fm; |
| 524 |
} |
| 525 |
else |
| 526 |
{ |
| 527 |
fnum += fm; |
| 528 |
} |
| 529 |
fnum &= 0xfff; |
| 530 |
|
| 531 |
basefreq = (fnum << chip->pg_block) >> 2; |
| 532 |
|
| 533 |
/* Apply detune */ |
| 534 |
if (dt_l) |
| 535 |
{ |
| 536 |
if (kcode > 0x1c) |
| 537 |
{ |
| 538 |
kcode = 0x1c; |
| 539 |
} |
| 540 |
block = kcode >> 2; |
| 541 |
note = kcode & 0x03; |
| 542 |
sum = block + 9 + ((dt_l == 3) | (dt_l & 0x02)); |
| 543 |
sum_h = sum >> 1; |
| 544 |
sum_l = sum & 0x01; |
| 545 |
detune = pg_detune[(sum_l << 2) | note] >> (9 - sum_h); |
| 546 |
} |
| 547 |
if (dt & 0x04) |
| 548 |
{ |
| 549 |
basefreq -= detune; |
| 550 |
} |
| 551 |
else |
| 552 |
{ |
| 553 |
basefreq += detune; |
| 554 |
} |
| 555 |
basefreq &= 0x1ffff; |
| 556 |
chip->pg_inc[slot] = (basefreq * chip->multi[slot]) >> 1; |
| 557 |
chip->pg_inc[slot] &= 0xfffff; |
| 558 |
} |
| 559 |
|
| 560 |
static void OPN2_PhaseGenerate(ym3438_t *chip) |
| 561 |
{ |
| 562 |
Bit32u slot; |
| 563 |
/* Mask increment */ |
| 564 |
slot = (chip->cycles + 20) % 24; |
| 565 |
if (chip->pg_reset[slot]) |
| 566 |
{ |
| 567 |
chip->pg_inc[slot] = 0; |
| 568 |
} |
| 569 |
/* Phase step */ |
| 570 |
slot = (chip->cycles + 19) % 24; |
| 571 |
if (chip->pg_reset[slot] || chip->mode_test_21[3]) |
| 572 |
{ |
| 573 |
chip->pg_phase[slot] = 0; |
| 574 |
} |
| 575 |
chip->pg_phase[slot] += chip->pg_inc[slot]; |
| 576 |
chip->pg_phase[slot] &= 0xfffff; |
| 577 |
} |
| 578 |
|
| 579 |
static void OPN2_EnvelopeSSGEG(ym3438_t *chip) |
| 580 |
{ |
| 581 |
Bit32u slot = chip->cycles; |
| 582 |
Bit8u direction = 0; |
| 583 |
chip->eg_ssg_pgrst_latch[slot] = 0; |
| 584 |
chip->eg_ssg_repeat_latch[slot] = 0; |
| 585 |
chip->eg_ssg_hold_up_latch[slot] = 0; |
| 586 |
chip->eg_ssg_inv[slot] = 0; |
| 587 |
if (chip->ssg_eg[slot] & 0x08) |
| 588 |
{ |
| 589 |
direction = chip->eg_ssg_dir[slot]; |
| 590 |
if (chip->eg_level[slot] & 0x200) |
| 591 |
{ |
| 592 |
/* Reset */ |
| 593 |
if ((chip->ssg_eg[slot] & 0x03) == 0x00) |
| 594 |
{ |
| 595 |
chip->eg_ssg_pgrst_latch[slot] = 1; |
| 596 |
} |
| 597 |
/* Repeat */ |
| 598 |
if ((chip->ssg_eg[slot] & 0x01) == 0x00) |
| 599 |
{ |
| 600 |
chip->eg_ssg_repeat_latch[slot] = 1; |
| 601 |
} |
| 602 |
/* Inverse */ |
| 603 |
if ((chip->ssg_eg[slot] & 0x03) == 0x02) |
| 604 |
{ |
| 605 |
direction ^= 1; |
| 606 |
} |
| 607 |
if ((chip->ssg_eg[slot] & 0x03) == 0x03) |
| 608 |
{ |
| 609 |
direction = 1; |
| 610 |
} |
| 611 |
} |
| 612 |
/* Hold up */ |
| 613 |
if (chip->eg_kon_latch[slot] |
| 614 |
&& ((chip->ssg_eg[slot] & 0x07) == 0x05 || (chip->ssg_eg[slot] & 0x07) == 0x03)) |
| 615 |
{ |
| 616 |
chip->eg_ssg_hold_up_latch[slot] = 1; |
| 617 |
} |
| 618 |
direction &= chip->eg_kon[slot]; |
| 619 |
chip->eg_ssg_inv[slot] = (chip->eg_ssg_dir[slot] ^ ((chip->ssg_eg[slot] >> 2) & 0x01)) |
| 620 |
& chip->eg_kon[slot]; |
| 621 |
} |
| 622 |
chip->eg_ssg_dir[slot] = direction; |
| 623 |
chip->eg_ssg_enable[slot] = (chip->ssg_eg[slot] >> 3) & 0x01; |
| 624 |
} |
| 625 |
|
| 626 |
static void OPN2_EnvelopeADSR(ym3438_t *chip) |
| 627 |
{ |
| 628 |
Bit32u slot = (chip->cycles + 22) % 24; |
| 629 |
|
| 630 |
Bit8u nkon = chip->eg_kon_latch[slot]; |
| 631 |
Bit8u okon = chip->eg_kon[slot]; |
| 632 |
Bit8u kon_event; |
| 633 |
Bit8u koff_event; |
| 634 |
Bit8u eg_off; |
| 635 |
Bit16s level; |
| 636 |
Bit16s nextlevel = 0; |
| 637 |
Bit16s ssg_level; |
| 638 |
Bit8u nextstate = chip->eg_state[slot]; |
| 639 |
Bit16s inc = 0; |
| 640 |
chip->eg_read[0] = chip->eg_read_inc; |
| 641 |
chip->eg_read_inc = chip->eg_inc > 0; |
| 642 |
|
| 643 |
/* Reset phase generator */ |
| 644 |
chip->pg_reset[slot] = (nkon && !okon) || chip->eg_ssg_pgrst_latch[slot]; |
| 645 |
|
| 646 |
/* KeyOn/Off */ |
| 647 |
kon_event = (nkon && !okon) || (okon && chip->eg_ssg_repeat_latch[slot]); |
| 648 |
koff_event = okon && !nkon; |
| 649 |
|
| 650 |
ssg_level = level = (Bit16s)chip->eg_level[slot]; |
| 651 |
|
| 652 |
if (chip->eg_ssg_inv[slot]) |
| 653 |
{ |
| 654 |
/* Inverse */ |
| 655 |
ssg_level = 512 - level; |
| 656 |
ssg_level &= 0x3ff; |
| 657 |
} |
| 658 |
if (koff_event) |
| 659 |
{ |
| 660 |
level = ssg_level; |
| 661 |
} |
| 662 |
if (chip->eg_ssg_enable[slot]) |
| 663 |
{ |
| 664 |
eg_off = level >> 9; |
| 665 |
} |
| 666 |
else |
| 667 |
{ |
| 668 |
eg_off = (level & 0x3f0) == 0x3f0; |
| 669 |
} |
| 670 |
nextlevel = level; |
| 671 |
if (kon_event) |
| 672 |
{ |
| 673 |
nextstate = eg_num_attack; |
| 674 |
/* Instant attack */ |
| 675 |
if (chip->eg_ratemax) |
| 676 |
{ |
| 677 |
nextlevel = 0; |
| 678 |
} |
| 679 |
else if (chip->eg_state[slot] == eg_num_attack && level != 0 && chip->eg_inc && nkon) |
| 680 |
{ |
| 681 |
inc = (~level << chip->eg_inc) >> 5; |
| 682 |
} |
| 683 |
} |
| 684 |
else |
| 685 |
{ |
| 686 |
switch (chip->eg_state[slot]) |
| 687 |
{ |
| 688 |
case eg_num_attack: |
| 689 |
if (level == 0) |
| 690 |
{ |
| 691 |
nextstate = eg_num_decay; |
| 692 |
} |
| 693 |
else if(chip->eg_inc && !chip->eg_ratemax && nkon) |
| 694 |
{ |
| 695 |
inc = (~level << chip->eg_inc) >> 5; |
| 696 |
} |
| 697 |
break; |
| 698 |
case eg_num_decay: |
| 699 |
if ((level >> 4) == (chip->eg_sl[1] << 1)) |
| 700 |
{ |
| 701 |
nextstate = eg_num_sustain; |
| 702 |
} |
| 703 |
else if (!eg_off && chip->eg_inc) |
| 704 |
{ |
| 705 |
inc = 1 << (chip->eg_inc - 1); |
| 706 |
if (chip->eg_ssg_enable[slot]) |
| 707 |
{ |
| 708 |
inc <<= 2; |
| 709 |
} |
| 710 |
} |
| 711 |
break; |
| 712 |
case eg_num_sustain: |
| 713 |
case eg_num_release: |
| 714 |
if (!eg_off && chip->eg_inc) |
| 715 |
{ |
| 716 |
inc = 1 << (chip->eg_inc - 1); |
| 717 |
if (chip->eg_ssg_enable[slot]) |
| 718 |
{ |
| 719 |
inc <<= 2; |
| 720 |
} |
| 721 |
} |
| 722 |
break; |
| 723 |
default: |
| 724 |
break; |
| 725 |
} |
| 726 |
if (!nkon) |
| 727 |
{ |
| 728 |
nextstate = eg_num_release; |
| 729 |
} |
| 730 |
} |
| 731 |
if (chip->eg_kon_csm[slot]) |
| 732 |
{ |
| 733 |
nextlevel |= chip->eg_tl[1] << 3; |
| 734 |
} |
| 735 |
|
| 736 |
/* Envelope off */ |
| 737 |
if (!kon_event && !chip->eg_ssg_hold_up_latch[slot] && chip->eg_state[slot] != eg_num_attack && eg_off) |
| 738 |
{ |
| 739 |
nextstate = eg_num_release; |
| 740 |
nextlevel = 0x3ff; |
| 741 |
} |
| 742 |
|
| 743 |
nextlevel += inc; |
| 744 |
|
| 745 |
chip->eg_kon[slot] = chip->eg_kon_latch[slot]; |
| 746 |
chip->eg_level[slot] = (Bit16u)nextlevel & 0x3ff; |
| 747 |
chip->eg_state[slot] = nextstate; |
| 748 |
} |
| 749 |
|
| 750 |
static void OPN2_EnvelopePrepare(ym3438_t *chip) |
| 751 |
{ |
| 752 |
Bit8u rate; |
| 753 |
Bit8u sum; |
| 754 |
Bit8u inc = 0; |
| 755 |
Bit32u slot = chip->cycles; |
| 756 |
Bit8u rate_sel; |
| 757 |
|
| 758 |
/* Prepare increment */ |
| 759 |
rate = (chip->eg_rate << 1) + chip->eg_ksv; |
| 760 |
|
| 761 |
if (rate > 0x3f) |
| 762 |
{ |
| 763 |
rate = 0x3f; |
| 764 |
} |
| 765 |
|
| 766 |
sum = ((rate >> 2) + chip->eg_shift_lock) & 0x0f; |
| 767 |
if (chip->eg_rate != 0 && chip->eg_quotient == 2) |
| 768 |
{ |
| 769 |
if (rate < 48) |
| 770 |
{ |
| 771 |
switch (sum) |
| 772 |
{ |
| 773 |
case 12: |
| 774 |
inc = 1; |
| 775 |
break; |
| 776 |
case 13: |
| 777 |
inc = (rate >> 1) & 0x01; |
| 778 |
break; |
| 779 |
case 14: |
| 780 |
inc = rate & 0x01; |
| 781 |
break; |
| 782 |
default: |
| 783 |
break; |
| 784 |
} |
| 785 |
} |
| 786 |
else |
| 787 |
{ |
| 788 |
inc = eg_stephi[rate & 0x03][chip->eg_timer_low_lock] + (rate >> 2) - 11; |
| 789 |
if (inc > 4) |
| 790 |
{ |
| 791 |
inc = 4; |
| 792 |
} |
| 793 |
} |
| 794 |
} |
| 795 |
chip->eg_inc = inc; |
| 796 |
chip->eg_ratemax = (rate >> 1) == 0x1f; |
| 797 |
|
| 798 |
/* Prepare rate & ksv */ |
| 799 |
rate_sel = chip->eg_state[slot]; |
| 800 |
if ((chip->eg_kon[slot] && chip->eg_ssg_repeat_latch[slot]) |
| 801 |
|| (!chip->eg_kon[slot] && chip->eg_kon_latch[slot])) |
| 802 |
{ |
| 803 |
rate_sel = eg_num_attack; |
| 804 |
} |
| 805 |
switch (rate_sel) |
| 806 |
{ |
| 807 |
case eg_num_attack: |
| 808 |
chip->eg_rate = chip->ar[slot]; |
| 809 |
break; |
| 810 |
case eg_num_decay: |
| 811 |
chip->eg_rate = chip->dr[slot]; |
| 812 |
break; |
| 813 |
case eg_num_sustain: |
| 814 |
chip->eg_rate = chip->sr[slot]; |
| 815 |
break; |
| 816 |
case eg_num_release: |
| 817 |
chip->eg_rate = (chip->rr[slot] << 1) | 0x01; |
| 818 |
break; |
| 819 |
default: |
| 820 |
break; |
| 821 |
} |
| 822 |
chip->eg_ksv = chip->pg_kcode >> (chip->ks[slot] ^ 0x03); |
| 823 |
if (chip->am[slot]) |
| 824 |
{ |
| 825 |
chip->eg_lfo_am = chip->lfo_am >> eg_am_shift[chip->ams[chip->channel]]; |
| 826 |
} |
| 827 |
else |
| 828 |
{ |
| 829 |
chip->eg_lfo_am = 0; |
| 830 |
} |
| 831 |
/* Delay TL & SL value */ |
| 832 |
chip->eg_tl[1] = chip->eg_tl[0]; |
| 833 |
chip->eg_tl[0] = chip->tl[slot]; |
| 834 |
chip->eg_sl[1] = chip->eg_sl[0]; |
| 835 |
chip->eg_sl[0] = chip->sl[slot]; |
| 836 |
} |
| 837 |
|
| 838 |
static void OPN2_EnvelopeGenerate(ym3438_t *chip) |
| 839 |
{ |
| 840 |
Bit32u slot = (chip->cycles + 23) % 24; |
| 841 |
Bit16u level; |
| 842 |
|
| 843 |
level = chip->eg_level[slot]; |
| 844 |
|
| 845 |
if (chip->eg_ssg_inv[slot]) |
| 846 |
{ |
| 847 |
/* Inverse */ |
| 848 |
level = 512 - level; |
| 849 |
} |
| 850 |
if (chip->mode_test_21[5]) |
| 851 |
{ |
| 852 |
level = 0; |
| 853 |
} |
| 854 |
level &= 0x3ff; |
| 855 |
|
| 856 |
/* Apply AM LFO */ |
| 857 |
level += chip->eg_lfo_am; |
| 858 |
|
| 859 |
/* Apply TL */ |
| 860 |
if (!(chip->mode_csm && chip->channel == 2 + 1)) |
| 861 |
{ |
| 862 |
level += chip->eg_tl[0] << 3; |
| 863 |
} |
| 864 |
if (level > 0x3ff) |
| 865 |
{ |
| 866 |
level = 0x3ff; |
| 867 |
} |
| 868 |
chip->eg_out[slot] = level; |
| 869 |
} |
| 870 |
|
| 871 |
static void OPN2_UpdateLFO(ym3438_t *chip) |
| 872 |
{ |
| 873 |
if ((chip->lfo_quotient & lfo_cycles[chip->lfo_freq]) == lfo_cycles[chip->lfo_freq]) |
| 874 |
{ |
| 875 |
chip->lfo_quotient = 0; |
| 876 |
chip->lfo_cnt++; |
| 877 |
} |
| 878 |
else |
| 879 |
{ |
| 880 |
chip->lfo_quotient += chip->lfo_inc; |
| 881 |
} |
| 882 |
chip->lfo_cnt &= chip->lfo_en; |
| 883 |
} |
| 884 |
|
| 885 |
static void OPN2_FMPrepare(ym3438_t *chip) |
| 886 |
{ |
| 887 |
Bit32u slot = (chip->cycles + 6) % 24; |
| 888 |
Bit32u channel = chip->channel; |
| 889 |
Bit16s mod, mod1, mod2; |
| 890 |
Bit32u op = slot / 6; |
| 891 |
Bit8u connect = chip->connect[channel]; |
| 892 |
Bit32u prevslot = (chip->cycles + 18) % 24; |
| 893 |
|
| 894 |
/* Calculate modulation */ |
| 895 |
mod1 = mod2 = 0; |
| 896 |
|
| 897 |
if (fm_algorithm[op][0][connect]) |
| 898 |
{ |
| 899 |
mod2 |= chip->fm_op1[channel][0]; |
| 900 |
} |
| 901 |
if (fm_algorithm[op][1][connect]) |
| 902 |
{ |
| 903 |
mod1 |= chip->fm_op1[channel][1]; |
| 904 |
} |
| 905 |
if (fm_algorithm[op][2][connect]) |
| 906 |
{ |
| 907 |
mod1 |= chip->fm_op2[channel]; |
| 908 |
} |
| 909 |
if (fm_algorithm[op][3][connect]) |
| 910 |
{ |
| 911 |
mod2 |= chip->fm_out[prevslot]; |
| 912 |
} |
| 913 |
if (fm_algorithm[op][4][connect]) |
| 914 |
{ |
| 915 |
mod1 |= chip->fm_out[prevslot]; |
| 916 |
} |
| 917 |
mod = mod1 + mod2; |
| 918 |
if (op == 0) |
| 919 |
{ |
| 920 |
/* Feedback */ |
| 921 |
mod = mod >> (10 - chip->fb[channel]); |
| 922 |
if (!chip->fb[channel]) |
| 923 |
{ |
| 924 |
mod = 0; |
| 925 |
} |
| 926 |
} |
| 927 |
else |
| 928 |
{ |
| 929 |
mod >>= 1; |
| 930 |
} |
| 931 |
chip->fm_mod[slot] = mod; |
| 932 |
|
| 933 |
slot = (chip->cycles + 18) % 24; |
| 934 |
/* OP1 */ |
| 935 |
if (slot / 6 == 0) |
| 936 |
{ |
| 937 |
chip->fm_op1[channel][1] = chip->fm_op1[channel][0]; |
| 938 |
chip->fm_op1[channel][0] = chip->fm_out[slot]; |
| 939 |
} |
| 940 |
/* OP2 */ |
| 941 |
if (slot / 6 == 2) |
| 942 |
{ |
| 943 |
chip->fm_op2[channel] = chip->fm_out[slot]; |
| 944 |
} |
| 945 |
} |
| 946 |
|
| 947 |
static void OPN2_ChGenerate(ym3438_t *chip) |
| 948 |
{ |
| 949 |
Bit32u slot = (chip->cycles + 18) % 24; |
| 950 |
Bit32u channel = chip->channel; |
| 951 |
Bit32u op = slot / 6; |
| 952 |
Bit32u test_dac = chip->mode_test_2c[5]; |
| 953 |
Bit16s acc = chip->ch_acc[channel]; |
| 954 |
Bit16s add = test_dac; |
| 955 |
Bit16s sum = 0; |
| 956 |
if (op == 0 && !test_dac) |
| 957 |
{ |
| 958 |
acc = 0; |
| 959 |
} |
| 960 |
if (chip->chip_type & ym3438_mode_opn) { |
| 961 |
if (fm_algorithm[op][5][chip->connect[channel]] && !test_dac) |
| 962 |
{ |
| 963 |
add += chip->fm_out[slot]; |
| 964 |
} |
| 965 |
sum = acc + add; |
| 966 |
} else { |
| 967 |
if (fm_algorithm[op][5][chip->connect[channel]] && !test_dac) |
| 968 |
{ |
| 969 |
add += chip->fm_out[slot] >> 5; |
| 970 |
} |
| 971 |
sum = acc + add; |
| 972 |
/* Clamp */ |
| 973 |
if (sum > 255) |
| 974 |
{ |
| 975 |
sum = 255; |
| 976 |
} |
| 977 |
else if(sum < -256) |
| 978 |
{ |
| 979 |
sum = -256; |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
if (op == 0 || test_dac) |
| 984 |
{ |
| 985 |
chip->ch_out[channel] = chip->ch_acc[channel]; |
| 986 |
} |
| 987 |
chip->ch_acc[channel] = sum; |
| 988 |
} |
| 989 |
|
| 990 |
static void OPN2_ChOutput(ym3438_t *chip) |
| 991 |
{ |
| 992 |
Bit32u cycles = chip->cycles; |
| 993 |
Bit32u slot = chip->cycles; |
| 994 |
Bit32u channel = chip->channel; |
| 995 |
Bit32u test_dac = chip->mode_test_2c[5]; |
| 996 |
Bit16s out; |
| 997 |
Bit16s sign; |
| 998 |
Bit32u out_en; |
| 999 |
chip->ch_read = chip->ch_lock; |
| 1000 |
if (slot < 12) |
| 1001 |
{ |
| 1002 |
/* Ch 4,5,6 */ |
| 1003 |
channel++; |
| 1004 |
} |
| 1005 |
if ((cycles & 3) == 0) |
| 1006 |
{ |
| 1007 |
if (!test_dac) |
| 1008 |
{ |
| 1009 |
/* Lock value */ |
| 1010 |
chip->ch_lock = chip->ch_out[channel]; |
| 1011 |
} |
| 1012 |
chip->ch_lock_l = chip->pan_l[channel]; |
| 1013 |
chip->ch_lock_r = chip->pan_r[channel]; |
| 1014 |
} |
| 1015 |
/* Ch 6 */ |
| 1016 |
if (((cycles >> 2) == 1 && chip->dacen) || test_dac) |
| 1017 |
{ |
| 1018 |
out = (Bit16s)chip->dacdata; |
| 1019 |
out = SIGN_EXTEND(8, out); |
| 1020 |
if (chip->chip_type & ym3438_mode_opn) { |
| 1021 |
out <<=5; |
| 1022 |
} |
| 1023 |
} |
| 1024 |
else |
| 1025 |
{ |
| 1026 |
out = chip->ch_lock; |
| 1027 |
} |
| 1028 |
chip->mol = 0; |
| 1029 |
chip->mor = 0; |
| 1030 |
|
| 1031 |
if (chip->chip_type & ym3438_mode_ym2612) |
| 1032 |
{ |
| 1033 |
out_en = ((cycles & 3) == 3) || test_dac; |
| 1034 |
/* YM2612 DAC emulation(not verified) */ |
| 1035 |
sign = out >> 8; |
| 1036 |
if (out >= 0) |
| 1037 |
{ |
| 1038 |
out++; |
| 1039 |
sign++; |
| 1040 |
} |
| 1041 |
if (chip->ch_lock_l && out_en) |
| 1042 |
{ |
| 1043 |
chip->mol = out; |
| 1044 |
} |
| 1045 |
else |
| 1046 |
{ |
| 1047 |
chip->mol = sign; |
| 1048 |
} |
| 1049 |
if (chip->ch_lock_r && out_en) |
| 1050 |
{ |
| 1051 |
chip->mor = out; |
| 1052 |
} |
| 1053 |
else |
| 1054 |
{ |
| 1055 |
chip->mor = sign; |
| 1056 |
} |
| 1057 |
/* Amplify signal */ |
| 1058 |
chip->mol *= 3; |
| 1059 |
chip->mor *= 3; |
| 1060 |
} |
| 1061 |
else |
| 1062 |
{ |
| 1063 |
out_en = ((cycles & 3) != 0) || test_dac; |
| 1064 |
if (chip->ch_lock_l && out_en) |
| 1065 |
{ |
| 1066 |
chip->mol = out; |
| 1067 |
} |
| 1068 |
if (chip->ch_lock_r && out_en) |
| 1069 |
{ |
| 1070 |
chip->mor = out; |
| 1071 |
} |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
static void OPN2_FMGenerate(ym3438_t *chip) |
| 1076 |
{ |
| 1077 |
Bit32u slot = (chip->cycles + 19) % 24; |
| 1078 |
/* Calculate phase */ |
| 1079 |
Bit16u phase = (chip->fm_mod[slot] + (chip->pg_phase[slot] >> 10)) & 0x3ff; |
| 1080 |
Bit16u quarter; |
| 1081 |
Bit16u level; |
| 1082 |
Bit16s output; |
| 1083 |
if (phase & 0x100) |
| 1084 |
{ |
| 1085 |
quarter = (phase ^ 0xff) & 0xff; |
| 1086 |
} |
| 1087 |
else |
| 1088 |
{ |
| 1089 |
quarter = phase & 0xff; |
| 1090 |
} |
| 1091 |
level = chip->msw ? mswrom[quarter] : logsinrom[quarter]; |
| 1092 |
/* Apply envelope */ |
| 1093 |
level += chip->eg_out[slot] << 2; |
| 1094 |
/* Transform */ |
| 1095 |
if (level > 0x1fff) |
| 1096 |
{ |
| 1097 |
level = 0x1fff; |
| 1098 |
} |
| 1099 |
output = ((exprom[(level & 0xff) ^ 0xff] | 0x400) << 2) >> (level >> 8); |
| 1100 |
if (phase & 0x200) |
| 1101 |
{ |
| 1102 |
output = ((~output) ^ (chip->mode_test_21[4] << 13)) + 1; |
| 1103 |
} |
| 1104 |
else |
| 1105 |
{ |
| 1106 |
output = output ^ (chip->mode_test_21[4] << 13); |
| 1107 |
} |
| 1108 |
output = SIGN_EXTEND(13, output); |
| 1109 |
chip->fm_out[slot] = output; |
| 1110 |
} |
| 1111 |
|
| 1112 |
static void OPN2_DoTimerA(ym3438_t *chip) |
| 1113 |
{ |
| 1114 |
Bit16u time; |
| 1115 |
Bit8u load; |
| 1116 |
load = chip->timer_a_overflow; |
| 1117 |
if (chip->cycles == 2) |
| 1118 |
{ |
| 1119 |
/* Lock load value */ |
| 1120 |
load |= (!chip->timer_a_load_lock && chip->timer_a_load); |
| 1121 |
chip->timer_a_load_lock = chip->timer_a_load; |
| 1122 |
if (chip->mode_csm) |
| 1123 |
{ |
| 1124 |
/* CSM KeyOn */ |
| 1125 |
chip->mode_kon_csm = load; |
| 1126 |
} |
| 1127 |
else |
| 1128 |
{ |
| 1129 |
chip->mode_kon_csm = 0; |
| 1130 |
} |
| 1131 |
} |
| 1132 |
/* Load counter */ |
| 1133 |
if (chip->timer_a_load_latch) |
| 1134 |
{ |
| 1135 |
time = chip->timer_a_reg; |
| 1136 |
} |
| 1137 |
else |
| 1138 |
{ |
| 1139 |
time = chip->timer_a_cnt; |
| 1140 |
} |
| 1141 |
chip->timer_a_load_latch = load; |
| 1142 |
/* Increase counter */ |
| 1143 |
if ((chip->cycles == 1 && chip->timer_a_load_lock) || chip->mode_test_21[2]) |
| 1144 |
{ |
| 1145 |
time++; |
| 1146 |
} |
| 1147 |
/* Set overflow flag */ |
| 1148 |
if (chip->timer_a_reset) |
| 1149 |
{ |
| 1150 |
chip->timer_a_reset = 0; |
| 1151 |
chip->timer_a_overflow_flag = 0; |
| 1152 |
} |
| 1153 |
else |
| 1154 |
{ |
| 1155 |
chip->timer_a_overflow_flag |= chip->timer_a_overflow & chip->timer_a_enable; |
| 1156 |
} |
| 1157 |
chip->timer_a_overflow = (time >> 10); |
| 1158 |
chip->timer_a_cnt = time & 0x3ff; |
| 1159 |
} |
| 1160 |
|
| 1161 |
static void OPN2_DoTimerB(ym3438_t *chip) |
| 1162 |
{ |
| 1163 |
Bit16u time; |
| 1164 |
Bit8u load; |
| 1165 |
load = chip->timer_b_overflow; |
| 1166 |
if (chip->cycles == 2) |
| 1167 |
{ |
| 1168 |
/* Lock load value */ |
| 1169 |
load |= (!chip->timer_b_load_lock && chip->timer_b_load); |
| 1170 |
chip->timer_b_load_lock = chip->timer_b_load; |
| 1171 |
} |
| 1172 |
/* Load counter */ |
| 1173 |
if (chip->timer_b_load_latch) |
| 1174 |
{ |
| 1175 |
time = chip->timer_b_reg; |
| 1176 |
} |
| 1177 |
else |
| 1178 |
{ |
| 1179 |
time = chip->timer_b_cnt; |
| 1180 |
} |
| 1181 |
chip->timer_b_load_latch = load; |
| 1182 |
/* Increase counter */ |
| 1183 |
if (chip->cycles == 1) |
| 1184 |
{ |
| 1185 |
chip->timer_b_subcnt++; |
| 1186 |
} |
| 1187 |
if ((chip->timer_b_subcnt == 0x10 && chip->timer_b_load_lock) || chip->mode_test_21[2]) |
| 1188 |
{ |
| 1189 |
time++; |
| 1190 |
} |
| 1191 |
chip->timer_b_subcnt &= 0x0f; |
| 1192 |
/* Set overflow flag */ |
| 1193 |
if (chip->timer_b_reset) |
| 1194 |
{ |
| 1195 |
chip->timer_b_reset = 0; |
| 1196 |
chip->timer_b_overflow_flag = 0; |
| 1197 |
} |
| 1198 |
else |
| 1199 |
{ |
| 1200 |
chip->timer_b_overflow_flag |= chip->timer_b_overflow & chip->timer_b_enable; |
| 1201 |
} |
| 1202 |
chip->timer_b_overflow = (time >> 8); |
| 1203 |
chip->timer_b_cnt = time & 0xff; |
| 1204 |
} |
| 1205 |
|
| 1206 |
static void OPN2_KeyOn(ym3438_t*chip) |
| 1207 |
{ |
| 1208 |
Bit32u slot = chip->cycles; |
| 1209 |
Bit32u chan = chip->channel; |
| 1210 |
/* Key On */ |
| 1211 |
chip->eg_kon_latch[slot] = chip->mode_kon[slot]; |
| 1212 |
chip->eg_kon_csm[slot] = 0; |
| 1213 |
if (chip->channel == 2 && chip->mode_kon_csm) |
| 1214 |
{ |
| 1215 |
/* CSM Key On */ |
| 1216 |
chip->eg_kon_latch[slot] = 1; |
| 1217 |
chip->eg_kon_csm[slot] = 1; |
| 1218 |
} |
| 1219 |
if (chip->cycles == chip->mode_kon_channel) |
| 1220 |
{ |
| 1221 |
/* OP1 */ |
| 1222 |
chip->mode_kon[chan] = chip->mode_kon_operator[0]; |
| 1223 |
/* OP2 */ |
| 1224 |
chip->mode_kon[chan + 12] = chip->mode_kon_operator[1]; |
| 1225 |
/* OP3 */ |
| 1226 |
chip->mode_kon[chan + 6] = chip->mode_kon_operator[2]; |
| 1227 |
/* OP4 */ |
| 1228 |
chip->mode_kon[chan + 18] = chip->mode_kon_operator[3]; |
| 1229 |
} |
| 1230 |
} |
| 1231 |
|
| 1232 |
void OPN2_Reset(ym3438_t *chip) |
| 1233 |
{ |
| 1234 |
Bit32u i; |
| 1235 |
memset(chip, 0, sizeof(ym3438_t)); |
| 1236 |
chip->chip_type = ym3438_mode_readmode; |
| 1237 |
chip->msw = 0; |
| 1238 |
for (i = 0; i < 24; i++) |
| 1239 |
{ |
| 1240 |
chip->eg_out[i] = 0x3ff; |
| 1241 |
chip->eg_level[i] = 0x3ff; |
| 1242 |
chip->eg_state[i] = eg_num_release; |
| 1243 |
chip->multi[i] = 1; |
| 1244 |
} |
| 1245 |
for (i = 0; i < 6; i++) |
| 1246 |
{ |
| 1247 |
chip->pan_l[i] = 1; |
| 1248 |
chip->pan_r[i] = 1; |
| 1249 |
} |
| 1250 |
} |
| 1251 |
|
| 1252 |
void OPN2_SetChipType(ym3438_t *chip, Bit32u type) |
| 1253 |
{ |
| 1254 |
chip->chip_type = type; |
| 1255 |
} |
| 1256 |
|
| 1257 |
void OPN2_SetMSW(ym3438_t *chip, Bit32u msw) |
| 1258 |
{ |
| 1259 |
chip->msw = msw; |
| 1260 |
} |
| 1261 |
|
| 1262 |
void OPN2_Clock(ym3438_t *chip, Bit16s *buffer) |
| 1263 |
{ |
| 1264 |
Bit32u slot = chip->cycles; |
| 1265 |
chip->lfo_inc = chip->mode_test_21[1]; |
| 1266 |
chip->pg_read >>= 1; |
| 1267 |
chip->eg_read[1] >>= 1; |
| 1268 |
chip->eg_cycle++; |
| 1269 |
/* Lock envelope generator timer value */ |
| 1270 |
if (chip->cycles == 1 && chip->eg_quotient == 2) |
| 1271 |
{ |
| 1272 |
if (chip->eg_cycle_stop) |
| 1273 |
{ |
| 1274 |
chip->eg_shift_lock = 0; |
| 1275 |
} |
| 1276 |
else |
| 1277 |
{ |
| 1278 |
chip->eg_shift_lock = chip->eg_shift + 1; |
| 1279 |
} |
| 1280 |
chip->eg_timer_low_lock = chip->eg_timer & 0x03; |
| 1281 |
} |
| 1282 |
/* Cycle specific functions */ |
| 1283 |
switch (chip->cycles) |
| 1284 |
{ |
| 1285 |
case 0: |
| 1286 |
chip->lfo_pm = chip->lfo_cnt >> 2; |
| 1287 |
if (chip->lfo_cnt & 0x40) |
| 1288 |
{ |
| 1289 |
chip->lfo_am = chip->lfo_cnt & 0x3f; |
| 1290 |
} |
| 1291 |
else |
| 1292 |
{ |
| 1293 |
chip->lfo_am = chip->lfo_cnt ^ 0x3f; |
| 1294 |
} |
| 1295 |
chip->lfo_am <<= 1; |
| 1296 |
break; |
| 1297 |
case 1: |
| 1298 |
chip->eg_quotient++; |
| 1299 |
chip->eg_quotient %= 3; |
| 1300 |
chip->eg_cycle = 0; |
| 1301 |
chip->eg_cycle_stop = 1; |
| 1302 |
chip->eg_shift = 0; |
| 1303 |
chip->eg_timer_inc |= chip->eg_quotient >> 1; |
| 1304 |
chip->eg_timer = chip->eg_timer + chip->eg_timer_inc; |
| 1305 |
chip->eg_timer_inc = chip->eg_timer >> 12; |
| 1306 |
chip->eg_timer &= 0xfff; |
| 1307 |
break; |
| 1308 |
case 2: |
| 1309 |
chip->pg_read = chip->pg_phase[21] & 0x3ff; |
| 1310 |
chip->eg_read[1] = chip->eg_out[0]; |
| 1311 |
break; |
| 1312 |
case 13: |
| 1313 |
chip->eg_cycle = 0; |
| 1314 |
chip->eg_cycle_stop = 1; |
| 1315 |
chip->eg_shift = 0; |
| 1316 |
chip->eg_timer = chip->eg_timer + chip->eg_timer_inc; |
| 1317 |
chip->eg_timer_inc = chip->eg_timer >> 12; |
| 1318 |
chip->eg_timer &= 0xfff; |
| 1319 |
break; |
| 1320 |
case 23: |
| 1321 |
chip->lfo_inc |= 1; |
| 1322 |
break; |
| 1323 |
} |
| 1324 |
chip->eg_timer &= ~(chip->mode_test_21[5] << chip->eg_cycle); |
| 1325 |
if (((chip->eg_timer >> chip->eg_cycle) | (chip->pin_test_in & chip->eg_custom_timer)) & chip->eg_cycle_stop) |
| 1326 |
{ |
| 1327 |
chip->eg_shift = chip->eg_cycle; |
| 1328 |
chip->eg_cycle_stop = 0; |
| 1329 |
} |
| 1330 |
|
| 1331 |
OPN2_DoIO(chip); |
| 1332 |
|
| 1333 |
OPN2_DoTimerA(chip); |
| 1334 |
OPN2_DoTimerB(chip); |
| 1335 |
OPN2_KeyOn(chip); |
| 1336 |
|
| 1337 |
OPN2_ChOutput(chip); |
| 1338 |
OPN2_ChGenerate(chip); |
| 1339 |
|
| 1340 |
OPN2_FMPrepare(chip); |
| 1341 |
OPN2_FMGenerate(chip); |
| 1342 |
|
| 1343 |
OPN2_PhaseGenerate(chip); |
| 1344 |
OPN2_PhaseCalcIncrement(chip); |
| 1345 |
|
| 1346 |
OPN2_EnvelopeADSR(chip); |
| 1347 |
OPN2_EnvelopeGenerate(chip); |
| 1348 |
OPN2_EnvelopeSSGEG(chip); |
| 1349 |
OPN2_EnvelopePrepare(chip); |
| 1350 |
|
| 1351 |
/* Prepare fnum & block */ |
| 1352 |
if (chip->mode_ch3) |
| 1353 |
{ |
| 1354 |
/* Channel 3 special mode */ |
| 1355 |
switch (slot) |
| 1356 |
{ |
| 1357 |
case 1: /* OP1 */ |
| 1358 |
chip->pg_fnum = chip->fnum_3ch[1]; |
| 1359 |
chip->pg_block = chip->block_3ch[1]; |
| 1360 |
chip->pg_kcode = chip->kcode_3ch[1]; |
| 1361 |
break; |
| 1362 |
case 7: /* OP3 */ |
| 1363 |
chip->pg_fnum = chip->fnum_3ch[0]; |
| 1364 |
chip->pg_block = chip->block_3ch[0]; |
| 1365 |
chip->pg_kcode = chip->kcode_3ch[0]; |
| 1366 |
break; |
| 1367 |
case 13: /* OP2 */ |
| 1368 |
chip->pg_fnum = chip->fnum_3ch[2]; |
| 1369 |
chip->pg_block = chip->block_3ch[2]; |
| 1370 |
chip->pg_kcode = chip->kcode_3ch[2]; |
| 1371 |
break; |
| 1372 |
case 19: /* OP4 */ |
| 1373 |
default: |
| 1374 |
chip->pg_fnum = chip->fnum[(chip->channel + 1) % 6]; |
| 1375 |
chip->pg_block = chip->block[(chip->channel + 1) % 6]; |
| 1376 |
chip->pg_kcode = chip->kcode[(chip->channel + 1) % 6]; |
| 1377 |
break; |
| 1378 |
} |
| 1379 |
} |
| 1380 |
else |
| 1381 |
{ |
| 1382 |
chip->pg_fnum = chip->fnum[(chip->channel + 1) % 6]; |
| 1383 |
chip->pg_block = chip->block[(chip->channel + 1) % 6]; |
| 1384 |
chip->pg_kcode = chip->kcode[(chip->channel + 1) % 6]; |
| 1385 |
} |
| 1386 |
|
| 1387 |
OPN2_UpdateLFO(chip); |
| 1388 |
OPN2_DoRegWrite(chip); |
| 1389 |
chip->cycles = (chip->cycles + 1) % 24; |
| 1390 |
chip->channel = chip->cycles % 6; |
| 1391 |
|
| 1392 |
buffer[0] = chip->mol; |
| 1393 |
buffer[1] = chip->mor; |
| 1394 |
|
| 1395 |
if (chip->status_time) |
| 1396 |
chip->status_time--; |
| 1397 |
} |
| 1398 |
|
| 1399 |
void OPN2_Write(ym3438_t *chip, Bit32u port, Bit8u data) |
| 1400 |
{ |
| 1401 |
port &= 3; |
| 1402 |
chip->write_data = ((port << 7) & 0x100) | data; |
| 1403 |
if (port & 1) |
| 1404 |
{ |
| 1405 |
/* Data */ |
| 1406 |
chip->write_d |= 1; |
| 1407 |
} |
| 1408 |
else |
| 1409 |
{ |
| 1410 |
/* Address */ |
| 1411 |
chip->write_a |= 1; |
| 1412 |
} |
| 1413 |
} |
| 1414 |
|
| 1415 |
void OPN2_SetTestPin(ym3438_t *chip, Bit32u value) |
| 1416 |
{ |
| 1417 |
chip->pin_test_in = value & 1; |
| 1418 |
} |
| 1419 |
|
| 1420 |
Bit32u OPN2_ReadTestPin(ym3438_t *chip) |
| 1421 |
{ |
| 1422 |
if (!chip->mode_test_2c[7]) |
| 1423 |
{ |
| 1424 |
return 0; |
| 1425 |
} |
| 1426 |
return chip->cycles == 23; |
| 1427 |
} |
| 1428 |
|
| 1429 |
Bit32u OPN2_ReadIRQPin(ym3438_t *chip) |
| 1430 |
{ |
| 1431 |
return chip->timer_a_overflow_flag | chip->timer_b_overflow_flag; |
| 1432 |
} |
| 1433 |
|
| 1434 |
Bit8u OPN2_Read(ym3438_t *chip, Bit32u port) |
| 1435 |
{ |
| 1436 |
if ((port & 3) == 0 || (chip->chip_type & ym3438_mode_readmode)) |
| 1437 |
{ |
| 1438 |
if (chip->mode_test_21[6]) |
| 1439 |
{ |
| 1440 |
/* Read test data */ |
| 1441 |
Bit32u slot = (chip->cycles + 18) % 24; |
| 1442 |
Bit16u testdata = ((chip->pg_read & 0x01) << 15) |
| 1443 |
| ((chip->eg_read[chip->mode_test_21[0]] & 0x01) << 14); |
| 1444 |
if (chip->mode_test_2c[4]) |
| 1445 |
{ |
| 1446 |
testdata |= chip->ch_read & 0x1ff; |
| 1447 |
} |
| 1448 |
else |
| 1449 |
{ |
| 1450 |
testdata |= chip->fm_out[slot] & 0x3fff; |
| 1451 |
} |
| 1452 |
if (chip->mode_test_21[7]) |
| 1453 |
{ |
| 1454 |
chip->status = testdata & 0xff; |
| 1455 |
} |
| 1456 |
else |
| 1457 |
{ |
| 1458 |
chip->status = testdata >> 8; |
| 1459 |
} |
| 1460 |
} |
| 1461 |
else |
| 1462 |
{ |
| 1463 |
chip->status = (chip->busy << 7) | (chip->timer_b_overflow_flag << 1) |
| 1464 |
| chip->timer_a_overflow_flag; |
| 1465 |
} |
| 1466 |
if (chip->chip_type & ym3438_mode_ym2612) |
| 1467 |
{ |
| 1468 |
chip->status_time = 300000; |
| 1469 |
} |
| 1470 |
else |
| 1471 |
{ |
| 1472 |
chip->status_time = 40000000; |
| 1473 |
} |
| 1474 |
} |
| 1475 |
if (chip->status_time) |
| 1476 |
{ |
| 1477 |
return chip->status; |
| 1478 |
} |
| 1479 |
return 0; |
| 1480 |
} |