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