Debian Codegraph @

android/app/jni/extern/opm/opm.c

raw
1 /* Nuked OPM
2 * Copyright (C) 2022 Nuke.YKT
3 *
4 * This file is part of Nuked OPM.
5 *
6 * Nuked OPM is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, either version 2.1
9 * of the License, or (at your option) any later version.
10 *
11 * Nuked OPM is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with Nuked OPM. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Nuked OPM emulator.
20 * Thanks:
21 * siliconpr0n.org(digshadow, John McMaster):
22 * YM2151 and other FM chip decaps and die shots.
23 *
24 * version: 0.9.3 beta
25 */
26 #include <string.h>
27 #include <stdint.h>
28 #include "opm.h"
29
30 enum {
31 eg_num_attack = 0,
32 eg_num_decay = 1,
33 eg_num_sustain = 2,
34 eg_num_release = 3
35 };
36
37 /* logsin table */
38 static const uint16_t logsinrom[256] = {
39 0x859, 0x6c3, 0x607, 0x58b, 0x52e, 0x4e4, 0x4a6, 0x471,
40 0x443, 0x41a, 0x3f5, 0x3d3, 0x3b5, 0x398, 0x37e, 0x365,
41 0x34e, 0x339, 0x324, 0x311, 0x2ff, 0x2ed, 0x2dc, 0x2cd,
42 0x2bd, 0x2af, 0x2a0, 0x293, 0x286, 0x279, 0x26d, 0x261,
43 0x256, 0x24b, 0x240, 0x236, 0x22c, 0x222, 0x218, 0x20f,
44 0x206, 0x1fd, 0x1f5, 0x1ec, 0x1e4, 0x1dc, 0x1d4, 0x1cd,
45 0x1c5, 0x1be, 0x1b7, 0x1b0, 0x1a9, 0x1a2, 0x19b, 0x195,
46 0x18f, 0x188, 0x182, 0x17c, 0x177, 0x171, 0x16b, 0x166,
47 0x160, 0x15b, 0x155, 0x150, 0x14b, 0x146, 0x141, 0x13c,
48 0x137, 0x133, 0x12e, 0x129, 0x125, 0x121, 0x11c, 0x118,
49 0x114, 0x10f, 0x10b, 0x107, 0x103, 0x0ff, 0x0fb, 0x0f8,
50 0x0f4, 0x0f0, 0x0ec, 0x0e9, 0x0e5, 0x0e2, 0x0de, 0x0db,
51 0x0d7, 0x0d4, 0x0d1, 0x0cd, 0x0ca, 0x0c7, 0x0c4, 0x0c1,
52 0x0be, 0x0bb, 0x0b8, 0x0b5, 0x0b2, 0x0af, 0x0ac, 0x0a9,
53 0x0a7, 0x0a4, 0x0a1, 0x09f, 0x09c, 0x099, 0x097, 0x094,
54 0x092, 0x08f, 0x08d, 0x08a, 0x088, 0x086, 0x083, 0x081,
55 0x07f, 0x07d, 0x07a, 0x078, 0x076, 0x074, 0x072, 0x070,
56 0x06e, 0x06c, 0x06a, 0x068, 0x066, 0x064, 0x062, 0x060,
57 0x05e, 0x05c, 0x05b, 0x059, 0x057, 0x055, 0x053, 0x052,
58 0x050, 0x04e, 0x04d, 0x04b, 0x04a, 0x048, 0x046, 0x045,
59 0x043, 0x042, 0x040, 0x03f, 0x03e, 0x03c, 0x03b, 0x039,
60 0x038, 0x037, 0x035, 0x034, 0x033, 0x031, 0x030, 0x02f,
61 0x02e, 0x02d, 0x02b, 0x02a, 0x029, 0x028, 0x027, 0x026,
62 0x025, 0x024, 0x023, 0x022, 0x021, 0x020, 0x01f, 0x01e,
63 0x01d, 0x01c, 0x01b, 0x01a, 0x019, 0x018, 0x017, 0x017,
64 0x016, 0x015, 0x014, 0x014, 0x013, 0x012, 0x011, 0x011,
65 0x010, 0x00f, 0x00f, 0x00e, 0x00d, 0x00d, 0x00c, 0x00c,
66 0x00b, 0x00a, 0x00a, 0x009, 0x009, 0x008, 0x008, 0x007,
67 0x007, 0x007, 0x006, 0x006, 0x005, 0x005, 0x005, 0x004,
68 0x004, 0x004, 0x003, 0x003, 0x003, 0x002, 0x002, 0x002,
69 0x002, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,
70 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000
71 };
72
73 /* exp table */
74 static const uint16_t exprom[256] = {
75 0x7fa, 0x7f5, 0x7ef, 0x7ea, 0x7e4, 0x7df, 0x7da, 0x7d4,
76 0x7cf, 0x7c9, 0x7c4, 0x7bf, 0x7b9, 0x7b4, 0x7ae, 0x7a9,
77 0x7a4, 0x79f, 0x799, 0x794, 0x78f, 0x78a, 0x784, 0x77f,
78 0x77a, 0x775, 0x770, 0x76a, 0x765, 0x760, 0x75b, 0x756,
79 0x751, 0x74c, 0x747, 0x742, 0x73d, 0x738, 0x733, 0x72e,
80 0x729, 0x724, 0x71f, 0x71a, 0x715, 0x710, 0x70b, 0x706,
81 0x702, 0x6fd, 0x6f8, 0x6f3, 0x6ee, 0x6e9, 0x6e5, 0x6e0,
82 0x6db, 0x6d6, 0x6d2, 0x6cd, 0x6c8, 0x6c4, 0x6bf, 0x6ba,
83 0x6b5, 0x6b1, 0x6ac, 0x6a8, 0x6a3, 0x69e, 0x69a, 0x695,
84 0x691, 0x68c, 0x688, 0x683, 0x67f, 0x67a, 0x676, 0x671,
85 0x66d, 0x668, 0x664, 0x65f, 0x65b, 0x657, 0x652, 0x64e,
86 0x649, 0x645, 0x641, 0x63c, 0x638, 0x634, 0x630, 0x62b,
87 0x627, 0x623, 0x61e, 0x61a, 0x616, 0x612, 0x60e, 0x609,
88 0x605, 0x601, 0x5fd, 0x5f9, 0x5f5, 0x5f0, 0x5ec, 0x5e8,
89 0x5e4, 0x5e0, 0x5dc, 0x5d8, 0x5d4, 0x5d0, 0x5cc, 0x5c8,
90 0x5c4, 0x5c0, 0x5bc, 0x5b8, 0x5b4, 0x5b0, 0x5ac, 0x5a8,
91 0x5a4, 0x5a0, 0x59c, 0x599, 0x595, 0x591, 0x58d, 0x589,
92 0x585, 0x581, 0x57e, 0x57a, 0x576, 0x572, 0x56f, 0x56b,
93 0x567, 0x563, 0x560, 0x55c, 0x558, 0x554, 0x551, 0x54d,
94 0x549, 0x546, 0x542, 0x53e, 0x53b, 0x537, 0x534, 0x530,
95 0x52c, 0x529, 0x525, 0x522, 0x51e, 0x51b, 0x517, 0x514,
96 0x510, 0x50c, 0x509, 0x506, 0x502, 0x4ff, 0x4fb, 0x4f8,
97 0x4f4, 0x4f1, 0x4ed, 0x4ea, 0x4e7, 0x4e3, 0x4e0, 0x4dc,
98 0x4d9, 0x4d6, 0x4d2, 0x4cf, 0x4cc, 0x4c8, 0x4c5, 0x4c2,
99 0x4be, 0x4bb, 0x4b8, 0x4b5, 0x4b1, 0x4ae, 0x4ab, 0x4a8,
100 0x4a4, 0x4a1, 0x49e, 0x49b, 0x498, 0x494, 0x491, 0x48e,
101 0x48b, 0x488, 0x485, 0x482, 0x47e, 0x47b, 0x478, 0x475,
102 0x472, 0x46f, 0x46c, 0x469, 0x466, 0x463, 0x460, 0x45d,
103 0x45a, 0x457, 0x454, 0x451, 0x44e, 0x44b, 0x448, 0x445,
104 0x442, 0x43f, 0x43c, 0x439, 0x436, 0x433, 0x430, 0x42d,
105 0x42a, 0x428, 0x425, 0x422, 0x41f, 0x41c, 0x419, 0x416,
106 0x414, 0x411, 0x40e, 0x40b, 0x408, 0x406, 0x403, 0x400
107 };
108
109 /* Envelope generator */
110 static const uint32_t eg_stephi[4][4] = {
111 { 0, 0, 0, 0 },
112 { 1, 0, 0, 0 },
113 { 1, 0, 1, 0 },
114 { 1, 1, 1, 0 }
115 };
116
117 /* Phase generator */
118 static const uint32_t pg_detune[8] = { 16, 17, 19, 20, 22, 24, 27, 29 };
119
120 typedef struct {
121 int32_t basefreq;
122 int32_t approxtype;
123 int32_t slope;
124 } freqtable_t;
125
126 static const freqtable_t pg_freqtable[64] = {
127 { 1299, 1, 19 },
128 { 1318, 1, 19 },
129 { 1337, 1, 19 },
130 { 1356, 1, 20 },
131 { 1376, 1, 20 },
132 { 1396, 1, 20 },
133 { 1416, 1, 21 },
134 { 1437, 1, 20 },
135 { 1458, 1, 21 },
136 { 1479, 1, 21 },
137 { 1501, 1, 22 },
138 { 1523, 1, 22 },
139 { 0, 0, 16 },
140 { 0, 0, 16 },
141 { 0, 0, 16 },
142 { 0, 0, 16 },
143 { 1545, 1, 22 },
144 { 1567, 1, 22 },
145 { 1590, 1, 23 },
146 { 1613, 1, 23 },
147 { 1637, 1, 23 },
148 { 1660, 1, 24 },
149 { 1685, 1, 24 },
150 { 1709, 1, 24 },
151 { 1734, 1, 25 },
152 { 1759, 1, 25 },
153 { 1785, 1, 26 },
154 { 1811, 1, 26 },
155 { 0, 0, 16 },
156 { 0, 0, 16 },
157 { 0, 0, 16 },
158 { 0, 0, 16 },
159 { 1837, 1, 26 },
160 { 1864, 1, 27 },
161 { 1891, 1, 27 },
162 { 1918, 1, 28 },
163 { 1946, 1, 28 },
164 { 1975, 1, 28 },
165 { 2003, 1, 29 },
166 { 2032, 1, 30 },
167 { 2062, 1, 30 },
168 { 2092, 1, 30 },
169 { 2122, 1, 31 },
170 { 2153, 1, 31 },
171 { 0, 0, 16 },
172 { 0, 0, 16 },
173 { 0, 0, 16 },
174 { 0, 0, 16 },
175 { 2185, 1, 31 },
176 { 2216, 0, 31 },
177 { 2249, 0, 31 },
178 { 2281, 0, 31 },
179 { 2315, 0, 31 },
180 { 2348, 0, 31 },
181 { 2382, 0, 30 },
182 { 2417, 0, 30 },
183 { 2452, 0, 30 },
184 { 2488, 0, 30 },
185 { 2524, 0, 30 },
186 { 2561, 0, 30 },
187 { 0, 0, 16 },
188 { 0, 0, 16 },
189 { 0, 0, 16 },
190 { 0, 0, 16 }
191 };
192
193
194 /* FM algorithm */
195 static const uint32_t fm_algorithm[4][6][8] = {
196 {
197 { 1, 1, 1, 1, 1, 1, 1, 1 }, /* M1_0 */
198 { 1, 1, 1, 1, 1, 1, 1, 1 }, /* M1_1 */
199 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* C1 */
200 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */
201 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */
202 { 0, 0, 0, 0, 0, 0, 0, 1 } /* Out */
203 },
204 {
205 { 0, 1, 0, 0, 0, 1, 0, 0 }, /* M1_0 */
206 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* M1_1 */
207 { 1, 1, 1, 0, 0, 0, 0, 0 }, /* C1 */
208 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */
209 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */
210 { 0, 0, 0, 0, 0, 1, 1, 1 } /* Out */
211 },
212 {
213 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* M1_0 */
214 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* M1_1 */
215 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* C1 */
216 { 1, 0, 0, 1, 1, 1, 1, 0 }, /* Last operator */
217 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* Last operator */
218 { 0, 0, 0, 0, 1, 1, 1, 1 } /* Out */
219 },
220 {
221 { 0, 0, 1, 0, 0, 1, 0, 0 }, /* M1_0 */
222 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* M1_1 */
223 { 0, 0, 0, 1, 0, 0, 0, 0 }, /* C1 */
224 { 1, 1, 0, 1, 1, 0, 0, 0 }, /* Last operator */
225 { 0, 0, 1, 0, 0, 0, 0, 0 }, /* Last operator */
226 { 1, 1, 1, 1, 1, 1, 1, 1 } /* Out */
227 }
228 };
229
230 static uint16_t lfo_counter2_table[] = {
231 0x0000, 0x4000, 0x6000, 0x7000,
232 0x7800, 0x7c00, 0x7e00, 0x7f00,
233 0x7f80, 0x7fc0, 0x7fe0, 0x7ff0,
234 0x7ff8, 0x7ffc, 0x7ffe, 0x7fff
235 };
236
237 static inline int32_t OPM_KCToFNum(int32_t kcode)
238 {
239 int32_t kcode_h = (kcode >> 4) & 63;
240 int32_t kcode_l = kcode & 15;
241 int32_t i, slope, sum = 0;
242 if (pg_freqtable[kcode_h].approxtype)
243 {
244 for (i = 0; i < 4; i++)
245 {
246 if (kcode_l & (1 << i))
247 {
248 sum += (pg_freqtable[kcode_h].slope >> (3 - i));
249 }
250 }
251 }
252 else
253 {
254 slope = pg_freqtable[kcode_h].slope | 1;
255 if (kcode_l & 1)
256 {
257 sum += (slope >> 3) + 2;
258 }
259 if (kcode_l & 2)
260 {
261 sum += 8;
262 }
263 if (kcode_l & 4)
264 {
265 sum += slope >> 1;
266 }
267 if (kcode_l & 8)
268 {
269 sum += slope;
270 sum++;
271 }
272 if ((kcode_l & 12) == 12 && (pg_freqtable[kcode_h].slope & 1) == 0)
273 {
274 sum += 4;
275 }
276 }
277 return pg_freqtable[kcode_h].basefreq + (sum >> 1);
278 }
279
280 static inline int32_t OPM_LFOApplyPMS(int32_t lfo, int32_t pms)
281 {
282 int32_t t, out;
283 int32_t top = (lfo >> 4) & 7;
284 if (pms != 7)
285 {
286 top >>= 1;
287 }
288 t = (top & 6) == 6 || ((top & 3) == 3 && pms >= 6);
289
290 out = top + ((top >> 2) & 1) + t;
291 out = out * 2 + ((lfo >> 4) & 1);
292
293 if (pms == 7)
294 {
295 out >>= 1;
296 }
297 out &= 15;
298 out = (lfo & 15) + out * 16;
299 switch (pms)
300 {
301 case 0:
302 default:
303 out = 0;
304 break;
305 case 1:
306 out = (out >> 5) & 3;
307 break;
308 case 2:
309 out = (out >> 4) & 7;
310 break;
311 case 3:
312 out = (out >> 3) & 15;
313 break;
314 case 4:
315 out = (out >> 2) & 31;
316 break;
317 case 5:
318 out = (out >> 1) & 63;
319 break;
320 case 6:
321 out = (out & 255) << 1;
322 break;
323 case 7:
324 out = (out & 255) << 2;
325 break;
326 }
327 return out;
328 }
329
330 static inline int32_t OPM_CalcKCode(int32_t kcf, int32_t lfo, int32_t lfo_sign, int32_t dt)
331 {
332 int32_t t2, t3, b0, b1, b2, b3, w2, w3, w6;
333 int32_t overflow1 = 0;
334 int32_t overflow2 = 0;
335 int32_t negoverflow = 0;
336 int32_t sum, cr;
337 if (!lfo_sign)
338 {
339 lfo = ~lfo;
340 }
341 sum = (kcf & 8191) + (lfo&8191) + (!lfo_sign);
342 cr = ((kcf & 255) + (lfo & 255) + (!lfo_sign)) >> 8;
343 if (sum & (1 << 13))
344 {
345 overflow1 = 1;
346 }
347 sum &= 8191;
348 if (lfo_sign && ((((sum >> 6) & 3) == 3) || cr))
349 {
350 sum += 64;
351 }
352 if (!lfo_sign && !cr)
353 {
354 sum += (-64)&8191;
355 negoverflow = 1;
356 }
357 if (sum & (1 << 13))
358 {
359 overflow2 = 1;
360 }
361 sum &= 8191;
362 if ((!lfo_sign && !overflow1) || (negoverflow && !overflow2))
363 {
364 sum = 0;
365 }
366 if (lfo_sign && (overflow1 || overflow2))
367 {
368 sum = 8127;
369 }
370
371 t2 = sum & 63;
372 if (dt == 2)
373 t2 += 20;
374 if (dt == 2 || dt == 3)
375 t2 += 32;
376
377 b0 = (t2 >> 6) & 1;
378 b1 = dt == 2;
379 b2 = ((sum >> 6) & 1);
380 b3 = ((sum >> 7) & 1);
381
382
383 w2 = (b0 && b1 && b2);
384 w3 = (b0 && b3);
385 w6 = (b0 && !w2 && !w3) || (b3 && !b0 && b1);
386
387 t2 &= 63;
388
389 t3 = (sum >> 6) + w6 + b1 + (w2 || w3) * 2 + (dt == 3) * 4 + (dt != 0) * 8;
390 if (t3 & 128)
391 {
392 t2 = 63;
393 t3 = 126;
394 }
395 sum = t3 * 64 + t2;
396 return sum;
397 }
398
399 static inline void OPM_PhaseCalcFNumBlock(opm_t *chip)
400 {
401 uint32_t slot = (chip->cycles + 7) % 32;
402 uint32_t channel = slot % 8;
403 uint32_t kcf = (chip->ch_kc[channel] << 6) + chip->ch_kf[channel];
404 uint32_t lfo = chip->lfo_pmd ? chip->lfo_pm_lock : 0;
405 uint32_t pms = chip->ch_pms[channel];
406 uint32_t dt = chip->sl_dt2[slot];
407 int32_t lfo_pm = OPM_LFOApplyPMS(lfo & 127, pms);
408 uint32_t kcode = OPM_CalcKCode(kcf, lfo_pm, (lfo & 0x80) != 0 && pms != 0 ? 0 : 1, dt);
409 uint32_t fnum = OPM_KCToFNum(kcode);
410 uint32_t kcode_h = kcode >> 8;
411 chip->pg_fnum[slot] = fnum;
412 chip->pg_kcode[slot] = kcode_h;
413 }
414
415 static inline void OPM_PhaseCalcIncrement(opm_t *chip)
416 {
417 uint32_t slot = chip->cycles;
418 uint32_t channel = slot % 8;
419 uint32_t dt = chip->sl_dt1[slot];
420 uint32_t dt_l = dt & 3;
421 uint32_t detune = 0;
422 uint32_t multi = chip->sl_mul[slot];
423 uint32_t kcode = chip->pg_kcode[slot];
424 uint32_t fnum = chip->pg_fnum[slot];
425 uint32_t block = kcode >> 2;
426 uint32_t basefreq = (fnum << block) >> 2;
427 uint32_t note, sum, sum_h, sum_l, inc;
428 /* Apply detune */
429 if (dt_l)
430 {
431 if (kcode > 0x1c)
432 {
433 kcode = 0x1c;
434 }
435 block = kcode >> 2;
436 note = kcode & 0x03;
437 sum = block + 9 + ((dt_l == 3) | (dt_l & 0x02));
438 sum_h = sum >> 1;
439 sum_l = sum & 0x01;
440 detune = pg_detune[(sum_l << 2) | note] >> (9 - sum_h);
441 }
442 if (dt & 0x04)
443 {
444 basefreq -= detune;
445 }
446 else
447 {
448 basefreq += detune;
449 }
450 basefreq &= 0x1ffff;
451 if (multi)
452 {
453 inc = basefreq * multi;
454 }
455 else
456 {
457 inc = basefreq >> 1;
458 }
459 inc &= 0xfffff;
460 chip->pg_inc[slot] = inc;
461 }
462
463 static inline void OPM_PhaseGenerate(opm_t *chip)
464 {
465 uint32_t slot = (chip->cycles + 27) % 32;
466 chip->pg_reset_latch[slot] = chip->pg_reset[slot];
467 slot = (chip->cycles + 25) % 32;
468 /* Mask increment */
469 if (chip->pg_reset_latch[slot])
470 {
471 chip->pg_inc[slot] = 0;
472 }
473 /* Phase step */
474 slot = (chip->cycles + 24) % 32;
475 if (chip->pg_reset_latch[slot] || chip->mode_test[3])
476 {
477 chip->pg_phase[slot] = 0;
478 }
479 chip->pg_phase[slot] += chip->pg_inc[slot];
480 chip->pg_phase[slot] &= 0xfffff;
481 }
482
483 static inline void OPM_PhaseDebug(opm_t *chip)
484 {
485 chip->pg_serial >>= 1;
486 if (chip->cycles == 5)
487 {
488 chip->pg_serial |= (chip->pg_phase[29] & 0x3ff);
489 }
490 }
491
492 static inline void OPM_KeyOn1(opm_t *chip)
493 {
494 uint32_t cycles = (chip->cycles + 1) % 32;
495 chip->kon_chanmatch = 0;
496 if (chip->mode_kon_channel + 24 == cycles)
497 {
498 chip->kon_chanmatch = 1;
499 }
500 }
501
502 static inline void OPM_KeyOn2(opm_t *chip)
503 {
504 uint32_t slot = (chip->cycles + 8) % 32;
505 if (chip->kon_chanmatch)
506 {
507 chip->mode_kon[(slot + 0) % 32] = chip->mode_kon_operator[0];
508 chip->mode_kon[(slot + 8) % 32] = chip->mode_kon_operator[2];
509 chip->mode_kon[(slot + 16) % 32] = chip->mode_kon_operator[1];
510 chip->mode_kon[(slot + 24) % 32] = chip->mode_kon_operator[3];
511 }
512 }
513
514 static inline void OPM_EnvelopePhase1(opm_t *chip)
515 {
516 uint32_t slot = (chip->cycles + 2) % 32;
517 uint32_t kon = chip->mode_kon[slot] | chip->kon_csm;
518 uint32_t konevent = !chip->kon[slot] && kon;
519 if (konevent)
520 {
521 chip->eg_state[slot] = eg_num_attack;
522 }
523
524 chip->kon2[slot] = chip->kon[slot];
525 chip->kon[slot] = kon;
526 }
527
528 static inline void OPM_EnvelopePhase2(opm_t *chip)
529 {
530 uint32_t slot = chip->cycles;
531 uint32_t chan = slot % 8;
532 uint8_t rate = 0, ksv, zr, ams;
533 switch (chip->eg_state[slot])
534 {
535 case eg_num_attack:
536 rate = chip->sl_ar[slot];
537 break;
538 case eg_num_decay:
539 rate = chip->sl_d1r[slot];
540 break;
541 case eg_num_sustain:
542 rate = chip->sl_d2r[slot];
543 break;
544 case eg_num_release:
545 rate = chip->sl_rr[slot] * 2 + 1;
546 break;
547 default:
548 break;
549 }
550 if (chip->ic)
551 {
552 rate = 31;
553 }
554
555 zr = rate == 0;
556
557 ksv = chip->pg_kcode[slot] >> (chip->sl_ks[slot] ^ 3);
558 if (chip->sl_ks[slot] == 0 && zr)
559 {
560 ksv &= ~3;
561 }
562 rate = rate * 2 + ksv;
563 if (rate & 64)
564 {
565 rate = 63;
566 }
567
568 chip->eg_tl[2] = chip->eg_tl[1];
569 chip->eg_tl[1] = chip->eg_tl[0];
570 chip->eg_tl[0] = chip->sl_tl[slot];
571 chip->eg_sl[1] = chip->eg_sl[0];
572 chip->eg_sl[0] = chip->sl_d1l[slot];
573 if (chip->sl_d1l[slot] == 15)
574 {
575 chip->eg_sl[0] = 31;
576 }
577 chip->eg_zr[1] = chip->eg_zr[0];
578 chip->eg_zr[0] = zr;
579 chip->eg_rate[1] = chip->eg_rate[0];
580 chip->eg_rate[0] = rate;
581 chip->eg_ratemax[1] = chip->eg_ratemax[0];
582 chip->eg_ratemax[0] = (rate >> 1) == 31;
583 ams = chip->sl_am_e[slot] ? chip->ch_ams[chan] : 0;
584 switch (ams)
585 {
586 default:
587 case 0:
588 chip->eg_am = 0;
589 break;
590 case 1:
591 chip->eg_am = chip->lfo_am_lock << 0;
592 break;
593 case 2:
594 chip->eg_am = chip->lfo_am_lock << 1;
595 break;
596 case 3:
597 chip->eg_am = chip->lfo_am_lock << 2;
598 break;
599 }
600 }
601
602 static inline void OPM_EnvelopePhase3(opm_t *chip)
603 {
604 uint32_t slot = (chip->cycles + 31) % 32;
605 chip->eg_shift = (chip->eg_timershift_lock + (chip->eg_rate[0] >> 2)) & 15;
606 chip->eg_inchi = eg_stephi[chip->eg_rate[0] & 3][chip->eg_timer_lock & 3];
607
608 chip->eg_outtemp[1] = chip->eg_outtemp[0];
609 chip->eg_outtemp[0] = chip->eg_level[slot] + chip->eg_am;
610 if (chip->eg_outtemp[0] & 1024)
611 {
612 chip->eg_outtemp[0] = 1023;
613 }
614 }
615
616 static inline void OPM_EnvelopePhase4(opm_t *chip)
617 {
618 uint32_t slot = (chip->cycles + 30) % 32;
619 uint8_t inc = 0;
620 uint8_t kon, eg_off, eg_zero, slreach;
621 if (chip->eg_clock & 2)
622 {
623 if (chip->eg_rate[1] >= 48)
624 {
625 inc = chip->eg_inchi + (chip->eg_rate[1] >> 2) - 11;
626 if (inc > 4)
627 {
628 inc = 4;
629 }
630 }
631 else if (!chip->eg_zr[1])
632 {
633 switch (chip->eg_shift)
634 {
635 case 12:
636 inc = chip->eg_rate[1] != 0;
637 break;
638 case 13:
639 inc = (chip->eg_rate[1] >> 1) & 1;
640 break;
641 case 14:
642 inc = chip->eg_rate[1] & 1;
643 break;
644 }
645 }
646 }
647 chip->eg_inc = inc;
648
649 kon = chip->kon[slot] && !chip->kon2[slot];
650 chip->pg_reset[slot] = kon;
651 chip->eg_instantattack = chip->eg_ratemax[1] && (kon || !chip->eg_ratemax[1]);
652
653 eg_off = (chip->eg_level[slot] & 0x3f0) == 0x3f0;
654 slreach = (chip->eg_level[slot] >> 4) == (chip->eg_sl[1] << 1);
655 eg_zero = chip->eg_level[slot] == 0;
656
657 chip->eg_mute = eg_off && chip->eg_state[slot] != eg_num_attack && !kon;
658 chip->eg_inclinear = 0;
659 if (!kon && !eg_off)
660 {
661 switch (chip->eg_state[slot])
662 {
663 case eg_num_decay:
664 if (!slreach)
665 chip->eg_inclinear = 1;
666 break;
667 case eg_num_sustain:
668 case eg_num_release:
669 chip->eg_inclinear = 1;
670 break;
671 }
672 }
673 chip->eg_incattack = chip->eg_state[slot] == eg_num_attack && !chip->eg_ratemax[1] && chip->kon[slot] && !eg_zero;
674
675
676 // Update state
677 if (kon)
678 {
679 chip->eg_state[slot] = eg_num_attack;
680 }
681 else if (!chip->kon[slot])
682 {
683 chip->eg_state[slot] = eg_num_release;
684 }
685 else
686 {
687 switch (chip->eg_state[slot])
688 {
689 case eg_num_attack:
690 if (eg_zero)
691 {
692 chip->eg_state[slot] = eg_num_decay;
693 }
694 break;
695 case eg_num_decay:
696 if (eg_off)
697 {
698 chip->eg_state[slot] = eg_num_release;
699 }
700 else if (slreach)
701 {
702 chip->eg_state[slot] = eg_num_sustain;
703 }
704 break;
705 case eg_num_sustain:
706 if (eg_off)
707 {
708 chip->eg_state[slot] = eg_num_release;
709 }
710 break;
711 case eg_num_release:
712 break;
713 }
714 }
715
716 if (chip->ic)
717 {
718 chip->eg_state[slot] = eg_num_release;
719 }
720 }
721
722 static inline void OPM_EnvelopePhase5(opm_t *chip)
723 {
724 uint32_t slot = (chip->cycles + 29) % 32;
725 uint32_t level = chip->eg_level[slot];
726 uint32_t step = 0;
727 if (chip->eg_instantattack)
728 {
729 level = 0;
730 }
731 if (chip->eg_mute || chip->ic)
732 {
733 level = 0x3ff;
734 }
735 if (chip->eg_inc)
736 {
737 if (chip->eg_inclinear)
738 {
739 step |= 1 << (chip->eg_inc - 1);
740 }
741 if (chip->eg_incattack)
742 {
743 step |= ((~(int32_t)chip->eg_level[slot]) << chip->eg_inc) >> 5;
744 }
745 }
746 level += step;
747 chip->eg_level[slot] = (uint16_t)level;
748
749 chip->eg_out[0] = chip->eg_outtemp[1] + (chip->eg_tl[2] << 3);
750 if (chip->eg_out[0] & 1024)
751 {
752 chip->eg_out[0] = 1023;
753 }
754
755 if (chip->eg_test)
756 {
757 chip->eg_out[0] = 0;
758 }
759
760 chip->eg_test = chip->mode_test[5];
761 }
762
763 static inline void OPM_EnvelopePhase6(opm_t *chip)
764 {
765 uint32_t slot = (chip->cycles + 28) % 32;
766 chip->eg_serial_bit = (chip->eg_serial >> 9) & 1;
767 if (chip->cycles == 3)
768 {
769 chip->eg_serial = chip->eg_out[0] ^ 1023;
770 }
771 else
772 {
773 chip->eg_serial <<= 1;
774 }
775
776 chip->eg_out[1] = chip->eg_out[0];
777 }
778
779 static void OPM_EnvelopeClock(opm_t *chip)
780 {
781 chip->eg_clock <<= 1;
782 if ((chip->eg_clockcnt & 2) != 0 || chip->mode_test[0])
783 {
784 chip->eg_clock |= 1;
785 }
786 if (chip->ic || (chip->cycles == 31 && (chip->eg_clockcnt & 2) != 0))
787 {
788 chip->eg_clockcnt = 0;
789 }
790 else if (chip->cycles == 31)
791 {
792 chip->eg_clockcnt++;
793 }
794 }
795
796 static inline void OPM_EnvelopeTimer(opm_t *chip)
797 {
798 uint32_t cycle = (chip->cycles + 31) % 16;
799 uint32_t cycle2;
800 uint8_t inc = ((chip->cycles + 31) % 32) < 16 && (chip->eg_clock & 1) != 0 && (cycle == 0 || chip->eg_timercarry);
801 uint8_t timerbit = (chip->eg_timer >> cycle) & 1;
802 uint8_t sum = timerbit + inc;
803 uint8_t sum0 = (sum & 1) && !chip->ic;
804 chip->eg_timercarry = sum >> 1;
805 chip->eg_timer = (chip->eg_timer & (~(1 << cycle))) | (sum0 << cycle);
806
807 cycle2 = (chip->cycles + 30) % 16;
808
809 chip->eg_timer2 <<= 1;
810 if ((chip->eg_timer & (1 << cycle2)) != 0 && !chip->eg_timerbstop)
811 {
812 chip->eg_timer2 |= 1;
813 }
814
815 if (chip->eg_timer & (1 << cycle2))
816 {
817 chip->eg_timerbstop = 1;
818 }
819
820 if (cycle == 0 || chip->ic2)
821 {
822 chip->eg_timerbstop = 0;
823 }
824
825 if (chip->cycles == 1 && (chip->eg_clock & 1) != 0)
826 {
827 chip->eg_timershift_lock = 0;
828 if (chip->eg_timer2 & (8 + 32 + 128 + 512 + 2048 + 8192 + 32768))
829 {
830 chip->eg_timershift_lock |= 1;
831 }
832 if (chip->eg_timer2 & (4 + 32 + 64 + 512 + 1024 + 8192 + 16384))
833 {
834 chip->eg_timershift_lock |= 2;
835 }
836 if (chip->eg_timer2 & (4 + 8 + 16 + 512 + 1024 + 2048 + 4096))
837 {
838 chip->eg_timershift_lock |= 4;
839 }
840 if (chip->eg_timer2 & (4 + 8 + 16 + 32 + 64 + 128 + 256))
841 {
842 chip->eg_timershift_lock |= 8;
843 }
844 chip->eg_timer_lock = chip->eg_timer;
845 }
846 }
847
848 static inline void OPM_OperatorPhase1(opm_t *chip)
849 {
850 uint32_t slot = chip->cycles;
851 int16_t mod = chip->op_mod[2];
852 chip->op_phase_in = chip->pg_phase[slot] >> 10;
853 if (chip->op_fbshift & 8)
854 {
855 if (chip->op_fb[1] == 0)
856 {
857 mod = 0;
858 }
859 else
860 {
861 mod = mod >> (9 - chip->op_fb[1]);
862 }
863 }
864 chip->op_mod_in = mod;
865 }
866
867 static inline void OPM_OperatorPhase2(opm_t *chip)
868 {
869 uint32_t slot = (chip->cycles + 31) % 32;
870 chip->op_phase = (chip->op_phase_in + chip->op_mod_in) & 1023;
871 }
872
873 static inline void OPM_OperatorPhase3(opm_t *chip)
874 {
875 uint32_t slot = (chip->cycles + 30) % 32;
876 uint16_t phase = chip->op_phase & 255;
877 if (chip->op_phase & 256)
878 {
879 phase ^= 255;
880 }
881 chip->op_logsin[0] = logsinrom[phase];
882 chip->op_sign <<= 1;
883 chip->op_sign |= (chip->op_phase >> 9) & 1;
884 }
885
886 static inline void OPM_OperatorPhase4(opm_t *chip)
887 {
888 uint32_t slot = (chip->cycles + 29) % 32;
889 chip->op_logsin[1] = chip->op_logsin[0];
890 }
891
892 static inline void OPM_OperatorPhase5(opm_t *chip)
893 {
894 uint32_t slot = (chip->cycles + 28) % 32;
895 chip->op_logsin[2] = chip->op_logsin[1];
896 }
897
898 static inline void OPM_OperatorPhase6(opm_t *chip)
899 {
900 uint32_t slot = (chip->cycles + 27) % 32;
901 chip->op_atten = chip->op_logsin[2] + (chip->eg_out[1] << 2);
902 if (chip->op_atten & 4096)
903 {
904 chip->op_atten = 4095;
905 }
906 }
907
908 static inline void OPM_OperatorPhase7(opm_t *chip)
909 {
910 uint32_t slot = (chip->cycles + 26) % 32;
911 chip->op_exp[0] = exprom[chip->op_atten & 255];
912 chip->op_pow[0] = chip->op_atten >> 8;
913 }
914
915 static inline void OPM_OperatorPhase8(opm_t *chip)
916 {
917 uint32_t slot = (chip->cycles + 25) % 32;
918 chip->op_exp[1] = chip->op_exp[0];
919 chip->op_pow[1] = chip->op_pow[0];
920 }
921
922 static inline void OPM_OperatorPhase9(opm_t *chip)
923 {
924 uint32_t slot = (chip->cycles + 24) % 32;
925 int16_t out = (chip->op_exp[1] << 2) >> (chip->op_pow[1]);
926 if (chip->op_sign & 32)
927 {
928 out = -out;
929 }
930 chip->op_out[0] = out;
931 }
932
933 static inline void OPM_OperatorPhase10(opm_t *chip)
934 {
935 uint32_t slot = (chip->cycles + 23) % 32;
936 chip->op_out[1] = chip->op_out[0];
937 }
938
939 static inline void OPM_OperatorPhase11(opm_t *chip)
940 {
941 uint32_t slot = (chip->cycles + 22) % 32;
942 chip->op_out[2] = chip->op_out[1];
943 }
944
945 static inline void OPM_OperatorPhase12(opm_t *chip)
946 {
947 uint32_t slot = (chip->cycles + 21) % 32;
948 chip->op_out[3] = chip->op_out[2];
949 }
950
951 static inline void OPM_OperatorPhase13(opm_t *chip)
952 {
953 uint32_t slot = (chip->cycles + 20) % 32;
954 chip->op_out[4] = chip->op_out[3];
955 chip->op_connect = chip->ch_connect[slot % 8];
956 }
957
958 static inline void OPM_OperatorPhase14(opm_t *chip)
959 {
960 uint32_t slot = (chip->cycles + 19) % 32;
961 chip->op_mix = chip->op_out[5] = chip->op_out[4];
962 chip->op_fbupdate = (chip->op_counter == 0);
963 chip->op_c1update = (chip->op_counter == 2);
964 chip->op_fbshift <<= 1;
965 chip->op_fbshift |= (chip->op_counter == 2);
966
967 chip->op_modtable[0] = fm_algorithm[(chip->op_counter+2)%4][0][chip->op_connect];
968 chip->op_modtable[1] = fm_algorithm[(chip->op_counter+2)%4][1][chip->op_connect];
969 chip->op_modtable[2] = fm_algorithm[(chip->op_counter+2)%4][2][chip->op_connect];
970 chip->op_modtable[3] = fm_algorithm[(chip->op_counter+2)%4][3][chip->op_connect];
971 chip->op_modtable[4] = fm_algorithm[(chip->op_counter+2)%4][4][chip->op_connect];
972 chip->op_mixl = fm_algorithm[chip->op_counter][5][chip->op_connect] && (chip->ch_rl[slot % 8] & 1) != 0;
973 chip->op_mixr = fm_algorithm[chip->op_counter][5][chip->op_connect] && (chip->ch_rl[slot % 8] & 2) != 0;
974 }
975
976 static inline void OPM_OperatorPhase15(opm_t *chip)
977 {
978 uint32_t slot = (chip->cycles + 18) % 32;
979 int16_t mod, mod1 = 0, mod2 = 0;
980 if (chip->op_modtable[0])
981 {
982 mod2 |= chip->op_m1[slot % 8][0];
983 }
984 if (chip->op_modtable[1])
985 {
986 mod1 |= chip->op_m1[slot % 8][1];
987 }
988 if (chip->op_modtable[2])
989 {
990 mod1 |= chip->op_c1[slot % 8];
991 }
992 if (chip->op_modtable[3])
993 {
994 mod2 |= chip->op_out[5];
995 }
996 if (chip->op_modtable[4])
997 {
998 mod1 |= chip->op_out[5];
999 }
1000 mod = (mod1 + mod2) >> 1;
1001 chip->op_mod[0] = mod;
1002 if (chip->op_fbupdate)
1003 {
1004 chip->op_m1[slot % 8][1] = chip->op_m1[slot % 8][0];
1005 chip->op_m1[slot % 8][0] = chip->op_out[5];
1006 }
1007 if (chip->op_c1update)
1008 {
1009 chip->op_c1[slot % 8] = chip->op_out[5];
1010 }
1011 }
1012
1013 static inline void OPM_OperatorPhase16(opm_t *chip)
1014 {
1015 uint32_t slot = (chip->cycles + 17) % 32;
1016 // hack
1017 chip->op_mod[2] = chip->op_mod[1];
1018 chip->op_fb[1] = chip->op_fb[0];
1019
1020 chip->op_mod[1] = chip->op_mod[0];
1021 chip->op_fb[0] = chip->ch_fb[slot % 8];
1022 }
1023
1024 static inline void OPM_OperatorCounter(opm_t *chip)
1025 {
1026 if ((chip->cycles % 8) == 4)
1027 {
1028 chip->op_counter++;
1029 }
1030 if (chip->cycles == 12)
1031 {
1032 chip->op_counter = 0;
1033 }
1034 }
1035
1036 static inline void OPM_Mixer2(opm_t *chip)
1037 {
1038 uint32_t cycles = (chip->cycles + 30) % 32;
1039 uint8_t bit;
1040 uint8_t top, ex;
1041 if (cycles < 16)
1042 {
1043 bit = chip->mix_serial[0] & 1;
1044 }
1045 else
1046 {
1047 bit = chip->mix_serial[1] & 1;
1048 }
1049 if (chip->cycles % 16 == 1)
1050 {
1051 chip->mix_sign_lock = bit ^ 1;
1052 chip->mix_top_bits_lock = (chip->mix_bits >> 15) & 63;
1053 }
1054 chip->mix_bits >>= 1;
1055 chip->mix_bits |= bit << 20;
1056 if (chip->cycles % 16 == 10)
1057 {
1058 top = chip->mix_top_bits_lock;
1059 if (chip->mix_sign_lock)
1060 {
1061 top ^= 63;
1062 }
1063 if (top & 32)
1064 {
1065 ex = 7;
1066 }
1067 else if (top & 16)
1068 {
1069 ex = 6;
1070 }
1071 else if (top & 8)
1072 {
1073 ex = 5;
1074 }
1075 else if (top & 4)
1076 {
1077 ex = 4;
1078 }
1079 else if (top & 2)
1080 {
1081 ex = 3;
1082 }
1083 else if (top & 1)
1084 {
1085 ex = 2;
1086 }
1087 else
1088 {
1089 ex = 1;
1090 }
1091 chip->mix_sign_lock2 = chip->mix_sign_lock;
1092 chip->mix_exp_lock = ex;
1093 }
1094 chip->mix_out_bit <<= 1;
1095 switch ((chip->cycles + 1) % 16)
1096 {
1097 case 0:
1098 chip->mix_out_bit |= chip->mix_sign_lock2 ^ 1;
1099 break;
1100 case 1:
1101 chip->mix_out_bit |= (chip->mix_exp_lock >> 0) & 1;
1102 break;
1103 case 2:
1104 chip->mix_out_bit |= (chip->mix_exp_lock >> 1) & 1;
1105 break;
1106 case 3:
1107 chip->mix_out_bit |= (chip->mix_exp_lock >> 2) & 1;
1108 break;
1109 default:
1110 if (chip->mix_exp_lock)
1111 {
1112 chip->mix_out_bit |= (chip->mix_bits >> (chip->mix_exp_lock - 1)) & 1;
1113 }
1114 break;
1115 }
1116 }
1117
1118 static inline void OPM_Output(opm_t *chip)
1119 {
1120 uint32_t slot = (chip->cycles + 27) % 32;
1121 chip->smp_so = (chip->mix_out_bit & 4) != 0;
1122 chip->smp_sh1 = (slot & 24) == 8 && !chip->ic;
1123 chip->smp_sh2 = (slot & 24) == 24 && !chip->ic;
1124 }
1125
1126 static inline void OPM_DAC(opm_t *chip)
1127 {
1128 int32_t exp, mant;
1129 if (chip->dac_osh1 && !chip->smp_sh1)
1130 {
1131 exp = (chip->dac_bits >> 10) & 7;
1132 mant = (chip->dac_bits >> 0) & 1023;
1133 mant -= 512;
1134 chip->dac_output[1] = (mant << exp) >> 1;
1135 }
1136 if (chip->dac_osh2 && !chip->smp_sh2)
1137 {
1138 exp = (chip->dac_bits >> 10) & 7;
1139 mant = (chip->dac_bits >> 0) & 1023;
1140 mant -= 512;
1141 chip->dac_output[0] = (mant << exp) >> 1;
1142 }
1143 chip->dac_bits >>= 1;
1144 chip->dac_bits |= chip->smp_so << 12;
1145 chip->dac_osh1 = chip->smp_sh1;
1146 chip->dac_osh2 = chip->smp_sh2;
1147 }
1148
1149 static inline void OPM_Mixer(opm_t *chip)
1150 {
1151 uint32_t slot = (chip->cycles + 18) % 32;
1152 uint32_t channel = (slot % 8);
1153 // Right channel
1154 chip->mix_serial[1] >>= 1;
1155 if (chip->cycles == 13)
1156 {
1157 chip->mix_serial[1] |= (chip->mix[1] & 1023) << 4;
1158 }
1159 if (chip->cycles == 14)
1160 {
1161 chip->mix_serial[1] |= ((chip->mix2[1] >> 10) & 31) << 13;
1162 chip->mix_serial[1] |= (((chip->mix2[1] >> 17) & 1) ^ 1) << 18;
1163 chip->mix_clamp_low[1] = 0;
1164 chip->mix_clamp_high[1] = 0;
1165 switch ((chip->mix2[1]>>15) & 7)
1166 {
1167 case 0:
1168 default:
1169 break;
1170 case 1:
1171 chip->mix_clamp_high[1] = 1;
1172 break;
1173 case 2:
1174 chip->mix_clamp_high[1] = 1;
1175 break;
1176 case 3:
1177 chip->mix_clamp_high[1] = 1;
1178 break;
1179 case 4:
1180 chip->mix_clamp_low[1] = 1;
1181 break;
1182 case 5:
1183 chip->mix_clamp_low[1] = 1;
1184 break;
1185 case 6:
1186 chip->mix_clamp_low[1] = 1;
1187 break;
1188 case 7:
1189 break;
1190 }
1191 }
1192 if (chip->mix_clamp_low[1])
1193 {
1194 chip->mix_serial[1] &= ~2;
1195 }
1196 if (chip->mix_clamp_high[1])
1197 {
1198 chip->mix_serial[1] |= 2;
1199 }
1200 // Left channel
1201 chip->mix_serial[0] >>= 1;
1202 if (chip->cycles == 29)
1203 {
1204 chip->mix_serial[0] |= (chip->mix[0] & 1023) << 4;
1205 }
1206 if (chip->cycles == 30)
1207 {
1208 chip->mix_serial[0] |= ((chip->mix2[0] >> 10) & 31) << 13;
1209 chip->mix_serial[0] |= (((chip->mix2[0] >> 17) & 1) ^ 1) << 18;
1210 chip->mix_clamp_low[0] = 0;
1211 chip->mix_clamp_high[0] = 0;
1212 switch ((chip->mix2[0]>>15) & 7)
1213 {
1214 case 0:
1215 default:
1216 break;
1217 case 1:
1218 chip->mix_clamp_high[0] = 1;
1219 break;
1220 case 2:
1221 chip->mix_clamp_high[0] = 1;
1222 break;
1223 case 3:
1224 chip->mix_clamp_high[0] = 1;
1225 break;
1226 case 4:
1227 chip->mix_clamp_low[0] = 1;
1228 break;
1229 case 5:
1230 chip->mix_clamp_low[0] = 1;
1231 break;
1232 case 6:
1233 chip->mix_clamp_low[0] = 1;
1234 break;
1235 case 7:
1236 break;
1237 }
1238 }
1239 if (chip->mix_clamp_low[0])
1240 {
1241 chip->mix_serial[0] &= ~2;
1242 }
1243 if (chip->mix_clamp_high[0])
1244 {
1245 chip->mix_serial[0] |= 2;
1246 }
1247 chip->mix2[0] = chip->mix[0];
1248 chip->mix2[1] = chip->mix[1];
1249 if (chip->cycles == 13)
1250 {
1251 chip->mix[1] = 0;
1252 }
1253 if (chip->cycles == 29)
1254 {
1255 chip->mix[0] = 0;
1256 }
1257 chip->mix[0] += chip->op_mix * chip->op_mixl;
1258 chip->mix[1] += chip->op_mix * chip->op_mixr;
1259
1260 if (slot<8) {
1261 chip->op_chmix[slot&7]=0;
1262 }
1263 chip->op_chmix[slot&7]+=chip->op_mix*(chip->op_mixl|chip->op_mixr);
1264 if (slot>=24) {
1265 chip->ch_out[slot&7]=chip->op_chmix[slot&7];
1266 }
1267 }
1268
1269 static inline void OPM_Noise(opm_t *chip)
1270 {
1271 uint8_t w1 = !chip->ic && !chip->noise_update;
1272 uint8_t xr = ((chip->noise_lfsr >> 2) & 1) ^ chip->noise_temp;
1273 uint8_t w2t = (chip->noise_lfsr & 0xffff) == 0xffff && chip->noise_temp == 0;
1274 uint8_t w2 = !w2t && !xr;
1275 uint8_t w3 = !chip->ic && !w1 && !w2;
1276 uint8_t w4 = ((chip->noise_lfsr & 1) == 0 || !w1) && !w3;
1277 if (!w1)
1278 {
1279 chip->noise_temp = (chip->noise_lfsr & 1) == 0;
1280 }
1281 chip->noise_lfsr >>= 1;
1282 chip->noise_lfsr |= w4 << 15;
1283 }
1284
1285 static inline void OPM_NoiseTimer(opm_t *chip)
1286 {
1287 uint32_t timer = chip->noise_timer;
1288
1289 chip->noise_update = chip->noise_timer_of;
1290
1291 if (chip->cycles % 16 == 15)
1292 {
1293 timer++;
1294 timer &= 31;
1295 }
1296 if (chip->ic || (chip->noise_timer_of && (chip->cycles % 16 == 15)))
1297 {
1298 timer = 0;
1299 }
1300
1301 chip->noise_timer_of = chip->noise_timer == (chip->noise_freq ^ 31);
1302 chip->noise_timer = timer;
1303 }
1304
1305 static inline void OPM_DoTimerA(opm_t *chip)
1306 {
1307 uint16_t value = chip->timer_a_val;
1308 value += chip->timer_a_inc;
1309 chip->timer_a_of = (value >> 10) & 1;
1310 if (chip->timer_a_do_reset)
1311 {
1312 value = 0;
1313 }
1314 if (chip->timer_a_do_load)
1315 {
1316 value = chip->timer_a_reg;
1317 }
1318
1319 chip->timer_a_val = value & 1023;
1320 }
1321
1322 static inline void OPM_DoTimerA2(opm_t *chip)
1323 {
1324 if (chip->cycles == 1)
1325 {
1326 chip->timer_a_load = chip->timer_loada;
1327 }
1328 chip->timer_a_inc = chip->mode_test[2] || (chip->timer_a_load && chip->cycles == 0);
1329 chip->timer_a_do_load = chip->timer_a_of || (chip->timer_a_load && chip->timer_a_temp);
1330 chip->timer_a_do_reset = chip->timer_a_temp;
1331 chip->timer_a_temp = !chip->timer_a_load;
1332 if (chip->timer_reseta || chip->ic)
1333 {
1334 chip->timer_a_status = 0;
1335 }
1336 else
1337 {
1338 chip->timer_a_status |= chip->timer_irqa && chip->timer_a_of;
1339 }
1340 chip->timer_reseta = 0;
1341 }
1342
1343 static inline void OPM_DoTimerB(opm_t *chip)
1344 {
1345 uint16_t value = chip->timer_b_val;
1346 value += chip->timer_b_inc;
1347 chip->timer_b_of = (value >> 8) & 1;
1348 if (chip->timer_b_do_reset)
1349 {
1350 value = 0;
1351 }
1352 if (chip->timer_b_do_load)
1353 {
1354 value = chip->timer_b_reg;
1355 }
1356
1357 chip->timer_b_val = value & 255;
1358
1359 if (chip->cycles == 0)
1360 {
1361 chip->timer_b_sub++;
1362 }
1363
1364 chip->timer_b_sub_of = (chip->timer_b_sub >> 4) & 1;
1365 chip->timer_b_sub &= 15;
1366 if (chip->ic)
1367 {
1368 chip->timer_b_sub = 0;
1369 }
1370 }
1371
1372 static inline void OPM_DoTimerB2(opm_t *chip)
1373 {
1374 chip->timer_b_inc = chip->mode_test[2] || (chip->timer_loadb && chip->timer_b_sub_of);
1375 chip->timer_b_do_load = chip->timer_b_of || (chip->timer_loadb && chip->timer_b_temp);
1376 chip->timer_b_do_reset = chip->timer_b_temp;
1377 chip->timer_b_temp = !chip->timer_loadb;
1378 if (chip->timer_resetb || chip->ic)
1379 {
1380 chip->timer_b_status = 0;
1381 }
1382 else
1383 {
1384 chip->timer_b_status |= chip->timer_irqb && chip->timer_b_of;
1385 }
1386 chip->timer_resetb = 0;
1387 }
1388
1389 static inline void OPM_DoTimerIRQ(opm_t *chip)
1390 {
1391 chip->timer_irq = chip->timer_a_status || chip->timer_b_status;
1392 }
1393
1394 static inline void OPM_DoLFOMult(opm_t *chip)
1395 {
1396 uint8_t ampm_sel = (chip->lfo_bit_counter & 8) != 0;
1397 uint8_t dp = ampm_sel ? chip->lfo_pmd : chip->lfo_amd;
1398 uint8_t bit = 0, b1, b2;
1399 uint8_t sum;
1400
1401 chip->lfo_out2_b = chip->lfo_out2;
1402
1403 switch (chip->lfo_bit_counter & 7)
1404 {
1405 case 0:
1406 bit = (dp & 64) != 0 && (chip->lfo_out1 & 64) == 0;
1407 break;
1408 case 1:
1409 bit = (dp & 32) != 0 && (chip->lfo_out1 & 32) == 0;
1410 break;
1411 case 2:
1412 bit = (dp & 16) != 0 && (chip->lfo_out1 & 16) == 0;
1413 break;
1414 case 3:
1415 bit = (dp & 8) != 0 && (chip->lfo_out1 & 8) == 0;
1416 break;
1417 case 4:
1418 bit = (dp & 4) != 0 && (chip->lfo_out1 & 4) == 0;
1419 break;
1420 case 5:
1421 bit = (dp & 2) != 0 && (chip->lfo_out1 & 2) == 0;
1422 break;
1423 case 6:
1424 bit = (dp & 1) != 0 && (chip->lfo_out1 & 1) == 0;
1425 break;
1426 }
1427
1428 b1 = (chip->lfo_out2 & 1) != 0;
1429 if ((chip->lfo_bit_counter & 7) == 0)
1430 {
1431 b1 = 0;
1432 }
1433 b2 = chip->lfo_mult_carry;
1434 if (chip->cycles % 16 == 15)
1435 {
1436 b2 = 0;
1437 }
1438 sum = bit + b1 + b2;
1439 chip->lfo_out2 >>= 1;
1440 chip->lfo_out2 |= (sum & 1) << 15;
1441 chip->lfo_mult_carry = sum >> 1;
1442 }
1443
1444 static inline void OPM_DoLFO1(opm_t *chip)
1445 {
1446 uint16_t counter2 = chip->lfo_counter2;
1447 uint8_t of_old = chip->lfo_counter2_of;
1448 uint8_t lfo_bit, noise, sum, carry, w[10];
1449 uint8_t lfo_pm_sign;
1450 uint8_t ampm_sel = (chip->lfo_bit_counter & 8) != 0;
1451 counter2 += (chip->lfo_counter1_of1 & 2) != 0 || chip->mode_test[3];
1452 chip->lfo_counter2_of = (counter2 >> 15) & 1;
1453 if (chip->ic)
1454 {
1455 counter2 = 0;
1456 }
1457 if (chip->lfo_counter2_load)
1458 {
1459 counter2 = lfo_counter2_table[chip->lfo_freq_hi];
1460 }
1461 chip->lfo_counter2 = counter2 & 32767;
1462 chip->lfo_counter2_load = chip->lfo_frq_update || of_old;
1463 chip->lfo_frq_update = 0;
1464 if ((chip->cycles % 16) == 12)
1465 {
1466 chip->lfo_counter1++;
1467 }
1468 chip->lfo_counter1_of1 <<= 1;
1469 chip->lfo_counter1_of1 |= (chip->lfo_counter1 >> 4) & 1;
1470 chip->lfo_counter1 &= 15;
1471 if (chip->ic)
1472 {
1473 chip->lfo_counter1 = 0;
1474 }
1475
1476 if ((chip->cycles & 15) == 5)
1477 {
1478 chip->lfo_counter2_of_lock2 = chip->lfo_counter2_of_lock;
1479 }
1480
1481 chip->lfo_counter3 += chip->lfo_counter3_clock;
1482 if (chip->ic)
1483 {
1484 chip->lfo_counter3 = 0;
1485 }
1486
1487 chip->lfo_counter3_clock = (chip->cycles & 15) == 13 && chip->lfo_counter2_of_lock2;
1488
1489 if ((chip->cycles & 15) == 15)
1490 {
1491 chip->lfo_trig_sign = (chip->lfo_val & 0x80) != 0;
1492 chip->lfo_saw_sign = (chip->lfo_val & 0x100) != 0;
1493 }
1494
1495 lfo_pm_sign = chip->lfo_wave == 2 ? chip->lfo_trig_sign : chip->lfo_saw_sign;
1496
1497 w[5] = ampm_sel ? chip->lfo_saw_sign : (chip->lfo_wave != 2 || !chip->lfo_trig_sign);
1498
1499 w[1] = !chip->lfo_clock || chip->lfo_wave == 3 || (chip->cycles & 15) != 15;
1500 w[2] = chip->lfo_wave == 2 && !w[1];
1501 w[4] = chip->lfo_clock_lock && chip->lfo_wave == 3;
1502 w[3] = !chip->ic && !chip->mode_test[1] && !w[4] && (chip->lfo_val & 0x8000) != 0;
1503
1504 w[7] = ((chip->cycles + 1) % 16) < 8;
1505
1506 w[6] = w[5] ^ w[3];
1507
1508 w[9] = ampm_sel ? ((chip->cycles % 16) == 6) : !chip->lfo_saw_sign;
1509
1510 w[8] = chip->lfo_wave == 1 ? w[9] : w[6];
1511
1512 w[8] &= w[7];
1513
1514 chip->lfo_out1 <<= 1;
1515 chip->lfo_out1 |= !w[8];
1516
1517 carry = !w[1] || ((chip->cycles & 15) != 15 && chip->lfo_val_carry != 0 && chip->lfo_wave != 3);
1518 sum = carry + w[2] + w[3];
1519 noise = chip->lfo_clock_lock && (chip->noise_lfsr & 1) != 0;
1520 lfo_bit = sum & 1;
1521 lfo_bit |= (chip->lfo_wave == 3) && noise;
1522 chip->lfo_val_carry = sum >> 1;
1523 chip->lfo_val <<= 1;
1524 chip->lfo_val |= lfo_bit;
1525
1526
1527 if (chip->cycles % 16 == 15 && (chip->lfo_bit_counter & 7) == 7)
1528 {
1529 if (ampm_sel)
1530 {
1531 chip->lfo_pm_lock = (chip->lfo_out2_b >> 8) & 255;
1532 chip->lfo_pm_lock ^= lfo_pm_sign << 7;
1533 }
1534 else
1535 {
1536 chip->lfo_am_lock = (chip->lfo_out2_b >> 8) & 255;
1537 }
1538 }
1539
1540 if ((chip->cycles & 15) == 14)
1541 {
1542 chip->lfo_bit_counter++;
1543 }
1544 if ((chip->cycles & 15) != 12 && chip->lfo_counter1_of2)
1545 {
1546 chip->lfo_bit_counter = 0;
1547 }
1548 chip->lfo_counter1_of2 = chip->lfo_counter1 == 2;
1549 }
1550
1551 static inline void OPM_DoLFO2(opm_t *chip)
1552 {
1553 chip->lfo_clock_test = chip->lfo_clock;
1554 chip->lfo_clock = (chip->lfo_counter2_of || chip->lfo_test || chip->lfo_counter3_step);
1555 if ((chip->cycles & 15) == 14)
1556 {
1557 chip->lfo_counter2_of_lock = chip->lfo_counter2_of;
1558 chip->lfo_clock_lock = chip->lfo_clock;
1559 }
1560 chip->lfo_counter3_step = 0;
1561 if (chip->lfo_counter3_clock)
1562 {
1563 if ((chip->lfo_counter3 & 1) == 0)
1564 {
1565 chip->lfo_counter3_step = (chip->lfo_freq_lo & 8) != 0;
1566 }
1567 else if ((chip->lfo_counter3 & 2) == 0)
1568 {
1569 chip->lfo_counter3_step = (chip->lfo_freq_lo & 4) != 0;
1570 }
1571 else if ((chip->lfo_counter3 & 4) == 0)
1572 {
1573 chip->lfo_counter3_step = (chip->lfo_freq_lo & 2) != 0;
1574 }
1575 else if ((chip->lfo_counter3 & 8) == 0)
1576 {
1577 chip->lfo_counter3_step = (chip->lfo_freq_lo & 1) != 0;
1578 }
1579 }
1580 chip->lfo_test = chip->mode_test[2];
1581 }
1582
1583 static inline void OPM_CSM(opm_t *chip)
1584 {
1585 chip->kon_csm = chip->kon_csm_lock;
1586 if (chip->cycles == 1)
1587 {
1588 chip->kon_csm_lock = chip->timer_a_do_load && chip->mode_csm;
1589 }
1590 }
1591
1592 static inline void OPM_NoiseChannel(opm_t *chip)
1593 {
1594 chip->nc_active |= chip->eg_serial_bit & 1;
1595 if (chip->cycles == 13)
1596 {
1597 chip->nc_active = 0;
1598 }
1599 chip->nc_out <<= 1;
1600 chip->nc_out |= chip->nc_sign ^ chip->eg_serial_bit;
1601 chip->nc_sign = !chip->nc_sign_lock;
1602 if (chip->cycles == 12)
1603 {
1604 chip->nc_active_lock = chip->nc_active;
1605 chip->nc_sign_lock2 = chip->nc_active_lock && !chip->nc_sign_lock;
1606 chip->nc_sign_lock = (chip->noise_lfsr & 1);
1607
1608 if (chip->noise_en)
1609 {
1610 if (chip->nc_sign_lock2)
1611 {
1612 chip->op_mix = ((chip->nc_out & ~1) << 2) | -4089;
1613 }
1614 else
1615 {
1616 chip->op_mix = ((chip->nc_out & ~1) << 2);
1617 }
1618 }
1619 }
1620 }
1621
1622 static inline void OPM_DoIO(opm_t *chip)
1623 {
1624 // Busy
1625 chip->write_busy_cnt += chip->write_busy;
1626 chip->write_busy = (!(chip->write_busy_cnt >> 5) && chip->write_busy && !chip->ic) | chip->write_d_en;
1627 chip->write_busy_cnt &= 0x1f;
1628 if (chip->ic)
1629 {
1630 chip->write_busy_cnt = 0;
1631 }
1632 // Write signal check
1633 chip->write_a_en = chip->write_a;
1634 chip->write_d_en = chip->write_d;
1635 chip->write_a = 0;
1636 chip->write_d = 0;
1637 }
1638
1639 static inline void OPM_DoRegWrite(opm_t *chip)
1640 {
1641 int32_t i;
1642 uint32_t channel = chip->cycles % 8;
1643 uint32_t slot = chip->cycles;
1644
1645 // Register write
1646 if (chip->reg_data_ready)
1647 {
1648 // Channel
1649 if ((chip->reg_address & 0xe7) == (0x20 | channel))
1650 {
1651 switch (chip->reg_address & 0x18)
1652 {
1653 case 0x00: // RL, FB, CONNECT
1654 chip->ch_rl[channel] = chip->reg_data >> 6;
1655 chip->ch_fb[channel] = (chip->reg_data >> 3) & 0x07;
1656 chip->ch_connect[channel] = chip->reg_data & 0x07;
1657 break;
1658 case 0x08: // KC
1659 chip->ch_kc[channel] = chip->reg_data & 0x7f;
1660 break;
1661 case 0x10: // KF
1662 chip->ch_kf[channel] = chip->reg_data >> 2;
1663 break;
1664 case 0x18: // PMS, AMS
1665 chip->ch_pms[channel] = (chip->reg_data >> 4) & 0x07;
1666 chip->ch_ams[channel] = chip->reg_data & 0x03;
1667 break;
1668 default:
1669 break;
1670 }
1671 }
1672 // Slot
1673 if ((chip->reg_address & 0x1f) == slot)
1674 {
1675 switch (chip->reg_address & 0xe0)
1676 {
1677 case 0x40: // DT1, MUL
1678 chip->sl_dt1[slot] = (chip->reg_data >> 4) & 0x07;
1679 chip->sl_mul[slot] = chip->reg_data & 0x0f;
1680 break;
1681 case 0x60: // TL
1682 chip->sl_tl[slot] = chip->reg_data & 0x7f;
1683 break;
1684 case 0x80: // KS, AR
1685 chip->sl_ks[slot] = chip->reg_data >> 6;
1686 chip->sl_ar[slot] = chip->reg_data & 0x1f;
1687 break;
1688 case 0xa0: // AMS-EN, D1R
1689 chip->sl_am_e[slot] = chip->reg_data >> 7;
1690 chip->sl_d1r[slot] = chip->reg_data & 0x1f;
1691 break;
1692 case 0xc0: // DT2, D2R
1693 chip->sl_dt2[slot] = chip->reg_data >> 6;
1694 chip->sl_d2r[slot] = chip->reg_data & 0x1f;
1695 break;
1696 case 0xe0: // D1L, RR
1697 chip->sl_d1l[slot] = chip->reg_data >> 4;
1698 chip->sl_rr[slot] = chip->reg_data & 0x0f;
1699 break;
1700 default:
1701 break;
1702 }
1703 }
1704 }
1705
1706 // Mode write
1707 if (chip->write_d_en)
1708 {
1709 switch (chip->mode_address)
1710 {
1711 case 0x01:
1712 for (i = 0; i < 8; i++)
1713 {
1714 chip->mode_test[i] = (chip->write_data >> i) & 0x01;
1715 }
1716 break;
1717 case 0x08:
1718 for (i = 0; i < 4; i++)
1719 {
1720 chip->mode_kon_operator[i] = (chip->write_data >> (i + 3)) & 0x01;
1721 }
1722 chip->mode_kon_channel = chip->write_data & 0x07;
1723 break;
1724 case 0x0f:
1725 chip->noise_en = chip->write_data >> 7;
1726 chip->noise_freq = chip->write_data & 0x1f;
1727 break;
1728 case 0x10:
1729 chip->timer_a_reg &= 0x03;
1730 chip->timer_a_reg |= chip->write_data << 2;
1731 break;
1732 case 0x11:
1733 chip->timer_a_reg &= 0x3fc;
1734 chip->timer_a_reg |= chip->write_data & 0x03;
1735 break;
1736 case 0x12:
1737 chip->timer_b_reg = chip->write_data;
1738 break;
1739 case 0x14:
1740 chip->mode_csm = (chip->write_data >> 7) & 1;
1741 chip->timer_irqb = (chip->write_data >> 3) & 1;
1742 chip->timer_irqa = (chip->write_data >> 2) & 1;
1743 chip->timer_resetb = (chip->write_data >> 5) & 1;
1744 chip->timer_reseta = (chip->write_data >> 4) & 1;
1745 chip->timer_loadb = (chip->write_data >> 1) & 1;
1746 chip->timer_loada = (chip->write_data >> 0) & 1;
1747 break;
1748 case 0x18:
1749 chip->lfo_freq_hi = chip->write_data >> 4;
1750 chip->lfo_freq_lo = chip->write_data & 0x0f;
1751 chip->lfo_frq_update = 1;
1752 break;
1753 case 0x19:
1754 if (chip->write_data & 0x80)
1755 {
1756 chip->lfo_pmd = chip->write_data & 0x7f;
1757 }
1758 else
1759 {
1760 chip->lfo_amd = chip->write_data;
1761 }
1762 break;
1763 case 0x1b:
1764 chip->lfo_wave = chip->write_data & 0x03;
1765 chip->io_ct1 = (chip->write_data >> 6) & 0x01;
1766 chip->io_ct2 = chip->write_data >> 7;
1767 break;
1768 }
1769 }
1770
1771 // Register data write
1772 chip->reg_data_ready = chip->reg_data_ready && !chip->write_a_en;
1773 if (chip->reg_address_ready && chip->write_d_en)
1774 {
1775 chip->reg_data = chip->write_data;
1776 chip->reg_data_ready = 1;
1777 }
1778
1779 // Register address write
1780 chip->reg_address_ready = chip->reg_address_ready && !chip->write_a_en;
1781 if (chip->write_a_en && (chip->write_data & 0xe0) != 0)
1782 {
1783 chip->reg_address = chip->write_data;
1784 chip->reg_address_ready = 1;
1785 }
1786 if (chip->write_a_en)
1787 {
1788 chip->mode_address = chip->write_data;
1789 }
1790 }
1791
1792 static inline void OPM_DoIC(opm_t *chip)
1793 {
1794 uint32_t channel = chip->cycles % 8;
1795 uint32_t slot = chip->cycles;
1796 if (chip->ic)
1797 {
1798 chip->ch_rl[channel] = 0;
1799 chip->ch_fb[channel] = 0;
1800 chip->ch_connect[channel] = 0;
1801 chip->ch_kc[channel] = 0;
1802 chip->ch_kf[channel] = 0;
1803 chip->ch_pms[channel] = 0;
1804 chip->ch_ams[channel] = 0;
1805
1806 chip->sl_dt1[slot] = 0;
1807 chip->sl_mul[slot] = 0;
1808 chip->sl_tl[slot] = 0;
1809 chip->sl_ks[slot] = 0;
1810 chip->sl_ar[slot] = 0;
1811 chip->sl_am_e[slot] = 0;
1812 chip->sl_d1r[slot] = 0;
1813 chip->sl_dt2[slot] = 0;
1814 chip->sl_d2r[slot] = 0;
1815 chip->sl_d1l[slot] = 0;
1816 chip->sl_rr[slot] = 0;
1817
1818 chip->timer_a_reg = 0;
1819 chip->timer_b_reg = 0;
1820 chip->timer_irqa = 0;
1821 chip->timer_irqb = 0;
1822 chip->timer_loada = 0;
1823 chip->timer_loadb = 0;
1824 chip->mode_csm = 0;
1825
1826 chip->mode_test[0] = 0;
1827 chip->mode_test[1] = 0;
1828 chip->mode_test[2] = 0;
1829 chip->mode_test[3] = 0;
1830 chip->mode_test[4] = 0;
1831 chip->mode_test[5] = 0;
1832 chip->mode_test[6] = 0;
1833 chip->mode_test[7] = 0;
1834 chip->noise_en = 0;
1835 chip->noise_freq = 0;
1836
1837 chip->mode_kon_channel = 0;
1838 chip->mode_kon_operator[0] = 0;
1839 chip->mode_kon_operator[1] = 0;
1840 chip->mode_kon_operator[2] = 0;
1841 chip->mode_kon_operator[3] = 0;
1842 chip->mode_kon[(slot + 8) % 32] = 0;
1843
1844 chip->lfo_pmd = 0;
1845 chip->lfo_amd = 0;
1846 chip->lfo_wave = 0;
1847 chip->lfo_freq_hi = 0;
1848 chip->lfo_freq_lo = 0;
1849
1850 chip->io_ct1 = 0;
1851 chip->io_ct2 = 0;
1852
1853 chip->reg_address = 0;
1854 chip->reg_data = 0;
1855 }
1856 chip->ic2 = chip->ic;
1857 }
1858
1859 void OPM_Clock(opm_t *chip, int32_t *output, uint8_t *sh1, uint8_t *sh2, uint8_t *so)
1860 {
1861 OPM_Mixer2(chip);
1862 OPM_Mixer(chip);
1863
1864 OPM_OperatorPhase16(chip);
1865 OPM_OperatorPhase15(chip);
1866 OPM_OperatorPhase14(chip);
1867 OPM_OperatorPhase13(chip);
1868 OPM_OperatorPhase12(chip);
1869 OPM_OperatorPhase11(chip);
1870 OPM_OperatorPhase10(chip);
1871 OPM_OperatorPhase9(chip);
1872 OPM_OperatorPhase8(chip);
1873 OPM_OperatorPhase7(chip);
1874 OPM_OperatorPhase6(chip);
1875 OPM_OperatorPhase5(chip);
1876 OPM_OperatorPhase4(chip);
1877 OPM_OperatorPhase3(chip);
1878 OPM_OperatorPhase2(chip);
1879 OPM_OperatorPhase1(chip);
1880 OPM_OperatorCounter(chip);
1881
1882 OPM_EnvelopeTimer(chip);
1883 OPM_EnvelopePhase6(chip);
1884 OPM_EnvelopePhase5(chip);
1885 OPM_EnvelopePhase4(chip);
1886 OPM_EnvelopePhase3(chip);
1887 OPM_EnvelopePhase2(chip);
1888 OPM_EnvelopePhase1(chip);
1889
1890 OPM_PhaseDebug(chip);
1891 OPM_PhaseGenerate(chip);
1892 OPM_PhaseCalcIncrement(chip);
1893 OPM_PhaseCalcFNumBlock(chip);
1894
1895 OPM_DoTimerIRQ(chip);
1896 OPM_DoTimerA(chip);
1897 OPM_DoTimerB(chip);
1898 OPM_DoLFOMult(chip);
1899 OPM_DoLFO1(chip);
1900 OPM_Noise(chip);
1901 OPM_KeyOn2(chip);
1902 OPM_DoRegWrite(chip);
1903 OPM_EnvelopeClock(chip);
1904 OPM_NoiseTimer(chip);
1905 OPM_KeyOn1(chip);
1906 OPM_DoIO(chip);
1907 OPM_DoTimerA2(chip);
1908 OPM_DoTimerB2(chip);
1909 OPM_DoLFO2(chip);
1910 OPM_CSM(chip);
1911 OPM_NoiseChannel(chip);
1912 OPM_Output(chip);
1913 OPM_DAC(chip);
1914 OPM_DoIC(chip);
1915 if (sh1)
1916 {
1917 *sh1 = chip->smp_sh1;
1918 }
1919 if (sh2)
1920 {
1921 *sh2 = chip->smp_sh2;
1922 }
1923 if (so)
1924 {
1925 *so = chip->smp_so;
1926 }
1927 if (output)
1928 {
1929 output[0] = chip->dac_output[0];
1930 output[1] = chip->dac_output[1];
1931 }
1932 chip->cycles = (chip->cycles + 1) % 32;
1933 }
1934
1935 void OPM_Write(opm_t *chip, uint32_t port, uint8_t data)
1936 {
1937 chip->write_data = data;
1938 if (chip->ic)
1939 {
1940 return;
1941 }
1942 if (port & 0x01)
1943 {
1944 chip->write_d = 1;
1945 }
1946 else
1947 {
1948 chip->write_a = 1;
1949 }
1950 }
1951
1952 uint8_t OPM_Read(opm_t *chip, uint32_t port)
1953 {
1954 uint16_t testdata;
1955 if (chip->mode_test[6])
1956 {
1957 testdata = chip->op_out[5] | ((chip->eg_serial_bit ^ 1) << 14) | ((chip->pg_serial & 1) << 15);
1958 if (chip->mode_test[7])
1959 {
1960 return testdata & 255;
1961 }
1962 else
1963 {
1964 return testdata >> 8;
1965 }
1966 }
1967 return (chip->write_busy << 7) | (chip->timer_b_status << 1) | chip->timer_a_status;
1968 }
1969
1970 uint8_t OPM_ReadIRQ(opm_t *chip)
1971 {
1972 return chip->timer_irq;
1973 }
1974
1975 uint8_t OPM_ReadCT1(opm_t *chip)
1976 {
1977 if(chip->mode_test[3])
1978 {
1979 return chip->lfo_clock_test;
1980 }
1981 return chip->io_ct1;
1982 }
1983
1984 uint8_t OPM_ReadCT2(opm_t *chip)
1985 {
1986 return chip->io_ct2;
1987 }
1988
1989 void OPM_SetIC(opm_t *chip, uint8_t ic)
1990 {
1991 if (chip->ic != ic)
1992 {
1993 chip->ic = ic;
1994 if (!ic)
1995 {
1996 chip->cycles = 0;
1997 }
1998 }
1999 }
2000
2001 void OPM_Reset(opm_t *chip)
2002 {
2003 uint32_t i;
2004 memset(chip, 0, sizeof(opm_t));
2005 OPM_SetIC(chip, 1);
2006 for (i = 0; i < 32 * 64; i++)
2007 {
2008 OPM_Clock(chip, NULL, NULL, NULL, NULL);
2009 }
2010 OPM_SetIC(chip, 0);
2011 }