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