Debian Codegraph @

BambooTracker/chip/ymfm/ymfm_fm.ipp

raw
1 // BSD 3-Clause License
2 //
3 // Copyright (c) 2021, Aaron Giles
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this
10 // list of conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice,
13 // this list of conditions and the following disclaimer in the documentation
14 // and/or other materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 namespace ymfm
32 {
33
34 //*********************************************************
35 // GLOBAL TABLE LOOKUPS
36 //*********************************************************
37
38 //-------------------------------------------------
39 // abs_sin_attenuation - given a sin (phase) input
40 // where the range 0-2*PI is mapped onto 10 bits,
41 // return the absolute value of sin(input),
42 // logarithmically-adjusted and treated as an
43 // attenuation value, in 4.8 fixed point format
44 //-------------------------------------------------
45
46 inline uint32_t abs_sin_attenuation(uint32_t input)
47 {
48 // the values here are stored as 4.8 logarithmic values for 1/4 phase
49 // this matches the internal format of the OPN chip, extracted from the die
50 static uint16_t const s_sin_table[256] =
51 {
52 0x859,0x6c3,0x607,0x58b,0x52e,0x4e4,0x4a6,0x471,0x443,0x41a,0x3f5,0x3d3,0x3b5,0x398,0x37e,0x365,
53 0x34e,0x339,0x324,0x311,0x2ff,0x2ed,0x2dc,0x2cd,0x2bd,0x2af,0x2a0,0x293,0x286,0x279,0x26d,0x261,
54 0x256,0x24b,0x240,0x236,0x22c,0x222,0x218,0x20f,0x206,0x1fd,0x1f5,0x1ec,0x1e4,0x1dc,0x1d4,0x1cd,
55 0x1c5,0x1be,0x1b7,0x1b0,0x1a9,0x1a2,0x19b,0x195,0x18f,0x188,0x182,0x17c,0x177,0x171,0x16b,0x166,
56 0x160,0x15b,0x155,0x150,0x14b,0x146,0x141,0x13c,0x137,0x133,0x12e,0x129,0x125,0x121,0x11c,0x118,
57 0x114,0x10f,0x10b,0x107,0x103,0x0ff,0x0fb,0x0f8,0x0f4,0x0f0,0x0ec,0x0e9,0x0e5,0x0e2,0x0de,0x0db,
58 0x0d7,0x0d4,0x0d1,0x0cd,0x0ca,0x0c7,0x0c4,0x0c1,0x0be,0x0bb,0x0b8,0x0b5,0x0b2,0x0af,0x0ac,0x0a9,
59 0x0a7,0x0a4,0x0a1,0x09f,0x09c,0x099,0x097,0x094,0x092,0x08f,0x08d,0x08a,0x088,0x086,0x083,0x081,
60 0x07f,0x07d,0x07a,0x078,0x076,0x074,0x072,0x070,0x06e,0x06c,0x06a,0x068,0x066,0x064,0x062,0x060,
61 0x05e,0x05c,0x05b,0x059,0x057,0x055,0x053,0x052,0x050,0x04e,0x04d,0x04b,0x04a,0x048,0x046,0x045,
62 0x043,0x042,0x040,0x03f,0x03e,0x03c,0x03b,0x039,0x038,0x037,0x035,0x034,0x033,0x031,0x030,0x02f,
63 0x02e,0x02d,0x02b,0x02a,0x029,0x028,0x027,0x026,0x025,0x024,0x023,0x022,0x021,0x020,0x01f,0x01e,
64 0x01d,0x01c,0x01b,0x01a,0x019,0x018,0x017,0x017,0x016,0x015,0x014,0x014,0x013,0x012,0x011,0x011,
65 0x010,0x00f,0x00f,0x00e,0x00d,0x00d,0x00c,0x00c,0x00b,0x00a,0x00a,0x009,0x009,0x008,0x008,0x007,
66 0x007,0x007,0x006,0x006,0x005,0x005,0x005,0x004,0x004,0x004,0x003,0x003,0x003,0x002,0x002,0x002,
67 0x002,0x001,0x001,0x001,0x001,0x001,0x001,0x001,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000
68 };
69
70 // if the top bit is set, we're in the second half of the curve
71 // which is a mirror image, so invert the index
72 if (bitfield(input, 8))
73 input = ~input;
74
75 // return the value from the table
76 return s_sin_table[input & 0xff];
77 }
78
79
80 //-------------------------------------------------
81 // attenuation_to_volume - given a 5.8 fixed point
82 // logarithmic attenuation value, return a 13-bit
83 // linear volume
84 //-------------------------------------------------
85
86 inline uint32_t attenuation_to_volume(uint32_t input)
87 {
88 // the values here are 10-bit mantissas with an implied leading bit
89 // this matches the internal format of the OPN chip, extracted from the die
90
91 // as a nod to performance, the implicit 0x400 bit is pre-incorporated, and
92 // the values are left-shifted by 2 so that a simple right shift is all that
93 // is needed; also the order is reversed to save a NOT on the input
94 #define X(a) (((a) | 0x400) << 2)
95 static uint16_t const s_power_table[256] =
96 {
97 X(0x3fa),X(0x3f5),X(0x3ef),X(0x3ea),X(0x3e4),X(0x3df),X(0x3da),X(0x3d4),
98 X(0x3cf),X(0x3c9),X(0x3c4),X(0x3bf),X(0x3b9),X(0x3b4),X(0x3ae),X(0x3a9),
99 X(0x3a4),X(0x39f),X(0x399),X(0x394),X(0x38f),X(0x38a),X(0x384),X(0x37f),
100 X(0x37a),X(0x375),X(0x370),X(0x36a),X(0x365),X(0x360),X(0x35b),X(0x356),
101 X(0x351),X(0x34c),X(0x347),X(0x342),X(0x33d),X(0x338),X(0x333),X(0x32e),
102 X(0x329),X(0x324),X(0x31f),X(0x31a),X(0x315),X(0x310),X(0x30b),X(0x306),
103 X(0x302),X(0x2fd),X(0x2f8),X(0x2f3),X(0x2ee),X(0x2e9),X(0x2e5),X(0x2e0),
104 X(0x2db),X(0x2d6),X(0x2d2),X(0x2cd),X(0x2c8),X(0x2c4),X(0x2bf),X(0x2ba),
105 X(0x2b5),X(0x2b1),X(0x2ac),X(0x2a8),X(0x2a3),X(0x29e),X(0x29a),X(0x295),
106 X(0x291),X(0x28c),X(0x288),X(0x283),X(0x27f),X(0x27a),X(0x276),X(0x271),
107 X(0x26d),X(0x268),X(0x264),X(0x25f),X(0x25b),X(0x257),X(0x252),X(0x24e),
108 X(0x249),X(0x245),X(0x241),X(0x23c),X(0x238),X(0x234),X(0x230),X(0x22b),
109 X(0x227),X(0x223),X(0x21e),X(0x21a),X(0x216),X(0x212),X(0x20e),X(0x209),
110 X(0x205),X(0x201),X(0x1fd),X(0x1f9),X(0x1f5),X(0x1f0),X(0x1ec),X(0x1e8),
111 X(0x1e4),X(0x1e0),X(0x1dc),X(0x1d8),X(0x1d4),X(0x1d0),X(0x1cc),X(0x1c8),
112 X(0x1c4),X(0x1c0),X(0x1bc),X(0x1b8),X(0x1b4),X(0x1b0),X(0x1ac),X(0x1a8),
113 X(0x1a4),X(0x1a0),X(0x19c),X(0x199),X(0x195),X(0x191),X(0x18d),X(0x189),
114 X(0x185),X(0x181),X(0x17e),X(0x17a),X(0x176),X(0x172),X(0x16f),X(0x16b),
115 X(0x167),X(0x163),X(0x160),X(0x15c),X(0x158),X(0x154),X(0x151),X(0x14d),
116 X(0x149),X(0x146),X(0x142),X(0x13e),X(0x13b),X(0x137),X(0x134),X(0x130),
117 X(0x12c),X(0x129),X(0x125),X(0x122),X(0x11e),X(0x11b),X(0x117),X(0x114),
118 X(0x110),X(0x10c),X(0x109),X(0x106),X(0x102),X(0x0ff),X(0x0fb),X(0x0f8),
119 X(0x0f4),X(0x0f1),X(0x0ed),X(0x0ea),X(0x0e7),X(0x0e3),X(0x0e0),X(0x0dc),
120 X(0x0d9),X(0x0d6),X(0x0d2),X(0x0cf),X(0x0cc),X(0x0c8),X(0x0c5),X(0x0c2),
121 X(0x0be),X(0x0bb),X(0x0b8),X(0x0b5),X(0x0b1),X(0x0ae),X(0x0ab),X(0x0a8),
122 X(0x0a4),X(0x0a1),X(0x09e),X(0x09b),X(0x098),X(0x094),X(0x091),X(0x08e),
123 X(0x08b),X(0x088),X(0x085),X(0x082),X(0x07e),X(0x07b),X(0x078),X(0x075),
124 X(0x072),X(0x06f),X(0x06c),X(0x069),X(0x066),X(0x063),X(0x060),X(0x05d),
125 X(0x05a),X(0x057),X(0x054),X(0x051),X(0x04e),X(0x04b),X(0x048),X(0x045),
126 X(0x042),X(0x03f),X(0x03c),X(0x039),X(0x036),X(0x033),X(0x030),X(0x02d),
127 X(0x02a),X(0x028),X(0x025),X(0x022),X(0x01f),X(0x01c),X(0x019),X(0x016),
128 X(0x014),X(0x011),X(0x00e),X(0x00b),X(0x008),X(0x006),X(0x003),X(0x000)
129 };
130 #undef X
131
132 // look up the fractional part, then shift by the whole
133 return s_power_table[input & 0xff] >> (input >> 8);
134 }
135
136
137 //-------------------------------------------------
138 // attenuation_increment - given a 6-bit ADSR
139 // rate value and a 3-bit stepping index,
140 // return a 4-bit increment to the attenutaion
141 // for this step (or for the attack case, the
142 // fractional scale factor to decrease by)
143 //-------------------------------------------------
144
145 inline uint32_t attenuation_increment(uint32_t rate, uint32_t index)
146 {
147 static uint32_t const s_increment_table[64] =
148 {
149 0x00000000, 0x00000000, 0x10101010, 0x10101010, // 0-3 (0x00-0x03)
150 0x10101010, 0x10101010, 0x11101110, 0x11101110, // 4-7 (0x04-0x07)
151 0x10101010, 0x10111010, 0x11101110, 0x11111110, // 8-11 (0x08-0x0B)
152 0x10101010, 0x10111010, 0x11101110, 0x11111110, // 12-15 (0x0C-0x0F)
153 0x10101010, 0x10111010, 0x11101110, 0x11111110, // 16-19 (0x10-0x13)
154 0x10101010, 0x10111010, 0x11101110, 0x11111110, // 20-23 (0x14-0x17)
155 0x10101010, 0x10111010, 0x11101110, 0x11111110, // 24-27 (0x18-0x1B)
156 0x10101010, 0x10111010, 0x11101110, 0x11111110, // 28-31 (0x1C-0x1F)
157 0x10101010, 0x10111010, 0x11101110, 0x11111110, // 32-35 (0x20-0x23)
158 0x10101010, 0x10111010, 0x11101110, 0x11111110, // 36-39 (0x24-0x27)
159 0x10101010, 0x10111010, 0x11101110, 0x11111110, // 40-43 (0x28-0x2B)
160 0x10101010, 0x10111010, 0x11101110, 0x11111110, // 44-47 (0x2C-0x2F)
161 0x11111111, 0x21112111, 0x21212121, 0x22212221, // 48-51 (0x30-0x33)
162 0x22222222, 0x42224222, 0x42424242, 0x44424442, // 52-55 (0x34-0x37)
163 0x44444444, 0x84448444, 0x84848484, 0x88848884, // 56-59 (0x38-0x3B)
164 0x88888888, 0x88888888, 0x88888888, 0x88888888 // 60-63 (0x3C-0x3F)
165 };
166 return bitfield(s_increment_table[rate], 4*index, 4);
167 }
168
169
170 //-------------------------------------------------
171 // detune_adjustment - given a 5-bit key code
172 // value and a 3-bit detune parameter, return a
173 // 6-bit signed phase displacement; this table
174 // has been verified against Nuked's equations,
175 // but the equations are rather complicated, so
176 // we'll keep the simplicity of the table
177 //-------------------------------------------------
178
179 inline int32_t detune_adjustment(uint32_t detune, uint32_t keycode)
180 {
181 static uint8_t const s_detune_adjustment[32][4] =
182 {
183 { 0, 0, 1, 2 }, { 0, 0, 1, 2 }, { 0, 0, 1, 2 }, { 0, 0, 1, 2 },
184 { 0, 1, 2, 2 }, { 0, 1, 2, 3 }, { 0, 1, 2, 3 }, { 0, 1, 2, 3 },
185 { 0, 1, 2, 4 }, { 0, 1, 3, 4 }, { 0, 1, 3, 4 }, { 0, 1, 3, 5 },
186 { 0, 2, 4, 5 }, { 0, 2, 4, 6 }, { 0, 2, 4, 6 }, { 0, 2, 5, 7 },
187 { 0, 2, 5, 8 }, { 0, 3, 6, 8 }, { 0, 3, 6, 9 }, { 0, 3, 7, 10 },
188 { 0, 4, 8, 11 }, { 0, 4, 8, 12 }, { 0, 4, 9, 13 }, { 0, 5, 10, 14 },
189 { 0, 5, 11, 16 }, { 0, 6, 12, 17 }, { 0, 6, 13, 19 }, { 0, 7, 14, 20 },
190 { 0, 8, 16, 22 }, { 0, 8, 16, 22 }, { 0, 8, 16, 22 }, { 0, 8, 16, 22 }
191 };
192 int32_t result = s_detune_adjustment[keycode][detune & 3];
193 return bitfield(detune, 2) ? -result : result;
194 }
195
196
197 //-------------------------------------------------
198 // opm_key_code_to_phase_step - converts an
199 // OPM concatenated block (3 bits), keycode
200 // (4 bits) and key fraction (6 bits) to a 0.10
201 // phase step, after applying the given delta;
202 // this applies to OPM and OPZ, so it lives here
203 // in a central location
204 //-------------------------------------------------
205
206 inline uint32_t opm_key_code_to_phase_step(uint32_t block_freq, int32_t delta)
207 {
208 // The phase step is essentially the fnum in OPN-speak. To compute this table,
209 // we used the standard formula for computing the frequency of a note, and
210 // then converted that frequency to fnum using the formula documented in the
211 // YM2608 manual.
212 //
213 // However, the YM2608 manual describes everything in terms of a nominal 8MHz
214 // clock, which produces an FM clock of:
215 //
216 // 8000000 / 24(operators) / 6(prescale) = 55555Hz FM clock
217 //
218 // Whereas the descriptions for the YM2151 use a nominal 3.579545MHz clock:
219 //
220 // 3579545 / 32(operators) / 2(prescale) = 55930Hz FM clock
221 //
222 // To correct for this, the YM2608 formula was adjusted to use a clock of
223 // 8053920Hz, giving this equation for the fnum:
224 //
225 // fnum = (double(144) * freq * (1 << 20)) / double(8053920) / 4;
226 //
227 // Unfortunately, the computed table differs in a few spots from the data
228 // verified from an actual chip. The table below comes from David Viens'
229 // analysis, used with his permission.
230 static const uint32_t s_phase_step[12*64] =
231 {
232 41568,41600,41632,41664,41696,41728,41760,41792,41856,41888,41920,41952,42016,42048,42080,42112,
233 42176,42208,42240,42272,42304,42336,42368,42400,42464,42496,42528,42560,42624,42656,42688,42720,
234 42784,42816,42848,42880,42912,42944,42976,43008,43072,43104,43136,43168,43232,43264,43296,43328,
235 43392,43424,43456,43488,43552,43584,43616,43648,43712,43744,43776,43808,43872,43904,43936,43968,
236 44032,44064,44096,44128,44192,44224,44256,44288,44352,44384,44416,44448,44512,44544,44576,44608,
237 44672,44704,44736,44768,44832,44864,44896,44928,44992,45024,45056,45088,45152,45184,45216,45248,
238 45312,45344,45376,45408,45472,45504,45536,45568,45632,45664,45728,45760,45792,45824,45888,45920,
239 45984,46016,46048,46080,46144,46176,46208,46240,46304,46336,46368,46400,46464,46496,46528,46560,
240 46656,46688,46720,46752,46816,46848,46880,46912,46976,47008,47072,47104,47136,47168,47232,47264,
241 47328,47360,47392,47424,47488,47520,47552,47584,47648,47680,47744,47776,47808,47840,47904,47936,
242 48032,48064,48096,48128,48192,48224,48288,48320,48384,48416,48448,48480,48544,48576,48640,48672,
243 48736,48768,48800,48832,48896,48928,48992,49024,49088,49120,49152,49184,49248,49280,49344,49376,
244 49440,49472,49504,49536,49600,49632,49696,49728,49792,49824,49856,49888,49952,49984,50048,50080,
245 50144,50176,50208,50240,50304,50336,50400,50432,50496,50528,50560,50592,50656,50688,50752,50784,
246 50880,50912,50944,50976,51040,51072,51136,51168,51232,51264,51328,51360,51424,51456,51488,51520,
247 51616,51648,51680,51712,51776,51808,51872,51904,51968,52000,52064,52096,52160,52192,52224,52256,
248 52384,52416,52448,52480,52544,52576,52640,52672,52736,52768,52832,52864,52928,52960,52992,53024,
249 53120,53152,53216,53248,53312,53344,53408,53440,53504,53536,53600,53632,53696,53728,53792,53824,
250 53920,53952,54016,54048,54112,54144,54208,54240,54304,54336,54400,54432,54496,54528,54592,54624,
251 54688,54720,54784,54816,54880,54912,54976,55008,55072,55104,55168,55200,55264,55296,55360,55392,
252 55488,55520,55584,55616,55680,55712,55776,55808,55872,55936,55968,56032,56064,56128,56160,56224,
253 56288,56320,56384,56416,56480,56512,56576,56608,56672,56736,56768,56832,56864,56928,56960,57024,
254 57120,57152,57216,57248,57312,57376,57408,57472,57536,57568,57632,57664,57728,57792,57824,57888,
255 57952,57984,58048,58080,58144,58208,58240,58304,58368,58400,58464,58496,58560,58624,58656,58720,
256 58784,58816,58880,58912,58976,59040,59072,59136,59200,59232,59296,59328,59392,59456,59488,59552,
257 59648,59680,59744,59776,59840,59904,59936,60000,60064,60128,60160,60224,60288,60320,60384,60416,
258 60512,60544,60608,60640,60704,60768,60800,60864,60928,60992,61024,61088,61152,61184,61248,61280,
259 61376,61408,61472,61536,61600,61632,61696,61760,61824,61856,61920,61984,62048,62080,62144,62208,
260 62272,62304,62368,62432,62496,62528,62592,62656,62720,62752,62816,62880,62944,62976,63040,63104,
261 63200,63232,63296,63360,63424,63456,63520,63584,63648,63680,63744,63808,63872,63904,63968,64032,
262 64096,64128,64192,64256,64320,64352,64416,64480,64544,64608,64672,64704,64768,64832,64896,64928,
263 65024,65056,65120,65184,65248,65312,65376,65408,65504,65536,65600,65664,65728,65792,65856,65888,
264 65984,66016,66080,66144,66208,66272,66336,66368,66464,66496,66560,66624,66688,66752,66816,66848,
265 66944,66976,67040,67104,67168,67232,67296,67328,67424,67456,67520,67584,67648,67712,67776,67808,
266 67904,67936,68000,68064,68128,68192,68256,68288,68384,68448,68512,68544,68640,68672,68736,68800,
267 68896,68928,68992,69056,69120,69184,69248,69280,69376,69440,69504,69536,69632,69664,69728,69792,
268 69920,69952,70016,70080,70144,70208,70272,70304,70400,70464,70528,70560,70656,70688,70752,70816,
269 70912,70976,71040,71104,71136,71232,71264,71360,71424,71488,71552,71616,71648,71744,71776,71872,
270 71968,72032,72096,72160,72192,72288,72320,72416,72480,72544,72608,72672,72704,72800,72832,72928,
271 72992,73056,73120,73184,73216,73312,73344,73440,73504,73568,73632,73696,73728,73824,73856,73952,
272 74080,74144,74208,74272,74304,74400,74432,74528,74592,74656,74720,74784,74816,74912,74944,75040,
273 75136,75200,75264,75328,75360,75456,75488,75584,75648,75712,75776,75840,75872,75968,76000,76096,
274 76224,76288,76352,76416,76448,76544,76576,76672,76736,76800,76864,76928,77024,77120,77152,77248,
275 77344,77408,77472,77536,77568,77664,77696,77792,77856,77920,77984,78048,78144,78240,78272,78368,
276 78464,78528,78592,78656,78688,78784,78816,78912,78976,79040,79104,79168,79264,79360,79392,79488,
277 79616,79680,79744,79808,79840,79936,79968,80064,80128,80192,80256,80320,80416,80512,80544,80640,
278 80768,80832,80896,80960,80992,81088,81120,81216,81280,81344,81408,81472,81568,81664,81696,81792,
279 81952,82016,82080,82144,82176,82272,82304,82400,82464,82528,82592,82656,82752,82848,82880,82976
280 };
281
282 // extract the block (octave) first
283 uint32_t block = bitfield(block_freq, 10, 3);
284
285 // the keycode (bits 6-9) is "gappy", mapping 12 values over 16 in each
286 // octave; to correct for this, we multiply the 4-bit value by 3/4 (or
287 // rather subtract 1/4); note that a (invalid) value of 15 will bleed into
288 // the next octave -- this is confirmed
289 uint32_t adjusted_code = bitfield(block_freq, 6, 4) - bitfield(block_freq, 8, 2);
290
291 // now re-insert the 6-bit fraction
292 int32_t eff_freq = (adjusted_code << 6) | bitfield(block_freq, 0, 6);
293
294 // now that the gaps are removed, add the delta
295 eff_freq += delta;
296
297 // handle over/underflow by adjusting the block:
298 if (uint32_t(eff_freq) >= 768)
299 {
300 // minimum delta is -512 (PM), so we can only underflow by 1 octave
301 if (eff_freq < 0)
302 {
303 eff_freq += 768;
304 if (block-- == 0)
305 return s_phase_step[0] >> 7;
306 }
307
308 // maximum delta is +512+608 (PM+detune), so we can overflow by up to 2 octaves
309 else
310 {
311 eff_freq -= 768;
312 if (eff_freq >= 768)
313 block++, eff_freq -= 768;
314 if (block++ >= 7)
315 return s_phase_step[767];
316 }
317 }
318
319 // look up the phase shift for the key code, then shift by octave
320 return s_phase_step[eff_freq] >> (block ^ 7);
321 }
322
323
324 //-------------------------------------------------
325 // opn_lfo_pm_phase_adjustment - given the 7 most
326 // significant frequency number bits, plus a 3-bit
327 // PM depth value and a signed 5-bit raw PM value,
328 // return a signed PM adjustment to the frequency;
329 // algorithm written to match Nuked behavior
330 //-------------------------------------------------
331
332 inline int32_t opn_lfo_pm_phase_adjustment(uint32_t fnum_bits, uint32_t pm_sensitivity, int32_t lfo_raw_pm)
333 {
334 // this table encodes 2 shift values to apply to the top 7 bits
335 // of fnum; it is effectively a cheap multiply by a constant
336 // value containing 0-2 bits
337 static uint8_t const s_lfo_pm_shifts[8][8] =
338 {
339 { 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77 },
340 { 0x77, 0x77, 0x77, 0x77, 0x72, 0x72, 0x72, 0x72 },
341 { 0x77, 0x77, 0x77, 0x72, 0x72, 0x72, 0x17, 0x17 },
342 { 0x77, 0x77, 0x72, 0x72, 0x17, 0x17, 0x12, 0x12 },
343 { 0x77, 0x77, 0x72, 0x17, 0x17, 0x17, 0x12, 0x07 },
344 { 0x77, 0x77, 0x17, 0x12, 0x07, 0x07, 0x02, 0x01 },
345 { 0x77, 0x77, 0x17, 0x12, 0x07, 0x07, 0x02, 0x01 },
346 { 0x77, 0x77, 0x17, 0x12, 0x07, 0x07, 0x02, 0x01 }
347 };
348
349 // look up the relevant shifts
350 int32_t abs_pm = (lfo_raw_pm < 0) ? -lfo_raw_pm : lfo_raw_pm;
351 uint32_t const shifts = s_lfo_pm_shifts[pm_sensitivity][bitfield(abs_pm, 0, 3)];
352
353 // compute the adjustment
354 int32_t adjust = (fnum_bits >> bitfield(shifts, 0, 4)) + (fnum_bits >> bitfield(shifts, 4, 4));
355 if (pm_sensitivity > 5)
356 adjust <<= pm_sensitivity - 5;
357 adjust >>= 2;
358
359 // every 16 cycles it inverts sign
360 return (lfo_raw_pm < 0) ? -adjust : adjust;
361 }
362
363
364
365 //*********************************************************
366 // FM OPERATOR
367 //*********************************************************
368
369 //-------------------------------------------------
370 // fm_operator - constructor
371 //-------------------------------------------------
372
373 template<class RegisterType>
374 fm_operator<RegisterType>::fm_operator(fm_engine_base<RegisterType> &owner, uint32_t opoffs) :
375 m_choffs(0),
376 m_opoffs(opoffs),
377 m_phase(0),
378 m_env_attenuation(0x3ff),
379 m_env_state(EG_RELEASE),
380 m_ssg_inverted(false),
381 m_key_state(0),
382 m_keyon_live(0),
383 m_regs(owner.regs()),
384 m_owner(owner)
385 {
386 }
387
388
389 //-------------------------------------------------
390 // reset - reset the channel state
391 //-------------------------------------------------
392
393 template<class RegisterType>
394 void fm_operator<RegisterType>::reset()
395 {
396 // reset our data
397 m_phase = 0;
398 m_env_attenuation = 0x3ff;
399 m_env_state = EG_RELEASE;
400 m_ssg_inverted = 0;
401 m_key_state = 0;
402 m_keyon_live = 0;
403 }
404
405
406 //-------------------------------------------------
407 // save_restore - save or restore the data
408 //-------------------------------------------------
409
410 template<class RegisterType>
411 void fm_operator<RegisterType>::save_restore(ymfm_saved_state &state)
412 {
413 state.save_restore(m_phase);
414 state.save_restore(m_env_attenuation);
415 state.save_restore(m_env_state);
416 state.save_restore(m_ssg_inverted);
417 state.save_restore(m_key_state);
418 state.save_restore(m_keyon_live);
419 }
420
421
422 //-------------------------------------------------
423 // prepare - prepare for clocking
424 //-------------------------------------------------
425
426 template<class RegisterType>
427 bool fm_operator<RegisterType>::prepare()
428 {
429 // cache the data
430 m_regs.cache_operator_data(m_choffs, m_opoffs, m_cache);
431
432 // clock the key state
433 clock_keystate(uint32_t(m_keyon_live != 0));
434 m_keyon_live &= ~(1 << KEYON_CSM);
435
436 // we're active until we're quiet after the release
437 return (m_env_state != (RegisterType::EG_HAS_REVERB ? EG_REVERB : EG_RELEASE) || m_env_attenuation < EG_QUIET);
438 }
439
440
441 //-------------------------------------------------
442 // clock - master clocking function
443 //-------------------------------------------------
444
445 template<class RegisterType>
446 void fm_operator<RegisterType>::clock(uint32_t env_counter, int32_t lfo_raw_pm)
447 {
448 // clock the SSG-EG state (OPN/OPNA)
449 if (m_regs.op_ssg_eg_enable(m_opoffs))
450 clock_ssg_eg_state();
451 else
452 m_ssg_inverted = false;
453
454 // clock the envelope if on an envelope cycle; env_counter is a x.2 value
455 if (bitfield(env_counter, 0, 2) == 0)
456 clock_envelope(env_counter >> 2);
457
458 // clock the phase
459 clock_phase(lfo_raw_pm);
460 }
461
462
463 //-------------------------------------------------
464 // compute_volume - compute the 14-bit signed
465 // volume of this operator, given a phase
466 // modulation and an AM LFO offset
467 //-------------------------------------------------
468
469 template<class RegisterType>
470 int32_t fm_operator<RegisterType>::compute_volume(uint32_t phase, uint32_t am_offset) const
471 {
472 // the low 10 bits of phase represents a full 2*PI period over
473 // the full sin wave
474
475 // early out if the envelope is effectively off
476 if (m_env_attenuation > EG_QUIET)
477 return 0;
478
479 // get the absolute value of the sin, as attenuation, as a 4.8 fixed point value
480 uint32_t sin_attenuation = m_cache.waveform[phase & (RegisterType::WAVEFORM_LENGTH - 1)];
481
482 // get the attenuation from the evelope generator as a 4.6 value, shifted up to 4.8
483 uint32_t env_attenuation = envelope_attenuation(am_offset) << 2;
484
485 // combine into a 5.8 value, then convert from attenuation to 13-bit linear volume
486 int32_t result = attenuation_to_volume((sin_attenuation & 0x7fff) + env_attenuation);
487
488 // negate if in the negative part of the sin wave (sign bit gives 14 bits)
489 return bitfield(sin_attenuation, 15) ? -result : result;
490 }
491
492
493 //-------------------------------------------------
494 // compute_noise_volume - compute the 14-bit
495 // signed noise volume of this operator, given a
496 // noise input value and an AM offset
497 //-------------------------------------------------
498
499 template<class RegisterType>
500 int32_t fm_operator<RegisterType>::compute_noise_volume(uint32_t am_offset) const
501 {
502 // application manual says the logarithmic transform is not applied here, so we
503 // just use the raw envelope attenuation, inverted (since 0 attenuation should be
504 // maximum), and shift it up from a 10-bit value to an 11-bit value
505 int32_t result = (envelope_attenuation(am_offset) ^ 0x3ff) << 1;
506
507 // QUESTION: is AM applied still?
508
509 // negate based on the noise state
510 return bitfield(m_regs.noise_state(), 0) ? -result : result;
511 }
512
513
514 //-------------------------------------------------
515 // keyonoff - signal a key on/off event
516 //-------------------------------------------------
517
518 template<class RegisterType>
519 void fm_operator<RegisterType>::keyonoff(uint32_t on, keyon_type type)
520 {
521 m_keyon_live = (m_keyon_live & ~(1 << int(type))) | (bitfield(on, 0) << int(type));
522 }
523
524
525 //-------------------------------------------------
526 // start_attack - start the attack phase; called
527 // when a keyon happens or when an SSG-EG cycle
528 // is complete and restarts
529 //-------------------------------------------------
530
531 template<class RegisterType>
532 void fm_operator<RegisterType>::start_attack(bool is_restart)
533 {
534 // don't change anything if already in attack state
535 if (m_env_state == EG_ATTACK)
536 return;
537 m_env_state = EG_ATTACK;
538
539 // generally not inverted at start, except if SSG-EG is enabled and
540 // one of the inverted modes is specified; leave this alone on a
541 // restart, as it is managed by the clock_ssg_eg_state() code
542 if (RegisterType::EG_HAS_SSG && !is_restart)
543 m_ssg_inverted = m_regs.op_ssg_eg_enable(m_opoffs) & bitfield(m_regs.op_ssg_eg_mode(m_opoffs), 2);
544
545 // reset the phase when we start an attack due to a key on
546 // (but not when due to an SSG-EG restart except in certain cases
547 // managed directly by the SSG-EG code)
548 if (!is_restart)
549 m_phase = 0;
550
551 // if the attack rate >= 62 then immediately go to max attenuation
552 if (m_cache.eg_rate[EG_ATTACK] >= 62)
553 m_env_attenuation = 0;
554 }
555
556
557 //-------------------------------------------------
558 // start_release - start the release phase;
559 // called when a keyoff happens
560 //-------------------------------------------------
561
562 template<class RegisterType>
563 void fm_operator<RegisterType>::start_release()
564 {
565 // don't change anything if already in release state
566 if (m_env_state >= EG_RELEASE)
567 return;
568 m_env_state = EG_RELEASE;
569
570 // if attenuation if inverted due to SSG-EG, snap the inverted attenuation
571 // as the starting point
572 if (RegisterType::EG_HAS_SSG && m_ssg_inverted)
573 {
574 m_env_attenuation = (0x200 - m_env_attenuation) & 0x3ff;
575 m_ssg_inverted = false;
576 }
577 }
578
579
580 //-------------------------------------------------
581 // clock_keystate - clock the keystate to match
582 // the incoming keystate
583 //-------------------------------------------------
584
585 template<class RegisterType>
586 void fm_operator<RegisterType>::clock_keystate(uint32_t keystate)
587 {
588 assert(keystate == 0 || keystate == 1);
589
590 // has the key changed?
591 if ((keystate ^ m_key_state) != 0)
592 {
593 m_key_state = keystate;
594
595 // if the key has turned on, start the attack
596 if (keystate != 0)
597 {
598 // OPLL has a DP ("depress"?) state to bring the volume
599 // down before starting the attack
600 if (RegisterType::EG_HAS_DEPRESS && m_env_attenuation < 0x200)
601 m_env_state = EG_DEPRESS;
602 else
603 start_attack();
604 }
605
606 // otherwise, start the release
607 else
608 start_release();
609 }
610 }
611
612
613 //-------------------------------------------------
614 // clock_ssg_eg_state - clock the SSG-EG state;
615 // should only be called if SSG-EG is enabled
616 //-------------------------------------------------
617
618 template<class RegisterType>
619 void fm_operator<RegisterType>::clock_ssg_eg_state()
620 {
621 // work only happens once the attenuation crosses above 0x200
622 if (!bitfield(m_env_attenuation, 9))
623 return;
624
625 // 8 SSG-EG modes:
626 // 000: repeat normally
627 // 001: run once, hold low
628 // 010: repeat, alternating between inverted/non-inverted
629 // 011: run once, hold high
630 // 100: inverted repeat normally
631 // 101: inverted run once, hold low
632 // 110: inverted repeat, alternating between inverted/non-inverted
633 // 111: inverted run once, hold high
634 uint32_t mode = m_regs.op_ssg_eg_mode(m_opoffs);
635
636 // hold modes (1/3/5/7)
637 if (bitfield(mode, 0))
638 {
639 // set the inverted flag to the end state (0 for modes 1/7, 1 for modes 3/5)
640 m_ssg_inverted = bitfield(mode, 2) ^ bitfield(mode, 1);
641
642 // if holding, force the attenuation to the expected value once we're
643 // past the attack phase
644 if (m_env_state != EG_ATTACK)
645 m_env_attenuation = m_ssg_inverted ? 0x200 : 0x3ff;
646 }
647
648 // continuous modes (0/2/4/6)
649 else
650 {
651 // toggle invert in alternating mode (even in attack state)
652 m_ssg_inverted ^= bitfield(mode, 1);
653
654 // restart attack if in decay/sustain states
655 if (m_env_state == EG_DECAY || m_env_state == EG_SUSTAIN)
656 start_attack(true);
657
658 // phase is reset to 0 in modes 0/4
659 if (bitfield(mode, 1) == 0)
660 m_phase = 0;
661 }
662
663 // in all modes, once we hit release state, attenuation is forced to maximum
664 if (m_env_state == EG_RELEASE)
665 m_env_attenuation = 0x3ff;
666 }
667
668
669 //-------------------------------------------------
670 // clock_envelope - clock the envelope state
671 // according to the given count
672 //-------------------------------------------------
673
674 template<class RegisterType>
675 void fm_operator<RegisterType>::clock_envelope(uint32_t env_counter)
676 {
677 // handle attack->decay transitions
678 if (m_env_state == EG_ATTACK && m_env_attenuation == 0)
679 m_env_state = EG_DECAY;
680
681 // handle decay->sustain transitions; it is important to do this immediately
682 // after the attack->decay transition above in the event that the sustain level
683 // is set to 0 (in which case we will skip right to sustain without doing any
684 // decay); as an example where this can be heard, check the cymbals sound
685 // in channel 0 of shinobi's test mode sound #5
686 if (m_env_state == EG_DECAY && m_env_attenuation >= m_cache.eg_sustain)
687 m_env_state = EG_SUSTAIN;
688
689 // fetch the appropriate 6-bit rate value from the cache
690 uint32_t rate = m_cache.eg_rate[m_env_state];
691
692 // compute the rate shift value; this is the shift needed to
693 // apply to the env_counter such that it becomes a 5.11 fixed
694 // point number
695 uint32_t rate_shift = rate >> 2;
696 env_counter <<= rate_shift;
697
698 // see if the fractional part is 0; if not, it's not time to clock
699 if (bitfield(env_counter, 0, 11) != 0)
700 return;
701
702 // determine the increment based on the non-fractional part of env_counter
703 uint32_t relevant_bits = bitfield(env_counter, (rate_shift <= 11) ? 11 : rate_shift, 3);
704 uint32_t increment = attenuation_increment(rate, relevant_bits);
705
706 // attack is the only one that increases
707 if (m_env_state == EG_ATTACK)
708 {
709 // glitch means that attack rates of 62/63 don't increment if
710 // changed after the initial key on (where they are handled
711 // specially); nukeykt confirms this happens on OPM, OPN, OPL/OPLL
712 // at least so assuming it is true for everyone
713 if (rate < 62)
714 m_env_attenuation += (~m_env_attenuation * increment) >> 4;
715 }
716
717 // all other cases are similar
718 else
719 {
720 // non-SSG-EG cases just apply the increment
721 if (!m_regs.op_ssg_eg_enable(m_opoffs))
722 m_env_attenuation += increment;
723
724 // SSG-EG only applies if less than mid-point, and then at 4x
725 else if (m_env_attenuation < 0x200)
726 m_env_attenuation += 4 * increment;
727
728 // clamp the final attenuation
729 if (m_env_attenuation >= 0x400)
730 m_env_attenuation = 0x3ff;
731
732 // transition from depress to attack
733 if (RegisterType::EG_HAS_DEPRESS && m_env_state == EG_DEPRESS && m_env_attenuation >= 0x200)
734 start_attack();
735
736 // transition from release to reverb, should switch at -18dB
737 if (RegisterType::EG_HAS_REVERB && m_env_state == EG_RELEASE && m_env_attenuation >= 0xc0)
738 m_env_state = EG_REVERB;
739 }
740 }
741
742
743 //-------------------------------------------------
744 // clock_phase - clock the 10.10 phase value; the
745 // OPN version of the logic has been verified
746 // against the Nuked phase generator
747 //-------------------------------------------------
748
749 template<class RegisterType>
750 void fm_operator<RegisterType>::clock_phase(int32_t lfo_raw_pm)
751 {
752 // read from the cache, or recalculate if PM active
753 uint32_t phase_step = m_cache.phase_step;
754 if (phase_step == opdata_cache::PHASE_STEP_DYNAMIC)
755 phase_step = m_regs.compute_phase_step(m_choffs, m_opoffs, m_cache, lfo_raw_pm);
756
757 // finally apply the step to the current phase value
758 m_phase += phase_step;
759 }
760
761
762 //-------------------------------------------------
763 // envelope_attenuation - return the effective
764 // attenuation of the envelope
765 //-------------------------------------------------
766
767 template<class RegisterType>
768 uint32_t fm_operator<RegisterType>::envelope_attenuation(uint32_t am_offset) const
769 {
770 uint32_t result = m_env_attenuation >> m_cache.eg_shift;
771
772 // invert if necessary due to SSG-EG
773 if (RegisterType::EG_HAS_SSG && m_ssg_inverted)
774 result = (0x200 - result) & 0x3ff;
775
776 // add in LFO AM modulation
777 if (m_regs.op_lfo_am_enable(m_opoffs))
778 result += am_offset;
779
780 // add in total level and KSL from the cache
781 result += m_cache.total_level;
782
783 // clamp to max, apply shift, and return
784 return std::min<uint32_t>(result, 0x3ff);
785 }
786
787
788
789 //*********************************************************
790 // FM CHANNEL
791 //*********************************************************
792
793 //-------------------------------------------------
794 // fm_channel - constructor
795 //-------------------------------------------------
796
797 template<class RegisterType>
798 fm_channel<RegisterType>::fm_channel(fm_engine_base<RegisterType> &owner, uint32_t choffs) :
799 m_choffs(choffs),
800 m_feedback{ 0, 0 },
801 m_feedback_in(0),
802 m_op{ nullptr, nullptr, nullptr, nullptr },
803 m_regs(owner.regs()),
804 m_owner(owner)
805 {
806 }
807
808
809 //-------------------------------------------------
810 // reset - reset the channel state
811 //-------------------------------------------------
812
813 template<class RegisterType>
814 void fm_channel<RegisterType>::reset()
815 {
816 // reset our data
817 m_feedback[0] = m_feedback[1] = 0;
818 m_feedback_in = 0;
819 }
820
821
822 //-------------------------------------------------
823 // save_restore - save or restore the data
824 //-------------------------------------------------
825
826 template<class RegisterType>
827 void fm_channel<RegisterType>::save_restore(ymfm_saved_state &state)
828 {
829 state.save_restore(m_feedback[0]);
830 state.save_restore(m_feedback[1]);
831 state.save_restore(m_feedback_in);
832 }
833
834
835 //-------------------------------------------------
836 // keyonoff - signal key on/off to our operators
837 //-------------------------------------------------
838
839 template<class RegisterType>
840 void fm_channel<RegisterType>::keyonoff(uint32_t states, keyon_type type, uint32_t chnum)
841 {
842 for (uint32_t opnum = 0; opnum < array_size(m_op); opnum++)
843 if (m_op[opnum] != nullptr)
844 m_op[opnum]->keyonoff(bitfield(states, opnum), type);
845
846 if (debug::LOG_KEYON_EVENTS && ((debug::GLOBAL_FM_CHANNEL_MASK >> chnum) & 1) != 0)
847 for (uint32_t opnum = 0; opnum < array_size(m_op); opnum++)
848 if (m_op[opnum] != nullptr)
849 debug::log_keyon("%c%s\n", bitfield(states, opnum) ? '+' : '-', m_regs.log_keyon(m_choffs, m_op[opnum]->opoffs()).c_str());
850 }
851
852
853 //-------------------------------------------------
854 // prepare - prepare for clocking
855 //-------------------------------------------------
856
857 template<class RegisterType>
858 bool fm_channel<RegisterType>::prepare()
859 {
860 uint32_t active_mask = 0;
861
862 // prepare all operators and determine if they are active
863 for (uint32_t opnum = 0; opnum < array_size(m_op); opnum++)
864 if (m_op[opnum] != nullptr)
865 if (m_op[opnum]->prepare())
866 active_mask |= 1 << opnum;
867
868 return (active_mask != 0);
869 }
870
871
872 //-------------------------------------------------
873 // clock - master clock of all operators
874 //-------------------------------------------------
875
876 template<class RegisterType>
877 void fm_channel<RegisterType>::clock(uint32_t env_counter, int32_t lfo_raw_pm)
878 {
879 // clock the feedback through
880 m_feedback[0] = m_feedback[1];
881 m_feedback[1] = m_feedback_in;
882
883 for (uint32_t opnum = 0; opnum < array_size(m_op); opnum++)
884 if (m_op[opnum] != nullptr)
885 m_op[opnum]->clock(env_counter, lfo_raw_pm);
886
887 /*
888 useful temporary code for envelope debugging
889 if (m_choffs == 0x101)
890 {
891 for (uint32_t opnum = 0; opnum < array_size(m_op); opnum++)
892 {
893 auto &op = *m_op[((opnum & 1) << 1) | ((opnum >> 1) & 1)];
894 printf(" %c%03X%c%c ",
895 "PADSRV"[op.debug_eg_state()],
896 op.debug_eg_attenuation(),
897 op.debug_ssg_inverted() ? '-' : '+',
898 m_regs.op_ssg_eg_enable(op.opoffs()) ? '0' + m_regs.op_ssg_eg_mode(op.opoffs()) : ' ');
899 }
900 printf(" -- ");
901 }
902 */
903 }
904
905
906 //-------------------------------------------------
907 // output_2op - combine 4 operators according to
908 // the specified algorithm, returning a sum
909 // according to the rshift and clipmax parameters,
910 // which vary between different implementations
911 //-------------------------------------------------
912
913 template<class RegisterType>
914 void fm_channel<RegisterType>::output_2op(output_data &output, uint32_t rshift, int32_t clipmax) const
915 {
916 // The first 2 operators should be populated
917 assert(m_op[0] != nullptr);
918 assert(m_op[1] != nullptr);
919
920 // AM amount is the same across all operators; compute it once
921 uint32_t am_offset = m_regs.lfo_am_offset(m_choffs);
922
923 // operator 1 has optional self-feedback
924 int32_t opmod = 0;
925 uint32_t feedback = m_regs.ch_feedback(m_choffs);
926 if (feedback != 0)
927 opmod = (m_feedback[0] + m_feedback[1]) >> (10 - feedback);
928
929 // compute the 14-bit volume/value of operator 1 and update the feedback
930 int32_t op1value = m_feedback_in = m_op[0]->compute_volume(m_op[0]->phase() + opmod, am_offset);
931
932 // now that the feedback has been computed, skip the rest if all volumes
933 // are clear; no need to do all this work for nothing
934 if (m_regs.ch_output_any(m_choffs) == 0)
935 return;
936
937 // Algorithms for two-operator case:
938 // 0: O1 -> O2 -> out
939 // 1: (O1 + O2) -> out
940 int32_t result;
941 if (bitfield(m_regs.ch_algorithm(m_choffs), 0) == 0)
942 {
943 // some OPL chips use the previous sample for modulation instead of
944 // the current sample
945 opmod = (RegisterType::MODULATOR_DELAY ? m_feedback[1] : op1value) >> 1;
946 result = m_op[1]->compute_volume(m_op[1]->phase() + opmod, am_offset) >> rshift;
947 }
948 else
949 {
950 result = (RegisterType::MODULATOR_DELAY ? m_feedback[1] : op1value) >> rshift;
951 result += m_op[1]->compute_volume(m_op[1]->phase(), am_offset) >> rshift;
952 int32_t clipmin = -clipmax - 1;
953 result = clamp(result, clipmin, clipmax);
954 }
955
956 // add to the output
957 add_to_output(m_choffs, output, result);
958 }
959
960
961 //-------------------------------------------------
962 // output_4op - combine 4 operators according to
963 // the specified algorithm, returning a sum
964 // according to the rshift and clipmax parameters,
965 // which vary between different implementations
966 //-------------------------------------------------
967
968 template<class RegisterType>
969 void fm_channel<RegisterType>::output_4op(output_data &output, uint32_t rshift, int32_t clipmax) const
970 {
971 // all 4 operators should be populated
972 assert(m_op[0] != nullptr);
973 assert(m_op[1] != nullptr);
974 assert(m_op[2] != nullptr);
975 assert(m_op[3] != nullptr);
976
977 // AM amount is the same across all operators; compute it once
978 uint32_t am_offset = m_regs.lfo_am_offset(m_choffs);
979
980 // operator 1 has optional self-feedback
981 int32_t opmod = 0;
982 uint32_t feedback = m_regs.ch_feedback(m_choffs);
983 if (feedback != 0)
984 opmod = (m_feedback[0] + m_feedback[1]) >> (10 - feedback);
985
986 // compute the 14-bit volume/value of operator 1 and update the feedback
987 int32_t op1value = m_feedback_in = m_op[0]->compute_volume(m_op[0]->phase() + opmod, am_offset);
988
989 // now that the feedback has been computed, skip the rest if all volumes
990 // are clear; no need to do all this work for nothing
991 if (m_regs.ch_output_any(m_choffs) == 0)
992 return;
993
994 // OPM/OPN offer 8 different connection algorithms for 4 operators,
995 // and OPL3 offers 4 more, which we designate here as 8-11.
996 //
997 // The operators are computed in order, with the inputs pulled from
998 // an array of values (opout) that is populated as we go:
999 // 0 = 0
1000 // 1 = O1
1001 // 2 = O2
1002 // 3 = O3
1003 // 4 = (O4)
1004 // 5 = O1+O2
1005 // 6 = O1+O3
1006 // 7 = O2+O3
1007 //
1008 // The s_algorithm_ops table describes the inputs and outputs of each
1009 // algorithm as follows:
1010 //
1011 // ---------x use opout[x] as operator 2 input
1012 // ------xxx- use opout[x] as operator 3 input
1013 // ---xxx---- use opout[x] as operator 4 input
1014 // --x------- include opout[1] in final sum
1015 // -x-------- include opout[2] in final sum
1016 // x--------- include opout[3] in final sum
1017 #define ALGORITHM(op2in, op3in, op4in, op1out, op2out, op3out) \
1018 ((op2in) | ((op3in) << 1) | ((op4in) << 4) | ((op1out) << 7) | ((op2out) << 8) | ((op3out) << 9))
1019 static uint16_t const s_algorithm_ops[8+4] =
1020 {
1021 ALGORITHM(1,2,3, 0,0,0), // 0: O1 -> O2 -> O3 -> O4 -> out (O4)
1022 ALGORITHM(0,5,3, 0,0,0), // 1: (O1 + O2) -> O3 -> O4 -> out (O4)
1023 ALGORITHM(0,2,6, 0,0,0), // 2: (O1 + (O2 -> O3)) -> O4 -> out (O4)
1024 ALGORITHM(1,0,7, 0,0,0), // 3: ((O1 -> O2) + O3) -> O4 -> out (O4)
1025 ALGORITHM(1,0,3, 0,1,0), // 4: ((O1 -> O2) + (O3 -> O4)) -> out (O2+O4)
1026 ALGORITHM(1,1,1, 0,1,1), // 5: ((O1 -> O2) + (O1 -> O3) + (O1 -> O4)) -> out (O2+O3+O4)
1027 ALGORITHM(1,0,0, 0,1,1), // 6: ((O1 -> O2) + O3 + O4) -> out (O2+O3+O4)
1028 ALGORITHM(0,0,0, 1,1,1), // 7: (O1 + O2 + O3 + O4) -> out (O1+O2+O3+O4)
1029 ALGORITHM(1,2,3, 0,0,0), // 8: O1 -> O2 -> O3 -> O4 -> out (O4) [same as 0]
1030 ALGORITHM(0,2,3, 1,0,0), // 9: (O1 + (O2 -> O3 -> O4)) -> out (O1+O4) [unique]
1031 ALGORITHM(1,0,3, 0,1,0), // 10: ((O1 -> O2) + (O3 -> O4)) -> out (O2+O4) [same as 4]
1032 ALGORITHM(0,2,0, 1,0,1) // 11: (O1 + (O2 -> O3) + O4) -> out (O1+O3+O4) [unique]
1033 };
1034 uint32_t algorithm_ops = s_algorithm_ops[m_regs.ch_algorithm(m_choffs)];
1035
1036 // populate the opout table
1037 int16_t opout[8];
1038 opout[0] = 0;
1039 opout[1] = op1value;
1040
1041 // compute the 14-bit volume/value of operator 2
1042 opmod = opout[bitfield(algorithm_ops, 0, 1)] >> 1;
1043 opout[2] = m_op[1]->compute_volume(m_op[1]->phase() + opmod, am_offset);
1044 opout[5] = opout[1] + opout[2];
1045
1046 // compute the 14-bit volume/value of operator 3
1047 opmod = opout[bitfield(algorithm_ops, 1, 3)] >> 1;
1048 opout[3] = m_op[2]->compute_volume(m_op[2]->phase() + opmod, am_offset);
1049 opout[6] = opout[1] + opout[3];
1050 opout[7] = opout[2] + opout[3];
1051
1052 // compute the 14-bit volume/value of operator 4; this could be a noise
1053 // value on the OPM; all algorithms consume OP4 output at a minimum
1054 int32_t result;
1055 if (m_regs.noise_enable() && m_choffs == 7)
1056 result = m_op[3]->compute_noise_volume(am_offset);
1057 else
1058 {
1059 opmod = opout[bitfield(algorithm_ops, 4, 3)] >> 1;
1060 result = m_op[3]->compute_volume(m_op[3]->phase() + opmod, am_offset);
1061 }
1062 result >>= rshift;
1063
1064 // optionally add OP1, OP2, OP3
1065 int32_t clipmin = -clipmax - 1;
1066 if (bitfield(algorithm_ops, 7) != 0)
1067 result = clamp(result + (opout[1] >> rshift), clipmin, clipmax);
1068 if (bitfield(algorithm_ops, 8) != 0)
1069 result = clamp(result + (opout[2] >> rshift), clipmin, clipmax);
1070 if (bitfield(algorithm_ops, 9) != 0)
1071 result = clamp(result + (opout[3] >> rshift), clipmin, clipmax);
1072
1073 // add to the output
1074 add_to_output(m_choffs, output, result);
1075 }
1076
1077
1078 //-------------------------------------------------
1079 // output_rhythm_ch6 - special case output
1080 // computation for OPL channel 6 in rhythm mode,
1081 // which outputs a Bass Drum instrument
1082 //-------------------------------------------------
1083
1084 template<class RegisterType>
1085 void fm_channel<RegisterType>::output_rhythm_ch6(output_data &output, uint32_t rshift, int32_t /*clipmax*/) const
1086 {
1087 // AM amount is the same across all operators; compute it once
1088 uint32_t am_offset = m_regs.lfo_am_offset(m_choffs);
1089
1090 // Bass Drum: this uses operators 12 and 15 (i.e., channel 6)
1091 // in an almost-normal way, except that if the algorithm is 1,
1092 // the first operator is ignored instead of added in
1093
1094 // operator 1 has optional self-feedback
1095 int32_t opmod = 0;
1096 uint32_t feedback = m_regs.ch_feedback(m_choffs);
1097 if (feedback != 0)
1098 opmod = (m_feedback[0] + m_feedback[1]) >> (10 - feedback);
1099
1100 // compute the 14-bit volume/value of operator 1 and update the feedback
1101 int32_t opout1 = m_feedback_in = m_op[0]->compute_volume(m_op[0]->phase() + opmod, am_offset);
1102
1103 // compute the 14-bit volume/value of operator 2, which is the result
1104 opmod = bitfield(m_regs.ch_algorithm(m_choffs), 0) ? 0 : (opout1 >> 1);
1105 int32_t result = m_op[1]->compute_volume(m_op[1]->phase() + opmod, am_offset) >> rshift;
1106
1107 // add to the output
1108 add_to_output(m_choffs, output, result * 2);
1109 }
1110
1111
1112 //-------------------------------------------------
1113 // output_rhythm_ch7 - special case output
1114 // computation for OPL channel 7 in rhythm mode,
1115 // which outputs High Hat and Snare Drum
1116 // instruments
1117 //-------------------------------------------------
1118
1119 template<class RegisterType>
1120 void fm_channel<RegisterType>::output_rhythm_ch7(uint32_t phase_select, output_data &output, uint32_t rshift, int32_t clipmax) const
1121 {
1122 // AM amount is the same across all operators; compute it once
1123 uint32_t am_offset = m_regs.lfo_am_offset(m_choffs);
1124 uint32_t noise_state = bitfield(m_regs.noise_state(), 0);
1125
1126 // High Hat: this uses the envelope from operator 13 (channel 7),
1127 // and a combination of noise and the operator 13/17 phase select
1128 // to compute the phase
1129 uint32_t phase = (phase_select << 9) | (0xd0 >> (2 * (noise_state ^ phase_select)));
1130 int32_t result = m_op[0]->compute_volume(phase, am_offset) >> rshift;
1131
1132 // Snare Drum: this uses the envelope from operator 16 (channel 7),
1133 // and a combination of noise and operator 13 phase to pick a phase
1134 uint32_t op13phase = m_op[0]->phase();
1135 phase = (0x100 << bitfield(op13phase, 8)) ^ (noise_state << 8);
1136 result += m_op[1]->compute_volume(phase, am_offset) >> rshift;
1137 result = clamp(result, -clipmax - 1, clipmax);
1138
1139 // add to the output
1140 add_to_output(m_choffs, output, result * 2);
1141 }
1142
1143
1144 //-------------------------------------------------
1145 // output_rhythm_ch8 - special case output
1146 // computation for OPL channel 8 in rhythm mode,
1147 // which outputs Tom Tom and Top Cymbal instruments
1148 //-------------------------------------------------
1149
1150 template<class RegisterType>
1151 void fm_channel<RegisterType>::output_rhythm_ch8(uint32_t phase_select, output_data &output, uint32_t rshift, int32_t clipmax) const
1152 {
1153 // AM amount is the same across all operators; compute it once
1154 uint32_t am_offset = m_regs.lfo_am_offset(m_choffs);
1155
1156 // Tom Tom: this is just a single operator processed normally
1157 int32_t result = m_op[0]->compute_volume(m_op[0]->phase(), am_offset) >> rshift;
1158
1159 // Top Cymbal: this uses the envelope from operator 17 (channel 8),
1160 // and the operator 13/17 phase select to compute the phase
1161 uint32_t phase = 0x100 | (phase_select << 9);
1162 result += m_op[1]->compute_volume(phase, am_offset) >> rshift;
1163 result = clamp(result, -clipmax - 1, clipmax);
1164
1165 // add to the output
1166 add_to_output(m_choffs, output, result * 2);
1167 }
1168
1169
1170
1171 //*********************************************************
1172 // FM ENGINE BASE
1173 //*********************************************************
1174
1175 //-------------------------------------------------
1176 // fm_engine_base - constructor
1177 //-------------------------------------------------
1178
1179 template<class RegisterType>
1180 fm_engine_base<RegisterType>::fm_engine_base(ymfm_interface &intf) :
1181 m_intf(intf),
1182 m_env_counter(0),
1183 m_status(0),
1184 m_clock_prescale(RegisterType::DEFAULT_PRESCALE),
1185 m_irq_mask(STATUS_TIMERA | STATUS_TIMERB),
1186 m_irq_state(0),
1187 m_timer_running{0,0},
1188 m_active_channels(ALL_CHANNELS),
1189 m_modified_channels(ALL_CHANNELS),
1190 m_prepare_count(0)
1191 {
1192 // inform the interface of their engine
1193 m_intf.m_engine = this;
1194
1195 // create the channels
1196 for (uint32_t chnum = 0; chnum < CHANNELS; chnum++)
1197 m_channel[chnum] = std::make_unique<fm_channel<RegisterType>>(*this, RegisterType::channel_offset(chnum));
1198
1199 // create the operators
1200 for (uint32_t opnum = 0; opnum < OPERATORS; opnum++)
1201 m_operator[opnum] = std::make_unique<fm_operator<RegisterType>>(*this, RegisterType::operator_offset(opnum));
1202
1203 #if (DEBUG_LOG_WAVFILES)
1204 for (uint32_t chnum = 0; chnum < CHANNELS; chnum++)
1205 m_wavfile[chnum].set_index(chnum);
1206 #endif
1207
1208 // do the initial operator assignment
1209 assign_operators();
1210 }
1211
1212
1213 //-------------------------------------------------
1214 // reset - reset the overall state
1215 //-------------------------------------------------
1216
1217 template<class RegisterType>
1218 void fm_engine_base<RegisterType>::reset()
1219 {
1220 // reset all status bits
1221 set_reset_status(0, 0xff);
1222
1223 // register type-specific initialization
1224 m_regs.reset();
1225
1226 // explicitly write to the mode register since it has side-effects
1227 // QUESTION: old cores initialize this to 0x30 -- who is right?
1228 write(RegisterType::REG_MODE, 0);
1229
1230 // reset the channels
1231 for (auto &chan : m_channel)
1232 chan->reset();
1233
1234 // reset the operators
1235 for (auto &op : m_operator)
1236 op->reset();
1237 }
1238
1239
1240 //-------------------------------------------------
1241 // save_restore - save or restore the data
1242 //-------------------------------------------------
1243
1244 template<class RegisterType>
1245 void fm_engine_base<RegisterType>::save_restore(ymfm_saved_state &state)
1246 {
1247 // save our data
1248 state.save_restore(m_env_counter);
1249 state.save_restore(m_status);
1250 state.save_restore(m_clock_prescale);
1251 state.save_restore(m_irq_mask);
1252 state.save_restore(m_irq_state);
1253 state.save_restore(m_timer_running[0]);
1254 state.save_restore(m_timer_running[1]);
1255 state.save_restore(m_total_clocks);
1256
1257 // save the register/family data
1258 m_regs.save_restore(state);
1259
1260 // save channel data
1261 for (uint32_t chnum = 0; chnum < CHANNELS; chnum++)
1262 m_channel[chnum]->save_restore(state);
1263
1264 // save operator data
1265 for (uint32_t opnum = 0; opnum < OPERATORS; opnum++)
1266 m_operator[opnum]->save_restore(state);
1267
1268 // invalidate any caches
1269 invalidate_caches();
1270 }
1271
1272
1273 //-------------------------------------------------
1274 // clock - iterate over all channels, clocking
1275 // them forward one step
1276 //-------------------------------------------------
1277
1278 template<class RegisterType>
1279 uint32_t fm_engine_base<RegisterType>::clock(uint32_t chanmask)
1280 {
1281 // update the clock counter
1282 m_total_clocks++;
1283
1284 // if something was modified, prepare
1285 // also prepare every 4k samples to catch ending notes
1286 if (m_modified_channels != 0 || m_prepare_count++ >= 4096)
1287 {
1288 // reassign operators to channels if dynamic
1289 if (RegisterType::DYNAMIC_OPS)
1290 assign_operators();
1291
1292 // call each channel to prepare
1293 m_active_channels = 0;
1294 for (uint32_t chnum = 0; chnum < CHANNELS; chnum++)
1295 if (bitfield(chanmask, chnum))
1296 if (m_channel[chnum]->prepare())
1297 m_active_channels |= 1 << chnum;
1298
1299 // reset the modified channels and prepare count
1300 m_modified_channels = m_prepare_count = 0;
1301 }
1302
1303 // if the envelope clock divider is 1, just increment by 4;
1304 // otherwise, increment by 1 and manually wrap when we reach the divide count
1305 if (RegisterType::EG_CLOCK_DIVIDER == 1)
1306 m_env_counter += 4;
1307 else if (bitfield(++m_env_counter, 0, 2) == RegisterType::EG_CLOCK_DIVIDER)
1308 m_env_counter += 4 - RegisterType::EG_CLOCK_DIVIDER;
1309
1310 // clock the noise generator
1311 int32_t lfo_raw_pm = m_regs.clock_noise_and_lfo();
1312
1313 // now update the state of all the channels and operators
1314 for (uint32_t chnum = 0; chnum < CHANNELS; chnum++)
1315 if (bitfield(chanmask, chnum))
1316 m_channel[chnum]->clock(m_env_counter, lfo_raw_pm);
1317
1318 // return the envelope counter as it is used to clock ADPCM-A
1319 return m_env_counter;
1320 }
1321
1322
1323 //-------------------------------------------------
1324 // output - compute a sum over the relevant
1325 // channels
1326 //-------------------------------------------------
1327
1328 template<class RegisterType>
1329 void fm_engine_base<RegisterType>::output(output_data &output, uint32_t rshift, int32_t clipmax, uint32_t chanmask) const
1330 {
1331 // mask out some channels for debug purposes
1332 chanmask &= debug::GLOBAL_FM_CHANNEL_MASK;
1333
1334 // mask out inactive channels
1335 if (!DEBUG_LOG_WAVFILES)
1336 chanmask &= m_active_channels;
1337
1338 // handle the rhythm case, where some of the operators are dedicated
1339 // to percussion (this is an OPL-specific feature)
1340 if (m_regs.rhythm_enable())
1341 {
1342 // we don't support the OPM noise channel here; ensure it is off
1343 assert(m_regs.noise_enable() == 0);
1344
1345 // precompute the operator 13+17 phase selection value
1346 uint32_t op13phase = m_operator[13]->phase();
1347 uint32_t op17phase = m_operator[17]->phase();
1348 uint32_t phase_select = (bitfield(op13phase, 2) ^ bitfield(op13phase, 7)) | bitfield(op13phase, 3) | (bitfield(op17phase, 5) ^ bitfield(op17phase, 3));
1349
1350 // sum over all the desired channels
1351 for (uint32_t chnum = 0; chnum < CHANNELS; chnum++)
1352 if (bitfield(chanmask, chnum))
1353 {
1354 #if (DEBUG_LOG_WAVFILES)
1355 auto reference = output;
1356 #endif
1357 if (chnum == 6)
1358 m_channel[chnum]->output_rhythm_ch6(output, rshift, clipmax);
1359 else if (chnum == 7)
1360 m_channel[chnum]->output_rhythm_ch7(phase_select, output, rshift, clipmax);
1361 else if (chnum == 8)
1362 m_channel[chnum]->output_rhythm_ch8(phase_select, output, rshift, clipmax);
1363 else if (m_channel[chnum]->is4op())
1364 m_channel[chnum]->output_4op(output, rshift, clipmax);
1365 else
1366 m_channel[chnum]->output_2op(output, rshift, clipmax);
1367 #if (DEBUG_LOG_WAVFILES)
1368 m_wavfile[chnum].add(output, reference);
1369 #endif
1370 }
1371 }
1372 else
1373 {
1374 // sum over all the desired channels
1375 for (uint32_t chnum = 0; chnum < CHANNELS; chnum++)
1376 if (bitfield(chanmask, chnum))
1377 {
1378 #if (DEBUG_LOG_WAVFILES)
1379 auto reference = output;
1380 #endif
1381 if (m_channel[chnum]->is4op())
1382 m_channel[chnum]->output_4op(output, rshift, clipmax);
1383 else
1384 m_channel[chnum]->output_2op(output, rshift, clipmax);
1385 #if (DEBUG_LOG_WAVFILES)
1386 m_wavfile[chnum].add(output, reference);
1387 #endif
1388 }
1389 }
1390 }
1391
1392
1393 //-------------------------------------------------
1394 // write - handle writes to the OPN registers
1395 //-------------------------------------------------
1396
1397 template<class RegisterType>
1398 void fm_engine_base<RegisterType>::write(uint16_t regnum, uint8_t data)
1399 {
1400 debug::log_fm_write("%03X = %02X\n", regnum, data);
1401
1402 // special case: writes to the mode register can impact IRQs;
1403 // schedule these writes to ensure ordering with timers
1404 if (regnum == RegisterType::REG_MODE)
1405 {
1406 m_intf.ymfm_sync_mode_write(data);
1407 return;
1408 }
1409
1410 // for now just mark all channels as modified
1411 m_modified_channels = ALL_CHANNELS;
1412
1413 // most writes are passive, consumed only when needed
1414 uint32_t keyon_channel;
1415 uint32_t keyon_opmask;
1416 if (m_regs.write(regnum, data, keyon_channel, keyon_opmask))
1417 {
1418 // handle writes to the keyon register(s)
1419 if (keyon_channel < CHANNELS)
1420 {
1421 // normal channel on/off
1422 m_channel[keyon_channel]->keyonoff(keyon_opmask, KEYON_NORMAL, keyon_channel);
1423 }
1424 else if (CHANNELS >= 9 && keyon_channel == RegisterType::RHYTHM_CHANNEL)
1425 {
1426 // special case for the OPL rhythm channels
1427 m_channel[6]->keyonoff(bitfield(keyon_opmask, 4) ? 3 : 0, KEYON_RHYTHM, 6);
1428 m_channel[7]->keyonoff(bitfield(keyon_opmask, 0) | (bitfield(keyon_opmask, 3) << 1), KEYON_RHYTHM, 7);
1429 m_channel[8]->keyonoff(bitfield(keyon_opmask, 2) | (bitfield(keyon_opmask, 1) << 1), KEYON_RHYTHM, 8);
1430 }
1431 }
1432 }
1433
1434
1435 //-------------------------------------------------
1436 // status - return the current state of the
1437 // status flags
1438 //-------------------------------------------------
1439
1440 template<class RegisterType>
1441 uint8_t fm_engine_base<RegisterType>::status() const
1442 {
1443 return m_status & ~STATUS_BUSY & ~m_regs.status_mask();
1444 }
1445
1446
1447 //-------------------------------------------------
1448 // assign_operators - get the current mapping of
1449 // operators to channels and assign them all
1450 //-------------------------------------------------
1451
1452 template<class RegisterType>
1453 void fm_engine_base<RegisterType>::assign_operators()
1454 {
1455 typename RegisterType::operator_mapping map;
1456 m_regs.operator_map(map);
1457
1458 for (uint32_t chnum = 0; chnum < CHANNELS; chnum++)
1459 for (uint32_t index = 0; index < 4; index++)
1460 {
1461 uint32_t opnum = bitfield(map.chan[chnum], 8 * index, 8);
1462 m_channel[chnum]->assign(index, (opnum == 0xff) ? nullptr : m_operator[opnum].get());
1463 }
1464 }
1465
1466
1467 //-------------------------------------------------
1468 // update_timer - update the state of the given
1469 // timer
1470 //-------------------------------------------------
1471
1472 template<class RegisterType>
1473 void fm_engine_base<RegisterType>::update_timer(uint32_t tnum, uint32_t enable, int32_t delta_clocks)
1474 {
1475 // if the timer is live, but not currently enabled, set the timer
1476 if (enable && !m_timer_running[tnum])
1477 {
1478 // period comes from the registers, and is different for each
1479 uint32_t period = (tnum == 0) ? (1024 - m_regs.timer_a_value()) : 16 * (256 - m_regs.timer_b_value());
1480
1481 // caller can also specify a delta to account for other effects
1482 period += delta_clocks;
1483
1484 // reset it
1485 m_intf.ymfm_set_timer(tnum, period * OPERATORS * m_clock_prescale);
1486 m_timer_running[tnum] = 1;
1487 }
1488
1489 // if the timer is not live, ensure it is not enabled
1490 else if (!enable)
1491 {
1492 m_intf.ymfm_set_timer(tnum, -1);
1493 m_timer_running[tnum] = 0;
1494 }
1495 }
1496
1497
1498 //-------------------------------------------------
1499 // engine_timer_expired - timer has expired - signal
1500 // status and possibly IRQs
1501 //-------------------------------------------------
1502
1503 template<class RegisterType>
1504 void fm_engine_base<RegisterType>::engine_timer_expired(uint32_t tnum)
1505 {
1506 // update status
1507 if (tnum == 0 && m_regs.enable_timer_a())
1508 set_reset_status(STATUS_TIMERA, 0);
1509 else if (tnum == 1 && m_regs.enable_timer_b())
1510 set_reset_status(STATUS_TIMERB, 0);
1511
1512 // if timer A fired in CSM mode, trigger CSM on all relevant channels
1513 if (tnum == 0 && m_regs.csm())
1514 for (uint32_t chnum = 0; chnum < CHANNELS; chnum++)
1515 if (bitfield(RegisterType::CSM_TRIGGER_MASK, chnum))
1516 {
1517 m_channel[chnum]->keyonoff(1, KEYON_CSM, chnum);
1518 m_modified_channels |= 1 << chnum;
1519 }
1520
1521 // reset
1522 m_timer_running[tnum] = false;
1523 update_timer(tnum, 1, 0);
1524 }
1525
1526
1527 //-------------------------------------------------
1528 // check_interrupts - check the interrupt sources
1529 // for interrupts
1530 //-------------------------------------------------
1531
1532 template<class RegisterType>
1533 void fm_engine_base<RegisterType>::engine_check_interrupts()
1534 {
1535 // update the state
1536 uint8_t old_state = m_irq_state;
1537 m_irq_state = ((m_status & m_irq_mask & ~m_regs.status_mask()) != 0);
1538
1539 // set the IRQ status bit
1540 if (m_irq_state)
1541 m_status |= STATUS_IRQ;
1542 else
1543 m_status &= ~STATUS_IRQ;
1544
1545 // if changed, signal the new state
1546 if (old_state != m_irq_state)
1547 m_intf.ymfm_update_irq(m_irq_state ? true : false);
1548 }
1549
1550
1551 //-------------------------------------------------
1552 // engine_mode_write - handle a mode register write
1553 // via timer callback
1554 //-------------------------------------------------
1555
1556 template<class RegisterType>
1557 void fm_engine_base<RegisterType>::engine_mode_write(uint8_t data)
1558 {
1559 // mark all channels as modified
1560 m_modified_channels = ALL_CHANNELS;
1561
1562 // actually write the mode register now
1563 uint32_t dummy1, dummy2;
1564 m_regs.write(RegisterType::REG_MODE, data, dummy1, dummy2);
1565
1566 // reset IRQ status -- when written, all other bits are ignored
1567 // QUESTION: should this maybe just reset the IRQ bit and not all the bits?
1568 // That is, check_interrupts would only set, this would only clear?
1569 if (m_regs.irq_reset())
1570 set_reset_status(0, 0x78);
1571 else
1572 {
1573 // reset timer status
1574 uint8_t reset_mask = 0;
1575 if (m_regs.reset_timer_b())
1576 reset_mask |= RegisterType::STATUS_TIMERB;
1577 if (m_regs.reset_timer_a())
1578 reset_mask |= RegisterType::STATUS_TIMERA;
1579 set_reset_status(0, reset_mask);
1580
1581 // load timers; note that timer B gets a small negative adjustment because
1582 // the *16 multiplier is free-running, so the first tick of the clock
1583 // is a bit shorter
1584 update_timer(1, m_regs.load_timer_b(), -(m_total_clocks & 15));
1585 update_timer(0, m_regs.load_timer_a(), 0);
1586 }
1587 }
1588
1589 }