openMSX
YM2151.cc
Go to the documentation of this file.
1/*****************************************************************************
2*
3* Yamaha YM2151 driver (version 2.150 final beta)
4*
5******************************************************************************/
6
7#include "YM2151.hh"
8
9#include "DeviceConfig.hh"
10#include "serialize.hh"
11
12#include "Math.hh"
13#include "cstd.hh"
14#include "enumerate.hh"
15#include "narrow.hh"
16#include "one_of.hh"
17#include "ranges.hh"
18#include "xrange.hh"
19
20#include <array>
21#include <cassert>
22#include <cmath>
23#include <cstring>
24#include <iostream>
25
26namespace openmsx {
27
28// TODO void ym2151WritePortCallback(void* ref, unsigned port, uint8_t value);
29
30static constexpr int FREQ_SH = 16; // 16.16 fixed point (frequency calculations)
31
32static constexpr int FREQ_MASK = (1 << FREQ_SH) - 1;
33
34static constexpr int ENV_BITS = 10;
35static constexpr int ENV_LEN = 1 << ENV_BITS;
36static constexpr double ENV_STEP = 128.0 / ENV_LEN;
37
38static constexpr int MAX_ATT_INDEX = ENV_LEN - 1; // 1023
39static constexpr int MIN_ATT_INDEX = 0;
40
41static constexpr unsigned EG_ATT = 4;
42static constexpr unsigned EG_DEC = 3;
43static constexpr unsigned EG_SUS = 2;
44static constexpr unsigned EG_REL = 1;
45static constexpr unsigned EG_OFF = 0;
46
47static constexpr int SIN_BITS = 10;
48static constexpr int SIN_LEN = 1 << SIN_BITS;
49static constexpr int SIN_MASK = SIN_LEN - 1;
50
51static constexpr int TL_RES_LEN = 256; // 8 bits addressing (real chip)
52
53// TL_TAB_LEN is calculated as:
54// 13 - sinus amplitude bits (Y axis)
55// 2 - sinus sign bit (Y axis)
56// TL_RES_LEN - sinus resolution (X axis)
57static constexpr unsigned TL_TAB_LEN = 13 * 2 * TL_RES_LEN;
58static constexpr auto tl_tab = [] {
59 std::array<int, TL_TAB_LEN> result = {};
60 for (auto x : xrange(TL_RES_LEN)) {
61 double m = (1 << 16) / cstd::exp2<6>((x + 1) * (ENV_STEP / 4.0) / 8.0);
62
63 // we never reach (1 << 16) here due to the (x + 1)
64 // result fits within 16 bits at maximum
65
66 auto n = int(m); // 16 bits here
67 n >>= 4; // 12 bits here
68 if (n & 1) { // round to closest
69 n = (n >> 1) + 1;
70 } else {
71 n = n >> 1;
72 }
73 // 11 bits here (rounded)
74 n <<= 2; // 13 bits here (as in real chip)
75 result[x * 2 + 0] = n;
76 result[x * 2 + 1] = -result[x * 2 + 0];
77
78 for (auto i : xrange(1, 13)) {
79 result[x * 2 + 0 + i * 2 * TL_RES_LEN] = result[x * 2 + 0] >> i;
80 result[x * 2 + 1 + i * 2 * TL_RES_LEN] = -result[x * 2 + 0 + i * 2 * TL_RES_LEN];
81 }
82 }
83 return result;
84}();
85
86static constexpr unsigned ENV_QUIET = TL_TAB_LEN >> 3;
87
88// sin waveform table in 'decibel' scale
89static constexpr auto sin_tab = [] {
90 std::array<unsigned, SIN_LEN> result = {};
91 for (auto i : xrange(SIN_LEN)) {
92 // non-standard sinus
93 double m = cstd::sin<2>((i * 2 + 1) * Math::pi / SIN_LEN); // verified on the real chip
94
95 // we never reach zero here due to (i * 2 + 1)
96 double o = -8.0 * cstd::log2<8, 3>(cstd::abs(m)); // convert to decibels
97 o = o / (ENV_STEP / 4);
98
99 auto n = int(2.0 * o);
100 if (n & 1) { // round to closest
101 n = (n >> 1) + 1;
102 } else {
103 n = n >> 1;
104 }
105 result[i] = n * 2 + (m >= 0.0 ? 0 : 1);
106 }
107 return result;
108}();
109
110
111// translate from D1L to volume index (16 D1L levels)
112static constexpr auto d1l_tab = [] {
113 std::array<unsigned, 16> result = {};
114 //for (auto [i, r] : enumerate(result)) { msvc bug
115 for (int i = 0; i < 16; ++i) {
116 // every 3 'dB' except for all bits = 1 = 45+48 'dB'
117 result[i] = unsigned((i != 15 ? i : i + 16) * (4.0 / ENV_STEP));
118 }
119 return result;
120}();
121
122
123static constexpr unsigned RATE_STEPS = 8;
124static constexpr std::array<uint8_t, 19 * RATE_STEPS> eg_inc = {
125
126//cycle:0 1 2 3 4 5 6 7
127
128/* 0 */ 0,1, 0,1, 0,1, 0,1, // rates 00..11 0 (increment by 0 or 1)
129/* 1 */ 0,1, 0,1, 1,1, 0,1, // rates 00..11 1
130/* 2 */ 0,1, 1,1, 0,1, 1,1, // rates 00..11 2
131/* 3 */ 0,1, 1,1, 1,1, 1,1, // rates 00..11 3
132
133/* 4 */ 1,1, 1,1, 1,1, 1,1, // rate 12 0 (increment by 1)
134/* 5 */ 1,1, 1,2, 1,1, 1,2, // rate 12 1
135/* 6 */ 1,2, 1,2, 1,2, 1,2, // rate 12 2
136/* 7 */ 1,2, 2,2, 1,2, 2,2, // rate 12 3
137
138/* 8 */ 2,2, 2,2, 2,2, 2,2, // rate 13 0 (increment by 2)
139/* 9 */ 2,2, 2,4, 2,2, 2,4, // rate 13 1
140/*10 */ 2,4, 2,4, 2,4, 2,4, // rate 13 2
141/*11 */ 2,4, 4,4, 2,4, 4,4, // rate 13 3
142
143/*12 */ 4,4, 4,4, 4,4, 4,4, // rate 14 0 (increment by 4)
144/*13 */ 4,4, 4,8, 4,4, 4,8, // rate 14 1
145/*14 */ 4,8, 4,8, 4,8, 4,8, // rate 14 2
146/*15 */ 4,8, 8,8, 4,8, 8,8, // rate 14 3
147
148/*16 */ 8,8, 8,8, 8,8, 8,8, // rates 15 0, 15 1, 15 2, 15 3 (increment by 8)
149/*17 */ 16,16,16,16,16,16,16,16, // rates 15 2, 15 3 for attack
150/*18 */ 0,0, 0,0, 0,0, 0,0, // infinity rates for attack and decay(s)
151};
152
153
154static constexpr uint8_t O(int a) { return narrow<uint8_t>(a * RATE_STEPS); }
155// note that there is no O(17) in this table - it's directly in the code
156static constexpr std::array<uint8_t, 32 + 64 + 32> eg_rate_select {
157// Envelope Generator rates (32 + 64 rates + 32 RKS)
158// 32 dummy (infinite time) rates
159O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
160O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
161O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
162O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
163
164// rates 00-11
165O( 0),O( 1),O( 2),O( 3),
166O( 0),O( 1),O( 2),O( 3),
167O( 0),O( 1),O( 2),O( 3),
168O( 0),O( 1),O( 2),O( 3),
169O( 0),O( 1),O( 2),O( 3),
170O( 0),O( 1),O( 2),O( 3),
171O( 0),O( 1),O( 2),O( 3),
172O( 0),O( 1),O( 2),O( 3),
173O( 0),O( 1),O( 2),O( 3),
174O( 0),O( 1),O( 2),O( 3),
175O( 0),O( 1),O( 2),O( 3),
176O( 0),O( 1),O( 2),O( 3),
177
178// rate 12
179O( 4),O( 5),O( 6),O( 7),
180
181// rate 13
182O( 8),O( 9),O(10),O(11),
183
184// rate 14
185O(12),O(13),O(14),O(15),
186
187// rate 15
188O(16),O(16),O(16),O(16),
189
190// 32 dummy rates (same as 15 3)
191O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
192O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
193O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
194O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16)
195};
196
197// rate 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
198// shift 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0
199// mask 2047, 1023, 511, 255, 127, 63, 31, 15, 7, 3, 1, 0, 0, 0, 0, 0
200static constexpr std::array<uint8_t, 32 + 64 + 32> eg_rate_shift = {
201// Envelope Generator counter shifts (32 + 64 rates + 32 RKS)
202// 32 infinite time rates
203 0, 0, 0, 0, 0, 0, 0, 0,
204 0, 0, 0, 0, 0, 0, 0, 0,
205 0, 0, 0, 0, 0, 0, 0, 0,
206 0, 0, 0, 0, 0, 0, 0, 0,
207
208// rates 00-11
209 11, 11, 11, 11,
210 10, 10, 10, 10,
211 9, 9, 9, 9,
212 8, 8, 8, 8,
213 7, 7, 7, 7,
214 6, 6, 6, 6,
215 5, 5, 5, 5,
216 4, 4, 4, 4,
217 3, 3, 3, 3,
218 2, 2, 2, 2,
219 1, 1, 1, 1,
220 0, 0, 0, 0,
221
222// rate 12
223 0, 0, 0, 0,
224
225// rate 13
226 0, 0, 0, 0,
227
228// rate 14
229 0, 0, 0, 0,
230
231// rate 15
232 0, 0, 0, 0,
233
234// 32 dummy rates (same as 15 3)
235 0, 0, 0, 0, 0, 0, 0, 0,
236 0, 0, 0, 0, 0, 0, 0, 0,
237 0, 0, 0, 0, 0, 0, 0, 0,
238 0, 0, 0, 0, 0, 0, 0, 0
239};
240
241// DT2 defines offset in cents from base note
242//
243// This table defines offset in frequency-deltas table.
244// User's Manual page 22
245//
246// Values below were calculated using formula: value = orig.val / 1.5625
247//
248// DT2=0 DT2=1 DT2=2 DT2=3
249// 0 600 781 950
250static constexpr std::array<unsigned, 4> dt2_tab = {0, 384, 500, 608};
251
252// DT1 defines offset in Hertz from base note
253// This table is converted while initialization...
254// Detune table shown in YM2151 User's Manual is wrong (verified on the real chip)
255static constexpr std::array<uint8_t, 4 * 32> dt1_tab = {
256// DT1 = 0
257 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
258 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
259
260// DT1 = 1
261 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
262 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 8, 8, 8, 8,
263
264// DT1 = 2
265 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5,
266 5, 6, 6, 7, 8, 8, 9,10,11,12,13,14,16,16,16,16,
267
268// DT1 = 3
269 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7,
270 8, 8, 9,10,11,12,13,14,16,17,19,20,22,22,22,22
271};
272
273static constexpr std::array<uint16_t, 768> phaseInc_rom = {
2741299,1300,1301,1302,1303,1304,1305,1306,1308,1309,1310,1311,1313,1314,1315,1316,
2751318,1319,1320,1321,1322,1323,1324,1325,1327,1328,1329,1330,1332,1333,1334,1335,
2761337,1338,1339,1340,1341,1342,1343,1344,1346,1347,1348,1349,1351,1352,1353,1354,
2771356,1357,1358,1359,1361,1362,1363,1364,1366,1367,1368,1369,1371,1372,1373,1374,
2781376,1377,1378,1379,1381,1382,1383,1384,1386,1387,1388,1389,1391,1392,1393,1394,
2791396,1397,1398,1399,1401,1402,1403,1404,1406,1407,1408,1409,1411,1412,1413,1414,
2801416,1417,1418,1419,1421,1422,1423,1424,1426,1427,1429,1430,1431,1432,1434,1435,
2811437,1438,1439,1440,1442,1443,1444,1445,1447,1448,1449,1450,1452,1453,1454,1455,
2821458,1459,1460,1461,1463,1464,1465,1466,1468,1469,1471,1472,1473,1474,1476,1477,
2831479,1480,1481,1482,1484,1485,1486,1487,1489,1490,1492,1493,1494,1495,1497,1498,
2841501,1502,1503,1504,1506,1507,1509,1510,1512,1513,1514,1515,1517,1518,1520,1521,
2851523,1524,1525,1526,1528,1529,1531,1532,1534,1535,1536,1537,1539,1540,1542,1543,
2861545,1546,1547,1548,1550,1551,1553,1554,1556,1557,1558,1559,1561,1562,1564,1565,
2871567,1568,1569,1570,1572,1573,1575,1576,1578,1579,1580,1581,1583,1584,1586,1587,
2881590,1591,1592,1593,1595,1596,1598,1599,1601,1602,1604,1605,1607,1608,1609,1610,
2891613,1614,1615,1616,1618,1619,1621,1622,1624,1625,1627,1628,1630,1631,1632,1633,
2901637,1638,1639,1640,1642,1643,1645,1646,1648,1649,1651,1652,1654,1655,1656,1657,
2911660,1661,1663,1664,1666,1667,1669,1670,1672,1673,1675,1676,1678,1679,1681,1682,
2921685,1686,1688,1689,1691,1692,1694,1695,1697,1698,1700,1701,1703,1704,1706,1707,
2931709,1710,1712,1713,1715,1716,1718,1719,1721,1722,1724,1725,1727,1728,1730,1731,
2941734,1735,1737,1738,1740,1741,1743,1744,1746,1748,1749,1751,1752,1754,1755,1757,
2951759,1760,1762,1763,1765,1766,1768,1769,1771,1773,1774,1776,1777,1779,1780,1782,
2961785,1786,1788,1789,1791,1793,1794,1796,1798,1799,1801,1802,1804,1806,1807,1809,
2971811,1812,1814,1815,1817,1819,1820,1822,1824,1825,1827,1828,1830,1832,1833,1835,
2981837,1838,1840,1841,1843,1845,1846,1848,1850,1851,1853,1854,1856,1858,1859,1861,
2991864,1865,1867,1868,1870,1872,1873,1875,1877,1879,1880,1882,1884,1885,1887,1888,
3001891,1892,1894,1895,1897,1899,1900,1902,1904,1906,1907,1909,1911,1912,1914,1915,
3011918,1919,1921,1923,1925,1926,1928,1930,1932,1933,1935,1937,1939,1940,1942,1944,
3021946,1947,1949,1951,1953,1954,1956,1958,1960,1961,1963,1965,1967,1968,1970,1972,
3031975,1976,1978,1980,1982,1983,1985,1987,1989,1990,1992,1994,1996,1997,1999,2001,
3042003,2004,2006,2008,2010,2011,2013,2015,2017,2019,2021,2022,2024,2026,2028,2029,
3052032,2033,2035,2037,2039,2041,2043,2044,2047,2048,2050,2052,2054,2056,2058,2059,
3062062,2063,2065,2067,2069,2071,2073,2074,2077,2078,2080,2082,2084,2086,2088,2089,
3072092,2093,2095,2097,2099,2101,2103,2104,2107,2108,2110,2112,2114,2116,2118,2119,
3082122,2123,2125,2127,2129,2131,2133,2134,2137,2139,2141,2142,2145,2146,2148,2150,
3092153,2154,2156,2158,2160,2162,2164,2165,2168,2170,2172,2173,2176,2177,2179,2181,
3102185,2186,2188,2190,2192,2194,2196,2197,2200,2202,2204,2205,2208,2209,2211,2213,
3112216,2218,2220,2222,2223,2226,2227,2230,2232,2234,2236,2238,2239,2242,2243,2246,
3122249,2251,2253,2255,2256,2259,2260,2263,2265,2267,2269,2271,2272,2275,2276,2279,
3132281,2283,2285,2287,2288,2291,2292,2295,2297,2299,2301,2303,2304,2307,2308,2311,
3142315,2317,2319,2321,2322,2325,2326,2329,2331,2333,2335,2337,2338,2341,2342,2345,
3152348,2350,2352,2354,2355,2358,2359,2362,2364,2366,2368,2370,2371,2374,2375,2378,
3162382,2384,2386,2388,2389,2392,2393,2396,2398,2400,2402,2404,2407,2410,2411,2414,
3172417,2419,2421,2423,2424,2427,2428,2431,2433,2435,2437,2439,2442,2445,2446,2449,
3182452,2454,2456,2458,2459,2462,2463,2466,2468,2470,2472,2474,2477,2480,2481,2484,
3192488,2490,2492,2494,2495,2498,2499,2502,2504,2506,2508,2510,2513,2516,2517,2520,
3202524,2526,2528,2530,2531,2534,2535,2538,2540,2542,2544,2546,2549,2552,2553,2556,
3212561,2563,2565,2567,2568,2571,2572,2575,2577,2579,2581,2583,2586,2589,2590,2593
322};
323
324// Frequency-deltas to get the closest frequency possible.
325// There are 11 octaves because of DT2 (max 950 cents over base frequency)
326// and LFO phase modulation (max 800 cents below AND over base frequency)
327// Summary: octave explanation
328// 0 note code - LFO PM
329// 1 note code
330// 2 note code
331// 3 note code
332// 4 note code
333// 5 note code
334// 6 note code
335// 7 note code
336// 8 note code
337// 9 note code + DT2 + LFO PM
338// 10 note code + DT2 + LFO PM
339static constexpr auto freq = [] {
340 std::array<unsigned, 11 * 768> result = {}; // 11 octaves, 768 'cents' per octave
341
342 // this loop calculates Hertz values for notes from c-0 to b-7
343 // including 64 'cents' (100/64 that is 1.5625 of real cent) per note
344 // i*100/64/1200 is equal to i/768
345
346 // real chip works with 10 bits fixed point values (10.10)
347 // -10 because phaseInc_rom table values are already in 10.10 format
348 double mult = 1 << (FREQ_SH - 10);
349
350 for (auto i : xrange(768)) {
351 double phaseInc = phaseInc_rom[i]; // real chip phase increment
352
353 // octave 2 - reference octave
354 // adjust to X.10 fixed point
355 result[768 + 2 * 768 + i] = int(phaseInc * mult) & 0xffffffc0;
356 // octave 0 and octave 1
357 for (auto j : xrange(2)) {
358 // adjust to X.10 fixed point
359 result[768 + j * 768 + i] = (result[768 + 2 * 768 + i] >> (2 - j)) & 0xffffffc0;
360 }
361 // octave 3 to 7
362 for (auto j : xrange(3, 8)) {
363 result[768 + j * 768 + i] = result[768 + 2 * 768 + i] << (j - 2);
364 }
365 }
366
367 // octave -1 (all equal to: oct 0, _KC_00_, _KF_00_)
368 for (auto i : xrange(768)) {
369 result[0 * 768 + i] = result[1 * 768 + 0];
370 }
371
372 // octave 8 and 9 (all equal to: oct 7, _KC_14_, _KF_63_)
373 for (auto j : xrange(8, 10)) {
374 for (auto i : xrange(768)) {
375 result[768 + j * 768 + i] = result[768 + 8 * 768 - 1];
376 }
377 }
378 return result;
379}();
380
381// Frequency deltas for DT1. These deltas alter operator frequency
382// after it has been taken from frequency-deltas table.
383static constexpr auto dt1_freq = [] {
384 std::array<int, 8 * 32> result = {}; // 8 DT1 levels, 32 KC values
385 double mult = 1 << FREQ_SH;
386 for (auto j : xrange(4)) {
387 for (auto i : xrange(32)) {
388 // calculate phase increment
389 double phaseInc = double(dt1_tab[j * 32 + i]) / (1 << 20) * SIN_LEN;
390
391 // positive and negative values
392 result[(j + 0) * 32 + i] = int(phaseInc * mult);
393 result[(j + 4) * 32 + i] = -result[(j + 0) * 32 + i];
394 }
395 }
396 return result;
397}();
398
399// This table tells how many cycles/samples it takes before noise is recalculated.
400// 2/2 means every cycle/sample, 2/5 means 2 out of 5 cycles/samples, etc.
401static constexpr auto noise_tab = [] {
402 std::array<int, 32> result = {}; // 17bit Noise Generator periods
403 //for (auto [i, r] : enumerate(result)) { msvc bug
404 for (int i = 0; i < 32; ++i) {
405 result[i] = 32 - (i != 31 ? i : 30); // rate 30 and 31 are the same
406 }
407 return result;
408}();
409
410// Noise LFO waveform.
411//
412// Here are just 256 samples out of much longer data.
413//
414// It does NOT repeat every 256 samples on real chip and I wasn't able to find
415// the point where it repeats (even in strings as long as 131072 samples).
416//
417// I only put it here because its better than nothing and perhaps
418// someone might be able to figure out the real algorithm.
419//
420// Note that (due to the way the LFO output is calculated) it is quite
421// possible that two values: 0x80 and 0x00 might be wrong in this table.
422// To be exact:
423// some 0x80 could be 0x81 as well as some 0x00 could be 0x01.
424static constexpr std::array<uint8_t, 256> lfo_noise_waveform = {
4250xFF,0xEE,0xD3,0x80,0x58,0xDA,0x7F,0x94,0x9E,0xE3,0xFA,0x00,0x4D,0xFA,0xFF,0x6A,
4260x7A,0xDE,0x49,0xF6,0x00,0x33,0xBB,0x63,0x91,0x60,0x51,0xFF,0x00,0xD8,0x7F,0xDE,
4270xDC,0x73,0x21,0x85,0xB2,0x9C,0x5D,0x24,0xCD,0x91,0x9E,0x76,0x7F,0x20,0xFB,0xF3,
4280x00,0xA6,0x3E,0x42,0x27,0x69,0xAE,0x33,0x45,0x44,0x11,0x41,0x72,0x73,0xDF,0xA2,
429
4300x32,0xBD,0x7E,0xA8,0x13,0xEB,0xD3,0x15,0xDD,0xFB,0xC9,0x9D,0x61,0x2F,0xBE,0x9D,
4310x23,0x65,0x51,0x6A,0x84,0xF9,0xC9,0xD7,0x23,0xBF,0x65,0x19,0xDC,0x03,0xF3,0x24,
4320x33,0xB6,0x1E,0x57,0x5C,0xAC,0x25,0x89,0x4D,0xC5,0x9C,0x99,0x15,0x07,0xCF,0xBA,
4330xC5,0x9B,0x15,0x4D,0x8D,0x2A,0x1E,0x1F,0xEA,0x2B,0x2F,0x64,0xA9,0x50,0x3D,0xAB,
434
4350x50,0x77,0xE9,0xC0,0xAC,0x6D,0x3F,0xCA,0xCF,0x71,0x7D,0x80,0xA6,0xFD,0xFF,0xB5,
4360xBD,0x6F,0x24,0x7B,0x00,0x99,0x5D,0xB1,0x48,0xB0,0x28,0x7F,0x80,0xEC,0xBF,0x6F,
4370x6E,0x39,0x90,0x42,0xD9,0x4E,0x2E,0x12,0x66,0xC8,0xCF,0x3B,0x3F,0x10,0x7D,0x79,
4380x00,0xD3,0x1F,0x21,0x93,0x34,0xD7,0x19,0x22,0xA2,0x08,0x20,0xB9,0xB9,0xEF,0x51,
439
4400x99,0xDE,0xBF,0xD4,0x09,0x75,0xE9,0x8A,0xEE,0xFD,0xE4,0x4E,0x30,0x17,0xDF,0xCE,
4410x11,0xB2,0x28,0x35,0xC2,0x7C,0x64,0xEB,0x91,0x5F,0x32,0x0C,0x6E,0x00,0xF9,0x92,
4420x19,0xDB,0x8F,0xAB,0xAE,0xD6,0x12,0xC4,0x26,0x62,0xCE,0xCC,0x0A,0x03,0xE7,0xDD,
4430xE2,0x4D,0x8A,0xA6,0x46,0x95,0x0F,0x8F,0xF5,0x15,0x97,0x32,0xD4,0x28,0x1E,0x55
444};
445
446void YM2151::keyOn(YM2151Operator& op, unsigned keySet) const
447{
448 if (!op.key) {
449 op.phase = 0; /* clear phase */
450 op.state = EG_ATT; /* KEY ON = attack */
451 op.volume += (~op.volume *
452 (eg_inc[op.eg_sel_ar + ((eg_cnt >> op.eg_sh_ar)&7)])
453 ) >>4;
454 if (op.volume <= MIN_ATT_INDEX) {
455 op.volume = MIN_ATT_INDEX;
456 op.state = EG_DEC;
457 }
458 }
459 op.key |= keySet;
460}
461
462void YM2151::keyOff(YM2151Operator& op, unsigned keyClear) const
463{
464 if (op.key) {
465 op.key &= keyClear;
466 if (!op.key && (op.state > EG_REL)) {
467 op.state = EG_REL; // KEY OFF = release
468 }
469 }
470}
471
472void YM2151::envelopeKONKOFF(std::span<YM2151Operator, 4> op, int v) const
473{
474 if (v & 0x08) { // M1
475 keyOn (op[0], 1);
476 } else {
477 keyOff(op[0], unsigned(~1));
478 }
479 if (v & 0x20) { // M2
480 keyOn (op[1], 1);
481 } else {
482 keyOff(op[1], unsigned(~1));
483 }
484 if (v & 0x10) { // C1
485 keyOn (op[2], 1);
486 } else {
487 keyOff(op[2], unsigned(~1));
488 }
489 if (v & 0x40) { // C2
490 keyOn (op[3], 1);
491 } else {
492 keyOff(op[3], unsigned(~1));
493 }
494}
495
496void YM2151::setConnect(std::span<YM2151Operator, 4> o, int cha, int v)
497{
498 YM2151Operator& om1 = o[0];
499 YM2151Operator& om2 = o[1];
500 YM2151Operator& oc1 = o[2];
501
502 // set connect algorithm
503 // MEM is simply one sample delay
504 switch (v & 7) {
505 case 0:
506 // M1---C1---MEM---M2---C2---OUT
507 om1.connect = &c1;
508 oc1.connect = &mem;
509 om2.connect = &c2;
510 om1.mem_connect = &m2;
511 break;
512
513 case 1:
514 // M1------+-MEM---M2---C2---OUT
515 // C1-+
516 om1.connect = &mem;
517 oc1.connect = &mem;
518 om2.connect = &c2;
519 om1.mem_connect = &m2;
520 break;
521
522 case 2:
523 // M1-----------------+-C2---OUT
524 // C1---MEM---M2-+
525 om1.connect = &c2;
526 oc1.connect = &mem;
527 om2.connect = &c2;
528 om1.mem_connect = &m2;
529 break;
530
531 case 3:
532 // M1---C1---MEM------+-C2---OUT
533 // M2-+
534 om1.connect = &c1;
535 oc1.connect = &mem;
536 om2.connect = &c2;
537 om1.mem_connect = &c2;
538 break;
539
540 case 4:
541 // M1---C1-+-OUT
542 // M2---C2-+
543 // MEM: not used
544 om1.connect = &c1;
545 oc1.connect = &chanOut[cha];
546 om2.connect = &c2;
547 om1.mem_connect = &mem; // store it anywhere where it will not be used
548 break;
549
550 case 5:
551 // +----C1----+
552 // M1-+-MEM---M2-+-OUT
553 // +----C2----+
554 om1.connect = nullptr; // special mark
555 oc1.connect = &chanOut[cha];
556 om2.connect = &chanOut[cha];
557 om1.mem_connect = &m2;
558 break;
559
560 case 6:
561 // M1---C1-+
562 // M2-+-OUT
563 // C2-+
564 // MEM: not used
565 om1.connect = &c1;
566 oc1.connect = &chanOut[cha];
567 om2.connect = &chanOut[cha];
568 om1.mem_connect = &mem; // store it anywhere where it will not be used
569 break;
570
571 case 7:
572 // M1-+
573 // C1-+-OUT
574 // M2-+
575 // C2-+
576 // MEM: not used
577 om1.connect = &chanOut[cha];
578 oc1.connect = &chanOut[cha];
579 om2.connect = &chanOut[cha];
580 om1.mem_connect = &mem; // store it anywhere where it will not be used
581 break;
582 }
583}
584
585void YM2151::refreshEG(std::span<YM2151Operator, 4> op)
586{
587 unsigned kc = op[0].kc;
588
589 // v = 32 + 2*RATE + RKS = max 126
590 unsigned v = kc >> op[0].ks;
591 if ((op[0].ar + v) < 32 + 62) {
592 op[0].eg_sh_ar = eg_rate_shift [op[0].ar + v];
593 op[0].eg_sel_ar = eg_rate_select[op[0].ar + v];
594 } else {
595 op[0].eg_sh_ar = 0;
596 op[0].eg_sel_ar = 17 * RATE_STEPS;
597 }
598 op[0].eg_sh_d1r = eg_rate_shift [op[0].d1r + v];
599 op[0].eg_sel_d1r = eg_rate_select[op[0].d1r + v];
600 op[0].eg_sh_d2r = eg_rate_shift [op[0].d2r + v];
601 op[0].eg_sel_d2r = eg_rate_select[op[0].d2r + v];
602 op[0].eg_sh_rr = eg_rate_shift [op[0].rr + v];
603 op[0].eg_sel_rr = eg_rate_select[op[0].rr + v];
604
605 v = kc >> op[1].ks;
606 if ((op[1].ar + v) < 32 + 62) {
607 op[1].eg_sh_ar = eg_rate_shift [op[1].ar + v];
608 op[1].eg_sel_ar = eg_rate_select[op[1].ar + v];
609 } else {
610 op[1].eg_sh_ar = 0;
611 op[1].eg_sel_ar = 17 * RATE_STEPS;
612 }
613 op[1].eg_sh_d1r = eg_rate_shift [op[1].d1r + v];
614 op[1].eg_sel_d1r = eg_rate_select[op[1].d1r + v];
615 op[1].eg_sh_d2r = eg_rate_shift [op[1].d2r + v];
616 op[1].eg_sel_d2r = eg_rate_select[op[1].d2r + v];
617 op[1].eg_sh_rr = eg_rate_shift [op[1].rr + v];
618 op[1].eg_sel_rr = eg_rate_select[op[1].rr + v];
619
620 v = kc >> op[2].ks;
621 if ((op[2].ar + v) < 32 + 62) {
622 op[2].eg_sh_ar = eg_rate_shift [op[2].ar + v];
623 op[2].eg_sel_ar = eg_rate_select[op[2].ar + v];
624 } else {
625 op[2].eg_sh_ar = 0;
626 op[2].eg_sel_ar = 17 * RATE_STEPS;
627 }
628 op[2].eg_sh_d1r = eg_rate_shift [op[2].d1r + v];
629 op[2].eg_sel_d1r = eg_rate_select[op[2].d1r + v];
630 op[2].eg_sh_d2r = eg_rate_shift [op[2].d2r + v];
631 op[2].eg_sel_d2r = eg_rate_select[op[2].d2r + v];
632 op[2].eg_sh_rr = eg_rate_shift [op[2].rr + v];
633 op[2].eg_sel_rr = eg_rate_select[op[2].rr + v];
634
635 v = kc >> op[3].ks;
636 if ((op[3].ar + v) < 32 + 62) {
637 op[3].eg_sh_ar = eg_rate_shift [op[3].ar + v];
638 op[3].eg_sel_ar = eg_rate_select[op[3].ar + v];
639 } else {
640 op[3].eg_sh_ar = 0;
641 op[3].eg_sel_ar = 17 * RATE_STEPS;
642 }
643 op[3].eg_sh_d1r = eg_rate_shift [op[3].d1r + v];
644 op[3].eg_sel_d1r = eg_rate_select[op[3].d1r + v];
645 op[3].eg_sh_d2r = eg_rate_shift [op[3].d2r + v];
646 op[3].eg_sel_d2r = eg_rate_select[op[3].d2r + v];
647 op[3].eg_sh_rr = eg_rate_shift [op[3].rr + v];
648 op[3].eg_sel_rr = eg_rate_select[op[3].rr + v];
649}
650
651void YM2151::writeReg(uint8_t r, uint8_t v, EmuTime::param time)
652{
653 updateStream(time);
654
655 YM2151Operator& op = oper[(r & 0x07) * 4 + ((r & 0x18) >> 3)];
656
657 regs[r] = v;
658 switch (r & 0xe0) {
659 case 0x00:
660 switch (r) {
661 case 0x01:
662 case 0x09:
663 // the test register is located differently between the variants
664 if ((r == 1 && variant == Variant::YM2151) ||
665 (r == 9 && variant == Variant::YM2164)) {
666 test = v;
667 if (v & 2) lfo_phase = 0; // bit 1: LFO reset
668 }
669 break;
670
671 case 0x08:
672 envelopeKONKOFF(subspan<4>(oper, 4 * (v & 7)), v);
673 break;
674
675 case 0x0f: // noise mode enable, noise period
676 noise = v;
677 noise_f = noise_tab[v & 0x1f];
678 noise_p = 0;
679 break;
680
681 case 0x10:
682 timer_A_val &= 0x03;
683 timer_A_val |= v << 2;
684 timer1->setValue(timer_A_val);
685 break;
686
687 case 0x11:
688 timer_A_val &= 0x03fc;
689 timer_A_val |= v & 3;
690 timer1->setValue(timer_A_val);
691 break;
692
693 case 0x12:
694 timer2->setValue(v);
695 break;
696
697 case 0x14: // CSM, irq flag reset, irq enable, timer start/stop
698 irq_enable = v; // bit 3-timer B, bit 2-timer A, bit 7 - CSM
699 if (v & 0x10) { // reset timer A irq flag
700 resetStatus(1);
701 }
702 if (v & 0x20) { // reset timer B irq flag
703 resetStatus(2);
704 }
705 timer1->setStart((v & 4) != 0, time);
706 timer2->setStart((v & 8) != 0, time);
707 break;
708
709 case 0x18: // LFO frequency
710 lfo_overflow = (1 << ((15 - (v >> 4)) + 3));
711 lfo_counter_add = 0x10 + (v & 0x0f);
712 break;
713
714 case 0x19: // PMD (bit 7==1) or AMD (bit 7==0)
715 if (v & 0x80) {
716 pmd = narrow<int8_t>(v & 0x7f);
717 } else {
718 amd = v & 0x7f;
719 }
720 break;
721
722 case 0x1b: // CT2, CT1, LFO waveform
723 ct = v >> 6;
724 lfo_wsel = v & 3;
725 // TODO ym2151WritePortCallback(0 , ct);
726 break;
727
728 default:
729 break;
730 }
731 break;
732
733 case 0x20: {
734 auto o = subspan<4>(oper, 4 * (r & 7));
735 switch (r & 0x18) {
736 case 0x00: // RL enable, Feedback, Connection
737 o[0].fb_shift = ((v >> 3) & 7) ? ((v >> 3) & 7) + 6 : 0;
738 pan[(r & 7) * 2 + 0] = (v & 0x40) ? ~0 : 0;
739 pan[(r & 7) * 2 + 1] = (v & 0x80) ? ~0 : 0;
740 setConnect(o, r & 7, v & 7);
741 break;
742
743 case 0x08: // Key Code
744 v &= 0x7f;
745 if (v != o[0].kc) {
746 unsigned kc_channel = (v - (v>>2))*64;
747 kc_channel += 768;
748 kc_channel |= (o[0].kc_i & 63);
749
750 o[0].kc = v;
751 o[0].kc_i = kc_channel;
752 o[1].kc = v;
753 o[1].kc_i = kc_channel;
754 o[2].kc = v;
755 o[2].kc_i = kc_channel;
756 o[3].kc = v;
757 o[3].kc_i = kc_channel;
758
759 unsigned kc = v>>2;
760 o[0].dt1 = dt1_freq[o[0].dt1_i + kc];
761 o[0].freq = ((freq[kc_channel + o[0].dt2] + o[0].dt1) * o[0].mul) >> 1;
762
763 o[1].dt1 = dt1_freq[o[1].dt1_i + kc];
764 o[1].freq = ((freq[kc_channel + o[1].dt2] + o[1].dt1) * o[1].mul) >> 1;
765
766 o[2].dt1 = dt1_freq[o[2].dt1_i + kc];
767 o[2].freq = ((freq[kc_channel + o[2].dt2] + o[2].dt1) * o[2].mul) >> 1;
768
769 o[3].dt1 = dt1_freq[o[3].dt1_i + kc];
770 o[3].freq = ((freq[kc_channel + o[3].dt2] + o[3].dt1) * o[3].mul) >> 1;
771
772 refreshEG(o);
773 }
774 break;
775
776 case 0x10: // Key Fraction
777 v >>= 2;
778 if (v != (o[0].kc_i & 63)) {
779 unsigned kc_channel = v;
780 kc_channel |= (o[0].kc_i & ~63);
781
782 o[0].kc_i = kc_channel;
783 o[1].kc_i = kc_channel;
784 o[2].kc_i = kc_channel;
785 o[3].kc_i = kc_channel;
786
787 o[0].freq = ((freq[kc_channel + o[0].dt2] + o[0].dt1) * o[0].mul) >> 1;
788 o[1].freq = ((freq[kc_channel + o[1].dt2] + o[1].dt1) * o[1].mul) >> 1;
789 o[2].freq = ((freq[kc_channel + o[2].dt2] + o[2].dt1) * o[2].mul) >> 1;
790 o[3].freq = ((freq[kc_channel + o[3].dt2] + o[3].dt1) * o[3].mul) >> 1;
791 }
792 break;
793
794 case 0x18: // PMS, AMS
795 o[0].pms = narrow<int8_t>((v >> 4) & 7);
796 o[0].ams = (v & 3);
797 break;
798 }
799 break;
800 }
801 case 0x40: { // DT1, MUL
802 unsigned olddt1_i = op.dt1_i;
803 unsigned oldMul = op.mul;
804
805 op.dt1_i = (v & 0x70) << 1;
806 op.mul = (v & 0x0f) ? (v & 0x0f) << 1 : 1;
807
808 if (olddt1_i != op.dt1_i) {
809 op.dt1 = dt1_freq[op.dt1_i + (op.kc>>2)];
810 }
811 if ((olddt1_i != op.dt1_i) || (oldMul != op.mul)) {
812 op.freq = ((freq[op.kc_i + op.dt2] + op.dt1) * op.mul) >> 1;
813 }
814 break;
815 }
816 case 0x60: // TL
817 op.tl = (v & 0x7f) << (ENV_BITS - 7); // 7bit TL
818 break;
819
820 case 0x80: { // KS, AR
821 unsigned oldKs = op.ks;
822 unsigned oldAr = op.ar;
823 op.ks = 5 - (v >> 6);
824 op.ar = (v & 0x1f) ? 32 + ((v & 0x1f) << 1) : 0;
825
826 if ((op.ar != oldAr) || (op.ks != oldKs)) {
827 if ((op.ar + (op.kc >> op.ks)) < 32 + 62) {
828 op.eg_sh_ar = eg_rate_shift [op.ar + (op.kc>>op.ks)];
829 op.eg_sel_ar = eg_rate_select[op.ar + (op.kc>>op.ks)];
830 } else {
831 op.eg_sh_ar = 0;
832 op.eg_sel_ar = 17 * RATE_STEPS;
833 }
834 }
835 if (op.ks != oldKs) {
836 op.eg_sh_d1r = eg_rate_shift [op.d1r + (op.kc >> op.ks)];
837 op.eg_sel_d1r = eg_rate_select[op.d1r + (op.kc >> op.ks)];
838 op.eg_sh_d2r = eg_rate_shift [op.d2r + (op.kc >> op.ks)];
839 op.eg_sel_d2r = eg_rate_select[op.d2r + (op.kc >> op.ks)];
840 op.eg_sh_rr = eg_rate_shift [op.rr + (op.kc >> op.ks)];
841 op.eg_sel_rr = eg_rate_select[op.rr + (op.kc >> op.ks)];
842 }
843 break;
844 }
845 case 0xa0: // LFO AM enable, D1R
846 op.AMmask = (v & 0x80) ? ~0 : 0;
847 op.d1r = (v & 0x1f) ? 32 + ((v & 0x1f) << 1) : 0;
848 op.eg_sh_d1r = eg_rate_shift [op.d1r + (op.kc >> op.ks)];
849 op.eg_sel_d1r = eg_rate_select[op.d1r + (op.kc >> op.ks)];
850 break;
851
852 case 0xc0: { // DT2, D2R
853 unsigned olddt2 = op.dt2;
854 op.dt2 = dt2_tab[v >> 6];
855 if (op.dt2 != olddt2) {
856 op.freq = ((freq[op.kc_i + op.dt2] + op.dt1) * op.mul) >> 1;
857 }
858 op.d2r = (v & 0x1f) ? 32 + ((v & 0x1f) << 1) : 0;
859 op.eg_sh_d2r = eg_rate_shift [op.d2r + (op.kc >> op.ks)];
860 op.eg_sel_d2r = eg_rate_select[op.d2r + (op.kc >> op.ks)];
861 break;
862 }
863 case 0xe0: // D1L, RR
864 op.d1l = d1l_tab[v >> 4];
865 op.rr = 34 + ((v & 0x0f) << 2);
866 op.eg_sh_rr = eg_rate_shift [op.rr + (op.kc >> op.ks)];
867 op.eg_sel_rr = eg_rate_select[op.rr + (op.kc >> op.ks)];
868 break;
869 }
870}
871
872static constexpr auto INPUT_RATE = unsigned(cstd::round(3579545 / 64.0));
873
874YM2151::YM2151(const std::string& name_, static_string_view desc,
875 const DeviceConfig& config, EmuTime::param time, Variant variant_)
876 : ResampledSoundDevice(config.getMotherBoard(), name_, desc, 8, INPUT_RATE, true)
877 , irq(config.getMotherBoard(), getName() + ".IRQ")
878 , timer1(EmuTimer::createOPM_1(config.getScheduler(), *this))
879 , timer2(variant_ == Variant::YM2164 ? EmuTimer::createOPP_2(config.getScheduler(), *this)
880 : EmuTimer::createOPM_2(config.getScheduler(), *this))
881 , variant(variant_)
882{
883 // TODO Registers 0x20-0xFF are cleared on reset.
884 // Should we do the same for registers 0x00-0x1F?
885
886 if (false) {
887 std::cout << "tl_tab:";
888 for (const auto& e : tl_tab) std::cout << ' ' << e;
889 std::cout << '\n';
890
891 std::cout << "sin_tab:";
892 for (const auto& e : sin_tab) std::cout << ' ' << e;
893 std::cout << '\n';
894
895 std::cout << "d1l_tab:";
896 for (const auto& e : d1l_tab) std::cout << ' ' << e;
897 std::cout << '\n';
898
899 std::cout << "freq:";
900 for (const auto& e : freq) std::cout << ' ' << e;
901 std::cout << '\n';
902
903 std::cout << "dt1_freq:";
904 for (const auto& e : dt1_freq) std::cout << ' ' << e;
905 std::cout << '\n';
906
907 std::cout << "noise_tab:";
908 for (const auto& e : noise_tab) std::cout << ' ' << e;
909 std::cout << '\n';
910 }
911
912 reset(time);
913
914 registerSound(config);
915}
916
921
922bool YM2151::checkMuteHelper()
923{
924 return ranges::all_of(oper, [](auto& op) { return op.state == EG_OFF; });
925}
926
927void YM2151::reset(EmuTime::param time)
928{
929 // initialize hardware registers
930 for (auto& op : oper) {
931 memset(&op, '\0', sizeof(op));
932 op.volume = MAX_ATT_INDEX;
933 op.kc_i = 768; // min kc_i value
934 }
935
936 eg_timer = 0;
937 eg_cnt = 0;
938
939 lfo_timer = 0;
940 lfo_counter = 0;
941 lfo_phase = 0;
942 lfo_wsel = 0;
943 pmd = 0;
944 amd = 0;
945 lfa = 0;
946 lfp = 0;
947
948 test = 0;
949
950 irq_enable = 0;
951 timer1->setStart(false, time);
952 timer2->setStart(false, time);
953
954 noise = 0;
955 noise_rng = 0;
956 noise_p = 0;
957 noise_f = noise_tab[0];
958
959 csm_req = 0;
960 status = 0;
961
962 writeReg(0x1b, 0, time); // only because of CT1, CT2 output pins
963 writeReg(0x18, 0, time); // set LFO frequency
964 for (auto i : xrange(0x20, 0x100)) { // set the operators
965 writeReg(narrow<uint8_t>(i), 0, time);
966 }
967
968 irq.reset();
969}
970
971int YM2151::opCalc(const YM2151Operator& op, unsigned env, int pm) const
972{
973 unsigned p = (env << 3) + sin_tab[(int((op.phase & ~FREQ_MASK) + (pm << 15)) >> FREQ_SH) & SIN_MASK];
974 if (p >= TL_TAB_LEN) {
975 return 0;
976 }
977 return tl_tab[p];
978}
979
980int YM2151::opCalc1(const YM2151Operator& op, unsigned env, int pm) const
981{
982 int i = (narrow_cast<int>(op.phase) & ~FREQ_MASK) + pm;
983 unsigned p = (env << 3) + sin_tab[(i >> FREQ_SH) & SIN_MASK];
984 if (p >= TL_TAB_LEN) {
985 return 0;
986 }
987 return tl_tab[p];
988}
989
990unsigned YM2151::volumeCalc(const YM2151Operator& op, unsigned AM) const
991{
992 return op.tl + unsigned(op.volume) + (AM & op.AMmask);
993}
994
995void YM2151::chanCalc(unsigned chan)
996{
997 m2 = c1 = c2 = mem = 0;
998 auto op = subspan<4>(oper, 4 * chan); // M1
999 *op[0].mem_connect = op[0].mem_value; // restore delayed sample (MEM) value to m2 or c2
1000
1001 unsigned AM = 0;
1002 if (op[0].ams) {
1003 AM = lfa << (op[0].ams-1);
1004 }
1005 unsigned env = volumeCalc(op[0], AM);
1006 {
1007 int out = op[0].fb_out_prev + op[0].fb_out_curr;
1008 op[0].fb_out_prev = op[0].fb_out_curr;
1009
1010 if (!op[0].connect) {
1011 // algorithm 5
1012 mem = c1 = c2 = op[0].fb_out_prev;
1013 } else {
1014 *op[0].connect = op[0].fb_out_prev;
1015 }
1016 op[0].fb_out_curr = 0;
1017 if (env < ENV_QUIET) {
1018 if (!op[0].fb_shift) {
1019 out = 0;
1020 }
1021 op[0].fb_out_curr = opCalc1(op[0], env, (out << op[0].fb_shift));
1022 }
1023 }
1024
1025 env = volumeCalc(op[1], AM); // M2
1026 if (env < ENV_QUIET) {
1027 *op[1].connect += opCalc(op[1], env, m2);
1028 }
1029 env = volumeCalc(op[2], AM); // C1
1030 if (env < ENV_QUIET) {
1031 *op[2].connect += opCalc(op[2], env, c1);
1032 }
1033 env = volumeCalc(op[3], AM); // C2
1034 if (env < ENV_QUIET) {
1035 chanOut[chan] += opCalc(op[3], env, c2);
1036 }
1037 // M1
1038 op[0].mem_value = mem;
1039}
1040
1041void YM2151::chan7Calc()
1042{
1043 m2 = c1 = c2 = mem = 0;
1044 auto op = subspan<4>(oper, 4 * 7);
1045
1046 *op[0].mem_connect = op[0].mem_value; // restore delayed sample (MEM) value to m2 or c2
1047
1048 unsigned AM = 0;
1049 if (op[0].ams) {
1050 AM = lfa << (op[0].ams - 1);
1051 }
1052 unsigned env = volumeCalc(op[0], AM);
1053 {
1054 int out = op[0].fb_out_prev + op[0].fb_out_curr;
1055 op[0].fb_out_prev = op[0].fb_out_curr;
1056
1057 if (!op[0].connect) {
1058 // algorithm 5
1059 mem = c1 = c2 = op[0].fb_out_prev;
1060 } else {
1061 // other algorithms
1062 *op[0].connect = op[0].fb_out_prev;
1063 }
1064 op[0].fb_out_curr = 0;
1065 if (env < ENV_QUIET) {
1066 if (!op[0].fb_shift) {
1067 out = 0;
1068 }
1069 op[0].fb_out_curr = opCalc1(op[0], env, (out << op[0].fb_shift));
1070 }
1071 }
1072
1073 env = volumeCalc(op[1], AM); // M2
1074 if (env < ENV_QUIET) {
1075 *op[1].connect += opCalc(op[1], env, m2);
1076 }
1077 env = volumeCalc(op[2], AM); // C1
1078 if (env < ENV_QUIET) {
1079 *op[2].connect += opCalc(op[2], env, c1);
1080 }
1081 env = volumeCalc(op[3], AM); // C2
1082 if (noise & 0x80) {
1083 int noiseOut = 0;
1084 if (env < 0x3ff) {
1085 noiseOut = (narrow<int>(env) ^ 0x3ff) * 2; // range of the YM2151 noise output is -2044 to 2040
1086 }
1087 chanOut[7] += (noise_rng & 0x10000) ? noiseOut : -noiseOut; // bit 16 -> output
1088 } else {
1089 if (env < ENV_QUIET) {
1090 chanOut[7] += opCalc(op[3], env, c2);
1091 }
1092 }
1093 // M1
1094 op[0].mem_value = mem;
1095}
1096
1097/*
1098The 'rate' is calculated from following formula (example on decay rate):
1099 rks = note-code after key scaling (a value from 0 to 31)
1100 DR = value written to the chip register
1101 rate = 2*DR + rks; (max rate = 2*31+31 = 93)
1102Four MSBs of the 'rate' above are the 'main' rate (from 00 to 15)
1103Two LSBs of the 'rate' above are the value 'x' (the shape type).
1104(eg. '11 2' means that 'rate' is 11*4+2=46)
1105
1106NOTE: A 'sample' in the description below is actually 3 output samples,
1107thats because the Envelope Generator clock is equal to internal_clock/3.
1108
1109Single '-' (minus) character in the diagrams below represents one sample
1110on the output; this is for rates 11 x (11 0, 11 1, 11 2 and 11 3)
1111
1112these 'main' rates:
111300 x: single '-' = 2048 samples; (ie. level can change every 2048 samples)
111401 x: single '-' = 1024 samples;
111502 x: single '-' = 512 samples;
111603 x: single '-' = 256 samples;
111704 x: single '-' = 128 samples;
111805 x: single '-' = 64 samples;
111906 x: single '-' = 32 samples;
112007 x: single '-' = 16 samples;
112108 x: single '-' = 8 samples;
112209 x: single '-' = 4 samples;
112310 x: single '-' = 2 samples;
112411 x: single '-' = 1 sample; (ie. level can change every 1 sample)
1125
1126Shapes for rates 11 x look like this:
1127rate: step:
112811 0 01234567
1129
1130level:
11310 --
11321 --
11332 --
11343 --
1135
1136rate: step:
113711 1 01234567
1138
1139level:
11400 --
11411 --
11422 -
11433 -
11444 --
1145
1146rate: step:
114711 2 01234567
1148
1149level:
11500 --
11511 -
11522 -
11533 --
11544 -
11555 -
1156
1157rate: step:
115811 3 01234567
1159
1160level:
11610 --
11621 -
11632 -
11643 -
11654 -
11665 -
11676 -
1168
1169
1170For rates 12 x, 13 x, 14 x and 15 x output level changes on every
1171sample - this means that the waveform looks like this: (but the level
1172changes by different values on different steps)
117312 3 01234567
1174
11750 -
11762 -
11774 -
11788 -
117910 -
118012 -
118114 -
118218 -
118320 -
1184
1185Notes about the timing:
1186----------------------
1187
11881. Synchronism
1189
1190Output level of each two (or more) voices running at the same 'main' rate
1191(eg 11 0 and 11 1 in the diagram below) will always be changing in sync,
1192even if there're started with some delay.
1193
1194Note that, in the diagram below, the decay phase in channel 0 starts at
1195sample #2, while in channel 1 it starts at sample #6. Anyway, both channels
1196will always change their levels at exactly the same (following) samples.
1197
1198(S - start point of this channel, A-attack phase, D-decay phase):
1199
1200step:
120101234567012345670123456
1202
1203channel 0:
1204 --
1205 | --
1206 | -
1207 | -
1208 | --
1209 | --
1210| --
1211| -
1212| -
1213| --
1214AADDDDDDDDDDDDDDDD
1215S
1216
121701234567012345670123456
1218channel 1:
1219 -
1220 | -
1221 | --
1222 | --
1223 | --
1224 | -
1225 | -
1226 | --
1227 | --
1228 | --
1229 AADDDDDDDDDDDDDDDD
1230 S
123101234567012345670123456
1232
1233
12342. Shifted (delayed) synchronism
1235
1236Output of each two (or more) voices running at different 'main' rate
1237(9 1, 10 1 and 11 1 in the diagrams below) will always be changing
1238in 'delayed-sync' (even if there're started with some delay as in "1.")
1239
1240Note that the shapes are delayed by exactly one sample per one 'main' rate
1241increment. (Normally one would expect them to start at the same samples.)
1242
1243See diagram below (* - start point of the shape).
1244
1245cycle:
12460123456701234567012345670123456701234567012345670123456701234567
1247
1248rate 09 1
1249*-------
1250 --------
1251 ----
1252 ----
1253 --------
1254 *-------
1255 | --------
1256 | ----
1257 | ----
1258 | --------
1259rate 10 1 |
1260-- |
1261 *--- |
1262 ---- |
1263 -- |
1264 -- |
1265 ---- |
1266 *--- |
1267 | ---- |
1268 | -- | | <- one step (two samples) delay between 9 1 and 10 1
1269 | -- | |
1270 | ----|
1271 | *---
1272 | ----
1273 | --
1274 | --
1275 | ----
1276rate 11 1 |
1277- |
1278 -- |
1279 *- |
1280 -- |
1281 - |
1282 - |
1283 -- |
1284 *- |
1285 -- |
1286 - || <- one step (one sample) delay between 10 1 and 11 1
1287 - ||
1288 --|
1289 *-
1290 --
1291 -
1292 -
1293 --
1294 *-
1295 --
1296 -
1297 -
1298 --
1299*/
1300
1301void YM2151::advanceEG()
1302{
1303 if (eg_timer++ != 3) {
1304 // envelope generator timer overflows every 3 samples (on real chip)
1305 return;
1306 }
1307 eg_timer = 0;
1308 eg_cnt++;
1309
1310 // envelope generator
1311 for (auto& op : oper) {
1312 switch (op.state) {
1313 case EG_ATT: // attack phase
1314 if (!(eg_cnt & ((1 << op.eg_sh_ar) - 1))) {
1315 op.volume += (~op.volume *
1316 (eg_inc[op.eg_sel_ar + ((eg_cnt >> op.eg_sh_ar) & 7)])
1317 ) >> 4;
1318 if (op.volume <= MIN_ATT_INDEX) {
1319 op.volume = MIN_ATT_INDEX;
1320 op.state = EG_DEC;
1321 }
1322 }
1323 break;
1324
1325 case EG_DEC: // decay phase
1326 if (!(eg_cnt & ((1 << op.eg_sh_d1r) - 1))) {
1327 op.volume += eg_inc[op.eg_sel_d1r + ((eg_cnt >> op.eg_sh_d1r) & 7)];
1328 if (unsigned(op.volume) >= op.d1l) {
1329 op.state = EG_SUS;
1330 }
1331 }
1332 break;
1333
1334 case EG_SUS: // sustain phase
1335 if (!(eg_cnt & ((1 << op.eg_sh_d2r) - 1))) {
1336 op.volume += eg_inc[op.eg_sel_d2r + ((eg_cnt >> op.eg_sh_d2r) & 7)];
1337 if (op.volume >= MAX_ATT_INDEX) {
1338 op.volume = MAX_ATT_INDEX;
1339 op.state = EG_OFF;
1340 }
1341 }
1342 break;
1343
1344 case EG_REL: // release phase
1345 if (!(eg_cnt & ((1 << op.eg_sh_rr) - 1))) {
1346 op.volume += eg_inc[op.eg_sel_rr + ((eg_cnt >> op.eg_sh_rr) & 7)];
1347 if (op.volume >= MAX_ATT_INDEX) {
1348 op.volume = MAX_ATT_INDEX;
1349 op.state = EG_OFF;
1350 }
1351 }
1352 break;
1353 }
1354 }
1355}
1356
1357void YM2151::advance()
1358{
1359 // LFO
1360 if (test & 2) {
1361 lfo_phase = 0;
1362 } else {
1363 if (lfo_timer++ >= lfo_overflow) {
1364 lfo_timer = 0;
1365 lfo_counter += lfo_counter_add;
1366 lfo_phase += (lfo_counter >> 4);
1367 lfo_phase &= 255;
1368 lfo_counter &= 15;
1369 }
1370 }
1371
1372 unsigned i = lfo_phase;
1373 // calculate LFO AM and PM waveform value (all verified on real chip,
1374 // except for noise algorithm which is impossible to analyze)
1375 auto [a, p] = [&]() -> std::pair<int, int> {
1376 switch (lfo_wsel) {
1377 case 0:
1378 // saw
1379 // AM: 255 down to 0
1380 // PM: 0 to 127, -127 to 0 (at PMD=127: LFP = 0 to 126, -126 to 0)
1381 return {
1382 /*a =*/ (255 - i),
1383 /*p =*/ ((i < 128) ? i : (i - 255))
1384 };
1385 case 1:
1386 // square
1387 // AM: 255, 0
1388 // PM: 128,-128 (LFP = exactly +PMD, -PMD)
1389 return {
1390 /*a =*/ ((i < 128) ? 255 : 0),
1391 /*p =*/ ((i < 128) ? 128 : -128)
1392 };
1393 case 2:
1394 // triangle
1395 // AM: 255 down to 1 step -2; 0 up to 254 step +2
1396 // PM: 0 to 126 step +2, 127 to 1 step -2,
1397 // 0 to -126 step -2, -127 to -1 step +2
1398 return {
1399 /*a =*/ ((i < 128) ? (255 - (i * 2)) : ((i * 2) - 256)),
1400 /*p =*/ [&] {
1401 if (i < 64) { // i = 0..63
1402 return i * 2; // 0 to 126 step +2
1403 } else if (i < 128) { // i = 64..127
1404 return 255 - i * 2; // 127 to 1 step -2
1405 } else if (i < 192) { // i = 128..191
1406 return 256 - i * 2; // 0 to -126 step -2
1407 } else { // i = 192..255
1408 return i * 2 - 511; // -127 to -1 step +2
1409 }
1410 }()
1411 };
1412 break;
1413 case 3:
1414 default: // keep the compiler happy
1415 // Random. The real algorithm is unknown !!!
1416 // We just use a snapshot of data from real chip
1417
1418 // AM: range 0 to 255
1419 // PM: range -128 to 127
1420 return {
1421 /*a =*/ lfo_noise_waveform[i],
1422 /*p =*/ (lfo_noise_waveform[i] - 128)
1423 };
1424 }
1425 }();
1426 lfa = a * amd / 128;
1427 lfp = p * pmd / 128;
1428
1429 // The Noise Generator of the YM2151 is 17-bit shift register.
1430 // Input to the bit16 is negated (bit0 XOR bit3) (XNOR).
1431 // Output of the register is negated (bit0 XOR bit3).
1432 // Simply use bit16 as the noise output.
1433
1434 // noise changes depending on the index in noise_tab (noise_f = noise_tab[x])
1435 // noise_tab contains how many cycles/samples (x2) the noise should change.
1436 // so, when it contains 29, noise should change every 14.5 cycles (2 out of 29).
1437 // if you read this code well, you'll see that is what happens here :)
1438 noise_p -= 2;
1439 if (noise_p < 0) {
1440 noise_p += noise_f;
1441 unsigned j = ((noise_rng ^ (noise_rng >> 3)) & 1) ^ 1;
1442 noise_rng = (j << 16) | (noise_rng >> 1);
1443 }
1444
1445 // phase generator
1446 for (auto c : xrange(8)) {
1447 auto op = subspan<4>(oper, 4 * c);
1448 // only when phase modulation from LFO is enabled for this channel
1449 if (op[0].pms) {
1450 int mod_ind = lfp; // -128..+127 (8bits signed)
1451 if (op[0].pms < 6) {
1452 mod_ind >>= (6 - op[0].pms);
1453 } else {
1454 mod_ind <<= (op[0].pms - 5);
1455 }
1456 if (mod_ind) {
1457 unsigned kc_channel = op[0].kc_i + mod_ind;
1458 op[0].phase += ((freq[kc_channel + op[0].dt2] + op[0].dt1) * op[0].mul) >> 1;
1459 op[1].phase += ((freq[kc_channel + op[1].dt2] + op[1].dt1) * op[1].mul) >> 1;
1460 op[2].phase += ((freq[kc_channel + op[2].dt2] + op[2].dt1) * op[2].mul) >> 1;
1461 op[3].phase += ((freq[kc_channel + op[3].dt2] + op[3].dt1) * op[3].mul) >> 1;
1462 } else { // phase modulation from LFO is equal to zero
1463 op[0].phase += op[0].freq;
1464 op[1].phase += op[1].freq;
1465 op[2].phase += op[2].freq;
1466 op[3].phase += op[3].freq;
1467 }
1468 } else { // phase modulation from LFO is disabled
1469 op[0].phase += op[0].freq;
1470 op[1].phase += op[1].freq;
1471 op[2].phase += op[2].freq;
1472 op[3].phase += op[3].freq;
1473 }
1474 }
1475
1476 // CSM is calculated *after* the phase generator calculations (verified
1477 // on real chip)
1478 // CSM keyon line seems to be ORed with the KO line inside of the chip.
1479 // The result is that it only works when KO (register 0x08) is off, ie. 0
1480 //
1481 // Interesting effect is that when timer A is set to 1023, the KEY ON happens
1482 // on every sample, so there is no KEY OFF at all - the result is that
1483 // the sound played is the same as after normal KEY ON.
1484 if (csm_req) { // CSM KEYON/KEYOFF sequence request
1485 if (csm_req == 2) { // KEY ON
1486 for (auto& op : oper) {
1487 keyOn(op, 2);
1488 }
1489 csm_req = 1;
1490 } else { // KEY OFF
1491 for (auto& op : oper) {
1492 keyOff(op, unsigned(~2));
1493 }
1494 csm_req = 0;
1495 }
1496 }
1497}
1498
1499void YM2151::generateChannels(std::span<float*> bufs, unsigned num)
1500{
1501 if (checkMuteHelper()) {
1502 // TODO update internal state, even if muted
1503 ranges::fill(bufs, nullptr);
1504 return;
1505 }
1506
1507 for (auto i : xrange(num)) {
1508 advanceEG();
1509
1510 for (auto j : xrange(8 - 1)) {
1511 chanOut[j] = 0;
1512 chanCalc(j);
1513 }
1514 chanOut[7] = 0;
1515 chan7Calc(); // special case for channel 7
1516
1517 for (auto j : xrange(8)) {
1518 bufs[j][2 * i + 0] += narrow_cast<float>(narrow_cast<int>(chanOut[j] & pan[2 * j + 0]));
1519 bufs[j][2 * i + 1] += narrow_cast<float>(narrow_cast<int>(chanOut[j] & pan[2 * j + 1]));
1520 }
1521 advance();
1522 }
1523}
1524
1525void YM2151::callback(uint8_t flag)
1526{
1527 assert(flag == one_of(1, 2));
1528 setStatus(flag);
1529
1530 if ((flag == 1) && (irq_enable & 0x80)) { // Timer 1
1531 csm_req = 2; // request KEY ON / KEY OFF sequence
1532 }
1533}
1534
1535uint8_t YM2151::readStatus() const
1536{
1537 return status;
1538}
1539
1540void YM2151::setStatus(uint8_t flags)
1541{
1542 status |= flags;
1543 auto enable = (irq_enable >> 2) & 3;
1544 if ((status & enable) != 0) {
1545 irq.set();
1546 }
1547}
1548
1549void YM2151::resetStatus(uint8_t flags)
1550{
1551 status &= ~flags;
1552 auto enable = (irq_enable >> 2) & 3;
1553 if ((status & enable) == 0) {
1554 irq.reset();
1555 }
1556}
1557
1558
1559template<typename Archive>
1560void YM2151::YM2151Operator::serialize(Archive& a, unsigned /*version*/)
1561{
1562 //int* connect; // recalculated from regs[0x20-0x27]
1563 //int* mem_connect; // recalculated from regs[0x20-0x27]
1564 a.serialize("phase", phase,
1565 "freq", freq,
1566 "dt1", dt1,
1567 "mul", mul,
1568 "dt1_i", dt1_i,
1569 "dt2", dt2,
1570 "mem_value", mem_value,
1571 //"fb_shift", fb_shift, // recalculated from regs[0x20-0x27]
1572 "fb_out_curr", fb_out_curr,
1573 "fb_out_prev", fb_out_prev,
1574 "kc", kc,
1575 "kc_i", kc_i,
1576 "pms", pms,
1577 "ams", ams,
1578 "AMmask", AMmask,
1579 "state", state,
1580 "tl", tl,
1581 "volume", volume,
1582 "d1l", d1l,
1583 "key", key,
1584 "ks", ks,
1585 "ar", ar,
1586 "d1r", d1r,
1587 "d2r", d2r,
1588 "rr", rr,
1589 "eg_sh_ar", eg_sh_ar,
1590 "eg_sel_ar", eg_sel_ar,
1591 "eg_sh_d1r", eg_sh_d1r,
1592 "eg_sel_d1r", eg_sel_d1r,
1593 "eg_sh_d2r", eg_sh_d2r,
1594 "eg_sel_d2r", eg_sel_d2r,
1595 "eg_sh_rr", eg_sh_rr,
1596 "eg_sel_rr", eg_sel_rr);
1597};
1598
1599template<typename Archive>
1600void YM2151::serialize(Archive& a, unsigned /*version*/)
1601{
1602 a.serialize("irq", irq,
1603 "timer1", *timer1,
1604 "timer2", *timer2,
1605 "operators", oper,
1606 //"pan", pan, // recalculated from regs[0x20-0x27]
1607 "eg_cnt", eg_cnt,
1608 "eg_timer", eg_timer,
1609 "lfo_phase", lfo_phase,
1610 "lfo_timer", lfo_timer,
1611 "lfo_overflow", lfo_overflow,
1612 "lfo_counter", lfo_counter,
1613 "lfo_counter_add", lfo_counter_add,
1614 "lfa", lfa,
1615 "lfp", lfp,
1616 "noise", noise,
1617 "noise_rng", noise_rng,
1618 "noise_p", noise_p,
1619 "noise_f", noise_f,
1620 "csm_req", csm_req,
1621 "irq_enable", irq_enable,
1622 "status", status,
1623 "chanout", chanOut,
1624 "m2", m2,
1625 "c1", c1,
1626 "c2", c2,
1627 "mem", mem,
1628 "timer_A_val", timer_A_val,
1629 "lfo_wsel", lfo_wsel,
1630 "amd", amd,
1631 "pmd", pmd,
1632 "test", test,
1633 "ct", ct);
1634 a.serialize_blob("registers", regs);
1635
1636 if constexpr (Archive::IS_LOADER) {
1637 // TODO restore more state from registers
1638 EmuTime::param time = timer1->getCurrentTime();
1639 for (auto r : xrange(uint8_t(0x20), uint8_t(0x28))) {
1640 writeReg(r , regs[r], time);
1641 }
1642 }
1643}
1645
1646} // namespace openmsx
void set()
Set the interrupt request on the bus.
Definition IRQHelper.hh:76
void reset()
Reset the interrupt request on the bus.
Definition IRQHelper.hh:85
void updateStream(EmuTime::param time)
void unregisterSound()
Unregisters this sound device with the Mixer.
void registerSound(const DeviceConfig &config)
Registers this sound device with the Mixer.
uint8_t readStatus() const
Definition YM2151.cc:1535
void reset(EmuTime::param time)
Definition YM2151.cc:927
void serialize(Archive &ar, unsigned version)
Definition YM2151.cc:1600
void writeReg(uint8_t r, uint8_t v, EmuTime::param time)
Definition YM2151.cc:651
static_string_view
constexpr double pi
Definition Math.hh:24
constexpr double round(double x)
Definition cstd.hh:247
constexpr T abs(T t)
Definition cstd.hh:17
This file implemented 3 utility functions:
Definition Autofire.cc:11
constexpr bool all_of(InputRange &&range, UnaryPredicate pred)
Definition ranges.hh:188
constexpr void fill(ForwardRange &&range, const T &value)
Definition ranges.hh:315
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
constexpr auto xrange(T e)
Definition xrange.hh:132