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