Debian Codegraph @

src/engine/platform/sound/m114s.c

raw
1 // license:BSD-3-Clause
2
3 /**********************************************************************************************
4 *
5 * SGS-Thomson Microelectronics M114S/M114A/M114AF Digital Sound Generator
6 * by Steve Ellenoff
7 * 09/02/2004
8 *
9 * Thanks to R.Belmont for Support & Agreeing to read through that nasty data sheet with me..
10 * Big thanks to Destruk for help in tracking down the data sheet.. Could have never done it
11 * without it!!
12 *
13 * A note about the word "table" as used by the M114S datasheet. A table refers to rom data
14 * representing 1 full period/cycle of a sound. The chip always reads from 2 different tables
15 * and intermixes them during playback for smoother sound. Different table lengths simply allow
16 * the chip to play lower frequencies due to the fact that the table represents 1 full period,ie
17 * 1 full sine wave, if that's what the rom data happens to contain.
18 *
19 * It would seem that the chip comes in two versions - the M114A 4Mhz & M114AF 6Mhz versions. Unlike other sound chips which
20 * allow for clock variations on the same chip, this chip uses hard coded frequency tables
21 * in an internal ROM based on which version of the chip is being used.
22 *
23 * Some ideas were taken from the source from BSMT2000 & AY8910
24 *
25 * Heavily modified/extended by Carsten Waechter later-on
26 *
27 * The mixer can optionally mix every (internal) channel into a separate stream for testing (set M114S_OUTPUT_CHANNELS to 16)
28 *
29 * TODO: - No full/tested support for the M114AF 6Mhz version of the chip (but seems at least to work 'good enough')
30 * - Maybe also low pass filter heavily/some channels only?? (some channels sound too high frequency heavy (clicks, pops), for example on Dakar)
31 **********************************************************************************************/
32
33 #include "m114s.h"
34 #include <stdbool.h>
35
36 #if defined(_MSC_VER) && (_MSC_VER <= 1500)
37 #define llabs _abs64
38 #endif
39
40 #define SAMPLE_RATE 48000
41
42 #define USE_FREQTABLE_FROM_MANUAL // undef to use a generated freqtable with 'correct' semitones (i.e. unlike the real chip) for the M114A 4MHz
43
44 //!! unsure how the actual vol envelope actually works internally and at what precision -> spec says 'either immediate' (=no envelope) or 'gradual increments of 1/256 of maximum amplitude'
45 // BUT THEN also mentions: only 8 MSBits from the 10 of v_linear are influenced! -> simple increase/decrease of the 8 MSBits with same frequency as table read (if diff > 128, every 2nd read if diff > 64, every 4th read if diff > 32 or every 8th read if diff <= 32)
46 #define USE_VOL_ENVELOPE // to test no volume envelope (=no smooth blending in of new volume that is set), set to 0
47
48 #define DO_FULL_PRECISION_MIXING // unclear how the mixing of channels and interpolation of channels works internally, so this allows to switch between two modes (use highest precision vs a lower precision/interpretation of the datasheet text)
49 #define USE_LERP_FOR_REPEATED_SAMPLES // unclear how repeated table reads really work, datasheet can be interpreted either way
50
51 #define MR_GAME_VOLUME_HACK // rather do this for specific machines only!? (i.e. is this only due to the filter network of output channels of Mr.Game??!)
52
53 #if 0
54 #include <stdio.h>
55 #include <stdlib.h>
56 #define LOG(x) printf x
57 #else
58 #define LOG(x) logerror x
59 #endif
60
61 /**********************************************************************************************
62
63 CONSTANTS
64
65 ***********************************************************************************************/
66
67 #define FRAC_BITS 16
68 #define FRAC_ONE (1 << FRAC_BITS)
69 #define FRAC_MASK (FRAC_ONE - 1)
70
71 /**********************************************************************************************
72
73 GLOBALS
74
75 ***********************************************************************************************/
76
77 /* Table 1 & Table 2 Repetition Values based on Mode */
78 static const int mode_to_rep[8][2] = {
79 {2,2}, //Mode 0
80 {1,1}, //Mode 1
81 {4,4}, //Mode 2
82 {1,1}, //Mode 3
83 {1,2}, //Mode 4
84 {1,1}, //Mode 5
85 {1,4}, //Mode 6
86 {1,1} //Mode 7
87 };
88
89 /* Table 1 Length Values based on Mode */ // these are always larger as table 2 values! (thus mixing code below makes sense to not handle the other case)
90 static const int mode_to_len_t1[8][8] = {
91 {16,32,64,128,256,512,1024,2048 }, //Mode 0
92 {16,32,64,128,256,512,1024,2048 }, //Mode 1
93 {16,32,64,128,256,512,1024,1024 }, //Mode 2
94 {16,32,64,128,256,512,1024,2048 }, //Mode 3
95 {16,32,64,128,256,512,1024,2048 }, //Mode 4
96 {16,32,64,128,256,512,1024,2048 }, //Mode 5
97 {16,32,64,128,256,512,1024,2048 }, //Mode 6
98 {16,32,64,128,256,512,1024,2048 }, //Mode 7
99 };
100
101 /* Table 2 Length Values based on Mode */
102 static const int mode_to_len_t2[8][8] = {
103 {16,32,64,128,256,512,1024,2048 }, //Mode 0 //datasheet has last value = 1048 //!! -> try 1024 instead??
104 {16,32,64,128,256,512,1024,2048 }, //Mode 1
105 {16,32,64,128,256,512,1024,1024 }, //Mode 2
106 {16,32,64,128,256,512,1024,2048 }, //Mode 3
107 { 8,16,32, 64,128,256, 512,1024 }, //Mode 4
108 {16,16,16, 32, 64,128, 256, 512 }, //Mode 5
109 { 4, 8,16, 32, 64,128, 256, 512 }, //Mode 6
110 {16,16,16, 16, 32, 64, 128, 256 }, //Mode 7
111 };
112
113 /* Attenuation Table */
114 static const int v_linear[32] = {
115 1023,939,863,791,727,667,611,559,515,471,431,
116 395,363,335,307,283,259,235,215,199,183,166,
117 152,140,128,117,107, 98, 90, 83, 76, 69
118 };
119 // a_decibel = 20 * log((v_linear+1)/1024)
120 /*static const double a_decibel[32] = {
121 0.00, 0.74, 1.48, 2.23, 2.96, 3.71, 4.47, 5.24, 5.95,
122 6.73, 7.50, 8.25, 8.98, 9.68,10.43,11.14,11.91,12.75,
123 13.52,14.19,14.91,15.75,16.51,17.22,17.99,18.77,19.54,
124 20.29,21.03,21.72,22.48,23.30
125 };*/
126
127 #ifdef USE_FREQTABLE_FROM_MANUAL
128 /* Frequency Table for a 4Mhz Clocked Chip */
129 static const double freqtable4Mhz[256] = {
130 1016.78,1021.45,1026.69,1031.46,1036.27,1041.67,1044.39,1045.48,
131 1046.57,1047.67,1048.77,1051.52,1056.52,1061.57,1066.67,1071.81, //0x00 - 0x0F
132 1077.01,1082.25,1087.55,1092.90,1098.30,1103.14,1106.81,1107.42,
133 1108.65,1109.88,1111.11,1114.21,1119.19,1124.86,1130.58,1135.72, //0x10 - 0x1F
134 1140.90,1146.79,1152.07,1158.08,1163.47,1168.91,1172.33,1173.71,
135 1174.40,1175.78,1177.16,1180.64,1186.24,1191.90,1197.60,1203.37, //0x20 - 0x2F
136 1209.19,1215.07,1221.00,1226.99,1232.29,1238.39,1242.24,1243.78,
137 1244.56,1245.33,1246.88,1250.78,1256.28,1262.63,1269.04,1274.70, //0x30 - 0x3F
138 1281.23,1287.00,1293.66,1299.55,1305.48,1312.34,1315.79,1317.52,
139 1318.39,1319.26,1321.00,1324.50,1331.56,1337.79,1344.09,1350.44, //0x40 - 0x4F
140 1356.85,1363.33,1369.86,1376.46,1383.13,1389.85,1393.73,1395.67,
141 1396.65,1397.62,1398.60,1403.51,1410.44,1417.43,1424.50,1430.62, //0x50 - 0x5F
142 1437.81,1445.09,1451.38,1458.79,1466.28,1472.75,1478.20,1479.29,
143 1480.38,1481.48,1482.58,1486.99,1494.77,1501.50,1508.30,1516.30, //0x60 - 0x6F
144 1523.23,1530.22,1538.46,1545.60,1552.80,1560.06,1564.95,1566.17,
145 1567.40,1568.63,1569.86,1576.04,1583.53,1591.09,1598.72,1606.43, //0x70 - 0x7F
146 1614.21,1622.06,1629.99,1638.00,1644.74,1652.89,1658.37,1659.75,
147 1661.13,1662.51,1663.89,1669.45,1677.85,1684.92,1693.48,1702.13, //0x80 - 0x8F
148 1709.40,1718.21,1727.12,1734.61,1743.68,1751.31,1757.47,1759.01, //(2nd entry in manual shows 1781.21 but that's cleary wrong)
149 1760.56,1762.11,1763.89,1768.35,1777.78,1785.71,1793.72,1803.43, //0x90 - 0x9F
150 1811.59,1819.84,1829.83,1838.24,1846.72,1855.29,1860.47,1862.20,
151 1863.93,1865.67,1867.41,1874.41,1883.24,1892.15,1901.14,1910.22, //0xA0 - 0xAF
152 1919.39,1928.64,1937.98,1947.42,1956.95,1966.57,1972.39,1974.33,
153 1976.28,1978.24,1980.20,1984.13,1994.02,2004.01,2014.10,2024.29, //0xB0 - 0xBF
154 2032.52,2042.90,2053.39,2063.98,2072.54,2083.33,2087.68,2089.86,
155 2092.05,2094.24,2096.44,2103.05,2114.16,2123.14,2134.47,2143.62, //0xC0 - 0xCF
156 2155.17,2164.50,2176.28,2185.79,2195.39,2207.51,2212.39,2214.84,
157 2217.29,2219.76,2222.22,2227.17,2239.64,2249.72,2259.89,2272.73, //0xD0 - 0xDF
158 2283.11,2293.58,2304.15,2314.81,2325.58,2339.18,2344.67,2347.42,
159 2350.18,2352.94,2355.71,2361.28,2372.48,2383.79,2395.21,2406.74, //0xE0 - 0xEF
160 0,0,0,0,0,0,0,0,
161 0,0,0,0,0,0,0,0, //0xF0 - 0xFF (Special codes)
162 };
163
164 /* Frequency Table for a 6Mhz Clocked Chip */
165 static const double freqtable6Mhz[256] = {
166 1523.78,1523.79,1538.64,1545.79,1552.99,1561.08,1565.16,1566.80,
167 1568.44,1570.08,1571.73,1575.86,1583.35,1590.91,1598.55,1606.26, //0x00 - 0x0F
168 1614.04,1621.90,1629.84,1637.86,1645.95,1653.22,1658.71,1659.62,
169 1661.46,1663.31,1665.16,1669.79,1677.27,1685.76,1694.34,1702.03, //0x10 - 0x1F
170 1709.80,1718.62,1726.54,1735.54,1743.62,1751.77,1758.91,1758.97,
171 1760.00,1762.07,1764.14,1769.35,1777.75,1786.22,1794.78,1803.42, //0x20 - 0x2F
172 1812.14,1820.95,1829.84,1838.82,1846.75,1855.90,1861.66,1863.98,
173 1865.14,1866.30,1868.63,1874.47,1882.71,1892.22,1901.83,1910.31, //0x30 - 0x3F
174 1920.10,1928.75,1938.73,1947.55,1956.45,1966.72,1971.89,1974.49,
175 1975.79,1977.10,1979.71,1984.95,1995.53,2004.87,2014.30,2023.82, //0x40 - 0x4F
176 2033.43,2043.14,2052.93,2062.82,2072.82,2082.89,2088.70,2091.61,
177 2093.07,2094.54,2096.00,2103.35,2113.74,2124.22,2134.81,2143.98, //0x50 - 0x5F
178 2154.77,2165.66,2175.09,2186.20,2197.42,2207.13,2215.28,2216.92,
179 2218.56,2220.21,2221.85,2228.46,2240.12,2250.21,2260.39,2272.39, //0x60 - 0x6F
180 2282.77,2293.25,2305.60,2316.29,2327.08,2337.97,2345.29,2347.13,
181 2348.97,2350.81,2352.65,2361.92,2373.14,2384.47,2395.91,2407.45, //0x70 - 0x7F
182 2419.11,2430.88,2442.77,2454.77,2464.87,2477.09,2485.31,2487.37,
183 2489.44,2491.50,2493.58,2501.90,2514.50,2525.09,2537.92,2550.88, //0x80 - 0x8F
184 2561.78,2574.98,2588.32,2599.55,2613.15,2624.59,2633.81,2636.13,
185 2638.45,2640.78,2643.10,2650.11,2664.25,2676.14,2688.14,2702.69, //0x90 - 0x9F
186 2714.93,2727.28,2742.25,2754.85,2767.57,2780.41,2788.17,2790.76,
187 2793.06,2795.97,2798.58,2809.07,2822.30,2835.65,2849.13,2862.73, //0xA0 - 0xAF
188 2876.47,2890.34,2904.34,2918.48,2932.76,2947.18,2955.90,2958.82,
189 2961.74,2964.67,2967.60,2973.49,2988.31,3003.29,3018.41,3033.68, //0xB0 - 0xBF
190 3046.02,3061.57,3077.29,3093.17,3105.99,3122.17,3128.68,3131.95,
191 3135.23,3138.51,3141.80,3151.71,3168.37,3181.83,3198.80,3212.52, //0xC0 - 0xCF
192 3229.83,3243.81,3261.46,3275.72,3290.10,3308.26,3315.58,3319.25,
193 3322.93,3326.61,3330.31,3337.73,3356.42,3371.52,3386.76,3406.00, //0xD0 - 0xDF
194 3421.55,3437.25,3453.09,3469.07,3485.21,3505.59,3513.81,3517.93,
195 3522.07,3526.21,3530.37,3538.70,3555.49,3572.44,3589.56,3606.84, //0xE0 - 0xEF
196 0,0,0,0,0,0,0,0,
197 0,0,0,0,0,0,0,0, //0xF0 - 0xFF (Special codes)
198 };
199 #else
200 /* Frequency Table for a 4Mhz Clocked Chip
201 Note: Only the 1st set of frequencies came from the manual, the rest were calculated using 1 semitone increments
202 using a program called Test Tone Generator 3.91
203 */
204 static const double freqtable4Mhz[256] = {
205 1016.78,1021.45,1026.69,1031.46,1036.27,1041.67,1044.39,1045.48,
206 1046.57,1047.67,1048.77,1051.52,1056.52,1061.57,1066.67,1071.81, //0x00 - 0x0F
207 1077.24,1082.19,1087.74,1092.79,1097.89,1103.61,1106.49,1107.65,
208 1108.80,1109.97,1111.13,1114.05,1119.34,1124.69,1130.10,1135.54, //0x10 - 0x1F
209 1141.29,1146.54,1152.42,1157.77,1163.17,1169.24,1172.29,1173.51,
210 1174.74,1175.97,1177.20,1180.29,1185.90,1191.57,1197.30,1203.07, //0x20 - 0x2F
211 1209.16,1214.72,1220.95,1226.62,1232.34,1238.76,1242.00,1243.29,
212 1244.59,1245.90,1247.20,1250.47,1256.42,1262.43,1268.49,1274.60, //0x30 - 0x3F
213 1281.06,1286.95,1293.55,1299.56,1305.62,1312.42,1315.85,1317.22,
214 1318.60,1319.98,1321.37,1324.83,1331.13,1337.49,1343.92,1350.40, //0x40 - 0x4F
215 1357.24,1363.47,1370.46,1376.83,1383.25,1390.46,1394.09,1395.55,
216 1397.00,1398.47,1399.94,1403.61,1410.29,1417.03,1423.83,1430.70, //0x50 - 0x5F
217 1437.94,1444.55,1451.96,1457.07,1465.51,1473.14,1477.00,1478.53,
218 1480.07,1481.63,1483.18,1486.99,1494.14,1501.29,1508.50,1515.77, //0x60 - 0x6F
219 1523.45,1530.45,1538.30,1545.44,1552.65,1560.74,1564.82,1566.45,
220 1568.08,1569.73,1571.38,1575.50,1583.00,1590.56,1598.20,1605.90, //0x70 - 0x7F
221 1614.04,1621.45,1629.77,1637.34,1644.98,1653.55,1657.87,1659.60,
222 1661.33,1663.07,1664.82,1669.18,1677.12,1685.14,1693.23,1701.39, //0x80 - 0x8F
223 1710.01,1717.87,1726.69,1734.70,1742.79,1751.87,1756.45,1758.28,
224 1760.11,1761.96,1763.81,1768.44,1776.85,1785.34,1793.92,1802.56, //0x90 - 0x9F
225 1811.70,1820.02,1829.35,1837.85,1846.42,1856.05,1860.89,1862.83,
226 1864.78,1866.74,1868.70,1873.60,1882.50,1891.50,1900.59,1909.75, //0xA0 - 0xAF
227 1919.43,1928.24,1938.13,1947.14,1956.22,1966.41,1971.55,1973.60,
228 1975.66,1977.74,1979.81,1985.01,1994.44,2003.98,2013.61,2023.31, //0xB0 - 0xBF
229 2033.56,2042.90,2053.38,2062.92,2072.54,2083.34,2088.78,2090.96,
230 2093.14,2095.34,2097.54,2103.04,2113.04,2123.14,2133.34,2143.62, //0xC0 - 0xCF
231 2154.48,2164.38,2175.48,2185.59,2195.78,2207.22,2212.99,2215.30,
232 2217.60,2219.94,2222.27,2228.09,2238.69,2249.39,2260.20,2271.09, //0xD0 - 0xDF
233 2282.59,2293.08,2304.84,2315.55,2326.35,2338.47,2344.58,2347.02,
234 2349.47,2351.94,2354.41,2360.58,2371.80,2383.14,2394.59,2406.13, //0xE0 - 0xEF
235 0,0,0,0,0,0,0,0,
236 0,0,0,0,0,0,0,0, //0xF0 - 0xFF (Special codes)
237 };
238 #endif
239
240 /**********************************************************************************************
241
242 read_sample -- returns 1 sample from the channel's output buffer, but upsamples/interpolates the data
243 as necessary to match the Machine driver's output sample rate.
244
245 ***********************************************************************************************/
246 int16_t read_sample(struct M114SChannel * const channel, const uint32_t length)
247 {
248 const uint32_t pos = channel->outpos >> FRAC_BITS;
249 if (pos < length)
250 {
251 const int32_t frac = channel->outpos & FRAC_MASK;
252
253 // interpolate
254 const int16_t val1 = channel->output[pos];
255 const int16_t val2 = channel->output[pos+1 < length ? pos : 0]; // wrap around to 0 (see code below!)?
256 const int16_t sample = (val1 * ((int32_t)FRAC_ONE - frac) + val2 * frac) >> FRAC_BITS;
257
258 channel->outpos += channel->incr;
259 return sample;
260 }
261 else {
262 //LOG(("End of Table\n"));
263 //channel->end_of_table++;
264 channel->outpos = 0; // related to cyclic reading of tables //!! rather wrap over including exact FRAC_BITS instead of just nulling it?
265 return channel->output[0]; //!! dto. would need interpolation then
266 }
267 }
268
269 /**********************************************************************************************
270
271 read_table -- Reads the two tables of rom data into a temporary buffer,
272 mixes the samples using the chip's internal interpolation equation,
273 applies the volume, and writes the single mixed sample to the output buffer for the channel.
274 It processes the entire table1 length of data.
275
276 Note: Eventually this should flag an End of Table, and should process new table data
277
278 ***********************************************************************************************/
279 void read_table(struct M114SChip * const chip, struct M114SChannel * const channel) // get rid of this and write directly to output buffer?!
280 {
281 int i;
282 #ifndef USE_LERP_FOR_REPEATED_SAMPLES
283 int j;
284 #endif
285 const int8_t * const rom = &chip->region_base[0];
286 const int t1start = channel->table1.start_address;
287 const int t2start = channel->table2.start_address;
288 const int lent1 = channel->table1.length;
289 const int lent2 = channel->table2.length;
290 const int rep1 = channel->table1.reread;
291 const int rep2 = channel->table2.reread;
292 const int intp = channel->regs.interp;
293
294 int8_t tb1[4096]; // Temp copy buffer for Table 1 // long enough to hold max sizes of 2048*2 or 1024*4
295 int8_t tb2[4096]; // Temp copy buffer for Table 2 // dto.
296 memset(&tb1,0,sizeof(tb1));
297 memset(&tb2,0,sizeof(tb2));
298
299 //LOG(("t1s = %d t2s = %d, l1=%d l2=%d, r1=%d, r2=%d, int = %d\n",t1start,t2start,lent1,lent2,rep1,rep2,intp));
300
301 // datasheet says: multiple reading permits interpolation between two adjoining samples on the same table, so do we really need to lerp instead of just repeating same values here?
302 #ifdef USE_LERP_FOR_REPEATED_SAMPLES
303 //Scan Table 1
304 if(rep1 == 1)
305 memcpy(tb1,&rom[t1start],lent1);
306 else if(rep1 == 2)
307 for(i=0; i<lent1; i++)
308 {
309 tb1[i*2+0] = rom[i+t1start];
310 tb1[i*2+1] = ((int)rom[i+t1start] + (int)rom[t1start + ((i < lent1-1) ? i+1 : i)])/2;
311 }
312 else //if(rep1 == 4)
313 for(i=0; i<lent1; i++)
314 {
315 int val1 = (int)rom[i+t1start];
316 int val2 = (int)rom[t1start + ((i < lent1-1) ? i+1 : i)];
317 tb1[i*4+0] = rom[i+t1start];
318 tb1[i*4+1] = (val1*3 + val2 )/4;
319 tb1[i*4+2] = (val1 + val2 )/2;
320 tb1[i*4+3] = (val1 + val2*3)/4;
321 }
322
323 //Scan Table 2
324 if (rep2 == 1)
325 memcpy(tb2,&rom[t2start+0x2000],lent2); //A13 is toggled high on Table 2 reading (Implementation specific - ie, Mr. Game)
326 else if(rep2 == 2)
327 for(i=0; i<lent2; i++)
328 {
329 tb2[i*2+0] = rom[i+t2start+0x2000];
330 tb2[i*2+1] = ((int)rom[i+t2start+0x2000] + (int)rom[t2start+0x2000 + ((i < lent2-1) ? i+1 : i)])/2;
331 }
332 else //if(rep2 == 4)
333 for(i=0; i<lent2; i++)
334 {
335 int val1 = (int)rom[i+t2start+0x2000];
336 int val2 = (int)rom[t2start+0x2000 + ((i < lent2-1) ? i+1 : i)];
337 tb2[i*4+0] = rom[i+t2start+0x2000];
338 tb2[i*4+1] = (val1*3 + val2 )/4;
339 tb2[i*4+2] = (val1 + val2 )/2;
340 tb2[i*4+3] = (val1 + val2*3)/4;
341 }
342 #else
343 //Scan Table 1
344 for(i=0; i<lent1; i++)
345 for(j=0; j<rep1; j++)
346 tb1[j+(i*rep1)] = rom[i+t1start];
347 //Scan Table 2
348 for(i=0; i<lent2; i++)
349 for(j=0; j<rep2; j++)
350 tb2[j+(i*rep2)] = rom[i+t2start+0x2000]; //A13 is toggled high on Table 2 reading (Implementation specific - ie, Mr. Game)
351 #endif
352
353 // Table1 is always larger, so use that as the size
354
355 // How to make up difference (table 2 can be shorter than table 1)? -> was memset above already, so zero for now
356 /*for(i=lent2*rep2; i<lent1*rep1; i++)
357 tb2[i] = ?;*/
358
359 // Now Mix based on Interpolation Bits
360 for(i=0; i<lent1*rep1; i++) {
361 int l;
362 //Apply volume - If envelope - inc/dec volume to calculate sample volume (only 8 most significant bits from the 10 bits, thus +/-4), otherwise, apply directly
363 #ifdef USE_VOL_ENVELOPE
364 if(channel->regs.env_enable && ((i & (abs(channel->step_rate_volume_env)-1)) == abs(channel->step_rate_volume_env)/2)) { // check if we match the frequency that the value must be updated
365 channel->current_volume += channel->step_rate_volume_env > 0 ? 4 : -4; // dependent on sign inc/dec by 4
366 if(channel->step_rate_volume_env > 0)
367 {
368 if(channel->current_volume > channel->target_volume)
369 channel->current_volume = channel->target_volume;
370 }
371 else if(channel->current_volume < channel->target_volume)
372 channel->current_volume = channel->target_volume;
373 }
374 #endif
375 //write to output buffer
376 #ifdef DO_FULL_PRECISION_MIXING
377 l = (int)tb1[i] * (intp + 1) + (int)tb2[i] * (15 - intp);
378 channel->output[i] = (int16_t)(0x6f * l * (channel->current_volume + 1) / (1024*16)); // Max Volume would be 256 for an int16_t value (so why was 0x6f chosen??)
379 #else
380 l = ((int)tb1[i] * (intp + 1) / 16) + ((int)tb2[i] * (15 - intp) / 16); // formula seen in datasheet, but unclear what this means precision wise (i.e. is this only meant as real number pseudo code?)
381 channel->output[i] = (int16_t)(0x6f * l * (channel->current_volume + 1) / 1024); // Max Volume would be 256 for an int16_t value (so why was 0x6f chosen??) //!! do 0x6f scale AFTER division??
382 #endif
383 }
384 }
385
386 /**********************************************************************************************
387
388 m114s_update -- update the sound chip so that it is in sync with CPU execution
389
390 ***********************************************************************************************/
391 //Seems this sometimes still produces some static, but I don't know why!
392 void m114s_update(struct M114SChip* chip,
393 int16_t **buffer,
394 int samples)
395 {
396
397 while (samples > 0)
398 {
399 #if M114S_OUTPUT_CHANNELS == 1
400 int32_t accum = 0;
401 #else
402 int32_t accum[M114S_OUTPUT_CHANNELS];
403 #endif
404 int c;
405
406 #if M114S_OUTPUT_CHANNELS != 1
407 /* clear accum */
408 for(c = 0; c < M114S_OUTPUT_CHANNELS; c++)
409 accum[c] = 0;
410 #endif
411
412 /* loop over channels */
413 for (c = 0; c < M114S_CHANNELS; c++)
414 {
415 struct M114SChannel * const channel = &chip->channels[c];
416 /* Grab the next sample from the table data if the channel is active */
417 if (channel->active)
418 {
419 //We use Table 1 to drive everything, as Table 2 is really for mixing into Table 1..
420 int32_t sample = read_sample(channel, channel->table1.total_length); //!! int16_t if MR_GAME_VOLUME_HACK would be off
421
422 #ifdef MR_GAME_VOLUME_HACK
423 sample = sample*chip->channel_volume[channel->regs.outputs] / 100; // boost percussion on Dakar, penalty some of the other instruments
424 #endif
425 //Mix the output of this channel to the appropriate output channel
426 #if M114S_OUTPUT_CHANNELS == 1
427 accum
428 #elif M114S_OUTPUT_CHANNELS == 4
429 accum[channel->regs.outputs]
430 #else
431 accum[c]
432 #endif
433 += sample;
434 }
435 }
436
437 /* Update the buffer & Ensure we don't clip */
438 #if M114S_OUTPUT_CHANNELS == 1
439 accum /= (int32_t)4;
440 *buffer++ = (accum < -32768) ? -32768 : ((accum > 32767) ? 32767 : accum);
441 #else
442 for (c = 0; c < M114S_OUTPUT_CHANNELS; c++)
443 *buffer[c]++ = (accum[c] < -32768) ? -32768 : ((accum[c] > 32767) ? 32767 : accum[c]);
444 #endif
445
446 samples--;
447 }
448 }
449
450 /**********************************************************************************************
451
452 M114S_sh_start -- start emulation of the M114S
453
454 ***********************************************************************************************/
455
456 INLINE void init_channel(struct M114SChannel * const channel)
457 {
458 //set all internal registers to 0!
459 channel->active = 0;
460 channel->outpos = 0;
461 channel->prev_volume = 0;
462 memset(&channel->output,0,sizeof(channel->output));
463 memset(&channel->regs, 0,sizeof(channel->regs));
464 memset(&channel->table1,0,sizeof(channel->table1));
465 memset(&channel->table2,0,sizeof(channel->table2));
466 }
467
468
469 INLINE void init_all_channels(struct M114SChip * const chip)
470 {
471 int i;
472
473 /* init the channels */
474 for (i = 0; i < M114S_CHANNELS; i++)
475 init_channel(&chip->channels[i]);
476
477 //Chip init stuff
478 memset(&chip->tempch_regs,0,sizeof(chip->tempch_regs));
479 chip->channel = 0;
480 chip->bytes_read = 0;
481 }
482
483
484 int M114S_sh_start(const struct MachineSound *msound)
485 {
486 const struct M114Sinterface *intf = msound->sound_interface;
487 #if M114S_OUTPUT_CHANNELS == 1
488 #else
489 int vol[M114S_OUTPUT_CHANNELS];
490 #endif
491 int i,j;
492
493 /* initialize the chips */
494 memset(&m114schip, 0, sizeof(m114schip));
495 for (i = 0; i < intf->num; i++)
496 {
497 /* Chip specific setup based on clock speed */
498 switch(intf->baseclock[i]) {
499 // M114A 4 Mhz
500 case 4000000:
501 m114schip[i].reset_cycles = 4000000 * 0.000128; // Chip resets in 128us (microseconds)
502 m114schip[i].is_M114A = 1;
503 break;
504 // M114AF 6 Mhz
505 case 6000000:
506 case 5994560: // from datasheet: 5.99456 MHz
507 m114schip[i].reset_cycles = intf->baseclock[i] * 0.000085; // Chip resets in 85us (microseconds)
508 m114schip[i].is_M114A = 0;
509 LOG(("M114S Chip #%d - 6Mhz chip clock not fully supported/tested at this time!\n", i));
510 return 1;
511 default:
512 LOG(("M114S Chip #%d - Invalid Base Clock value specified! Only 4Mhz & 6Mhz values allowed!\n",i));
513 return 1;
514 }
515
516 /* initialize the region & interface info */
517 m114schip[i].cpu_num = intf->cpunum[i];
518 m114schip[i].region_base = (int8_t *)memory_region(intf->region[i]);
519 m114schip[i].intf = (struct M114Sinterface *)intf;
520
521 /* init the channels */
522 init_all_channels(&m114schip[i]);
523 for(j = 0; j < 4; j++)
524 m114schip[i].channel_volume[j] = intf->mixing_level[i][j];
525 }
526
527 /* success */
528 return 0;
529 }
530
531 /**********************************************************************************************
532
533 M114S_sh_stop -- stop emulation of the M114S
534
535 ***********************************************************************************************/
536
537 void M114S_sh_stop(void)
538 {
539 }
540
541 /**********************************************************************************************
542
543 M114S_sh_reset -- reset emulation of the M114S
544
545 ***********************************************************************************************/
546
547 void M114S_sh_reset(void)
548 {
549 int i;
550 for (i = 0; i < 1; i++) {
551 /* reset all channels */
552 init_all_channels(&m114schip[i]);
553 }
554 }
555
556 /**********************************************************************************************
557
558 process_freq_codes -- There are up to 16 special values for frequency that signify a code
559
560 ***********************************************************************************************/
561 void process_freq_codes(struct M114SChip * const chip)
562 {
563 //Grab pointer to channel being programmed
564 struct M114SChannel * const channel = &chip->channels[chip->channel];
565 switch(channel->regs.frequency)
566 {
567 //ROMID - ROM Identification (Are you kidding me?)
568 case 0xf8:
569 LOG(("* * Channel: %02d: Frequency Code: %02x - ROMID * * \n",chip->channel,channel->regs.frequency));
570 break;
571 //SSG - Set Syncro Global
572 case 0xf9:
573 LOG(("* * Channel: %02d: Frequency Code: %02x - SSG * * \n",chip->channel,channel->regs.frequency));
574 break;
575 //RSS - Reverse Syncro Status
576 case 0xfa:
577 // check if used, as this will force frequency changes to wait until table ends
578 LOG(("* * Channel: %02d: Frequency Code: %02x - RSS * * \n",chip->channel,channel->regs.frequency));
579 break;
580 //RSG - Reset Syncro Global
581 case 0xfb:
582 LOG(("* * Channel: %02d: Frequency Code: %02x - RSG * * \n",chip->channel,channel->regs.frequency));
583 break;
584 //PSF - Previously Selected Frequency
585 case 0xfc:
586 // seems to be unused by Mr.Game
587 LOG(("* * Channel: %02d: Frequency Code: %02x - PSF * * \n",chip->channel,channel->regs.frequency));
588 break;
589 //FFT - Forced Table Termination
590 case 0xff:
591 //Stop whatever output from playing by simulating an end of table event!
592 channel->outpos = 0; // but this will just put the cyclic counter back to the beginning!?!
593 //LOG(("* * Channel: %02d: Frequency Code: %02x - FFT * * \n",chip->channel,channel->regs.frequency));
594 break;
595 default:
596 LOG(("* * Channel: %02d: Frequency Code: %02x - UNKNOWN * * \n",chip->channel,channel->regs.frequency));
597 break;
598 }
599 }
600
601 /**********************************************************************************************
602
603 process_channel_data -- complete programming for a channel now exists, process it!
604
605 ***********************************************************************************************/
606
607 void process_channel_data(struct M114SChip * const chip)
608 {
609 //Grab pointer to channel being programmed
610 struct M114SChannel * const channel = &chip->channels[chip->channel];
611
612 //Reset # of bytes for next group
613 chip->bytes_read = 0;
614
615 //Copy data to the appropriate channel registers from our temp channel registers
616 memcpy(&channel->regs,&chip->tempch_regs,sizeof(chip->tempch_regs));
617
618 //Look for the 16 special frequency codes
619 if(channel->regs.frequency >= 0xf0) {
620 process_freq_codes(chip);
621 //FFT & PSF are the only codes that should continue to process channel data AFAIK
622 if(channel->regs.frequency != 0xff && channel->regs.frequency != 0xfc)
623 return;
624 }
625
626 //If Attenuation set to 0x3F - The channel becomes inactive
627 if(channel->regs.atten == 0x3f) {
628 channel->active = 0;
629 return;
630 }
631 else
632 //Process this channel
633 {
634 //Calculate # of repetitions for Table 1 & Table 2
635 const int rep1 = mode_to_rep[channel->regs.read_meth][0];
636 const int rep2 = mode_to_rep[channel->regs.read_meth][1];
637 //Calculate Table Length for Table 1 & Table 2
638 const int lent1 = mode_to_len_t1[channel->regs.read_meth][channel->regs.table_len];
639 const int lent2 = mode_to_len_t2[channel->regs.read_meth][channel->regs.table_len];
640 //Start & Stop Address - Note the special case for table length of 16 - Bit 5 always 1 in this case // but also for special cases 8 and 4 for table 2?!?
641 //Calculate Table 1 Start & End Address in ROM
642 const int t1start = ((channel->regs.table1_addr<<5) & (~(lent1-1)&0x1fff)) | (lent1 == 16 ? 0x10 : 0); //T1 Addr is only upper 8 bits, but masked by length
643 //const int t1end = t1start | (lent1-1);
644 //Calculate Table 2 Start & End Address in ROM
645 const int t2start = ((channel->regs.table2_addr<<5) & (~(lent2-1)&0x1fff)) | (lent2 <= 16 ? 0x10 : 0); //T2 Addr is only upper 8 bits, but masked by length
646 //const int t2end = t2start | (lent2-1);
647
648 //Calculate initial frequency of both tables
649 double freq = chip->is_M114A ? freqtable4Mhz[channel->regs.frequency] : freqtable6Mhz[channel->regs.frequency];
650
651 //Calculate new volume
652 channel->target_volume = channel->regs.atten < 32 ? v_linear[channel->regs.atten] : 0; // channel must be kept active, but is it really 0? Or is the last value used (v_linear[31])? Or something inbetween??
653
654 //Adjust frequency if octave divisor set
655 if (channel->regs.oct_divisor)
656 freq /= 2.; // maybe new frequency must be clamped to freqtable[0]??
657
658 //Channel is now active!
659 channel->active = 1;
660
661 // Setup Sample Rate/Step size increase
662 channel->incr = (uint32_t)(freq * ((double)(16u << FRAC_BITS) / SAMPLE_RATE));
663
664 //Assign start & stop address offsets to ROM
665 channel->table1.start_address = t1start;
666 channel->table2.start_address = t2start;
667 //channel->table1.stop_address = t1end;
668 //channel->table2.stop_address = t2end;
669
670 //Assign # of times to re-read & Length
671 channel->table1.reread = rep1;
672 channel->table2.reread = rep2;
673 channel->table1.length = lent1;
674 channel->table2.length = lent2;
675 channel->table1.total_length = lent1*rep1;
676 channel->table2.total_length = lent2*rep2;
677
678 //Calculate Sample Volume
679 //- If Envelope should be used, Take the difference in new volume & current volume, and break it into the # of samples in the table
680 //- If No Envelope should be used, Volume is simply based on the Volume Table
681 #ifdef USE_VOL_ENVELOPE
682 if(channel->regs.env_enable) {
683 const int diff = channel->target_volume - channel->prev_volume;
684 channel->step_rate_volume_env = abs(diff) > 128 ? 1 : abs(diff) > 64 ? 2 : abs(diff) > 32 ? 4 : 8;
685 if(channel->target_volume < channel->prev_volume)
686 channel->step_rate_volume_env = -channel->step_rate_volume_env;
687 channel->current_volume = channel->prev_volume;
688 }
689 else
690 #endif
691 {
692 channel->current_volume = channel->target_volume;
693 }
694 //Update Last Volume
695 channel->prev_volume = channel->target_volume;
696
697 //Temp hack to ensure we only generate the ouput data 1x - this is WRONG and needs to be addressed eventually!
698 //if(channel->output[0] == 0)
699 read_table(chip,channel);
700
701 #if 0
702 //if(chip->channel == 2) {
703 if(channel->regs.outputs == 2) {
704 if(channel->regs.frequency == 0x70)
705 {
706 LOG(("orig: = %0f, freq = %0f, incr = %0d \n",freqtable4Mhz[channel->regs.frequency],freq,channel->incr));
707 }
708 //LOG(("EOT=%d\n",channel->end_of_table));
709 LOG(("C:%02d V:%02d FQ:%03x TS1:%02x TS2:%02x T1L:%04d T1R:%01d T2L:%04d T2R:%01d OD=%01d I:%02d E:%01d\n",
710 chip->channel,
711 channel->regs.atten,
712 channel->regs.frequency,
713 t1start,t2start,
714 lent1,rep1,
715 lent2,rep2,
716 channel->regs.oct_divisor,
717 channel->regs.interp,
718 channel->regs.env_enable
719 ));
720 }
721 #endif
722
723 }
724 }
725
726 /**********************************************************************************************
727
728 m114s_data_write -- handle a write to the data bus of the M114S
729
730 The chip has a data bus width of 6 bits, and must be fed 8 consecutive bytes
731 - thus 48 bits of programming! All data must be fed in order, so we make a few assumptions.
732
733 ***********************************************************************************************/
734 void m114s_data_write(struct M114SChip* chip, data8_t data)
735 {
736 /* Check if the chip needs to 'auto-reset' - this occurs if during the programming sequence (ie before all 8 bytes read)
737 a certain amount of time elapses without receiving another byte of programming...
738 128us in the 4Mhz chip, 85us in the 6Mhz chip.
739 */
740 static UINT64 last_totcyc = 0;
741 UINT64 curr_totcyc = cpu_gettotalcycles64(chip->cpu_num);
742 double diff = (double)llabs((INT64)(curr_totcyc-last_totcyc));
743 last_totcyc = curr_totcyc;
744 if(chip->bytes_read && diff > chip->reset_cycles) {
745 LOG(("M114S: Auto Reset - bytes read=%0d - data=%0x, elapsed cycles = %f\n",chip->bytes_read,data&0x3f,diff));
746 M114S_sh_reset();
747 }
748
749 data &= 0x3f; //Strip off bits 7-8 (only 6 bits for the data bus to the chip)
750 chip->bytes_read++;
751 switch(chip->bytes_read)
752 {
753 /* BYTE #1 -
754 Bits 0-5: Attenuation Value (0-63) - 0 = No Attenuation, 3E = Max, 3F = Silence active channel */
755 case 1:
756 chip->tempch_regs.atten = data;
757 break;
758
759 /* BYTE #2 -
760 Bits 0-1: Table 2 Address (Bits 6-7)
761 Bits 2-3: Table 1 Address (Bits 6-7)
762 Bits 4-5: Output Pin Selection (0-3) */
763 case 2:
764 chip->tempch_regs.table2_addr = (data & 0x03)<<6;
765 chip->tempch_regs.table1_addr = (data & 0x0c)<<4;
766 chip->tempch_regs.outputs = (data & 0x30)>>4;
767 break;
768
769 /* BYTE #3 -
770 Bits 0-5: Table 2 Address (Bits 0-5) */
771 case 3:
772 chip->tempch_regs.table2_addr |= data;
773 break;
774
775 /* BYTE #4 -
776 Bits 0-5: Table 1 Address (Bits 0-5) */
777 case 4:
778 chip->tempch_regs.table1_addr |= data;
779 break;
780
781 /* BYTE #5 -
782 Bits 0-2: Reading Method
783 Bits 3-5: Table Length */
784 case 5:
785 chip->tempch_regs.read_meth = data & 0x07;
786 chip->tempch_regs.table_len = (data & 0x38)>>3;
787 break;
788
789 /* BYTE #6 -
790 Bits 0 : Octave Divisor
791 Bits 1 : Envelope Enable/Disable
792 Bits 2-5: Interpolation Value (0-15) */
793 case 6:
794 chip->tempch_regs.oct_divisor = (data & 0x01);
795 chip->tempch_regs.env_enable = (data & 0x02);
796 chip->tempch_regs.interp = (data & 0x3c)>>2;
797 break;
798
799 /* BYTE #7 -
800 Bits 0-1: Frequency (Bits 0-1)
801 Bits 2-5: Channel */
802 case 7:
803 chip->tempch_regs.frequency = (data & 0x03);
804 chip->channel = (data & 0x3c)>>2;
805 break;
806
807 /* BYTE #8 -
808 Bits 0-5: Frequency (Bits 2-7) */
809 case 8:
810 chip->tempch_regs.frequency |= (data<<2);
811 /* Process the channel data */
812 process_channel_data(chip);
813 break;
814
815 default:
816 LOG(("M114S.C - logic error - too many bytes processed: %x\n",chip->bytes_read));
817 break;
818 }
819 }
820