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