openMSX
YMF262.cc
Go to the documentation of this file.
1 /*
2  *
3  * File: ymf262.c - software implementation of YMF262
4  * FM sound generator type OPL3
5  *
6  * Copyright (C) 2003 Jarek Burczynski
7  *
8  * Version 0.2
9  *
10  *
11  * Revision History:
12  *
13  * 03-03-2003: initial release
14  * - thanks to Olivier Galibert and Chris Hardy for YMF262 and YAC512 chips
15  * - thanks to Stiletto for the datasheets
16  *
17  *
18  *
19  * differences between OPL2 and OPL3 not documented in Yamaha datahasheets:
20  * - sinus table is a little different: the negative part is off by one...
21  *
22  * - in order to enable selection of four different waveforms on OPL2
23  * one must set bit 5 in register 0x01(test).
24  * on OPL3 this bit is ignored and 4-waveform select works *always*.
25  * (Don't confuse this with OPL3's 8-waveform select.)
26  *
27  * - Envelope Generator: all 15 x rates take zero time on OPL3
28  * (on OPL2 15 0 and 15 1 rates take some time while 15 2 and 15 3 rates
29  * take zero time)
30  *
31  * - channel calculations: output of operator 1 is in perfect sync with
32  * output of operator 2 on OPL3; on OPL and OPL2 output of operator 1
33  * is always delayed by one sample compared to output of operator 2
34  *
35  *
36  * differences between OPL2 and OPL3 shown in datasheets:
37  * - YMF262 does not support CSM mode
38  */
39 
40 #include "YMF262.hh"
41 #include "DeviceConfig.hh"
42 #include "MSXMotherBoard.hh"
43 #include "Math.hh"
44 #include "cstd.hh"
45 #include "outer.hh"
46 #include "serialize.hh"
47 #include <cmath>
48 #include <cstring>
49 #include <iostream>
50 
51 namespace openmsx {
52 
53 static inline YMF262::FreqIndex fnumToIncrement(unsigned block_fnum)
54 {
55  // opn phase increment counter = 20bit
56  // chip works with 10.10 fixed point, while we use 16.16
57  unsigned block = (block_fnum & 0x1C00) >> 10;
58  return YMF262::FreqIndex(block_fnum & 0x03FF) >> (11 - block);
59 }
60 
61 // envelope output entries
62 static constexpr int ENV_BITS = 10;
63 static constexpr int ENV_LEN = 1 << ENV_BITS;
64 static constexpr double ENV_STEP = 128.0 / ENV_LEN;
65 
66 static constexpr int MAX_ATT_INDEX = (1 << (ENV_BITS - 1)) - 1; // 511
67 static constexpr int MIN_ATT_INDEX = 0;
68 
69 // sinwave entries
70 static constexpr int SIN_BITS = 10;
71 static constexpr int SIN_LEN = 1 << SIN_BITS;
72 static constexpr int SIN_MASK = SIN_LEN - 1;
73 
74 static constexpr int TL_RES_LEN = 256; // 8 bits addressing (real chip)
75 
76 // register number to channel number , slot offset
77 static constexpr byte MOD = 0;
78 static constexpr byte CAR = 1;
79 
80 
81 // mapping of register number (offset) to slot number used by the emulator
82 static constexpr int slot_array[32] = {
83  0, 2, 4, 1, 3, 5, -1, -1,
84  6, 8, 10, 7, 9, 11, -1, -1,
85  12, 14, 16, 13, 15, 17, -1, -1,
86  -1, -1, -1, -1, -1, -1, -1, -1
87 };
88 
89 
90 // key scale level
91 // table is 3dB/octave , DV converts this into 6dB/octave
92 // 0.1875 is bit 0 weight of the envelope counter (volume) expressed
93 // in the 'decibel' scale
94 #define DV(x) int((x) / (0.1875 / 2.0))
95 static constexpr unsigned ksl_tab[8 * 16] = {
96  // OCT 0
97  DV( 0.000), DV( 0.000), DV( 0.000), DV( 0.000),
98  DV( 0.000), DV( 0.000), DV( 0.000), DV( 0.000),
99  DV( 0.000), DV( 0.000), DV( 0.000), DV( 0.000),
100  DV( 0.000), DV( 0.000), DV( 0.000), DV( 0.000),
101  // OCT 1
102  DV( 0.000), DV( 0.000), DV( 0.000), DV( 0.000),
103  DV( 0.000), DV( 0.000), DV( 0.000), DV( 0.000),
104  DV( 0.000), DV( 0.750), DV( 1.125), DV( 1.500),
105  DV( 1.875), DV( 2.250), DV( 2.625), DV( 3.000),
106  // OCT 2
107  DV( 0.000), DV( 0.000), DV( 0.000), DV( 0.000),
108  DV( 0.000), DV( 1.125), DV( 1.875), DV( 2.625),
109  DV( 3.000), DV( 3.750), DV( 4.125), DV( 4.500),
110  DV( 4.875), DV( 5.250), DV( 5.625), DV( 6.000),
111  // OCT 3
112  DV( 0.000), DV( 0.000), DV( 0.000), DV( 1.875),
113  DV( 3.000), DV( 4.125), DV( 4.875), DV( 5.625),
114  DV( 6.000), DV( 6.750), DV( 7.125), DV( 7.500),
115  DV( 7.875), DV( 8.250), DV( 8.625), DV( 9.000),
116  // OCT 4
117  DV( 0.000), DV( 0.000), DV( 3.000), DV( 4.875),
118  DV( 6.000), DV( 7.125), DV( 7.875), DV( 8.625),
119  DV( 9.000), DV( 9.750), DV(10.125), DV(10.500),
120  DV(10.875), DV(11.250), DV(11.625), DV(12.000),
121  // OCT 5
122  DV( 0.000), DV( 3.000), DV( 6.000), DV( 7.875),
123  DV( 9.000), DV(10.125), DV(10.875), DV(11.625),
124  DV(12.000), DV(12.750), DV(13.125), DV(13.500),
125  DV(13.875), DV(14.250), DV(14.625), DV(15.000),
126  // OCT 6
127  DV( 0.000), DV( 6.000), DV( 9.000), DV(10.875),
128  DV(12.000), DV(13.125), DV(13.875), DV(14.625),
129  DV(15.000), DV(15.750), DV(16.125), DV(16.500),
130  DV(16.875), DV(17.250), DV(17.625), DV(18.000),
131  // OCT 7
132  DV( 0.000), DV( 9.000), DV(12.000), DV(13.875),
133  DV(15.000), DV(16.125), DV(16.875), DV(17.625),
134  DV(18.000), DV(18.750), DV(19.125), DV(19.500),
135  DV(19.875), DV(20.250), DV(20.625), DV(21.000)
136 };
137 #undef DV
138 
139 // sustain level table (3dB per step)
140 // 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,93 (dB)
141 #define SC(db) unsigned((db) * (2.0 / ENV_STEP))
142 static constexpr unsigned sl_tab[16] = {
143  SC( 0), SC( 1), SC( 2), SC(3 ), SC(4 ), SC(5 ), SC(6 ), SC( 7),
144  SC( 8), SC( 9), SC(10), SC(11), SC(12), SC(13), SC(14), SC(31)
145 };
146 #undef SC
147 
148 
149 static constexpr byte RATE_STEPS = 8;
150 static constexpr byte eg_inc[15 * RATE_STEPS] = {
151 //cycle:0 1 2 3 4 5 6 7
152  0,1, 0,1, 0,1, 0,1, // 0 rates 00..12 0 (increment by 0 or 1)
153  0,1, 0,1, 1,1, 0,1, // 1 rates 00..12 1
154  0,1, 1,1, 0,1, 1,1, // 2 rates 00..12 2
155  0,1, 1,1, 1,1, 1,1, // 3 rates 00..12 3
156 
157  1,1, 1,1, 1,1, 1,1, // 4 rate 13 0 (increment by 1)
158  1,1, 1,2, 1,1, 1,2, // 5 rate 13 1
159  1,2, 1,2, 1,2, 1,2, // 6 rate 13 2
160  1,2, 2,2, 1,2, 2,2, // 7 rate 13 3
161 
162  2,2, 2,2, 2,2, 2,2, // 8 rate 14 0 (increment by 2)
163  2,2, 2,4, 2,2, 2,4, // 9 rate 14 1
164  2,4, 2,4, 2,4, 2,4, // 10 rate 14 2
165  2,4, 4,4, 2,4, 4,4, // 11 rate 14 3
166 
167  4,4, 4,4, 4,4, 4,4, // 12 rates 15 0, 15 1, 15 2, 15 3 for decay
168  8,8, 8,8, 8,8, 8,8, // 13 rates 15 0, 15 1, 15 2, 15 3 for attack (zero time)
169  0,0, 0,0, 0,0, 0,0, // 14 infinity rates for attack and decay(s)
170 };
171 
172 
173 #define O(a) ((a) * RATE_STEPS)
174 // note that there is no O(13) in this table - it's directly in the code
175 static constexpr byte eg_rate_select[16 + 64 + 16] = {
176  // Envelope Generator rates (16 + 64 rates + 16 RKS)
177  // 16 infinite time rates
178  O(14), O(14), O(14), O(14), O(14), O(14), O(14), O(14),
179  O(14), O(14), O(14), O(14), O(14), O(14), O(14), O(14),
180 
181  // rates 00-12
182  O( 0), O( 1), O( 2), O( 3),
183  O( 0), O( 1), O( 2), O( 3),
184  O( 0), O( 1), O( 2), O( 3),
185  O( 0), O( 1), O( 2), O( 3),
186  O( 0), O( 1), O( 2), O( 3),
187  O( 0), O( 1), O( 2), O( 3),
188  O( 0), O( 1), O( 2), O( 3),
189  O( 0), O( 1), O( 2), O( 3),
190  O( 0), O( 1), O( 2), O( 3),
191  O( 0), O( 1), O( 2), O( 3),
192  O( 0), O( 1), O( 2), O( 3),
193  O( 0), O( 1), O( 2), O( 3),
194  O( 0), O( 1), O( 2), O( 3),
195 
196  // rate 13
197  O( 4), O( 5), O( 6), O( 7),
198 
199  // rate 14
200  O( 8), O( 9), O(10), O(11),
201 
202  // rate 15
203  O(12), O(12), O(12), O(12),
204 
205  // 16 dummy rates (same as 15 3)
206  O(12), O(12), O(12), O(12), O(12), O(12), O(12), O(12),
207  O(12), O(12), O(12), O(12), O(12), O(12), O(12), O(12),
208 };
209 #undef O
210 
211 // rate 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
212 // shift 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0
213 // mask 4095, 2047, 1023, 511, 255, 127, 63, 31, 15, 7, 3, 1, 0, 0, 0, 0
214 #define O(a) ((a) * 1)
215 static constexpr byte eg_rate_shift[16 + 64 + 16] =
216 {
217  // Envelope Generator counter shifts (16 + 64 rates + 16 RKS)
218  // 16 infinite time rates
219  O( 0), O( 0), O( 0), O( 0), O( 0), O( 0), O( 0), O( 0),
220  O( 0), O( 0), O( 0), O( 0), O( 0), O( 0), O( 0), O( 0),
221 
222  // rates 00-15
223  O(12), O(12), O(12), O(12),
224  O(11), O(11), O(11), O(11),
225  O(10), O(10), O(10), O(10),
226  O( 9), O( 9), O( 9), O( 9),
227  O( 8), O( 8), O( 8), O( 8),
228  O( 7), O( 7), O( 7), O( 7),
229  O( 6), O( 6), O( 6), O( 6),
230  O( 5), O( 5), O( 5), O( 5),
231  O( 4), O( 4), O( 4), O( 4),
232  O( 3), O( 3), O( 3), O( 3),
233  O( 2), O( 2), O( 2), O( 2),
234  O( 1), O( 1), O( 1), O( 1),
235  O( 0), O( 0), O( 0), O( 0),
236  O( 0), O( 0), O( 0), O( 0),
237  O( 0), O( 0), O( 0), O( 0),
238  O( 0), O( 0), O( 0), O( 0),
239 
240  // 16 dummy rates (same as 15 3)
241  O( 0), O( 0), O( 0), O( 0), O( 0), O( 0), O( 0), O( 0),
242  O( 0), O( 0), O( 0), O( 0), O( 0), O( 0), O( 0), O( 0),
243 };
244 #undef O
245 
246 
247 // multiple table
248 #define ML(x) byte(2 * (x))
249 static constexpr byte mul_tab[16] = {
250  // 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,10,12,12,15,15
251  ML( 0.5), ML( 1.0), ML( 2.0), ML( 3.0),
252  ML( 4.0), ML( 5.0), ML( 6.0), ML( 7.0),
253  ML( 8.0), ML( 9.0), ML(10.0), ML(10.0),
254  ML(12.0), ML(12.0), ML(15.0), ML(15.0)
255 };
256 #undef ML
257 
258 // LFO Amplitude Modulation table (verified on real YM3812)
259 // 27 output levels (triangle waveform); 1 level takes one of: 192, 256 or 448 samples
260 //
261 // Length: 210 elements
262 //
263 // Each of the elements has to be repeated
264 // exactly 64 times (on 64 consecutive samples).
265 // The whole table takes: 64 * 210 = 13440 samples.
266 //
267 // When AM = 1 data is used directly
268 // When AM = 0 data is divided by 4 before being used (loosing precision is important)
269 
270 static constexpr unsigned LFO_AM_TAB_ELEMENTS = 210;
271 static constexpr byte lfo_am_table[LFO_AM_TAB_ELEMENTS] = {
272  0, 0, 0,
273  0, 0, 0, 0,
274  1, 1, 1, 1,
275  2, 2, 2, 2,
276  3, 3, 3, 3,
277  4, 4, 4, 4,
278  5, 5, 5, 5,
279  6, 6, 6, 6,
280  7, 7, 7, 7,
281  8, 8, 8, 8,
282  9, 9, 9, 9,
283  10, 10, 10, 10,
284  11, 11, 11, 11,
285  12, 12, 12, 12,
286  13, 13, 13, 13,
287  14, 14, 14, 14,
288  15, 15, 15, 15,
289  16, 16, 16, 16,
290  17, 17, 17, 17,
291  18, 18, 18, 18,
292  19, 19, 19, 19,
293  20, 20, 20, 20,
294  21, 21, 21, 21,
295  22, 22, 22, 22,
296  23, 23, 23, 23,
297  24, 24, 24, 24,
298  25, 25, 25, 25,
299  26, 26, 26,
300  25, 25, 25, 25,
301  24, 24, 24, 24,
302  23, 23, 23, 23,
303  22, 22, 22, 22,
304  21, 21, 21, 21,
305  20, 20, 20, 20,
306  19, 19, 19, 19,
307  18, 18, 18, 18,
308  17, 17, 17, 17,
309  16, 16, 16, 16,
310  15, 15, 15, 15,
311  14, 14, 14, 14,
312  13, 13, 13, 13,
313  12, 12, 12, 12,
314  11, 11, 11, 11,
315  10, 10, 10, 10,
316  9, 9, 9, 9,
317  8, 8, 8, 8,
318  7, 7, 7, 7,
319  6, 6, 6, 6,
320  5, 5, 5, 5,
321  4, 4, 4, 4,
322  3, 3, 3, 3,
323  2, 2, 2, 2,
324  1, 1, 1, 1
325 };
326 
327 // LFO Phase Modulation table (verified on real YM3812)
328 static constexpr signed char lfo_pm_table[8 * 8 * 2] = {
329  // FNUM2/FNUM = 00 0xxxxxxx (0x0000)
330  0, 0, 0, 0, 0, 0, 0, 0, // LFO PM depth = 0
331  0, 0, 0, 0, 0, 0, 0, 0, // LFO PM depth = 1
332 
333  // FNUM2/FNUM = 00 1xxxxxxx (0x0080)
334  0, 0, 0, 0, 0, 0, 0, 0, // LFO PM depth = 0
335  1, 0, 0, 0,-1, 0, 0, 0, // LFO PM depth = 1
336 
337  // FNUM2/FNUM = 01 0xxxxxxx (0x0100)
338  1, 0, 0, 0,-1, 0, 0, 0, // LFO PM depth = 0
339  2, 1, 0,-1,-2,-1, 0, 1, // LFO PM depth = 1
340 
341  // FNUM2/FNUM = 01 1xxxxxxx (0x0180)
342  1, 0, 0, 0,-1, 0, 0, 0, // LFO PM depth = 0
343  3, 1, 0,-1,-3,-1, 0, 1, // LFO PM depth = 1
344 
345  // FNUM2/FNUM = 10 0xxxxxxx (0x0200)
346  2, 1, 0,-1,-2,-1, 0, 1, // LFO PM depth = 0
347  4, 2, 0,-2,-4,-2, 0, 2, // LFO PM depth = 1
348 
349  // FNUM2/FNUM = 10 1xxxxxxx (0x0280)
350  2, 1, 0,-1,-2,-1, 0, 1, // LFO PM depth = 0
351  5, 2, 0,-2,-5,-2, 0, 2, // LFO PM depth = 1
352 
353  // FNUM2/FNUM = 11 0xxxxxxx (0x0300)
354  3, 1, 0,-1,-3,-1, 0, 1, // LFO PM depth = 0
355  6, 3, 0,-3,-6,-3, 0, 3, // LFO PM depth = 1
356 
357  // FNUM2/FNUM = 11 1xxxxxxx (0x0380)
358  3, 1, 0,-1,-3,-1, 0, 1, // LFO PM depth = 0
359  7, 3, 0,-3,-7,-3, 0, 3 // LFO PM depth = 1
360 };
361 
362 // TL_TAB_LEN is calculated as:
363 // (12+1)=13 - sinus amplitude bits (Y axis)
364 // additional 1: to compensate for calculations of negative part of waveform
365 // (if we don't add it then the greatest possible _negative_ value would be -2
366 // and we really need -1 for waveform #7)
367 // 2 - sinus sign bit (Y axis)
368 // TL_RES_LEN - sinus resolution (X axis)
369 static constexpr int TL_TAB_LEN = 13 * 2 * TL_RES_LEN;
370 static constexpr int ENV_QUIET = TL_TAB_LEN >> 4;
371 
372 struct TlTab {
373  int tab[TL_TAB_LEN];
374 };
375 
376 static constexpr TlTab getTlTab()
377 {
378  TlTab t = {};
379  // this _is_ different from OPL2 (verified on real YMF262)
380  for (int x = 0; x < TL_RES_LEN; x++) {
381  double m = (1 << 16) / cstd::exp2<6>((x + 1) * (ENV_STEP / 4.0) / 8.0);
382 
383  // we never reach (1<<16) here due to the (x+1)
384  // result fits within 16 bits at maximum
385  int n = int(m); // 16 bits here
386  n >>= 4; // 12 bits here
387  n = (n >> 1) + (n & 1); // round to nearest
388  // 11 bits here (rounded)
389  n <<= 1; // 12 bits here (as in real chip)
390  t.tab[x * 2 + 0] = n;
391  t.tab[x * 2 + 1] = ~t.tab[x * 2 + 0];
392 
393  for (int i = 1; i < 13; i++) {
394  t.tab[x * 2 + 0 + i * 2 * TL_RES_LEN] =
395  t.tab[x * 2 + 0] >> i;
396  t.tab[x * 2 + 1 + i * 2 * TL_RES_LEN] =
397  ~t.tab[x * 2 + 0 + i * 2 * TL_RES_LEN];
398  }
399  }
400  return t;
401 }
402 
403 static constexpr TlTab tl = getTlTab();
404 
405 
406 // sin waveform table in 'decibel' scale
407 // there are eight waveforms on OPL3 chips
408 struct SinTab {
409  unsigned tab[SIN_LEN * 8];
410 };
411 
412 static constexpr SinTab getSinTab()
413 {
414  SinTab sin = {};
415 
416  for (int i = 0; i < SIN_LEN / 4; i++) {
417  // non-standard sinus
418  double m = cstd::sin<2>(((i * 2) + 1) * M_PI / SIN_LEN); // checked against the real chip
419  // we never reach zero here due to ((i * 2) + 1)
420  double o = -8.0 * cstd::log2<11, 3>(m); // convert to 'decibels'
421  o = o / (double(ENV_STEP) / 4);
422 
423  int n = int(2 * o);
424  n = (n >> 1) + (n & 1); // round to nearest
425  sin.tab[i] = 2 * n;
426  }
427  for (int i = 0; i < SIN_LEN / 4; i++) {
428  sin.tab[SIN_LEN / 2 - 1 - i] = sin.tab[i];
429  }
430  for (int i = 0; i < SIN_LEN / 2; i++) {
431  sin.tab[SIN_LEN / 2 + i] = sin.tab[i] + 1;
432  }
433 
434  for (int i = 0; i < SIN_LEN; ++i) {
435  // these 'pictures' represent _two_ cycles
436  // waveform 1: __ __
437  // / \____/ \____
438  // output only first half of the sinus waveform (positive one)
439  sin.tab[1 * SIN_LEN + i] = (i & (1 << (SIN_BITS - 1)))
440  ? TL_TAB_LEN
441  : sin.tab[i];
442 
443  // waveform 2: __ __ __ __
444  // / \/ \/ \/ \.
445  // abs(sin)
446  sin.tab[2 * SIN_LEN + i] = sin.tab[i & (SIN_MASK >> 1)];
447 
448  // waveform 3: _ _ _ _
449  // / |_/ |_/ |_/ |_
450  // abs(output only first quarter of the sinus waveform)
451  sin.tab[3 * SIN_LEN + i] = (i & (1 << (SIN_BITS - 2)))
452  ? TL_TAB_LEN
453  : sin.tab[i & (SIN_MASK>>2)];
454 
455  // waveform 4: /\ ____/\ ____
456  // \/ \/
457  // output whole sinus waveform in half the cycle(step=2)
458  // and output 0 on the other half of cycle
459  sin.tab[4 * SIN_LEN + i] = (i & (1 << (SIN_BITS - 1)))
460  ? TL_TAB_LEN
461  : sin.tab[i * 2];
462 
463  // waveform 5: /\/\____/\/\____
464  //
465  // output abs(whole sinus) waveform in half the cycle(step=2)
466  // and output 0 on the other half of cycle
467  sin.tab[5 * SIN_LEN + i] = (i & (1 << (SIN_BITS - 1)))
468  ? TL_TAB_LEN
469  : sin.tab[(i * 2) & (SIN_MASK >> 1)];
470 
471  // waveform 6: ____ ____
472  // ____ ____
473  // output maximum in half the cycle and output minimum
474  // on the other half of cycle
475  sin.tab[6 * SIN_LEN + i] = (i & (1 << (SIN_BITS - 1)))
476  ? 1 // negative
477  : 0; // positive
478 
479  // waveform 7:|\____ |\____
480  // \| \|
481  // output sawtooth waveform
482  int x = (i & (1 << (SIN_BITS - 1)))
483  ? ((SIN_LEN - 1) - i) * 16 + 1 // negative: from 8177 to 1
484  : i * 16; // positive: from 0 to 8176
485  x = std::min(x, TL_TAB_LEN); // clip to the allowed range
486  sin.tab[7 * SIN_LEN + i] = x;
487  }
488 
489  return sin;
490 }
491 
492 static constexpr SinTab sin = getSinTab();
493 
494 
495 // TODO clean this up
496 static int phase_modulation; // phase modulation input (SLOT 2)
497 static int phase_modulation2; // phase modulation input (SLOT 3
498  // in 4 operator channels)
499 
500 
501 YMF262::Slot::Slot()
502  : Cnt(0), Incr(0)
503 {
504  ar = dr = rr = KSR = ksl = ksr = mul = 0;
505  fb_shift = op1_out[0] = op1_out[1] = 0;
506  CON = eg_type = vib = false;
507  connect = nullptr;
508  TL = TLL = volume = sl = 0;
509  state = EG_OFF;
510  eg_m_ar = eg_sh_ar = eg_sel_ar = eg_m_dr = eg_sh_dr = 0;
511  eg_sel_dr = eg_m_rr = eg_sh_rr = eg_sel_rr = 0;
512  key = AMmask = 0;
513  wavetable = &sin.tab[0 * SIN_LEN];
514 }
515 
516 YMF262::Channel::Channel()
517 {
518  block_fnum = ksl_base = kcode = 0;
519  extended = false;
520  fc = FreqIndex(0);
521 }
522 
523 
524 void YMF262::callback(byte flag)
525 {
526  setStatus(flag);
527 }
528 
529 // status set and IRQ handling
530 void YMF262::setStatus(byte flag)
531 {
532  // set status flag masking out disabled IRQs
533  status |= flag;
534  if (status & statusMask) {
535  status |= 0x80;
536  irq.set();
537  }
538 }
539 
540 // status reset and IRQ handling
541 void YMF262::resetStatus(byte flag)
542 {
543  // reset status flag
544  status &= ~flag;
545  if (!(status & statusMask)) {
546  status &= 0x7F;
547  irq.reset();
548  }
549 }
550 
551 // IRQ mask set
552 void YMF262::changeStatusMask(byte flag)
553 {
554  statusMask = flag;
555  status &= statusMask;
556  if (status) {
557  status |= 0x80;
558  irq.set();
559  } else {
560  status &= 0x7F;
561  irq.reset();
562  }
563 }
564 
565 
566 void YMF262::Slot::advanceEnvelopeGenerator(unsigned egCnt)
567 {
568  switch (state) {
569  case EG_ATTACK:
570  if (!(egCnt & eg_m_ar)) {
571  volume += (~volume * eg_inc[eg_sel_ar + ((egCnt >> eg_sh_ar) & 7)]) >> 3;
572  if (volume <= MIN_ATT_INDEX) {
573  volume = MIN_ATT_INDEX;
574  state = EG_DECAY;
575  }
576  }
577  break;
578 
579  case EG_DECAY:
580  if (!(egCnt & eg_m_dr)) {
581  volume += eg_inc[eg_sel_dr + ((egCnt >> eg_sh_dr) & 7)];
582  if (volume >= sl) {
583  state = EG_SUSTAIN;
584  }
585  }
586  break;
587 
588  case EG_SUSTAIN:
589  // this is important behaviour:
590  // one can change percusive/non-percussive
591  // modes on the fly and the chip will remain
592  // in sustain phase - verified on real YM3812
593  if (eg_type) {
594  // non-percussive mode
595  // do nothing
596  } else {
597  // percussive mode
598  // during sustain phase chip adds Release Rate (in percussive mode)
599  if (!(egCnt & eg_m_rr)) {
600  volume += eg_inc[eg_sel_rr + ((egCnt >> eg_sh_rr) & 7)];
601  if (volume >= MAX_ATT_INDEX) {
602  volume = MAX_ATT_INDEX;
603  }
604  } else {
605  // do nothing in sustain phase
606  }
607  }
608  break;
609 
610  case EG_RELEASE:
611  if (!(egCnt & eg_m_rr)) {
612  volume += eg_inc[eg_sel_rr + ((egCnt >> eg_sh_rr) & 7)];
613  if (volume >= MAX_ATT_INDEX) {
614  volume = MAX_ATT_INDEX;
615  state = EG_OFF;
616  }
617  }
618  break;
619 
620  default:
621  break;
622  }
623 }
624 
625 void YMF262::Slot::advancePhaseGenerator(Channel& ch, unsigned lfo_pm)
626 {
627  if (vib) {
628  // LFO phase modulation active
629  unsigned block_fnum = ch.block_fnum;
630  unsigned fnum_lfo = (block_fnum & 0x0380) >> 7;
631  int lfo_fn_table_index_offset = lfo_pm_table[lfo_pm + 16 * fnum_lfo];
632  Cnt += fnumToIncrement(block_fnum + lfo_fn_table_index_offset) * mul;
633  } else {
634  // LFO phase modulation disabled for this operator
635  Cnt += Incr;
636  }
637 }
638 
639 // advance to next sample
640 void YMF262::advance()
641 {
642  // Vibrato: 8 output levels (triangle waveform);
643  // 1 level takes 1024 samples
644  lfo_pm_cnt.addQuantum();
645  unsigned lfo_pm = (lfo_pm_cnt.toInt() & 7) | lfo_pm_depth_range;
646 
647  ++eg_cnt;
648  for (auto& ch : channel) {
649  for (auto& op : ch.slot) {
650  op.advanceEnvelopeGenerator(eg_cnt);
651  op.advancePhaseGenerator(ch, lfo_pm);
652  }
653  }
654 
655  // The Noise Generator of the YM3812 is 23-bit shift register.
656  // Period is equal to 2^23-2 samples.
657  // Register works at sampling frequency of the chip, so output
658  // can change on every sample.
659  //
660  // Output of the register and input to the bit 22 is:
661  // bit0 XOR bit14 XOR bit15 XOR bit22
662  //
663  // Simply use bit 22 as the noise output.
664  //
665  // unsigned j = ((noise_rng >> 0) ^ (noise_rng >> 14) ^
666  // (noise_rng >> 15) ^ (noise_rng >> 22)) & 1;
667  // noise_rng = (j << 22) | (noise_rng >> 1);
668  //
669  // Instead of doing all the logic operations above, we
670  // use a trick here (and use bit 0 as the noise output).
671  // The difference is only that the noise bit changes one
672  // step ahead. This doesn't matter since we don't know
673  // what is real state of the noise_rng after the reset.
674  if (noise_rng & 1) {
675  noise_rng ^= 0x800302;
676  }
677  noise_rng >>= 1;
678 }
679 
680 inline int YMF262::Slot::op_calc(unsigned phase, unsigned lfo_am) const
681 {
682  unsigned env = (TLL + volume + (lfo_am & AMmask)) << 4;
683  int p = env + wavetable[phase & SIN_MASK];
684  return (p < TL_TAB_LEN) ? tl.tab[p] : 0;
685 }
686 
687 // calculate output of a standard 2 operator channel
688 // (or 1st part of a 4-op channel)
689 void YMF262::Channel::chan_calc(unsigned lfo_am)
690 {
691  // !! something is wrong with this, it caused bug
692  // !! [2823673] moonsound 4 operator FM fail
693  // !! optimization disabled for now
694  // !! TODO investigate
695  // !! maybe this micro optimization isn't worth the trouble/risk
696  // !!
697  // - mod.connect can point to 'phase_modulation' or 'ch0-output'
698  // - car.connect can point to 'phase_modulation2' or 'ch0-output'
699  // (see register #C0-#C8 writes)
700  // - phase_modulation2 is only used in 4op mode
701  // - mod.connect and car.connect can point to the same thing, so we need
702  // an addition for car.connect (and initialize phase_modulation2 to
703  // zero). For mod.connect we can directly assign the value.
704 
705  // ?? is this paragraph correct ??
706  // phase_modulation should be initialized to zero here. But there seems
707  // to be an optimization bug in gcc-4.2: it *seems* that when we
708  // initialize phase_modulation to zero in this function, the optimizer
709  // assumes it still has value zero at the end of this function (where
710  // it's used to calculate car.connect). As a workaround we initialize
711  // phase_modulation each time before calling this function.
712  phase_modulation = 0;
713  phase_modulation2 = 0;
714 
715  auto& mod = slot[MOD];
716  int out = mod.fb_shift
717  ? mod.op1_out[0] + mod.op1_out[1]
718  : 0;
719  mod.op1_out[0] = mod.op1_out[1];
720  mod.op1_out[1] = mod.op_calc(mod.Cnt.toInt() + (out >> mod.fb_shift), lfo_am);
721  *mod.connect += mod.op1_out[1];
722 
723  auto& car = slot[CAR];
724  *car.connect += car.op_calc(car.Cnt.toInt() + phase_modulation, lfo_am);
725 }
726 
727 // calculate output of a 2nd part of 4-op channel
728 void YMF262::Channel::chan_calc_ext(unsigned lfo_am)
729 {
730  // !! see remark in chan_cal(), something is wrong with this
731  // !! optimization disabled for now
732  // !!
733  // - mod.connect can point to 'phase_modulation' or 'ch3-output'
734  // - car.connect always points to 'ch3-output' (always 4op-mode)
735  // (see register #C0-#C8 writes)
736  // - mod.connect and car.connect can point to the same thing, so we need
737  // an addition for car.connect. For mod.connect we can directly assign
738  // the value.
739 
740  phase_modulation = 0;
741 
742  auto& mod = slot[MOD];
743  *mod.connect += mod.op_calc(mod.Cnt.toInt() + phase_modulation2, lfo_am);
744 
745  auto& car = slot[CAR];
746  *car.connect += car.op_calc(car.Cnt.toInt() + phase_modulation, lfo_am);
747 }
748 
749 // operators used in the rhythm sounds generation process:
750 //
751 // Envelope Generator:
752 //
753 // channel operator register number Bass High Snare Tom Top
754 // / slot number TL ARDR SLRR Wave Drum Hat Drum Tom Cymbal
755 // 6 / 0 12 50 70 90 f0 +
756 // 6 / 1 15 53 73 93 f3 +
757 // 7 / 0 13 51 71 91 f1 +
758 // 7 / 1 16 54 74 94 f4 +
759 // 8 / 0 14 52 72 92 f2 +
760 // 8 / 1 17 55 75 95 f5 +
761 //
762 // Phase Generator:
763 //
764 // channel operator register number Bass High Snare Tom Top
765 // / slot number MULTIPLE Drum Hat Drum Tom Cymbal
766 // 6 / 0 12 30 +
767 // 6 / 1 15 33 +
768 // 7 / 0 13 31 + + +
769 // 7 / 1 16 34 ----- n o t u s e d -----
770 // 8 / 0 14 32 +
771 // 8 / 1 17 35 + +
772 //
773 // channel operator register number Bass High Snare Tom Top
774 // number number BLK/FNUM2 FNUM Drum Hat Drum Tom Cymbal
775 // 6 12,15 B6 A6 +
776 //
777 // 7 13,16 B7 A7 + + +
778 //
779 // 8 14,17 B8 A8 + + +
780 
781 // The following formulas can be well optimized.
782 // I leave them in direct form for now (in case I've missed something).
783 
784 inline int YMF262::genPhaseHighHat()
785 {
786  // high hat phase generation (verified on real YM3812):
787  // phase = d0 or 234 (based on frequency only)
788  // phase = 34 or 2d0 (based on noise)
789 
790  // base frequency derived from operator 1 in channel 7
791  int op71phase = channel[7].slot[MOD].Cnt.toInt();
792  bool bit7 = (op71phase & 0x80) != 0;
793  bool bit3 = (op71phase & 0x08) != 0;
794  bool bit2 = (op71phase & 0x04) != 0;
795  bool res1 = (bit2 ^ bit7) | bit3;
796  // when res1 = 0 phase = 0x000 | 0xd0;
797  // when res1 = 1 phase = 0x200 | (0xd0>>2);
798  unsigned phase = res1 ? (0x200 | (0xd0 >> 2)) : 0xd0;
799 
800  // enable gate based on frequency of operator 2 in channel 8
801  int op82phase = channel[8].slot[CAR].Cnt.toInt();
802  bool bit5e= (op82phase & 0x20) != 0;
803  bool bit3e= (op82phase & 0x08) != 0;
804  bool res2 = (bit3e ^ bit5e);
805  // when res2 = 0 pass the phase from calculation above (res1);
806  // when res2 = 1 phase = 0x200 | (0xd0>>2);
807  if (res2) {
808  phase = (0x200 | (0xd0 >> 2));
809  }
810 
811  // when phase & 0x200 is set and noise=1 then phase = 0x200|0xd0
812  // when phase & 0x200 is set and noise=0 then phase = 0x200|(0xd0>>2), ie no change
813  if (phase & 0x200) {
814  if (noise_rng & 1) {
815  phase = 0x200 | 0xd0;
816  }
817  } else {
818  // when phase & 0x200 is clear and noise=1 then phase = 0xd0>>2
819  // when phase & 0x200 is clear and noise=0 then phase = 0xd0, ie no change
820  if (noise_rng & 1) {
821  phase = 0xd0 >> 2;
822  }
823  }
824  return phase;
825 }
826 
827 inline int YMF262::genPhaseSnare()
828 {
829  // verified on real YM3812
830  // base frequency derived from operator 1 in channel 7
831  // noise bit XOR'es phase by 0x100
832  return ((channel[7].slot[MOD].Cnt.toInt() & 0x100) + 0x100)
833  ^ ((noise_rng & 1) << 8);
834 }
835 
836 inline int YMF262::genPhaseCymbal()
837 {
838  // verified on real YM3812
839  // enable gate based on frequency of operator 2 in channel 8
840  // NOTE: YM2413_2 uses bit5 | bit3, this core uses bit5 ^ bit3
841  // most likely only one of the two is correct
842  int op82phase = channel[8].slot[CAR].Cnt.toInt();
843  if ((op82phase ^ (op82phase << 2)) & 0x20) { // bit5 ^ bit3
844  return 0x300;
845  } else {
846  // base frequency derived from operator 1 in channel 7
847  int op71phase = channel[7].slot[MOD].Cnt.toInt();
848  bool bit7 = (op71phase & 0x80) != 0;
849  bool bit3 = (op71phase & 0x08) != 0;
850  bool bit2 = (op71phase & 0x04) != 0;
851  return ((bit2 != bit7) || bit3) ? 0x300 : 0x100;
852  }
853 }
854 
855 // calculate rhythm
856 void YMF262::chan_calc_rhythm(unsigned lfo_am)
857 {
858  // Bass Drum (verified on real YM3812):
859  // - depends on the channel 6 'connect' register:
860  // when connect = 0 it works the same as in normal (non-rhythm)
861  // mode (op1->op2->out)
862  // when connect = 1 _only_ operator 2 is present on output
863  // (op2->out), operator 1 is ignored
864  // - output sample always is multiplied by 2
865  auto& mod6 = channel[6].slot[MOD];
866  int out = mod6.fb_shift ? mod6.op1_out[0] + mod6.op1_out[1] : 0;
867  mod6.op1_out[0] = mod6.op1_out[1];
868  int pm = mod6.CON ? 0 : mod6.op1_out[0];
869  mod6.op1_out[1] = mod6.op_calc(mod6.Cnt.toInt() + (out >> mod6.fb_shift), lfo_am);
870  auto& car6 = channel[6].slot[CAR];
871  chanout[6] += 2 * car6.op_calc(car6.Cnt.toInt() + pm, lfo_am);
872 
873  // Phase generation is based on:
874  // HH (13) channel 7->slot 1 combined with channel 8->slot 2
875  // (same combination as TOP CYMBAL but different output phases)
876  // SD (16) channel 7->slot 1
877  // TOM (14) channel 8->slot 1
878  // TOP (17) channel 7->slot 1 combined with channel 8->slot 2
879  // (same combination as HIGH HAT but different output phases)
880  //
881  // Envelope generation based on:
882  // HH channel 7->slot1
883  // SD channel 7->slot2
884  // TOM channel 8->slot1
885  // TOP channel 8->slot2
886  auto& mod7 = channel[7].slot[MOD];
887  chanout[7] += 2 * mod7.op_calc(genPhaseHighHat(), lfo_am);
888  auto& car7 = channel[7].slot[CAR];
889  chanout[7] += 2 * car7.op_calc(genPhaseSnare(), lfo_am);
890  auto& mod8 = channel[8].slot[MOD];
891  chanout[8] += 2 * mod8.op_calc(mod8.Cnt.toInt(), lfo_am);
892  auto& car8 = channel[8].slot[CAR];
893  chanout[8] += 2 * car8.op_calc(genPhaseCymbal(), lfo_am);
894 }
895 
896 void YMF262::Slot::FM_KEYON(byte key_set)
897 {
898  if (!key) {
899  // restart Phase Generator
900  Cnt = FreqIndex(0);
901  // phase -> Attack
902  state = EG_ATTACK;
903  }
904  key |= key_set;
905 }
906 
907 void YMF262::Slot::FM_KEYOFF(byte key_clr)
908 {
909  if (key) {
910  key &= ~key_clr;
911  if (!key) {
912  // phase -> Release
913  if (state != EG_OFF) {
914  state = EG_RELEASE;
915  }
916  }
917  }
918 }
919 
920 void YMF262::Slot::update_ar_dr()
921 {
922  if ((ar + ksr) < 16 + 60) {
923  // verified on real YMF262 - all 15 x rates take "zero" time
924  eg_sh_ar = eg_rate_shift [ar + ksr];
925  eg_sel_ar = eg_rate_select[ar + ksr];
926  } else {
927  eg_sh_ar = 0;
928  eg_sel_ar = 13 * RATE_STEPS;
929  }
930  eg_m_ar = (1 << eg_sh_ar) - 1;
931  eg_sh_dr = eg_rate_shift [dr + ksr];
932  eg_sel_dr = eg_rate_select[dr + ksr];
933  eg_m_dr = (1 << eg_sh_dr) - 1;
934 }
935 void YMF262::Slot::update_rr()
936 {
937  eg_sh_rr = eg_rate_shift [rr + ksr];
938  eg_sel_rr = eg_rate_select[rr + ksr];
939  eg_m_rr = (1 << eg_sh_rr) - 1;
940 }
941 
942 // update phase increment counter of operator (also update the EG rates if necessary)
943 void YMF262::Slot::calc_fc(const Channel& ch)
944 {
945  // (frequency) phase increment counter
946  Incr = ch.fc * mul;
947 
948  int newKsr = ch.kcode >> KSR;
949  if (ksr == newKsr) return;
950  ksr = newKsr;
951 
952  // calculate envelope generator rates
953  update_ar_dr();
954  update_rr();
955 }
956 
957 static const unsigned channelPairTab[18] = {
958  0, 1, 2, 0, 1, 2, unsigned(~0), unsigned(~0), unsigned(~0),
959  9, 10, 11, 9, 10, 11, unsigned(~0), unsigned(~0), unsigned(~0),
960 };
961 inline bool YMF262::isExtended(unsigned ch) const
962 {
963  assert(ch < 18);
964  if (!OPL3_mode) return false;
965  if (channelPairTab[ch] == unsigned(~0)) return false;
966  return channel[channelPairTab[ch]].extended;
967 }
968 static inline unsigned getFirstOfPairNum(unsigned ch)
969 {
970  assert((ch < 18) && (channelPairTab[ch] != unsigned(~0)));
971  return channelPairTab[ch];
972 }
973 inline YMF262::Channel& YMF262::getFirstOfPair(unsigned ch)
974 {
975  return channel[getFirstOfPairNum(ch) + 0];
976 }
977 inline YMF262::Channel& YMF262::getSecondOfPair(unsigned ch)
978 {
979  return channel[getFirstOfPairNum(ch) + 3];
980 }
981 
982 // set multi,am,vib,EG-TYP,KSR,mul
983 void YMF262::set_mul(unsigned sl, byte v)
984 {
985  unsigned chan_no = sl / 2;
986  auto& ch = channel[chan_no];
987  auto& slot = ch.slot[sl & 1];
988 
989  slot.mul = mul_tab[v & 0x0f];
990  slot.KSR = (v & 0x10) ? 0 : 2;
991  slot.eg_type = (v & 0x20) != 0;
992  slot.vib = (v & 0x40) != 0;
993  slot.AMmask = (v & 0x80) ? ~0 : 0;
994 
995  if (isExtended(chan_no)) {
996  // 4op mode
997  // update this slot using frequency data for 1st channel of a pair
998  slot.calc_fc(getFirstOfPair(chan_no));
999  } else {
1000  // normal (OPL2 mode or 2op mode)
1001  slot.calc_fc(ch);
1002  }
1003 }
1004 
1005 // set ksl & tl
1006 void YMF262::set_ksl_tl(unsigned sl, byte v)
1007 {
1008  unsigned chan_no = sl / 2;
1009  auto& ch = channel[chan_no];
1010  auto& slot = ch.slot[sl & 1];
1011 
1012  // This is indeed {0.0, 3.0, 1.5, 6.0} dB/oct, verified on real YMF262.
1013  // Note the illogical order of 2nd and 3rd element.
1014  static const unsigned ksl_shift[4] = { 31, 1, 2, 0 };
1015  slot.ksl = ksl_shift[v >> 6];
1016 
1017  slot.TL = (v & 0x3F) << (ENV_BITS - 1 - 7); // 7 bits TL (bit 6 = always 0)
1018 
1019  if (isExtended(chan_no)) {
1020  // update this slot using frequency data for 1st channel of a pair
1021  auto& ch0 = getFirstOfPair(chan_no);
1022  slot.TLL = slot.TL + (ch0.ksl_base >> slot.ksl);
1023  } else {
1024  // normal
1025  slot.TLL = slot.TL + (ch.ksl_base >> slot.ksl);
1026  }
1027 }
1028 
1029 // set attack rate & decay rate
1030 void YMF262::set_ar_dr(unsigned sl, byte v)
1031 {
1032  auto& ch = channel[sl / 2];
1033  auto& slot = ch.slot[sl & 1];
1034 
1035  slot.ar = (v >> 4) ? 16 + ((v >> 4) << 2) : 0;
1036  slot.dr = (v & 0x0F) ? 16 + ((v & 0x0F) << 2) : 0;
1037  slot.update_ar_dr();
1038 }
1039 
1040 // set sustain level & release rate
1041 void YMF262::set_sl_rr(unsigned sl, byte v)
1042 {
1043  auto& ch = channel[sl / 2];
1044  auto& slot = ch.slot[sl & 1];
1045 
1046  slot.sl = sl_tab[v >> 4];
1047  slot.rr = (v & 0x0F) ? 16 + ((v & 0x0F) << 2) : 0;
1048  slot.update_rr();
1049 }
1050 
1052 {
1053  // no need to call updateStream(time)
1054  return peekReg(r);
1055 }
1056 
1057 byte YMF262::peekReg(unsigned r) const
1058 {
1059  return reg[r];
1060 }
1061 
1062 void YMF262::writeReg(unsigned r, byte v, EmuTime::param time)
1063 {
1064  if (!OPL3_mode && (r != 0x105)) {
1065  // in OPL2 mode the only accessible in set #2 is register 0x05
1066  r &= ~0x100;
1067  }
1068  writeReg512(r, v, time);
1069 }
1070 void YMF262::writeReg512(unsigned r, byte v, EmuTime::param time)
1071 {
1072  updateStream(time); // TODO optimize only for regs that directly influence sound
1073  writeRegDirect(r, v, time);
1074 }
1075 void YMF262::writeRegDirect(unsigned r, byte v, EmuTime::param time)
1076 {
1077  reg[r] = v;
1078 
1079  switch (r) {
1080  case 0x104:
1081  // 6 channels enable
1082  channel[ 0].extended = (v & 0x01) != 0;
1083  channel[ 1].extended = (v & 0x02) != 0;
1084  channel[ 2].extended = (v & 0x04) != 0;
1085  channel[ 9].extended = (v & 0x08) != 0;
1086  channel[10].extended = (v & 0x10) != 0;
1087  channel[11].extended = (v & 0x20) != 0;
1088  return;
1089 
1090  case 0x105:
1091  // OPL3 mode when bit0=1 otherwise it is OPL2 mode
1092  OPL3_mode = v & 0x01;
1093 
1094  // Verified on real YMF278: When NEW2 bit is first set, a read
1095  // from the status register (once) returns bit 1 set (0x02).
1096  // This only happens once after reset, so clearing NEW2 and
1097  // setting it again doesn't cause another change in the status
1098  // register. Also, only bit 1 changes.
1099  if ((v & 0x02) && !alreadySignaledNEW2 && isYMF278) {
1100  status2 = 0x02;
1101  alreadySignaledNEW2 = true;
1102  }
1103 
1104  // following behaviour was tested on real YMF262,
1105  // switching OPL3/OPL2 modes on the fly:
1106  // - does not change the waveform previously selected
1107  // (unless when ....)
1108  // - does not update CH.A, CH.B, CH.C and CH.D output
1109  // selectors (registers c0-c8) (unless when ....)
1110  // - does not disable channels 9-17 on OPL3->OPL2 switch
1111  // - does not switch 4 operator channels back to 2
1112  // operator channels
1113  return;
1114  }
1115 
1116  unsigned ch_offset = (r & 0x100) ? 9 : 0;
1117  switch (r & 0xE0) {
1118  case 0x00: // 00-1F:control
1119  switch (r & 0x1F) {
1120  case 0x01: // test register
1121  break;
1122 
1123  case 0x02: // Timer 1
1124  timer1->setValue(v);
1125  break;
1126 
1127  case 0x03: // Timer 2
1128  timer2->setValue(v);
1129  break;
1130 
1131  case 0x04: // IRQ clear / mask and Timer enable
1132  if (v & 0x80) {
1133  // IRQ flags clear
1134  resetStatus(0x60);
1135  } else {
1136  changeStatusMask((~v) & 0x60);
1137  timer1->setStart((v & R04_ST1) != 0, time);
1138  timer2->setStart((v & R04_ST2) != 0, time);
1139  }
1140  break;
1141 
1142  case 0x08: // x,NTS,x,x, x,x,x,x
1143  nts = (v & 0x40) != 0;
1144  break;
1145 
1146  default:
1147  break;
1148  }
1149  break;
1150 
1151  case 0x20: { // am ON, vib ON, ksr, eg_type, mul
1152  int slot = slot_array[r & 0x1F];
1153  if (slot < 0) return;
1154  set_mul(slot + ch_offset * 2, v);
1155  break;
1156  }
1157  case 0x40: {
1158  int slot = slot_array[r & 0x1F];
1159  if (slot < 0) return;
1160  set_ksl_tl(slot + ch_offset * 2, v);
1161  break;
1162  }
1163  case 0x60: {
1164  int slot = slot_array[r & 0x1F];
1165  if (slot < 0) return;
1166  set_ar_dr(slot + ch_offset * 2, v);
1167  break;
1168  }
1169  case 0x80: {
1170  int slot = slot_array[r & 0x1F];
1171  if (slot < 0) return;
1172  set_sl_rr(slot + ch_offset * 2, v);
1173  break;
1174  }
1175  case 0xA0: {
1176  // note: not r != 0x1BD, only first register block
1177  if (r == 0xBD) {
1178  // am depth, vibrato depth, r,bd,sd,tom,tc,hh
1179  lfo_am_depth = (v & 0x80) != 0;
1180  lfo_pm_depth_range = (v & 0x40) ? 8 : 0;
1181  rhythm = v & 0x3F;
1182 
1183  if (rhythm & 0x20) {
1184  // BD key on/off
1185  if (v & 0x10) {
1186  channel[6].slot[MOD].FM_KEYON (2);
1187  channel[6].slot[CAR].FM_KEYON (2);
1188  } else {
1189  channel[6].slot[MOD].FM_KEYOFF(2);
1190  channel[6].slot[CAR].FM_KEYOFF(2);
1191  }
1192  // HH key on/off
1193  if (v & 0x01) {
1194  channel[7].slot[MOD].FM_KEYON (2);
1195  } else {
1196  channel[7].slot[MOD].FM_KEYOFF(2);
1197  }
1198  // SD key on/off
1199  if (v & 0x08) {
1200  channel[7].slot[CAR].FM_KEYON (2);
1201  } else {
1202  channel[7].slot[CAR].FM_KEYOFF(2);
1203  }
1204  // TOM key on/off
1205  if (v & 0x04) {
1206  channel[8].slot[MOD].FM_KEYON (2);
1207  } else {
1208  channel[8].slot[MOD].FM_KEYOFF(2);
1209  }
1210  // TOP-CY key on/off
1211  if (v & 0x02) {
1212  channel[8].slot[CAR].FM_KEYON (2);
1213  } else {
1214  channel[8].slot[CAR].FM_KEYOFF(2);
1215  }
1216  } else {
1217  // BD key off
1218  channel[6].slot[MOD].FM_KEYOFF(2);
1219  channel[6].slot[CAR].FM_KEYOFF(2);
1220  // HH key off
1221  channel[7].slot[MOD].FM_KEYOFF(2);
1222  // SD key off
1223  channel[7].slot[CAR].FM_KEYOFF(2);
1224  // TOM key off
1225  channel[8].slot[MOD].FM_KEYOFF(2);
1226  // TOP-CY off
1227  channel[8].slot[CAR].FM_KEYOFF(2);
1228  }
1229  return;
1230  }
1231 
1232  // keyon,block,fnum
1233  if ((r & 0x0F) > 8) {
1234  return;
1235  }
1236  unsigned chan_no = (r & 0x0F) + ch_offset;
1237  auto& ch = channel[chan_no];
1238  int block_fnum;
1239  if (!(r & 0x10)) {
1240  // a0-a8
1241  block_fnum = (ch.block_fnum & 0x1F00) | v;
1242  } else {
1243  // b0-b8
1244  block_fnum = ((v & 0x1F) << 8) | (ch.block_fnum & 0xFF);
1245  if (isExtended(chan_no)) {
1246  if (getFirstOfPairNum(chan_no) == chan_no) {
1247  // keyon/off slots of both channels
1248  // forming a 4-op channel
1249  auto& ch0 = getFirstOfPair(chan_no);
1250  auto& ch3 = getSecondOfPair(chan_no);
1251  if (v & 0x20) {
1252  ch0.slot[MOD].FM_KEYON(1);
1253  ch0.slot[CAR].FM_KEYON(1);
1254  ch3.slot[MOD].FM_KEYON(1);
1255  ch3.slot[CAR].FM_KEYON(1);
1256  } else {
1257  ch0.slot[MOD].FM_KEYOFF(1);
1258  ch0.slot[CAR].FM_KEYOFF(1);
1259  ch3.slot[MOD].FM_KEYOFF(1);
1260  ch3.slot[CAR].FM_KEYOFF(1);
1261  }
1262  } else {
1263  // do nothing
1264  }
1265  } else {
1266  // 2 operator function keyon/off
1267  if (v & 0x20) {
1268  ch.slot[MOD].FM_KEYON (1);
1269  ch.slot[CAR].FM_KEYON (1);
1270  } else {
1271  ch.slot[MOD].FM_KEYOFF(1);
1272  ch.slot[CAR].FM_KEYOFF(1);
1273  }
1274  }
1275  }
1276  // update
1277  if (ch.block_fnum != block_fnum) {
1278  ch.block_fnum = block_fnum;
1279  ch.ksl_base = ksl_tab[block_fnum >> 6];
1280  ch.fc = fnumToIncrement(block_fnum);
1281 
1282  // BLK 2,1,0 bits -> bits 3,2,1 of kcode
1283  ch.kcode = (ch.block_fnum & 0x1C00) >> 9;
1284 
1285  // the info below is actually opposite to what is stated
1286  // in the Manuals (verifed on real YMF262)
1287  // if notesel == 0 -> lsb of kcode is bit 10 (MSB) of fnum
1288  // if notesel == 1 -> lsb of kcode is bit 9 (MSB-1) of fnum
1289  if (nts) {
1290  ch.kcode |= (ch.block_fnum & 0x100) >> 8; // notesel == 1
1291  } else {
1292  ch.kcode |= (ch.block_fnum & 0x200) >> 9; // notesel == 0
1293  }
1294  if (isExtended(chan_no)) {
1295  if (getFirstOfPairNum(chan_no) == chan_no) {
1296  // update slots of both channels
1297  // forming up 4-op channel
1298  // refresh Total Level
1299  auto& ch0 = getFirstOfPair(chan_no);
1300  auto& ch3 = getSecondOfPair(chan_no);
1301  ch0.slot[MOD].TLL = ch0.slot[MOD].TL + (ch.ksl_base >> ch0.slot[MOD].ksl);
1302  ch0.slot[CAR].TLL = ch0.slot[CAR].TL + (ch.ksl_base >> ch0.slot[CAR].ksl);
1303  ch3.slot[MOD].TLL = ch3.slot[MOD].TL + (ch.ksl_base >> ch3.slot[MOD].ksl);
1304  ch3.slot[CAR].TLL = ch3.slot[CAR].TL + (ch.ksl_base >> ch3.slot[CAR].ksl);
1305 
1306  // refresh frequency counter
1307  ch0.slot[MOD].calc_fc(ch);
1308  ch0.slot[CAR].calc_fc(ch);
1309  ch3.slot[MOD].calc_fc(ch);
1310  ch3.slot[CAR].calc_fc(ch);
1311  } else {
1312  // nothing
1313  }
1314  } else {
1315  // refresh Total Level in both SLOTs of this channel
1316  ch.slot[MOD].TLL = ch.slot[MOD].TL + (ch.ksl_base >> ch.slot[MOD].ksl);
1317  ch.slot[CAR].TLL = ch.slot[CAR].TL + (ch.ksl_base >> ch.slot[CAR].ksl);
1318 
1319  // refresh frequency counter in both SLOTs of this channel
1320  ch.slot[MOD].calc_fc(ch);
1321  ch.slot[CAR].calc_fc(ch);
1322  }
1323  }
1324  break;
1325  }
1326  case 0xC0: {
1327  // CH.D, CH.C, CH.B, CH.A, FB(3bits), C
1328  if ((r & 0xF) > 8) {
1329  return;
1330  }
1331  unsigned chan_no = (r & 0x0F) + ch_offset;
1332  auto& ch = channel[chan_no];
1333 
1334  unsigned base = chan_no * 4;
1335  if (OPL3_mode) {
1336  // OPL3 mode
1337  pan[base + 0] = (v & 0x10) ? unsigned(~0) : 0; // ch.A
1338  pan[base + 1] = (v & 0x20) ? unsigned(~0) : 0; // ch.B
1339  pan[base + 2] = (v & 0x40) ? unsigned(~0) : 0; // ch.C
1340  pan[base + 3] = (v & 0x80) ? unsigned(~0) : 0; // ch.D
1341  } else {
1342  // OPL2 mode - always enabled
1343  pan[base + 0] = unsigned(~0); // ch.A
1344  pan[base + 1] = unsigned(~0); // ch.B
1345  pan[base + 2] = unsigned(~0); // ch.C
1346  pan[base + 3] = unsigned(~0); // ch.D
1347  }
1348 
1349  ch.slot[MOD].setFeedbackShift((v >> 1) & 7);
1350  ch.slot[MOD].CON = v & 1;
1351 
1352  if (isExtended(chan_no)) {
1353  unsigned chan_no0 = getFirstOfPairNum(chan_no);
1354  unsigned chan_no3 = chan_no0 + 3;
1355  auto& ch0 = getFirstOfPair(chan_no);
1356  auto& ch3 = getSecondOfPair(chan_no);
1357  switch ((ch0.slot[MOD].CON ? 2:0) | (ch3.slot[MOD].CON ? 1:0)) {
1358  case 0:
1359  // 1 -> 2 -> 3 -> 4 -> out
1360  ch0.slot[MOD].connect = &phase_modulation;
1361  ch0.slot[CAR].connect = &phase_modulation2;
1362  ch3.slot[MOD].connect = &phase_modulation;
1363  ch3.slot[CAR].connect = &chanout[chan_no3];
1364  break;
1365  case 1:
1366  // 1 -> 2 -\.
1367  // 3 -> 4 --+-> out
1368  ch0.slot[MOD].connect = &phase_modulation;
1369  ch0.slot[CAR].connect = &chanout[chan_no0];
1370  ch3.slot[MOD].connect = &phase_modulation;
1371  ch3.slot[CAR].connect = &chanout[chan_no3];
1372  break;
1373  case 2:
1374  // 1 ----------\.
1375  // 2 -> 3 -> 4 -+-> out
1376  ch0.slot[MOD].connect = &chanout[chan_no0];
1377  ch0.slot[CAR].connect = &phase_modulation2;
1378  ch3.slot[MOD].connect = &phase_modulation;
1379  ch3.slot[CAR].connect = &chanout[chan_no3];
1380  break;
1381  case 3:
1382  // 1 -----\.
1383  // 2 -> 3 -+-> out
1384  // 4 -----/
1385  ch0.slot[MOD].connect = &chanout[chan_no0];
1386  ch0.slot[CAR].connect = &phase_modulation2;
1387  ch3.slot[MOD].connect = &chanout[chan_no3];
1388  ch3.slot[CAR].connect = &chanout[chan_no3];
1389  break;
1390  }
1391  } else {
1392  // 2 operators mode
1393  ch.slot[MOD].connect = ch.slot[MOD].CON
1394  ? &chanout[chan_no]
1395  : &phase_modulation;
1396  ch.slot[CAR].connect = &chanout[chan_no];
1397  }
1398  break;
1399  }
1400  case 0xE0: {
1401  // waveform select
1402  int slot = slot_array[r & 0x1f];
1403  if (slot < 0) return;
1404  slot += ch_offset * 2;
1405  auto& ch = channel[slot / 2];
1406 
1407  // store 3-bit value written regardless of current OPL2 or OPL3
1408  // mode... (verified on real YMF262)
1409  v &= 7;
1410  // ... but select only waveforms 0-3 in OPL2 mode
1411  if (!OPL3_mode) {
1412  v &= 3;
1413  }
1414  ch.slot[slot & 1].wavetable = &sin.tab[v * SIN_LEN];
1415  break;
1416  }
1417  }
1418 }
1419 
1420 
1421 void YMF262::reset(EmuTime::param time)
1422 {
1423  eg_cnt = 0;
1424 
1425  noise_rng = 1; // noise shift register
1426  nts = false; // note split
1427  alreadySignaledNEW2 = false;
1428  resetStatus(0x60);
1429 
1430  // reset with register write
1431  writeRegDirect(0x01, 0, time); // test register
1432  writeRegDirect(0x02, 0, time); // Timer1
1433  writeRegDirect(0x03, 0, time); // Timer2
1434  writeRegDirect(0x04, 0, time); // IRQ mask clear
1435 
1436  // FIX IT registers 101, 104 and 105
1437  // FIX IT (dont change CH.D, CH.C, CH.B and CH.A in C0-C8 registers)
1438  for (int c = 0xFF; c >= 0x20; c--) {
1439  writeRegDirect(c, 0, time);
1440  }
1441  // FIX IT (dont change CH.D, CH.C, CH.B and CH.A in C0-C8 registers)
1442  for (int c = 0x1FF; c >= 0x120; c--) {
1443  writeRegDirect(c, 0, time);
1444  }
1445 
1446  // reset operator parameters
1447  for (auto& ch : channel) {
1448  for (auto& sl : ch.slot) {
1449  sl.state = EG_OFF;
1450  sl.volume = MAX_ATT_INDEX;
1451  }
1452  }
1453 
1454  setMixLevel(0x1b, time); // -9dB left and right
1455 }
1456 
1457 static unsigned calcInputRate(bool isYMF278)
1458 {
1459  return unsigned(lrintf(isYMF278 ? 33868800.0f / (19 * 36)
1460  : 4 * 3579545.0f / ( 8 * 36)));
1461 }
1462 YMF262::YMF262(const std::string& name_,
1463  const DeviceConfig& config, bool isYMF278_)
1464  : ResampledSoundDevice(config.getMotherBoard(), name_, "MoonSound FM-part",
1465  18, calcInputRate(isYMF278_), true)
1466  , debuggable(config.getMotherBoard(), getName())
1467  , timer1(isYMF278_
1468  ? EmuTimer::createOPL4_1(config.getScheduler(), *this)
1469  : EmuTimer::createOPL3_1(config.getScheduler(), *this))
1470  , timer2(isYMF278_
1471  ? EmuTimer::createOPL4_2(config.getScheduler(), *this)
1472  : EmuTimer::createOPL3_2(config.getScheduler(), *this))
1473  , irq(config.getMotherBoard(), getName() + ".IRQ")
1474  , lfo_am_cnt(0), lfo_pm_cnt(0)
1475  , isYMF278(isYMF278_)
1476 {
1477  lfo_am_depth = false;
1478  lfo_pm_depth_range = 0;
1479  rhythm = 0;
1480  OPL3_mode = false;
1481  status = status2 = statusMask = 0;
1482 
1483  // avoid (harmless) UMR in serialize()
1484  memset(chanout, 0, sizeof(chanout));
1485  memset(reg, 0, sizeof(reg));
1486 
1487  // For debugging: print out tables to be able to compare before/after
1488  // when the calculation changes.
1489  if (false) {
1490  for (auto& e : tl.tab) std::cout << e << '\n';
1491  std::cout << '\n';
1492  for (auto& e : sin.tab) std::cout << e << '\n';
1493  }
1494 
1495  registerSound(config);
1496  reset(config.getMotherBoard().getCurrentTime()); // must come after registerSound() because of call to setSoftwareVolume() via setMixLevel()
1497 }
1498 
1500 {
1501  unregisterSound();
1502 }
1503 
1505 {
1506  // no need to call updateStream(time)
1507  byte result = status | status2;
1508  status2 = 0;
1509  return result;
1510 }
1511 
1513 {
1514  return status | status2;
1515 }
1516 
1517 bool YMF262::checkMuteHelper()
1518 {
1519  // TODO this doesn't always mute when possible
1520  for (auto& ch : channel) {
1521  for (auto& sl : ch.slot) {
1522  if (!((sl.state == EG_OFF) ||
1523  ((sl.state == EG_RELEASE) &&
1524  ((sl.TLL + sl.volume) >= ENV_QUIET)))) {
1525  return false;
1526  }
1527  }
1528  }
1529  return true;
1530 }
1531 
1532 void YMF262::setMixLevel(uint8_t x, EmuTime::param time)
1533 {
1534  // Only present on YMF278
1535  // see mix_level[] and vol_factor() in YMF278.cc
1536  static const float level[8] = {
1537  (1.00f / 1), // 0dB
1538  (0.75f / 1), // -3dB (approx)
1539  (1.00f / 2), // -6dB
1540  (0.75f / 2), // -9dB (approx)
1541  (1.00f / 4), // -12dB
1542  (0.75f / 4), // -15dB (approx)
1543  (1.00f / 8), // -18dB
1544  (0.00f ), // -inf dB
1545  };
1546  setSoftwareVolume(level[x & 7], level[(x >> 3) & 7], time);
1547 }
1548 
1549 float YMF262::getAmplificationFactorImpl() const
1550 {
1551  return 1.0f / 4096.0f;
1552 }
1553 
1554 void YMF262::generateChannels(float** bufs, unsigned num)
1555 {
1556  // TODO implement per-channel mute (instead of all-or-nothing)
1557  // TODO output rhythm on separate channels?
1558  if (checkMuteHelper()) {
1559  // TODO update internal state, even if muted
1560  for (int i = 0; i < 18; ++i) {
1561  bufs[i] = nullptr;
1562  }
1563  return;
1564  }
1565 
1566  bool rhythmEnabled = (rhythm & 0x20) != 0;
1567 
1568  for (unsigned j = 0; j < num; ++j) {
1569  // Amplitude modulation: 27 output levels (triangle waveform);
1570  // 1 level takes one of: 192, 256 or 448 samples
1571  // One entry from LFO_AM_TABLE lasts for 64 samples
1572  lfo_am_cnt.addQuantum();
1573  if (lfo_am_cnt == LFOAMIndex(LFO_AM_TAB_ELEMENTS)) {
1574  // lfo_am_table is 210 elements long
1575  lfo_am_cnt = LFOAMIndex(0);
1576  }
1577  unsigned tmp = lfo_am_table[lfo_am_cnt.toInt()];
1578  unsigned lfo_am = lfo_am_depth ? tmp : tmp / 4;
1579 
1580  // clear channel outputs
1581  memset(chanout, 0, sizeof(chanout));
1582 
1583  // channels 0,3 1,4 2,5 9,12 10,13 11,14
1584  // in either 2op or 4op mode
1585  for (int k = 0; k <= 9; k += 9) {
1586  for (int i = 0; i < 3; ++i) {
1587  auto& ch0 = channel[k + i + 0];
1588  auto& ch3 = channel[k + i + 3];
1589  // extended 4op ch#0 part 1 or 2op ch#0
1590  ch0.chan_calc(lfo_am);
1591  if (ch0.extended) {
1592  // extended 4op ch#0 part 2
1593  ch3.chan_calc_ext(lfo_am);
1594  } else {
1595  // standard 2op ch#3
1596  ch3.chan_calc(lfo_am);
1597  }
1598  }
1599  }
1600 
1601  // channels 6,7,8 rhythm or 2op mode
1602  if (!rhythmEnabled) {
1603  channel[6].chan_calc(lfo_am);
1604  channel[7].chan_calc(lfo_am);
1605  channel[8].chan_calc(lfo_am);
1606  } else {
1607  // Rhythm part
1608  chan_calc_rhythm(lfo_am);
1609  }
1610 
1611  // channels 15,16,17 are fixed 2-operator channels only
1612  channel[15].chan_calc(lfo_am);
1613  channel[16].chan_calc(lfo_am);
1614  channel[17].chan_calc(lfo_am);
1615 
1616  for (int i = 0; i < 18; ++i) {
1617  bufs[i][2 * j + 0] += int(chanout[i] & pan[4 * i + 0]);
1618  bufs[i][2 * j + 1] += int(chanout[i] & pan[4 * i + 1]);
1619  // unused c += int(chanout[i] & pan[4 * i + 2]);
1620  // unused d += int(chanout[i] & pan[4 * i + 3]);
1621  }
1622 
1623  advance();
1624  }
1625 }
1626 
1627 
1628 static std::initializer_list<enum_string<YMF262::EnvelopeState>> envelopeStateInfo = {
1629  { "ATTACK", YMF262::EG_ATTACK },
1630  { "DECAY", YMF262::EG_DECAY },
1631  { "SUSTAIN", YMF262::EG_SUSTAIN },
1632  { "RELEASE", YMF262::EG_RELEASE },
1633  { "OFF", YMF262::EG_OFF }
1634 };
1635 SERIALIZE_ENUM(YMF262::EnvelopeState, envelopeStateInfo);
1636 
1637 template<typename Archive>
1638 void YMF262::Slot::serialize(Archive& a, unsigned /*version*/)
1639 {
1640  // wavetable
1641  auto waveform = unsigned((wavetable - sin.tab) / SIN_LEN);
1642  a.serialize("waveform", waveform);
1643  if (a.isLoader()) {
1644  wavetable = &sin.tab[waveform * SIN_LEN];
1645  }
1646 
1647  // done by rewriting registers:
1648  // connect, fb_shift, CON
1649  // TODO handle more state like this
1650 
1651  a.serialize("Cnt", Cnt,
1652  "Incr", Incr,
1653  "op1_out", op1_out,
1654  "TL", TL,
1655  "TLL", TLL,
1656  "volume", volume,
1657  "sl", sl,
1658  "state", state,
1659  "eg_m_ar", eg_m_ar,
1660  "eg_m_dr", eg_m_dr,
1661  "eg_m_rr", eg_m_rr,
1662  "eg_sh_ar", eg_sh_ar,
1663  "eg_sel_ar", eg_sel_ar,
1664  "eg_sh_dr", eg_sh_dr,
1665  "eg_sel_dr", eg_sel_dr,
1666  "eg_sh_rr", eg_sh_rr,
1667  "eg_sel_rr", eg_sel_rr,
1668  "key", key,
1669  "eg_type", eg_type,
1670  "AMmask", AMmask,
1671  "vib", vib,
1672  "ar", ar,
1673  "dr", dr,
1674  "rr", rr,
1675  "KSR", KSR,
1676  "ksl", ksl,
1677  "ksr", ksr,
1678  "mul", mul);
1679 }
1680 
1681 template<typename Archive>
1682 void YMF262::Channel::serialize(Archive& a, unsigned /*version*/)
1683 {
1684  a.serialize("slots", slot,
1685  "block_fnum", block_fnum,
1686  "fc", fc,
1687  "ksl_base", ksl_base,
1688  "kcode", kcode,
1689  "extended", extended);
1690 }
1691 
1692 // version 1: initial version
1693 // version 2: added alreadySignaledNEW2
1694 template<typename Archive>
1695 void YMF262::serialize(Archive& a, unsigned version)
1696 {
1697  a.serialize("timer1", *timer1,
1698  "timer2", *timer2,
1699  "irq", irq,
1700  "chanout", chanout);
1701  a.serialize_blob("registers", reg, sizeof(reg));
1702  a.serialize("channels", channel,
1703  "eg_cnt", eg_cnt,
1704  "noise_rng", noise_rng,
1705  "lfo_am_cnt", lfo_am_cnt,
1706  "lfo_pm_cnt", lfo_pm_cnt,
1707  "lfo_am_depth", lfo_am_depth,
1708  "lfo_pm_depth_range", lfo_pm_depth_range,
1709  "rhythm", rhythm,
1710  "nts", nts,
1711  "OPL3_mode", OPL3_mode,
1712  "status", status,
1713  "status2", status2,
1714  "statusMask", statusMask);
1715  if (a.versionAtLeast(version, 2)) {
1716  a.serialize("alreadySignaledNEW2", alreadySignaledNEW2);
1717  } else {
1718  assert(a.isLoader());
1719  alreadySignaledNEW2 = true; // we can't know the actual value,
1720  // but 'true' is the safest value
1721  }
1722 
1723  // TODO restore more state by rewriting register values
1724  // this handles pan
1725  EmuTime::param time = timer1->getCurrentTime();
1726  for (int i = 0xC0; i <= 0xC8; ++i) {
1727  writeRegDirect(i + 0x000, reg[i + 0x000], time);
1728  writeRegDirect(i + 0x100, reg[i + 0x100], time);
1729  }
1730 }
1731 
1733 
1734 
1735 // YMF262::Debuggable
1736 
1737 YMF262::Debuggable::Debuggable(MSXMotherBoard& motherBoard_,
1738  const std::string& name_)
1739  : SimpleDebuggable(motherBoard_, name_ + " regs",
1740  "MoonSound FM-part registers", 0x200)
1741 {
1742 }
1743 
1744 byte YMF262::Debuggable::read(unsigned address)
1745 {
1746  auto& ymf262 = OUTER(YMF262, debuggable);
1747  return ymf262.peekReg(address);
1748 }
1749 
1750 void YMF262::Debuggable::write(unsigned address, byte value, EmuTime::param time)
1751 {
1752  auto& ymf262 = OUTER(YMF262, debuggable);
1753  ymf262.writeReg512(address, value, time);
1754 }
1755 
1756 } // namespace openmsx
SERIALIZE_ENUM(CassettePlayer::State, stateInfo)
void serialize(Archive &ar, unsigned version)
Definition: YMF262.cc:1695
FixedPoint< 16 > FreqIndex
16.16 fixed point type for frequency calculations
Definition: YMF262.hh:41
void setSoftwareVolume(float volume, EmuTime::param time)
Change the &#39;software volume&#39; of this sound device.
Definition: SoundDevice.cc:141
void setMixLevel(uint8_t x, EmuTime::param time)
Definition: YMF262.cc:1532
vecN< N, T > min(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:269
void unregisterSound()
Unregisters this sound device with the Mixer.
Definition: SoundDevice.cc:131
FixedPoint< 16 > FreqIndex
16.16 fixed point type for frequency calculations.
byte peekStatus() const
Definition: YMF262.cc:1512
#define M_PI
Definition: Math.hh:26
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
void writeReg(unsigned r, byte v, EmuTime::param time)
Definition: YMF262.cc:1062
byte peekReg(unsigned reg) const
Definition: YMF262.cc:1057
void writeReg512(unsigned r, byte v, EmuTime::param time)
Definition: YMF262.cc:1070
#define DV(x)
Definition: YMF262.cc:94
byte readStatus()
Definition: YMF262.cc:1504
YMF262(const std::string &name, const DeviceConfig &config, bool isYMF278)
Definition: YMF262.cc:1462
#define O(a)
Definition: YMF262.cc:214
#define ML(x)
Definition: YMF262.cc:248
void reset(EmuTime::param time)
Definition: YMF262.cc:1421
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
void addQuantum()
Increase this value with the smallest possible amount.
Definition: FixedPoint.hh:217
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:981
string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:589
unsigned tab[SIN_LEN *8]
Definition: YMF262.cc:409
EmuTime::param getCurrentTime()
Convenience method: This is the same as getScheduler().getCurrentTime().
constexpr int toInt() const
Returns the integer part (rounded down) of this fixed point number.
Definition: FixedPoint.hh:76
int tab[TL_TAB_LEN]
Definition: YMF262.cc:373
#define SC(db)
Definition: YMF262.cc:141
#define OUTER(type, member)
Definition: outer.hh:38
byte readReg(unsigned reg)
Definition: YMF262.cc:1051
MSXMotherBoard & getMotherBoard() const
Definition: DeviceConfig.cc:13
TclObject t
void serialize(Archive &ar, T &t, unsigned version)
void registerSound(const DeviceConfig &config)
Registers this sound device with the Mixer.
Definition: SoundDevice.cc:90