Human68k CUI emulator with sound.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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