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