Debian Codegraph @

android/app/jni/extern/Nuked-OPLL/opll.c

raw
1 /*
2 * Copyright (C) 2019-2023 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 * Yamaha YM2413 emulator
16 * Thanks:
17 * siliconpr0n.org(digshadow, John McMaster):
18 * VRC VII decap and die shot.
19 *
20 * version: 1.0.2
21 */
22
23 #include <string.h>
24 #include "opll.h"
25
26 enum {
27 eg_num_attack = 0,
28 eg_num_decay = 1,
29 eg_num_sustain = 2,
30 eg_num_release = 3
31 };
32
33 enum {
34 rm_num_bd0 = 0,
35 rm_num_hh = 1,
36 rm_num_tom = 2,
37 rm_num_bd1 = 3,
38 rm_num_sd = 4,
39 rm_num_tc = 5
40 };
41
42 /* logsin table */
43 static const uint16_t logsinrom[256] = {
44 0x859, 0x6c3, 0x607, 0x58b, 0x52e, 0x4e4, 0x4a6, 0x471,
45 0x443, 0x41a, 0x3f5, 0x3d3, 0x3b5, 0x398, 0x37e, 0x365,
46 0x34e, 0x339, 0x324, 0x311, 0x2ff, 0x2ed, 0x2dc, 0x2cd,
47 0x2bd, 0x2af, 0x2a0, 0x293, 0x286, 0x279, 0x26d, 0x261,
48 0x256, 0x24b, 0x240, 0x236, 0x22c, 0x222, 0x218, 0x20f,
49 0x206, 0x1fd, 0x1f5, 0x1ec, 0x1e4, 0x1dc, 0x1d4, 0x1cd,
50 0x1c5, 0x1be, 0x1b7, 0x1b0, 0x1a9, 0x1a2, 0x19b, 0x195,
51 0x18f, 0x188, 0x182, 0x17c, 0x177, 0x171, 0x16b, 0x166,
52 0x160, 0x15b, 0x155, 0x150, 0x14b, 0x146, 0x141, 0x13c,
53 0x137, 0x133, 0x12e, 0x129, 0x125, 0x121, 0x11c, 0x118,
54 0x114, 0x10f, 0x10b, 0x107, 0x103, 0x0ff, 0x0fb, 0x0f8,
55 0x0f4, 0x0f0, 0x0ec, 0x0e9, 0x0e5, 0x0e2, 0x0de, 0x0db,
56 0x0d7, 0x0d4, 0x0d1, 0x0cd, 0x0ca, 0x0c7, 0x0c4, 0x0c1,
57 0x0be, 0x0bb, 0x0b8, 0x0b5, 0x0b2, 0x0af, 0x0ac, 0x0a9,
58 0x0a7, 0x0a4, 0x0a1, 0x09f, 0x09c, 0x099, 0x097, 0x094,
59 0x092, 0x08f, 0x08d, 0x08a, 0x088, 0x086, 0x083, 0x081,
60 0x07f, 0x07d, 0x07a, 0x078, 0x076, 0x074, 0x072, 0x070,
61 0x06e, 0x06c, 0x06a, 0x068, 0x066, 0x064, 0x062, 0x060,
62 0x05e, 0x05c, 0x05b, 0x059, 0x057, 0x055, 0x053, 0x052,
63 0x050, 0x04e, 0x04d, 0x04b, 0x04a, 0x048, 0x046, 0x045,
64 0x043, 0x042, 0x040, 0x03f, 0x03e, 0x03c, 0x03b, 0x039,
65 0x038, 0x037, 0x035, 0x034, 0x033, 0x031, 0x030, 0x02f,
66 0x02e, 0x02d, 0x02b, 0x02a, 0x029, 0x028, 0x027, 0x026,
67 0x025, 0x024, 0x023, 0x022, 0x021, 0x020, 0x01f, 0x01e,
68 0x01d, 0x01c, 0x01b, 0x01a, 0x019, 0x018, 0x017, 0x017,
69 0x016, 0x015, 0x014, 0x014, 0x013, 0x012, 0x011, 0x011,
70 0x010, 0x00f, 0x00f, 0x00e, 0x00d, 0x00d, 0x00c, 0x00c,
71 0x00b, 0x00a, 0x00a, 0x009, 0x009, 0x008, 0x008, 0x007,
72 0x007, 0x007, 0x006, 0x006, 0x005, 0x005, 0x005, 0x004,
73 0x004, 0x004, 0x003, 0x003, 0x003, 0x002, 0x002, 0x002,
74 0x002, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,
75 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000
76 };
77
78 /* exp table */
79 static const uint16_t exprom[256] = {
80 0x7fa, 0x7f5, 0x7ef, 0x7ea, 0x7e4, 0x7df, 0x7da, 0x7d4,
81 0x7cf, 0x7c9, 0x7c4, 0x7bf, 0x7b9, 0x7b4, 0x7ae, 0x7a9,
82 0x7a4, 0x79f, 0x799, 0x794, 0x78f, 0x78a, 0x784, 0x77f,
83 0x77a, 0x775, 0x770, 0x76a, 0x765, 0x760, 0x75b, 0x756,
84 0x751, 0x74c, 0x747, 0x742, 0x73d, 0x738, 0x733, 0x72e,
85 0x729, 0x724, 0x71f, 0x71a, 0x715, 0x710, 0x70b, 0x706,
86 0x702, 0x6fd, 0x6f8, 0x6f3, 0x6ee, 0x6e9, 0x6e5, 0x6e0,
87 0x6db, 0x6d6, 0x6d2, 0x6cd, 0x6c8, 0x6c4, 0x6bf, 0x6ba,
88 0x6b5, 0x6b1, 0x6ac, 0x6a8, 0x6a3, 0x69e, 0x69a, 0x695,
89 0x691, 0x68c, 0x688, 0x683, 0x67f, 0x67a, 0x676, 0x671,
90 0x66d, 0x668, 0x664, 0x65f, 0x65b, 0x657, 0x652, 0x64e,
91 0x649, 0x645, 0x641, 0x63c, 0x638, 0x634, 0x630, 0x62b,
92 0x627, 0x623, 0x61e, 0x61a, 0x616, 0x612, 0x60e, 0x609,
93 0x605, 0x601, 0x5fd, 0x5f9, 0x5f5, 0x5f0, 0x5ec, 0x5e8,
94 0x5e4, 0x5e0, 0x5dc, 0x5d8, 0x5d4, 0x5d0, 0x5cc, 0x5c8,
95 0x5c4, 0x5c0, 0x5bc, 0x5b8, 0x5b4, 0x5b0, 0x5ac, 0x5a8,
96 0x5a4, 0x5a0, 0x59c, 0x599, 0x595, 0x591, 0x58d, 0x589,
97 0x585, 0x581, 0x57e, 0x57a, 0x576, 0x572, 0x56f, 0x56b,
98 0x567, 0x563, 0x560, 0x55c, 0x558, 0x554, 0x551, 0x54d,
99 0x549, 0x546, 0x542, 0x53e, 0x53b, 0x537, 0x534, 0x530,
100 0x52c, 0x529, 0x525, 0x522, 0x51e, 0x51b, 0x517, 0x514,
101 0x510, 0x50c, 0x509, 0x506, 0x502, 0x4ff, 0x4fb, 0x4f8,
102 0x4f4, 0x4f1, 0x4ed, 0x4ea, 0x4e7, 0x4e3, 0x4e0, 0x4dc,
103 0x4d9, 0x4d6, 0x4d2, 0x4cf, 0x4cc, 0x4c8, 0x4c5, 0x4c2,
104 0x4be, 0x4bb, 0x4b8, 0x4b5, 0x4b1, 0x4ae, 0x4ab, 0x4a8,
105 0x4a4, 0x4a1, 0x49e, 0x49b, 0x498, 0x494, 0x491, 0x48e,
106 0x48b, 0x488, 0x485, 0x482, 0x47e, 0x47b, 0x478, 0x475,
107 0x472, 0x46f, 0x46c, 0x469, 0x466, 0x463, 0x460, 0x45d,
108 0x45a, 0x457, 0x454, 0x451, 0x44e, 0x44b, 0x448, 0x445,
109 0x442, 0x43f, 0x43c, 0x439, 0x436, 0x433, 0x430, 0x42d,
110 0x42a, 0x428, 0x425, 0x422, 0x41f, 0x41c, 0x419, 0x416,
111 0x414, 0x411, 0x40e, 0x40b, 0x408, 0x406, 0x403, 0x400
112 };
113
114 static const opll_patch_t patch_ds1001[opll_patch_max] = {
115 { 0x05, 0x00, 0x00, 0x06,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x03, 0x01 },{ 0x00, 0x00 },{ 0x0e, 0x08 },{ 0x08, 0x01 },{ 0x04, 0x02 },{ 0x02, 0x07 } },
116 { 0x14, 0x00, 0x01, 0x05,{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x03, 0x01 },{ 0x00, 0x00 },{ 0x0d, 0x0f },{ 0x08, 0x06 },{ 0x02, 0x01 },{ 0x03, 0x02 } },
117 { 0x08, 0x00, 0x01, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x0f, 0x0b },{ 0x0a, 0x02 },{ 0x02, 0x01 },{ 0x00, 0x02 } },
118 { 0x0c, 0x00, 0x00, 0x07,{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x01, 0x01 },{ 0x01, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x0a, 0x06 },{ 0x08, 0x04 },{ 0x06, 0x02 },{ 0x01, 0x07 } },
119 { 0x1e, 0x00, 0x00, 0x06,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x00 },{ 0x02, 0x01 },{ 0x00, 0x00 },{ 0x0e, 0x07 },{ 0x01, 0x06 },{ 0x00, 0x02 },{ 0x01, 0x08 } },
120 { 0x06, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x02, 0x01 },{ 0x00, 0x00 },{ 0x0a, 0x0e },{ 0x03, 0x02 },{ 0x0f, 0x0f },{ 0x04, 0x04 } },
121 { 0x1d, 0x00, 0x00, 0x07,{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x08, 0x08 },{ 0x02, 0x01 },{ 0x01, 0x00 },{ 0x01, 0x07 } },
122 { 0x22, 0x01, 0x00, 0x07,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x03, 0x01 },{ 0x00, 0x00 },{ 0x0a, 0x07 },{ 0x02, 0x02 },{ 0x00, 0x01 },{ 0x01, 0x07 } },
123 { 0x25, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x01, 0x01 },{ 0x05, 0x01 },{ 0x00, 0x00 },{ 0x04, 0x07 },{ 0x00, 0x03 },{ 0x07, 0x00 },{ 0x02, 0x01 } },
124 { 0x0f, 0x00, 0x01, 0x07,{ 0x01, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x01, 0x00 },{ 0x05, 0x01 },{ 0x00, 0x00 },{ 0x0a, 0x0a },{ 0x08, 0x05 },{ 0x05, 0x00 },{ 0x01, 0x02 } },
125 { 0x24, 0x00, 0x00, 0x07,{ 0x00, 0x01 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x07, 0x01 },{ 0x00, 0x00 },{ 0x0f, 0x0f },{ 0x08, 0x08 },{ 0x02, 0x01 },{ 0x02, 0x02 } },
126 { 0x11, 0x00, 0x00, 0x06,{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x00 },{ 0x01, 0x03 },{ 0x00, 0x00 },{ 0x06, 0x07 },{ 0x05, 0x04 },{ 0x01, 0x01 },{ 0x08, 0x06 } },
127 { 0x13, 0x00, 0x00, 0x05,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x02 },{ 0x03, 0x00 },{ 0x0c, 0x09 },{ 0x09, 0x05 },{ 0x00, 0x00 },{ 0x03, 0x02 } },
128 { 0x0c, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x01, 0x03 },{ 0x00, 0x00 },{ 0x09, 0x0c },{ 0x04, 0x00 },{ 0x03, 0x0f },{ 0x03, 0x06 } },
129 { 0x0d, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x01, 0x01 },{ 0x00, 0x01 },{ 0x01, 0x02 },{ 0x00, 0x00 },{ 0x0c, 0x0d },{ 0x01, 0x05 },{ 0x05, 0x00 },{ 0x06, 0x06 } },
130
131 { 0x18, 0x00, 0x01, 0x07,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x00, 0x00 },{ 0x0d, 0x00 },{ 0x0f, 0x00 },{ 0x06, 0x00 },{ 0x0a, 0x00 } },
132 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x00, 0x00 },{ 0x0c, 0x00 },{ 0x08, 0x00 },{ 0x0a, 0x00 },{ 0x07, 0x00 } },
133 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x05, 0x00 },{ 0x00, 0x00 },{ 0x0f, 0x00 },{ 0x08, 0x00 },{ 0x05, 0x00 },{ 0x09, 0x00 } },
134 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x00, 0x0f },{ 0x00, 0x08 },{ 0x00, 0x06 },{ 0x00, 0x0d } },
135 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x00, 0x0d },{ 0x00, 0x08 },{ 0x00, 0x04 },{ 0x00, 0x08 } },
136 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x00, 0x0a },{ 0x00, 0x0a },{ 0x00, 0x05 },{ 0x00, 0x05 } }
137 };
138
139 static const opll_patch_t patch_ym2413[opll_patch_max] = {
140 { 0x1e, 0x01, 0x00, 0x07,{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x01, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x0d, 0x07 },{ 0x00, 0x08 },{ 0x00, 0x01 },{ 0x00, 0x07 } },
141 { 0x1a, 0x00, 0x01, 0x05,{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x03, 0x01 },{ 0x00, 0x00 },{ 0x0d, 0x0f },{ 0x08, 0x07 },{ 0x02, 0x01 },{ 0x03, 0x03 } },
142 { 0x19, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x03, 0x01 },{ 0x02, 0x00 },{ 0x0f, 0x0c },{ 0x02, 0x04 },{ 0x01, 0x02 },{ 0x01, 0x03 } },
143 { 0x0e, 0x00, 0x00, 0x07,{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x01, 0x01 },{ 0x01, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x0a, 0x06 },{ 0x08, 0x04 },{ 0x07, 0x02 },{ 0x00, 0x07 } },
144 { 0x1e, 0x00, 0x00, 0x06,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x00 },{ 0x02, 0x01 },{ 0x00, 0x00 },{ 0x0e, 0x07 },{ 0x00, 0x06 },{ 0x00, 0x02 },{ 0x00, 0x08 } },
145 { 0x16, 0x00, 0x00, 0x05,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x00 },{ 0x01, 0x02 },{ 0x00, 0x00 },{ 0x0e, 0x07 },{ 0x00, 0x01 },{ 0x00, 0x01 },{ 0x00, 0x08 } },
146 { 0x1d, 0x00, 0x00, 0x07,{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x08, 0x08 },{ 0x02, 0x01 },{ 0x01, 0x00 },{ 0x00, 0x07 } },
147 { 0x2d, 0x01, 0x00, 0x04,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x03, 0x01 },{ 0x00, 0x00 },{ 0x0a, 0x07 },{ 0x02, 0x02 },{ 0x00, 0x00 },{ 0x00, 0x07 } },
148 { 0x1b, 0x00, 0x00, 0x06,{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x06, 0x06 },{ 0x04, 0x05 },{ 0x01, 0x01 },{ 0x00, 0x07 } },
149 { 0x0b, 0x01, 0x01, 0x00,{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x08, 0x0f },{ 0x05, 0x07 },{ 0x07, 0x00 },{ 0x01, 0x07 } },
150 { 0x03, 0x01, 0x00, 0x01,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x03, 0x01 },{ 0x02, 0x00 },{ 0x0f, 0x0e },{ 0x0a, 0x04 },{ 0x01, 0x00 },{ 0x00, 0x04 } },
151 { 0x24, 0x00, 0x00, 0x07,{ 0x00, 0x01 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x07, 0x01 },{ 0x00, 0x00 },{ 0x0f, 0x0f },{ 0x08, 0x08 },{ 0x02, 0x01 },{ 0x02, 0x02 } },
152 { 0x0c, 0x00, 0x00, 0x05,{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x00 },{ 0x00, 0x01 },{ 0x01, 0x00 },{ 0x00, 0x00 },{ 0x0c, 0x0f },{ 0x02, 0x05 },{ 0x02, 0x04 },{ 0x00, 0x02 } },
153 { 0x15, 0x00, 0x00, 0x03,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x00 },{ 0x0c, 0x09 },{ 0x09, 0x05 },{ 0x00, 0x00 },{ 0x03, 0x02 } },
154 { 0x09, 0x00, 0x00, 0x03,{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x02, 0x00 },{ 0x0f, 0x0e },{ 0x01, 0x04 },{ 0x04, 0x01 },{ 0x00, 0x03 } },
155
156 { 0x18, 0x00, 0x01, 0x07,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x00, 0x00 },{ 0x0d, 0x00 },{ 0x0f, 0x00 },{ 0x06, 0x00 },{ 0x0a, 0x00 } },
157 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x00, 0x00 },{ 0x0c, 0x00 },{ 0x08, 0x00 },{ 0x0a, 0x00 },{ 0x07, 0x00 } },
158 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x05, 0x00 },{ 0x00, 0x00 },{ 0x0f, 0x00 },{ 0x08, 0x00 },{ 0x05, 0x00 },{ 0x09, 0x00 } },
159 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x00, 0x0f },{ 0x00, 0x08 },{ 0x00, 0x06 },{ 0x00, 0x0d } },
160 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x00, 0x0d },{ 0x00, 0x08 },{ 0x00, 0x04 },{ 0x00, 0x08 } },
161 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x00, 0x0a },{ 0x00, 0x0a },{ 0x00, 0x05 },{ 0x00, 0x05 } }
162 };
163
164 static const opll_patch_t patch_ymf281[opll_patch_max] = {
165 { 0x1a, 0x00, 0x00, 0x07,{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x02, 0x01 },{ 0x00, 0x00 },{ 0x0f, 0x06 },{ 0x0f, 0x07 },{ 0x00, 0x01 },{ 0x00, 0x06 } },
166 { 0x05, 0x00, 0x00, 0x01,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x0f, 0x08 },{ 0x06, 0x03 },{ 0x08, 0x00 },{ 0x00, 0x03 } },
167 { 0x16, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x03, 0x01 },{ 0x02, 0x00 },{ 0x0f, 0x0d },{ 0x02, 0x03 },{ 0x01, 0x00 },{ 0x01, 0x03 } },
168 { 0x0b, 0x00, 0x01, 0x07,{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x01, 0x01 },{ 0x01, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x0a, 0x06 },{ 0x08, 0x04 },{ 0x07, 0x01 },{ 0x00, 0x07 } },
169 { 0x1e, 0x00, 0x00, 0x06,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x00 },{ 0x02, 0x01 },{ 0x00, 0x00 },{ 0x0e, 0x07 },{ 0x01, 0x06 },{ 0x00, 0x02 },{ 0x00, 0x08 } },
170 { 0x02, 0x00, 0x01, 0x06,{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x02, 0x00 },{ 0x09, 0x06 },{ 0x0a, 0x01 },{ 0x02, 0x02 },{ 0x00, 0x07 } },
171 { 0x1b, 0x00, 0x00, 0x07,{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x08, 0x08 },{ 0x04, 0x03 },{ 0x01, 0x00 },{ 0x00, 0x07 } },
172 { 0x0a, 0x00, 0x00, 0x02,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x07, 0x02 },{ 0x03, 0x00 },{ 0x06, 0x06 },{ 0x06, 0x04 },{ 0x04, 0x02 },{ 0x00, 0x07 } },
173 { 0x07, 0x00, 0x00, 0x03,{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x0c, 0x07 },{ 0x05, 0x07 },{ 0x05, 0x00 },{ 0x01, 0x07 } },
174 { 0x1e, 0x00, 0x00, 0x07,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x01, 0x00 },{ 0x06, 0x01 },{ 0x01, 0x00 },{ 0x0f, 0x0f },{ 0x02, 0x03 },{ 0x0f, 0x0f },{ 0x00, 0x03 } },
175 { 0x18, 0x00, 0x00, 0x06,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x0f, 0x0e },{ 0x05, 0x03 },{ 0x02, 0x01 },{ 0x00, 0x03 } },
176 { 0x24, 0x00, 0x00, 0x07,{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x07, 0x01 },{ 0x00, 0x00 },{ 0x0f, 0x0f },{ 0x08, 0x08 },{ 0x02, 0x00 },{ 0x02, 0x03 } },
177 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x01, 0x01 },{ 0x01, 0x00 },{ 0x05, 0x04 },{ 0x00, 0x00 },{ 0x0f, 0x0f },{ 0x0f, 0x03 },{ 0x07, 0x0f },{ 0x00, 0x05 } },
178 { 0x03, 0x00, 0x00, 0x07,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x00, 0x01 },{ 0x0f, 0x01 },{ 0x00, 0x00 },{ 0x0f, 0x0e },{ 0x0c, 0x03 },{ 0x03, 0x0f },{ 0x0f, 0x0c } },
179 { 0x00, 0x00, 0x00, 0x07,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x0a, 0x01 },{ 0x00, 0x00 },{ 0x0b, 0x08 },{ 0x0f, 0x04 },{ 0x00, 0x0f },{ 0x00, 0x05 } },
180
181 { 0x18, 0x00, 0x01, 0x07,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x00, 0x00 },{ 0x0d, 0x00 },{ 0x0f, 0x00 },{ 0x06, 0x00 },{ 0x0a, 0x00 } },
182 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x00, 0x00 },{ 0x0c, 0x00 },{ 0x08, 0x00 },{ 0x0a, 0x00 },{ 0x07, 0x00 } },
183 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x05, 0x00 },{ 0x00, 0x00 },{ 0x0f, 0x00 },{ 0x08, 0x00 },{ 0x05, 0x00 },{ 0x09, 0x00 } },
184 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x00, 0x0f },{ 0x00, 0x08 },{ 0x00, 0x06 },{ 0x00, 0x0d } },
185 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x00, 0x0d },{ 0x00, 0x08 },{ 0x00, 0x04 },{ 0x00, 0x08 } },
186 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x00, 0x0a },{ 0x00, 0x0a },{ 0x00, 0x05 },{ 0x00, 0x05 } }
187 };
188
189 static const opll_patch_t patch_ym2423[opll_patch_max] = {
190 { 0x1b, 0x00, 0x00, 0x07,{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x09, 0x05 },{ 0x04, 0x04 },{ 0x01, 0x00 },{ 0x00, 0x05 } },
191 { 0x12, 0x00, 0x00, 0x04,{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x03, 0x01 },{ 0x01, 0x00 },{ 0x0f, 0x0f },{ 0x03, 0x02 },{ 0x0a, 0x0e },{ 0x00, 0x09 } },
192 { 0x11, 0x00, 0x00, 0x05,{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x02 },{ 0x0f, 0x0f },{ 0x02, 0x02 },{ 0x05, 0x07 },{ 0x00, 0x05 } },
193 { 0x28, 0x00, 0x00, 0x07,{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x03, 0x02 },{ 0x00, 0x00 },{ 0x0f, 0x0f },{ 0x03, 0x02 },{ 0x09, 0x0b },{ 0x00, 0x04 } },
194 { 0x17, 0x00, 0x00, 0x05,{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x02, 0x01 },{ 0x02, 0x00 },{ 0x05, 0x06 },{ 0x01, 0x0f },{ 0x07, 0x00 },{ 0x00, 0x09 } },
195 { 0x18, 0x00, 0x00, 0x06,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x03, 0x00 },{ 0x00, 0x00 },{ 0x0f, 0x0f },{ 0x07, 0x04 },{ 0x05, 0x08 },{ 0x00, 0x05 } },
196 { 0x1c, 0x00, 0x00, 0x07,{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x05, 0x07 },{ 0x01, 0x01 },{ 0x02, 0x02 },{ 0x00, 0x06 } },
197 { 0x1b, 0x00, 0x00, 0x07,{ 0x00, 0x01 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x01, 0x04 },{ 0x00, 0x00 },{ 0x07, 0x03 },{ 0x03, 0x0f },{ 0x00, 0x00 },{ 0x00, 0x06 } },
198 { 0x0d, 0x00, 0x00, 0x03,{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x04, 0x06 },{ 0x02, 0x0f },{ 0x02, 0x00 },{ 0x00, 0x06 } },
199 { 0x10, 0x00, 0x00, 0x05,{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x02 },{ 0x0f, 0x0f },{ 0x03, 0x03 },{ 0x02, 0x00 },{ 0x00, 0x04 } },
200 { 0x1b, 0x00, 0x00, 0x07,{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x0c, 0x09 },{ 0x05, 0x06 },{ 0x0f, 0x0f },{ 0x00, 0x06 } },
201 { 0x1b, 0x00, 0x00, 0x00,{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x09, 0x01 },{ 0x03, 0x00 },{ 0x0f, 0x0f },{ 0x05, 0x03 },{ 0x07, 0x0f },{ 0x00, 0x02 } },
202 { 0x11, 0x00, 0x00, 0x03,{ 0x00, 0x01 },{ 0x01, 0x00 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x00, 0x02 },{ 0x02, 0x00 },{ 0x09, 0x0b },{ 0x04, 0x01 },{ 0x0e, 0x0f },{ 0x00, 0x07 } },
203 { 0x17, 0x00, 0x00, 0x06,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x0d, 0x0e },{ 0x03, 0x01 },{ 0x0b, 0x0e },{ 0x00, 0x0b } },
204 { 0x0d, 0x00, 0x00, 0x05,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x01 },{ 0x01, 0x01 },{ 0x01, 0x06 },{ 0x00, 0x00 },{ 0x0f, 0x0f },{ 0x02, 0x04 },{ 0x02, 0x09 },{ 0x00, 0x09 } },
205
206 { 0x18, 0x00, 0x01, 0x07,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x00, 0x00 },{ 0x0d, 0x00 },{ 0x0f, 0x00 },{ 0x06, 0x00 },{ 0x0a, 0x00 } },
207 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x01, 0x00 },{ 0x00, 0x00 },{ 0x0c, 0x00 },{ 0x08, 0x00 },{ 0x0a, 0x00 },{ 0x07, 0x00 } },
208 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x05, 0x00 },{ 0x00, 0x00 },{ 0x0f, 0x00 },{ 0x08, 0x00 },{ 0x05, 0x00 },{ 0x09, 0x00 } },
209 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x00, 0x0f },{ 0x00, 0x08 },{ 0x00, 0x06 },{ 0x00, 0x0d } },
210 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x00, 0x0d },{ 0x00, 0x08 },{ 0x00, 0x04 },{ 0x00, 0x08 } },
211 { 0x00, 0x00, 0x00, 0x00,{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x00 },{ 0x00, 0x01 },{ 0x00, 0x00 },{ 0x00, 0x0a },{ 0x00, 0x0a },{ 0x00, 0x05 },{ 0x00, 0x05 } }
212 };
213
214 static const uint32_t ch_offset[18] = {
215 1, 2, 0, 1, 2, 3, 4, 5, 3, 4, 5, 6, 7, 8, 6, 7, 8, 0
216 };
217
218 static const uint32_t pg_multi[16] = {
219 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 24, 24, 30, 30
220 };
221
222 static const uint32_t eg_stephi[4][4] = {
223 { 0, 0, 0, 0 },
224 { 1, 0, 0, 0 },
225 { 1, 0, 1, 0 },
226 { 1, 1, 1, 0 }
227 };
228
229 static const uint32_t eg_ksltable[16] = {
230 0, 32, 40, 45, 48, 51, 53, 55, 56, 58, 59, 60, 61, 62, 63, 64
231 };
232
233 static void OPLL_DoIO(opll_t *chip) {
234 /* Write signal check */
235 chip->write_a_en = (chip->write_a & 0x03) == 0x01;
236 chip->write_d_en = (chip->write_d & 0x03) == 0x01;
237 chip->write_a <<= 1;
238 chip->write_d <<= 1;
239 }
240
241 static void OPLL_DoModeWrite(opll_t *chip) {
242 if ((chip->write_mode_address & 0x10) && chip->write_d_en) {
243 uint8_t slot = chip->write_mode_address & 0x01;
244 switch (chip->write_mode_address & 0x0f) {
245 case 0x00:
246 case 0x01:
247 chip->patch.multi[slot] = chip->write_data & 0x0f;
248 chip->patch.ksr[slot] = (chip->write_data >> 4) & 0x01;
249 chip->patch.et[slot] = (chip->write_data >> 5) & 0x01;
250 chip->patch.vib[slot] = (chip->write_data >> 6) & 0x01;
251 chip->patch.am[slot] = (chip->write_data >> 7) & 0x01;
252 break;
253
254 case 0x02:
255 chip->patch.ksl[0] = (chip->write_data >> 6) & 0x03;
256 chip->patch.tl = chip->write_data & 0x3f;
257 break;
258
259 case 0x03:
260 chip->patch.ksl[1] = (chip->write_data >> 6) & 0x03;
261 chip->patch.dc = (chip->write_data >> 4) & 0x01;
262 chip->patch.dm = (chip->write_data >> 3) & 0x01;
263 chip->patch.fb = chip->write_data & 0x07;
264 break;
265
266 case 0x04:
267 case 0x05:
268 chip->patch.dr[slot] = chip->write_data & 0x0f;
269 chip->patch.ar[slot] = (chip->write_data >> 4) & 0x0f;
270 break;
271
272 case 0x06:
273 case 0x07:
274 chip->patch.rr[slot] = chip->write_data & 0x0f;
275 chip->patch.sl[slot] = (chip->write_data >> 4) & 0x0f;
276 break;
277
278 case 0x0e:
279 chip->rhythm = chip->write_data & 0x3f;
280 if (chip->chip_type == opll_type_ds1001) {
281 chip->rhythm |= 0x20;
282 }
283 chip->rm_enable = (chip->rm_enable & 0x7f) | ((chip->rhythm << 2) & 0x80);
284 break;
285
286 case 0x0f:
287 chip->testmode = chip->write_data & 0x0f;
288 break;
289 }
290 }
291 }
292
293 const opll_patch_t* OPLL_GetPatchROM(uint32_t chip_type) {
294 switch (chip_type) {
295 case opll_type_ds1001:
296 return patch_ds1001;
297 break;
298 case opll_type_ymf281:
299 case opll_type_ymf281b:
300 return patch_ymf281;
301 break;
302 case opll_type_ym2423:
303 return patch_ym2423;
304 break;
305 case opll_type_ym2413:
306 case opll_type_ym2413b:
307 case opll_type_ym2420:
308 default:
309 return patch_ym2413;
310 break;
311 }
312 return patch_ym2413;
313 }
314
315 void OPLL_Reset(opll_t *chip, uint32_t chip_type) {
316 uint32_t i;
317 memset(chip, 0, sizeof(opll_t));
318 chip->chip_type = chip_type;
319 if (chip_type == opll_type_ds1001) {
320 /* Rhythm mode is always on */
321 chip->rhythm = 0x20;
322 chip->rm_enable = (int8_t)0x80;
323 }
324 switch (chip_type) {
325 case opll_type_ds1001:
326 chip->patchrom = patch_ds1001;
327 break;
328 case opll_type_ymf281:
329 case opll_type_ymf281b:
330 chip->patchrom = patch_ymf281;
331 break;
332 case opll_type_ym2423:
333 chip->patchrom = patch_ym2423;
334 break;
335 case opll_type_ym2413:
336 case opll_type_ym2413b:
337 case opll_type_ym2420:
338 default:
339 chip->patchrom = patch_ym2413;
340 break;
341 }
342 for (i = 0; i < 18; i++) {
343 chip->eg_state[i] = eg_num_release;
344 chip->eg_level[i] = 0x7f;
345 chip->eg_out = 0x7f;
346 }
347 chip->rm_select = rm_num_tc + 1;
348 }
349
350 static void OPLL_DoRegWrite(opll_t *chip) {
351 /* Address */
352 if (chip->write_a_en) {
353 if ((chip->write_data & 0xc0) == 0x00) {
354 /* FM Write */
355 chip->write_fm_address = 1;
356 chip->address = chip->write_data;
357 } else {
358 chip->write_fm_address = 0;
359 }
360 }
361 /* Data */
362 if (chip->write_fm_address && chip->write_d_en) {
363 chip->data = chip->write_data;
364 }
365
366 /* Update registers */
367 if (chip->write_fm_data && !chip->write_a_en) {
368 if ((chip->address & 0x0f) == chip->cycles && chip->cycles < 16) {
369 uint32_t channel = chip->cycles % 9;
370 switch (chip->address & 0xf0) {
371 case 0x10:
372 if (chip->chip_type == opll_type_ym2420)
373 {
374 chip->fnum[channel] = (chip->fnum[channel] & 0x0f) | ((chip->data & 0x1f) << 4);
375 chip->block[channel] = (chip->data >> 5) & 0x07;
376 }
377 else
378 chip->fnum[channel] = (chip->fnum[channel] & 0x100) | chip->data;
379 break;
380 case 0x20:
381 if (chip->chip_type == opll_type_ym2420)
382 chip->fnum[channel] = (chip->fnum[channel] & 0x1f0) | (chip->data & 0x0f);
383 else
384 {
385 chip->fnum[channel] = (chip->fnum[channel] & 0xff) | ((chip->data & 0x01) << 8);
386 chip->block[channel] = (chip->data >> 1) & 0x07;
387 }
388 chip->kon[channel] = (chip->data >> 4) & 0x01;
389 chip->son[channel] = (chip->data >> 5) & 0x01;
390 break;
391 case 0x30:
392 chip->vol[channel] = chip->data & 0x0f;
393 chip->inst[channel] = (chip->data >> 4) & 0x0f;
394 break;
395 }
396 }
397 }
398
399
400 if (chip->write_a_en) {
401 chip->write_fm_data = 0;
402 }
403 if (chip->write_fm_address && chip->write_d_en) {
404 chip->write_fm_data = 1;
405 }
406 if (chip->write_a_en) {
407 if (((chip->write_data & 0xf0) == 0x00)) {
408 chip->write_mode_address = 0x10 | (chip->write_data & 0x0f);
409 } else {
410 chip->write_mode_address = 0x00;
411 }
412 }
413
414 }
415
416 static void OPLL_PreparePatch1(opll_t *chip) {
417 uint8_t instr;
418 uint32_t mcsel = ((chip->cycles + 1) / 3) & 0x01;
419 uint32_t instr_index;
420 uint32_t ch = ch_offset[chip->cycles];
421 const opll_patch_t *patch;
422 instr = chip->inst[ch];
423 if (instr > 0) {
424 instr_index = opll_patch_1 + instr - 1;
425 }
426 if (chip->rm_select <= rm_num_tc) {
427 instr_index = opll_patch_drum_0 + chip->rm_select;
428 }
429 if (chip->rm_select <= rm_num_tc || instr > 0) {
430 patch = &chip->patchrom[instr_index];
431 } else {
432 patch = &chip->patch;
433 }
434 if (chip->rm_select == rm_num_hh || chip->rm_select == rm_num_tom) {
435 chip->c_tl = chip->inst[ch] << 2;
436 } else if (mcsel == 1) {
437 chip->c_tl = chip->vol[ch] << 2;
438 } else {
439 chip->c_tl = patch->tl;
440 }
441
442 chip->c_adrr[0] = patch->ar[mcsel];
443 chip->c_adrr[1] = patch->dr[mcsel];
444 chip->c_adrr[2] = patch->rr[mcsel];
445 chip->c_et = patch->et[mcsel];
446 chip->c_ksr = patch->ksr[mcsel];
447 chip->c_ksl = patch->ksl[mcsel];
448 chip->c_ksr_freq = (chip->block[ch] << 1) | (chip->fnum[ch] >> 8);
449 chip->c_ksl_freq = (chip->fnum[ch]>>5);
450 chip->c_ksl_block = (chip->block[ch]);
451 }
452
453 static void OPLL_PreparePatch2(opll_t *chip) {
454 uint8_t instr;
455 uint32_t mcsel = ((chip->cycles + 1) / 3) & 0x01;
456 uint32_t instr_index;
457 const opll_patch_t *patch;
458 instr = chip->inst[ch_offset[chip->cycles]];
459 if (instr > 0) {
460 instr_index = opll_patch_1 + instr - 1;
461 }
462 if (chip->rm_select <= rm_num_tc) {
463 instr_index = opll_patch_drum_0 + chip->rm_select;
464 }
465 if (chip->rm_select <= rm_num_tc || instr > 0) {
466 patch = &chip->patchrom[instr_index];
467 } else {
468 patch = &chip->patch;
469 }
470
471 chip->c_fnum = chip->fnum[ch_offset[chip->cycles]];
472 chip->c_block = chip->block[ch_offset[chip->cycles]];
473
474 chip->c_multi = patch->multi[mcsel];
475 chip->c_sl = patch->sl[mcsel];
476 chip->c_fb = patch->fb;
477 chip->c_vib = patch->vib[mcsel];
478 chip->c_am = patch->am[mcsel];
479 chip->c_dc <<= 1;
480 chip->c_dm <<= 1;
481 chip->c_dc |= patch->dc;
482 chip->c_dm |= patch->dm;
483 }
484
485 static void OPLL_PhaseGenerate(opll_t *chip) {
486 uint32_t ismod;
487 uint32_t phase;
488 uint16_t pg_out;
489
490 chip->pg_phase[(chip->cycles + 17) % 18] = chip->pg_phase_next + chip->pg_inc;
491
492 if ((chip->rm_enable & 0x40) && (chip->cycles == 13 || chip->cycles == 14)) {
493 ismod = 0;
494 } else {
495 ismod = ((chip->cycles + 3) / 3) & 1;
496 }
497 phase = chip->pg_phase[chip->cycles];
498 /* KeyOn event check */
499 if ((chip->testmode & 0x04)
500 || (ismod && (chip->eg_dokon & 0x8000)) || (!ismod && (chip->eg_dokon & 0x01))) {
501 chip->pg_phase_next = 0;
502 } else {
503 chip->pg_phase_next = phase;
504 }
505 /* Rhythm mode */
506 if (chip->cycles == 13) {
507 chip->rm_hh_bit2 = (phase >> (2 + 9)) & 1;
508 chip->rm_hh_bit3 = (phase >> (3 + 9)) & 1;
509 chip->rm_hh_bit7 = (phase >> (7 + 9)) & 1;
510 chip->rm_hh_bit8 = (phase >> (8 + 9)) & 1;
511 } else if (chip->cycles == 17 && (chip->rm_enable & 0x80)) {
512 chip->rm_tc_bit3 = (phase >> (3 + 9)) & 1;
513 chip->rm_tc_bit5 = (phase >> (5 + 9)) & 1;
514 }
515 if ((chip->rm_enable & 0x80)) {
516 uint8_t rm_bit;
517 switch (chip->cycles) {
518 case 13:
519 /* HH */
520 rm_bit = (chip->rm_hh_bit2 ^ chip->rm_hh_bit7)
521 | (chip->rm_hh_bit3 ^ chip->rm_tc_bit5)
522 | (chip->rm_tc_bit3 ^ chip->rm_tc_bit5);
523 pg_out = rm_bit << 9;
524 if (rm_bit ^ (chip->rm_noise & 1)) {
525 pg_out |= 0xd0;
526 } else {
527 pg_out |= 0x34;
528 }
529 break;
530 case 16:
531 /* SD */
532 pg_out = (chip->rm_hh_bit8 << 9)
533 | ((chip->rm_hh_bit8 ^ (chip->rm_noise & 1)) << 8);
534 break;
535 case 17:
536 /* TC */
537 rm_bit = (chip->rm_hh_bit2 ^ chip->rm_hh_bit7)
538 | (chip->rm_hh_bit3 ^ chip->rm_tc_bit5)
539 | (chip->rm_tc_bit3 ^ chip->rm_tc_bit5);
540 pg_out = (rm_bit << 9) | 0x100;
541 break;
542 default:
543 pg_out = phase >> 9;
544 }
545 } else {
546 pg_out = phase >> 9;
547 }
548 chip->pg_out = pg_out;
549 }
550
551 static void OPLL_PhaseCalcIncrement(opll_t *chip) {
552 uint32_t freq;
553 uint16_t block;
554 freq = chip->c_fnum << 1;
555 block = chip->c_block;
556 /* Apply vibrato */
557 if (chip->c_vib) {
558 switch (chip->lfo_vib_counter) {
559 case 0:
560 case 4:
561 break;
562 case 1:
563 case 3:
564 freq += freq >> 8;
565 break;
566 case 2:
567 freq += freq >> 7;
568 break;
569 case 5:
570 case 7:
571 freq -= freq >> 8;
572 break;
573 case 6:
574 freq -= freq >> 7;
575 break;
576 }
577 }
578 /* Apply block */
579 freq = (freq << block) >> 1;
580
581 chip->pg_inc = (freq * pg_multi[chip->c_multi]) >> 1;
582 }
583
584 static void OPLL_EnvelopeKSLTL(opll_t *chip) {
585 int32_t ksl;
586
587 ksl = eg_ksltable[chip->c_ksl_freq]-((8-chip->c_ksl_block)<<3);
588 if (ksl < 0) {
589 ksl = 0;
590 }
591
592 ksl <<= 1;
593
594 if (chip->c_ksl) {
595 ksl = ksl >> (3-chip->c_ksl);
596 } else {
597 ksl = 0;
598 }
599
600 chip->eg_ksltl = ksl + (chip->c_tl<<1);
601 }
602
603 static void OPLL_EnvelopeOutput(opll_t *chip) {
604 int32_t level = chip->eg_level[(chip->cycles+17)%18];
605
606 level += chip->eg_ksltl;
607
608 if (chip->c_am) {
609 level += chip->lfo_am_out;
610 }
611
612 if (level >= 128) {
613 level = 127;
614 }
615
616 if (chip->testmode & 0x01) {
617 level = 0;
618 }
619
620 chip->eg_out = level;
621 }
622
623 static void OPLL_EnvelopeGenerate(opll_t *chip) {
624 uint8_t timer_inc;
625 uint8_t timer_bit;
626 uint8_t timer_low;
627 uint8_t rate;
628 uint8_t state_rate;
629 uint8_t ksr;
630 uint8_t sum;
631 uint8_t rate_hi;
632 uint8_t rate_lo;
633 int32_t level;
634 int32_t next_level;
635 uint8_t zero;
636 uint8_t state;
637 uint8_t next_state;
638 int32_t step;
639 int32_t sl;
640 uint32_t mcsel = ((chip->cycles + 1) / 3) & 0x01;
641
642
643 /* EG timer */
644 if ((chip->eg_counter_state & 3) != 3) {
645 timer_inc = 0;
646 } else if (chip->cycles == 0) {
647 timer_inc = 1;
648 } else {
649 timer_inc = chip->eg_timer_carry;
650 }
651 timer_low = chip->eg_timer & 3;
652 timer_bit = chip->eg_timer & 1;
653 timer_bit += timer_inc;
654 chip->eg_timer_carry = timer_bit >> 1;
655 chip->eg_timer = ((timer_bit & 1) << 17) | (chip->eg_timer >> 1);
656 if (chip->testmode & 0x08) {
657 chip->eg_timer &= 0x2ffff;
658 chip->eg_timer |= (chip->write_data << (16 - 2)) & 0x10000;
659 }
660 if (!chip->eg_timer_shift_stop && ((chip->eg_timer >> 16) & 1)) {
661 chip->eg_timer_shift = chip->cycles;
662 }
663 if (chip->cycles == 0 && (chip->eg_counter_state_prev & 1) == 1) {
664 chip->eg_timer_low_lock = timer_low;
665 chip->eg_timer_shift_lock = chip->eg_timer_shift;
666 if (chip->eg_timer_shift_lock > 13)
667 chip->eg_timer_shift_lock = 0;
668
669 chip->eg_timer_shift = 0;
670 }
671 chip->eg_timer_shift_stop |= (chip->eg_timer >> 16) & 1;
672 if (chip->cycles == 0) {
673 chip->eg_timer_shift_stop = 0;
674 }
675 chip->eg_counter_state_prev = chip->eg_counter_state;
676 if (chip->cycles == 17) {
677 chip->eg_counter_state++;
678 }
679
680 level = chip->eg_level[(chip->cycles+16)%18];
681 next_level = level;
682 zero = level == 0;
683 chip->eg_silent = level == 0x7f;
684
685 if (chip->eg_state[(chip->cycles+16)%18] != eg_num_attack && (chip->eg_off&2) && !(chip->eg_dokon&2)) {
686 next_level = 0x7f;
687 }
688
689 if (chip->eg_maxrate && (chip->eg_dokon&2)) {
690 next_level = 0x00;
691 }
692
693
694 state = chip->eg_state[(chip->cycles+16)%18];
695 next_state = eg_num_attack;
696
697 step = 0;
698 sl = chip->eg_sl;
699
700 switch (state) {
701 case eg_num_attack:
702 if (!chip->eg_maxrate && (chip->eg_kon & 2) && !zero) {
703 int32_t shift = (chip->eg_rate_hi < 12) ? chip->eg_inc_lo : (chip->eg_rate_hi - 11 + chip->eg_inc_hi);
704 if (shift > 0) {
705 step = ~level >> (5 - shift);
706 }
707 }
708 if (zero) {
709 next_state = eg_num_decay;
710 } else {
711 next_state = eg_num_attack;
712 }
713 break;
714 case eg_num_decay:
715 if (!(chip->eg_off & 2) && !(chip->eg_dokon & 2) && (level >> 3) != sl)
716 {
717 uint8_t i0 = chip->eg_rate_hi == 15 || (chip->eg_rate_hi == 14 && chip->eg_inc_hi);
718 uint8_t i1 = (chip->eg_rate_hi == 14 && !chip->eg_inc_hi) || (chip->eg_rate_hi == 13 && chip->eg_inc_hi) ||
719 (chip->eg_rate_hi == 13 && !chip->eg_inc_hi && (chip->eg_counter_state_prev & 1))
720 || (chip->eg_rate_hi == 12 && chip->eg_inc_hi && (chip->eg_counter_state_prev & 1))
721 || (chip->eg_rate_hi == 12 && !chip->eg_inc_hi && ((chip->eg_counter_state_prev & 3) == 3))
722 || (chip->eg_inc_lo && ((chip->eg_counter_state_prev & 3) == 3));
723 step = (i0<<1) | i1;
724 }
725 if ((level >> 3) == sl) {
726 next_state = eg_num_sustain;
727 } else {
728 next_state = eg_num_decay;
729 }
730 break;
731 case eg_num_sustain:
732 case eg_num_release:
733 if (!(chip->eg_off & 2) && !(chip->eg_dokon & 2))
734 {
735 uint8_t i0 = chip->eg_rate_hi == 15 || (chip->eg_rate_hi == 14 && chip->eg_inc_hi);
736 uint8_t i1 = (chip->eg_rate_hi == 14 && !chip->eg_inc_hi) || (chip->eg_rate_hi == 13 && chip->eg_inc_hi) ||
737 (chip->eg_rate_hi == 13 && !chip->eg_inc_hi && (chip->eg_counter_state_prev & 1))
738 || (chip->eg_rate_hi == 12 && chip->eg_inc_hi && (chip->eg_counter_state_prev & 1))
739 || (chip->eg_rate_hi == 12 && !chip->eg_inc_hi && ((chip->eg_counter_state_prev & 3) == 3))
740 || (chip->eg_inc_lo && ((chip->eg_counter_state_prev & 3) == 3));
741 step = (i0<<1) | i1;
742 }
743 next_state = state;
744 break;
745 }
746
747 if (!(chip->eg_kon & 2)) {
748 next_state = eg_num_release;
749 }
750 if (chip->eg_dokon & 2) {
751 next_state = eg_num_attack;
752 }
753
754 chip->eg_level[(chip->cycles+16)%18] = next_level+step;
755 chip->eg_state[(chip->cycles+16)%18] = next_state;
756
757 rate_hi = chip->eg_rate >> 2;
758 rate_lo = chip->eg_rate & 3;
759 chip->eg_inc_hi = eg_stephi[rate_lo][chip->eg_timer_low_lock];
760 sum = (chip->eg_timer_shift_lock + rate_hi) & 0x0f;
761 chip->eg_inc_lo = 0;
762 if (rate_hi < 12 && !chip->eg_zerorate) {
763 switch (sum) {
764 case 12:
765 chip->eg_inc_lo = 1;
766 break;
767 case 13:
768 chip->eg_inc_lo = (rate_lo >> 1) & 1;
769 break;
770 case 14:
771 chip->eg_inc_lo = rate_lo & 1;
772 break;
773 }
774 }
775 chip->eg_maxrate = rate_hi == 0x0f;
776
777 chip->eg_rate_hi = rate_hi;
778
779 chip->eg_kon <<= 1;
780 chip->eg_kon |= chip->kon[ch_offset[chip->cycles]];
781 chip->eg_off <<= 1;
782 chip->eg_off |= (chip->eg_level[chip->cycles] >> 2) == 0x1f;
783 switch (chip->rm_select) {
784 case rm_num_bd0:
785 case rm_num_bd1:
786 chip->eg_kon |= (chip->rhythm >> 4) & 1;
787 break;
788 case rm_num_sd:
789 chip->eg_kon |= (chip->rhythm >> 3) & 1;
790 break;
791 case rm_num_tom:
792 chip->eg_kon |= (chip->rhythm >> 2) & 1;
793 break;
794 case rm_num_tc:
795 chip->eg_kon |= (chip->rhythm >> 1) & 1;
796 break;
797 case rm_num_hh:
798 chip->eg_kon |= chip->rhythm & 1;
799 break;
800 }
801
802 /* Calculate rate */
803 rate = 0;
804 chip->eg_dokon <<= 1;
805 state_rate = chip->eg_state[chip->cycles];
806 if (state_rate == eg_num_release && (chip->eg_kon&1) && (chip->eg_off&1)) {
807 state_rate = eg_num_attack;
808 chip->eg_dokon |= 1;
809 }
810 switch (state_rate) {
811 case eg_num_attack:
812 rate = chip->c_adrr[0];
813 break;
814 case eg_num_decay:
815 rate = chip->c_adrr[1];
816 break;
817 case eg_num_sustain:
818 if (!chip->c_et) {
819 rate = chip->c_adrr[2];
820 }
821 break;
822 case eg_num_release:
823 if (chip->son[ch_offset[chip->cycles]]) {
824 rate = 5;
825 } else {
826 rate = chip->c_adrr[2];
827 }
828 break;
829 }
830 if (!(chip->eg_kon&1) && !mcsel && chip->rm_select != rm_num_tom && chip->rm_select != rm_num_hh) {
831 rate = 0;
832 }
833 if ((chip->eg_kon&1) && chip->eg_state[chip->cycles] == eg_num_release && !(chip->eg_off&1)) {
834 rate = 12;
835 }
836 if (!(chip->eg_kon&1) && !chip->son[ch_offset[chip->cycles]] && mcsel == 1 && !chip->c_et) {
837 rate = 7;
838 }
839 chip->eg_zerorate = rate == 0;
840 ksr = chip->c_ksr_freq;
841 if (!chip->c_ksr)
842 ksr >>= 2;
843 chip->eg_rate = (rate << 2) + ksr;
844 if (chip->eg_rate & 0x40) {
845 chip->eg_rate = 0x3c | (ksr & 3);
846 }
847 chip->eg_sl = chip->c_sl;
848 }
849
850 static void OPLL_Channel(opll_t *chip) {
851 int16_t ch_out = chip->ch_out;
852 uint8_t ismod = (chip->cycles / 3) & 1;
853 uint8_t mute_m = ismod || ((chip->rm_enable&0x40) && (chip->cycles+15)%18 >= 12);
854 if (chip->chip_type == opll_type_ds1001) {
855 chip->output_m = ch_out;
856 if (chip->output_m >= 0) {
857 chip->output_m++;
858 }
859 if (mute_m) {
860 chip->output_m = 0;
861 }
862 chip->output_r = 0;
863 return;
864 } else {
865 uint8_t mute_r = 1;
866 /* TODO: This might be incorrect */
867 if ((chip->rm_enable & 0x40)) {
868 switch (chip->cycles) {
869 case 16: /* HH */
870 case 17: /* TOM */
871 case 0: /* BD */
872 case 1: /* SD */
873 case 2: /* TC */
874 case 3: /* HH */
875 case 4: /* TOM */
876 case 5: /* BD */
877 case 9: /* TOM */
878 case 10: /* TOM */
879 mute_r = 0;
880 break;
881 }
882 }
883 if (chip->chip_type == opll_type_ym2413b || chip->chip_type == opll_type_ymf281b) {
884 if (mute_m)
885 chip->output_m = 0;
886 else
887 chip->output_m = ch_out;
888 if (mute_r)
889 chip->output_r = 0;
890 else
891 chip->output_r = ch_out;
892 } else {
893 int16_t sign = ch_out >> 8;
894 if (ch_out >= 0) {
895 ch_out++;
896 sign++;
897 }
898 if (mute_m)
899 chip->output_m = sign;
900 else
901 chip->output_m = ch_out;
902 if (mute_r)
903 chip->output_r = sign;
904 else
905 chip->output_r = ch_out;
906 }
907 }
908 }
909
910 static void OPLL_Operator(opll_t *chip) {
911 uint8_t ismod1, ismod2, ismod3;
912 uint32_t op_mod;
913 uint16_t exp_shift;
914 int16_t output;
915 uint32_t level;
916 uint32_t phase;
917 int16_t routput;
918 if ((chip->rm_enable & 0x80) && (chip->cycles == 15 || chip->cycles == 16)) {
919 ismod1 = 0;
920 } else {
921 ismod1 = ((chip->cycles + 1) / 3) & 1;
922 }
923 if ((chip->rm_enable & 0x40) && (chip->cycles == 13 || chip->cycles == 14)) {
924 ismod2 = 0;
925 } else {
926 ismod2 = ((chip->cycles + 3) / 3) & 1;
927 }
928 if ((chip->rm_enable & 0x40) && (chip->cycles == 16 || chip->cycles == 17)) {
929 ismod3 = 0;
930 } else {
931 ismod3 = (chip->cycles / 3) & 1;
932 }
933
934 op_mod = 0;
935
936 if (ismod3) {
937 op_mod |= chip->op_mod << 1;
938 }
939
940 if (ismod2 && chip->c_fb) {
941 op_mod |= chip->op_fbsum >> (7 - chip->c_fb);
942 }
943
944 exp_shift = chip->op_exp_s;
945 if (chip->eg_silent || ((chip->op_neg&2) && (ismod1 ? (chip->c_dm&4) : (chip->c_dc&4)))) {
946 exp_shift |= 12;
947 }
948
949 output = chip->op_exp_m>>exp_shift;
950 if (!chip->eg_silent && (chip->op_neg&2)) {
951 output = ~output;
952 }
953
954 level = chip->op_logsin+(chip->eg_out<<4);
955 if (level >= 4096) {
956 level = 4095;
957 }
958
959 chip->op_exp_m = exprom[level & 0xff];
960 chip->op_exp_s = level >> 8;
961
962 phase = (op_mod + chip->pg_out) & 0x3ff;
963 if (phase & 0x100) {
964 phase ^= 0xff;
965 }
966 chip->op_logsin = logsinrom[phase & 0xff];
967 chip->op_neg <<= 1;
968 chip->op_neg |= phase >> 9;
969 chip->op_fbsum = (chip->op_fb1[(chip->cycles + 3) % 9] + chip->op_fb2[(chip->cycles + 3) % 9]) >> 1;
970
971 if (ismod1) {
972 chip->op_fb2[chip->cycles%9] = chip->op_fb1[chip->cycles%9];
973 chip->op_fb1[chip->cycles%9] = output;
974 }
975 chip->op_mod = output&0x1ff;
976
977 if (chip->chip_type == opll_type_ds1001) {
978 routput = 0;
979 } else {
980 switch (chip->cycles) {
981 case 2:
982 routput = chip->ch_out_hh;
983 break;
984 case 3:
985 routput = chip->ch_out_tm;
986 break;
987 case 4:
988 routput = chip->ch_out_bd;
989 break;
990 case 8:
991 routput = chip->ch_out_sd;
992 break;
993 case 9:
994 routput = chip->ch_out_tc;
995 break;
996 default:
997 routput = 0; /* TODO: Not quite true */
998 break;
999 }
1000 switch (chip->cycles) {
1001 case 15:
1002 chip->ch_out_hh = output>>3;
1003 break;
1004 case 16:
1005 chip->ch_out_tm = output>>3;
1006 break;
1007 case 17:
1008 chip->ch_out_bd = output>>3;
1009 break;
1010 case 0:
1011 chip->ch_out_sd = output>>3;
1012 break;
1013 case 1:
1014 chip->ch_out_tc = output>>3;
1015 break;
1016 default:
1017 break;
1018 }
1019 }
1020
1021 if (!(chip->rm_enable & 0x80))
1022 routput = 0;
1023
1024 chip->ch_out = ismod1 ? routput : (output>>3);
1025
1026 if (!ismod1) {
1027 chip->output_ch[(chip->cycles+1)%9] = chip->ch_out;
1028 }
1029 }
1030
1031 static void OPLL_DoRhythm(opll_t *chip) {
1032 uint8_t nbit;
1033
1034 /* Noise */
1035 nbit = (chip->rm_noise ^ (chip->rm_noise >> 14)) & 0x01;
1036 nbit |= (chip->rm_noise == 0x00) | ((chip->testmode >> 1) & 0x01);
1037 chip->rm_noise = (nbit << 22) | (chip->rm_noise >> 1);
1038 }
1039
1040 static void OPLL_DoLFO(opll_t *chip) {
1041 uint8_t am_inc = 0;
1042 uint8_t am_bit;
1043
1044 /* Update counter */
1045 if (chip->cycles == 17) {
1046 uint8_t vib_step = ((chip->lfo_counter & 0x3ff) + 1) >> 10;
1047 chip->lfo_am_step = ((chip->lfo_counter & 0x3f) + 1) >> 6;
1048 vib_step |= (chip->testmode >> 3) & 0x01;
1049 chip->lfo_vib_counter += vib_step;
1050 chip->lfo_vib_counter &= 0x07;
1051 chip->lfo_counter++;
1052 }
1053
1054 /* LFO AM */
1055 if ((chip->lfo_am_step || (chip->testmode & 0x08)) && chip->cycles < 9) {
1056 am_inc = chip->lfo_am_dir | (chip->cycles == 0);
1057 }
1058
1059 if (chip->cycles >= 9) {
1060 chip->lfo_am_car = 0;
1061 }
1062
1063 if (chip->cycles == 0) {
1064 if (chip->lfo_am_dir && (chip->lfo_am_counter & 0x7f) == 0) {
1065 chip->lfo_am_dir = 0;
1066 } else if (!chip->lfo_am_dir && (chip->lfo_am_counter & 0x69) == 0x69) {
1067 chip->lfo_am_dir = 1;
1068 }
1069 }
1070
1071 am_bit = chip->lfo_am_counter & 0x01;
1072 am_bit += am_inc + chip->lfo_am_car;
1073 chip->lfo_am_car = am_bit >> 1;
1074 am_bit &= 0x01;
1075 chip->lfo_am_counter = (am_bit << 8) | (chip->lfo_am_counter >> 1);
1076
1077
1078 /* Reset LFO */
1079 if (chip->testmode & 0x02) {
1080 chip->lfo_vib_counter = 0;
1081 chip->lfo_counter = 0;
1082 chip->lfo_am_dir = 0;
1083 chip->lfo_am_counter &= 0xff;
1084 }
1085 }
1086
1087
1088 void OPLL_Clock(opll_t *chip, int32_t *buffer) {
1089 buffer[0] = chip->output_m;
1090 buffer[1] = chip->output_r;
1091 if (chip->cycles == 0) {
1092 chip->lfo_am_out = (chip->lfo_am_counter >> 3) & 0x0f;
1093 }
1094 chip->rm_enable >>= 1;
1095 OPLL_DoModeWrite(chip);
1096 chip->rm_select++;
1097 if (chip->rm_select > rm_num_tc) {
1098 chip->rm_select = rm_num_tc + 1;
1099 }
1100 if (chip->cycles == 11 && (chip->rm_enable & 0x80) == 0x80) {
1101 chip->rm_select = rm_num_bd0;
1102 }
1103 OPLL_PreparePatch1(chip);
1104
1105 OPLL_Channel(chip);
1106
1107 OPLL_PhaseGenerate(chip);
1108
1109 OPLL_Operator(chip);
1110
1111 OPLL_PhaseCalcIncrement(chip);
1112
1113 OPLL_EnvelopeOutput(chip);
1114 OPLL_EnvelopeKSLTL(chip);
1115 OPLL_EnvelopeGenerate(chip);
1116
1117 OPLL_DoLFO(chip);
1118 OPLL_DoRhythm(chip);
1119 OPLL_PreparePatch2(chip);
1120 OPLL_DoRegWrite(chip);
1121 OPLL_DoIO(chip);
1122 chip->cycles = (chip->cycles + 1) % 18;
1123
1124 }
1125
1126
1127 void OPLL_Write(opll_t *chip, uint32_t port, uint8_t data) {
1128 chip->write_data = data;
1129 if (port & 1) {
1130 /* Data */
1131 chip->write_d |= 1;
1132 } else {
1133 /* Address */
1134 chip->write_a |= 1;
1135 }
1136 }