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