summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/libflac/doc/html/documentation.html
blob: 2b40f170e54f86525daf0fba6d09788c65ffd6c9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- Copyright (c) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson -->
<!-- Permission is granted to copy, distribute and/or modify this document -->
<!-- under the terms of the GNU Free Documentation License, Version 1.1 -->
<!-- or any later version published by the Free Software Foundation; -->
<!-- with no invariant sections. -->
<!-- A copy of the license can be found at http://www.gnu.org/copyleft/fdl.html -->
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<meta name="author" content="Josh Coalson" />
	<meta name="description" content="A free, open source codec for lossless audio compression and decompression" />
	<meta name="keywords" content="free,lossless,audio,codec,encoder,decoder,compression,compressor,archival,archive,archiving,backup,music" />
	<link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
	<link rel="stylesheet" type="text/css" href="flac.css" />
	<title>FLAC - documentation</title>
</head>

<body>

<div class="logo">
	<a href="http://flac.sourceforge.net/"><img src="images/logo130.gif" alt="FLAC Logo" align="middle" border="0" hspace="0" /></a>
</div>

<div class="above_nav"></div>

<div class="navbar">
	&nbsp;<a href="index.html">home</a>&nbsp;&nbsp;|
	&nbsp;<a href="faq.html">faq</a>&nbsp;&nbsp;|
	&nbsp;<a href="news.html">news</a>&nbsp;&nbsp;|
	&nbsp;<a href="download.html">download</a>&nbsp;&nbsp;|
	&nbsp;documentation&nbsp;&nbsp;|
	&nbsp;<a href="comparison.html">comparison</a>&nbsp;&nbsp;|
	&nbsp;<a href="changelog.html">changelog</a>&nbsp;&nbsp;|
	&nbsp;<a href="links.html">links</a>&nbsp;&nbsp;|
	&nbsp;<a href="developers.html">developers</a>&nbsp;
</div>

<div class="langbar">
	&nbsp;english&nbsp;&nbsp;|
	&nbsp;<a href="ru/documentation.html">russian</a>&nbsp;
</div>

<div class="below_nav"></div>

<div class="box">
	<div class="box_title">
		documentation
	</div>
	<div class="box_header"></div>
	<div class="box_body">
		FLAC is a general purpose audio format supported by many programs.  Most of the documentation here is about the FLAC format itself and the tools we provide, but there is also information on using other programs that support FLAC.
		<ul>
			<li><a href="features.html">Introduction</a> - What is FLAC?</li>
			<li><a href="download.html">Getting FLAC</a> - How to download what you need to play or make FLAC files.</li>
			<li><a href="documentation_tasks.html">Using FLAC</a> - If you have some FLAC files and want to do something with them, or want to create FLAC files, look here.</li>
			<li><a href="faq.html">FAQ</a> - Frequently Asked Questions</li>
		</ul>
		In more detail:
		<ul>
			<li><a href="documentation_format_overview.html">About the FLAC Format</a> - An overview of the FLAC format for power users.</li>
			<li><a href="documentation_tools.html">Official Tools</a> - How to use the <span class="commandname">flac</span> and <span class="commandname">metaflac</span> command-line tools.</li>
			<li><a href="comparison.html">Comparison</a> - A comparison of FLAC with other lossless codecs.</li>
			<li><a href="documentation_bugs.html">Bugs</a> - How to report bugs and request features, and a list of known bugs in the FLAC tools.</li>
			<li><a href="http://sourceforge.net/tracker/?group_id=13478&amp;atid=213478">Request Support</a> - Support for the official FLAC tools.  For other programs, use <a href="http://www.hydrogenaudio.org/">hydrogenaudio.org</a>
			<li><a href="http://lists.xiph.org/mailman/listinfo/flac">FLAC Mailing List</a> - General discussion about FLAC, tools, releases, etc.  (You must subscribe to post.)</li>
		</ul>
		For developers who want to add FLAC support to their programs:
		<ul>
			<li><a href="documentation_format_overview.html">About the FLAC Format</a> - An overview of the FLAC format for power users.</li>
			<li><a href="format.html">FLAC Format Specification</a> - The formal specification.</li>
			<li><a href="ogg_mapping.html">Ogg FLAC Mapping</a> - How FLAC should be embedded in an Ogg container.</li>
			<li><a href="api/index.html">libFLAC and libFLAC++ APIs</a> - Complete library documentation.</li>
			<li><a href="documentation_example_code.html">Example Code</a> - Some simple example programs demonstrating the use of libFLAC and libFLAC++.</li>
			<li><a href="license.html">License</a> - The license under which the official tools are distributed.</li>
			<li>(see also the <a href="developer.html">developer page</a>)</li>
		</ul>
		Keep in mind that the online version of the documentation will always apply to the latest release.  For older releases, check the documentation included with the release package.
	</div>
	<div class="box_footer"></div>
</div>


<div class="copyright">
	<!-- @@@ oh so hacky -->
	<table>
		<tr>
			<td align="left">
				Copyright (c) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson
			</td>
			<td width="1%" align="right">
				<a href="http://sourceforge.net"><img src="http://sflogo.sourceforge.net/sflogo.php?group_id=13478&amp;type=1" width="88" height="31" border="0" alt="SourceForge.net Logo" /></a>
			</td>
			<td width="1%" align="right">
				<a href="http://www.eff.org/cafe/"><img src="images/cafebug.gif" alt="CAFE Logo" border="0" /></a><br />
			</td>
			<td width="1%" align="right">
				<a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31" width="88" border="0" hspace="0" /></a>
			</td>
		</tr>
	</table>
</div>

</body>
</html>
> 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/**********************************************************************************************

     Ensoniq ES5505/6 driver
     by Aaron Giles

Ensoniq OTIS - ES5505                                            Ensoniq OTTO - ES5506

  OTIS is a VLSI device designed in a 2 micron double metal        OTTO is a VLSI device designed in a 1.5 micron double metal
   CMOS process. The device is the next generation of audio         CMOS process. The device is the next generation of audio
   technology from ENSONIQ. This new chip achieves a new            technology from ENSONIQ. All calculations in the device are
   level of audio fidelity performance. These improvements          made with at least 18-bit accuracy.
   are achieved through the use of frequency interpolation
   and on board real time digital filters. All calculations       The major features of OTTO are:
   in the device are made with at least 16 bit accuracy.           - 68 pin PLCC package
                                                                   - On chip real time digital filters
 The major features of OTIS are:                                   - Frequency interpolation
  - 48 Pin dual in line package                                    - 32 independent voices
  - On chip real time digital filters                              - Loop start and stop posistions for each voice
  - Frequency interpolation                                        - Bidirectional and reverse looping
  - 32 independent voices (up from 25 in DOCII)                    - 68000 compatibility for asynchronous bus communication
  - Loop start and stop positions for each voice                   - separate host and sound memory interface
  - Bidirectional and reverse looping                              - 6 channel stereo serial communication port
  - 68000 compatibility for asynchronous bus communication         - Programmable clocks for defining serial protocol
  - On board pulse width modulation D to A                         - Internal volume multiplication and stereo panning
  - 4 channel stereo serial communication port                     - A to D input for pots and wheels
  - Internal volume multiplication and stereo panning              - Hardware support for envelopes
  - A to D input for pots and wheels                               - Support for dual OTTO systems
  - Up to 10MHz operation                                          - Optional compressed data format for sample data
                                                                   - Up to 16MHz operation
              ______    ______
            _|o     \__/      |_
 A17/D13 - |_|1             48|_| - VSS                                                           A A A A A A
            _|                |_                                                                  2 1 1 1 1 1 A
 A18/D14 - |_|2             47|_| - A16/D12                                                       0 9 8 7 6 5 1
            _|                |_                                                                  / / / / / / 4
 A19/D15 - |_|3             46|_| - A15/D11                                   H H H H H H H V V H D D D D D D /
            _|                |_                                              D D D D D D D S D D 1 1 1 1 1 1 D
      BS - |_|4             45|_| - A14/D10                                   0 1 2 3 4 5 6 S D 7 5 4 3 2 1 0 9
            _|                |_                                             ------------------------------------+
  PWZERO - |_|5             44|_| - A13/D9                                  / 9 8 7 6 5 4 3 2 1 6 6 6 6 6 6 6 6  |
            _|                |_                                           /                    8 7 6 5 4 3 2 1  |
    SER0 - |_|6             43|_| - A12/D8                                |                                      |
            _|       E        |_                                      SER0|10                                  60|A13/D8
    SER1 - |_|7      N      42|_| - A11/D7                            SER1|11                                  59|A12/D7
            _|       S        |_                                      SER2|12                                  58|A11/D6
    SER2 - |_|8      O      41|_| - A10/D6                            SER3|13              ENSONIQ             57|A10/D5
            _|       N        |_                                      SER4|14                                  56|A9/D4
    SER3 - |_|9      I      40|_| - A9/D5                             SER5|15                                  55|A8/D3
            _|       Q        |_                                      WCLK|16                                  54|A7/D2
 SERWCLK - |_|10            39|_| - A8/D4                            LRCLK|17               ES5506             53|A6/D1
            _|                |_                                      BCLK|18                                  52|A5/D0
   SERLR - |_|11            38|_| - A7/D3                             RESB|19                                  51|A4
            _|                |_                                       HA5|20                                  50|A3
 SERBCLK - |_|12     E      37|_| - A6/D2                              HA4|21                OTTO              49|A2
            _|       S        |_                                       HA3|22                                  48|A1
     RLO - |_|13     5      36|_| - A5/D1                              HA2|23                                  47|A0
            _|       5        |_                                       HA1|24                                  46|BS1
     RHI - |_|14     0      35|_| - A4/D0                              HA0|25                                  45|BS0
            _|       5        |_                                    POT_IN|26                                  44|DTACKB
     LLO - |_|15            34|_| - CLKIN                                 |   2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4  |
            _|                |_                                          |   7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3  |
     LHI - |_|16            33|_| - CAS                                   +--------------------------------------+
            _|                |_                                              B E E B E B B D S B B B E K B W W
     POT - |_|17     O      32|_| - AMUX                                      S B L N L S S D S S X S   L Q / /
            _|       T        |_                                              E E R E H M C V V A U A   C R R R
   DTACK - |_|18     I      31|_| - RAS                                       R R D H           R M C     I M
            _|       S        |_                                              _ D                 A
     R/W - |_|19            30|_| - E                                         T
            _|                |_                                              O
      MS - |_|20            29|_| - IRQ                                       P
            _|                |_
      CS - |_|21            28|_| - A3
            _|                |_
     RES - |_|22            27|_| - A2
            _|                |_
     VSS - |_|23            26|_| - A1
            _|                |_
     VDD - |_|24            25|_| - A0
             |________________|

***********************************************************************************************/

#include "emu.h"
#include "es5506.h"

#if ES5506_MAKE_WAVS
#include "sound/wavwrite.h"
#endif


/**********************************************************************************************

     CONSTANTS

***********************************************************************************************/

#define VERBOSE                 0
#include "logmacro.h"

#define RAINE_CHECK             0


#define MAX_SAMPLE_CHUNK        10000
#define ULAW_MAXBITS            8

#define CONTROL_BS1             0x8000
#define CONTROL_BS0             0x4000
#define CONTROL_CMPD            0x2000
#define CONTROL_CA2             0x1000
#define CONTROL_CA1             0x0800
#define CONTROL_CA0             0x0400
#define CONTROL_LP4             0x0200
#define CONTROL_LP3             0x0100
#define CONTROL_IRQ             0x0080
#define CONTROL_DIR             0x0040
#define CONTROL_IRQE            0x0020
#define CONTROL_BLE             0x0010
#define CONTROL_LPE             0x0008
#define CONTROL_LEI             0x0004
#define CONTROL_STOP1           0x0002
#define CONTROL_STOP0           0x0001

#define CONTROL_BSMASK          (CONTROL_BS1 | CONTROL_BS0)
#define CONTROL_CAMASK          (CONTROL_CA2 | CONTROL_CA1 | CONTROL_CA0)
#define CONTROL_LPMASK          (CONTROL_LP4 | CONTROL_LP3)
#define CONTROL_LOOPMASK        (CONTROL_BLE | CONTROL_LPE)
#define CONTROL_STOPMASK        (CONTROL_STOP1 | CONTROL_STOP0)


es550x_device::es550x_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, type, tag, owner, clock),
		device_sound_interface(mconfig, *this),
		m_stream(nullptr),
		m_sample_rate(0),
		m_write_latch(0),
		m_read_latch(0),
		m_master_clock(0),
		m_current_page(0),
		m_active_voices(0),
		m_mode(0),
		m_wst(0),
		m_wend(0),
		m_lrend(0),
		m_irqv(0),
		m_scratch(nullptr),
		m_ulaw_lookup(nullptr),
		m_volume_lookup(nullptr),
#if ES5506_MAKE_WAVS
		m_wavraw(nullptr),
#endif
		m_region0(nullptr),
		m_region1(nullptr),
		m_region2(nullptr),
		m_region3(nullptr),
		m_channels(0),
		m_irq_cb(*this),
		m_read_port_cb(*this),
		m_sample_rate_changed_cb(*this)
{
	for (auto & elem : m_region_base)
	{
		elem = nullptr;
	}
}

DEFINE_DEVICE_TYPE(ES5506, es5506_device, "es5506", "Ensoniq ES5506")

es5506_device::es5506_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: es550x_device(mconfig, ES5506, tag, owner, clock)
{
}

//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------
void es550x_device::device_start()
{
}

void es5506_device::device_start()
{
	int j;
	uint32_t accum_mask;
	int channels = 1;  /* 1 channel by default, for backward compatibility */

	/* only override the number of channels if the value is in the valid range 1 .. 6 */
	if (1 <= m_channels && m_channels <= 6)
		channels = m_channels;

	/* create the stream */
	m_stream = machine().sound().stream_alloc(*this, 0, 2 * channels, clock() / (16*32));

	/* initialize the regions */
	m_region_base[0] = m_region_base[1] = m_region_base[2] = m_region_base[3] = nullptr;
	if (m_region0)
	{
		memory_region *region0 = machine().root_device().memregion(m_region0);
		if (region0 != nullptr)
		{
			m_region_base[0] = (uint16_t *)region0->base();
		}
	}
	if (m_region1)
	{
		memory_region *region1 = machine().root_device().memregion(m_region1);
		if (region1 != nullptr)
		{
			m_region_base[1] = (uint16_t *)region1->base();
		}
	}
	if (m_region2)
	{
		memory_region *region2 = machine().root_device().memregion(m_region2);
		if (region2 != nullptr)
		{
			m_region_base[2] = (uint16_t *)region2->base();
		}
	}
	if (m_region3)
	{
		memory_region *region3 = machine().root_device().memregion(m_region3);
		if (region3 != nullptr)
		{
			m_region_base[3] = (uint16_t *)region3->base();
		}
	}

	/* initialize the rest of the structure */
	m_master_clock = clock();
	m_irq_cb.resolve();
	m_read_port_cb.resolve();
	m_sample_rate_changed_cb.resolve();
	m_irqv = 0x80;
	m_channels = channels;

	/* KT-76 assumes all voices are active on an ES5506 without setting them! */
	m_active_voices = 31;
	m_sample_rate = m_master_clock / (16 * (m_active_voices + 1));
	m_stream->set_sample_rate(m_sample_rate);

	/* compute the tables */
	compute_tables();

	/* init the voices */
	accum_mask = 0xffffffff;
	for (j = 0; j < 32; j++)
	{
		m_voice[j].index = j;
		m_voice[j].control = CONTROL_STOPMASK;
		m_voice[j].lvol = 0xffff;
		m_voice[j].rvol = 0xffff;
		m_voice[j].exbank = 0;
		m_voice[j].accum_mask = accum_mask;
	}

	/* allocate memory */
	m_scratch = make_unique_clear<int32_t[]>(2 * MAX_SAMPLE_CHUNK);

	/* register save */
	save_item(NAME(m_sample_rate));
	save_item(NAME(m_write_latch));
	save_item(NAME(m_read_latch));

	save_item(NAME(m_current_page));
	save_item(NAME(m_active_voices));
	save_item(NAME(m_mode));
	save_item(NAME(m_wst));
	save_item(NAME(m_wend));
	save_item(NAME(m_lrend));
	save_item(NAME(m_irqv));

	save_pointer(NAME(m_scratch), 2 * MAX_SAMPLE_CHUNK);

	for (j = 0; j < 32; j++)
	{
		save_item(NAME(m_voice[j].control), j);
		save_item(NAME(m_voice[j].freqcount), j);
		save_item(NAME(m_voice[j].start), j);
		save_item(NAME(m_voice[j].lvol), j);
		save_item(NAME(m_voice[j].end), j);
		save_item(NAME(m_voice[j].lvramp), j);
		save_item(NAME(m_voice[j].accum), j);
		save_item(NAME(m_voice[j].rvol), j);
		save_item(NAME(m_voice[j].rvramp), j);
		save_item(NAME(m_voice[j].ecount), j);
		save_item(NAME(m_voice[j].k2), j);
		save_item(NAME(m_voice[j].k2ramp), j);
		save_item(NAME(m_voice[j].k1), j);
		save_item(NAME(m_voice[j].k1ramp), j);
		save_item(NAME(m_voice[j].o4n1), j);
		save_item(NAME(m_voice[j].o3n1), j);
		save_item(NAME(m_voice[j].o3n2), j);
		save_item(NAME(m_voice[j].o2n1), j);
		save_item(NAME(m_voice[j].o2n2), j);
		save_item(NAME(m_voice[j].o1n1), j);
		save_item(NAME(m_voice[j].exbank), j);
		save_item(NAME(m_voice[j].filtcount), j);
	}

	/* success */
}

//-------------------------------------------------
//  device_clock_changed
//-------------------------------------------------

void es550x_device::device_clock_changed()
{
	m_master_clock = clock();
	m_sample_rate = m_master_clock / (16 * (m_active_voices + 1));
	m_stream->set_sample_rate(m_sample_rate);
	if (!m_sample_rate_changed_cb.isnull())
		m_sample_rate_changed_cb(m_sample_rate);
}

//-------------------------------------------------
//  device_reset - device-specific reset
//-------------------------------------------------

void es550x_device::device_reset()
{
}

//-------------------------------------------------
//  device_stop - device-specific stop
//-------------------------------------------------

void es550x_device::device_stop()
{
#if ES5506_MAKE_WAVS
	{
		int i;

		for (i = 0; i < MAX_ES5506; i++)
		{
			if (es5506[i].m_wavraw)
				wav_close(es5506[i].m_wavraw);
		}
	}
#endif
}

DEFINE_DEVICE_TYPE(ES5505, es5505_device, "es5505", "Ensoniq ES5505")

es5505_device::es5505_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: es550x_device(mconfig, ES5505, tag, owner, clock)
{
}

//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void es5505_device::device_start()
{
	int j;
	uint32_t accum_mask;
	int channels = 1;  /* 1 channel by default, for backward compatibility */

	/* only override the number of channels if the value is in the valid range 1 .. 4 */
	if (1 <= m_channels && m_channels <= 4)
		channels = m_channels;

	/* create the stream */
	m_stream = machine().sound().stream_alloc(*this, 0, 2 * channels, clock() / (16*32));

	/* initialize the regions */
	if (m_region0)
	{
		memory_region* region = machine().root_device().memregion(m_region0);
		m_region_base[0] = region ? reinterpret_cast<uint16_t *>(region->base()) : nullptr;
	}
	if (m_region1)
	{
		memory_region* region = machine().root_device().memregion(m_region1);
		m_region_base[1] = region ? reinterpret_cast<uint16_t *>(region->base()) : nullptr;
	}

	/* initialize the rest of the structure */
	m_master_clock = clock();
	m_irq_cb.resolve();
	m_read_port_cb.resolve();
	m_sample_rate_changed_cb.resolve();
	m_irqv = 0x80;
	m_channels = channels;

	/* compute the tables */
	compute_tables();

	/* init the voices */
	accum_mask = 0x7fffffff;
	for (j = 0; j < 32; j++)
	{
		m_voice[j].index = j;
		m_voice[j].control = CONTROL_STOPMASK;
		m_voice[j].lvol = 0xffff;
		m_voice[j].rvol = 0xffff;
		m_voice[j].exbank = 0;
		m_voice[j].accum_mask = accum_mask;
	}

	/* allocate memory */
	m_scratch = make_unique_clear<int32_t[]>(2 * MAX_SAMPLE_CHUNK);

	/* register save */
	save_item(NAME(m_sample_rate));
	save_item(NAME(m_write_latch));
	save_item(NAME(m_read_latch));

	save_item(NAME(m_current_page));
	save_item(NAME(m_active_voices));
	save_item(NAME(m_mode));
	save_item(NAME(m_wst));
	save_item(NAME(m_wend));
	save_item(NAME(m_lrend));
	save_item(NAME(m_irqv));

	save_pointer(NAME(m_scratch), 2 * MAX_SAMPLE_CHUNK);

	for (j = 0; j < 32; j++)
	{
		save_item(NAME(m_voice[j].control), j);
		save_item(NAME(m_voice[j].freqcount), j);
		save_item(NAME(m_voice[j].start), j);
		save_item(NAME(m_voice[j].lvol), j);
		save_item(NAME(m_voice[j].end), j);
		save_item(NAME(m_voice[j].lvramp), j);
		save_item(NAME(m_voice[j].accum), j);
		save_item(NAME(m_voice[j].rvol), j);
		save_item(NAME(m_voice[j].rvramp), j);
		save_item(NAME(m_voice[j].ecount), j);
		save_item(NAME(m_voice[j].k2), j);
		save_item(NAME(m_voice[j].k2ramp), j);
		save_item(NAME(m_voice[j].k1), j);
		save_item(NAME(m_voice[j].k1ramp), j);
		save_item(NAME(m_voice[j].o4n1), j);
		save_item(NAME(m_voice[j].o3n1), j);
		save_item(NAME(m_voice[j].o3n2), j);
		save_item(NAME(m_voice[j].o2n1), j);
		save_item(NAME(m_voice[j].o2n2), j);
		save_item(NAME(m_voice[j].o1n1), j);
		save_item(NAME(m_voice[j].exbank), j);
		save_item(NAME(m_voice[j].filtcount), j);
	}

	/* success */
}


/**********************************************************************************************

     update_irq_state -- update the IRQ state

***********************************************************************************************/


void es550x_device::update_irq_state()
{
	/* ES5505/6 irq line has been set high - inform the host */
	if (!m_irq_cb.isnull())
		m_irq_cb(1); /* IRQB set high */
}

void es550x_device::update_internal_irq_state()
{
	/*  Host (cpu) has just read the voice interrupt vector (voice IRQ ack).

	    Reset the voice vector to show the IRQB line is low (top bit set).
	    If we have any stacked interrupts (other voices waiting to be
	    processed - with their IRQ bit set) then they will be moved into
	    the vector next time the voice is processed.  In emulation
	    terms they get updated next time generate_samples() is called.
	*/

	m_irqv=0x80;

	if (!m_irq_cb.isnull())
		m_irq_cb(0); /* IRQB set low */
}

/**********************************************************************************************

     compute_tables -- compute static tables

***********************************************************************************************/

void es550x_device::compute_tables()
{
	int i;

	/* allocate ulaw lookup table */
	m_ulaw_lookup = make_unique_clear<int16_t[]>(1 << ULAW_MAXBITS);

	/* generate ulaw lookup table */
	for (i = 0; i < (1 << ULAW_MAXBITS); i++)
	{
		uint16_t rawval = (i << (16 - ULAW_MAXBITS)) | (1 << (15 - ULAW_MAXBITS));
		uint8_t exponent = rawval >> 13;
		uint32_t mantissa = (rawval << 3) & 0xffff;

		if (exponent == 0)
			m_ulaw_lookup[i] = (int16_t)mantissa >> 7;
		else
		{
			mantissa = (mantissa >> 1) | (~mantissa & 0x8000);
			m_ulaw_lookup[i] = (int16_t)mantissa >> (7 - exponent);
		}
	}

	/* allocate volume lookup table */
	m_volume_lookup = make_unique_clear<uint16_t[]>(4096);

	/* generate volume lookup table */
	for (i = 0; i < 4096; i++)
	{
		uint8_t exponent = i >> 8;
		uint32_t mantissa = (i & 0xff) | 0x100;

		m_volume_lookup[i] = (mantissa << 11) >> (20 - exponent);
	}
}



/**********************************************************************************************

     interpolate -- interpolate between two samples

***********************************************************************************************/

#define interpolate(sample1, sample2, accum)                                \
		(sample1 * (int32_t)(0x800 - (accum & 0x7ff)) +                       \
			sample2 * (int32_t)(accum & 0x7ff)) >> 11;



/**********************************************************************************************

     apply_filters -- apply the 4-pole digital filter to the sample

***********************************************************************************************/

#define apply_filters(voice, sample)                                                            \
do                                                                                              \
{                                                                                               \
	/* pole 1 is always low-pass using K1 */                                                    \
	sample = ((int32_t)(voice->k1 >> 2) * (sample - voice->o1n1) / 16384) + voice->o1n1;          \
	voice->o1n1 = sample;                                                                       \
																								\
	/* pole 2 is always low-pass using K1 */                                                    \
	sample = ((int32_t)(voice->k1 >> 2) * (sample - voice->o2n1) / 16384) + voice->o2n1;          \
	voice->o2n2 = voice->o2n1;                                                                  \
	voice->o2n1 = sample;                                                                       \
																								\
	/* remaining poles depend on the current filter setting */                                  \
	switch (voice->control & CONTROL_LPMASK)                                                    \
	{                                                                                           \
		case 0:                                                                                 \
			/* pole 3 is high-pass using K2 */                                                  \
			sample = sample - voice->o2n2 + ((int32_t)(voice->k2 >> 2) * voice->o3n1) / 32768 + voice->o3n1 / 2; \
			voice->o3n2 = voice->o3n1;                                                          \
			voice->o3n1 = sample;                                                               \
																								\
			/* pole 4 is high-pass using K2 */                                                  \
			sample = sample - voice->o3n2 + ((int32_t)(voice->k2 >> 2) * voice->o4n1) / 32768 + voice->o4n1 / 2; \
			voice->o4n1 = sample;                                                               \
			break;                                                                              \
																								\
		case CONTROL_LP3:                                                                       \
			/* pole 3 is low-pass using K1 */                                                   \
			sample = ((int32_t)(voice->k1 >> 2) * (sample - voice->o3n1) / 16384) + voice->o3n1;  \
			voice->o3n2 = voice->o3n1;                                                          \
			voice->o3n1 = sample;                                                               \
																								\
			/* pole 4 is high-pass using K2 */                                                  \
			sample = sample - voice->o3n2 + ((int32_t)(voice->k2 >> 2) * voice->o4n1) / 32768 + voice->o4n1 / 2; \
			voice->o4n1 = sample;                                                               \
			break;                                                                              \
																								\
		case CONTROL_LP4:                                                                       \
			/* pole 3 is low-pass using K2 */                                                   \
			sample = ((int32_t)(voice->k2 >> 2) * (sample - voice->o3n1) / 16384) + voice->o3n1;  \
			voice->o3n2 = voice->o3n1;                                                          \
			voice->o3n1 = sample;                                                               \
																								\
			/* pole 4 is low-pass using K2 */                                                   \
			sample = ((int32_t)(voice->k2 >> 2) * (sample - voice->o4n1) / 16384) + voice->o4n1;  \
			voice->o4n1 = sample;                                                               \
			break;                                                                              \
																								\
		case CONTROL_LP4 | CONTROL_LP3:                                                         \
			/* pole 3 is low-pass using K1 */                                                   \
			sample = ((int32_t)(voice->k1 >> 2) * (sample - voice->o3n1) / 16384) + voice->o3n1;  \
			voice->o3n2 = voice->o3n1;                                                          \
			voice->o3n1 = sample;                                                               \
																								\
			/* pole 4 is low-pass using K2 */                                                   \
			sample = ((int32_t)(voice->k2 >> 2) * (sample - voice->o4n1) / 16384) + voice->o4n1;  \
			voice->o4n1 = sample;                                                               \
			break;                                                                              \
	}                                                                                           \
} while (0)



/**********************************************************************************************

     update_envelopes -- update the envelopes

***********************************************************************************************/

#define update_envelopes(voice, samples)                                            \
do                                                                                  \
{                                                                                   \
	int count = (samples > 1 && samples > voice->ecount) ? voice->ecount : samples; \
																					\
	/* decrement the envelope counter */                                            \
	voice->ecount -= count;                                                         \
																					\
	/* ramp left volume */                                                          \
	if (voice->lvramp)                                                              \
	{                                                                               \
		voice->lvol += (int8_t)voice->lvramp * count;                                 \
		if ((int32_t)voice->lvol < 0) voice->lvol = 0;                                \
		else if (voice->lvol > 0xffff) voice->lvol = 0xffff;                        \
	}                                                                               \
																					\
	/* ramp right volume */                                                         \
	if (voice->rvramp)                                                              \
	{                                                                               \
		voice->rvol += (int8_t)voice->rvramp * count;                                 \
		if ((int32_t)voice->rvol < 0) voice->rvol = 0;                                \
		else if (voice->rvol > 0xffff) voice->rvol = 0xffff;                        \
	}                                                                               \
																					\
	/* ramp k1 filter constant */                                                   \
	if (voice->k1ramp && ((int32_t)voice->k1ramp >= 0 || !(voice->filtcount & 7)))    \
	{                                                                               \
		voice->k1 += (int8_t)voice->k1ramp * count;                                   \
		if ((int32_t)voice->k1 < 0) voice->k1 = 0;                                    \
		else if (voice->k1 > 0xffff) voice->k1 = 0xffff;                            \
	}                                                                               \
																					\
	/* ramp k2 filter constant */                                                   \
	if (voice->k2ramp && ((int32_t)voice->k2ramp >= 0 || !(voice->filtcount & 7)))    \
	{                                                                               \
		voice->k2 += (int8_t)voice->k2ramp * count;                                   \
		if ((int32_t)voice->k2 < 0) voice->k2 = 0;                                    \
		else if (voice->k2 > 0xffff) voice->k2 = 0xffff;                            \
	}                                                                               \
																					\
	/* update the filter constant counter */                                        \
	voice->filtcount += count;                                                      \
																					\
} while (0)



/**********************************************************************************************

     check_for_end_forward
     check_for_end_reverse -- check for loop end and loop appropriately

***********************************************************************************************/

#define check_for_end_forward(voice, accum)                                         \
do                                                                                  \
{                                                                                   \
	/* are we past the end? */                                                      \
	if (accum > voice->end && !(voice->control & CONTROL_LEI))                      \
	{                                                                               \
		/* generate interrupt if required */                                        \
		if (voice->control&CONTROL_IRQE)                                            \
			voice->control |= CONTROL_IRQ;                                          \
																					\
		/* handle the different types of looping */                                 \
		switch (voice->control & CONTROL_LOOPMASK)                                  \
		{                                                                           \
			/* non-looping */                                                       \
			case 0:                                                                 \
				voice->control |= CONTROL_STOP0;                                    \
				goto alldone;                                                       \
																					\
			/* uni-directional looping */                                           \
			case CONTROL_LPE:                                                       \
				accum = (voice->start + (accum - voice->end)) & voice->accum_mask;  \
				break;                                                              \
																					\
			/* trans-wave looping */                                                \
			case CONTROL_BLE:                                                       \
				accum = (voice->start + (accum - voice->end)) & voice->accum_mask;  \
				voice->control = (voice->control & ~CONTROL_LOOPMASK) | CONTROL_LEI;\
				break;                                                              \
																					\
			/* bi-directional looping */                                            \
			case CONTROL_LPE | CONTROL_BLE:                                         \
				accum = (voice->end - (accum - voice->end)) & voice->accum_mask;    \
				voice->control ^= CONTROL_DIR;                                      \
				goto reverse;                                                       \
		}                                                                           \
	}                                                                               \
} while (0)


#define check_for_end_reverse(voice, accum)                                         \
do                                                                                  \
{                                                                                   \
	/* are we past the end? */                                                      \
	if (accum < voice->start && !(voice->control & CONTROL_LEI))                    \
	{                                                                               \
		/* generate interrupt if required */                                        \
		if (voice->control&CONTROL_IRQE)                                            \
			voice->control |= CONTROL_IRQ;                                          \
																					\
		/* handle the different types of looping */                                 \
		switch (voice->control & CONTROL_LOOPMASK)                                  \
		{                                                                           \
			/* non-looping */                                                       \
			case 0:                                                                 \
				voice->control |= CONTROL_STOP0;                                    \
				goto alldone;                                                       \
																					\
			/* uni-directional looping */                                           \
			case CONTROL_LPE:                                                       \
				accum = (voice->end - (voice->start - accum)) & voice->accum_mask;  \
				break;                                                              \
																					\
			/* trans-wave looping */                                                \
			case CONTROL_BLE:                                                       \
				accum = (voice->end - (voice->start - accum)) & voice->accum_mask;  \
				voice->control = (voice->control & ~CONTROL_LOOPMASK) | CONTROL_LEI;\
				break;                                                              \
																					\
			/* bi-directional looping */                                            \
			case CONTROL_LPE | CONTROL_BLE:                                         \
				accum = (voice->start + (voice->start - accum)) & voice->accum_mask;\
				voice->control ^= CONTROL_DIR;                                      \
				goto reverse;                                                       \
		}                                                                           \
	}                                                                               \
} while (0)



/**********************************************************************************************

     generate_dummy -- generate nothing, just apply envelopes

***********************************************************************************************/

void es550x_device::generate_dummy(es550x_voice *voice, uint16_t *base, int32_t *lbuffer, int32_t *rbuffer, int samples)
{
	uint32_t freqcount = voice->freqcount;
	uint32_t accum = voice->accum & voice->accum_mask;

	/* outer loop, in case we switch directions */
	while (samples > 0 && !(voice->control & CONTROL_STOPMASK))
	{
reverse:
		/* two cases: first case is forward direction */
		if (!(voice->control & CONTROL_DIR))
		{
			/* loop while we still have samples to generate */
			while (samples--)
			{
				/* fetch two samples */
				accum = (accum + freqcount) & voice->accum_mask;

				/* update filters/volumes */
				if (voice->ecount != 0)
					update_envelopes(voice, 1);

				/* check for loop end */
				check_for_end_forward(voice, accum);
			}
		}

		/* two cases: second case is backward direction */
		else
		{
			/* loop while we still have samples to generate */
			while (samples--)
			{
				/* fetch two samples */
				accum = (accum - freqcount) & voice->accum_mask;

				/* update filters/volumes */
				if (voice->ecount != 0)
					update_envelopes(voice, 1);

				/* check for loop end */
				check_for_end_reverse(voice, accum);
			}
		}
	}

	/* if we stopped, process any additional envelope */
alldone:
	voice->accum = accum;
	if (samples > 0)
		update_envelopes(voice, samples);
}



/**********************************************************************************************

     generate_ulaw -- general u-law decoding routine

***********************************************************************************************/

void es550x_device::generate_ulaw(es550x_voice *voice, uint16_t *base, int32_t *lbuffer, int32_t *rbuffer, int samples)
{
	uint32_t freqcount = voice->freqcount;
	uint32_t accum = voice->accum & voice->accum_mask;
	int32_t lvol = m_volume_lookup[voice->lvol >> 4];
	int32_t rvol = m_volume_lookup[voice->rvol >> 4];

	/* pre-add the bank offset */
	base += voice->exbank;

	/* outer loop, in case we switch directions */
	while (samples > 0 && !(voice->control & CONTROL_STOPMASK))
	{
reverse:
		/* two cases: first case is forward direction */
		if (!(voice->control & CONTROL_DIR))
		{
			/* loop while we still have samples to generate */
			while (samples--)
			{
				/* fetch two samples */
				int32_t val1 = base[accum >> 11];
				int32_t val2 = base[((accum + (1 << 11)) & voice->accum_mask) >> 11];

				/* decompress u-law */
				val1 = m_ulaw_lookup[val1 >> (16 - ULAW_MAXBITS)];
				val2 = m_ulaw_lookup[val2 >> (16 - ULAW_MAXBITS)];

				/* interpolate */
				val1 = interpolate(val1, val2, accum);
				accum = (accum + freqcount) & voice->accum_mask;

				/* apply filters */
				apply_filters(voice, val1);

				/* update filters/volumes */
				if (voice->ecount != 0)
				{
					update_envelopes(voice, 1);
					lvol = m_volume_lookup[voice->lvol >> 4];
					rvol = m_volume_lookup[voice->rvol >> 4];
				}

				/* apply volumes and add */
				*lbuffer++ += (val1 * lvol) >> 11;
				*rbuffer++ += (val1 * rvol) >> 11;

				/* check for loop end */
				check_for_end_forward(voice, accum);
			}
		}

		/* two cases: second case is backward direction */
		else
		{
			/* loop while we still have samples to generate */
			while (samples--)
			{
				/* fetch two samples */
				int32_t val1 = base[accum >> 11];
				int32_t val2 = base[((accum + (1 << 11)) & voice->accum_mask) >> 11];

				/* decompress u-law */
				val1 = m_ulaw_lookup[val1 >> (16 - ULAW_MAXBITS)];
				val2 = m_ulaw_lookup[val2 >> (16 - ULAW_MAXBITS)];

				/* interpolate */
				val1 = interpolate(val1, val2, accum);
				accum = (accum - freqcount) & voice->accum_mask;

				/* apply filters */
				apply_filters(voice, val1);

				/* update filters/volumes */
				if (voice->ecount != 0)
				{
					update_envelopes(voice, 1);
					lvol = m_volume_lookup[voice->lvol >> 4];
					rvol = m_volume_lookup[voice->rvol >> 4];
				}

				/* apply volumes and add */
				*lbuffer++ += (val1 * lvol) >> 11;
				*rbuffer++ += (val1 * rvol) >> 11;

				/* check for loop end */
				check_for_end_reverse(voice, accum);
			}
		}
	}

	/* if we stopped, process any additional envelope */
alldone:
	voice->accum = accum;
	if (samples > 0)
		update_envelopes(voice, samples);
}



/**********************************************************************************************

     generate_pcm -- general PCM decoding routine

***********************************************************************************************/

void es550x_device::generate_pcm(es550x_voice *voice, uint16_t *base, int32_t *lbuffer, int32_t *rbuffer, int samples)
{
	uint32_t freqcount = voice->freqcount;
	uint32_t accum = voice->accum & voice->accum_mask;
	int32_t lvol = m_volume_lookup[voice->lvol >> 4];
	int32_t rvol = m_volume_lookup[voice->rvol >> 4];

	/* pre-add the bank offset */
	base += voice->exbank;

	/* outer loop, in case we switch directions */
	while (samples > 0 && !(voice->control & CONTROL_STOPMASK))
	{
reverse:
		/* two cases: first case is forward direction */
		if (!(voice->control & CONTROL_DIR))
		{
			/* loop while we still have samples to generate */
			while (samples--)
			{
				/* fetch two samples */
				int32_t val1 = (int16_t)base[accum >> 11];
				int32_t val2 = (int16_t)base[((accum + (1 << 11)) & voice->accum_mask) >> 11];

				/* interpolate */
				val1 = interpolate(val1, val2, accum);
				accum = (accum + freqcount) & voice->accum_mask;

				/* apply filters */
				apply_filters(voice, val1);

				/* update filters/volumes */
				if (voice->ecount != 0)
				{
					update_envelopes(voice, 1);
					lvol = m_volume_lookup[voice->lvol >> 4];
					rvol = m_volume_lookup[voice->rvol >> 4];
				}

				/* apply volumes and add */
				*lbuffer++ += (val1 * lvol) >> 11;
				*rbuffer++ += (val1 * rvol) >> 11;

				/* check for loop end */
				check_for_end_forward(voice, accum);
			}
		}

		/* two cases: second case is backward direction */
		else
		{
			/* loop while we still have samples to generate */
			while (samples--)
			{
				/* fetch two samples */
				int32_t val1 = (int16_t)base[accum >> 11];
				int32_t val2 = (int16_t)base[((accum + (1 << 11)) & voice->accum_mask) >> 11];

				/* interpolate */
				val1 = interpolate(val1, val2, accum);
				accum = (accum - freqcount) & voice->accum_mask;

				/* apply filters */
				apply_filters(voice, val1);

				/* update filters/volumes */
				if (voice->ecount != 0)
				{
					update_envelopes(voice, 1);
					lvol = m_volume_lookup[voice->lvol >> 4];
					rvol = m_volume_lookup[voice->rvol >> 4];
				}

				/* apply volumes and add */
				*lbuffer++ += (val1 * lvol) >> 11;
				*rbuffer++ += (val1 * rvol) >> 11;

				/* check for loop end */
				check_for_end_reverse(voice, accum);
			}
		}
	}

	/* if we stopped, process any additional envelope */
alldone:
	voice->accum = accum;
	if (samples > 0)
		update_envelopes(voice, samples);
}



/**********************************************************************************************

     generate_samples -- tell each voice to generate samples

***********************************************************************************************/

void es5506_device::generate_samples(int32_t **outputs, int offset, int samples)
{
	int v;

	/* skip if nothing to do */
	if (!samples)
		return;

	/* clear out the accumulators */
	for (int i = 0; i < m_channels << 1; i++)
	{
		memset(outputs[i] + offset, 0, sizeof(int32_t) * samples);
	}

	/* loop over voices */
	for (v = 0; v <= m_active_voices; v++)
	{
		es550x_voice *voice = &m_voice[v];
		uint16_t *base = m_region_base[voice->control >> 14];

		/* special case: if end == start, stop the voice */
		if (voice->start == voice->end)
			voice->control |= CONTROL_STOP0;

		int voice_channel = (voice->control & CONTROL_CAMASK) >> 10;
		int channel = voice_channel % m_channels;
		int l = channel << 1;
		int r = l + 1;
		int32_t *left = outputs[l] + offset;
		int32_t *right = outputs[r] + offset;

		/* generate from the appropriate source */
		if (!base)
		{
			logerror("es5506: nullptr region base %d\n",voice->control >> 14);
			generate_dummy(voice, base, left, right, samples);
		}
		else if (voice->control & 0x2000)
			generate_ulaw(voice, base, left, right, samples);
		else
			generate_pcm(voice, base, left, right, samples);

		/* does this voice have it's IRQ bit raised? */
		if (voice->control&CONTROL_IRQ)
		{
			logerror("es5506: IRQ raised on voice %d!!\n",v);

			/* only update voice vector if existing IRQ is acked by host */
			if (m_irqv&0x80)
			{
				/* latch voice number into vector, and set high bit low */
				m_irqv=v&0x7f;

				/* take down IRQ bit on voice */
				voice->control&=~CONTROL_IRQ;

				/* inform host of irq */
				update_irq_state();
			}
		}
	}
}

void es5505_device::generate_samples(int32_t **outputs, int offset, int samples)
{
	int v;

	/* skip if nothing to do */
	if (!samples)
		return;

	/* clear out the accumulators */
	for (int i = 0; i < m_channels << 1; i++)
	{
		memset(outputs[i] + offset, 0, sizeof(int32_t) * samples);
	}

	/* loop over voices */
	for (v = 0; v <= m_active_voices; v++)
	{
		es550x_voice *voice = &m_voice[v];
		uint16_t *base = m_region_base[voice->control >> 14];

// This special case does not appear to match the behaviour observed in the es5505 in
// actual Ensoniq synthesizers: those, it turns out, do set loop start and end to the
// same value, and expect the voice to keep running. Examples can be found among the
// transwaves on the VFX / SD-1 series of synthesizers.
#if 0
		/* special case: if end == start, stop the voice */
		if (voice->start == voice->end)
			voice->control |= CONTROL_STOP0;
#endif

		int voice_channel = (voice->control & CONTROL_CAMASK) >> 10;
		int channel = voice_channel % m_channels;
		int l = channel << 1;
		int r = l + 1;
		int32_t *left = outputs[l] + offset;
		int32_t *right = outputs[r] + offset;

		/* generate from the appropriate source */
		if (!base)
		{
			logerror("es5506: nullptr region base %d\n",voice->control >> 14);
			generate_dummy(voice, base, left, right, samples);
		}
		else if (voice->control & 0x2000)
			generate_ulaw(voice, base, left, right, samples);
		else
			generate_pcm(voice, base, left, right, samples);

		/* does this voice have it's IRQ bit raised? */
		if (voice->control&CONTROL_IRQ)
		{
			logerror("es5506: IRQ raised on voice %d!!\n",v);

			/* only update voice vector if existing IRQ is acked by host */
			if (m_irqv&0x80)
			{
				/* latch voice number into vector, and set high bit low */
				m_irqv=v&0x7f;

				/* take down IRQ bit on voice */
				voice->control&=~CONTROL_IRQ;

				/* inform host of irq */
				update_irq_state();
			}
		}
	}
}



/**********************************************************************************************

     reg_write -- handle a write to the selected ES5506 register

***********************************************************************************************/

inline void es5506_device::reg_write_low(es550x_voice *voice, offs_t offset, uint32_t data)
{
	switch (offset)
	{
		case 0x00/8:    /* CR */
			voice->control = data & 0xffff;
			LOG("voice %d, control=%04x\n", m_current_page & 0x1f, voice->control);
			break;

		case 0x08/8:    /* FC */
			voice->freqcount = data & 0x1ffff;
			LOG("voice %d, freq count=%08x\n", m_current_page & 0x1f, voice->freqcount);
			break;

		case 0x10/8:    /* LVOL */
			voice->lvol = data & 0xffff;
			LOG("voice %d, left vol=%04x\n", m_current_page & 0x1f, voice->lvol);
			break;

		case 0x18/8:    /* LVRAMP */
			voice->lvramp = (data & 0xff00) >> 8;
			LOG("voice %d, left vol ramp=%04x\n", m_current_page & 0x1f, voice->lvramp);
			break;

		case 0x20/8:    /* RVOL */
			voice->rvol = data & 0xffff;
			LOG("voice %d, right vol=%04x\n", m_current_page & 0x1f, voice->rvol);
			break;

		case 0x28/8:    /* RVRAMP */
			voice->rvramp = (data & 0xff00) >> 8;
			LOG("voice %d, right vol ramp=%04x\n", m_current_page & 0x1f, voice->rvramp);
			break;

		case 0x30/8:    /* ECOUNT */
			voice->ecount = data & 0x1ff;
			voice->filtcount = 0;
			LOG("voice %d, envelope count=%04x\n", m_current_page & 0x1f, voice->ecount);
			break;

		case 0x38/8:    /* K2 */
			voice->k2 = data & 0xffff;
			LOG("voice %d, K2=%04x\n", m_current_page & 0x1f, voice->k2);
			break;

		case 0x40/8:    /* K2RAMP */
			voice->k2ramp = ((data & 0xff00) >> 8) | ((data & 0x0001) << 31);
			LOG("voice %d, K2 ramp=%04x\n", m_current_page & 0x1f, voice->k2ramp);
			break;

		case 0x48/8:    /* K1 */
			voice->k1 = data & 0xffff;
			LOG("voice %d, K1=%04x\n", m_current_page & 0x1f, voice->k1);
			break;

		case 0x50/8:    /* K1RAMP */
			voice->k1ramp = ((data & 0xff00) >> 8) | ((data & 0x0001) << 31);
			LOG("voice %d, K1 ramp=%04x\n", m_current_page & 0x1f, voice->k1ramp);
			break;

		case 0x58/8:    /* ACTV */
		{
			m_active_voices = data & 0x1f;
			m_sample_rate = m_master_clock / (16 * (m_active_voices + 1));
			m_stream->set_sample_rate(m_sample_rate);
			if (!m_sample_rate_changed_cb.isnull())
				m_sample_rate_changed_cb(m_sample_rate);

			LOG("active voices=%d, sample_rate=%d\n", m_active_voices, m_sample_rate);
			break;
		}

		case 0x60/8:    /* MODE */
			m_mode = data & 0x1f;
			break;

		case 0x68/8:    /* PAR - read only */
		case 0x70/8:    /* IRQV - read only */
			break;

		case 0x78/8:    /* PAGE */
			m_current_page = data & 0x7f;
			break;
	}
}

inline void es5506_device::reg_write_high(es550x_voice *voice, offs_t offset, uint32_t data)
{
	switch (offset)
	{
		case 0x00/8:    /* CR */
			voice->control = data & 0xffff;
			LOG("voice %d, control=%04x\n", m_current_page & 0x1f, voice->control);
			break;

		case 0x08/8:    /* START */
			voice->start = data & 0xfffff800;
			LOG("voice %d, loop start=%08x\n", m_current_page & 0x1f, voice->start);
			break;

		case 0x10/8:    /* END */
			voice->end = data & 0xffffff80;
			LOG("voice %d, loop end=%08x\n", m_current_page & 0x1f, voice->end);
			break;

		case 0x18/8:    /* ACCUM */
			voice->accum = data;
			LOG("voice %d, accum=%08x\n", m_current_page & 0x1f, voice->accum);
			break;

		case 0x20/8:    /* O4(n-1) */
			voice->o4n1 = (int32_t)(data << 14) >> 14;
			LOG("voice %d, O4(n-1)=%05x\n", m_current_page & 0x1f, voice->o4n1 & 0x3ffff);
			break;

		case 0x28/8:    /* O3(n-1) */
			voice->o3n1 = (int32_t)(data << 14) >> 14;
			LOG("voice %d, O3(n-1)=%05x\n", m_current_page & 0x1f, voice->o3n1 & 0x3ffff);
			break;

		case 0x30/8:    /* O3(n-2) */
			voice->o3n2 = (int32_t)(data << 14) >> 14;
			LOG("voice %d, O3(n-2)=%05x\n", m_current_page & 0x1f, voice->o3n2 & 0x3ffff);
			break;

		case 0x38/8:    /* O2(n-1) */
			voice->o2n1 = (int32_t)(data << 14) >> 14;
			LOG("voice %d, O2(n-1)=%05x\n", m_current_page & 0x1f, voice->o2n1 & 0x3ffff);
			break;

		case 0x40/8:    /* O2(n-2) */
			voice->o2n2 = (int32_t)(data << 14) >> 14;
			LOG("voice %d, O2(n-2)=%05x\n", m_current_page & 0x1f, voice->o2n2 & 0x3ffff);
			break;

		case 0x48/8:    /* O1(n-1) */
			voice->o1n1 = (int32_t)(data << 14) >> 14;
			LOG("voice %d, O1(n-1)=%05x\n", m_current_page & 0x1f, voice->o1n1 & 0x3ffff);
			break;

		case 0x50/8:    /* W_ST */
			m_wst = data & 0x7f;
			break;

		case 0x58/8:    /* W_END */
			m_wend = data & 0x7f;
			break;

		case 0x60/8:    /* LR_END */
			m_lrend = data & 0x7f;
			break;

		case 0x68/8:    /* PAR - read only */
		case 0x70/8:    /* IRQV - read only */
			break;

		case 0x78/8:    /* PAGE */
			m_current_page = data & 0x7f;
			break;
	}
}

inline void es5506_device::reg_write_test(es550x_voice *voice, offs_t offset, uint32_t data)
{
	switch (offset)
	{
		case 0x00/8:    /* CHANNEL 0 LEFT */
			LOG("Channel 0 left test write %08x\n", data);
			break;

		case 0x08/8:    /* CHANNEL 0 RIGHT */
			LOG("Channel 0 right test write %08x\n", data);
			break;

		case 0x10/8:    /* CHANNEL 1 LEFT */
			LOG("Channel 1 left test write %08x\n", data);
			break;

		case 0x18/8:    /* CHANNEL 1 RIGHT */
			LOG("Channel 1 right test write %08x\n", data);
			break;

		case 0x20/8:    /* CHANNEL 2 LEFT */
			LOG("Channel 2 left test write %08x\n", data);
			break;

		case 0x28/8:    /* CHANNEL 2 RIGHT */
			LOG("Channel 2 right test write %08x\n", data);
			break;

		case 0x30/8:    /* CHANNEL 3 LEFT */
			LOG("Channel 3 left test write %08x\n", data);
			break;

		case 0x38/8:    /* CHANNEL 3 RIGHT */
			LOG("Channel 3 right test write %08x\n", data);
			break;

		case 0x40/8:    /* CHANNEL 4 LEFT */
			LOG("Channel 4 left test write %08x\n", data);
			break;

		case 0x48/8:    /* CHANNEL 4 RIGHT */
			LOG("Channel 4 right test write %08x\n", data);
			break;

		case 0x50/8:    /* CHANNEL 5 LEFT */
			LOG("Channel 5 left test write %08x\n", data);
			break;

		case 0x58/8:    /* CHANNEL 6 RIGHT */
			LOG("Channel 5 right test write %08x\n", data);
			break;

		case 0x60/8:    /* EMPTY */
			LOG("Test write EMPTY %08x\n", data);
			break;

		case 0x68/8:    /* PAR - read only */
		case 0x70/8:    /* IRQV - read only */
			break;

		case 0x78/8:    /* PAGE */
			m_current_page = data & 0x7f;
			break;
	}
}

WRITE8_MEMBER( es5506_device::write )
{
	es550x_voice *voice = &m_voice[m_current_page & 0x1f];
	int shift = 8 * (offset & 3);

	/* accumulate the data */
	m_write_latch = (m_write_latch & ~(0xff000000 >> shift)) | (data << (24 - shift));

	/* wait for a write to complete */
	if (shift != 24)
		return;

	/* force an update */
	m_stream->update();

	/* switch off the page and register */
	if (m_current_page < 0x20)
		reg_write_low(voice, offset / 4, m_write_latch);
	else if (m_current_page < 0x40)
		reg_write_high(voice, offset / 4, m_write_latch);
	else
		reg_write_test(voice, offset / 4, m_write_latch);

	/* clear the write latch when done */
	m_write_latch = 0;
}



/**********************************************************************************************

     reg_read -- read from the specified ES5506 register

***********************************************************************************************/

inline uint32_t es5506_device::reg_read_low(es550x_voice *voice, offs_t offset)
{
	uint32_t result = 0;

	switch (offset)
	{
		case 0x00/8:    /* CR */
			result = voice->control;
			break;

		case 0x08/8:    /* FC */
			result = voice->freqcount;
			break;

		case 0x10/8:    /* LVOL */
			result = voice->lvol;
			break;

		case 0x18/8:    /* LVRAMP */
			result = voice->lvramp << 8;
			break;

		case 0x20/8:    /* RVOL */
			result = voice->rvol;
			break;

		case 0x28/8:    /* RVRAMP */
			result = voice->rvramp << 8;
			break;

		case 0x30/8:    /* ECOUNT */
			result = voice->ecount;
			break;

		case 0x38/8:    /* K2 */
			result = voice->k2;
			break;

		case 0x40/8:    /* K2RAMP */
			result = (voice->k2ramp << 8) | (voice->k2ramp >> 31);
			break;

		case 0x48/8:    /* K1 */
			result = voice->k1;
			break;

		case 0x50/8:    /* K1RAMP */
			result = (voice->k1ramp << 8) | (voice->k1ramp >> 31);
			break;

		case 0x58/8:    /* ACTV */
			result = m_active_voices;
			break;

		case 0x60/8:    /* MODE */
			result = m_mode;
			break;

		case 0x68/8:    /* PAR */
			if (!m_read_port_cb.isnull())
				result = m_read_port_cb(0);
			break;

		case 0x70/8:    /* IRQV */
			result = m_irqv;
			update_internal_irq_state();
			break;

		case 0x78/8:    /* PAGE */
			result = m_current_page;
			break;
	}
	return result;
}


inline uint32_t es5506_device::reg_read_high(es550x_voice *voice, offs_t offset)
{
	uint32_t result = 0;

	switch (offset)
	{
		case 0x00/8:    /* CR */
			result = voice->control;
			break;

		case 0x08/8:    /* START */
			result = voice->start;
			break;

		case 0x10/8:    /* END */
			result = voice->end;
			break;

		case 0x18/8:    /* ACCUM */
			result = voice->accum;
			break;

		case 0x20/8:    /* O4(n-1) */
			result = voice->o4n1 & 0x3ffff;
			break;

		case 0x28/8:    /* O3(n-1) */
			result = voice->o3n1 & 0x3ffff;
			break;

		case 0x30/8:    /* O3(n-2) */
			result = voice->o3n2 & 0x3ffff;
			break;

		case 0x38/8:    /* O2(n-1) */
			result = voice->o2n1 & 0x3ffff;
			break;

		case 0x40/8:    /* O2(n-2) */
			result = voice->o2n2 & 0x3ffff;
			break;

		case 0x48/8:    /* O1(n-1) */
			result = voice->o1n1 & 0x3ffff;
			break;

		case 0x50/8:    /* W_ST */
			result = m_wst;
			break;

		case 0x58/8:    /* W_END */
			result = m_wend;
			break;

		case 0x60/8:    /* LR_END */
			result = m_lrend;
			break;

		case 0x68/8:    /* PAR */
			if (!m_read_port_cb.isnull())
				result = m_read_port_cb(0);
			break;

		case 0x70/8:    /* IRQV */
			result = m_irqv;
			update_internal_irq_state();
			break;

		case 0x78/8:    /* PAGE */
			result = m_current_page;
			break;
	}
	return result;
}
inline uint32_t es5506_device::reg_read_test(es550x_voice *voice, offs_t offset)
{
	uint32_t result = 0;

	switch (offset)
	{
		case 0x68/8:    /* PAR */
			if (!m_read_port_cb.isnull())
				result = m_read_port_cb(0);
			break;

		case 0x70/8:    /* IRQV */
			result = m_irqv;
			break;

		case 0x78/8:    /* PAGE */
			result = m_current_page;
			break;
	}
	return result;
}

READ8_MEMBER( es5506_device::read )
{
	es550x_voice *voice = &m_voice[m_current_page & 0x1f];
	int shift = 8 * (offset & 3);

	/* only read on offset 0 */
	if (shift != 0)
		return m_read_latch >> (24 - shift);

	LOG("read from %02x/%02x -> ", m_current_page, offset / 4 * 8);

	/* force an update */
	m_stream->update();

	/* switch off the page and register */
	if (m_current_page < 0x20)
		m_read_latch = reg_read_low(voice, offset / 4);
	else if (m_current_page < 0x40)
		m_read_latch = reg_read_high(voice, offset / 4);
	else
		m_read_latch = reg_read_test(voice, offset / 4);

	LOG("%08x\n", m_read_latch);

	/* return the high byte */
	return m_read_latch >> 24;
}



void es5506_device::voice_bank_w(int voice, int bank)
{
	m_voice[voice].exbank=bank;
}


/**********************************************************************************************

     reg_write -- handle a write to the selected ES5505 register

***********************************************************************************************/

inline void es5505_device::reg_write_low(es550x_voice *voice, offs_t offset, uint16_t data, uint16_t mem_mask)
{
	switch (offset)
	{
		case 0x00:  /* CR */
			if (ACCESSING_BITS_0_7)
			{
#if RAINE_CHECK
				voice->control &= ~(CONTROL_STOPMASK | CONTROL_LOOPMASK | CONTROL_DIR);
#else
				voice->control &= ~(CONTROL_STOPMASK | CONTROL_BS0 | CONTROL_LOOPMASK | CONTROL_IRQE | CONTROL_DIR | CONTROL_IRQ);
#endif
				voice->control |= (data & (CONTROL_STOPMASK | CONTROL_LOOPMASK | CONTROL_IRQE | CONTROL_DIR | CONTROL_IRQ)) |
									((data << 12) & CONTROL_BS0);
			}
			if (ACCESSING_BITS_8_15)
			{
				voice->control &= ~(CONTROL_CA0 | CONTROL_CA1 | CONTROL_LPMASK);
				voice->control |= ((data >> 2) & CONTROL_LPMASK) |
									((data << 2) & (CONTROL_CA0 | CONTROL_CA1));
			}

			LOG("%s:voice %d, control=%04x (raw=%04x & %04x)\n", machine().describe_context(), m_current_page & 0x1f, voice->control, data, mem_mask ^ 0xffff);
			break;

		case 0x01:  /* FC */
			if (ACCESSING_BITS_0_7)
				voice->freqcount = (voice->freqcount & ~0x001fe) | ((data & 0x00ff) << 1);
			if (ACCESSING_BITS_8_15)
				voice->freqcount = (voice->freqcount & ~0x1fe00) | ((data & 0xff00) << 1);
			LOG("%s:voice %d, freq count=%08x\n", machine().describe_context(), m_current_page & 0x1f, voice->freqcount);
			break;

		case 0x02:  /* STRT (hi) */
			if (ACCESSING_BITS_0_7)
				voice->start = (voice->start & ~0x03fc0000) | ((data & 0x00ff) << 18);
			if (ACCESSING_BITS_8_15)
				voice->start = (voice->start & ~0x7c000000) | ((data & 0x1f00) << 18);
			LOG("%s:voice %d, loop start=%08x\n", machine().describe_context(), m_current_page & 0x1f, voice->start);
			break;

		case 0x03:  /* STRT (lo) */
			if (ACCESSING_BITS_0_7)
				voice->start = (voice->start & ~0x00000380) | ((data & 0x00e0) << 2);
			if (ACCESSING_BITS_8_15)
				voice->start = (voice->start & ~0x0003fc00) | ((data & 0xff00) << 2);
			LOG("%s:voice %d, loop start=%08x\n", machine().describe_context(), m_current_page & 0x1f, voice->start);
			break;

		case 0x04:  /* END (hi) */
			if (ACCESSING_BITS_0_7)
				voice->end = (voice->end & ~0x03fc0000) | ((data & 0x00ff) << 18);
			if (ACCESSING_BITS_8_15)
				voice->end = (voice->end & ~0x7c000000) | ((data & 0x1f00) << 18);
#if RAINE_CHECK
			voice->control |= CONTROL_STOP0;
#endif
			LOG("%s:voice %d, loop end=%08x\n", machine().describe_context(), m_current_page & 0x1f, voice->end);
			break;

		case 0x05:  /* END (lo) */
			if (ACCESSING_BITS_0_7)
				voice->end = (voice->end & ~0x00000380) | ((data & 0x00e0) << 2);
			if (ACCESSING_BITS_8_15)
				voice->end = (voice->end & ~0x0003fc00) | ((data & 0xff00) << 2);
#if RAINE_CHECK
			voice->control |= CONTROL_STOP0;
#endif
			LOG("%s:voice %d, loop end=%08x\n", machine().describe_context(), m_current_page & 0x1f, voice->end);
			break;

		case 0x06:  /* K2 */
			if (ACCESSING_BITS_0_7)
				voice->k2 = (voice->k2 & ~0x00f0) | (data & 0x00f0);
			if (ACCESSING_BITS_8_15)
				voice->k2 = (voice->k2 & ~0xff00) | (data & 0xff00);
			LOG("%s:voice %d, K2=%04x\n", machine().describe_context(), m_current_page & 0x1f, voice->k2);
			break;

		case 0x07:  /* K1 */
			if (ACCESSING_BITS_0_7)
				voice->k1 = (voice->k1 & ~0x00f0) | (data & 0x00f0);
			if (ACCESSING_BITS_8_15)
				voice->k1 = (voice->k1 & ~0xff00) | (data & 0xff00);
			LOG("%s:voice %d, K1=%04x\n", machine().describe_context(), m_current_page & 0x1f, voice->k1);
			break;

		case 0x08:  /* LVOL */
			if (ACCESSING_BITS_8_15)
				voice->lvol = (voice->lvol & ~0xff00) | (data & 0xff00);
			LOG("%s:voice %d, left vol=%04x\n", machine().describe_context(), m_current_page & 0x1f, voice->lvol);
			break;

		case 0x09:  /* RVOL */
			if (ACCESSING_BITS_8_15)
				voice->rvol = (voice->rvol & ~0xff00) | (data & 0xff00);
			LOG("%s:voice %d, right vol=%04x\n", machine().describe_context(), m_current_page & 0x1f, voice->rvol);
			break;

		case 0x0a:  /* ACC (hi) */
			if (ACCESSING_BITS_0_7)
				voice->accum = (voice->accum & ~0x03fc0000) | ((data & 0x00ff) << 18);
			if (ACCESSING_BITS_8_15)
				voice->accum = (voice->accum & ~0x7c000000) | ((data & 0x1f00) << 18);
			LOG("%s:voice %d, accum=%08x\n", machine().describe_context(), m_current_page & 0x1f, voice->accum);
			break;

		case 0x0b:  /* ACC (lo) */
			if (ACCESSING_BITS_0_7)
				voice->accum = (voice->accum & ~0x000003fc) | ((data & 0x00ff) << 2);
			if (ACCESSING_BITS_8_15)
				voice->accum = (voice->accum & ~0x0003fc00) | ((data & 0xff00) << 2);
			LOG("%s:voice %d, accum=%08x\n", machine().describe_context(), m_current_page & 0x1f, voice->accum);
			break;

		case 0x0c:  /* unused */
			break;

		case 0x0d:  /* ACT */
			if (ACCESSING_BITS_0_7)
			{
				m_active_voices = data & 0x1f;
				m_sample_rate = m_master_clock / (16 * (m_active_voices + 1));
				m_stream->set_sample_rate(m_sample_rate);
				if (!m_sample_rate_changed_cb.isnull())
					m_sample_rate_changed_cb(m_sample_rate);

				LOG("active voices=%d, sample_rate=%d\n", m_active_voices, m_sample_rate);
			}
			break;

		case 0x0e:  /* IRQV - read only */
			break;

		case 0x0f:  /* PAGE */
			if (ACCESSING_BITS_0_7)
				m_current_page = data & 0x7f;
			break;
	}
}


inline void es5505_device::reg_write_high(es550x_voice *voice, offs_t offset, uint16_t data, uint16_t mem_mask)
{
	switch (offset)
	{
		case 0x00:  /* CR */
			if (ACCESSING_BITS_0_7)
			{
				voice->control &= ~(CONTROL_STOPMASK | CONTROL_BS0 | CONTROL_LOOPMASK | CONTROL_IRQE | CONTROL_DIR | CONTROL_IRQ);
				voice->control |= (data & (CONTROL_STOPMASK | CONTROL_LOOPMASK | CONTROL_IRQE | CONTROL_DIR | CONTROL_IRQ)) |
									((data << 12) & CONTROL_BS0);
			}
			if (ACCESSING_BITS_8_15)
			{
				voice->control &= ~(CONTROL_CA0 | CONTROL_CA1 | CONTROL_LPMASK);
				voice->control |= ((data >> 2) & CONTROL_LPMASK) |
									((data << 2) & (CONTROL_CA0 | CONTROL_CA1));
			}
			LOG("%s:voice %d, control=%04x (raw=%04x & %04x)\n", machine().describe_context(), m_current_page & 0x1f, voice->control, data, mem_mask);
			break;

		case 0x01:  /* O4(n-1) */
			if (ACCESSING_BITS_0_7)
				voice->o4n1 = (voice->o4n1 & ~0x00ff) | (data & 0x00ff);
			if (ACCESSING_BITS_8_15)
				voice->o4n1 = (int16_t)((voice->o4n1 & ~0xff00) | (data & 0xff00));
			LOG("%s:voice %d, O4(n-1)=%05x\n", machine().describe_context(), m_current_page & 0x1f, voice->o4n1 & 0x3ffff);
			break;

		case 0x02:  /* O3(n-1) */
			if (ACCESSING_BITS_0_7)
				voice->o3n1 = (voice->o3n1 & ~0x00ff) | (data & 0x00ff);
			if (ACCESSING_BITS_8_15)
				voice->o3n1 = (int16_t)((voice->o3n1 & ~0xff00) | (data & 0xff00));
			LOG("%s:voice %d, O3(n-1)=%05x\n", machine().describe_context(), m_current_page & 0x1f, voice->o3n1 & 0x3ffff);
			break;

		case 0x03:  /* O3(n-2) */
			if (ACCESSING_BITS_0_7)
				voice->o3n2 = (voice->o3n2 & ~0x00ff) | (data & 0x00ff);
			if (ACCESSING_BITS_8_15)
				voice->o3n2 = (int16_t)((voice->o3n2 & ~0xff00) | (data & 0xff00));
			LOG("%s:voice %d, O3(n-2)=%05x\n", machine().describe_context(), m_current_page & 0x1f, voice->o3n2 & 0x3ffff);
			break;

		case 0x04:  /* O2(n-1) */
			if (ACCESSING_BITS_0_7)
				voice->o2n1 = (voice->o2n1 & ~0x00ff) | (data & 0x00ff);
			if (ACCESSING_BITS_8_15)
				voice->o2n1 = (int16_t)((voice->o2n1 & ~0xff00) | (data & 0xff00));
			LOG("%s:voice %d, O2(n-1)=%05x\n", machine().describe_context(), m_current_page & 0x1f, voice->o2n1 & 0x3ffff);
			break;

		case 0x05:  /* O2(n-2) */
			if (ACCESSING_BITS_0_7)
				voice->o2n2 = (voice->o2n2 & ~0x00ff) | (data & 0x00ff);
			if (ACCESSING_BITS_8_15)
				voice->o2n2 = (int16_t)((voice->o2n2 & ~0xff00) | (data & 0xff00));
			LOG("%s:voice %d, O2(n-2)=%05x\n", machine().describe_context(), m_current_page & 0x1f, voice->o2n2 & 0x3ffff);
			break;

		case 0x06:  /* O1(n-1) */
			if (ACCESSING_BITS_0_7)
				voice->o1n1 = (voice->o1n1 & ~0x00ff) | (data & 0x00ff);
			if (ACCESSING_BITS_8_15)
				voice->o1n1 = (int16_t)((voice->o1n1 & ~0xff00) | (data & 0xff00));
			LOG("%s:voice %d, O1(n-1)=%05x (accum=%08x)\n", machine().describe_context(), m_current_page & 0x1f, voice->o2n1 & 0x3ffff, voice->accum);
			break;

		case 0x07:
		case 0x08:
		case 0x09:
		case 0x0a:
		case 0x0b:
		case 0x0c:  /* unused */
			break;

		case 0x0d:  /* ACT */
			if (ACCESSING_BITS_0_7)
			{
				m_active_voices = data & 0x1f;
				m_sample_rate = m_master_clock / (16 * (m_active_voices + 1));
				m_stream->set_sample_rate(m_sample_rate);
				if (!m_sample_rate_changed_cb.isnull())
					m_sample_rate_changed_cb(m_sample_rate);

				LOG("active voices=%d, sample_rate=%d\n", m_active_voices, m_sample_rate);
			}
			break;

		case 0x0e:  /* IRQV - read only */
			break;

		case 0x0f:  /* PAGE */
			if (ACCESSING_BITS_0_7)
				m_current_page = data & 0x7f;
			break;
	}
}


inline void es5505_device::reg_write_test(es550x_voice *voice, offs_t offset, uint16_t data, uint16_t mem_mask)
{
	switch (offset)
	{
		case 0x00:  /* CH0L */
		case 0x01:  /* CH0R */
		case 0x02:  /* CH1L */
		case 0x03:  /* CH1R */
		case 0x04:  /* CH2L */
		case 0x05:  /* CH2R */
		case 0x06:  /* CH3L */
		case 0x07:  /* CH3R */
			break;

		case 0x08:  /* SERMODE */
			m_mode = data & 0x0007;
			break;

		case 0x09:  /* PAR */
			break;

		case 0x0d:  /* ACT */
			if (ACCESSING_BITS_0_7)
			{
				m_active_voices = data & 0x1f;
				m_sample_rate = m_master_clock / (16 * (m_active_voices + 1));
				m_stream->set_sample_rate(m_sample_rate);
				if (!m_sample_rate_changed_cb.isnull())
					m_sample_rate_changed_cb(m_sample_rate);

				LOG("active voices=%d, sample_rate=%d\n", m_active_voices, m_sample_rate);
			}
			break;

		case 0x0e:  /* IRQV - read only */
			break;

		case 0x0f:  /* PAGE */
			if (ACCESSING_BITS_0_7)
				m_current_page = data & 0x7f;
			break;
	}
}


WRITE16_MEMBER( es5505_device::write )
{
	es550x_voice *voice = &m_voice[m_current_page & 0x1f];

//  logerror("%s:ES5505 write %02x/%02x = %04x & %04x\n", machine().describe_context(), m_current_page, offset, data, mem_mask);

	/* force an update */
	m_stream->update();

	/* switch off the page and register */
	if (m_current_page < 0x20)
		reg_write_low(voice, offset, data, mem_mask);
	else if (m_current_page < 0x40)
		reg_write_high(voice, offset, data, mem_mask);
	else
		reg_write_test(voice, offset, data, mem_mask);
}



/**********************************************************************************************

     reg_read -- read from the specified ES5505 register

***********************************************************************************************/

inline uint16_t es5505_device::reg_read_low(es550x_voice *voice, offs_t offset)
{
	uint16_t result = 0;

	switch (offset)
	{
		case 0x00:  /* CR */
			result = (voice->control & (CONTROL_STOPMASK | CONTROL_LOOPMASK | CONTROL_IRQE | CONTROL_DIR | CONTROL_IRQ)) |
						((voice->control & CONTROL_BS0) >> 12) |
						((voice->control & CONTROL_LPMASK) << 2) |
						((voice->control & (CONTROL_CA0 | CONTROL_CA1)) >> 2) |
						0xf000;
			break;

		case 0x01:  /* FC */
			result = voice->freqcount >> 1;
			break;

		case 0x02:  /* STRT (hi) */
			result = voice->start >> 18;
			break;

		case 0x03:  /* STRT (lo) */
			result = voice->start >> 2;
			break;

		case 0x04:  /* END (hi) */
			result = voice->end >> 18;
			break;

		case 0x05:  /* END (lo) */
			result = voice->end >> 2;
			break;

		case 0x06:  /* K2 */
			result = voice->k2;
			break;

		case 0x07:  /* K1 */
			result = voice->k1;
			break;

		case 0x08:  /* LVOL */
			result = voice->lvol;
			break;

		case 0x09:  /* RVOL */
			result = voice->rvol;
			break;

		case 0x0a:  /* ACC (hi) */
			result = voice->accum >> 18;
			break;

		case 0x0b:  /* ACC (lo) */
			result = voice->accum >> 2;
			break;

		case 0x0c:  /* unused */
			break;

		case 0x0d:  /* ACT */
			result = m_active_voices;
			break;

		case 0x0e:  /* IRQV */
			result = m_irqv;
			update_internal_irq_state();
			break;

		case 0x0f:  /* PAGE */
			result = m_current_page;
			break;
	}
	return result;
}


inline uint16_t es5505_device::reg_read_high(es550x_voice *voice, offs_t offset)
{
	uint16_t result = 0;

	switch (offset)
	{
		case 0x00:  /* CR */
			result = (voice->control & (CONTROL_STOPMASK | CONTROL_LOOPMASK | CONTROL_IRQE | CONTROL_DIR | CONTROL_IRQ)) |
						((voice->control & CONTROL_BS0) >> 12) |
						((voice->control & CONTROL_LPMASK) << 2) |
						((voice->control & (CONTROL_CA0 | CONTROL_CA1)) >> 2) |
						0xf000;
			break;

		case 0x01:  /* O4(n-1) */
			result = voice->o4n1;
			break;

		case 0x02:  /* O3(n-1) */
			result = voice->o3n1;
			break;

		case 0x03:  /* O3(n-2) */
			result = voice->o3n2;
			break;

		case 0x04:  /* O2(n-1) */
			result = voice->o2n1;
			break;

		case 0x05:  /* O2(n-2) */
			result = voice->o2n2;
			break;

		case 0x06:  /* O1(n-1) */
			/* special case for the Taito F3 games: they set the accumulator on a stopped */
			/* voice and assume the filters continue to process the data. They then read */
			/* the O1(n-1) in order to extract raw data from the sound ROMs. Since we don't */
			/* want to waste time filtering stopped channels, we just look for a read from */
			/* this register on a stopped voice, and return the raw sample data at the */
			/* accumulator */
			if ((voice->control & CONTROL_STOPMASK) && m_region_base[voice->control >> 14])
			{
				voice->o1n1 = m_region_base[voice->control >> 14][voice->exbank + (voice->accum >> 11)];
				// logerror("%02x %08x ==> %08x\n",voice->o1n1,voice->control >> 14,voice->exbank + (voice->accum >> 11));
			}
			result = voice->o1n1;
			break;

		case 0x07:
		case 0x08:
		case 0x09:
		case 0x0a:
		case 0x0b:
		case 0x0c:  /* unused */
			break;

		case 0x0d:  /* ACT */
			result = m_active_voices;
			break;

		case 0x0e:  /* IRQV */
			result = m_irqv;
			update_internal_irq_state();
			break;

		case 0x0f:  /* PAGE */
			result = m_current_page;
			break;
	}
	return result;
}


inline uint16_t es5505_device::reg_read_test(es550x_voice *voice, offs_t offset)
{
	uint16_t result = 0;

	switch (offset)
	{
		case 0x00:  /* CH0L */
		case 0x01:  /* CH0R */
		case 0x02:  /* CH1L */
		case 0x03:  /* CH1R */
		case 0x04:  /* CH2L */
		case 0x05:  /* CH2R */
		case 0x06:  /* CH3L */
		case 0x07:  /* CH3R */
			break;

		case 0x08:  /* SERMODE */
			result = m_mode;
			break;

		case 0x09:  /* PAR */
			if (!m_read_port_cb.isnull())
				result = m_read_port_cb(0);
			break;

		/* The following are global, and thus accessible form all pages */
		case 0x0d:  /* ACT */
			result = m_active_voices;
			break;

		case 0x0e:  /* IRQV */
			result = m_irqv;
			update_internal_irq_state();
			break;

		case 0x0f:  /* PAGE */
			result = m_current_page;
			break;
	}
	return result;
}


READ16_MEMBER( es5505_device::read )
{
	es550x_voice *voice = &m_voice[m_current_page & 0x1f];
	uint16_t result;

	LOG("read from %02x/%02x -> ", m_current_page, offset);

	/* force an update */
	m_stream->update();

	/* switch off the page and register */
	if (m_current_page < 0x20)
		result = reg_read_low(voice, offset);
	else if (m_current_page < 0x40)
		result = reg_read_high(voice, offset);
	else
		result = reg_read_test(voice, offset);

	LOG("%04x (accum=%08x)\n", result, voice->accum);

	/* return the high byte */
	return result;
}



void es5505_device::voice_bank_w(int voice, int bank)
{
#if RAINE_CHECK
	m_voice[voice].control = CONTROL_STOPMASK;
#endif
	m_voice[voice].exbank=bank;
}


//-------------------------------------------------
//  sound_stream_update - handle a stream update
//-------------------------------------------------

void es550x_device::sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
{
}

void es5506_device::sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
{
#if ES5506_MAKE_WAVS
	/* start the logging once we have a sample rate */
	if (m_sample_rate)
	{
		if (!m_wavraw)
			m_wavraw = wav_open("raw.wav", m_sample_rate, 2);
	}
#endif

	/* loop until all samples are output */
	int offset = 0;
	while (samples)
	{
		int length = (samples > MAX_SAMPLE_CHUNK) ? MAX_SAMPLE_CHUNK : samples;

		generate_samples(outputs, offset, length);

#if ES5506_MAKE_WAVS
		/* log the raw data */
		if (m_wavraw) {
			/* determine left/right source data */
			int32_t *lsrc = m_scratch, *rsrc = m_scratch + length;
			int channel;
			memset(lsrc, 0, sizeof(int32_t) * length * 2);
			/* loop over the output channels */
			for (channel = 0; channel < m_channels; channel++) {
				int32_t *l = outputs[(channel << 1)] + offset;
				int32_t *r = outputs[(channel << 1) + 1] + offset;
				/* add the current channel's samples to the WAV data */
				for (samp = 0; samp < length; samp++) {
					lsrc[samp] += l[samp];
					rsrc[samp] += r[samp];
				}
			}
			wav_add_data_32lr(m_wavraw, lsrc, rsrc, length, 4);
		}
#endif

		/* account for these samples */
		offset += length;
		samples -= length;
	}
}

void es5505_device::sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
{
#if ES5506_MAKE_WAVS
	/* start the logging once we have a sample rate */
	if (m_sample_rate)
	{
		if (!m_wavraw)
			m_wavraw = wav_open("raw.wav", m_sample_rate, 2);
	}
#endif

	/* loop until all samples are output */
	int offset = 0;
	while (samples)
	{
		int length = (samples > MAX_SAMPLE_CHUNK) ? MAX_SAMPLE_CHUNK : samples;

		generate_samples(outputs, offset, length);

#if ES5506_MAKE_WAVS
		/* log the raw data */
		if (m_wavraw) {
			/* determine left/right source data */
			int32_t *lsrc = m_scratch, *rsrc = m_scratch + length;
			int channel;
			memset(lsrc, 0, sizeof(int32_t) * length * 2);
			/* loop over the output channels */
			for (channel = 0; channel < m_channels; channel++) {
				int32_t *l = outputs[(channel << 1)] + offset;
				int32_t *r = outputs[(channel << 1) + 1] + offset;
				/* add the current channel's samples to the WAV data */
				for (samp = 0; samp < length; samp++) {
					lsrc[samp] += l[samp];
					rsrc[samp] += r[samp];
				}
			}
			wav_add_data_32lr(m_wavraw, lsrc, rsrc, length, 4);
		}
#endif

		/* account for these samples */
		offset += length;
		samples -= length;
	}
}