| 1 |
// |
| 2 |
// Copyright (C) 2013-2018 Alexey Khokholov (Nuke.YKT) |
| 3 |
// |
| 4 |
// This program is free software; you can redistribute it and/or |
| 5 |
// modify it under the terms of the GNU General Public License |
| 6 |
// as published by the Free Software Foundation; either version 2 |
| 7 |
// of the License, or (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// |
| 15 |
// Nuked OPL3 emulator. |
| 16 |
// Thanks: |
| 17 |
// MAME Development Team(Jarek Burczynski, Tatsuyuki Satoh): |
| 18 |
// Feedback and Rhythm part calculation information. |
| 19 |
// forums.submarine.org.uk(carbon14, opl3): |
| 20 |
// Tremolo and phase generator calculation information. |
| 21 |
// OPLx decapsulated(Matthew Gambrell, Olli Niemitalo): |
| 22 |
// OPL2 ROMs. |
| 23 |
// siliconpr0n.org(John McMaster, digshadow): |
| 24 |
// YMF262 and VRC VII decaps and die shots. |
| 25 |
// |
| 26 |
// version: 1.8 |
| 27 |
// |
| 28 |
|
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
#include "opl3.h" |
| 33 |
|
| 34 |
#include "dsda/configuration.h" |
| 35 |
static int mus_opl_gain; |
| 36 |
|
| 37 |
#define RSM_FRAC 10 |
| 38 |
|
| 39 |
// Channel types |
| 40 |
|
| 41 |
enum { |
| 42 |
ch_2op = 0, |
| 43 |
ch_4op = 1, |
| 44 |
ch_4op2 = 2, |
| 45 |
ch_drum = 3 |
| 46 |
}; |
| 47 |
|
| 48 |
// Envelope key types |
| 49 |
|
| 50 |
enum { |
| 51 |
egk_norm = 0x01, |
| 52 |
egk_drum = 0x02 |
| 53 |
}; |
| 54 |
|
| 55 |
|
| 56 |
// |
| 57 |
// logsin table |
| 58 |
// |
| 59 |
|
| 60 |
static const Bit16u logsinrom[256] = { |
| 61 |
0x859, 0x6c3, 0x607, 0x58b, 0x52e, 0x4e4, 0x4a6, 0x471, |
| 62 |
0x443, 0x41a, 0x3f5, 0x3d3, 0x3b5, 0x398, 0x37e, 0x365, |
| 63 |
0x34e, 0x339, 0x324, 0x311, 0x2ff, 0x2ed, 0x2dc, 0x2cd, |
| 64 |
0x2bd, 0x2af, 0x2a0, 0x293, 0x286, 0x279, 0x26d, 0x261, |
| 65 |
0x256, 0x24b, 0x240, 0x236, 0x22c, 0x222, 0x218, 0x20f, |
| 66 |
0x206, 0x1fd, 0x1f5, 0x1ec, 0x1e4, 0x1dc, 0x1d4, 0x1cd, |
| 67 |
0x1c5, 0x1be, 0x1b7, 0x1b0, 0x1a9, 0x1a2, 0x19b, 0x195, |
| 68 |
0x18f, 0x188, 0x182, 0x17c, 0x177, 0x171, 0x16b, 0x166, |
| 69 |
0x160, 0x15b, 0x155, 0x150, 0x14b, 0x146, 0x141, 0x13c, |
| 70 |
0x137, 0x133, 0x12e, 0x129, 0x125, 0x121, 0x11c, 0x118, |
| 71 |
0x114, 0x10f, 0x10b, 0x107, 0x103, 0x0ff, 0x0fb, 0x0f8, |
| 72 |
0x0f4, 0x0f0, 0x0ec, 0x0e9, 0x0e5, 0x0e2, 0x0de, 0x0db, |
| 73 |
0x0d7, 0x0d4, 0x0d1, 0x0cd, 0x0ca, 0x0c7, 0x0c4, 0x0c1, |
| 74 |
0x0be, 0x0bb, 0x0b8, 0x0b5, 0x0b2, 0x0af, 0x0ac, 0x0a9, |
| 75 |
0x0a7, 0x0a4, 0x0a1, 0x09f, 0x09c, 0x099, 0x097, 0x094, |
| 76 |
0x092, 0x08f, 0x08d, 0x08a, 0x088, 0x086, 0x083, 0x081, |
| 77 |
0x07f, 0x07d, 0x07a, 0x078, 0x076, 0x074, 0x072, 0x070, |
| 78 |
0x06e, 0x06c, 0x06a, 0x068, 0x066, 0x064, 0x062, 0x060, |
| 79 |
0x05e, 0x05c, 0x05b, 0x059, 0x057, 0x055, 0x053, 0x052, |
| 80 |
0x050, 0x04e, 0x04d, 0x04b, 0x04a, 0x048, 0x046, 0x045, |
| 81 |
0x043, 0x042, 0x040, 0x03f, 0x03e, 0x03c, 0x03b, 0x039, |
| 82 |
0x038, 0x037, 0x035, 0x034, 0x033, 0x031, 0x030, 0x02f, |
| 83 |
0x02e, 0x02d, 0x02b, 0x02a, 0x029, 0x028, 0x027, 0x026, |
| 84 |
0x025, 0x024, 0x023, 0x022, 0x021, 0x020, 0x01f, 0x01e, |
| 85 |
0x01d, 0x01c, 0x01b, 0x01a, 0x019, 0x018, 0x017, 0x017, |
| 86 |
0x016, 0x015, 0x014, 0x014, 0x013, 0x012, 0x011, 0x011, |
| 87 |
0x010, 0x00f, 0x00f, 0x00e, 0x00d, 0x00d, 0x00c, 0x00c, |
| 88 |
0x00b, 0x00a, 0x00a, 0x009, 0x009, 0x008, 0x008, 0x007, |
| 89 |
0x007, 0x007, 0x006, 0x006, 0x005, 0x005, 0x005, 0x004, |
| 90 |
0x004, 0x004, 0x003, 0x003, 0x003, 0x002, 0x002, 0x002, |
| 91 |
0x002, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, |
| 92 |
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000 |
| 93 |
}; |
| 94 |
|
| 95 |
// |
| 96 |
// exp table |
| 97 |
// |
| 98 |
|
| 99 |
static const Bit16u exprom[256] = { |
| 100 |
0x7fa, 0x7f5, 0x7ef, 0x7ea, 0x7e4, 0x7df, 0x7da, 0x7d4, |
| 101 |
0x7cf, 0x7c9, 0x7c4, 0x7bf, 0x7b9, 0x7b4, 0x7ae, 0x7a9, |
| 102 |
0x7a4, 0x79f, 0x799, 0x794, 0x78f, 0x78a, 0x784, 0x77f, |
| 103 |
0x77a, 0x775, 0x770, 0x76a, 0x765, 0x760, 0x75b, 0x756, |
| 104 |
0x751, 0x74c, 0x747, 0x742, 0x73d, 0x738, 0x733, 0x72e, |
| 105 |
0x729, 0x724, 0x71f, 0x71a, 0x715, 0x710, 0x70b, 0x706, |
| 106 |
0x702, 0x6fd, 0x6f8, 0x6f3, 0x6ee, 0x6e9, 0x6e5, 0x6e0, |
| 107 |
0x6db, 0x6d6, 0x6d2, 0x6cd, 0x6c8, 0x6c4, 0x6bf, 0x6ba, |
| 108 |
0x6b5, 0x6b1, 0x6ac, 0x6a8, 0x6a3, 0x69e, 0x69a, 0x695, |
| 109 |
0x691, 0x68c, 0x688, 0x683, 0x67f, 0x67a, 0x676, 0x671, |
| 110 |
0x66d, 0x668, 0x664, 0x65f, 0x65b, 0x657, 0x652, 0x64e, |
| 111 |
0x649, 0x645, 0x641, 0x63c, 0x638, 0x634, 0x630, 0x62b, |
| 112 |
0x627, 0x623, 0x61e, 0x61a, 0x616, 0x612, 0x60e, 0x609, |
| 113 |
0x605, 0x601, 0x5fd, 0x5f9, 0x5f5, 0x5f0, 0x5ec, 0x5e8, |
| 114 |
0x5e4, 0x5e0, 0x5dc, 0x5d8, 0x5d4, 0x5d0, 0x5cc, 0x5c8, |
| 115 |
0x5c4, 0x5c0, 0x5bc, 0x5b8, 0x5b4, 0x5b0, 0x5ac, 0x5a8, |
| 116 |
0x5a4, 0x5a0, 0x59c, 0x599, 0x595, 0x591, 0x58d, 0x589, |
| 117 |
0x585, 0x581, 0x57e, 0x57a, 0x576, 0x572, 0x56f, 0x56b, |
| 118 |
0x567, 0x563, 0x560, 0x55c, 0x558, 0x554, 0x551, 0x54d, |
| 119 |
0x549, 0x546, 0x542, 0x53e, 0x53b, 0x537, 0x534, 0x530, |
| 120 |
0x52c, 0x529, 0x525, 0x522, 0x51e, 0x51b, 0x517, 0x514, |
| 121 |
0x510, 0x50c, 0x509, 0x506, 0x502, 0x4ff, 0x4fb, 0x4f8, |
| 122 |
0x4f4, 0x4f1, 0x4ed, 0x4ea, 0x4e7, 0x4e3, 0x4e0, 0x4dc, |
| 123 |
0x4d9, 0x4d6, 0x4d2, 0x4cf, 0x4cc, 0x4c8, 0x4c5, 0x4c2, |
| 124 |
0x4be, 0x4bb, 0x4b8, 0x4b5, 0x4b1, 0x4ae, 0x4ab, 0x4a8, |
| 125 |
0x4a4, 0x4a1, 0x49e, 0x49b, 0x498, 0x494, 0x491, 0x48e, |
| 126 |
0x48b, 0x488, 0x485, 0x482, 0x47e, 0x47b, 0x478, 0x475, |
| 127 |
0x472, 0x46f, 0x46c, 0x469, 0x466, 0x463, 0x460, 0x45d, |
| 128 |
0x45a, 0x457, 0x454, 0x451, 0x44e, 0x44b, 0x448, 0x445, |
| 129 |
0x442, 0x43f, 0x43c, 0x439, 0x436, 0x433, 0x430, 0x42d, |
| 130 |
0x42a, 0x428, 0x425, 0x422, 0x41f, 0x41c, 0x419, 0x416, |
| 131 |
0x414, 0x411, 0x40e, 0x40b, 0x408, 0x406, 0x403, 0x400 |
| 132 |
}; |
| 133 |
|
| 134 |
// |
| 135 |
// freq mult table multiplied by 2 |
| 136 |
// |
| 137 |
// 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 12, 12, 15, 15 |
| 138 |
// |
| 139 |
|
| 140 |
static const Bit8u mt[16] = { |
| 141 |
1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 24, 24, 30, 30 |
| 142 |
}; |
| 143 |
|
| 144 |
// |
| 145 |
// ksl table |
| 146 |
// |
| 147 |
|
| 148 |
static const Bit8u kslrom[16] = { |
| 149 |
0, 32, 40, 45, 48, 51, 53, 55, 56, 58, 59, 60, 61, 62, 63, 64 |
| 150 |
}; |
| 151 |
|
| 152 |
static const Bit8u kslshift[4] = { |
| 153 |
8, 1, 2, 0 |
| 154 |
}; |
| 155 |
|
| 156 |
// |
| 157 |
// envelope generator constants |
| 158 |
// |
| 159 |
|
| 160 |
static const Bit8u eg_incstep[4][4] = { |
| 161 |
{ 0, 0, 0, 0 }, |
| 162 |
{ 1, 0, 0, 0 }, |
| 163 |
{ 1, 0, 1, 0 }, |
| 164 |
{ 1, 1, 1, 0 } |
| 165 |
}; |
| 166 |
|
| 167 |
// |
| 168 |
// address decoding |
| 169 |
// |
| 170 |
|
| 171 |
static const Bit8s ad_slot[0x20] = { |
| 172 |
0, 1, 2, 3, 4, 5, -1, -1, 6, 7, 8, 9, 10, 11, -1, -1, |
| 173 |
12, 13, 14, 15, 16, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
| 174 |
}; |
| 175 |
|
| 176 |
static const Bit8u ch_slot[18] = { |
| 177 |
0, 1, 2, 6, 7, 8, 12, 13, 14, 18, 19, 20, 24, 25, 26, 30, 31, 32 |
| 178 |
}; |
| 179 |
|
| 180 |
// |
| 181 |
// Envelope generator |
| 182 |
// |
| 183 |
|
| 184 |
typedef Bit16s(*envelope_sinfunc)(Bit16u phase, Bit16u envelope); |
| 185 |
typedef void(*envelope_genfunc)(opl3_slot *slott); |
| 186 |
|
| 187 |
static Bit16s OPL3_EnvelopeCalcExp(Bit32u level) |
| 188 |
{ |
| 189 |
if (level > 0x1fff) |
| 190 |
{ |
| 191 |
level = 0x1fff; |
| 192 |
} |
| 193 |
return (exprom[level & 0xff] << 1) >> (level >> 8); |
| 194 |
} |
| 195 |
|
| 196 |
static Bit16s OPL3_EnvelopeCalcSin0(Bit16u phase, Bit16u envelope) |
| 197 |
{ |
| 198 |
Bit16u out = 0; |
| 199 |
Bit16u neg = 0; |
| 200 |
phase &= 0x3ff; |
| 201 |
if (phase & 0x200) |
| 202 |
{ |
| 203 |
neg = 0xffff; |
| 204 |
} |
| 205 |
if (phase & 0x100) |
| 206 |
{ |
| 207 |
out = logsinrom[(phase & 0xff) ^ 0xff]; |
| 208 |
} |
| 209 |
else |
| 210 |
{ |
| 211 |
out = logsinrom[phase & 0xff]; |
| 212 |
} |
| 213 |
return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg; |
| 214 |
} |
| 215 |
|
| 216 |
static Bit16s OPL3_EnvelopeCalcSin1(Bit16u phase, Bit16u envelope) |
| 217 |
{ |
| 218 |
Bit16u out = 0; |
| 219 |
phase &= 0x3ff; |
| 220 |
if (phase & 0x200) |
| 221 |
{ |
| 222 |
out = 0x1000; |
| 223 |
} |
| 224 |
else if (phase & 0x100) |
| 225 |
{ |
| 226 |
out = logsinrom[(phase & 0xff) ^ 0xff]; |
| 227 |
} |
| 228 |
else |
| 229 |
{ |
| 230 |
out = logsinrom[phase & 0xff]; |
| 231 |
} |
| 232 |
return OPL3_EnvelopeCalcExp(out + (envelope << 3)); |
| 233 |
} |
| 234 |
|
| 235 |
static Bit16s OPL3_EnvelopeCalcSin2(Bit16u phase, Bit16u envelope) |
| 236 |
{ |
| 237 |
Bit16u out = 0; |
| 238 |
phase &= 0x3ff; |
| 239 |
if (phase & 0x100) |
| 240 |
{ |
| 241 |
out = logsinrom[(phase & 0xff) ^ 0xff]; |
| 242 |
} |
| 243 |
else |
| 244 |
{ |
| 245 |
out = logsinrom[phase & 0xff]; |
| 246 |
} |
| 247 |
return OPL3_EnvelopeCalcExp(out + (envelope << 3)); |
| 248 |
} |
| 249 |
|
| 250 |
static Bit16s OPL3_EnvelopeCalcSin3(Bit16u phase, Bit16u envelope) |
| 251 |
{ |
| 252 |
Bit16u out = 0; |
| 253 |
phase &= 0x3ff; |
| 254 |
if (phase & 0x100) |
| 255 |
{ |
| 256 |
out = 0x1000; |
| 257 |
} |
| 258 |
else |
| 259 |
{ |
| 260 |
out = logsinrom[phase & 0xff]; |
| 261 |
} |
| 262 |
return OPL3_EnvelopeCalcExp(out + (envelope << 3)); |
| 263 |
} |
| 264 |
|
| 265 |
static Bit16s OPL3_EnvelopeCalcSin4(Bit16u phase, Bit16u envelope) |
| 266 |
{ |
| 267 |
Bit16u out = 0; |
| 268 |
Bit16u neg = 0; |
| 269 |
phase &= 0x3ff; |
| 270 |
if ((phase & 0x300) == 0x100) |
| 271 |
{ |
| 272 |
neg = 0xffff; |
| 273 |
} |
| 274 |
if (phase & 0x200) |
| 275 |
{ |
| 276 |
out = 0x1000; |
| 277 |
} |
| 278 |
else if (phase & 0x80) |
| 279 |
{ |
| 280 |
out = logsinrom[((phase ^ 0xff) << 1) & 0xff]; |
| 281 |
} |
| 282 |
else |
| 283 |
{ |
| 284 |
out = logsinrom[(phase << 1) & 0xff]; |
| 285 |
} |
| 286 |
return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg; |
| 287 |
} |
| 288 |
|
| 289 |
static Bit16s OPL3_EnvelopeCalcSin5(Bit16u phase, Bit16u envelope) |
| 290 |
{ |
| 291 |
Bit16u out = 0; |
| 292 |
phase &= 0x3ff; |
| 293 |
if (phase & 0x200) |
| 294 |
{ |
| 295 |
out = 0x1000; |
| 296 |
} |
| 297 |
else if (phase & 0x80) |
| 298 |
{ |
| 299 |
out = logsinrom[((phase ^ 0xff) << 1) & 0xff]; |
| 300 |
} |
| 301 |
else |
| 302 |
{ |
| 303 |
out = logsinrom[(phase << 1) & 0xff]; |
| 304 |
} |
| 305 |
return OPL3_EnvelopeCalcExp(out + (envelope << 3)); |
| 306 |
} |
| 307 |
|
| 308 |
static Bit16s OPL3_EnvelopeCalcSin6(Bit16u phase, Bit16u envelope) |
| 309 |
{ |
| 310 |
Bit16u neg = 0; |
| 311 |
phase &= 0x3ff; |
| 312 |
if (phase & 0x200) |
| 313 |
{ |
| 314 |
neg = 0xffff; |
| 315 |
} |
| 316 |
return OPL3_EnvelopeCalcExp(envelope << 3) ^ neg; |
| 317 |
} |
| 318 |
|
| 319 |
static Bit16s OPL3_EnvelopeCalcSin7(Bit16u phase, Bit16u envelope) |
| 320 |
{ |
| 321 |
Bit16u out = 0; |
| 322 |
Bit16u neg = 0; |
| 323 |
phase &= 0x3ff; |
| 324 |
if (phase & 0x200) |
| 325 |
{ |
| 326 |
neg = 0xffff; |
| 327 |
phase = (phase & 0x1ff) ^ 0x1ff; |
| 328 |
} |
| 329 |
out = phase << 3; |
| 330 |
return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg; |
| 331 |
} |
| 332 |
|
| 333 |
static const envelope_sinfunc envelope_sin[8] = { |
| 334 |
OPL3_EnvelopeCalcSin0, |
| 335 |
OPL3_EnvelopeCalcSin1, |
| 336 |
OPL3_EnvelopeCalcSin2, |
| 337 |
OPL3_EnvelopeCalcSin3, |
| 338 |
OPL3_EnvelopeCalcSin4, |
| 339 |
OPL3_EnvelopeCalcSin5, |
| 340 |
OPL3_EnvelopeCalcSin6, |
| 341 |
OPL3_EnvelopeCalcSin7 |
| 342 |
}; |
| 343 |
|
| 344 |
enum envelope_gen_num |
| 345 |
{ |
| 346 |
envelope_gen_num_attack = 0, |
| 347 |
envelope_gen_num_decay = 1, |
| 348 |
envelope_gen_num_sustain = 2, |
| 349 |
envelope_gen_num_release = 3 |
| 350 |
}; |
| 351 |
|
| 352 |
static void OPL3_EnvelopeUpdateKSL(opl3_slot *slot) |
| 353 |
{ |
| 354 |
Bit16s ksl = (kslrom[slot->channel->f_num >> 6] << 2) |
| 355 |
- ((0x08 - slot->channel->block) << 5); |
| 356 |
if (ksl < 0) |
| 357 |
{ |
| 358 |
ksl = 0; |
| 359 |
} |
| 360 |
slot->eg_ksl = (Bit8u)ksl; |
| 361 |
} |
| 362 |
|
| 363 |
static void OPL3_EnvelopeCalc(opl3_slot *slot) |
| 364 |
{ |
| 365 |
Bit8u nonzero; |
| 366 |
Bit8u rate; |
| 367 |
Bit8u rate_hi; |
| 368 |
Bit8u rate_lo; |
| 369 |
Bit8u reg_rate = 0; |
| 370 |
Bit8u ks; |
| 371 |
Bit8u eg_shift, shift; |
| 372 |
Bit16u eg_rout; |
| 373 |
Bit16s eg_inc; |
| 374 |
Bit8u eg_off; |
| 375 |
Bit8u reset = 0; |
| 376 |
slot->eg_out = slot->eg_rout + (slot->reg_tl << 2) |
| 377 |
+ (slot->eg_ksl >> kslshift[slot->reg_ksl]) + *slot->trem; |
| 378 |
if (slot->key && slot->eg_gen == envelope_gen_num_release) |
| 379 |
{ |
| 380 |
reset = 1; |
| 381 |
reg_rate = slot->reg_ar; |
| 382 |
} |
| 383 |
else |
| 384 |
{ |
| 385 |
switch (slot->eg_gen) |
| 386 |
{ |
| 387 |
case envelope_gen_num_attack: |
| 388 |
reg_rate = slot->reg_ar; |
| 389 |
break; |
| 390 |
case envelope_gen_num_decay: |
| 391 |
reg_rate = slot->reg_dr; |
| 392 |
break; |
| 393 |
case envelope_gen_num_sustain: |
| 394 |
if (!slot->reg_type) |
| 395 |
{ |
| 396 |
reg_rate = slot->reg_rr; |
| 397 |
} |
| 398 |
break; |
| 399 |
case envelope_gen_num_release: |
| 400 |
reg_rate = slot->reg_rr; |
| 401 |
break; |
| 402 |
} |
| 403 |
} |
| 404 |
slot->pg_reset = reset; |
| 405 |
ks = slot->channel->ksv >> ((slot->reg_ksr ^ 1) << 1); |
| 406 |
nonzero = (reg_rate != 0); |
| 407 |
rate = ks + (reg_rate << 2); |
| 408 |
rate_hi = rate >> 2; |
| 409 |
rate_lo = rate & 0x03; |
| 410 |
if (rate_hi & 0x10) |
| 411 |
{ |
| 412 |
rate_hi = 0x0f; |
| 413 |
} |
| 414 |
eg_shift = rate_hi + slot->chip->eg_add; |
| 415 |
shift = 0; |
| 416 |
if (nonzero) |
| 417 |
{ |
| 418 |
if (rate_hi < 12) |
| 419 |
{ |
| 420 |
if (slot->chip->eg_state) |
| 421 |
{ |
| 422 |
switch (eg_shift) |
| 423 |
{ |
| 424 |
case 12: |
| 425 |
shift = 1; |
| 426 |
break; |
| 427 |
case 13: |
| 428 |
shift = (rate_lo >> 1) & 0x01; |
| 429 |
break; |
| 430 |
case 14: |
| 431 |
shift = rate_lo & 0x01; |
| 432 |
break; |
| 433 |
default: |
| 434 |
break; |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
else |
| 439 |
{ |
| 440 |
shift = (rate_hi & 0x03) + eg_incstep[rate_lo][slot->chip->timer & 0x03]; |
| 441 |
if (shift & 0x04) |
| 442 |
{ |
| 443 |
shift = 0x03; |
| 444 |
} |
| 445 |
if (!shift) |
| 446 |
{ |
| 447 |
shift = slot->chip->eg_state; |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
eg_rout = slot->eg_rout; |
| 452 |
eg_inc = 0; |
| 453 |
eg_off = 0; |
| 454 |
// Instant attack |
| 455 |
if (reset && rate_hi == 0x0f) |
| 456 |
{ |
| 457 |
eg_rout = 0x00; |
| 458 |
} |
| 459 |
// Envelope off |
| 460 |
if ((slot->eg_rout & 0x1f8) == 0x1f8) |
| 461 |
{ |
| 462 |
eg_off = 1; |
| 463 |
} |
| 464 |
if (slot->eg_gen != envelope_gen_num_attack && !reset && eg_off) |
| 465 |
{ |
| 466 |
eg_rout = 0x1ff; |
| 467 |
} |
| 468 |
switch (slot->eg_gen) |
| 469 |
{ |
| 470 |
case envelope_gen_num_attack: |
| 471 |
if (!slot->eg_rout) |
| 472 |
{ |
| 473 |
slot->eg_gen = envelope_gen_num_decay; |
| 474 |
} |
| 475 |
else if (slot->key && shift > 0 && rate_hi != 0x0f) |
| 476 |
{ |
| 477 |
eg_inc = ((~slot->eg_rout) << shift) >> 4; |
| 478 |
} |
| 479 |
break; |
| 480 |
case envelope_gen_num_decay: |
| 481 |
if ((slot->eg_rout >> 4) == slot->reg_sl) |
| 482 |
{ |
| 483 |
slot->eg_gen = envelope_gen_num_sustain; |
| 484 |
} |
| 485 |
else if (!eg_off && !reset && shift > 0) |
| 486 |
{ |
| 487 |
eg_inc = 1 << (shift - 1); |
| 488 |
} |
| 489 |
break; |
| 490 |
case envelope_gen_num_sustain: |
| 491 |
case envelope_gen_num_release: |
| 492 |
if (!eg_off && !reset && shift > 0) |
| 493 |
{ |
| 494 |
eg_inc = 1 << (shift - 1); |
| 495 |
} |
| 496 |
break; |
| 497 |
} |
| 498 |
slot->eg_rout = (eg_rout + eg_inc) & 0x1ff; |
| 499 |
// Key off |
| 500 |
if (reset) |
| 501 |
{ |
| 502 |
slot->eg_gen = envelope_gen_num_attack; |
| 503 |
} |
| 504 |
if (!slot->key) |
| 505 |
{ |
| 506 |
slot->eg_gen = envelope_gen_num_release; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
static void OPL3_EnvelopeKeyOn(opl3_slot *slot, Bit8u type) |
| 511 |
{ |
| 512 |
slot->key |= type; |
| 513 |
} |
| 514 |
|
| 515 |
static void OPL3_EnvelopeKeyOff(opl3_slot *slot, Bit8u type) |
| 516 |
{ |
| 517 |
slot->key &= ~type; |
| 518 |
} |
| 519 |
|
| 520 |
// |
| 521 |
// Phase Generator |
| 522 |
// |
| 523 |
|
| 524 |
static void OPL3_PhaseGenerate(opl3_slot *slot) |
| 525 |
{ |
| 526 |
opl3_chip *chip; |
| 527 |
Bit16u f_num; |
| 528 |
Bit32u basefreq; |
| 529 |
Bit8u rm_xor, n_bit; |
| 530 |
Bit32u noise; |
| 531 |
Bit16u phase; |
| 532 |
|
| 533 |
chip = slot->chip; |
| 534 |
f_num = slot->channel->f_num; |
| 535 |
if (slot->reg_vib) |
| 536 |
{ |
| 537 |
Bit8s range; |
| 538 |
Bit8u vibpos; |
| 539 |
|
| 540 |
range = (f_num >> 7) & 7; |
| 541 |
vibpos = slot->chip->vibpos; |
| 542 |
|
| 543 |
if (!(vibpos & 3)) |
| 544 |
{ |
| 545 |
range = 0; |
| 546 |
} |
| 547 |
else if (vibpos & 1) |
| 548 |
{ |
| 549 |
range >>= 1; |
| 550 |
} |
| 551 |
range >>= slot->chip->vibshift; |
| 552 |
|
| 553 |
if (vibpos & 4) |
| 554 |
{ |
| 555 |
range = -range; |
| 556 |
} |
| 557 |
f_num += range; |
| 558 |
} |
| 559 |
basefreq = (f_num << slot->channel->block) >> 1; |
| 560 |
phase = (Bit16u)(slot->pg_phase >> 9); |
| 561 |
if (slot->pg_reset) |
| 562 |
{ |
| 563 |
slot->pg_phase = 0; |
| 564 |
} |
| 565 |
slot->pg_phase += (basefreq * mt[slot->reg_mult]) >> 1; |
| 566 |
// Rhythm mode |
| 567 |
noise = chip->noise; |
| 568 |
slot->pg_phase_out = phase; |
| 569 |
if (slot->slot_num == 13) // hh |
| 570 |
{ |
| 571 |
chip->rm_hh_bit2 = (phase >> 2) & 1; |
| 572 |
chip->rm_hh_bit3 = (phase >> 3) & 1; |
| 573 |
chip->rm_hh_bit7 = (phase >> 7) & 1; |
| 574 |
chip->rm_hh_bit8 = (phase >> 8) & 1; |
| 575 |
} |
| 576 |
if (slot->slot_num == 17 && (chip->rhy & 0x20)) // tc |
| 577 |
{ |
| 578 |
chip->rm_tc_bit3 = (phase >> 3) & 1; |
| 579 |
chip->rm_tc_bit5 = (phase >> 5) & 1; |
| 580 |
} |
| 581 |
if (chip->rhy & 0x20) |
| 582 |
{ |
| 583 |
rm_xor = (chip->rm_hh_bit2 ^ chip->rm_hh_bit7) |
| 584 |
| (chip->rm_hh_bit3 ^ chip->rm_tc_bit5) |
| 585 |
| (chip->rm_tc_bit3 ^ chip->rm_tc_bit5); |
| 586 |
switch (slot->slot_num) |
| 587 |
{ |
| 588 |
case 13: // hh |
| 589 |
slot->pg_phase_out = rm_xor << 9; |
| 590 |
if (rm_xor ^ (noise & 1)) |
| 591 |
{ |
| 592 |
slot->pg_phase_out |= 0xd0; |
| 593 |
} |
| 594 |
else |
| 595 |
{ |
| 596 |
slot->pg_phase_out |= 0x34; |
| 597 |
} |
| 598 |
break; |
| 599 |
case 16: // sd |
| 600 |
slot->pg_phase_out = (chip->rm_hh_bit8 << 9) |
| 601 |
| ((chip->rm_hh_bit8 ^ (noise & 1)) << 8); |
| 602 |
break; |
| 603 |
case 17: // tc |
| 604 |
slot->pg_phase_out = (rm_xor << 9) | 0x80; |
| 605 |
break; |
| 606 |
default: |
| 607 |
break; |
| 608 |
} |
| 609 |
} |
| 610 |
n_bit = ((noise >> 14) ^ noise) & 0x01; |
| 611 |
chip->noise = (noise >> 1) | (n_bit << 22); |
| 612 |
} |
| 613 |
|
| 614 |
// |
| 615 |
// Slot |
| 616 |
// |
| 617 |
|
| 618 |
static void OPL3_SlotWrite20(opl3_slot *slot, Bit8u data) |
| 619 |
{ |
| 620 |
if ((data >> 7) & 0x01) |
| 621 |
{ |
| 622 |
slot->trem = &slot->chip->tremolo; |
| 623 |
} |
| 624 |
else |
| 625 |
{ |
| 626 |
slot->trem = (Bit8u*)&slot->chip->zeromod; |
| 627 |
} |
| 628 |
slot->reg_vib = (data >> 6) & 0x01; |
| 629 |
slot->reg_type = (data >> 5) & 0x01; |
| 630 |
slot->reg_ksr = (data >> 4) & 0x01; |
| 631 |
slot->reg_mult = data & 0x0f; |
| 632 |
} |
| 633 |
|
| 634 |
static void OPL3_SlotWrite40(opl3_slot *slot, Bit8u data) |
| 635 |
{ |
| 636 |
slot->reg_ksl = (data >> 6) & 0x03; |
| 637 |
slot->reg_tl = data & 0x3f; |
| 638 |
OPL3_EnvelopeUpdateKSL(slot); |
| 639 |
} |
| 640 |
|
| 641 |
static void OPL3_SlotWrite60(opl3_slot *slot, Bit8u data) |
| 642 |
{ |
| 643 |
slot->reg_ar = (data >> 4) & 0x0f; |
| 644 |
slot->reg_dr = data & 0x0f; |
| 645 |
} |
| 646 |
|
| 647 |
static void OPL3_SlotWrite80(opl3_slot *slot, Bit8u data) |
| 648 |
{ |
| 649 |
slot->reg_sl = (data >> 4) & 0x0f; |
| 650 |
if (slot->reg_sl == 0x0f) |
| 651 |
{ |
| 652 |
slot->reg_sl = 0x1f; |
| 653 |
} |
| 654 |
slot->reg_rr = data & 0x0f; |
| 655 |
} |
| 656 |
|
| 657 |
static void OPL3_SlotWriteE0(opl3_slot *slot, Bit8u data) |
| 658 |
{ |
| 659 |
slot->reg_wf = data & 0x07; |
| 660 |
if (slot->chip->newm == 0x00) |
| 661 |
{ |
| 662 |
slot->reg_wf &= 0x03; |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
static void OPL3_SlotGenerate(opl3_slot *slot) |
| 667 |
{ |
| 668 |
slot->out = envelope_sin[slot->reg_wf](slot->pg_phase_out + *slot->mod, slot->eg_out); |
| 669 |
} |
| 670 |
|
| 671 |
static void OPL3_SlotCalcFB(opl3_slot *slot) |
| 672 |
{ |
| 673 |
if (slot->channel->fb != 0x00) |
| 674 |
{ |
| 675 |
slot->fbmod = (slot->prout + slot->out) >> (0x09 - slot->channel->fb); |
| 676 |
} |
| 677 |
else |
| 678 |
{ |
| 679 |
slot->fbmod = 0; |
| 680 |
} |
| 681 |
slot->prout = slot->out; |
| 682 |
} |
| 683 |
|
| 684 |
// |
| 685 |
// Channel |
| 686 |
// |
| 687 |
|
| 688 |
static void OPL3_ChannelSetupAlg(opl3_channel *channel); |
| 689 |
|
| 690 |
static void OPL3_ChannelUpdateRhythm(opl3_chip *chip, Bit8u data) |
| 691 |
{ |
| 692 |
opl3_channel *channel6; |
| 693 |
opl3_channel *channel7; |
| 694 |
opl3_channel *channel8; |
| 695 |
Bit8u chnum; |
| 696 |
|
| 697 |
chip->rhy = data & 0x3f; |
| 698 |
if (chip->rhy & 0x20) |
| 699 |
{ |
| 700 |
channel6 = &chip->channel[6]; |
| 701 |
channel7 = &chip->channel[7]; |
| 702 |
channel8 = &chip->channel[8]; |
| 703 |
channel6->out[0] = &channel6->slots[1]->out; |
| 704 |
channel6->out[1] = &channel6->slots[1]->out; |
| 705 |
channel6->out[2] = &chip->zeromod; |
| 706 |
channel6->out[3] = &chip->zeromod; |
| 707 |
channel7->out[0] = &channel7->slots[0]->out; |
| 708 |
channel7->out[1] = &channel7->slots[0]->out; |
| 709 |
channel7->out[2] = &channel7->slots[1]->out; |
| 710 |
channel7->out[3] = &channel7->slots[1]->out; |
| 711 |
channel8->out[0] = &channel8->slots[0]->out; |
| 712 |
channel8->out[1] = &channel8->slots[0]->out; |
| 713 |
channel8->out[2] = &channel8->slots[1]->out; |
| 714 |
channel8->out[3] = &channel8->slots[1]->out; |
| 715 |
for (chnum = 6; chnum < 9; chnum++) |
| 716 |
{ |
| 717 |
chip->channel[chnum].chtype = ch_drum; |
| 718 |
} |
| 719 |
OPL3_ChannelSetupAlg(channel6); |
| 720 |
OPL3_ChannelSetupAlg(channel7); |
| 721 |
OPL3_ChannelSetupAlg(channel8); |
| 722 |
//hh |
| 723 |
if (chip->rhy & 0x01) |
| 724 |
{ |
| 725 |
OPL3_EnvelopeKeyOn(channel7->slots[0], egk_drum); |
| 726 |
} |
| 727 |
else |
| 728 |
{ |
| 729 |
OPL3_EnvelopeKeyOff(channel7->slots[0], egk_drum); |
| 730 |
} |
| 731 |
//tc |
| 732 |
if (chip->rhy & 0x02) |
| 733 |
{ |
| 734 |
OPL3_EnvelopeKeyOn(channel8->slots[1], egk_drum); |
| 735 |
} |
| 736 |
else |
| 737 |
{ |
| 738 |
OPL3_EnvelopeKeyOff(channel8->slots[1], egk_drum); |
| 739 |
} |
| 740 |
//tom |
| 741 |
if (chip->rhy & 0x04) |
| 742 |
{ |
| 743 |
OPL3_EnvelopeKeyOn(channel8->slots[0], egk_drum); |
| 744 |
} |
| 745 |
else |
| 746 |
{ |
| 747 |
OPL3_EnvelopeKeyOff(channel8->slots[0], egk_drum); |
| 748 |
} |
| 749 |
//sd |
| 750 |
if (chip->rhy & 0x08) |
| 751 |
{ |
| 752 |
OPL3_EnvelopeKeyOn(channel7->slots[1], egk_drum); |
| 753 |
} |
| 754 |
else |
| 755 |
{ |
| 756 |
OPL3_EnvelopeKeyOff(channel7->slots[1], egk_drum); |
| 757 |
} |
| 758 |
//bd |
| 759 |
if (chip->rhy & 0x10) |
| 760 |
{ |
| 761 |
OPL3_EnvelopeKeyOn(channel6->slots[0], egk_drum); |
| 762 |
OPL3_EnvelopeKeyOn(channel6->slots[1], egk_drum); |
| 763 |
} |
| 764 |
else |
| 765 |
{ |
| 766 |
OPL3_EnvelopeKeyOff(channel6->slots[0], egk_drum); |
| 767 |
OPL3_EnvelopeKeyOff(channel6->slots[1], egk_drum); |
| 768 |
} |
| 769 |
} |
| 770 |
else |
| 771 |
{ |
| 772 |
for (chnum = 6; chnum < 9; chnum++) |
| 773 |
{ |
| 774 |
chip->channel[chnum].chtype = ch_2op; |
| 775 |
OPL3_ChannelSetupAlg(&chip->channel[chnum]); |
| 776 |
OPL3_EnvelopeKeyOff(chip->channel[chnum].slots[0], egk_drum); |
| 777 |
OPL3_EnvelopeKeyOff(chip->channel[chnum].slots[1], egk_drum); |
| 778 |
} |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
static void OPL3_ChannelWriteA0(opl3_channel *channel, Bit8u data) |
| 783 |
{ |
| 784 |
if (channel->chip->newm && channel->chtype == ch_4op2) |
| 785 |
{ |
| 786 |
return; |
| 787 |
} |
| 788 |
channel->f_num = (channel->f_num & 0x300) | data; |
| 789 |
channel->ksv = (channel->block << 1) |
| 790 |
| ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01); |
| 791 |
OPL3_EnvelopeUpdateKSL(channel->slots[0]); |
| 792 |
OPL3_EnvelopeUpdateKSL(channel->slots[1]); |
| 793 |
if (channel->chip->newm && channel->chtype == ch_4op) |
| 794 |
{ |
| 795 |
channel->pair->f_num = channel->f_num; |
| 796 |
channel->pair->ksv = channel->ksv; |
| 797 |
OPL3_EnvelopeUpdateKSL(channel->pair->slots[0]); |
| 798 |
OPL3_EnvelopeUpdateKSL(channel->pair->slots[1]); |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
static void OPL3_ChannelWriteB0(opl3_channel *channel, Bit8u data) |
| 803 |
{ |
| 804 |
if (channel->chip->newm && channel->chtype == ch_4op2) |
| 805 |
{ |
| 806 |
return; |
| 807 |
} |
| 808 |
channel->f_num = (channel->f_num & 0xff) | ((data & 0x03) << 8); |
| 809 |
channel->block = (data >> 2) & 0x07; |
| 810 |
channel->ksv = (channel->block << 1) |
| 811 |
| ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01); |
| 812 |
OPL3_EnvelopeUpdateKSL(channel->slots[0]); |
| 813 |
OPL3_EnvelopeUpdateKSL(channel->slots[1]); |
| 814 |
if (channel->chip->newm && channel->chtype == ch_4op) |
| 815 |
{ |
| 816 |
channel->pair->f_num = channel->f_num; |
| 817 |
channel->pair->block = channel->block; |
| 818 |
channel->pair->ksv = channel->ksv; |
| 819 |
OPL3_EnvelopeUpdateKSL(channel->pair->slots[0]); |
| 820 |
OPL3_EnvelopeUpdateKSL(channel->pair->slots[1]); |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
static void OPL3_ChannelSetupAlg(opl3_channel *channel) |
| 825 |
{ |
| 826 |
if (channel->chtype == ch_drum) |
| 827 |
{ |
| 828 |
if (channel->ch_num == 7 || channel->ch_num == 8) |
| 829 |
{ |
| 830 |
channel->slots[0]->mod = &channel->chip->zeromod; |
| 831 |
channel->slots[1]->mod = &channel->chip->zeromod; |
| 832 |
return; |
| 833 |
} |
| 834 |
switch (channel->alg & 0x01) |
| 835 |
{ |
| 836 |
case 0x00: |
| 837 |
channel->slots[0]->mod = &channel->slots[0]->fbmod; |
| 838 |
channel->slots[1]->mod = &channel->slots[0]->out; |
| 839 |
break; |
| 840 |
case 0x01: |
| 841 |
channel->slots[0]->mod = &channel->slots[0]->fbmod; |
| 842 |
channel->slots[1]->mod = &channel->chip->zeromod; |
| 843 |
break; |
| 844 |
} |
| 845 |
return; |
| 846 |
} |
| 847 |
if (channel->alg & 0x08) |
| 848 |
{ |
| 849 |
return; |
| 850 |
} |
| 851 |
if (channel->alg & 0x04) |
| 852 |
{ |
| 853 |
channel->pair->out[0] = &channel->chip->zeromod; |
| 854 |
channel->pair->out[1] = &channel->chip->zeromod; |
| 855 |
channel->pair->out[2] = &channel->chip->zeromod; |
| 856 |
channel->pair->out[3] = &channel->chip->zeromod; |
| 857 |
switch (channel->alg & 0x03) |
| 858 |
{ |
| 859 |
case 0x00: |
| 860 |
channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod; |
| 861 |
channel->pair->slots[1]->mod = &channel->pair->slots[0]->out; |
| 862 |
channel->slots[0]->mod = &channel->pair->slots[1]->out; |
| 863 |
channel->slots[1]->mod = &channel->slots[0]->out; |
| 864 |
channel->out[0] = &channel->slots[1]->out; |
| 865 |
channel->out[1] = &channel->chip->zeromod; |
| 866 |
channel->out[2] = &channel->chip->zeromod; |
| 867 |
channel->out[3] = &channel->chip->zeromod; |
| 868 |
break; |
| 869 |
case 0x01: |
| 870 |
channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod; |
| 871 |
channel->pair->slots[1]->mod = &channel->pair->slots[0]->out; |
| 872 |
channel->slots[0]->mod = &channel->chip->zeromod; |
| 873 |
channel->slots[1]->mod = &channel->slots[0]->out; |
| 874 |
channel->out[0] = &channel->pair->slots[1]->out; |
| 875 |
channel->out[1] = &channel->slots[1]->out; |
| 876 |
channel->out[2] = &channel->chip->zeromod; |
| 877 |
channel->out[3] = &channel->chip->zeromod; |
| 878 |
break; |
| 879 |
case 0x02: |
| 880 |
channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod; |
| 881 |
channel->pair->slots[1]->mod = &channel->chip->zeromod; |
| 882 |
channel->slots[0]->mod = &channel->pair->slots[1]->out; |
| 883 |
channel->slots[1]->mod = &channel->slots[0]->out; |
| 884 |
channel->out[0] = &channel->pair->slots[0]->out; |
| 885 |
channel->out[1] = &channel->slots[1]->out; |
| 886 |
channel->out[2] = &channel->chip->zeromod; |
| 887 |
channel->out[3] = &channel->chip->zeromod; |
| 888 |
break; |
| 889 |
case 0x03: |
| 890 |
channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod; |
| 891 |
channel->pair->slots[1]->mod = &channel->chip->zeromod; |
| 892 |
channel->slots[0]->mod = &channel->pair->slots[1]->out; |
| 893 |
channel->slots[1]->mod = &channel->chip->zeromod; |
| 894 |
channel->out[0] = &channel->pair->slots[0]->out; |
| 895 |
channel->out[1] = &channel->slots[0]->out; |
| 896 |
channel->out[2] = &channel->slots[1]->out; |
| 897 |
channel->out[3] = &channel->chip->zeromod; |
| 898 |
break; |
| 899 |
} |
| 900 |
} |
| 901 |
else |
| 902 |
{ |
| 903 |
switch (channel->alg & 0x01) |
| 904 |
{ |
| 905 |
case 0x00: |
| 906 |
channel->slots[0]->mod = &channel->slots[0]->fbmod; |
| 907 |
channel->slots[1]->mod = &channel->slots[0]->out; |
| 908 |
channel->out[0] = &channel->slots[1]->out; |
| 909 |
channel->out[1] = &channel->chip->zeromod; |
| 910 |
channel->out[2] = &channel->chip->zeromod; |
| 911 |
channel->out[3] = &channel->chip->zeromod; |
| 912 |
break; |
| 913 |
case 0x01: |
| 914 |
channel->slots[0]->mod = &channel->slots[0]->fbmod; |
| 915 |
channel->slots[1]->mod = &channel->chip->zeromod; |
| 916 |
channel->out[0] = &channel->slots[0]->out; |
| 917 |
channel->out[1] = &channel->slots[1]->out; |
| 918 |
channel->out[2] = &channel->chip->zeromod; |
| 919 |
channel->out[3] = &channel->chip->zeromod; |
| 920 |
break; |
| 921 |
} |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
static void OPL3_ChannelWriteC0(opl3_channel *channel, Bit8u data) |
| 926 |
{ |
| 927 |
channel->fb = (data & 0x0e) >> 1; |
| 928 |
channel->con = data & 0x01; |
| 929 |
channel->alg = channel->con; |
| 930 |
if (channel->chip->newm) |
| 931 |
{ |
| 932 |
if (channel->chtype == ch_4op) |
| 933 |
{ |
| 934 |
channel->pair->alg = 0x04 | (channel->con << 1) | (channel->pair->con); |
| 935 |
channel->alg = 0x08; |
| 936 |
OPL3_ChannelSetupAlg(channel->pair); |
| 937 |
} |
| 938 |
else if (channel->chtype == ch_4op2) |
| 939 |
{ |
| 940 |
channel->alg = 0x04 | (channel->pair->con << 1) | (channel->con); |
| 941 |
channel->pair->alg = 0x08; |
| 942 |
OPL3_ChannelSetupAlg(channel); |
| 943 |
} |
| 944 |
else |
| 945 |
{ |
| 946 |
OPL3_ChannelSetupAlg(channel); |
| 947 |
} |
| 948 |
} |
| 949 |
else |
| 950 |
{ |
| 951 |
OPL3_ChannelSetupAlg(channel); |
| 952 |
} |
| 953 |
if (channel->chip->newm) |
| 954 |
{ |
| 955 |
channel->cha = ((data >> 4) & 0x01) ? ~0 : 0; |
| 956 |
channel->chb = ((data >> 5) & 0x01) ? ~0 : 0; |
| 957 |
} |
| 958 |
else |
| 959 |
{ |
| 960 |
channel->cha = channel->chb = (Bit16u)~0; |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
static void OPL3_ChannelKeyOn(opl3_channel *channel) |
| 965 |
{ |
| 966 |
if (channel->chip->newm) |
| 967 |
{ |
| 968 |
if (channel->chtype == ch_4op) |
| 969 |
{ |
| 970 |
OPL3_EnvelopeKeyOn(channel->slots[0], egk_norm); |
| 971 |
OPL3_EnvelopeKeyOn(channel->slots[1], egk_norm); |
| 972 |
OPL3_EnvelopeKeyOn(channel->pair->slots[0], egk_norm); |
| 973 |
OPL3_EnvelopeKeyOn(channel->pair->slots[1], egk_norm); |
| 974 |
} |
| 975 |
else if (channel->chtype == ch_2op || channel->chtype == ch_drum) |
| 976 |
{ |
| 977 |
OPL3_EnvelopeKeyOn(channel->slots[0], egk_norm); |
| 978 |
OPL3_EnvelopeKeyOn(channel->slots[1], egk_norm); |
| 979 |
} |
| 980 |
} |
| 981 |
else |
| 982 |
{ |
| 983 |
OPL3_EnvelopeKeyOn(channel->slots[0], egk_norm); |
| 984 |
OPL3_EnvelopeKeyOn(channel->slots[1], egk_norm); |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
static void OPL3_ChannelKeyOff(opl3_channel *channel) |
| 989 |
{ |
| 990 |
if (channel->chip->newm) |
| 991 |
{ |
| 992 |
if (channel->chtype == ch_4op) |
| 993 |
{ |
| 994 |
OPL3_EnvelopeKeyOff(channel->slots[0], egk_norm); |
| 995 |
OPL3_EnvelopeKeyOff(channel->slots[1], egk_norm); |
| 996 |
OPL3_EnvelopeKeyOff(channel->pair->slots[0], egk_norm); |
| 997 |
OPL3_EnvelopeKeyOff(channel->pair->slots[1], egk_norm); |
| 998 |
} |
| 999 |
else if (channel->chtype == ch_2op || channel->chtype == ch_drum) |
| 1000 |
{ |
| 1001 |
OPL3_EnvelopeKeyOff(channel->slots[0], egk_norm); |
| 1002 |
OPL3_EnvelopeKeyOff(channel->slots[1], egk_norm); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
else |
| 1006 |
{ |
| 1007 |
OPL3_EnvelopeKeyOff(channel->slots[0], egk_norm); |
| 1008 |
OPL3_EnvelopeKeyOff(channel->slots[1], egk_norm); |
| 1009 |
} |
| 1010 |
} |
| 1011 |
|
| 1012 |
static void OPL3_ChannelSet4Op(opl3_chip *chip, Bit8u data) |
| 1013 |
{ |
| 1014 |
Bit8u bit; |
| 1015 |
Bit8u chnum; |
| 1016 |
for (bit = 0; bit < 6; bit++) |
| 1017 |
{ |
| 1018 |
chnum = bit; |
| 1019 |
if (bit >= 3) |
| 1020 |
{ |
| 1021 |
chnum += 9 - 3; |
| 1022 |
} |
| 1023 |
if ((data >> bit) & 0x01) |
| 1024 |
{ |
| 1025 |
chip->channel[chnum].chtype = ch_4op; |
| 1026 |
chip->channel[chnum + 3].chtype = ch_4op2; |
| 1027 |
} |
| 1028 |
else |
| 1029 |
{ |
| 1030 |
chip->channel[chnum].chtype = ch_2op; |
| 1031 |
chip->channel[chnum + 3].chtype = ch_2op; |
| 1032 |
} |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
static Bit16s OPL3_ClipSample(Bit32s sample) |
| 1037 |
{ |
| 1038 |
if (sample > 32767) |
| 1039 |
{ |
| 1040 |
sample = 32767; |
| 1041 |
} |
| 1042 |
else if (sample < -32768) |
| 1043 |
{ |
| 1044 |
sample = -32768; |
| 1045 |
} |
| 1046 |
return (Bit16s)sample; |
| 1047 |
} |
| 1048 |
|
| 1049 |
void OPL3_Generate(opl3_chip *chip, Bit16s *buf) |
| 1050 |
{ |
| 1051 |
Bit8u ii; |
| 1052 |
Bit8u jj; |
| 1053 |
Bit16s accm; |
| 1054 |
Bit8u shift = 0; |
| 1055 |
|
| 1056 |
buf[1] = OPL3_ClipSample(chip->mixbuff[1]); |
| 1057 |
|
| 1058 |
for (ii = 0; ii < 15; ii++) |
| 1059 |
{ |
| 1060 |
OPL3_SlotCalcFB(&chip->slot[ii]); |
| 1061 |
OPL3_EnvelopeCalc(&chip->slot[ii]); |
| 1062 |
OPL3_PhaseGenerate(&chip->slot[ii]); |
| 1063 |
OPL3_SlotGenerate(&chip->slot[ii]); |
| 1064 |
} |
| 1065 |
|
| 1066 |
chip->mixbuff[0] = 0; |
| 1067 |
for (ii = 0; ii < 18; ii++) |
| 1068 |
{ |
| 1069 |
accm = 0; |
| 1070 |
for (jj = 0; jj < 4; jj++) |
| 1071 |
{ |
| 1072 |
accm += *chip->channel[ii].out[jj]; |
| 1073 |
} |
| 1074 |
chip->mixbuff[0] += (Bit16s)(accm & chip->channel[ii].cha); |
| 1075 |
} |
| 1076 |
|
| 1077 |
for (ii = 15; ii < 18; ii++) |
| 1078 |
{ |
| 1079 |
OPL3_SlotCalcFB(&chip->slot[ii]); |
| 1080 |
OPL3_EnvelopeCalc(&chip->slot[ii]); |
| 1081 |
OPL3_PhaseGenerate(&chip->slot[ii]); |
| 1082 |
OPL3_SlotGenerate(&chip->slot[ii]); |
| 1083 |
} |
| 1084 |
|
| 1085 |
buf[0] = OPL3_ClipSample(chip->mixbuff[0]); |
| 1086 |
|
| 1087 |
for (ii = 18; ii < 33; ii++) |
| 1088 |
{ |
| 1089 |
OPL3_SlotCalcFB(&chip->slot[ii]); |
| 1090 |
OPL3_EnvelopeCalc(&chip->slot[ii]); |
| 1091 |
OPL3_PhaseGenerate(&chip->slot[ii]); |
| 1092 |
OPL3_SlotGenerate(&chip->slot[ii]); |
| 1093 |
} |
| 1094 |
|
| 1095 |
chip->mixbuff[1] = 0; |
| 1096 |
for (ii = 0; ii < 18; ii++) |
| 1097 |
{ |
| 1098 |
accm = 0; |
| 1099 |
for (jj = 0; jj < 4; jj++) |
| 1100 |
{ |
| 1101 |
accm += *chip->channel[ii].out[jj]; |
| 1102 |
} |
| 1103 |
chip->mixbuff[1] += (Bit16s)(accm & chip->channel[ii].chb); |
| 1104 |
} |
| 1105 |
|
| 1106 |
for (ii = 33; ii < 36; ii++) |
| 1107 |
{ |
| 1108 |
OPL3_SlotCalcFB(&chip->slot[ii]); |
| 1109 |
OPL3_EnvelopeCalc(&chip->slot[ii]); |
| 1110 |
OPL3_PhaseGenerate(&chip->slot[ii]); |
| 1111 |
OPL3_SlotGenerate(&chip->slot[ii]); |
| 1112 |
} |
| 1113 |
|
| 1114 |
if ((chip->timer & 0x3f) == 0x3f) |
| 1115 |
{ |
| 1116 |
chip->tremolopos = (chip->tremolopos + 1) % 210; |
| 1117 |
} |
| 1118 |
if (chip->tremolopos < 105) |
| 1119 |
{ |
| 1120 |
chip->tremolo = chip->tremolopos >> chip->tremoloshift; |
| 1121 |
} |
| 1122 |
else |
| 1123 |
{ |
| 1124 |
chip->tremolo = (210 - chip->tremolopos) >> chip->tremoloshift; |
| 1125 |
} |
| 1126 |
|
| 1127 |
if ((chip->timer & 0x3ff) == 0x3ff) |
| 1128 |
{ |
| 1129 |
chip->vibpos = (chip->vibpos + 1) & 7; |
| 1130 |
} |
| 1131 |
|
| 1132 |
chip->timer++; |
| 1133 |
|
| 1134 |
chip->eg_add = 0; |
| 1135 |
if (chip->eg_timer) |
| 1136 |
{ |
| 1137 |
while (shift < 36 && ((chip->eg_timer >> shift) & 1) == 0) |
| 1138 |
{ |
| 1139 |
shift++; |
| 1140 |
} |
| 1141 |
if (shift > 12) |
| 1142 |
{ |
| 1143 |
chip->eg_add = 0; |
| 1144 |
} |
| 1145 |
else |
| 1146 |
{ |
| 1147 |
chip->eg_add = shift + 1; |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
if (chip->eg_timerrem || chip->eg_state) |
| 1152 |
{ |
| 1153 |
if (chip->eg_timer == 0xfffffffff) |
| 1154 |
{ |
| 1155 |
chip->eg_timer = 0; |
| 1156 |
chip->eg_timerrem = 1; |
| 1157 |
} |
| 1158 |
else |
| 1159 |
{ |
| 1160 |
chip->eg_timer++; |
| 1161 |
chip->eg_timerrem = 0; |
| 1162 |
} |
| 1163 |
} |
| 1164 |
|
| 1165 |
chip->eg_state ^= 1; |
| 1166 |
|
| 1167 |
while (chip->writebuf[chip->writebuf_cur].time <= chip->writebuf_samplecnt) |
| 1168 |
{ |
| 1169 |
if (!(chip->writebuf[chip->writebuf_cur].reg & 0x200)) |
| 1170 |
{ |
| 1171 |
break; |
| 1172 |
} |
| 1173 |
chip->writebuf[chip->writebuf_cur].reg &= 0x1ff; |
| 1174 |
OPL3_WriteReg(chip, chip->writebuf[chip->writebuf_cur].reg, |
| 1175 |
chip->writebuf[chip->writebuf_cur].data); |
| 1176 |
chip->writebuf_cur = (chip->writebuf_cur + 1) % OPL_WRITEBUF_SIZE; |
| 1177 |
} |
| 1178 |
chip->writebuf_samplecnt++; |
| 1179 |
} |
| 1180 |
|
| 1181 |
void OPL3_GenerateResampled(opl3_chip *chip, Bit16s *buf) |
| 1182 |
{ |
| 1183 |
while (chip->samplecnt >= chip->rateratio) |
| 1184 |
{ |
| 1185 |
chip->oldsamples[0] = chip->samples[0]; |
| 1186 |
chip->oldsamples[1] = chip->samples[1]; |
| 1187 |
OPL3_Generate(chip, chip->samples); |
| 1188 |
chip->samplecnt -= chip->rateratio; |
| 1189 |
} |
| 1190 |
buf[0] = (Bit16s)((chip->oldsamples[0] * (chip->rateratio - chip->samplecnt) |
| 1191 |
+ chip->samples[0] * chip->samplecnt) / chip->rateratio); |
| 1192 |
buf[1] = (Bit16s)((chip->oldsamples[1] * (chip->rateratio - chip->samplecnt) |
| 1193 |
+ chip->samples[1] * chip->samplecnt) / chip->rateratio); |
| 1194 |
chip->samplecnt += 1 << RSM_FRAC; |
| 1195 |
} |
| 1196 |
|
| 1197 |
void OPL3_Reset(opl3_chip *chip, Bit32u samplerate) |
| 1198 |
{ |
| 1199 |
Bit8u slotnum; |
| 1200 |
Bit8u channum; |
| 1201 |
|
| 1202 |
memset(chip, 0, sizeof(opl3_chip)); |
| 1203 |
for (slotnum = 0; slotnum < 36; slotnum++) |
| 1204 |
{ |
| 1205 |
chip->slot[slotnum].chip = chip; |
| 1206 |
chip->slot[slotnum].mod = &chip->zeromod; |
| 1207 |
chip->slot[slotnum].eg_rout = 0x1ff; |
| 1208 |
chip->slot[slotnum].eg_out = 0x1ff; |
| 1209 |
chip->slot[slotnum].eg_gen = envelope_gen_num_release; |
| 1210 |
chip->slot[slotnum].trem = (Bit8u*)&chip->zeromod; |
| 1211 |
chip->slot[slotnum].slot_num = slotnum; |
| 1212 |
} |
| 1213 |
for (channum = 0; channum < 18; channum++) |
| 1214 |
{ |
| 1215 |
chip->channel[channum].slots[0] = &chip->slot[ch_slot[channum]]; |
| 1216 |
chip->channel[channum].slots[1] = &chip->slot[ch_slot[channum] + 3]; |
| 1217 |
chip->slot[ch_slot[channum]].channel = &chip->channel[channum]; |
| 1218 |
chip->slot[ch_slot[channum] + 3].channel = &chip->channel[channum]; |
| 1219 |
if ((channum % 9) < 3) |
| 1220 |
{ |
| 1221 |
chip->channel[channum].pair = &chip->channel[channum + 3]; |
| 1222 |
} |
| 1223 |
else if ((channum % 9) < 6) |
| 1224 |
{ |
| 1225 |
chip->channel[channum].pair = &chip->channel[channum - 3]; |
| 1226 |
} |
| 1227 |
chip->channel[channum].chip = chip; |
| 1228 |
chip->channel[channum].out[0] = &chip->zeromod; |
| 1229 |
chip->channel[channum].out[1] = &chip->zeromod; |
| 1230 |
chip->channel[channum].out[2] = &chip->zeromod; |
| 1231 |
chip->channel[channum].out[3] = &chip->zeromod; |
| 1232 |
chip->channel[channum].chtype = ch_2op; |
| 1233 |
chip->channel[channum].cha = 0xffff; |
| 1234 |
chip->channel[channum].chb = 0xffff; |
| 1235 |
chip->channel[channum].ch_num = channum; |
| 1236 |
OPL3_ChannelSetupAlg(&chip->channel[channum]); |
| 1237 |
} |
| 1238 |
chip->noise = 1; |
| 1239 |
chip->rateratio = (samplerate << RSM_FRAC) / 49716; |
| 1240 |
chip->tremoloshift = 4; |
| 1241 |
chip->vibshift = 1; |
| 1242 |
|
| 1243 |
mus_opl_gain = dsda_IntConfig(dsda_config_mus_opl_gain); |
| 1244 |
} |
| 1245 |
|
| 1246 |
void OPL3_WriteReg(opl3_chip *chip, Bit16u reg, Bit8u v) |
| 1247 |
{ |
| 1248 |
Bit8u high = (reg >> 8) & 0x01; |
| 1249 |
Bit8u regm = reg & 0xff; |
| 1250 |
switch (regm & 0xf0) |
| 1251 |
{ |
| 1252 |
case 0x00: |
| 1253 |
if (high) |
| 1254 |
{ |
| 1255 |
switch (regm & 0x0f) |
| 1256 |
{ |
| 1257 |
case 0x04: |
| 1258 |
OPL3_ChannelSet4Op(chip, v); |
| 1259 |
break; |
| 1260 |
case 0x05: |
| 1261 |
chip->newm = v & 0x01; |
| 1262 |
break; |
| 1263 |
} |
| 1264 |
} |
| 1265 |
else |
| 1266 |
{ |
| 1267 |
switch (regm & 0x0f) |
| 1268 |
{ |
| 1269 |
case 0x08: |
| 1270 |
chip->nts = (v >> 6) & 0x01; |
| 1271 |
break; |
| 1272 |
} |
| 1273 |
} |
| 1274 |
break; |
| 1275 |
case 0x20: |
| 1276 |
case 0x30: |
| 1277 |
if (ad_slot[regm & 0x1f] >= 0) |
| 1278 |
{ |
| 1279 |
OPL3_SlotWrite20(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v); |
| 1280 |
} |
| 1281 |
break; |
| 1282 |
case 0x40: |
| 1283 |
case 0x50: |
| 1284 |
if (ad_slot[regm & 0x1f] >= 0) |
| 1285 |
{ |
| 1286 |
OPL3_SlotWrite40(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v); |
| 1287 |
} |
| 1288 |
break; |
| 1289 |
case 0x60: |
| 1290 |
case 0x70: |
| 1291 |
if (ad_slot[regm & 0x1f] >= 0) |
| 1292 |
{ |
| 1293 |
OPL3_SlotWrite60(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v); |
| 1294 |
} |
| 1295 |
break; |
| 1296 |
case 0x80: |
| 1297 |
case 0x90: |
| 1298 |
if (ad_slot[regm & 0x1f] >= 0) |
| 1299 |
{ |
| 1300 |
OPL3_SlotWrite80(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v); |
| 1301 |
} |
| 1302 |
break; |
| 1303 |
case 0xe0: |
| 1304 |
case 0xf0: |
| 1305 |
if (ad_slot[regm & 0x1f] >= 0) |
| 1306 |
{ |
| 1307 |
OPL3_SlotWriteE0(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v); |
| 1308 |
} |
| 1309 |
break; |
| 1310 |
case 0xa0: |
| 1311 |
if ((regm & 0x0f) < 9) |
| 1312 |
{ |
| 1313 |
OPL3_ChannelWriteA0(&chip->channel[9 * high + (regm & 0x0f)], v); |
| 1314 |
} |
| 1315 |
break; |
| 1316 |
case 0xb0: |
| 1317 |
if (regm == 0xbd && !high) |
| 1318 |
{ |
| 1319 |
chip->tremoloshift = (((v >> 7) ^ 1) << 1) + 2; |
| 1320 |
chip->vibshift = ((v >> 6) & 0x01) ^ 1; |
| 1321 |
OPL3_ChannelUpdateRhythm(chip, v); |
| 1322 |
} |
| 1323 |
else if ((regm & 0x0f) < 9) |
| 1324 |
{ |
| 1325 |
OPL3_ChannelWriteB0(&chip->channel[9 * high + (regm & 0x0f)], v); |
| 1326 |
if (v & 0x20) |
| 1327 |
{ |
| 1328 |
OPL3_ChannelKeyOn(&chip->channel[9 * high + (regm & 0x0f)]); |
| 1329 |
} |
| 1330 |
else |
| 1331 |
{ |
| 1332 |
OPL3_ChannelKeyOff(&chip->channel[9 * high + (regm & 0x0f)]); |
| 1333 |
} |
| 1334 |
} |
| 1335 |
break; |
| 1336 |
case 0xc0: |
| 1337 |
if ((regm & 0x0f) < 9) |
| 1338 |
{ |
| 1339 |
OPL3_ChannelWriteC0(&chip->channel[9 * high + (regm & 0x0f)], v); |
| 1340 |
} |
| 1341 |
break; |
| 1342 |
} |
| 1343 |
} |
| 1344 |
|
| 1345 |
void OPL3_WriteRegBuffered(opl3_chip *chip, Bit16u reg, Bit8u v) |
| 1346 |
{ |
| 1347 |
Bit64u time1, time2; |
| 1348 |
|
| 1349 |
if (chip->writebuf[chip->writebuf_last].reg & 0x200) |
| 1350 |
{ |
| 1351 |
OPL3_WriteReg(chip, chip->writebuf[chip->writebuf_last].reg & 0x1ff, |
| 1352 |
chip->writebuf[chip->writebuf_last].data); |
| 1353 |
|
| 1354 |
chip->writebuf_cur = (chip->writebuf_last + 1) % OPL_WRITEBUF_SIZE; |
| 1355 |
chip->writebuf_samplecnt = chip->writebuf[chip->writebuf_last].time; |
| 1356 |
} |
| 1357 |
|
| 1358 |
chip->writebuf[chip->writebuf_last].reg = reg | 0x200; |
| 1359 |
chip->writebuf[chip->writebuf_last].data = v; |
| 1360 |
time1 = chip->writebuf_lasttime + OPL_WRITEBUF_DELAY; |
| 1361 |
time2 = chip->writebuf_samplecnt; |
| 1362 |
|
| 1363 |
if (time1 < time2) |
| 1364 |
{ |
| 1365 |
time1 = time2; |
| 1366 |
} |
| 1367 |
|
| 1368 |
chip->writebuf[chip->writebuf_last].time = time1; |
| 1369 |
chip->writebuf_lasttime = time1; |
| 1370 |
chip->writebuf_last = (chip->writebuf_last + 1) % OPL_WRITEBUF_SIZE; |
| 1371 |
} |
| 1372 |
|
| 1373 |
void OPL3_GenerateStream(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples) |
| 1374 |
{ |
| 1375 |
Bit32u i; |
| 1376 |
|
| 1377 |
for(i = 0; i < numsamples; i++) |
| 1378 |
{ |
| 1379 |
Bit32s sample; |
| 1380 |
OPL3_GenerateResampled(chip, sndptr); |
| 1381 |
sample = sndptr[0] * mus_opl_gain / 50; |
| 1382 |
sndptr[0] = OPL3_ClipSample(sample); |
| 1383 |
sample = sndptr[1] * mus_opl_gain / 50; |
| 1384 |
sndptr[1] = OPL3_ClipSample(sample); |
| 1385 |
sndptr += 2; |
| 1386 |
} |
| 1387 |
} |