Debian Codegraph @

extern/ESFMu/esfm_registers.c

raw
1 /*
2 * ESFMu: emulator for the ESS "ESFM" enhanced OPL3 clone
3 * Copyright (C) 2023 Kagamiin~
4 *
5 * This file includes code and data from the Nuked OPL3 project, copyright (C)
6 * 2013-2023 Nuke.YKT. Its usage, modification and redistribution is allowed
7 * under the terms of the GNU Lesser General Public License version 2.1 or
8 * later.
9 *
10 * ESFMu is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation, either version 2.1
13 * of the License, or (at your option) any later version.
14 *
15 * ESFMu is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with ESFMu. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24 /*
25 * ESFMu wouldn't have been possible without the hard work and dedication of
26 * the retro computer hardware research and preservation community.
27 *
28 * I'd like to thank:
29 * - Nuke.YKT
30 * Developer of Nuked OPL3, which was the basis for ESFMu's code and
31 * also a great learning resource on Yamaha FM synthesis for myself.
32 * Nuke.YKT also gives shoutouts on behalf of Nuked OPL3 to:
33 * - MAME Development Team(Jarek Burczynski, Tatsuyuki Satoh):
34 * Feedback and Rhythm part calculation information.
35 * - forums.submarine.org.uk(carbon14, opl3):
36 * Tremolo and phase generator calculation information.
37 * - OPLx decapsulated(Matthew Gambrell, Olli Niemitalo):
38 * OPL2 ROMs.
39 * - siliconpr0n.org(John McMaster, digshadow):
40 * YMF262 and VRC VII decaps and die shots.
41 * - rainwarrior
42 * For performing the initial research on ESFM drivers and documenting
43 * ESS's patent on native mode operator organization.
44 * - jwt27
45 * For kickstarting the ESFM research project and compiling rainwarrior's
46 * findings and more in an accessible document ("ESFM Demystified").
47 * - pachuco/CatButts
48 * For documenting ESS's patent on ESFM's feedback implementation, which
49 * was vital in getting ESFMu's sound output to be accurate.
50 * - And everybody who helped out with real hardware testing
51 */
52
53 #include "esfm.h"
54 #include <stdlib.h>
55 #include <stddef.h>
56 #include <string.h>
57 #include <stdbool.h>
58
59
60 /*
61 * Table of KSL values extracted from OPL3 ROM; taken straight from Nuked OPL3
62 * source code.
63 * TODO: Check if ESFM uses the same KSL values.
64 */
65
66 static const int16 kslrom[16] = {
67 0, 32, 40, 45, 48, 51, 53, 55, 56, 58, 59, 60, 61, 62, 63, 64
68 };
69
70 /*
71 * This maps the low 5 bits of emulation mode address to an emulation mode
72 * slot; taken straight from Nuked OPL3. Used for decoding certain emulation
73 * mode address ranges.
74 */
75 static const int8_t ad_slot[0x20] = {
76 0, 1, 2, 3, 4, 5, -1, -1, 6, 7, 8, 9, 10, 11, -1, -1,
77 12, 13, 14, 15, 16, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
78 };
79
80 /*
81 * This maps an emulation mode slot index to a tuple representing the
82 * corresponding native mode channel and slot.
83 */
84 static const emu_slot_channel_mapping emu_slot_map[36] =
85 {
86 { 0, 0}, { 1, 0}, { 2, 0}, { 0, 1}, { 1, 1}, { 2, 1},
87 { 3, 0}, { 4, 0}, { 5, 0}, { 3, 1}, { 4, 1}, { 5, 1},
88 { 6, 0}, { 7, 0}, { 8, 0}, { 6, 1}, { 7, 1}, { 8, 1},
89 { 9, 0}, {10, 0}, {11, 0}, { 9, 1}, {10, 1}, {11, 1},
90 {12, 0}, {13, 0}, {14, 0}, {12, 1}, {13, 1}, {14, 1},
91 {15, 0}, {16, 0}, {17, 0}, {15, 1}, {16, 1}, {17, 1}
92 };
93
94 /*
95 * This encodes which emulation mode channels are the secondary channel in a
96 * 4-op channel pair (where the entry is non-negative), and which is the
97 * corresponding primary channel for that secondary channel.
98 */
99 static const int emu_4op_secondary_to_primary[18] =
100 {
101 -1, -1, -1, 0, 1, 2, -1, -1, -1,
102 -1, -1, -1, 9, 10, 11, -1, -1, -1
103 };
104
105 /*
106 * This encodes the operator outputs to be enabled or disabled for
107 * each 4-op algorithm in emulation mode.
108 * Indices: FM+FM, FM+AM, AM+FM, AM+AM (lower channel MSB, upper channel LSB)
109 * Values: enable OP1, OP2, OP3, OP4
110 */
111 static const bool emu_4op_alg_output_enable[4][4] =
112 {
113 {0, 0, 0, 1},
114 {0, 1, 0, 1},
115 {1, 0, 0, 1},
116 {1, 0, 1, 1}
117 };
118
119 /*
120 * This encodes the operator interconnections to be enabled or disabled for
121 * each 4-op algorithm in emulation mode.
122 * Indices: FM+FM, FM+AM, AM+FM, AM+AM (lower channel MSB, upper channel LSB)
123 * Values: enable OP1FB, OP1->2, OP2->3, OP3->4
124 */
125 static const bool emu_4op_alg_mod_enable[4][4] =
126 {
127 {1, 1, 1, 1},
128 {1, 1, 0, 1},
129 {1, 0, 1, 1},
130 {1, 0, 1, 0}
131 };
132
133
134 /* ------------------------------------------------------------------------- */
135 static void
136 ESFM_emu_rearrange_connections(esfm_channel *channel)
137 {
138 int secondary_to_primary;
139
140 secondary_to_primary = emu_4op_secondary_to_primary[channel->channel_idx];
141 if (secondary_to_primary >= 0)
142 {
143 esfm_channel *pair_primary = &channel->chip->channels[secondary_to_primary];
144 if (pair_primary->emu_mode_4op_enable)
145 {
146 // always work from primary channel in pair when dealing with 4-op
147 channel = pair_primary;
148 }
149 }
150
151 if (channel->emu_mode_4op_enable && (channel->channel_idx % 9) < 3 && channel->chip->emu_newmode)
152 {
153 esfm_channel *secondary = &channel->chip->channels[channel->channel_idx + 3];
154 uint2 algorithm = ((channel->slots[0].emu_connection_typ != 0) << 1)
155 | (secondary->slots[0].emu_connection_typ != 0);
156 int i;
157
158 secondary->slots[0].in.mod_input = &channel->slots[1].in.output;
159
160 for (i = 0; i < 2; i++)
161 {
162 channel->slots[i].in.emu_mod_enable =
163 emu_4op_alg_mod_enable[algorithm][i] ? ~((int13) 0) : 0;
164 channel->slots[i].in.emu_output_enable =
165 emu_4op_alg_output_enable[algorithm][i] ? ~((int13) 0) : 0;
166
167 secondary->slots[i].in.emu_mod_enable =
168 emu_4op_alg_mod_enable[algorithm][i + 2] ? ~((int13) 0) : 0;
169 secondary->slots[i].in.emu_output_enable =
170 emu_4op_alg_output_enable[algorithm][i + 2] ? ~((int13) 0) : 0;
171 }
172 }
173 else if ((channel->chip->emu_rhy_mode_flags & 0x20) != 0
174 && (channel->channel_idx == 7 || channel->channel_idx == 8))
175 {
176 channel->slots[0].in.emu_mod_enable = 0;
177 channel->slots[1].in.emu_mod_enable = 0;
178 channel->slots[0].in.emu_output_enable = ~((int13) 0);
179 channel->slots[1].in.emu_output_enable = ~((int13) 0);
180 }
181 else
182 {
183 channel->slots[0].in.mod_input = &channel->slots[0].in.feedback_buf;
184
185 channel->slots[0].in.emu_mod_enable = ~((int13) 0);
186 channel->slots[0].in.emu_output_enable =
187 (channel->slots[0].emu_connection_typ != 0) ? ~((int13) 0) : 0;
188 channel->slots[1].in.emu_output_enable = ~((int13) 0);
189 channel->slots[1].in.emu_mod_enable =
190 (channel->slots[0].emu_connection_typ != 0) ? 0 : ~((int13) 0);
191 }
192 }
193
194
195 /* ------------------------------------------------------------------------- */
196 static void
197 ESFM_emu_to_native_switch(esfm_chip *chip)
198 {
199 size_t channel_idx, slot_idx;
200 for (channel_idx = 0; channel_idx < 18; channel_idx++)
201 {
202 for (slot_idx = 0; slot_idx < 4; slot_idx++)
203 {
204 esfm_channel *channel = &chip->channels[channel_idx];
205 esfm_slot *slot = &channel->slots[slot_idx];
206
207 if (slot_idx == 0)
208 {
209 slot->in.mod_input = &slot->in.feedback_buf;
210 }
211 else
212 {
213 esfm_slot *prev_slot = &channel->slots[slot_idx - 1];
214 slot->in.mod_input = &prev_slot->in.output;
215 }
216 }
217 }
218 }
219
220 /* ------------------------------------------------------------------------- */
221 static void
222 ESFM_native_to_emu_switch(esfm_chip *chip)
223 {
224 size_t channel_idx;
225 for (channel_idx = 0; channel_idx < 18; channel_idx++)
226 {
227 ESFM_emu_rearrange_connections(&chip->channels[channel_idx]);
228 }
229 }
230
231 /* ------------------------------------------------------------------------- */
232 static void
233 ESFM_slot_update_keyscale(esfm_slot *slot)
234 {
235 if (slot->slot_idx > 0 && !slot->chip->native_mode)
236 {
237 return;
238 }
239
240 int16 ksl = (kslrom[slot->f_num >> 6] << 2) - ((0x08 - slot->block) << 5);
241 if (ksl < 0)
242 {
243 ksl = 0;
244 }
245 slot->in.eg_ksl_offset = ksl;
246 slot->in.keyscale = (slot->block << 1)
247 | ((slot->f_num >> (8 + !slot->chip->keyscale_mode)) & 0x01);
248 }
249
250 /* ------------------------------------------------------------------------- */
251 static void
252 ESFM_emu_channel_update_keyscale(esfm_channel *channel)
253 {
254 int secondary_to_primary;
255
256 secondary_to_primary = emu_4op_secondary_to_primary[channel->channel_idx];
257 if (secondary_to_primary >= 0)
258 {
259 esfm_channel *pair_primary = &channel->chip->channels[secondary_to_primary];
260 if (pair_primary->emu_mode_4op_enable)
261 {
262 // always work from primary channel in pair when dealing with 4-op
263 channel = pair_primary;
264 }
265 }
266
267 ESFM_slot_update_keyscale(&channel->slots[0]);
268 channel->slots[1].in.eg_ksl_offset = channel->slots[0].in.eg_ksl_offset;
269 channel->slots[1].in.keyscale = channel->slots[0].in.keyscale;
270
271 if (channel->emu_mode_4op_enable && (channel->channel_idx % 9) < 3 && channel->chip->emu_newmode)
272 {
273 int i;
274 esfm_channel *secondary = &channel->chip->channels[channel->channel_idx + 3];
275 secondary->slots[0].f_num = channel->slots[0].f_num;
276 secondary->slots[0].block = channel->slots[0].block;
277
278 for (i = 0; i < 2; i++)
279 {
280 secondary->slots[i].in.eg_ksl_offset = channel->slots[0].in.eg_ksl_offset;
281 secondary->slots[i].in.keyscale = channel->slots[0].in.keyscale;
282 }
283 }
284 }
285
286 /* ------------------------------------------------------------------------- */
287 static inline uint8_t
288 ESFM_slot_readback (esfm_slot *slot, uint8_t register_idx)
289 {
290 uint8_t data = 0;
291 switch (register_idx & 0x07)
292 {
293 case 0x00:
294 data |= (slot->tremolo_en != 0) << 7;
295 data |= (slot->vibrato_en != 0) << 6;
296 data |= (slot->env_sustaining != 0) << 5;
297 data |= (slot->vibrato_en != 0) << 4;
298 data |= slot->mult & 0x0f;
299 break;
300 case 0x01:
301 data |= slot->ksl << 6;
302 data |= slot->t_level & 0x3f;
303 break;
304 case 0x02:
305 data |= slot->attack_rate << 4;
306 data |= slot->decay_rate & 0x0f;
307 break;
308 case 0x03:
309 data |= slot->sustain_lvl << 4;
310 data |= slot->release_rate & 0x0f;
311 break;
312 case 0x04:
313 data = slot->f_num & 0xff;
314 break;
315 case 0x05:
316 data |= slot->env_delay << 5;
317 data |= (slot->block & 0x07) << 2;
318 data |= (slot->f_num >> 8) & 0x03;
319 break;
320 case 0x06:
321 data |= (slot->tremolo_deep != 0) << 7;
322 data |= (slot->vibrato_deep != 0) << 6;
323 data |= (slot->out_enable[1] != 0) << 5;
324 data |= (slot->out_enable[0] != 0) << 4;
325 data |= (slot->mod_in_level & 0x07) << 1;
326 data |= slot->emu_connection_typ & 0x01;
327 break;
328 case 0x07:
329 data |= slot->output_level << 5;
330 data |= (slot->rhy_noise & 0x03) << 3;
331 data |= slot->waveform & 0x07;
332 break;
333 }
334 return data;
335 }
336
337 /* ------------------------------------------------------------------------- */
338 static inline void
339 ESFM_slot_write (esfm_slot *slot, uint8_t register_idx, uint8_t data)
340 {
341 switch (register_idx & 0x07)
342 {
343 case 0x00:
344 slot->tremolo_en = (data & 0x80) != 0;
345 slot->vibrato_en = (data & 0x40) != 0;
346 slot->env_sustaining = (data & 0x20) != 0;
347 slot->ksr = (data & 0x10) != 0;
348 slot->mult = data & 0x0f;
349 break;
350 case 0x01:
351 slot->ksl = data >> 6;
352 slot->t_level = data & 0x3f;
353 ESFM_slot_update_keyscale(slot);
354 break;
355 case 0x02:
356 slot->attack_rate = data >> 4;
357 slot->decay_rate = data & 0x0f;
358 break;
359 case 0x03:
360 slot->sustain_lvl = data >> 4;
361 slot->release_rate = data & 0x0f;
362 break;
363 case 0x04:
364 slot->f_num = (slot->f_num & 0x300) | data;
365 ESFM_slot_update_keyscale(slot);
366 break;
367 case 0x05:
368 if (slot->env_delay < (data >> 5))
369 {
370 slot->in.eg_delay_transitioned_01 = 1;
371 }
372 else if (slot->env_delay > (data >> 5))
373 {
374 slot->in.eg_delay_transitioned_10 = 1;
375 }
376 slot->env_delay = data >> 5;
377 slot->emu_key_on = (data >> 5) & 0x01;
378 slot->block = (data >> 2) & 0x07;
379 slot->f_num = (slot->f_num & 0xff) | ((data & 0x03) << 8);
380 ESFM_slot_update_keyscale(slot);
381 break;
382 case 0x06:
383 slot->tremolo_deep = (data & 0x80) != 0;
384 slot->vibrato_deep = (data & 0x40) != 0;
385 slot->out_enable[1] = (data & 0x20) ? ~((int13) 0) : 0;
386 slot->out_enable[0] = (data & 0x10) ? ~((int13) 0) : 0;
387 slot->mod_in_level = (data >> 1) & 0x07;
388 slot->emu_connection_typ = data & 0x01;
389 break;
390 case 0x07:
391 slot->output_level = data >> 5;
392 slot->rhy_noise = (data >> 3) & 0x03;
393 slot->waveform = data & 0x07;
394 break;
395 }
396 }
397
398 #define KEY_ON_REGS_START (18 * 4 * 8)
399 #define TIMER1_REG (0x402)
400 #define TIMER2_REG (0x403)
401 #define TIMER_SETUP_REG (0x404)
402 #define CONFIG_REG (0x408)
403 #define BASSDRUM_REG (0x4bd)
404 #define TEST_REG (0x501)
405 #define FOUROP_CONN_REG (0x504)
406 #define NATIVE_MODE_REG (0x505)
407
408 /* ------------------------------------------------------------------------- */
409 static void
410 ESFM_write_reg_native (esfm_chip *chip, uint16_t address, uint8_t data)
411 {
412 int i;
413 address = address & 0x7ff;
414
415 if (address < KEY_ON_REGS_START)
416 {
417 // Slot register write
418 size_t channel_idx = address >> 5;
419 size_t slot_idx = (address >> 3) & 0x03;
420 size_t register_idx = address & 0x07;
421 esfm_slot *slot = &chip->channels[channel_idx].slots[slot_idx];
422
423 ESFM_slot_write(slot, register_idx, data);
424 }
425 else if (address < KEY_ON_REGS_START + 16)
426 {
427 // Key-on registers
428 size_t channel_idx = (address - KEY_ON_REGS_START);
429 esfm_channel *channel = &chip->channels[channel_idx];
430 channel->key_on = data & 0x01;
431 channel->emu_mode_4op_enable = (data & 0x02) != 0;
432 }
433 else if (address < KEY_ON_REGS_START + 20)
434 {
435 // Key-on channels 17 and 18 (each half)
436 size_t channel_idx = 16 + ((address & 0x02) >> 1);
437 bool second_half = address & 0x01;
438 esfm_channel *channel = &chip->channels[channel_idx];
439 if (second_half)
440 {
441 channel->key_on_2 = data & 0x01;
442 channel->emu_mode_4op_enable_2 = (data & 0x02) != 0;
443 }
444 else
445 {
446 channel->key_on = data & 0x01;
447 channel->emu_mode_4op_enable = (data & 0x02) != 0;
448 }
449 }
450 else
451 {
452 switch (address & 0x5ff)
453 {
454 case TIMER1_REG:
455 chip->timer_reload[0] = data;
456 break;
457 case TIMER2_REG:
458 chip->timer_reload[1] = data;
459 break;
460 case TIMER_SETUP_REG:
461 if (data & 0x80)
462 {
463 chip->timer_overflow[0] = 0;
464 chip->timer_overflow[1] = 0;
465 chip->irq_bit = 0;
466 }
467 chip->timer_enable[0] = (data & 0x01) != 0;
468 chip->timer_enable[1] = (data & 0x02) != 0;
469 chip->timer_mask[0] = (data & 0x20) != 0;
470 chip->timer_mask[1] = (data & 0x40) != 0;
471 break;
472 case CONFIG_REG:
473 chip->keyscale_mode = (data & 0x40) != 0;
474 break;
475 case BASSDRUM_REG:
476 chip->emu_rhy_mode_flags = data & 0x3f;
477 chip->emu_vibrato_deep = (data & 0x40) != 0;
478 chip->emu_tremolo_deep = (data & 0x80) != 0;
479 break;
480 case FOUROP_CONN_REG:
481 for (i = 0; i < 3; i++)
482 {
483 chip->channels[i].emu_mode_4op_enable = (data >> i) & 0x01;
484 chip->channels[i + 9].emu_mode_4op_enable = (data >> (i + 3)) & 0x01;
485 }
486 break;
487 case TEST_REG:
488 chip->test_bit_eg_halt = (data & 0x01) | ((data & 0x20) != 0);
489 chip->test_bit_distort = (data & 0x02) != 0;
490 chip->test_bit_attenuate = (data & 0x10) != 0;
491 chip->test_bit_phase_stop_reset = (data & 0x40) != 0;
492 break;
493 }
494 }
495 }
496
497 /* ------------------------------------------------------------------------- */
498 static uint8_t
499 ESFM_readback_reg_native (esfm_chip *chip, uint16_t address)
500 {
501 int i;
502 uint8_t data = 0;
503 address = address & 0x7ff;
504
505 if (address < KEY_ON_REGS_START)
506 {
507 // Slot register read
508 size_t channel_idx = address >> 5;
509 size_t slot_idx = (address >> 3) & 0x03;
510 size_t register_idx = address & 0x07;
511 esfm_slot *slot = &chip->channels[channel_idx].slots[slot_idx];
512
513 data = ESFM_slot_readback(slot, register_idx);
514 }
515 else if (address < KEY_ON_REGS_START + 16)
516 {
517 // Key-on registers
518 size_t channel_idx = (address - KEY_ON_REGS_START);
519 esfm_channel *channel = &chip->channels[channel_idx];
520
521 data |= channel->key_on != 0;
522 data |= (channel->emu_mode_4op_enable != 0) << 1;
523 }
524 else if (address < KEY_ON_REGS_START + 20)
525 {
526 // Key-on channels 17 and 18 (each half)
527 size_t channel_idx = 16 + ((address & 0x02) >> 1);
528 bool second_half = address & 0x01;
529 esfm_channel *channel = &chip->channels[channel_idx];
530 if (second_half)
531 {
532 data |= channel->key_on_2 != 0;
533 data |= (channel->emu_mode_4op_enable_2 != 0) << 1;
534 }
535 else
536 {
537 data |= channel->key_on != 0;
538 data |= (channel->emu_mode_4op_enable != 0) << 1;
539 }
540 }
541 else
542 {
543 switch (address & 0x5ff)
544 {
545 case TIMER1_REG:
546 data = chip->timer_reload[0];
547 break;
548 case TIMER2_REG:
549 data = chip->timer_reload[1];
550 break;
551 case TIMER_SETUP_REG:
552 data |= chip->timer_enable[0] != 0;
553 data |= (chip->timer_enable[1] != 0) << 1;
554 data |= (chip->timer_mask[0] != 0) << 5;
555 data |= (chip->timer_mask[1] != 0) << 6;
556 break;
557 case CONFIG_REG:
558 data |= (chip->keyscale_mode != 0) << 6;
559 break;
560 case BASSDRUM_REG:
561 data |= chip->emu_rhy_mode_flags;
562 data |= chip->emu_vibrato_deep << 6;
563 data |= chip->emu_tremolo_deep << 7;
564 break;
565 case TEST_REG:
566 data |= chip->test_bit_eg_halt != 0;
567 data |= (chip->test_bit_distort != 0) << 1;
568 data |= (chip->test_bit_attenuate != 0) << 4;
569 data |= (chip->test_bit_eg_halt != 0) << 5;
570 data |= (chip->test_bit_phase_stop_reset != 0) << 6;
571 break;
572 case FOUROP_CONN_REG:
573 for (i = 0; i < 3; i++)
574 {
575 data |= (chip->channels[i].emu_mode_4op_enable != 0) << i;
576 data |= (chip->channels[i + 9].emu_mode_4op_enable != 0) << (i + 3);
577 }
578 break;
579 case NATIVE_MODE_REG:
580 data |= (chip->native_mode != 0) << 7;
581 break;
582 }
583 }
584 return data;
585 }
586
587 /* ------------------------------------------------------------------------- */
588 static void
589 ESFM_write_reg_emu (esfm_chip *chip, uint16_t address, uint8_t data)
590 {
591 bool high = (address & 0x100) != 0;
592 uint8_t reg = address & 0xff;
593 int emu_slot_idx = ad_slot[address & 0x1f];
594 int natv_chan_idx = -1;
595 int natv_slot_idx = -1;
596 int emu_chan_idx = (reg & 0x0f) > 8 ? -1 : ((reg & 0x0f) + high * 9);
597
598 if (emu_slot_idx >= 0)
599 {
600 if (high)
601 {
602 emu_slot_idx += 18;
603 }
604
605 natv_chan_idx = emu_slot_map[emu_slot_idx].channel_idx;
606 natv_slot_idx = emu_slot_map[emu_slot_idx].slot_idx;
607 }
608
609 if (reg == 0xbd)
610 {
611 chip->emu_rhy_mode_flags = data & 0x3f;
612 chip->emu_vibrato_deep = (data & 0x40) != 0;
613 chip->emu_tremolo_deep = (data & 0x80) != 0;
614 if (chip->emu_rhy_mode_flags & 0x20)
615 {
616 // TODO: check if writes to 0xbd actually affect the readable key-on flags at
617 // 0x246, 0x247, 0x248; and if there's any visible effect from the SD and TC flags
618 chip->channels[6].key_on = (data & 0x10) != 0;
619 chip->channels[7].key_on = (data & 0x01) != 0;
620 chip->channels[8].key_on = (data & 0x04) != 0;
621 chip->channels[7].key_on_2 = (data & 0x08) != 0;
622 chip->channels[8].key_on_2 = (data & 0x02) != 0;
623 }
624 ESFM_emu_rearrange_connections(&chip->channels[7]);
625 ESFM_emu_rearrange_connections(&chip->channels[8]);
626 return;
627 }
628
629 switch(reg & 0xf0)
630 {
631 case 0x00:
632 if (high)
633 {
634 int i;
635 switch(reg & 0x0f)
636 {
637 case 0x01:
638 chip->emu_wavesel_enable = (data & 0x20) != 0;
639 break;
640 case 0x02:
641 chip->timer_reload[0] = data;
642 break;
643 case 0x03:
644 chip->timer_reload[1] = data;
645 break;
646 case 0x04:
647 for (i = 0; i < 3; i++)
648 {
649 chip->channels[i].emu_mode_4op_enable = (data >> i) & 0x01;
650 chip->channels[i + 9].emu_mode_4op_enable = (data >> (i + 3)) & 0x01;
651 }
652 for (i = 0; i < 6; i++)
653 {
654 ESFM_emu_rearrange_connections(&chip->channels[i]);
655 ESFM_emu_rearrange_connections(&chip->channels[i + 9]);
656 }
657 break;
658 case 0x05:
659 chip->emu_newmode = data & 0x01;
660 if ((data & 0x80) != 0)
661 {
662 chip->native_mode = 1;
663 ESFM_emu_to_native_switch(chip);
664 }
665 break;
666 case 0x08:
667 chip->keyscale_mode = (data & 0x40) != 0;
668 break;
669 }
670 }
671 else
672 {
673 switch(reg & 0x0f)
674 {
675 case 0x01:
676 chip->emu_wavesel_enable = (data & 0x20) != 0;
677 break;
678 case 0x02:
679 chip->timer_reload[0] = data;
680 break;
681 case 0x03:
682 chip->timer_reload[1] = data;
683 break;
684 case 0x04:
685 chip->timer_enable[0] = data & 0x01;
686 chip->timer_enable[1] = (data & 0x02) != 0;
687 chip->timer_mask[0] = (data & 0x20) != 0;
688 chip->timer_mask[1] = (data & 0x40) != 0;
689 if (data & 0x80)
690 {
691 chip->irq_bit = 0;
692 }
693 break;
694 case 0x08:
695 chip->keyscale_mode = (data & 0x40) != 0;
696 break;
697 }
698 }
699 break;
700 case 0x20: case 0x30:
701 if (emu_slot_idx >= 0)
702 {
703 ESFM_slot_write(&chip->channels[natv_chan_idx].slots[natv_slot_idx], 0x0, data);
704 }
705 break;
706 case 0x40: case 0x50:
707 if (emu_slot_idx >= 0)
708 {
709 ESFM_slot_write(&chip->channels[natv_chan_idx].slots[natv_slot_idx], 0x1, data);
710 ESFM_emu_channel_update_keyscale(&chip->channels[natv_chan_idx]);
711 }
712 break;
713 case 0x60: case 0x70:
714 if (emu_slot_idx >= 0)
715 {
716 ESFM_slot_write(&chip->channels[natv_chan_idx].slots[natv_slot_idx], 0x2, data);
717 }
718 break;
719 case 0x80: case 0x90:
720 if (emu_slot_idx >= 0)
721 {
722 ESFM_slot_write(&chip->channels[natv_chan_idx].slots[natv_slot_idx], 0x3, data);
723 }
724 break;
725 case 0xa0:
726 if (emu_chan_idx >= 0)
727 {
728 ESFM_slot_write(&chip->channels[emu_chan_idx].slots[0], 0x4, data);
729 ESFM_emu_channel_update_keyscale(&chip->channels[emu_chan_idx]);
730 }
731 break;
732 case 0xb0:
733 if (emu_chan_idx >= 0)
734 {
735 esfm_channel *channel = &chip->channels[emu_chan_idx];
736 // TODO: check if emulation mode actually writes to the native mode key on registers
737 // it might only use slot 0's emu key on field...
738 channel->key_on = (data & 0x20) != 0;
739 if (channel->channel_idx == 7 || channel->channel_idx == 8)
740 {
741 channel->key_on_2 = (data & 0x20) != 0;
742 }
743 ESFM_slot_write(&channel->slots[0], 0x5, data);
744 ESFM_emu_channel_update_keyscale(&chip->channels[emu_chan_idx]);
745 }
746 break;
747 case 0xc0:
748 if (emu_chan_idx >= 0)
749 {
750 ESFM_slot_write(&chip->channels[emu_chan_idx].slots[0], 0x6, data);
751 ESFM_emu_rearrange_connections(&chip->channels[emu_chan_idx]);
752 }
753 break;
754 case 0xe0: case 0xf0:
755 if (emu_slot_idx >= 0)
756 {
757 ESFM_slot_write(&chip->channels[natv_chan_idx].slots[natv_slot_idx], 0x7, data);
758 }
759 break;
760 }
761 }
762
763
764 /* ------------------------------------------------------------------------- */
765 void
766 ESFM_write_reg (esfm_chip *chip, uint16_t address, uint8_t data)
767 {
768 if (chip->native_mode)
769 {
770 ESFM_write_reg_native(chip, address, data);
771 return;
772 }
773 else
774 {
775 ESFM_write_reg_emu(chip, address, data);
776 return;
777 }
778 }
779
780 /* ------------------------------------------------------------------------- */
781 void
782 ESFM_write_reg_buffered (esfm_chip *chip, uint16_t address, uint8_t data)
783 {
784 uint64_t timestamp;
785 esfm_write_buf *new_entry, *last_entry;
786
787 new_entry = &chip->write_buf[chip->write_buf_end];
788 last_entry = &chip->write_buf[(chip->write_buf_end - 1) % ESFM_WRITEBUF_SIZE];
789
790 if (new_entry->valid) {
791 ESFM_write_reg(chip, new_entry->address, new_entry->data);
792 chip->write_buf_start = (chip->write_buf_end + 1) % ESFM_WRITEBUF_SIZE;
793 }
794
795 new_entry->valid = 1;
796 new_entry->address = address;
797 new_entry->data = data;
798 timestamp = last_entry->timestamp + ESFM_WRITEBUF_DELAY;
799 if (timestamp < chip->write_buf_timestamp)
800 {
801 timestamp = chip->write_buf_timestamp;
802 }
803
804 new_entry->timestamp = timestamp;
805 chip->write_buf_end = (chip->write_buf_end + 1) % ESFM_WRITEBUF_SIZE;
806 }
807
808 /* ------------------------------------------------------------------------- */
809 void
810 ESFM_write_reg_buffered_fast (esfm_chip *chip, uint16_t address, uint8_t data)
811 {
812 esfm_write_buf *new_entry;
813
814 new_entry = &chip->write_buf[chip->write_buf_end];
815
816 if (new_entry->valid) {
817 ESFM_write_reg(chip, new_entry->address, new_entry->data);
818 chip->write_buf_start = (chip->write_buf_end + 1) % ESFM_WRITEBUF_SIZE;
819 }
820
821 new_entry->valid = 1;
822 new_entry->address = address;
823 new_entry->data = data;
824 new_entry->timestamp = chip->write_buf_timestamp;
825 chip->write_buf_end = (chip->write_buf_end + 1) % ESFM_WRITEBUF_SIZE;
826 }
827
828 /* ------------------------------------------------------------------------- */
829 uint8_t
830 ESFM_readback_reg (esfm_chip *chip, uint16_t address)
831 {
832 if (chip->native_mode)
833 {
834 return ESFM_readback_reg_native(chip, address);
835 }
836 else
837 {
838 return 0;
839 }
840 }
841
842 /* ------------------------------------------------------------------------- */
843 void
844 ESFM_write_port (esfm_chip *chip, uint8_t offset, uint8_t data)
845 {
846 if (chip->native_mode)
847 {
848 switch(offset)
849 {
850 case 0:
851 chip->native_mode = 0;
852 ESFM_native_to_emu_switch(chip);
853 chip->addr_latch = data;
854 break;
855 case 1:
856 ESFM_write_reg_native(chip, chip->addr_latch, data);
857 break;
858 case 2:
859 chip->addr_latch = (chip->addr_latch & 0xff00) | data;
860 break;
861 case 3:
862 chip->addr_latch = chip->addr_latch & 0xff;
863 chip->addr_latch |= (uint16)data << 8;
864 break;
865 }
866 }
867 else
868 {
869 switch(offset)
870 {
871 case 0:
872 chip->addr_latch = data;
873 break;
874 case 1: case 3:
875 ESFM_write_reg_emu(chip, chip->addr_latch, data);
876 break;
877 case 2:
878 chip->addr_latch = (uint16)data | 0x100;
879 break;
880 }
881 }
882 }
883
884 /* ------------------------------------------------------------------------- */
885 uint8_t
886 ESFM_read_port (esfm_chip *chip, uint8_t offset)
887 {
888 uint8_t data = 0;
889 if (chip->native_mode)
890 {
891 switch(offset)
892 {
893 case 0:
894 // TODO: actually implement timer count, trigger and reset
895 data |= (chip->irq_bit != 0) << 7;
896 data |= (chip->timer_overflow[0] != 0) << 6;
897 data |= (chip->timer_overflow[1] != 0) << 5;
898 break;
899 case 1:
900 data = ESFM_readback_reg_native(chip, chip->addr_latch);
901 break;
902 // TODO: verify what the ESFM chip actually returns when reading
903 // from the other address ports
904 }
905 }
906 else
907 {
908 switch(offset)
909 {
910 case 0:
911 data |= (chip->irq_bit != 0) << 7;
912 data |= (chip->timer_overflow[0] != 0) << 6;
913 data |= (chip->timer_overflow[1] != 0) << 5;
914 break;
915 case 1:
916 data = 0;
917 break;
918 case 2: case 3:
919 // This matches OPL3 behavior.
920 // TODO: verify what the ESFM chip actually returns when reading
921 // from address ports in emulation mode
922 data = 0xff;
923 break;
924 }
925 }
926 return data;
927 }
928
929 /* ------------------------------------------------------------------------- */
930 void
931 ESFM_set_mode (esfm_chip *chip, bool native_mode)
932 {
933 native_mode = native_mode != 0;
934
935 if (native_mode != (chip->native_mode != 0))
936 {
937 chip->native_mode = native_mode;
938 if (native_mode)
939 {
940 ESFM_emu_to_native_switch(chip);
941 }
942 else
943 {
944 ESFM_native_to_emu_switch(chip);
945 }
946 }
947 }
948
949 /* ------------------------------------------------------------------------- */
950 void
951 ESFM_init (esfm_chip *chip, uint8_t fast)
952 {
953 esfm_slot *slot;
954 esfm_channel *channel;
955 size_t channel_idx, slot_idx;
956
957 memset(chip, 0, sizeof(esfm_chip));
958 for (channel_idx = 0; channel_idx < 18; channel_idx++)
959 {
960 for (slot_idx = 0; slot_idx < 4; slot_idx++)
961 {
962 channel = &chip->channels[channel_idx];
963 slot = &channel->slots[slot_idx];
964
965 channel->chip = chip;
966 channel->channel_idx = channel_idx;
967 slot->channel = channel;
968 slot->chip = chip;
969 slot->slot_idx = slot_idx;
970 slot->in.eg_position = slot->in.eg_output = 0x1ff;
971 slot->in.eg_state = EG_RELEASE;
972 slot->in.emu_mod_enable = ~((int13) 0);
973 if (slot_idx == 0)
974 {
975 slot->in.mod_input = &slot->in.feedback_buf;
976 }
977 else
978 {
979 esfm_slot *prev_slot = &channel->slots[slot_idx - 1];
980 slot->in.mod_input = &prev_slot->in.output;
981 }
982
983 if (slot_idx == 1)
984 {
985 slot->in.emu_output_enable = ~((int13) 0);
986 }
987
988 if (channel_idx > 15 && slot_idx & 0x02)
989 {
990 slot->in.key_on = &channel->key_on_2;
991 }
992 else
993 {
994 slot->in.key_on = &channel->key_on;
995 }
996
997 slot->out_enable[0] = slot->out_enable[1] = ~((int13) 0);
998 }
999 }
1000
1001 chip->lfsr = 1;
1002 chip->fast_mode = fast;
1003 }
1004