61 static const uint16_t logsinrom[256] = {
62 0x859, 0x6c3, 0x607, 0x58b, 0x52e, 0x4e4, 0x4a6, 0x471,
63 0x443, 0x41a, 0x3f5, 0x3d3, 0x3b5, 0x398, 0x37e, 0x365,
64 0x34e, 0x339, 0x324, 0x311, 0x2ff, 0x2ed, 0x2dc, 0x2cd,
65 0x2bd, 0x2af, 0x2a0, 0x293, 0x286, 0x279, 0x26d, 0x261,
66 0x256, 0x24b, 0x240, 0x236, 0x22c, 0x222, 0x218, 0x20f,
67 0x206, 0x1fd, 0x1f5, 0x1ec, 0x1e4, 0x1dc, 0x1d4, 0x1cd,
68 0x1c5, 0x1be, 0x1b7, 0x1b0, 0x1a9, 0x1a2, 0x19b, 0x195,
69 0x18f, 0x188, 0x182, 0x17c, 0x177, 0x171, 0x16b, 0x166,
70 0x160, 0x15b, 0x155, 0x150, 0x14b, 0x146, 0x141, 0x13c,
71 0x137, 0x133, 0x12e, 0x129, 0x125, 0x121, 0x11c, 0x118,
72 0x114, 0x10f, 0x10b, 0x107, 0x103, 0x0ff, 0x0fb, 0x0f8,
73 0x0f4, 0x0f0, 0x0ec, 0x0e9, 0x0e5, 0x0e2, 0x0de, 0x0db,
74 0x0d7, 0x0d4, 0x0d1, 0x0cd, 0x0ca, 0x0c7, 0x0c4, 0x0c1,
75 0x0be, 0x0bb, 0x0b8, 0x0b5, 0x0b2, 0x0af, 0x0ac, 0x0a9,
76 0x0a7, 0x0a4, 0x0a1, 0x09f, 0x09c, 0x099, 0x097, 0x094,
77 0x092, 0x08f, 0x08d, 0x08a, 0x088, 0x086, 0x083, 0x081,
78 0x07f, 0x07d, 0x07a, 0x078, 0x076, 0x074, 0x072, 0x070,
79 0x06e, 0x06c, 0x06a, 0x068, 0x066, 0x064, 0x062, 0x060,
80 0x05e, 0x05c, 0x05b, 0x059, 0x057, 0x055, 0x053, 0x052,
81 0x050, 0x04e, 0x04d, 0x04b, 0x04a, 0x048, 0x046, 0x045,
82 0x043, 0x042, 0x040, 0x03f, 0x03e, 0x03c, 0x03b, 0x039,
83 0x038, 0x037, 0x035, 0x034, 0x033, 0x031, 0x030, 0x02f,
84 0x02e, 0x02d, 0x02b, 0x02a, 0x029, 0x028, 0x027, 0x026,
85 0x025, 0x024, 0x023, 0x022, 0x021, 0x020, 0x01f, 0x01e,
86 0x01d, 0x01c, 0x01b, 0x01a, 0x019, 0x018, 0x017, 0x017,
87 0x016, 0x015, 0x014, 0x014, 0x013, 0x012, 0x011, 0x011,
88 0x010, 0x00f, 0x00f, 0x00e, 0x00d, 0x00d, 0x00c, 0x00c,
89 0x00b, 0x00a, 0x00a, 0x009, 0x009, 0x008, 0x008, 0x007,
90 0x007, 0x007, 0x006, 0x006, 0x005, 0x005, 0x005, 0x004,
91 0x004, 0x004, 0x003, 0x003, 0x003, 0x002, 0x002, 0x002,
92 0x002, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,
93 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000
100 static const uint16_t
exprom[256] = {
101 0x7fa, 0x7f5, 0x7ef, 0x7ea, 0x7e4, 0x7df, 0x7da, 0x7d4,
102 0x7cf, 0x7c9, 0x7c4, 0x7bf, 0x7b9, 0x7b4, 0x7ae, 0x7a9,
103 0x7a4, 0x79f, 0x799, 0x794, 0x78f, 0x78a, 0x784, 0x77f,
104 0x77a, 0x775, 0x770, 0x76a, 0x765, 0x760, 0x75b, 0x756,
105 0x751, 0x74c, 0x747, 0x742, 0x73d, 0x738, 0x733, 0x72e,
106 0x729, 0x724, 0x71f, 0x71a, 0x715, 0x710, 0x70b, 0x706,
107 0x702, 0x6fd, 0x6f8, 0x6f3, 0x6ee, 0x6e9, 0x6e5, 0x6e0,
108 0x6db, 0x6d6, 0x6d2, 0x6cd, 0x6c8, 0x6c4, 0x6bf, 0x6ba,
109 0x6b5, 0x6b1, 0x6ac, 0x6a8, 0x6a3, 0x69e, 0x69a, 0x695,
110 0x691, 0x68c, 0x688, 0x683, 0x67f, 0x67a, 0x676, 0x671,
111 0x66d, 0x668, 0x664, 0x65f, 0x65b, 0x657, 0x652, 0x64e,
112 0x649, 0x645, 0x641, 0x63c, 0x638, 0x634, 0x630, 0x62b,
113 0x627, 0x623, 0x61e, 0x61a, 0x616, 0x612, 0x60e, 0x609,
114 0x605, 0x601, 0x5fd, 0x5f9, 0x5f5, 0x5f0, 0x5ec, 0x5e8,
115 0x5e4, 0x5e0, 0x5dc, 0x5d8, 0x5d4, 0x5d0, 0x5cc, 0x5c8,
116 0x5c4, 0x5c0, 0x5bc, 0x5b8, 0x5b4, 0x5b0, 0x5ac, 0x5a8,
117 0x5a4, 0x5a0, 0x59c, 0x599, 0x595, 0x591, 0x58d, 0x589,
118 0x585, 0x581, 0x57e, 0x57a, 0x576, 0x572, 0x56f, 0x56b,
119 0x567, 0x563, 0x560, 0x55c, 0x558, 0x554, 0x551, 0x54d,
120 0x549, 0x546, 0x542, 0x53e, 0x53b, 0x537, 0x534, 0x530,
121 0x52c, 0x529, 0x525, 0x522, 0x51e, 0x51b, 0x517, 0x514,
122 0x510, 0x50c, 0x509, 0x506, 0x502, 0x4ff, 0x4fb, 0x4f8,
123 0x4f4, 0x4f1, 0x4ed, 0x4ea, 0x4e7, 0x4e3, 0x4e0, 0x4dc,
124 0x4d9, 0x4d6, 0x4d2, 0x4cf, 0x4cc, 0x4c8, 0x4c5, 0x4c2,
125 0x4be, 0x4bb, 0x4b8, 0x4b5, 0x4b1, 0x4ae, 0x4ab, 0x4a8,
126 0x4a4, 0x4a1, 0x49e, 0x49b, 0x498, 0x494, 0x491, 0x48e,
127 0x48b, 0x488, 0x485, 0x482, 0x47e, 0x47b, 0x478, 0x475,
128 0x472, 0x46f, 0x46c, 0x469, 0x466, 0x463, 0x460, 0x45d,
129 0x45a, 0x457, 0x454, 0x451, 0x44e, 0x44b, 0x448, 0x445,
130 0x442, 0x43f, 0x43c, 0x439, 0x436, 0x433, 0x430, 0x42d,
131 0x42a, 0x428, 0x425, 0x422, 0x41f, 0x41c, 0x419, 0x416,
132 0x414, 0x411, 0x40e, 0x40b, 0x408, 0x406, 0x403, 0x400
141 static const uint8_t mt[16] = {
142 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 24, 24, 30, 30
149 static const uint8_t kslrom[16] = {
150 0, 32, 40, 45, 48, 51, 53, 55, 56, 58, 59, 60, 61, 62, 63, 64
153 static const uint8_t kslshift[4] = {
161 static const uint8_t eg_incstep[4][4] = {
172 static const int8_t ad_slot[0x20] = {
173 0, 1, 2, 3, 4, 5, -1, -1, 6, 7, 8, 9, 10, 11, -1, -1,
174 12, 13, 14, 15, 16, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
177 static const uint8_t ch_slot[18] = {
178 0, 1, 2, 6, 7, 8, 12, 13, 14, 18, 19, 20, 24, 25, 26, 30, 31, 32
188 static int16_t OPL3_EnvelopeCalcExp(uint32_t level)
194 return (
exprom[level & 0xff] << 1) >> (level >> 8);
197 static int16_t OPL3_EnvelopeCalcSin0(uint16_t
phase, uint16_t envelope)
208 out = logsinrom[(
phase & 0xff) ^ 0xff];
212 out = logsinrom[
phase & 0xff];
214 return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg;
217 static int16_t OPL3_EnvelopeCalcSin1(uint16_t
phase, uint16_t envelope)
225 else if (
phase & 0x100)
227 out = logsinrom[(
phase & 0xff) ^ 0xff];
231 out = logsinrom[
phase & 0xff];
233 return OPL3_EnvelopeCalcExp(out + (envelope << 3));
236 static int16_t OPL3_EnvelopeCalcSin2(uint16_t
phase, uint16_t envelope)
242 out = logsinrom[(
phase & 0xff) ^ 0xff];
246 out = logsinrom[
phase & 0xff];
248 return OPL3_EnvelopeCalcExp(out + (envelope << 3));
251 static int16_t OPL3_EnvelopeCalcSin3(uint16_t
phase, uint16_t envelope)
261 out = logsinrom[
phase & 0xff];
263 return OPL3_EnvelopeCalcExp(out + (envelope << 3));
266 static int16_t OPL3_EnvelopeCalcSin4(uint16_t
phase, uint16_t envelope)
271 if ((
phase & 0x300) == 0x100)
279 else if (
phase & 0x80)
281 out = logsinrom[((
phase ^ 0xff) << 1) & 0xff];
285 out = logsinrom[(
phase << 1) & 0xff];
287 return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg;
290 static int16_t OPL3_EnvelopeCalcSin5(uint16_t
phase, uint16_t envelope)
298 else if (
phase & 0x80)
300 out = logsinrom[((
phase ^ 0xff) << 1) & 0xff];
304 out = logsinrom[(
phase << 1) & 0xff];
306 return OPL3_EnvelopeCalcExp(out + (envelope << 3));
309 static int16_t OPL3_EnvelopeCalcSin6(uint16_t
phase, uint16_t envelope)
317 return OPL3_EnvelopeCalcExp(envelope << 3) ^ neg;
320 static int16_t OPL3_EnvelopeCalcSin7(uint16_t
phase, uint16_t envelope)
331 return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg;
335 OPL3_EnvelopeCalcSin0,
336 OPL3_EnvelopeCalcSin1,
337 OPL3_EnvelopeCalcSin2,
338 OPL3_EnvelopeCalcSin3,
339 OPL3_EnvelopeCalcSin4,
340 OPL3_EnvelopeCalcSin5,
341 OPL3_EnvelopeCalcSin6,
342 OPL3_EnvelopeCalcSin7
353 static void OPL3_EnvelopeUpdateKSL(
opl3_slot *slot)
361 slot->
eg_ksl = (uint8_t)ksl;
364 static void OPL3_EnvelopeCalc(
opl3_slot *slot)
370 uint8_t reg_rate = 0;
372 uint8_t eg_shift, shift;
407 nonzero = (reg_rate != 0);
408 rate = ks + (reg_rate << 2);
410 rate_lo = rate & 0x03;
429 shift = (rate_lo >> 1) & 0x01;
432 shift = rate_lo & 0x01;
441 shift = (rate_hi & 0x03) + eg_incstep[rate_lo][slot->
chip->
timer & 0x03];
456 if (reset && rate_hi == 0x0f)
461 if ((slot->
eg_rout & 0x1f8) == 0x1f8)
476 else if (slot->
key && shift > 0 && rate_hi != 0x0f)
478 eg_inc = ~slot->
eg_rout >> (4 - shift);
486 else if (!eg_off && !reset && shift > 0)
488 eg_inc = 1 << (shift - 1);
493 if (!eg_off && !reset && shift > 0)
495 eg_inc = 1 << (shift - 1);
499 slot->
eg_rout = (eg_rout + eg_inc) & 0x1ff;
511 static void OPL3_EnvelopeKeyOn(
opl3_slot *slot, uint8_t type)
516 static void OPL3_EnvelopeKeyOff(
opl3_slot *slot, uint8_t type)
525 static void OPL3_PhaseGenerate(
opl3_slot *slot)
530 uint8_t rm_xor, n_bit;
541 range = (f_num >> 7) & 7;
582 if (chip->
rhy & 0x20)
591 if (rm_xor ^ (noise & 1))
611 n_bit = ((noise >> 14) ^ noise) & 0x01;
612 chip->
noise = (noise >> 1) | (n_bit << 22);
621 if ((
data >> 7) & 0x01)
639 OPL3_EnvelopeUpdateKSL(slot);
667 static void OPL3_SlotGenerate(
opl3_slot *slot)
672 static void OPL3_SlotCalcFB(
opl3_slot *slot)
689 static void OPL3_ChannelSetupAlg(
opl3_channel *channel);
691 static void OPL3_ChannelUpdateRhythm(
opl3_chip *chip, uint8_t
data)
699 if (chip->
rhy & 0x20)
716 for (chnum = 6; chnum < 9; chnum++)
720 OPL3_ChannelSetupAlg(channel6);
721 OPL3_ChannelSetupAlg(channel7);
722 OPL3_ChannelSetupAlg(channel8);
724 if (chip->
rhy & 0x01)
733 if (chip->
rhy & 0x02)
742 if (chip->
rhy & 0x04)
751 if (chip->
rhy & 0x08)
760 if (chip->
rhy & 0x10)
773 for (chnum = 6; chnum < 9; chnum++)
776 OPL3_ChannelSetupAlg(&chip->
channel[chnum]);
790 channel->
ksv = (channel->
block << 1)
791 | ((channel->
f_num >> (0x09 - channel->
chip->
nts)) & 0x01);
792 OPL3_EnvelopeUpdateKSL(channel->
slots[0]);
793 OPL3_EnvelopeUpdateKSL(channel->
slots[1]);
798 OPL3_EnvelopeUpdateKSL(channel->
pair->
slots[0]);
799 OPL3_EnvelopeUpdateKSL(channel->
pair->
slots[1]);
811 channel->
ksv = (channel->
block << 1)
812 | ((channel->
f_num >> (0x09 - channel->
chip->
nts)) & 0x01);
813 OPL3_EnvelopeUpdateKSL(channel->
slots[0]);
814 OPL3_EnvelopeUpdateKSL(channel->
slots[1]);
820 OPL3_EnvelopeUpdateKSL(channel->
pair->
slots[0]);
821 OPL3_EnvelopeUpdateKSL(channel->
pair->
slots[1]);
835 switch (channel->
alg & 0x01)
848 if (channel->
alg & 0x08)
852 if (channel->
alg & 0x04)
858 switch (channel->
alg & 0x03)
904 switch (channel->
alg & 0x01)
928 channel->
fb = (
data & 0x0e) >> 1;
930 channel->
alg = channel->
con;
937 OPL3_ChannelSetupAlg(channel->
pair);
941 channel->
alg = 0x04 | (channel->
pair->
con << 1) | (channel->
con);
943 OPL3_ChannelSetupAlg(channel);
947 OPL3_ChannelSetupAlg(channel);
952 OPL3_ChannelSetupAlg(channel);
956 channel->
cha = ((
data >> 4) & 0x01) ? ~0 : 0;
957 channel->
chb = ((
data >> 5) & 0x01) ? ~0 : 0;
961 channel->
cha = channel->
chb = (uint16_t)~0;
1013 static void OPL3_ChannelSet4Op(
opl3_chip *chip, uint8_t
data)
1017 for (bit = 0; bit < 6; bit++)
1024 if ((
data >> bit) & 0x01)
1037 static int16_t OPL3_ClipSample(int32_t sample)
1043 else if (sample < -32768)
1047 return (int16_t)sample;
1059 for (ii = 0; ii < 15; ii++)
1061 OPL3_SlotCalcFB(&chip->
slot[ii]);
1062 OPL3_EnvelopeCalc(&chip->
slot[ii]);
1063 OPL3_PhaseGenerate(&chip->
slot[ii]);
1064 OPL3_SlotGenerate(&chip->
slot[ii]);
1068 for (ii = 0; ii < 18; ii++)
1071 for (jj = 0; jj < 4; jj++)
1078 for (ii = 15; ii < 18; ii++)
1080 OPL3_SlotCalcFB(&chip->
slot[ii]);
1081 OPL3_EnvelopeCalc(&chip->
slot[ii]);
1082 OPL3_PhaseGenerate(&chip->
slot[ii]);
1083 OPL3_SlotGenerate(&chip->
slot[ii]);
1088 for (ii = 18; ii < 33; ii++)
1090 OPL3_SlotCalcFB(&chip->
slot[ii]);
1091 OPL3_EnvelopeCalc(&chip->
slot[ii]);
1092 OPL3_PhaseGenerate(&chip->
slot[ii]);
1093 OPL3_SlotGenerate(&chip->
slot[ii]);
1097 for (ii = 0; ii < 18; ii++)
1100 for (jj = 0; jj < 4; jj++)
1107 for (ii = 33; ii < 36; ii++)
1109 OPL3_SlotCalcFB(&chip->
slot[ii]);
1110 OPL3_EnvelopeCalc(&chip->
slot[ii]);
1111 OPL3_PhaseGenerate(&chip->
slot[ii]);
1112 OPL3_SlotGenerate(&chip->
slot[ii]);
1115 if ((chip->
timer & 0x3f) == 0x3f)
1128 if ((chip->
timer & 0x3ff) == 0x3ff)
1138 while (shift < 36 && ((chip->
eg_timer >> shift) & 1) == 0)
1148 chip->
eg_add = shift + 1;
1204 for (slotnum = 0; slotnum < 36; slotnum++)
1214 for (channum = 0; channum < 18; channum++)
1220 if ((channum % 9) < 3)
1224 else if ((channum % 9) < 6)
1237 OPL3_ChannelSetupAlg(&chip->
channel[channum]);
1247 uint8_t high = (reg >> 8) & 0x01;
1248 uint8_t regm = reg & 0xff;
1249 switch (regm & 0xf0)
1254 switch (regm & 0x0f)
1257 OPL3_ChannelSet4Op(chip, v);
1260 chip->
newm = v & 0x01;
1266 switch (regm & 0x0f)
1269 chip->
nts = (v >> 6) & 0x01;
1276 if (ad_slot[regm & 0x1f] >= 0)
1278 OPL3_SlotWrite20(&chip->
slot[18 * high + ad_slot[regm & 0x1f]], v);
1283 if (ad_slot[regm & 0x1f] >= 0)
1285 OPL3_SlotWrite40(&chip->
slot[18 * high + ad_slot[regm & 0x1f]], v);
1290 if (ad_slot[regm & 0x1f] >= 0)
1292 OPL3_SlotWrite60(&chip->
slot[18 * high + ad_slot[regm & 0x1f]], v);
1297 if (ad_slot[regm & 0x1f] >= 0)
1299 OPL3_SlotWrite80(&chip->
slot[18 * high + ad_slot[regm & 0x1f]], v);
1304 if (ad_slot[regm & 0x1f] >= 0)
1306 OPL3_SlotWriteE0(&chip->
slot[18 * high + ad_slot[regm & 0x1f]], v);
1310 if ((regm & 0x0f) < 9)
1312 OPL3_ChannelWriteA0(&chip->
channel[9 * high + (regm & 0x0f)], v);
1316 if (regm == 0xbd && !high)
1319 chip->
vibshift = ((v >> 6) & 0x01) ^ 1;
1320 OPL3_ChannelUpdateRhythm(chip, v);
1322 else if ((regm & 0x0f) < 9)
1324 OPL3_ChannelWriteB0(&chip->
channel[9 * high + (regm & 0x0f)], v);
1327 OPL3_ChannelKeyOn(&chip->
channel[9 * high + (regm & 0x0f)]);
1331 OPL3_ChannelKeyOff(&chip->
channel[9 * high + (regm & 0x0f)]);
1336 if ((regm & 0x0f) < 9)
1338 OPL3_ChannelWriteC0(&chip->
channel[9 * high + (regm & 0x0f)], v);
1346 uint64_t time1, time2;
1380 while (numsamples--) {
1382 sndptr1 += increment1;
1383 sndptr2 += increment2;