1 /*
2  * Copyright (C) 2013-2018 Alexey Khokholov (Nuke.YKT)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  *  Nuked OPL3 emulator.
19  *  Thanks:
20  *      MAME Development Team(Jarek Burczynski, Tatsuyuki Satoh):
21  *          Feedback and Rhythm part calculation information.
22  *      forums.submarine.org.uk(carbon14, opl3):
23  *          Tremolo and phase generator calculation information.
24  *      OPLx decapsulated(Matthew Gambrell, Olli Niemitalo):
25  *          OPL2 ROMs.
26  *      siliconpr0n.org(John McMaster, digshadow):
27  *          YMF262 and VRC VII decaps and die shots.
28  *
29  * version: 1.8
30  */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include "nukedopl3.h"
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 #if OPL_FAST_WAVEGEN
57 /*
58  * logsin table
59  */
60 
61 static const Bit16u logsinrom[512] = {
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,
94     0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000,
95     0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x002,
96     0x002, 0x002, 0x002, 0x003, 0x003, 0x003, 0x004, 0x004,
97     0x004, 0x005, 0x005, 0x005, 0x006, 0x006, 0x007, 0x007,
98     0x007, 0x008, 0x008, 0x009, 0x009, 0x00a, 0x00a, 0x00b,
99     0x00c, 0x00c, 0x00d, 0x00d, 0x00e, 0x00f, 0x00f, 0x010,
100     0x011, 0x011, 0x012, 0x013, 0x014, 0x014, 0x015, 0x016,
101     0x017, 0x017, 0x018, 0x019, 0x01a, 0x01b, 0x01c, 0x01d,
102     0x01e, 0x01f, 0x020, 0x021, 0x022, 0x023, 0x024, 0x025,
103     0x026, 0x027, 0x028, 0x029, 0x02a, 0x02b, 0x02d, 0x02e,
104     0x02f, 0x030, 0x031, 0x033, 0x034, 0x035, 0x037, 0x038,
105     0x039, 0x03b, 0x03c, 0x03e, 0x03f, 0x040, 0x042, 0x043,
106     0x045, 0x046, 0x048, 0x04a, 0x04b, 0x04d, 0x04e, 0x050,
107     0x052, 0x053, 0x055, 0x057, 0x059, 0x05b, 0x05c, 0x05e,
108     0x060, 0x062, 0x064, 0x066, 0x068, 0x06a, 0x06c, 0x06e,
109     0x070, 0x072, 0x074, 0x076, 0x078, 0x07a, 0x07d, 0x07f,
110     0x081, 0x083, 0x086, 0x088, 0x08a, 0x08d, 0x08f, 0x092,
111     0x094, 0x097, 0x099, 0x09c, 0x09f, 0x0a1, 0x0a4, 0x0a7,
112     0x0a9, 0x0ac, 0x0af, 0x0b2, 0x0b5, 0x0b8, 0x0bb, 0x0be,
113     0x0c1, 0x0c4, 0x0c7, 0x0ca, 0x0cd, 0x0d1, 0x0d4, 0x0d7,
114     0x0db, 0x0de, 0x0e2, 0x0e5, 0x0e9, 0x0ec, 0x0f0, 0x0f4,
115     0x0f8, 0x0fb, 0x0ff, 0x103, 0x107, 0x10b, 0x10f, 0x114,
116     0x118, 0x11c, 0x121, 0x125, 0x129, 0x12e, 0x133, 0x137,
117     0x13c, 0x141, 0x146, 0x14b, 0x150, 0x155, 0x15b, 0x160,
118     0x166, 0x16b, 0x171, 0x177, 0x17c, 0x182, 0x188, 0x18f,
119     0x195, 0x19b, 0x1a2, 0x1a9, 0x1b0, 0x1b7, 0x1be, 0x1c5,
120     0x1cd, 0x1d4, 0x1dc, 0x1e4, 0x1ec, 0x1f5, 0x1fd, 0x206,
121     0x20f, 0x218, 0x222, 0x22c, 0x236, 0x240, 0x24b, 0x256,
122     0x261, 0x26d, 0x279, 0x286, 0x293, 0x2a0, 0x2af, 0x2bd,
123     0x2cd, 0x2dc, 0x2ed, 0x2ff, 0x311, 0x324, 0x339, 0x34e,
124     0x365, 0x37e, 0x398, 0x3b5, 0x3d3, 0x3f5, 0x41a, 0x443,
125     0x471, 0x4a6, 0x4e4, 0x52e, 0x58b, 0x607, 0x6c3, 0x859
126 };
127 
128 /*
129  * exp table
130  */
131 
132 static const Bit16u exprom[256] = {
133     0xff4, 0xfea, 0xfde, 0xfd4, 0xfc8, 0xfbe, 0xfb4, 0xfa8,
134     0xf9e, 0xf92, 0xf88, 0xf7e, 0xf72, 0xf68, 0xf5c, 0xf52,
135     0xf48, 0xf3e, 0xf32, 0xf28, 0xf1e, 0xf14, 0xf08, 0xefe,
136     0xef4, 0xeea, 0xee0, 0xed4, 0xeca, 0xec0, 0xeb6, 0xeac,
137     0xea2, 0xe98, 0xe8e, 0xe84, 0xe7a, 0xe70, 0xe66, 0xe5c,
138     0xe52, 0xe48, 0xe3e, 0xe34, 0xe2a, 0xe20, 0xe16, 0xe0c,
139     0xe04, 0xdfa, 0xdf0, 0xde6, 0xddc, 0xdd2, 0xdca, 0xdc0,
140     0xdb6, 0xdac, 0xda4, 0xd9a, 0xd90, 0xd88, 0xd7e, 0xd74,
141     0xd6a, 0xd62, 0xd58, 0xd50, 0xd46, 0xd3c, 0xd34, 0xd2a,
142     0xd22, 0xd18, 0xd10, 0xd06, 0xcfe, 0xcf4, 0xcec, 0xce2,
143     0xcda, 0xcd0, 0xcc8, 0xcbe, 0xcb6, 0xcae, 0xca4, 0xc9c,
144     0xc92, 0xc8a, 0xc82, 0xc78, 0xc70, 0xc68, 0xc60, 0xc56,
145     0xc4e, 0xc46, 0xc3c, 0xc34, 0xc2c, 0xc24, 0xc1c, 0xc12,
146     0xc0a, 0xc02, 0xbfa, 0xbf2, 0xbea, 0xbe0, 0xbd8, 0xbd0,
147     0xbc8, 0xbc0, 0xbb8, 0xbb0, 0xba8, 0xba0, 0xb98, 0xb90,
148     0xb88, 0xb80, 0xb78, 0xb70, 0xb68, 0xb60, 0xb58, 0xb50,
149     0xb48, 0xb40, 0xb38, 0xb32, 0xb2a, 0xb22, 0xb1a, 0xb12,
150     0xb0a, 0xb02, 0xafc, 0xaf4, 0xaec, 0xae4, 0xade, 0xad6,
151     0xace, 0xac6, 0xac0, 0xab8, 0xab0, 0xaa8, 0xaa2, 0xa9a,
152     0xa92, 0xa8c, 0xa84, 0xa7c, 0xa76, 0xa6e, 0xa68, 0xa60,
153     0xa58, 0xa52, 0xa4a, 0xa44, 0xa3c, 0xa36, 0xa2e, 0xa28,
154     0xa20, 0xa18, 0xa12, 0xa0c, 0xa04, 0x9fe, 0x9f6, 0x9f0,
155     0x9e8, 0x9e2, 0x9da, 0x9d4, 0x9ce, 0x9c6, 0x9c0, 0x9b8,
156     0x9b2, 0x9ac, 0x9a4, 0x99e, 0x998, 0x990, 0x98a, 0x984,
157     0x97c, 0x976, 0x970, 0x96a, 0x962, 0x95c, 0x956, 0x950,
158     0x948, 0x942, 0x93c, 0x936, 0x930, 0x928, 0x922, 0x91c,
159     0x916, 0x910, 0x90a, 0x904, 0x8fc, 0x8f6, 0x8f0, 0x8ea,
160     0x8e4, 0x8de, 0x8d8, 0x8d2, 0x8cc, 0x8c6, 0x8c0, 0x8ba,
161     0x8b4, 0x8ae, 0x8a8, 0x8a2, 0x89c, 0x896, 0x890, 0x88a,
162     0x884, 0x87e, 0x878, 0x872, 0x86c, 0x866, 0x860, 0x85a,
163     0x854, 0x850, 0x84a, 0x844, 0x83e, 0x838, 0x832, 0x82c,
164     0x828, 0x822, 0x81c, 0x816, 0x810, 0x80c, 0x806, 0x800
165 };
166 #else
167 /*
168  * logsin table
169  */
170 
171 static const Bit16u logsinrom[256] = {
172     0x859, 0x6c3, 0x607, 0x58b, 0x52e, 0x4e4, 0x4a6, 0x471,
173     0x443, 0x41a, 0x3f5, 0x3d3, 0x3b5, 0x398, 0x37e, 0x365,
174     0x34e, 0x339, 0x324, 0x311, 0x2ff, 0x2ed, 0x2dc, 0x2cd,
175     0x2bd, 0x2af, 0x2a0, 0x293, 0x286, 0x279, 0x26d, 0x261,
176     0x256, 0x24b, 0x240, 0x236, 0x22c, 0x222, 0x218, 0x20f,
177     0x206, 0x1fd, 0x1f5, 0x1ec, 0x1e4, 0x1dc, 0x1d4, 0x1cd,
178     0x1c5, 0x1be, 0x1b7, 0x1b0, 0x1a9, 0x1a2, 0x19b, 0x195,
179     0x18f, 0x188, 0x182, 0x17c, 0x177, 0x171, 0x16b, 0x166,
180     0x160, 0x15b, 0x155, 0x150, 0x14b, 0x146, 0x141, 0x13c,
181     0x137, 0x133, 0x12e, 0x129, 0x125, 0x121, 0x11c, 0x118,
182     0x114, 0x10f, 0x10b, 0x107, 0x103, 0x0ff, 0x0fb, 0x0f8,
183     0x0f4, 0x0f0, 0x0ec, 0x0e9, 0x0e5, 0x0e2, 0x0de, 0x0db,
184     0x0d7, 0x0d4, 0x0d1, 0x0cd, 0x0ca, 0x0c7, 0x0c4, 0x0c1,
185     0x0be, 0x0bb, 0x0b8, 0x0b5, 0x0b2, 0x0af, 0x0ac, 0x0a9,
186     0x0a7, 0x0a4, 0x0a1, 0x09f, 0x09c, 0x099, 0x097, 0x094,
187     0x092, 0x08f, 0x08d, 0x08a, 0x088, 0x086, 0x083, 0x081,
188     0x07f, 0x07d, 0x07a, 0x078, 0x076, 0x074, 0x072, 0x070,
189     0x06e, 0x06c, 0x06a, 0x068, 0x066, 0x064, 0x062, 0x060,
190     0x05e, 0x05c, 0x05b, 0x059, 0x057, 0x055, 0x053, 0x052,
191     0x050, 0x04e, 0x04d, 0x04b, 0x04a, 0x048, 0x046, 0x045,
192     0x043, 0x042, 0x040, 0x03f, 0x03e, 0x03c, 0x03b, 0x039,
193     0x038, 0x037, 0x035, 0x034, 0x033, 0x031, 0x030, 0x02f,
194     0x02e, 0x02d, 0x02b, 0x02a, 0x029, 0x028, 0x027, 0x026,
195     0x025, 0x024, 0x023, 0x022, 0x021, 0x020, 0x01f, 0x01e,
196     0x01d, 0x01c, 0x01b, 0x01a, 0x019, 0x018, 0x017, 0x017,
197     0x016, 0x015, 0x014, 0x014, 0x013, 0x012, 0x011, 0x011,
198     0x010, 0x00f, 0x00f, 0x00e, 0x00d, 0x00d, 0x00c, 0x00c,
199     0x00b, 0x00a, 0x00a, 0x009, 0x009, 0x008, 0x008, 0x007,
200     0x007, 0x007, 0x006, 0x006, 0x005, 0x005, 0x005, 0x004,
201     0x004, 0x004, 0x003, 0x003, 0x003, 0x002, 0x002, 0x002,
202     0x002, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,
203     0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000
204 };
205 
206 /*
207  * exp table
208  */
209 
210 static const Bit16u exprom[256] = {
211     0x7fa, 0x7f5, 0x7ef, 0x7ea, 0x7e4, 0x7df, 0x7da, 0x7d4,
212     0x7cf, 0x7c9, 0x7c4, 0x7bf, 0x7b9, 0x7b4, 0x7ae, 0x7a9,
213     0x7a4, 0x79f, 0x799, 0x794, 0x78f, 0x78a, 0x784, 0x77f,
214     0x77a, 0x775, 0x770, 0x76a, 0x765, 0x760, 0x75b, 0x756,
215     0x751, 0x74c, 0x747, 0x742, 0x73d, 0x738, 0x733, 0x72e,
216     0x729, 0x724, 0x71f, 0x71a, 0x715, 0x710, 0x70b, 0x706,
217     0x702, 0x6fd, 0x6f8, 0x6f3, 0x6ee, 0x6e9, 0x6e5, 0x6e0,
218     0x6db, 0x6d6, 0x6d2, 0x6cd, 0x6c8, 0x6c4, 0x6bf, 0x6ba,
219     0x6b5, 0x6b1, 0x6ac, 0x6a8, 0x6a3, 0x69e, 0x69a, 0x695,
220     0x691, 0x68c, 0x688, 0x683, 0x67f, 0x67a, 0x676, 0x671,
221     0x66d, 0x668, 0x664, 0x65f, 0x65b, 0x657, 0x652, 0x64e,
222     0x649, 0x645, 0x641, 0x63c, 0x638, 0x634, 0x630, 0x62b,
223     0x627, 0x623, 0x61e, 0x61a, 0x616, 0x612, 0x60e, 0x609,
224     0x605, 0x601, 0x5fd, 0x5f9, 0x5f5, 0x5f0, 0x5ec, 0x5e8,
225     0x5e4, 0x5e0, 0x5dc, 0x5d8, 0x5d4, 0x5d0, 0x5cc, 0x5c8,
226     0x5c4, 0x5c0, 0x5bc, 0x5b8, 0x5b4, 0x5b0, 0x5ac, 0x5a8,
227     0x5a4, 0x5a0, 0x59c, 0x599, 0x595, 0x591, 0x58d, 0x589,
228     0x585, 0x581, 0x57e, 0x57a, 0x576, 0x572, 0x56f, 0x56b,
229     0x567, 0x563, 0x560, 0x55c, 0x558, 0x554, 0x551, 0x54d,
230     0x549, 0x546, 0x542, 0x53e, 0x53b, 0x537, 0x534, 0x530,
231     0x52c, 0x529, 0x525, 0x522, 0x51e, 0x51b, 0x517, 0x514,
232     0x510, 0x50c, 0x509, 0x506, 0x502, 0x4ff, 0x4fb, 0x4f8,
233     0x4f4, 0x4f1, 0x4ed, 0x4ea, 0x4e7, 0x4e3, 0x4e0, 0x4dc,
234     0x4d9, 0x4d6, 0x4d2, 0x4cf, 0x4cc, 0x4c8, 0x4c5, 0x4c2,
235     0x4be, 0x4bb, 0x4b8, 0x4b5, 0x4b1, 0x4ae, 0x4ab, 0x4a8,
236     0x4a4, 0x4a1, 0x49e, 0x49b, 0x498, 0x494, 0x491, 0x48e,
237     0x48b, 0x488, 0x485, 0x482, 0x47e, 0x47b, 0x478, 0x475,
238     0x472, 0x46f, 0x46c, 0x469, 0x466, 0x463, 0x460, 0x45d,
239     0x45a, 0x457, 0x454, 0x451, 0x44e, 0x44b, 0x448, 0x445,
240     0x442, 0x43f, 0x43c, 0x439, 0x436, 0x433, 0x430, 0x42d,
241     0x42a, 0x428, 0x425, 0x422, 0x41f, 0x41c, 0x419, 0x416,
242     0x414, 0x411, 0x40e, 0x40b, 0x408, 0x406, 0x403, 0x400
243 };
244 #endif
245 
246 /*
247  * freq mult table multiplied by 2
248  *
249  * 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 12, 12, 15, 15
250  */
251 
252 static const Bit8u mt[16] = {
253     1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 24, 24, 30, 30
254 };
255 
256 /*
257  * ksl table
258  */
259 
260 static const Bit8u kslrom[16] = {
261     0, 32, 40, 45, 48, 51, 53, 55, 56, 58, 59, 60, 61, 62, 63, 64
262 };
263 
264 static const Bit8u kslshift[4] = {
265     8, 1, 2, 0
266 };
267 
268 /*
269  * envelope generator constants
270  */
271 
272 static const Bit8u eg_incstep[4][4] = {
273     { 0, 0, 0, 0 },
274     { 1, 0, 0, 0 },
275     { 1, 0, 1, 0 },
276     { 1, 1, 1, 0 }
277 };
278 
279 /*
280  * address decoding
281  */
282 
283 static const Bit8s ad_slot[0x20] = {
284     0, 1, 2, 3, 4, 5, -1, -1, 6, 7, 8, 9, 10, 11, -1, -1,
285     12, 13, 14, 15, 16, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
286 };
287 
288 static const Bit8u ch_slot[18] = {
289     0, 1, 2, 6, 7, 8, 12, 13, 14, 18, 19, 20, 24, 25, 26, 30, 31, 32
290 };
291 
292 /*
293  * Pan law table
294  */
295 
296 static const Bit16u panlawtable[] =
297 {
298     65535, 65529, 65514, 65489, 65454, 65409, 65354, 65289,
299     65214, 65129, 65034, 64929, 64814, 64689, 64554, 64410,
300     64255, 64091, 63917, 63733, 63540, 63336, 63123, 62901,
301     62668, 62426, 62175, 61914, 61644, 61364, 61075, 60776,
302     60468, 60151, 59825, 59489, 59145, 58791, 58428, 58057,
303     57676, 57287, 56889, 56482, 56067, 55643, 55211, 54770,
304     54320, 53863, 53397, 52923, 52441, 51951, 51453, 50947,
305     50433, 49912, 49383, 48846, 48302, 47750, 47191,
306     46340, /* Center left */
307     46340, /* Center right */
308     45472, 44885, 44291, 43690, 43083, 42469, 41848, 41221,
309     40588, 39948, 39303, 38651, 37994, 37330, 36661, 35986,
310     35306, 34621, 33930, 33234, 32533, 31827, 31116, 30400,
311     29680, 28955, 28225, 27492, 26754, 26012, 25266, 24516,
312     23762, 23005, 22244, 21480, 20713, 19942, 19169, 18392,
313     17613, 16831, 16046, 15259, 14469, 13678, 12884, 12088,
314     11291, 10492, 9691, 8888, 8085, 7280, 6473, 5666,
315     4858, 4050, 3240, 2431, 1620, 810, 0
316 };
317 
318 /*
319  * Envelope generator
320  */
321 
322 #if !OPL_FAST_WAVEGEN
323 typedef Bit16s(*envelope_sinfunc)(Bit16u phase, Bit16u envelope);
324 
OPL3_EnvelopeCalcExp(Bit32u level)325 static Bit16s OPL3_EnvelopeCalcExp(Bit32u level)
326 {
327     if (level > 0x1fff)
328     {
329         level = 0x1fff;
330     }
331     return (exprom[level & 0xff] << 1) >> (level >> 8);
332 }
333 
OPL3_EnvelopeCalcSin0(Bit16u phase,Bit16u envelope)334 static Bit16s OPL3_EnvelopeCalcSin0(Bit16u phase, Bit16u envelope)
335 {
336     Bit16u out = 0;
337     Bit16u neg = 0;
338     phase &= 0x3ff;
339     if (phase & 0x200)
340     {
341         neg = 0xffff;
342     }
343     if (phase & 0x100)
344     {
345         out = logsinrom[(phase & 0xff) ^ 0xff];
346     }
347     else
348     {
349         out = logsinrom[phase & 0xff];
350     }
351     return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg;
352 }
353 
OPL3_EnvelopeCalcSin1(Bit16u phase,Bit16u envelope)354 static Bit16s OPL3_EnvelopeCalcSin1(Bit16u phase, Bit16u envelope)
355 {
356     Bit16u out = 0;
357     phase &= 0x3ff;
358     if (phase & 0x200)
359     {
360         out = 0x1000;
361     }
362     else if (phase & 0x100)
363     {
364         out = logsinrom[(phase & 0xff) ^ 0xff];
365     }
366     else
367     {
368         out = logsinrom[phase & 0xff];
369     }
370     return OPL3_EnvelopeCalcExp(out + (envelope << 3));
371 }
372 
OPL3_EnvelopeCalcSin2(Bit16u phase,Bit16u envelope)373 static Bit16s OPL3_EnvelopeCalcSin2(Bit16u phase, Bit16u envelope)
374 {
375     Bit16u out = 0;
376     phase &= 0x3ff;
377     if (phase & 0x100)
378     {
379         out = logsinrom[(phase & 0xff) ^ 0xff];
380     }
381     else
382     {
383         out = logsinrom[phase & 0xff];
384     }
385     return OPL3_EnvelopeCalcExp(out + (envelope << 3));
386 }
387 
OPL3_EnvelopeCalcSin3(Bit16u phase,Bit16u envelope)388 static Bit16s OPL3_EnvelopeCalcSin3(Bit16u phase, Bit16u envelope)
389 {
390     Bit16u out = 0;
391     phase &= 0x3ff;
392     if (phase & 0x100)
393     {
394         out = 0x1000;
395     }
396     else
397     {
398         out = logsinrom[phase & 0xff];
399     }
400     return OPL3_EnvelopeCalcExp(out + (envelope << 3));
401 }
402 
OPL3_EnvelopeCalcSin4(Bit16u phase,Bit16u envelope)403 static Bit16s OPL3_EnvelopeCalcSin4(Bit16u phase, Bit16u envelope)
404 {
405     Bit16u out = 0;
406     Bit16u neg = 0;
407     phase &= 0x3ff;
408     if ((phase & 0x300) == 0x100)
409     {
410         neg = 0xffff;
411     }
412     if (phase & 0x200)
413     {
414         out = 0x1000;
415     }
416     else if (phase & 0x80)
417     {
418         out = logsinrom[((phase ^ 0xff) << 1) & 0xff];
419     }
420     else
421     {
422         out = logsinrom[(phase << 1) & 0xff];
423     }
424     return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg;
425 }
426 
OPL3_EnvelopeCalcSin5(Bit16u phase,Bit16u envelope)427 static Bit16s OPL3_EnvelopeCalcSin5(Bit16u phase, Bit16u envelope)
428 {
429     Bit16u out = 0;
430     phase &= 0x3ff;
431     if (phase & 0x200)
432     {
433         out = 0x1000;
434     }
435     else if (phase & 0x80)
436     {
437         out = logsinrom[((phase ^ 0xff) << 1) & 0xff];
438     }
439     else
440     {
441         out = logsinrom[(phase << 1) & 0xff];
442     }
443     return OPL3_EnvelopeCalcExp(out + (envelope << 3));
444 }
445 
OPL3_EnvelopeCalcSin6(Bit16u phase,Bit16u envelope)446 static Bit16s OPL3_EnvelopeCalcSin6(Bit16u phase, Bit16u envelope)
447 {
448     Bit16u neg = 0;
449     phase &= 0x3ff;
450     if (phase & 0x200)
451     {
452         neg = 0xffff;
453     }
454     return OPL3_EnvelopeCalcExp(envelope << 3) ^ neg;
455 }
456 
OPL3_EnvelopeCalcSin7(Bit16u phase,Bit16u envelope)457 static Bit16s OPL3_EnvelopeCalcSin7(Bit16u phase, Bit16u envelope)
458 {
459     Bit16u out = 0;
460     Bit16u neg = 0;
461     phase &= 0x3ff;
462     if (phase & 0x200)
463     {
464         neg = 0xffff;
465         phase = (phase & 0x1ff) ^ 0x1ff;
466     }
467     out = phase << 3;
468     return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg;
469 }
470 
471 static const envelope_sinfunc envelope_sin[8] = {
472     OPL3_EnvelopeCalcSin0,
473     OPL3_EnvelopeCalcSin1,
474     OPL3_EnvelopeCalcSin2,
475     OPL3_EnvelopeCalcSin3,
476     OPL3_EnvelopeCalcSin4,
477     OPL3_EnvelopeCalcSin5,
478     OPL3_EnvelopeCalcSin6,
479     OPL3_EnvelopeCalcSin7
480 };
481 #endif
482 
483 enum envelope_gen_num
484 {
485     envelope_gen_num_attack = 0,
486     envelope_gen_num_decay = 1,
487     envelope_gen_num_sustain = 2,
488     envelope_gen_num_release = 3
489 };
490 
OPL3_EnvelopeUpdateKSL(opl3_slot * slot)491 static void OPL3_EnvelopeUpdateKSL(opl3_slot *slot)
492 {
493     Bit16s ksl = (kslrom[slot->channel->f_num >> 6] << 2)
494                - ((0x08 - slot->channel->block) << 5);
495     if (ksl < 0)
496     {
497         ksl = 0;
498     }
499     slot->eg_ksl = (Bit8u)ksl;
500 }
501 
OPL3_EnvelopeCalc(opl3_slot * slot)502 static void OPL3_EnvelopeCalc(opl3_slot *slot)
503 {
504     Bit8u nonzero;
505     Bit8u rate;
506     Bit8u rate_hi;
507     Bit8u rate_lo;
508     Bit8u reg_rate = 0;
509     Bit8u ks;
510     Bit8u eg_shift, shift;
511     Bit16u eg_rout;
512     Bit16s eg_inc;
513     Bit8u eg_off;
514     Bit8u reset = 0;
515     slot->eg_out = slot->eg_rout + (slot->reg_tl << 2)
516                  + (slot->eg_ksl >> kslshift[slot->reg_ksl]) + *slot->trem;
517 
518 #if OPL_FAST_WAVEGEN
519     if (slot->eg_out > 0x1ff)
520     {
521         slot->eg_out = 0x1ff;
522     }
523     slot->eg_out <<= 3;
524 #endif
525 
526     if (slot->key && slot->eg_gen == envelope_gen_num_release)
527     {
528         reset = 1;
529         reg_rate = slot->reg_ar;
530     }
531     else
532     {
533         switch (slot->eg_gen)
534         {
535         case envelope_gen_num_attack:
536             reg_rate = slot->reg_ar;
537             break;
538         case envelope_gen_num_decay:
539             reg_rate = slot->reg_dr;
540             break;
541         case envelope_gen_num_sustain:
542             if (!slot->reg_type)
543             {
544                 reg_rate = slot->reg_rr;
545             }
546             break;
547         case envelope_gen_num_release:
548             reg_rate = slot->reg_rr;
549             break;
550         }
551     }
552     slot->pg_reset = reset;
553     ks = slot->channel->ksv >> ((slot->reg_ksr ^ 1) << 1);
554     nonzero = (reg_rate != 0);
555     rate = ks + (reg_rate << 2);
556     rate_hi = rate >> 2;
557     rate_lo = rate & 0x03;
558     if (rate_hi & 0x10)
559     {
560         rate_hi = 0x0f;
561     }
562     eg_shift = rate_hi + slot->chip->eg_add;
563     shift = 0;
564     if (nonzero)
565     {
566         if (rate_hi < 12)
567         {
568             if (slot->chip->eg_state)
569             {
570                 switch (eg_shift)
571                 {
572                 case 12:
573                     shift = 1;
574                     break;
575                 case 13:
576                     shift = (rate_lo >> 1) & 0x01;
577                     break;
578                 case 14:
579                     shift = rate_lo & 0x01;
580                     break;
581                 default:
582                     break;
583                 }
584             }
585         }
586         else
587         {
588             shift = (rate_hi & 0x03) + eg_incstep[rate_lo][slot->chip->timer & 0x03];
589             if (shift & 0x04)
590             {
591                 shift = 0x03;
592             }
593             if (!shift)
594             {
595                 shift = slot->chip->eg_state;
596             }
597         }
598     }
599     eg_rout = slot->eg_rout;
600     eg_inc = 0;
601     eg_off = 0;
602     /* Instant attack */
603     if (reset && rate_hi == 0x0f)
604     {
605         eg_rout = 0x00;
606     }
607     /* Envelope off */
608     if ((slot->eg_rout & 0x1f8) == 0x1f8)
609     {
610         eg_off = 1;
611     }
612     if (slot->eg_gen != envelope_gen_num_attack && !reset && eg_off)
613     {
614         eg_rout = 0x1ff;
615     }
616     switch (slot->eg_gen)
617     {
618     case envelope_gen_num_attack:
619         if (!slot->eg_rout)
620         {
621             slot->eg_gen = envelope_gen_num_decay;
622         }
623         else if (slot->key && shift > 0 && rate_hi != 0x0f)
624         {
625             eg_inc = ((~slot->eg_rout) << shift) >> 4;
626         }
627         break;
628     case envelope_gen_num_decay:
629         if ((slot->eg_rout >> 4) == slot->reg_sl)
630         {
631             slot->eg_gen = envelope_gen_num_sustain;
632         }
633         else if (!eg_off && !reset && shift > 0)
634         {
635             eg_inc = 1 << (shift - 1);
636         }
637         break;
638     case envelope_gen_num_sustain:
639     case envelope_gen_num_release:
640         if (!eg_off && !reset && shift > 0)
641         {
642             eg_inc = 1 << (shift - 1);
643         }
644         break;
645     }
646     slot->eg_rout = (eg_rout + eg_inc) & 0x1ff;
647     /* Key off */
648     if (reset)
649     {
650         slot->eg_gen = envelope_gen_num_attack;
651     }
652     if (!slot->key)
653     {
654         slot->eg_gen = envelope_gen_num_release;
655     }
656 }
657 
OPL3_EnvelopeKeyOn(opl3_slot * slot,Bit8u type)658 static void OPL3_EnvelopeKeyOn(opl3_slot *slot, Bit8u type)
659 {
660     slot->key |= type;
661 }
662 
OPL3_EnvelopeKeyOff(opl3_slot * slot,Bit8u type)663 static void OPL3_EnvelopeKeyOff(opl3_slot *slot, Bit8u type)
664 {
665     slot->key &= ~type;
666 }
667 
668 /*
669  * Phase Generator
670  */
671 
OPL3_PhaseGenerate(opl3_slot * slot)672 static void OPL3_PhaseGenerate(opl3_slot *slot)
673 {
674     opl3_chip *chip;
675     Bit16u f_num;
676     Bit32u basefreq;
677     Bit8u rm_xor, n_bit;
678     Bit32u noise;
679     Bit16u phase;
680 
681     chip = slot->chip;
682     f_num = slot->channel->f_num;
683     if (slot->reg_vib)
684     {
685         Bit8s range;
686         Bit8u vibpos;
687 
688         range = (f_num >> 7) & 7;
689         vibpos = slot->chip->vibpos;
690 
691         if (!(vibpos & 3))
692         {
693             range = 0;
694         }
695         else if (vibpos & 1)
696         {
697             range >>= 1;
698         }
699         range >>= slot->chip->vibshift;
700 
701         if (vibpos & 4)
702         {
703             range = -range;
704         }
705         f_num += range;
706     }
707     basefreq = (f_num << slot->channel->block) >> 1;
708     phase = (Bit16u)(slot->pg_phase >> 9);
709     if (slot->pg_reset)
710     {
711         slot->pg_phase = 0;
712     }
713     slot->pg_phase += (basefreq * mt[slot->reg_mult]) >> 1;
714     /* Rhythm mode */
715     noise = chip->noise;
716     slot->pg_phase_out = phase;
717     if (slot->slot_num == 13) /* hh */
718     {
719         chip->rm_hh_bit2 = (phase >> 2) & 1;
720         chip->rm_hh_bit3 = (phase >> 3) & 1;
721         chip->rm_hh_bit7 = (phase >> 7) & 1;
722         chip->rm_hh_bit8 = (phase >> 8) & 1;
723     }
724     if (slot->slot_num == 17 && (chip->rhy & 0x20)) /* tc */
725     {
726         chip->rm_tc_bit3 = (phase >> 3) & 1;
727         chip->rm_tc_bit5 = (phase >> 5) & 1;
728     }
729     if (chip->rhy & 0x20)
730     {
731         rm_xor = (chip->rm_hh_bit2 ^ chip->rm_hh_bit7)
732                | (chip->rm_hh_bit3 ^ chip->rm_tc_bit5)
733                | (chip->rm_tc_bit3 ^ chip->rm_tc_bit5);
734         switch (slot->slot_num)
735         {
736         case 13: /* hh */
737             slot->pg_phase_out = rm_xor << 9;
738             if (rm_xor ^ (noise & 1))
739             {
740                 slot->pg_phase_out |= 0xd0;
741             }
742             else
743             {
744                 slot->pg_phase_out |= 0x34;
745             }
746             break;
747         case 16: /* sd */
748             slot->pg_phase_out = (chip->rm_hh_bit8 << 9)
749                                | ((chip->rm_hh_bit8 ^ (noise & 1)) << 8);
750             break;
751         case 17: /* tc */
752             slot->pg_phase_out = (rm_xor << 9) | 0x80;
753             break;
754         default:
755             break;
756         }
757     }
758     n_bit = ((noise >> 14) ^ noise) & 0x01;
759     chip->noise = (noise >> 1) | (n_bit << 22);
760 }
761 
762 /*
763  * Slot
764  */
765 
OPL3_SlotWrite20(opl3_slot * slot,Bit8u data)766 static void OPL3_SlotWrite20(opl3_slot *slot, Bit8u data)
767 {
768     if ((data >> 7) & 0x01)
769     {
770         slot->trem = &slot->chip->tremolo;
771     }
772     else
773     {
774         slot->trem = (Bit8u*)&slot->chip->zeromod;
775     }
776     slot->reg_vib = (data >> 6) & 0x01;
777     slot->reg_type = (data >> 5) & 0x01;
778     slot->reg_ksr = (data >> 4) & 0x01;
779     slot->reg_mult = data & 0x0f;
780 }
781 
OPL3_SlotWrite40(opl3_slot * slot,Bit8u data)782 static void OPL3_SlotWrite40(opl3_slot *slot, Bit8u data)
783 {
784     slot->reg_ksl = (data >> 6) & 0x03;
785     slot->reg_tl = data & 0x3f;
786     OPL3_EnvelopeUpdateKSL(slot);
787 }
788 
OPL3_SlotWrite60(opl3_slot * slot,Bit8u data)789 static void OPL3_SlotWrite60(opl3_slot *slot, Bit8u data)
790 {
791     slot->reg_ar = (data >> 4) & 0x0f;
792     slot->reg_dr = data & 0x0f;
793 }
794 
OPL3_SlotWrite80(opl3_slot * slot,Bit8u data)795 static void OPL3_SlotWrite80(opl3_slot *slot, Bit8u data)
796 {
797     slot->reg_sl = (data >> 4) & 0x0f;
798     if (slot->reg_sl == 0x0f)
799     {
800         slot->reg_sl = 0x1f;
801     }
802     slot->reg_rr = data & 0x0f;
803 }
804 
OPL3_SlotWriteE0(opl3_slot * slot,Bit8u data)805 static void OPL3_SlotWriteE0(opl3_slot *slot, Bit8u data)
806 {
807     slot->reg_wf = data & 0x07;
808     if (slot->chip->newm == 0x00)
809     {
810         slot->reg_wf &= 0x03;
811     }
812 
813 #if OPL_FAST_WAVEGEN
814     switch (slot->reg_wf)
815     {
816     case 1:
817     case 4:
818     case 5:
819         slot->maskzero = 0x200;
820         break;
821     case 3:
822         slot->maskzero = 0x100;
823         break;
824     default:
825         slot->maskzero = 0;
826         break;
827     }
828 
829     switch (slot->reg_wf)
830     {
831     case 4:
832         slot->signpos = (31-8);  /* sigext of (phase & 0x100) */
833         break;
834     case 0:
835     case 6:
836     case 7:
837         slot->signpos = (31-9);  /* sigext of (phase & 0x200) */
838         break;
839     default:
840         slot->signpos = (31-16);  /* set "neg" to zero */
841         break;
842     }
843 
844     switch (slot->reg_wf)
845     {
846     case 4:
847     case 5:
848         slot->phaseshift = 1;
849         break;
850     case 6:
851         slot->phaseshift = 16; /* set phase to zero and flag for non-sin wave */
852         break;
853     case 7:
854         slot->phaseshift = 32; /* no shift (work by mod 32), but flag for non-sin wave */
855         break;
856     default:
857         slot->phaseshift = 0;
858         break;
859     }
860 #endif
861 }
862 
863 #if OPL_FAST_WAVEGEN
OPL3_SlotGenerate(opl3_slot * slot)864 static void OPL3_SlotGenerate(opl3_slot *slot)
865 {
866     Bit16u phase = slot->pg_phase_out + *slot->mod;
867     Bit32u neg, level;
868     Bit8u  phaseshift;
869 
870     /* Fast paths for mute segments */
871     if (phase & slot->maskzero)
872     {
873         slot->out = 0;
874         return;
875     }
876 
877     neg = (Bit32s)((Bit32u)phase << slot->signpos) >> 31;
878     phaseshift = slot->phaseshift;
879     level = slot->eg_out;
880 
881     phase <<= phaseshift;
882     if (phaseshift <= 1)
883     {
884         level += logsinrom[phase & 0x1ff];
885     }
886     else
887     {
888         level += ((phase ^ neg) & 0x3ff) << 3;
889     }
890     slot->out = exprom[level & 0xff] >> (level >> 8) ^ neg;
891 }
892 #else
OPL3_SlotGenerate(opl3_slot * slot)893 static void OPL3_SlotGenerate(opl3_slot *slot)
894 {
895     slot->out = envelope_sin[slot->reg_wf](slot->pg_phase_out + *slot->mod, slot->eg_out);
896 }
897 #endif
898 
OPL3_SlotCalcFB(opl3_slot * slot)899 static void OPL3_SlotCalcFB(opl3_slot *slot)
900 {
901     if (slot->channel->fb != 0x00)
902     {
903         slot->fbmod = (slot->prout + slot->out) >> (0x09 - slot->channel->fb);
904     }
905     else
906     {
907         slot->fbmod = 0;
908     }
909     slot->prout = slot->out;
910 }
911 
912 /*
913  * Channel
914  */
915 
916 static void OPL3_ChannelSetupAlg(opl3_channel *channel);
917 
OPL3_ChannelUpdateRhythm(opl3_chip * chip,Bit8u data)918 static void OPL3_ChannelUpdateRhythm(opl3_chip *chip, Bit8u data)
919 {
920     opl3_channel *channel6;
921     opl3_channel *channel7;
922     opl3_channel *channel8;
923     Bit8u chnum;
924 
925     chip->rhy = data & 0x3f;
926     if (chip->rhy & 0x20)
927     {
928         channel6 = &chip->channel[6];
929         channel7 = &chip->channel[7];
930         channel8 = &chip->channel[8];
931         channel6->out[0] = &channel6->slotz[1]->out;
932         channel6->out[1] = &channel6->slotz[1]->out;
933         channel6->out[2] = &chip->zeromod;
934         channel6->out[3] = &chip->zeromod;
935         channel7->out[0] = &channel7->slotz[0]->out;
936         channel7->out[1] = &channel7->slotz[0]->out;
937         channel7->out[2] = &channel7->slotz[1]->out;
938         channel7->out[3] = &channel7->slotz[1]->out;
939         channel8->out[0] = &channel8->slotz[0]->out;
940         channel8->out[1] = &channel8->slotz[0]->out;
941         channel8->out[2] = &channel8->slotz[1]->out;
942         channel8->out[3] = &channel8->slotz[1]->out;
943         for (chnum = 6; chnum < 9; chnum++)
944         {
945             chip->channel[chnum].chtype = ch_drum;
946         }
947         OPL3_ChannelSetupAlg(channel6);
948         OPL3_ChannelSetupAlg(channel7);
949         OPL3_ChannelSetupAlg(channel8);
950         /* hh */
951         if (chip->rhy & 0x01)
952         {
953             OPL3_EnvelopeKeyOn(channel7->slotz[0], egk_drum);
954         }
955         else
956         {
957             OPL3_EnvelopeKeyOff(channel7->slotz[0], egk_drum);
958         }
959         /* tc */
960         if (chip->rhy & 0x02)
961         {
962             OPL3_EnvelopeKeyOn(channel8->slotz[1], egk_drum);
963         }
964         else
965         {
966             OPL3_EnvelopeKeyOff(channel8->slotz[1], egk_drum);
967         }
968         /* tom */
969         if (chip->rhy & 0x04)
970         {
971             OPL3_EnvelopeKeyOn(channel8->slotz[0], egk_drum);
972         }
973         else
974         {
975             OPL3_EnvelopeKeyOff(channel8->slotz[0], egk_drum);
976         }
977         /* sd */
978         if (chip->rhy & 0x08)
979         {
980             OPL3_EnvelopeKeyOn(channel7->slotz[1], egk_drum);
981         }
982         else
983         {
984             OPL3_EnvelopeKeyOff(channel7->slotz[1], egk_drum);
985         }
986         /* bd */
987         if (chip->rhy & 0x10)
988         {
989             OPL3_EnvelopeKeyOn(channel6->slotz[0], egk_drum);
990             OPL3_EnvelopeKeyOn(channel6->slotz[1], egk_drum);
991         }
992         else
993         {
994             OPL3_EnvelopeKeyOff(channel6->slotz[0], egk_drum);
995             OPL3_EnvelopeKeyOff(channel6->slotz[1], egk_drum);
996         }
997     }
998     else
999     {
1000         for (chnum = 6; chnum < 9; chnum++)
1001         {
1002             chip->channel[chnum].chtype = ch_2op;
1003             OPL3_ChannelSetupAlg(&chip->channel[chnum]);
1004             OPL3_EnvelopeKeyOff(chip->channel[chnum].slotz[0], egk_drum);
1005             OPL3_EnvelopeKeyOff(chip->channel[chnum].slotz[1], egk_drum);
1006         }
1007     }
1008 }
1009 
OPL3_ChannelWriteA0(opl3_channel * channel,Bit8u data)1010 static void OPL3_ChannelWriteA0(opl3_channel *channel, Bit8u data)
1011 {
1012     if (channel->chip->newm && channel->chtype == ch_4op2)
1013     {
1014         return;
1015     }
1016     channel->f_num = (channel->f_num & 0x300) | data;
1017     channel->ksv = (channel->block << 1)
1018                  | ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01);
1019     OPL3_EnvelopeUpdateKSL(channel->slotz[0]);
1020     OPL3_EnvelopeUpdateKSL(channel->slotz[1]);
1021     if (channel->chip->newm && channel->chtype == ch_4op)
1022     {
1023         channel->pair->f_num = channel->f_num;
1024         channel->pair->ksv = channel->ksv;
1025         OPL3_EnvelopeUpdateKSL(channel->pair->slotz[0]);
1026         OPL3_EnvelopeUpdateKSL(channel->pair->slotz[1]);
1027     }
1028 }
1029 
OPL3_ChannelWriteB0(opl3_channel * channel,Bit8u data)1030 static void OPL3_ChannelWriteB0(opl3_channel *channel, Bit8u data)
1031 {
1032     if (channel->chip->newm && channel->chtype == ch_4op2)
1033     {
1034         return;
1035     }
1036     channel->f_num = (channel->f_num & 0xff) | ((data & 0x03) << 8);
1037     channel->block = (data >> 2) & 0x07;
1038     channel->ksv = (channel->block << 1)
1039                  | ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01);
1040     OPL3_EnvelopeUpdateKSL(channel->slotz[0]);
1041     OPL3_EnvelopeUpdateKSL(channel->slotz[1]);
1042     if (channel->chip->newm && channel->chtype == ch_4op)
1043     {
1044         channel->pair->f_num = channel->f_num;
1045         channel->pair->block = channel->block;
1046         channel->pair->ksv = channel->ksv;
1047         OPL3_EnvelopeUpdateKSL(channel->pair->slotz[0]);
1048         OPL3_EnvelopeUpdateKSL(channel->pair->slotz[1]);
1049     }
1050 }
1051 
OPL3_ChannelSetupAlg(opl3_channel * channel)1052 static void OPL3_ChannelSetupAlg(opl3_channel *channel)
1053 {
1054     if (channel->chtype == ch_drum)
1055     {
1056         if (channel->ch_num == 7 || channel->ch_num == 8)
1057         {
1058             channel->slotz[0]->mod = &channel->chip->zeromod;
1059             channel->slotz[1]->mod = &channel->chip->zeromod;
1060             return;
1061         }
1062         switch (channel->alg & 0x01)
1063         {
1064         case 0x00:
1065             channel->slotz[0]->mod = &channel->slotz[0]->fbmod;
1066             channel->slotz[1]->mod = &channel->slotz[0]->out;
1067             break;
1068         case 0x01:
1069             channel->slotz[0]->mod = &channel->slotz[0]->fbmod;
1070             channel->slotz[1]->mod = &channel->chip->zeromod;
1071             break;
1072         }
1073         return;
1074     }
1075     if (channel->alg & 0x08)
1076     {
1077         return;
1078     }
1079     if (channel->alg & 0x04)
1080     {
1081         channel->pair->out[0] = &channel->chip->zeromod;
1082         channel->pair->out[1] = &channel->chip->zeromod;
1083         channel->pair->out[2] = &channel->chip->zeromod;
1084         channel->pair->out[3] = &channel->chip->zeromod;
1085         switch (channel->alg & 0x03)
1086         {
1087         case 0x00:
1088             channel->pair->slotz[0]->mod = &channel->pair->slotz[0]->fbmod;
1089             channel->pair->slotz[1]->mod = &channel->pair->slotz[0]->out;
1090             channel->slotz[0]->mod = &channel->pair->slotz[1]->out;
1091             channel->slotz[1]->mod = &channel->slotz[0]->out;
1092             channel->out[0] = &channel->slotz[1]->out;
1093             channel->out[1] = &channel->chip->zeromod;
1094             channel->out[2] = &channel->chip->zeromod;
1095             channel->out[3] = &channel->chip->zeromod;
1096             break;
1097         case 0x01:
1098             channel->pair->slotz[0]->mod = &channel->pair->slotz[0]->fbmod;
1099             channel->pair->slotz[1]->mod = &channel->pair->slotz[0]->out;
1100             channel->slotz[0]->mod = &channel->chip->zeromod;
1101             channel->slotz[1]->mod = &channel->slotz[0]->out;
1102             channel->out[0] = &channel->pair->slotz[1]->out;
1103             channel->out[1] = &channel->slotz[1]->out;
1104             channel->out[2] = &channel->chip->zeromod;
1105             channel->out[3] = &channel->chip->zeromod;
1106             break;
1107         case 0x02:
1108             channel->pair->slotz[0]->mod = &channel->pair->slotz[0]->fbmod;
1109             channel->pair->slotz[1]->mod = &channel->chip->zeromod;
1110             channel->slotz[0]->mod = &channel->pair->slotz[1]->out;
1111             channel->slotz[1]->mod = &channel->slotz[0]->out;
1112             channel->out[0] = &channel->pair->slotz[0]->out;
1113             channel->out[1] = &channel->slotz[1]->out;
1114             channel->out[2] = &channel->chip->zeromod;
1115             channel->out[3] = &channel->chip->zeromod;
1116             break;
1117         case 0x03:
1118             channel->pair->slotz[0]->mod = &channel->pair->slotz[0]->fbmod;
1119             channel->pair->slotz[1]->mod = &channel->chip->zeromod;
1120             channel->slotz[0]->mod = &channel->pair->slotz[1]->out;
1121             channel->slotz[1]->mod = &channel->chip->zeromod;
1122             channel->out[0] = &channel->pair->slotz[0]->out;
1123             channel->out[1] = &channel->slotz[0]->out;
1124             channel->out[2] = &channel->slotz[1]->out;
1125             channel->out[3] = &channel->chip->zeromod;
1126             break;
1127         }
1128     }
1129     else
1130     {
1131         switch (channel->alg & 0x01)
1132         {
1133         case 0x00:
1134             channel->slotz[0]->mod = &channel->slotz[0]->fbmod;
1135             channel->slotz[1]->mod = &channel->slotz[0]->out;
1136             channel->out[0] = &channel->slotz[1]->out;
1137             channel->out[1] = &channel->chip->zeromod;
1138             channel->out[2] = &channel->chip->zeromod;
1139             channel->out[3] = &channel->chip->zeromod;
1140             break;
1141         case 0x01:
1142             channel->slotz[0]->mod = &channel->slotz[0]->fbmod;
1143             channel->slotz[1]->mod = &channel->chip->zeromod;
1144             channel->out[0] = &channel->slotz[0]->out;
1145             channel->out[1] = &channel->slotz[1]->out;
1146             channel->out[2] = &channel->chip->zeromod;
1147             channel->out[3] = &channel->chip->zeromod;
1148             break;
1149         }
1150     }
1151 }
1152 
OPL3_ChannelWriteC0(opl3_channel * channel,Bit8u data)1153 static void OPL3_ChannelWriteC0(opl3_channel *channel, Bit8u data)
1154 {
1155     channel->fb = (data & 0x0e) >> 1;
1156     channel->con = data & 0x01;
1157     channel->alg = channel->con;
1158     if (channel->chip->newm)
1159     {
1160         if (channel->chtype == ch_4op)
1161         {
1162             channel->pair->alg = 0x04 | (channel->con << 1) | (channel->pair->con);
1163             channel->alg = 0x08;
1164             OPL3_ChannelSetupAlg(channel->pair);
1165         }
1166         else if (channel->chtype == ch_4op2)
1167         {
1168             channel->alg = 0x04 | (channel->pair->con << 1) | (channel->con);
1169             channel->pair->alg = 0x08;
1170             OPL3_ChannelSetupAlg(channel);
1171         }
1172         else
1173         {
1174             OPL3_ChannelSetupAlg(channel);
1175         }
1176     }
1177     else
1178     {
1179         OPL3_ChannelSetupAlg(channel);
1180     }
1181     if (channel->chip->newm)
1182     {
1183         channel->cha = ((data >> 4) & 0x01) ? ~0 : 0;
1184         channel->chb = ((data >> 5) & 0x01) ? ~0 : 0;
1185     }
1186     else
1187     {
1188         channel->cha = channel->chb = (Bit16u)~0;
1189     }
1190 }
1191 
OPL3_ChannelKeyOn(opl3_channel * channel)1192 static void OPL3_ChannelKeyOn(opl3_channel *channel)
1193 {
1194     if (channel->chip->newm)
1195     {
1196         if (channel->chtype == ch_4op)
1197         {
1198             OPL3_EnvelopeKeyOn(channel->slotz[0], egk_norm);
1199             OPL3_EnvelopeKeyOn(channel->slotz[1], egk_norm);
1200             OPL3_EnvelopeKeyOn(channel->pair->slotz[0], egk_norm);
1201             OPL3_EnvelopeKeyOn(channel->pair->slotz[1], egk_norm);
1202         }
1203         else if (channel->chtype == ch_2op || channel->chtype == ch_drum)
1204         {
1205             OPL3_EnvelopeKeyOn(channel->slotz[0], egk_norm);
1206             OPL3_EnvelopeKeyOn(channel->slotz[1], egk_norm);
1207         }
1208     }
1209     else
1210     {
1211         OPL3_EnvelopeKeyOn(channel->slotz[0], egk_norm);
1212         OPL3_EnvelopeKeyOn(channel->slotz[1], egk_norm);
1213     }
1214 }
1215 
OPL3_ChannelKeyOff(opl3_channel * channel)1216 static void OPL3_ChannelKeyOff(opl3_channel *channel)
1217 {
1218     if (channel->chip->newm)
1219     {
1220         if (channel->chtype == ch_4op)
1221         {
1222             OPL3_EnvelopeKeyOff(channel->slotz[0], egk_norm);
1223             OPL3_EnvelopeKeyOff(channel->slotz[1], egk_norm);
1224             OPL3_EnvelopeKeyOff(channel->pair->slotz[0], egk_norm);
1225             OPL3_EnvelopeKeyOff(channel->pair->slotz[1], egk_norm);
1226         }
1227         else if (channel->chtype == ch_2op || channel->chtype == ch_drum)
1228         {
1229             OPL3_EnvelopeKeyOff(channel->slotz[0], egk_norm);
1230             OPL3_EnvelopeKeyOff(channel->slotz[1], egk_norm);
1231         }
1232     }
1233     else
1234     {
1235         OPL3_EnvelopeKeyOff(channel->slotz[0], egk_norm);
1236         OPL3_EnvelopeKeyOff(channel->slotz[1], egk_norm);
1237     }
1238 }
1239 
OPL3_ChannelSet4Op(opl3_chip * chip,Bit8u data)1240 static void OPL3_ChannelSet4Op(opl3_chip *chip, Bit8u data)
1241 {
1242     Bit8u bit;
1243     Bit8u chnum;
1244     for (bit = 0; bit < 6; bit++)
1245     {
1246         chnum = bit;
1247         if (bit >= 3)
1248         {
1249             chnum += 9 - 3;
1250         }
1251         if ((data >> bit) & 0x01)
1252         {
1253             chip->channel[chnum].chtype = ch_4op;
1254             chip->channel[chnum + 3].chtype = ch_4op2;
1255         }
1256         else
1257         {
1258             chip->channel[chnum].chtype = ch_2op;
1259             chip->channel[chnum + 3].chtype = ch_2op;
1260         }
1261     }
1262 }
1263 
OPL3_ClipSample(Bit32s sample)1264 static Bit16s OPL3_ClipSample(Bit32s sample)
1265 {
1266     if (sample > 32767)
1267     {
1268         sample = 32767;
1269     }
1270     else if (sample < -32768)
1271     {
1272         sample = -32768;
1273     }
1274     return (Bit16s)sample;
1275 }
1276 
OPL3_Generate(opl3_chip * chip,Bit16s * buf)1277 void OPL3_Generate(opl3_chip *chip, Bit16s *buf)
1278 {
1279     Bit8u ii;
1280     Bit8u jj;
1281     Bit16s accm;
1282     Bit8u shift = 0;
1283 
1284     buf[1] = OPL3_ClipSample(chip->mixbuff[1]);
1285 
1286     for (ii = 0; ii < 15; ii++)
1287     {
1288         OPL3_SlotCalcFB(&chip->slot[ii]);
1289         OPL3_EnvelopeCalc(&chip->slot[ii]);
1290         OPL3_PhaseGenerate(&chip->slot[ii]);
1291         OPL3_SlotGenerate(&chip->slot[ii]);
1292     }
1293 
1294     chip->mixbuff[0] = 0;
1295     for (ii = 0; ii < 18; ii++)
1296     {
1297         accm = 0;
1298         for (jj = 0; jj < 4; jj++)
1299         {
1300             accm += *chip->channel[ii].out[jj];
1301         }
1302         chip->mixbuff[0] += (Bit16s)((accm * chip->channel[ii].chl / 65535) & chip->channel[ii].cha);
1303     }
1304 
1305     for (ii = 15; ii < 18; ii++)
1306     {
1307         OPL3_SlotCalcFB(&chip->slot[ii]);
1308         OPL3_EnvelopeCalc(&chip->slot[ii]);
1309         OPL3_PhaseGenerate(&chip->slot[ii]);
1310         OPL3_SlotGenerate(&chip->slot[ii]);
1311     }
1312 
1313     buf[0] = OPL3_ClipSample(chip->mixbuff[0]);
1314 
1315     for (ii = 18; ii < 33; ii++)
1316     {
1317         OPL3_SlotCalcFB(&chip->slot[ii]);
1318         OPL3_EnvelopeCalc(&chip->slot[ii]);
1319         OPL3_PhaseGenerate(&chip->slot[ii]);
1320         OPL3_SlotGenerate(&chip->slot[ii]);
1321     }
1322 
1323     chip->mixbuff[1] = 0;
1324     for (ii = 0; ii < 18; ii++)
1325     {
1326         accm = 0;
1327         for (jj = 0; jj < 4; jj++)
1328         {
1329             accm += *chip->channel[ii].out[jj];
1330         }
1331         chip->mixbuff[1] += (Bit16s)((accm * chip->channel[ii].chr / 65535) & chip->channel[ii].chb);
1332     }
1333 
1334     for (ii = 33; ii < 36; ii++)
1335     {
1336         OPL3_SlotCalcFB(&chip->slot[ii]);
1337         OPL3_EnvelopeCalc(&chip->slot[ii]);
1338         OPL3_PhaseGenerate(&chip->slot[ii]);
1339         OPL3_SlotGenerate(&chip->slot[ii]);
1340     }
1341 
1342     if ((chip->timer & 0x3f) == 0x3f)
1343     {
1344         chip->tremolopos = (chip->tremolopos + 1) % 210;
1345     }
1346     if (chip->tremolopos < 105)
1347     {
1348         chip->tremolo = chip->tremolopos >> chip->tremoloshift;
1349     }
1350     else
1351     {
1352         chip->tremolo = (210 - chip->tremolopos) >> chip->tremoloshift;
1353     }
1354 
1355     if ((chip->timer & 0x3ff) == 0x3ff)
1356     {
1357         chip->vibpos = (chip->vibpos + 1) & 7;
1358     }
1359 
1360     chip->timer++;
1361 
1362     chip->eg_add = 0;
1363     if (chip->eg_timer)
1364     {
1365         while (shift < 36 && ((chip->eg_timer >> shift) & 1) == 0)
1366         {
1367             shift++;
1368         }
1369         if (shift > 12)
1370         {
1371             chip->eg_add = 0;
1372         }
1373         else
1374         {
1375             chip->eg_add = shift + 1;
1376         }
1377     }
1378 
1379     if (chip->eg_timerrem || chip->eg_state)
1380     {
1381         if (chip->eg_timer == 0xfffffffffULL)
1382         {
1383             chip->eg_timer = 0;
1384             chip->eg_timerrem = 1;
1385         }
1386         else
1387         {
1388             chip->eg_timer++;
1389             chip->eg_timerrem = 0;
1390         }
1391     }
1392 
1393     chip->eg_state ^= 1;
1394 
1395     while (chip->writebuf[chip->writebuf_cur].time <= chip->writebuf_samplecnt)
1396     {
1397         if (!(chip->writebuf[chip->writebuf_cur].reg & 0x200))
1398         {
1399             break;
1400         }
1401         chip->writebuf[chip->writebuf_cur].reg &= 0x1ff;
1402         OPL3_WriteReg(chip, chip->writebuf[chip->writebuf_cur].reg,
1403                       chip->writebuf[chip->writebuf_cur].data);
1404         chip->writebuf_cur = (chip->writebuf_cur + 1) % OPL_WRITEBUF_SIZE;
1405     }
1406     chip->writebuf_samplecnt++;
1407 }
1408 
OPL3_GenerateResampled(opl3_chip * chip,Bit16s * buf)1409 void OPL3_GenerateResampled(opl3_chip *chip, Bit16s *buf)
1410 {
1411     while (chip->samplecnt >= chip->rateratio)
1412     {
1413         chip->oldsamples[0] = chip->samples[0];
1414         chip->oldsamples[1] = chip->samples[1];
1415         OPL3_Generate(chip, chip->samples);
1416         chip->samplecnt -= chip->rateratio;
1417     }
1418     buf[0] = (Bit16s)((chip->oldsamples[0] * (chip->rateratio - chip->samplecnt)
1419                      + chip->samples[0] * chip->samplecnt) / chip->rateratio);
1420     buf[1] = (Bit16s)((chip->oldsamples[1] * (chip->rateratio - chip->samplecnt)
1421                      + chip->samples[1] * chip->samplecnt) / chip->rateratio);
1422     chip->samplecnt += 1 << RSM_FRAC;
1423 }
1424 
OPL3_Reset(opl3_chip * chip,Bit32u samplerate)1425 void OPL3_Reset(opl3_chip *chip, Bit32u samplerate)
1426 {
1427     Bit8u slotnum;
1428     Bit8u channum;
1429 
1430     memset(chip, 0, sizeof(opl3_chip));
1431     for (slotnum = 0; slotnum < 36; slotnum++)
1432     {
1433         chip->slot[slotnum].chip = chip;
1434         chip->slot[slotnum].mod = &chip->zeromod;
1435         chip->slot[slotnum].eg_rout = 0x1ff;
1436 #if OPL_FAST_WAVEGEN
1437         chip->slot[slotnum].eg_out = 0x1ff << 3;
1438 #else
1439         chip->slot[slotnum].eg_out = 0x1ff;
1440 #endif
1441         chip->slot[slotnum].eg_gen = envelope_gen_num_release;
1442         chip->slot[slotnum].trem = (Bit8u*)&chip->zeromod;
1443         chip->slot[slotnum].slot_num = slotnum;
1444 #if OPL_FAST_WAVEGEN
1445         chip->slot[slotnum].signpos = (31-9);  /* for wf=0 need use sigext of (phase & 0x200) */
1446 #endif
1447     }
1448     for (channum = 0; channum < 18; channum++)
1449     {
1450         chip->channel[channum].slotz[0] = &chip->slot[ch_slot[channum]];
1451         chip->channel[channum].slotz[1] = &chip->slot[ch_slot[channum] + 3];
1452         chip->slot[ch_slot[channum]].channel = &chip->channel[channum];
1453         chip->slot[ch_slot[channum] + 3].channel = &chip->channel[channum];
1454         if ((channum % 9) < 3)
1455         {
1456             chip->channel[channum].pair = &chip->channel[channum + 3];
1457         }
1458         else if ((channum % 9) < 6)
1459         {
1460             chip->channel[channum].pair = &chip->channel[channum - 3];
1461         }
1462         chip->channel[channum].chip = chip;
1463         chip->channel[channum].out[0] = &chip->zeromod;
1464         chip->channel[channum].out[1] = &chip->zeromod;
1465         chip->channel[channum].out[2] = &chip->zeromod;
1466         chip->channel[channum].out[3] = &chip->zeromod;
1467         chip->channel[channum].chtype = ch_2op;
1468         chip->channel[channum].cha = 0xffff;
1469         chip->channel[channum].chb = 0xffff;
1470         chip->channel[channum].chl = 46340;
1471         chip->channel[channum].chr = 46340;
1472         chip->channel[channum].ch_num = channum;
1473         OPL3_ChannelSetupAlg(&chip->channel[channum]);
1474     }
1475     chip->noise = 1;
1476     chip->rateratio = (samplerate << RSM_FRAC) / 49716;
1477     chip->tremoloshift = 4;
1478     chip->vibshift = 1;
1479 }
1480 
OPL3_ChannelWritePan(opl3_channel * channel,Bit8u data)1481 static void OPL3_ChannelWritePan(opl3_channel *channel, Bit8u data)
1482 {
1483     channel->chl = panlawtable[data & 0x7F];
1484     channel->chr = panlawtable[0x7F - (data & 0x7F)];
1485 }
1486 
OPL3_WritePan(opl3_chip * chip,Bit16u reg,Bit8u v)1487 void OPL3_WritePan(opl3_chip *chip, Bit16u reg, Bit8u v)
1488 {
1489     Bit8u high = (reg >> 8) & 0x01;
1490     Bit8u regm = reg & 0xff;
1491     OPL3_ChannelWritePan(&chip->channel[9 * high + (regm & 0x0f)], v);
1492 }
1493 
OPL3_WriteReg(opl3_chip * chip,Bit16u reg,Bit8u v)1494 void OPL3_WriteReg(opl3_chip *chip, Bit16u reg, Bit8u v)
1495 {
1496     Bit8u high = (reg >> 8) & 0x01;
1497     Bit8u regm = reg & 0xff;
1498     switch (regm & 0xf0)
1499     {
1500     case 0x00:
1501         if (high)
1502         {
1503             switch (regm & 0x0f)
1504             {
1505             case 0x04:
1506                 OPL3_ChannelSet4Op(chip, v);
1507                 break;
1508             case 0x05:
1509                 chip->newm = v & 0x01;
1510                 break;
1511             }
1512         }
1513         else
1514         {
1515             switch (regm & 0x0f)
1516             {
1517             case 0x08:
1518                 chip->nts = (v >> 6) & 0x01;
1519                 break;
1520             }
1521         }
1522         break;
1523     case 0x20:
1524     case 0x30:
1525         if (ad_slot[regm & 0x1f] >= 0)
1526         {
1527             OPL3_SlotWrite20(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
1528         }
1529         break;
1530     case 0x40:
1531     case 0x50:
1532         if (ad_slot[regm & 0x1f] >= 0)
1533         {
1534             OPL3_SlotWrite40(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
1535         }
1536         break;
1537     case 0x60:
1538     case 0x70:
1539         if (ad_slot[regm & 0x1f] >= 0)
1540         {
1541             OPL3_SlotWrite60(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
1542         }
1543         break;
1544     case 0x80:
1545     case 0x90:
1546         if (ad_slot[regm & 0x1f] >= 0)
1547         {
1548             OPL3_SlotWrite80(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
1549         }
1550         break;
1551     case 0xe0:
1552     case 0xf0:
1553         if (ad_slot[regm & 0x1f] >= 0)
1554         {
1555             OPL3_SlotWriteE0(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
1556         }
1557         break;
1558     case 0xa0:
1559         if ((regm & 0x0f) < 9)
1560         {
1561             OPL3_ChannelWriteA0(&chip->channel[9 * high + (regm & 0x0f)], v);
1562         }
1563         break;
1564     case 0xb0:
1565         if (regm == 0xbd && !high)
1566         {
1567             chip->tremoloshift = (((v >> 7) ^ 1) << 1) + 2;
1568             chip->vibshift = ((v >> 6) & 0x01) ^ 1;
1569             OPL3_ChannelUpdateRhythm(chip, v);
1570         }
1571         else if ((regm & 0x0f) < 9)
1572         {
1573             OPL3_ChannelWriteB0(&chip->channel[9 * high + (regm & 0x0f)], v);
1574             if (v & 0x20)
1575             {
1576                 OPL3_ChannelKeyOn(&chip->channel[9 * high + (regm & 0x0f)]);
1577             }
1578             else
1579             {
1580                 OPL3_ChannelKeyOff(&chip->channel[9 * high + (regm & 0x0f)]);
1581             }
1582         }
1583         break;
1584     case 0xc0:
1585         if ((regm & 0x0f) < 9)
1586         {
1587             OPL3_ChannelWriteC0(&chip->channel[9 * high + (regm & 0x0f)], v);
1588         }
1589         break;
1590     }
1591 }
1592 
OPL3_WriteRegBuffered(opl3_chip * chip,Bit16u reg,Bit8u v)1593 void OPL3_WriteRegBuffered(opl3_chip *chip, Bit16u reg, Bit8u v)
1594 {
1595     Bit64u time1, time2;
1596 
1597     if (chip->writebuf[chip->writebuf_last].reg & 0x200)
1598     {
1599         OPL3_WriteReg(chip, chip->writebuf[chip->writebuf_last].reg & 0x1ff,
1600                       chip->writebuf[chip->writebuf_last].data);
1601 
1602         chip->writebuf_cur = (chip->writebuf_last + 1) % OPL_WRITEBUF_SIZE;
1603         chip->writebuf_samplecnt = chip->writebuf[chip->writebuf_last].time;
1604     }
1605 
1606     chip->writebuf[chip->writebuf_last].reg = reg | 0x200;
1607     chip->writebuf[chip->writebuf_last].data = v;
1608     time1 = chip->writebuf_lasttime + OPL_WRITEBUF_DELAY;
1609     time2 = chip->writebuf_samplecnt;
1610 
1611     if (time1 < time2)
1612     {
1613         time1 = time2;
1614     }
1615 
1616     chip->writebuf[chip->writebuf_last].time = time1;
1617     chip->writebuf_lasttime = time1;
1618     chip->writebuf_last = (chip->writebuf_last + 1) % OPL_WRITEBUF_SIZE;
1619 }
1620 
OPL3_GenerateStream(opl3_chip * chip,Bit16s * sndptr,Bit32u numsamples)1621 void OPL3_GenerateStream(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples)
1622 {
1623     Bit32u i;
1624 
1625     for(i = 0; i < numsamples; i++)
1626     {
1627         OPL3_GenerateResampled(chip, sndptr);
1628         sndptr += 2;
1629     }
1630 }
1631 
1632 #define OPL3_MIN(A, B)          (((A) > (B)) ? (B) : (A))
1633 #define OPL3_MAX(A, B)          (((A) < (B)) ? (B) : (A))
1634 #define OPL3_CLAMP(V, MIN, MAX) OPL3_MAX(OPL3_MIN(V, MAX), MIN)
1635 
OPL3_GenerateStreamMix(opl3_chip * chip,Bit16s * sndptr,Bit32u numsamples)1636 void OPL3_GenerateStreamMix(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples)
1637 {
1638     Bit32u i;
1639     Bit16s sample[2];
1640     Bit32s mix[2];
1641 
1642     for(i = 0; i < numsamples; i++)
1643     {
1644         OPL3_GenerateResampled(chip, sample);
1645         mix[0] = sndptr[0] + sample[0];
1646         mix[1] = sndptr[1] + sample[1];
1647         sndptr[0] = OPL3_CLAMP(mix[0], INT16_MIN, INT16_MAX);
1648         sndptr[1] = OPL3_CLAMP(mix[1], INT16_MIN, INT16_MAX);
1649         sndptr += 2;
1650     }
1651 }
1652