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) {
467 if (op.state > EG_REL) {
468 op.state = EG_REL; /* KEY OFF = release */
469 }
470 }
471 }
472}
473
474void YM2151::envelopeKONKOFF(std::span<YM2151Operator, 4> op, int v) const
475{
476 if (v & 0x08) { // M1
477 keyOn (op[0], 1);
478 } else {
479 keyOff(op[0], unsigned(~1));
480 }
481 if (v & 0x20) { // M2
482 keyOn (op[1], 1);
483 } else {
484 keyOff(op[1], unsigned(~1));
485 }
486 if (v & 0x10) { // C1
487 keyOn (op[2], 1);
488 } else {
489 keyOff(op[2], unsigned(~1));
490 }
491 if (v & 0x40) { // C2
492 keyOn (op[3], 1);
493 } else {
494 keyOff(op[3], unsigned(~1));
495 }
496}
497
498void YM2151::setConnect(std::span<YM2151Operator, 4> o, int cha, int v)
499{
500 YM2151Operator& om1 = o[0];
501 YM2151Operator& om2 = o[1];
502 YM2151Operator& oc1 = o[2];
503
504 // set connect algorithm
505 // MEM is simply one sample delay
506 switch (v & 7) {
507 case 0:
508 // M1---C1---MEM---M2---C2---OUT
509 om1.connect = &c1;
510 oc1.connect = &mem;
511 om2.connect = &c2;
512 om1.mem_connect = &m2;
513 break;
514
515 case 1:
516 // M1------+-MEM---M2---C2---OUT
517 // C1-+
518 om1.connect = &mem;
519 oc1.connect = &mem;
520 om2.connect = &c2;
521 om1.mem_connect = &m2;
522 break;
523
524 case 2:
525 // M1-----------------+-C2---OUT
526 // C1---MEM---M2-+
527 om1.connect = &c2;
528 oc1.connect = &mem;
529 om2.connect = &c2;
530 om1.mem_connect = &m2;
531 break;
532
533 case 3:
534 // M1---C1---MEM------+-C2---OUT
535 // M2-+
536 om1.connect = &c1;
537 oc1.connect = &mem;
538 om2.connect = &c2;
539 om1.mem_connect = &c2;
540 break;
541
542 case 4:
543 // M1---C1-+-OUT
544 // M2---C2-+
545 // MEM: not used
546 om1.connect = &c1;
547 oc1.connect = &chanOut[cha];
548 om2.connect = &c2;
549 om1.mem_connect = &mem; // store it anywhere where it will not be used
550 break;
551
552 case 5:
553 // +----C1----+
554 // M1-+-MEM---M2-+-OUT
555 // +----C2----+
556 om1.connect = nullptr; // special mark
557 oc1.connect = &chanOut[cha];
558 om2.connect = &chanOut[cha];
559 om1.mem_connect = &m2;
560 break;
561
562 case 6:
563 // M1---C1-+
564 // M2-+-OUT
565 // C2-+
566 // MEM: not used
567 om1.connect = &c1;
568 oc1.connect = &chanOut[cha];
569 om2.connect = &chanOut[cha];
570 om1.mem_connect = &mem; // store it anywhere where it will not be used
571 break;
572
573 case 7:
574 // M1-+
575 // C1-+-OUT
576 // M2-+
577 // C2-+
578 // MEM: not used
579 om1.connect = &chanOut[cha];
580 oc1.connect = &chanOut[cha];
581 om2.connect = &chanOut[cha];
582 om1.mem_connect = &mem; // store it anywhere where it will not be used
583 break;
584 }
585}
586
587void YM2151::refreshEG(std::span<YM2151Operator, 4> op)
588{
589 unsigned kc = op[0].kc;
590
591 // v = 32 + 2*RATE + RKS = max 126
592 unsigned v = kc >> op[0].ks;
593 if ((op[0].ar + v) < 32 + 62) {
594 op[0].eg_sh_ar = eg_rate_shift [op[0].ar + v];
595 op[0].eg_sel_ar = eg_rate_select[op[0].ar + v];
596 } else {
597 op[0].eg_sh_ar = 0;
598 op[0].eg_sel_ar = 17 * RATE_STEPS;
599 }
600 op[0].eg_sh_d1r = eg_rate_shift [op[0].d1r + v];
601 op[0].eg_sel_d1r = eg_rate_select[op[0].d1r + v];
602 op[0].eg_sh_d2r = eg_rate_shift [op[0].d2r + v];
603 op[0].eg_sel_d2r = eg_rate_select[op[0].d2r + v];
604 op[0].eg_sh_rr = eg_rate_shift [op[0].rr + v];
605 op[0].eg_sel_rr = eg_rate_select[op[0].rr + v];
606
607 v = kc >> op[1].ks;
608 if ((op[1].ar + v) < 32 + 62) {
609 op[1].eg_sh_ar = eg_rate_shift [op[1].ar + v];
610 op[1].eg_sel_ar = eg_rate_select[op[1].ar + v];
611 } else {
612 op[1].eg_sh_ar = 0;
613 op[1].eg_sel_ar = 17 * RATE_STEPS;
614 }
615 op[1].eg_sh_d1r = eg_rate_shift [op[1].d1r + v];
616 op[1].eg_sel_d1r = eg_rate_select[op[1].d1r + v];
617 op[1].eg_sh_d2r = eg_rate_shift [op[1].d2r + v];
618 op[1].eg_sel_d2r = eg_rate_select[op[1].d2r + v];
619 op[1].eg_sh_rr = eg_rate_shift [op[1].rr + v];
620 op[1].eg_sel_rr = eg_rate_select[op[1].rr + v];
621
622 v = kc >> op[2].ks;
623 if ((op[2].ar + v) < 32 + 62) {
624 op[2].eg_sh_ar = eg_rate_shift [op[2].ar + v];
625 op[2].eg_sel_ar = eg_rate_select[op[2].ar + v];
626 } else {
627 op[2].eg_sh_ar = 0;
628 op[2].eg_sel_ar = 17 * RATE_STEPS;
629 }
630 op[2].eg_sh_d1r = eg_rate_shift [op[2].d1r + v];
631 op[2].eg_sel_d1r = eg_rate_select[op[2].d1r + v];
632 op[2].eg_sh_d2r = eg_rate_shift [op[2].d2r + v];
633 op[2].eg_sel_d2r = eg_rate_select[op[2].d2r + v];
634 op[2].eg_sh_rr = eg_rate_shift [op[2].rr + v];
635 op[2].eg_sel_rr = eg_rate_select[op[2].rr + v];
636
637 v = kc >> op[3].ks;
638 if ((op[3].ar + v) < 32 + 62) {
639 op[3].eg_sh_ar = eg_rate_shift [op[3].ar + v];
640 op[3].eg_sel_ar = eg_rate_select[op[3].ar + v];
641 } else {
642 op[3].eg_sh_ar = 0;
643 op[3].eg_sel_ar = 17 * RATE_STEPS;
644 }
645 op[3].eg_sh_d1r = eg_rate_shift [op[3].d1r + v];
646 op[3].eg_sel_d1r = eg_rate_select[op[3].d1r + v];
647 op[3].eg_sh_d2r = eg_rate_shift [op[3].d2r + v];
648 op[3].eg_sel_d2r = eg_rate_select[op[3].d2r + v];
649 op[3].eg_sh_rr = eg_rate_shift [op[3].rr + v];
650 op[3].eg_sel_rr = eg_rate_select[op[3].rr + v];
651}
652
653void YM2151::writeReg(uint8_t r, uint8_t v, EmuTime::param time)
654{
655 updateStream(time);
656
657 YM2151Operator& op = oper[(r & 0x07) * 4 + ((r & 0x18) >> 3)];
658
659 regs[r] = v;
660 switch (r & 0xe0) {
661 case 0x00:
662 switch (r) {
663 case 0x01:
664 case 0x09:
665 // the test register is located differently between the variants
666 if ((r == 1 && variant == Variant::YM2151) ||
667 (r == 9 && variant == Variant::YM2164)) {
668 test = v;
669 if (v & 2) lfo_phase = 0; // bit 1: LFO reset
670 }
671 break;
672
673 case 0x08:
674 envelopeKONKOFF(subspan<4>(oper, 4 * (v & 7)), v);
675 break;
676
677 case 0x0f: // noise mode enable, noise period
678 noise = v;
679 noise_f = noise_tab[v & 0x1f];
680 noise_p = 0;
681 break;
682
683 case 0x10:
684 timer_A_val &= 0x03;
685 timer_A_val |= v << 2;
686 timer1->setValue(timer_A_val);
687 break;
688
689 case 0x11:
690 timer_A_val &= 0x03fc;
691 timer_A_val |= v & 3;
692 timer1->setValue(timer_A_val);
693 break;
694
695 case 0x12:
696 timer2->setValue(v);
697 break;
698
699 case 0x14: // CSM, irq flag reset, irq enable, timer start/stop
700 irq_enable = v; // bit 3-timer B, bit 2-timer A, bit 7 - CSM
701 if (v & 0x10) { // reset timer A irq flag
702 resetStatus(1);
703 }
704 if (v & 0x20) { // reset timer B irq flag
705 resetStatus(2);
706 }
707 timer1->setStart((v & 4) != 0, time);
708 timer2->setStart((v & 8) != 0, time);
709 break;
710
711 case 0x18: // LFO frequency
712 lfo_overflow = (1 << ((15 - (v >> 4)) + 3));
713 lfo_counter_add = 0x10 + (v & 0x0f);
714 break;
715
716 case 0x19: // PMD (bit 7==1) or AMD (bit 7==0)
717 if (v & 0x80) {
718 pmd = narrow<int8_t>(v & 0x7f);
719 } else {
720 amd = v & 0x7f;
721 }
722 break;
723
724 case 0x1b: // CT2, CT1, LFO waveform
725 ct = v >> 6;
726 lfo_wsel = v & 3;
727 // TODO ym2151WritePortCallback(0 , ct);
728 break;
729
730 default:
731 break;
732 }
733 break;
734
735 case 0x20: {
736 auto o = subspan<4>(oper, 4 * (r & 7));
737 switch (r & 0x18) {
738 case 0x00: // RL enable, Feedback, Connection
739 o[0].fb_shift = ((v >> 3) & 7) ? ((v >> 3) & 7) + 6 : 0;
740 pan[(r & 7) * 2 + 0] = (v & 0x40) ? ~0 : 0;
741 pan[(r & 7) * 2 + 1] = (v & 0x80) ? ~0 : 0;
742 setConnect(o, r & 7, v & 7);
743 break;
744
745 case 0x08: // Key Code
746 v &= 0x7f;
747 if (v != o[0].kc) {
748 unsigned kc_channel = (v - (v>>2))*64;
749 kc_channel += 768;
750 kc_channel |= (o[0].kc_i & 63);
751
752 o[0].kc = v;
753 o[0].kc_i = kc_channel;
754 o[1].kc = v;
755 o[1].kc_i = kc_channel;
756 o[2].kc = v;
757 o[2].kc_i = kc_channel;
758 o[3].kc = v;
759 o[3].kc_i = kc_channel;
760
761 unsigned kc = v>>2;
762 o[0].dt1 = dt1_freq[o[0].dt1_i + kc];
763 o[0].freq = ((freq[kc_channel + o[0].dt2] + o[0].dt1) * o[0].mul) >> 1;
764
765 o[1].dt1 = dt1_freq[o[1].dt1_i + kc];
766 o[1].freq = ((freq[kc_channel + o[1].dt2] + o[1].dt1) * o[1].mul) >> 1;
767
768 o[2].dt1 = dt1_freq[o[2].dt1_i + kc];
769 o[2].freq = ((freq[kc_channel + o[2].dt2] + o[2].dt1) * o[2].mul) >> 1;
770
771 o[3].dt1 = dt1_freq[o[3].dt1_i + kc];
772 o[3].freq = ((freq[kc_channel + o[3].dt2] + o[3].dt1) * o[3].mul) >> 1;
773
774 refreshEG(o);
775 }
776 break;
777
778 case 0x10: // Key Fraction
779 v >>= 2;
780 if (v != (o[0].kc_i & 63)) {
781 unsigned kc_channel = v;
782 kc_channel |= (o[0].kc_i & ~63);
783
784 o[0].kc_i = kc_channel;
785 o[1].kc_i = kc_channel;
786 o[2].kc_i = kc_channel;
787 o[3].kc_i = kc_channel;
788
789 o[0].freq = ((freq[kc_channel + o[0].dt2] + o[0].dt1) * o[0].mul) >> 1;
790 o[1].freq = ((freq[kc_channel + o[1].dt2] + o[1].dt1) * o[1].mul) >> 1;
791 o[2].freq = ((freq[kc_channel + o[2].dt2] + o[2].dt1) * o[2].mul) >> 1;
792 o[3].freq = ((freq[kc_channel + o[3].dt2] + o[3].dt1) * o[3].mul) >> 1;
793 }
794 break;
795
796 case 0x18: // PMS, AMS
797 o[0].pms = narrow<int8_t>((v >> 4) & 7);
798 o[0].ams = (v & 3);
799 break;
800 }
801 break;
802 }
803 case 0x40: { // DT1, MUL
804 unsigned olddt1_i = op.dt1_i;
805 unsigned oldMul = op.mul;
806
807 op.dt1_i = (v & 0x70) << 1;
808 op.mul = (v & 0x0f) ? (v & 0x0f) << 1 : 1;
809
810 if (olddt1_i != op.dt1_i) {
811 op.dt1 = dt1_freq[op.dt1_i + (op.kc>>2)];
812 }
813 if ((olddt1_i != op.dt1_i) || (oldMul != op.mul)) {
814 op.freq = ((freq[op.kc_i + op.dt2] + op.dt1) * op.mul) >> 1;
815 }
816 break;
817 }
818 case 0x60: // TL
819 op.tl = (v & 0x7f) << (ENV_BITS - 7); // 7bit TL
820 break;
821
822 case 0x80: { // KS, AR
823 unsigned oldKs = op.ks;
824 unsigned oldAr = op.ar;
825 op.ks = 5 - (v >> 6);
826 op.ar = (v & 0x1f) ? 32 + ((v & 0x1f) << 1) : 0;
827
828 if ((op.ar != oldAr) || (op.ks != oldKs)) {
829 if ((op.ar + (op.kc >> op.ks)) < 32 + 62) {
830 op.eg_sh_ar = eg_rate_shift [op.ar + (op.kc>>op.ks)];
831 op.eg_sel_ar = eg_rate_select[op.ar + (op.kc>>op.ks)];
832 } else {
833 op.eg_sh_ar = 0;
834 op.eg_sel_ar = 17 * RATE_STEPS;
835 }
836 }
837 if (op.ks != oldKs) {
838 op.eg_sh_d1r = eg_rate_shift [op.d1r + (op.kc >> op.ks)];
839 op.eg_sel_d1r = eg_rate_select[op.d1r + (op.kc >> op.ks)];
840 op.eg_sh_d2r = eg_rate_shift [op.d2r + (op.kc >> op.ks)];
841 op.eg_sel_d2r = eg_rate_select[op.d2r + (op.kc >> op.ks)];
842 op.eg_sh_rr = eg_rate_shift [op.rr + (op.kc >> op.ks)];
843 op.eg_sel_rr = eg_rate_select[op.rr + (op.kc >> op.ks)];
844 }
845 break;
846 }
847 case 0xa0: // LFO AM enable, D1R
848 op.AMmask = (v & 0x80) ? ~0 : 0;
849 op.d1r = (v & 0x1f) ? 32 + ((v & 0x1f) << 1) : 0;
850 op.eg_sh_d1r = eg_rate_shift [op.d1r + (op.kc >> op.ks)];
851 op.eg_sel_d1r = eg_rate_select[op.d1r + (op.kc >> op.ks)];
852 break;
853
854 case 0xc0: { // DT2, D2R
855 unsigned olddt2 = op.dt2;
856 op.dt2 = dt2_tab[v >> 6];
857 if (op.dt2 != olddt2) {
858 op.freq = ((freq[op.kc_i + op.dt2] + op.dt1) * op.mul) >> 1;
859 }
860 op.d2r = (v & 0x1f) ? 32 + ((v & 0x1f) << 1) : 0;
861 op.eg_sh_d2r = eg_rate_shift [op.d2r + (op.kc >> op.ks)];
862 op.eg_sel_d2r = eg_rate_select[op.d2r + (op.kc >> op.ks)];
863 break;
864 }
865 case 0xe0: // D1L, RR
866 op.d1l = d1l_tab[v >> 4];
867 op.rr = 34 + ((v & 0x0f) << 2);
868 op.eg_sh_rr = eg_rate_shift [op.rr + (op.kc >> op.ks)];
869 op.eg_sel_rr = eg_rate_select[op.rr + (op.kc >> op.ks)];
870 break;
871 }
872}
873
874static constexpr auto INPUT_RATE = unsigned(cstd::round(3579545 / 64.0));
875
876YM2151::YM2151(const std::string& name_, static_string_view desc,
877 const DeviceConfig& config, EmuTime::param time, Variant variant_)
878 : ResampledSoundDevice(config.getMotherBoard(), name_, desc, 8, INPUT_RATE, true)
879 , irq(config.getMotherBoard(), getName() + ".IRQ")
880 , timer1(EmuTimer::createOPM_1(config.getScheduler(), *this))
881 , timer2(variant_ == Variant::YM2164 ? EmuTimer::createOPP_2(config.getScheduler(), *this)
882 : EmuTimer::createOPM_2(config.getScheduler(), *this))
883 , variant(variant_)
884{
885 // TODO Registers 0x20-0xFF are cleared on reset.
886 // Should we do the same for registers 0x00-0x1F?
887
888 if (false) {
889 std::cout << "tl_tab:";
890 for (const auto& e : tl_tab) std::cout << ' ' << e;
891 std::cout << '\n';
892
893 std::cout << "sin_tab:";
894 for (const auto& e : sin_tab) std::cout << ' ' << e;
895 std::cout << '\n';
896
897 std::cout << "d1l_tab:";
898 for (const auto& e : d1l_tab) std::cout << ' ' << e;
899 std::cout << '\n';
900
901 std::cout << "freq:";
902 for (const auto& e : freq) std::cout << ' ' << e;
903 std::cout << '\n';
904
905 std::cout << "dt1_freq:";
906 for (const auto& e : dt1_freq) std::cout << ' ' << e;
907 std::cout << '\n';
908
909 std::cout << "noise_tab:";
910 for (const auto& e : noise_tab) std::cout << ' ' << e;
911 std::cout << '\n';
912 }
913
914 reset(time);
915
916 registerSound(config);
917}
918
923
924bool YM2151::checkMuteHelper()
925{
926 return ranges::all_of(oper, [](auto& op) { return op.state == EG_OFF; });
927}
928
929void YM2151::reset(EmuTime::param time)
930{
931 // initialize hardware registers
932 for (auto& op : oper) {
933 memset(&op, '\0', sizeof(op));
934 op.volume = MAX_ATT_INDEX;
935 op.kc_i = 768; // min kc_i value
936 }
937
938 eg_timer = 0;
939 eg_cnt = 0;
940
941 lfo_timer = 0;
942 lfo_counter = 0;
943 lfo_phase = 0;
944 lfo_wsel = 0;
945 pmd = 0;
946 amd = 0;
947 lfa = 0;
948 lfp = 0;
949
950 test = 0;
951
952 irq_enable = 0;
953 timer1->setStart(false, time);
954 timer2->setStart(false, time);
955
956 noise = 0;
957 noise_rng = 0;
958 noise_p = 0;
959 noise_f = noise_tab[0];
960
961 csm_req = 0;
962 status = 0;
963
964 writeReg(0x1b, 0, time); // only because of CT1, CT2 output pins
965 writeReg(0x18, 0, time); // set LFO frequency
966 for (auto i : xrange(0x20, 0x100)) { // set the operators
967 writeReg(narrow<uint8_t>(i), 0, time);
968 }
969
970 irq.reset();
971}
972
973int YM2151::opCalc(YM2151Operator& op, unsigned env, int pm) const
974{
975 unsigned p = (env << 3) + sin_tab[(int((op.phase & ~FREQ_MASK) + (pm << 15)) >> FREQ_SH) & SIN_MASK];
976 if (p >= TL_TAB_LEN) {
977 return 0;
978 }
979 return tl_tab[p];
980}
981
982int YM2151::opCalc1(YM2151Operator& op, unsigned env, int pm) const
983{
984 int i = (narrow_cast<int>(op.phase) & ~FREQ_MASK) + pm;
985 unsigned p = (env << 3) + sin_tab[(i >> FREQ_SH) & SIN_MASK];
986 if (p >= TL_TAB_LEN) {
987 return 0;
988 }
989 return tl_tab[p];
990}
991
992unsigned YM2151::volumeCalc(YM2151Operator& op, unsigned AM) const
993{
994 return op.tl + unsigned(op.volume) + (AM & op.AMmask);
995}
996
997void YM2151::chanCalc(unsigned chan)
998{
999 m2 = c1 = c2 = mem = 0;
1000 auto op = subspan<4>(oper, 4 * chan); // M1
1001 *op[0].mem_connect = op[0].mem_value; // restore delayed sample (MEM) value to m2 or c2
1002
1003 unsigned AM = 0;
1004 if (op[0].ams) {
1005 AM = lfa << (op[0].ams-1);
1006 }
1007 unsigned env = volumeCalc(op[0], AM);
1008 {
1009 int out = op[0].fb_out_prev + op[0].fb_out_curr;
1010 op[0].fb_out_prev = op[0].fb_out_curr;
1011
1012 if (!op[0].connect) {
1013 // algorithm 5
1014 mem = c1 = c2 = op[0].fb_out_prev;
1015 } else {
1016 *op[0].connect = op[0].fb_out_prev;
1017 }
1018 op[0].fb_out_curr = 0;
1019 if (env < ENV_QUIET) {
1020 if (!op[0].fb_shift) {
1021 out = 0;
1022 }
1023 op[0].fb_out_curr = opCalc1(op[0], env, (out << op[0].fb_shift));
1024 }
1025 }
1026
1027 env = volumeCalc(op[1], AM); // M2
1028 if (env < ENV_QUIET) {
1029 *op[1].connect += opCalc(op[1], env, m2);
1030 }
1031 env = volumeCalc(op[2], AM); // C1
1032 if (env < ENV_QUIET) {
1033 *op[2].connect += opCalc(op[2], env, c1);
1034 }
1035 env = volumeCalc(op[3], AM); // C2
1036 if (env < ENV_QUIET) {
1037 chanOut[chan] += opCalc(op[3], env, c2);
1038 }
1039 // M1
1040 op[0].mem_value = mem;
1041}
1042
1043void YM2151::chan7Calc()
1044{
1045 m2 = c1 = c2 = mem = 0;
1046 auto op = subspan<4>(oper, 4 * 7);
1047
1048 *op[0].mem_connect = op[0].mem_value; // restore delayed sample (MEM) value to m2 or c2
1049
1050 unsigned AM = 0;
1051 if (op[0].ams) {
1052 AM = lfa << (op[0].ams - 1);
1053 }
1054 unsigned env = volumeCalc(op[0], AM);
1055 {
1056 int out = op[0].fb_out_prev + op[0].fb_out_curr;
1057 op[0].fb_out_prev = op[0].fb_out_curr;
1058
1059 if (!op[0].connect) {
1060 // algorithm 5
1061 mem = c1 = c2 = op[0].fb_out_prev;
1062 } else {
1063 // other algorithms
1064 *op[0].connect = op[0].fb_out_prev;
1065 }
1066 op[0].fb_out_curr = 0;
1067 if (env < ENV_QUIET) {
1068 if (!op[0].fb_shift) {
1069 out = 0;
1070 }
1071 op[0].fb_out_curr = opCalc1(op[0], env, (out << op[0].fb_shift));
1072 }
1073 }
1074
1075 env = volumeCalc(op[1], AM); // M2
1076 if (env < ENV_QUIET) {
1077 *op[1].connect += opCalc(op[1], env, m2);
1078 }
1079 env = volumeCalc(op[2], AM); // C1
1080 if (env < ENV_QUIET) {
1081 *op[2].connect += opCalc(op[2], env, c1);
1082 }
1083 env = volumeCalc(op[3], AM); // C2
1084 if (noise & 0x80) {
1085 int noiseOut = 0;
1086 if (env < 0x3ff) {
1087 noiseOut = (narrow<int>(env) ^ 0x3ff) * 2; // range of the YM2151 noise output is -2044 to 2040
1088 }
1089 chanOut[7] += (noise_rng & 0x10000) ? noiseOut : -noiseOut; // bit 16 -> output
1090 } else {
1091 if (env < ENV_QUIET) {
1092 chanOut[7] += opCalc(op[3], env, c2);
1093 }
1094 }
1095 // M1
1096 op[0].mem_value = mem;
1097}
1098
1099/*
1100The 'rate' is calculated from following formula (example on decay rate):
1101 rks = note-code after key scaling (a value from 0 to 31)
1102 DR = value written to the chip register
1103 rate = 2*DR + rks; (max rate = 2*31+31 = 93)
1104Four MSBs of the 'rate' above are the 'main' rate (from 00 to 15)
1105Two LSBs of the 'rate' above are the value 'x' (the shape type).
1106(eg. '11 2' means that 'rate' is 11*4+2=46)
1107
1108NOTE: A 'sample' in the description below is actually 3 output samples,
1109thats because the Envelope Generator clock is equal to internal_clock/3.
1110
1111Single '-' (minus) character in the diagrams below represents one sample
1112on the output; this is for rates 11 x (11 0, 11 1, 11 2 and 11 3)
1113
1114these 'main' rates:
111500 x: single '-' = 2048 samples; (ie. level can change every 2048 samples)
111601 x: single '-' = 1024 samples;
111702 x: single '-' = 512 samples;
111803 x: single '-' = 256 samples;
111904 x: single '-' = 128 samples;
112005 x: single '-' = 64 samples;
112106 x: single '-' = 32 samples;
112207 x: single '-' = 16 samples;
112308 x: single '-' = 8 samples;
112409 x: single '-' = 4 samples;
112510 x: single '-' = 2 samples;
112611 x: single '-' = 1 sample; (ie. level can change every 1 sample)
1127
1128Shapes for rates 11 x look like this:
1129rate: step:
113011 0 01234567
1131
1132level:
11330 --
11341 --
11352 --
11363 --
1137
1138rate: step:
113911 1 01234567
1140
1141level:
11420 --
11431 --
11442 -
11453 -
11464 --
1147
1148rate: step:
114911 2 01234567
1150
1151level:
11520 --
11531 -
11542 -
11553 --
11564 -
11575 -
1158
1159rate: step:
116011 3 01234567
1161
1162level:
11630 --
11641 -
11652 -
11663 -
11674 -
11685 -
11696 -
1170
1171
1172For rates 12 x, 13 x, 14 x and 15 x output level changes on every
1173sample - this means that the waveform looks like this: (but the level
1174changes by different values on different steps)
117512 3 01234567
1176
11770 -
11782 -
11794 -
11808 -
118110 -
118212 -
118314 -
118418 -
118520 -
1186
1187Notes about the timing:
1188----------------------
1189
11901. Synchronism
1191
1192Output level of each two (or more) voices running at the same 'main' rate
1193(eg 11 0 and 11 1 in the diagram below) will always be changing in sync,
1194even if there're started with some delay.
1195
1196Note that, in the diagram below, the decay phase in channel 0 starts at
1197sample #2, while in channel 1 it starts at sample #6. Anyway, both channels
1198will always change their levels at exactly the same (following) samples.
1199
1200(S - start point of this channel, A-attack phase, D-decay phase):
1201
1202step:
120301234567012345670123456
1204
1205channel 0:
1206 --
1207 | --
1208 | -
1209 | -
1210 | --
1211 | --
1212| --
1213| -
1214| -
1215| --
1216AADDDDDDDDDDDDDDDD
1217S
1218
121901234567012345670123456
1220channel 1:
1221 -
1222 | -
1223 | --
1224 | --
1225 | --
1226 | -
1227 | -
1228 | --
1229 | --
1230 | --
1231 AADDDDDDDDDDDDDDDD
1232 S
123301234567012345670123456
1234
1235
12362. Shifted (delayed) synchronism
1237
1238Output of each two (or more) voices running at different 'main' rate
1239(9 1, 10 1 and 11 1 in the diagrams below) will always be changing
1240in 'delayed-sync' (even if there're started with some delay as in "1.")
1241
1242Note that the shapes are delayed by exactly one sample per one 'main' rate
1243increment. (Normally one would expect them to start at the same samples.)
1244
1245See diagram below (* - start point of the shape).
1246
1247cycle:
12480123456701234567012345670123456701234567012345670123456701234567
1249
1250rate 09 1
1251*-------
1252 --------
1253 ----
1254 ----
1255 --------
1256 *-------
1257 | --------
1258 | ----
1259 | ----
1260 | --------
1261rate 10 1 |
1262-- |
1263 *--- |
1264 ---- |
1265 -- |
1266 -- |
1267 ---- |
1268 *--- |
1269 | ---- |
1270 | -- | | <- one step (two samples) delay between 9 1 and 10 1
1271 | -- | |
1272 | ----|
1273 | *---
1274 | ----
1275 | --
1276 | --
1277 | ----
1278rate 11 1 |
1279- |
1280 -- |
1281 *- |
1282 -- |
1283 - |
1284 - |
1285 -- |
1286 *- |
1287 -- |
1288 - || <- one step (one sample) delay between 10 1 and 11 1
1289 - ||
1290 --|
1291 *-
1292 --
1293 -
1294 -
1295 --
1296 *-
1297 --
1298 -
1299 -
1300 --
1301*/
1302
1303void YM2151::advanceEG()
1304{
1305 if (eg_timer++ != 3) {
1306 // envelope generator timer overflows every 3 samples (on real chip)
1307 return;
1308 }
1309 eg_timer = 0;
1310 eg_cnt++;
1311
1312 // envelope generator
1313 for (auto& op : oper) {
1314 switch (op.state) {
1315 case EG_ATT: // attack phase
1316 if (!(eg_cnt & ((1 << op.eg_sh_ar) - 1))) {
1317 op.volume += (~op.volume *
1318 (eg_inc[op.eg_sel_ar + ((eg_cnt >> op.eg_sh_ar) & 7)])
1319 ) >> 4;
1320 if (op.volume <= MIN_ATT_INDEX) {
1321 op.volume = MIN_ATT_INDEX;
1322 op.state = EG_DEC;
1323 }
1324 }
1325 break;
1326
1327 case EG_DEC: // decay phase
1328 if (!(eg_cnt & ((1 << op.eg_sh_d1r) - 1))) {
1329 op.volume += eg_inc[op.eg_sel_d1r + ((eg_cnt >> op.eg_sh_d1r) & 7)];
1330 if (unsigned(op.volume) >= op.d1l) {
1331 op.state = EG_SUS;
1332 }
1333 }
1334 break;
1335
1336 case EG_SUS: // sustain phase
1337 if (!(eg_cnt & ((1 << op.eg_sh_d2r) - 1))) {
1338 op.volume += eg_inc[op.eg_sel_d2r + ((eg_cnt >> op.eg_sh_d2r) & 7)];
1339 if (op.volume >= MAX_ATT_INDEX) {
1340 op.volume = MAX_ATT_INDEX;
1341 op.state = EG_OFF;
1342 }
1343 }
1344 break;
1345
1346 case EG_REL: // release phase
1347 if (!(eg_cnt & ((1 << op.eg_sh_rr) - 1))) {
1348 op.volume += eg_inc[op.eg_sel_rr + ((eg_cnt >> op.eg_sh_rr) & 7)];
1349 if (op.volume >= MAX_ATT_INDEX) {
1350 op.volume = MAX_ATT_INDEX;
1351 op.state = EG_OFF;
1352 }
1353 }
1354 break;
1355 }
1356 }
1357}
1358
1359void YM2151::advance()
1360{
1361 // LFO
1362 if (test & 2) {
1363 lfo_phase = 0;
1364 } else {
1365 if (lfo_timer++ >= lfo_overflow) {
1366 lfo_timer = 0;
1367 lfo_counter += lfo_counter_add;
1368 lfo_phase += (lfo_counter >> 4);
1369 lfo_phase &= 255;
1370 lfo_counter &= 15;
1371 }
1372 }
1373
1374 unsigned i = lfo_phase;
1375 // calculate LFO AM and PM waveform value (all verified on real chip,
1376 // except for noise algorithm which is impossible to analyze)
1377 auto [a, p] = [&]() -> std::pair<int, int> {
1378 switch (lfo_wsel) {
1379 case 0:
1380 // saw
1381 // AM: 255 down to 0
1382 // PM: 0 to 127, -127 to 0 (at PMD=127: LFP = 0 to 126, -126 to 0)
1383 return {
1384 /*a =*/ (255 - i),
1385 /*p =*/ ((i < 128) ? i : (i - 255))
1386 };
1387 case 1:
1388 // square
1389 // AM: 255, 0
1390 // PM: 128,-128 (LFP = exactly +PMD, -PMD)
1391 return {
1392 /*a =*/ ((i < 128) ? 255 : 0),
1393 /*p =*/ ((i < 128) ? 128 : -128)
1394 };
1395 case 2:
1396 // triangle
1397 // AM: 255 down to 1 step -2; 0 up to 254 step +2
1398 // PM: 0 to 126 step +2, 127 to 1 step -2,
1399 // 0 to -126 step -2, -127 to -1 step +2
1400 return {
1401 /*a =*/ ((i < 128) ? (255 - (i * 2)) : ((i * 2) - 256)),
1402 /*p =*/ [&] {
1403 if (i < 64) { // i = 0..63
1404 return i * 2; // 0 to 126 step +2
1405 } else if (i < 128) { // i = 64..127
1406 return 255 - i * 2; // 127 to 1 step -2
1407 } else if (i < 192) { // i = 128..191
1408 return 256 - i * 2; // 0 to -126 step -2
1409 } else { // i = 192..255
1410 return i * 2 - 511; // -127 to -1 step +2
1411 }
1412 }()
1413 };
1414 break;
1415 case 3:
1416 default: // keep the compiler happy
1417 // Random. The real algorithm is unknown !!!
1418 // We just use a snapshot of data from real chip
1419
1420 // AM: range 0 to 255
1421 // PM: range -128 to 127
1422 return {
1423 /*a =*/ lfo_noise_waveform[i],
1424 /*p =*/ (lfo_noise_waveform[i] - 128)
1425 };
1426 }
1427 }();
1428 lfa = a * amd / 128;
1429 lfp = p * pmd / 128;
1430
1431 // The Noise Generator of the YM2151 is 17-bit shift register.
1432 // Input to the bit16 is negated (bit0 XOR bit3) (XNOR).
1433 // Output of the register is negated (bit0 XOR bit3).
1434 // Simply use bit16 as the noise output.
1435
1436 // noise changes depending on the index in noise_tab (noise_f = noise_tab[x])
1437 // noise_tab contains how many cycles/samples (x2) the noise should change.
1438 // so, when it contains 29, noise should change every 14.5 cycles (2 out of 29).
1439 // if you read this code well, you'll see that is what happens here :)
1440 noise_p -= 2;
1441 if (noise_p < 0) {
1442 noise_p += noise_f;
1443 unsigned j = ((noise_rng ^ (noise_rng >> 3)) & 1) ^ 1;
1444 noise_rng = (j << 16) | (noise_rng >> 1);
1445 }
1446
1447 // phase generator
1448 for (auto c : xrange(8)) {
1449 auto op = subspan<4>(oper, 4 * c);
1450 // only when phase modulation from LFO is enabled for this channel
1451 if (op[0].pms) {
1452 int mod_ind = lfp; // -128..+127 (8bits signed)
1453 if (op[0].pms < 6) {
1454 mod_ind >>= (6 - op[0].pms);
1455 } else {
1456 mod_ind <<= (op[0].pms - 5);
1457 }
1458 if (mod_ind) {
1459 unsigned kc_channel = op[0].kc_i + mod_ind;
1460 op[0].phase += ((freq[kc_channel + op[0].dt2] + op[0].dt1) * op[0].mul) >> 1;
1461 op[1].phase += ((freq[kc_channel + op[1].dt2] + op[1].dt1) * op[1].mul) >> 1;
1462 op[2].phase += ((freq[kc_channel + op[2].dt2] + op[2].dt1) * op[2].mul) >> 1;
1463 op[3].phase += ((freq[kc_channel + op[3].dt2] + op[3].dt1) * op[3].mul) >> 1;
1464 } else { // phase modulation from LFO is equal to zero
1465 op[0].phase += op[0].freq;
1466 op[1].phase += op[1].freq;
1467 op[2].phase += op[2].freq;
1468 op[3].phase += op[3].freq;
1469 }
1470 } else { // phase modulation from LFO is disabled
1471 op[0].phase += op[0].freq;
1472 op[1].phase += op[1].freq;
1473 op[2].phase += op[2].freq;
1474 op[3].phase += op[3].freq;
1475 }
1476 }
1477
1478 // CSM is calculated *after* the phase generator calculations (verified
1479 // on real chip)
1480 // CSM keyon line seems to be ORed with the KO line inside of the chip.
1481 // The result is that it only works when KO (register 0x08) is off, ie. 0
1482 //
1483 // Interesting effect is that when timer A is set to 1023, the KEY ON happens
1484 // on every sample, so there is no KEY OFF at all - the result is that
1485 // the sound played is the same as after normal KEY ON.
1486 if (csm_req) { // CSM KEYON/KEYOFF sequence request
1487 if (csm_req == 2) { // KEY ON
1488 for (auto& op : oper) {
1489 keyOn(op, 2);
1490 }
1491 csm_req = 1;
1492 } else { // KEY OFF
1493 for (auto& op : oper) {
1494 keyOff(op, unsigned(~2));
1495 }
1496 csm_req = 0;
1497 }
1498 }
1499}
1500
1501void YM2151::generateChannels(std::span<float*> bufs, unsigned num)
1502{
1503 if (checkMuteHelper()) {
1504 // TODO update internal state, even if muted
1505 ranges::fill(bufs, nullptr);
1506 return;
1507 }
1508
1509 for (auto i : xrange(num)) {
1510 advanceEG();
1511
1512 for (auto j : xrange(8 - 1)) {
1513 chanOut[j] = 0;
1514 chanCalc(j);
1515 }
1516 chanOut[7] = 0;
1517 chan7Calc(); // special case for channel 7
1518
1519 for (auto j : xrange(8)) {
1520 bufs[j][2 * i + 0] += narrow_cast<float>(narrow_cast<int>(chanOut[j] & pan[2 * j + 0]));
1521 bufs[j][2 * i + 1] += narrow_cast<float>(narrow_cast<int>(chanOut[j] & pan[2 * j + 1]));
1522 }
1523 advance();
1524 }
1525}
1526
1527void YM2151::callback(uint8_t flag)
1528{
1529 assert(flag == one_of(1, 2));
1530 setStatus(flag);
1531
1532 if ((flag == 1) && (irq_enable & 0x80)) { // Timer 1
1533 csm_req = 2; // request KEY ON / KEY OFF sequence
1534 }
1535}
1536
1537uint8_t YM2151::readStatus() const
1538{
1539 return status;
1540}
1541
1542void YM2151::setStatus(uint8_t flags)
1543{
1544 status |= flags;
1545 auto enable = (irq_enable >> 2) & 3;
1546 if ((status & enable) != 0) {
1547 irq.set();
1548 }
1549}
1550
1551void YM2151::resetStatus(uint8_t flags)
1552{
1553 status &= ~flags;
1554 auto enable = (irq_enable >> 2) & 3;
1555 if ((status & enable) == 0) {
1556 irq.reset();
1557 }
1558}
1559
1560
1561template<typename Archive>
1562void YM2151::YM2151Operator::serialize(Archive& a, unsigned /*version*/)
1563{
1564 //int* connect; // recalculated from regs[0x20-0x27]
1565 //int* mem_connect; // recalculated from regs[0x20-0x27]
1566 a.serialize("phase", phase,
1567 "freq", freq,
1568 "dt1", dt1,
1569 "mul", mul,
1570 "dt1_i", dt1_i,
1571 "dt2", dt2,
1572 "mem_value", mem_value,
1573 //"fb_shift", fb_shift, // recalculated from regs[0x20-0x27]
1574 "fb_out_curr", fb_out_curr,
1575 "fb_out_prev", fb_out_prev,
1576 "kc", kc,
1577 "kc_i", kc_i,
1578 "pms", pms,
1579 "ams", ams,
1580 "AMmask", AMmask,
1581 "state", state,
1582 "tl", tl,
1583 "volume", volume,
1584 "d1l", d1l,
1585 "key", key,
1586 "ks", ks,
1587 "ar", ar,
1588 "d1r", d1r,
1589 "d2r", d2r,
1590 "rr", rr,
1591 "eg_sh_ar", eg_sh_ar,
1592 "eg_sel_ar", eg_sel_ar,
1593 "eg_sh_d1r", eg_sh_d1r,
1594 "eg_sel_d1r", eg_sel_d1r,
1595 "eg_sh_d2r", eg_sh_d2r,
1596 "eg_sel_d2r", eg_sel_d2r,
1597 "eg_sh_rr", eg_sh_rr,
1598 "eg_sel_rr", eg_sel_rr);
1599};
1600
1601template<typename Archive>
1602void YM2151::serialize(Archive& a, unsigned /*version*/)
1603{
1604 a.serialize("irq", irq,
1605 "timer1", *timer1,
1606 "timer2", *timer2,
1607 "operators", oper,
1608 //"pan", pan, // recalculated from regs[0x20-0x27]
1609 "eg_cnt", eg_cnt,
1610 "eg_timer", eg_timer,
1611 "lfo_phase", lfo_phase,
1612 "lfo_timer", lfo_timer,
1613 "lfo_overflow", lfo_overflow,
1614 "lfo_counter", lfo_counter,
1615 "lfo_counter_add", lfo_counter_add,
1616 "lfa", lfa,
1617 "lfp", lfp,
1618 "noise", noise,
1619 "noise_rng", noise_rng,
1620 "noise_p", noise_p,
1621 "noise_f", noise_f,
1622 "csm_req", csm_req,
1623 "irq_enable", irq_enable,
1624 "status", status,
1625 "chanout", chanOut,
1626 "m2", m2,
1627 "c1", c1,
1628 "c2", c2,
1629 "mem", mem,
1630 "timer_A_val", timer_A_val,
1631 "lfo_wsel", lfo_wsel,
1632 "amd", amd,
1633 "pmd", pmd,
1634 "test", test,
1635 "ct", ct);
1636 a.serialize_blob("registers", regs);
1637
1638 if constexpr (Archive::IS_LOADER) {
1639 // TODO restore more state from registers
1640 EmuTime::param time = timer1->getCurrentTime();
1641 for (auto r : xrange(uint8_t(0x20), uint8_t(0x28))) {
1642 writeReg(r , regs[r], time);
1643 }
1644 }
1645}
1647
1648} // namespace openmsx
void set()
Set the interrupt request on the bus.
Definition IRQHelper.hh:74
void reset()
Reset the interrupt request on the bus.
Definition IRQHelper.hh:83
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:1537
void reset(EmuTime::param time)
Definition YM2151.cc:929
void serialize(Archive &ar, unsigned version)
Definition YM2151.cc:1602
void writeReg(uint8_t r, uint8_t v, EmuTime::param time)
Definition YM2151.cc:653
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
bool all_of(InputRange &&range, UnaryPredicate pred)
Definition ranges.hh:186
constexpr void fill(ForwardRange &&range, const T &value)
Definition ranges.hh:305
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
constexpr auto xrange(T e)
Definition xrange.hh:132