From f4d57a8411737521615e3b1cf2126fc61fc326a7 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Tue, 9 Jul 2024 08:18:48 -0400 Subject: [PATCH 01/28] Some formatting in snd_opl_nuked.c --- src/sound/snd_opl_nuked.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index 95b61638e..1cc2bba9b 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -322,10 +322,10 @@ static const uint8_t kslshift[4] = { // envelope generator constants static const uint8_t eg_incstep[4][4] = { - {0, 0, 0, 0}, - { 1, 0, 0, 0}, - { 1, 0, 1, 0}, - { 1, 1, 1, 0} + { 0, 0, 0, 0 }, + { 1, 0, 0, 0 }, + { 1, 0, 1, 0 }, + { 1, 1, 1, 0 } }; // address decoding @@ -500,7 +500,8 @@ static const env_sinfunc env_sin[8] = { static void env_update_ksl(slot_t *slot) { - int16_t ksl = (kslrom[slot->chan->f_num >> 6] << 2) - ((0x08 - slot->chan->block) << 5); + int16_t ksl = (kslrom[slot->chan->f_num >> 6] << 2) + - ((0x08 - slot->chan->block) << 5); if (ksl < 0) ksl = 0; @@ -524,7 +525,8 @@ env_calc(slot_t *slot) uint8_t eg_off; uint8_t reset = 0; - slot->eg_out = slot->eg_rout + (slot->reg_tl << 2) + (slot->eg_ksl >> kslshift[slot->reg_ksl]) + *slot->trem; + slot->eg_out = slot->eg_rout + (slot->reg_tl << 2) + + (slot->eg_ksl >> kslshift[slot->reg_ksl]) + *slot->trem; if (slot->key && slot->eg_gen == envelope_gen_num_release) { reset = 1; reg_rate = slot->reg_ar; @@ -702,7 +704,9 @@ phase_generate(slot_t *slot) dev->rm_tc_bit5 = (phase >> 5) & 1; } if (dev->rhy & 0x20) { - rm_xor = (dev->rm_hh_bit2 ^ dev->rm_hh_bit7) | (dev->rm_hh_bit3 ^ dev->rm_tc_bit5) | (dev->rm_tc_bit3 ^ dev->rm_tc_bit5); + rm_xor = (dev->rm_hh_bit2 ^ dev->rm_hh_bit7) + | (dev->rm_hh_bit3 ^ dev->rm_tc_bit5) + | (dev->rm_tc_bit3 ^ dev->rm_tc_bit5); switch (slot->slot_num) { case 13: // hh @@ -714,7 +718,8 @@ phase_generate(slot_t *slot) break; case 16: // sd - slot->pg_phase_out = (dev->rm_hh_bit8 << 9) | ((dev->rm_hh_bit8 ^ (noise & 1)) << 8); + slot->pg_phase_out = (dev->rm_hh_bit8 << 9) + | ((dev->rm_hh_bit8 ^ (noise & 1)) << 8); break; case 17: // tc @@ -991,7 +996,8 @@ channel_write_a0(chan_t *ch, uint8_t data) return; ch->f_num = (ch->f_num & 0x300) | data; - ch->ksv = (ch->block << 1) | ((ch->f_num >> (0x09 - ch->dev->nts)) & 0x01); + ch->ksv = (ch->block << 1) + | ((ch->f_num >> (0x09 - ch->dev->nts)) & 0x01); env_update_ksl(ch->slots[0]); env_update_ksl(ch->slots[1]); @@ -1013,7 +1019,8 @@ channel_write_b0(chan_t *ch, uint8_t data) ch->f_num = (ch->f_num & 0xff) | ((data & 0x03) << 8); ch->block = (data >> 2) & 0x07; - ch->ksv = (ch->block << 1) | ((ch->f_num >> (0x09 - ch->dev->nts)) & 0x01); + ch->ksv = (ch->block << 1) + | ((ch->f_num >> (0x09 - ch->dev->nts)) & 0x01); env_update_ksl(ch->slots[0]); env_update_ksl(ch->slots[1]); From 4f020f9a834a51aac7bb59138458eaeb7be08c96 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Tue, 9 Jul 2024 08:19:37 -0400 Subject: [PATCH 02/28] Update Nuke.YKT's copyright date --- src/sound/snd_opl_nuked.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index 1cc2bba9b..b1c30ef31 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -35,7 +35,7 @@ * * Copyright 2017-2020 Fred N. van Kempen. * Copyright 2016-2020 Miran Grca. - * Copyright 2013-2018 Alexey Khokholov (Nuke.YKT) + * Copyright 2013-2020 Alexey Khokholov (Nuke.YKT) */ #include #include From 24b39c9c8fe6522ab4f2b99f21c4bd5f6e6f74f3 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Mon, 8 Jul 2024 19:43:25 -0400 Subject: [PATCH 03/28] Move stuff around to accomodate nukedopl update --- src/sound/snd_opl_nuked.c | 219 +++++++++++++++++++------------------- 1 file changed, 112 insertions(+), 107 deletions(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index b1c30ef31..d8c8a948c 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -804,114 +804,10 @@ slot_calc_fb(slot_t *slot) slot->prout = slot->out; } +// Channel + static void -channel_setup_alg(chan_t *ch) -{ - if (ch->chtype == ch_drum) { - if (ch->ch_num == 7 || ch->ch_num == 8) { - ch->slots[0]->mod = &ch->dev->zeromod; - ch->slots[1]->mod = &ch->dev->zeromod; - return; - } - - switch (ch->alg & 0x01) { - case 0x00: - ch->slots[0]->mod = &ch->slots[0]->fbmod; - ch->slots[1]->mod = &ch->slots[0]->out; - break; - - case 0x01: - ch->slots[0]->mod = &ch->slots[0]->fbmod; - ch->slots[1]->mod = &ch->dev->zeromod; - break; - - default: - break; - } - return; - } - - if (ch->alg & 0x08) - return; - - if (ch->alg & 0x04) { - ch->pair->out[0] = &ch->dev->zeromod; - ch->pair->out[1] = &ch->dev->zeromod; - ch->pair->out[2] = &ch->dev->zeromod; - ch->pair->out[3] = &ch->dev->zeromod; - - switch (ch->alg & 0x03) { - case 0x00: - ch->pair->slots[0]->mod = &ch->pair->slots[0]->fbmod; - ch->pair->slots[1]->mod = &ch->pair->slots[0]->out; - ch->slots[0]->mod = &ch->pair->slots[1]->out; - ch->slots[1]->mod = &ch->slots[0]->out; - ch->out[0] = &ch->slots[1]->out; - ch->out[1] = &ch->dev->zeromod; - ch->out[2] = &ch->dev->zeromod; - ch->out[3] = &ch->dev->zeromod; - break; - - case 0x01: - ch->pair->slots[0]->mod = &ch->pair->slots[0]->fbmod; - ch->pair->slots[1]->mod = &ch->pair->slots[0]->out; - ch->slots[0]->mod = &ch->dev->zeromod; - ch->slots[1]->mod = &ch->slots[0]->out; - ch->out[0] = &ch->pair->slots[1]->out; - ch->out[1] = &ch->slots[1]->out; - ch->out[2] = &ch->dev->zeromod; - ch->out[3] = &ch->dev->zeromod; - break; - - case 0x02: - ch->pair->slots[0]->mod = &ch->pair->slots[0]->fbmod; - ch->pair->slots[1]->mod = &ch->dev->zeromod; - ch->slots[0]->mod = &ch->pair->slots[1]->out; - ch->slots[1]->mod = &ch->slots[0]->out; - ch->out[0] = &ch->pair->slots[0]->out; - ch->out[1] = &ch->slots[1]->out; - ch->out[2] = &ch->dev->zeromod; - ch->out[3] = &ch->dev->zeromod; - break; - - case 0x03: - ch->pair->slots[0]->mod = &ch->pair->slots[0]->fbmod; - ch->pair->slots[1]->mod = &ch->dev->zeromod; - ch->slots[0]->mod = &ch->pair->slots[1]->out; - ch->slots[1]->mod = &ch->dev->zeromod; - ch->out[0] = &ch->pair->slots[0]->out; - ch->out[1] = &ch->slots[0]->out; - ch->out[2] = &ch->slots[1]->out; - ch->out[3] = &ch->dev->zeromod; - break; - - default: - break; - } - } else - switch (ch->alg & 0x01) { - case 0x00: - ch->slots[0]->mod = &ch->slots[0]->fbmod; - ch->slots[1]->mod = &ch->slots[0]->out; - ch->out[0] = &ch->slots[1]->out; - ch->out[1] = &ch->dev->zeromod; - ch->out[2] = &ch->dev->zeromod; - ch->out[3] = &ch->dev->zeromod; - break; - - case 0x01: - ch->slots[0]->mod = &ch->slots[0]->fbmod; - ch->slots[1]->mod = &ch->dev->zeromod; - ch->out[0] = &ch->slots[0]->out; - ch->out[1] = &ch->slots[1]->out; - ch->out[2] = &ch->dev->zeromod; - ch->out[3] = &ch->dev->zeromod; - break; - - default: - break; - } -} +channel_setup_alg(chan_t *ch); static void channel_update_rhythm(nuked_t *dev, uint8_t data) @@ -1035,6 +931,115 @@ channel_write_b0(chan_t *ch, uint8_t data) } } +static void +channel_setup_alg(chan_t *ch) +{ + if (ch->chtype == ch_drum) { + if (ch->ch_num == 7 || ch->ch_num == 8) { + ch->slots[0]->mod = &ch->dev->zeromod; + ch->slots[1]->mod = &ch->dev->zeromod; + return; + } + + switch (ch->alg & 0x01) { + case 0x00: + ch->slots[0]->mod = &ch->slots[0]->fbmod; + ch->slots[1]->mod = &ch->slots[0]->out; + break; + + case 0x01: + ch->slots[0]->mod = &ch->slots[0]->fbmod; + ch->slots[1]->mod = &ch->dev->zeromod; + break; + + default: + break; + } + return; + } + + if (ch->alg & 0x08) + return; + + if (ch->alg & 0x04) { + ch->pair->out[0] = &ch->dev->zeromod; + ch->pair->out[1] = &ch->dev->zeromod; + ch->pair->out[2] = &ch->dev->zeromod; + ch->pair->out[3] = &ch->dev->zeromod; + + switch (ch->alg & 0x03) { + case 0x00: + ch->pair->slots[0]->mod = &ch->pair->slots[0]->fbmod; + ch->pair->slots[1]->mod = &ch->pair->slots[0]->out; + ch->slots[0]->mod = &ch->pair->slots[1]->out; + ch->slots[1]->mod = &ch->slots[0]->out; + ch->out[0] = &ch->slots[1]->out; + ch->out[1] = &ch->dev->zeromod; + ch->out[2] = &ch->dev->zeromod; + ch->out[3] = &ch->dev->zeromod; + break; + + case 0x01: + ch->pair->slots[0]->mod = &ch->pair->slots[0]->fbmod; + ch->pair->slots[1]->mod = &ch->pair->slots[0]->out; + ch->slots[0]->mod = &ch->dev->zeromod; + ch->slots[1]->mod = &ch->slots[0]->out; + ch->out[0] = &ch->pair->slots[1]->out; + ch->out[1] = &ch->slots[1]->out; + ch->out[2] = &ch->dev->zeromod; + ch->out[3] = &ch->dev->zeromod; + break; + + case 0x02: + ch->pair->slots[0]->mod = &ch->pair->slots[0]->fbmod; + ch->pair->slots[1]->mod = &ch->dev->zeromod; + ch->slots[0]->mod = &ch->pair->slots[1]->out; + ch->slots[1]->mod = &ch->slots[0]->out; + ch->out[0] = &ch->pair->slots[0]->out; + ch->out[1] = &ch->slots[1]->out; + ch->out[2] = &ch->dev->zeromod; + ch->out[3] = &ch->dev->zeromod; + break; + + case 0x03: + ch->pair->slots[0]->mod = &ch->pair->slots[0]->fbmod; + ch->pair->slots[1]->mod = &ch->dev->zeromod; + ch->slots[0]->mod = &ch->pair->slots[1]->out; + ch->slots[1]->mod = &ch->dev->zeromod; + ch->out[0] = &ch->pair->slots[0]->out; + ch->out[1] = &ch->slots[0]->out; + ch->out[2] = &ch->slots[1]->out; + ch->out[3] = &ch->dev->zeromod; + break; + + default: + break; + } + } else + switch (ch->alg & 0x01) { + case 0x00: + ch->slots[0]->mod = &ch->slots[0]->fbmod; + ch->slots[1]->mod = &ch->slots[0]->out; + ch->out[0] = &ch->slots[1]->out; + ch->out[1] = &ch->dev->zeromod; + ch->out[2] = &ch->dev->zeromod; + ch->out[3] = &ch->dev->zeromod; + break; + + case 0x01: + ch->slots[0]->mod = &ch->slots[0]->fbmod; + ch->slots[1]->mod = &ch->dev->zeromod; + ch->out[0] = &ch->slots[0]->out; + ch->out[1] = &ch->slots[1]->out; + ch->out[2] = &ch->dev->zeromod; + ch->out[3] = &ch->dev->zeromod; + break; + + default: + break; + } +} + static void channel_write_c0(chan_t *ch, uint8_t data) { From 3bc9d82f67cb6526bd87478b7ac2a7a16d97fbb7 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Mon, 8 Jul 2024 21:14:02 -0400 Subject: [PATCH 04/28] get rid of left shift Co-Authored-By: nukeykt --- src/sound/snd_opl_nuked.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index d8c8a948c..b1540a94c 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -612,7 +612,7 @@ env_calc(slot_t *slot) if (!slot->eg_rout) slot->eg_gen = envelope_gen_num_decay; else if (slot->key && shift > 0 && rate_hi != 0x0f) - eg_inc = ((~slot->eg_rout) << shift) >> 4; + eg_inc = ~slot->eg_rout >> (4 - shift); break; case envelope_gen_num_decay: From 39e6b0fb2afefe4ee5fb80f1da04102177628a66 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Mon, 8 Jul 2024 23:06:54 -0400 Subject: [PATCH 05/28] Factor out repeated structure accesses into local variables Co-Authored-By: Evan Ramos <1585791+hendricks266@users.noreply.github.com> --- src/sound/snd_opl_nuked.c | 163 ++++++++++++++++++++++---------------- 1 file changed, 94 insertions(+), 69 deletions(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index b1540a94c..baeb9da47 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -1239,88 +1239,108 @@ nuked_write_reg_buffered(void *priv, uint16_t reg, uint8_t val) nuked_t *dev = (nuked_t *) priv; uint64_t time1; uint64_t time2; + wrbuf_t *writebuf; + uint32_t writebuf_last; - if (dev->wrbuf[dev->wrbuf_last].reg & 0x0200) { - nuked_write_reg(dev, dev->wrbuf[dev->wrbuf_last].reg & 0x01ff, - dev->wrbuf[dev->wrbuf_last].data); + writebuf_last = dev->wrbuf_last; + writebuf = &dev->wrbuf[writebuf_last]; - dev->wrbuf_cur = (dev->wrbuf_last + 1) % WRBUF_SIZE; - dev->wrbuf_samplecnt = dev->wrbuf[dev->wrbuf_last].time; + if (writebuf->reg & 0x0200) { + nuked_write_reg(dev, writebuf->reg & 0x01ff, writebuf->data); + + dev->wrbuf_cur = (writebuf_last + 1) % WRBUF_SIZE; + dev->wrbuf_samplecnt = writebuf->time; } - dev->wrbuf[dev->wrbuf_last].reg = reg | 0x0200; - dev->wrbuf[dev->wrbuf_last].data = val; - time1 = dev->wrbuf_lasttime + WRBUF_DELAY; - time2 = dev->wrbuf_samplecnt; + writebuf->reg = reg | 0x0200; + writebuf->data = val; + time1 = dev->wrbuf_lasttime + WRBUF_DELAY; + time2 = dev->wrbuf_samplecnt; if (time1 < time2) time1 = time2; - dev->wrbuf[dev->wrbuf_last].time = time1; - dev->wrbuf_lasttime = time1; - dev->wrbuf_last = (dev->wrbuf_last + 1) % WRBUF_SIZE; + writebuf->time = time1; + dev->wrbuf_lasttime = time1; + dev->wrbuf_last = (writebuf_last + 1) % WRBUF_SIZE; } void nuked_generate(void *priv, int32_t *bufp) { nuked_t *dev = (nuked_t *) priv; - int16_t accm; - int16_t shift = 0; - uint8_t i; - uint8_t j; + slot_t *slot; + chan_t *ch; + wrbuf_t *writebuf; + int16_t **out; + int32_t mix; + int16_t accm; + int16_t shift = 0; + uint8_t i; + uint8_t j; bufp[1] = dev->mixbuff[1]; for (i = 0; i < 15; i++) { - slot_calc_fb(&dev->slot[i]); - env_calc(&dev->slot[i]); - phase_generate(&dev->slot[i]); - slot_generate(&dev->slot[i]); + slot = &dev->slot[i]; + slot_calc_fb(slot); + env_calc(slot); + phase_generate(slot); + slot_generate(slot); } - dev->mixbuff[0] = 0; + mix = 0; for (i = 0; i < 18; i++) { - accm = 0; - + ch = &dev->chan[i]; + out = ch->out; for (j = 0; j < 4; j++) - accm += *dev->chan[i].out[j]; + accm += *out[j]; - dev->mixbuff[0] += (int16_t) (accm & dev->chan[i].cha); + mix += (int16_t) (accm & ch->cha); } + + dev->mixbuff[0] = mix; + for (i = 15; i < 18; i++) { - slot_calc_fb(&dev->slot[i]); - env_calc(&dev->slot[i]); - phase_generate(&dev->slot[i]); - slot_generate(&dev->slot[i]); + slot = &dev->slot[i]; + slot_calc_fb(slot); + env_calc(slot); + phase_generate(slot); + slot_generate(slot); } bufp[0] = dev->mixbuff[0]; for (i = 18; i < 33; i++) { - slot_calc_fb(&dev->slot[i]); - env_calc(&dev->slot[i]); - phase_generate(&dev->slot[i]); - slot_generate(&dev->slot[i]); + slot = &dev->slot[i]; + slot_calc_fb(slot); + env_calc(slot); + phase_generate(slot); + slot_generate(slot); } - dev->mixbuff[1] = 0; + mix = 0; for (i = 0; i < 18; i++) { + ch = &dev->chan[i]; + out = ch->out; accm = 0; for (j = 0; j < 4; j++) - accm += *dev->chan[i].out[j]; + accm += *out[j]; - dev->mixbuff[1] += (int16_t) (accm & dev->chan[i].chb); + mix += (int16_t) (accm & ch->chb); } + dev->mixbuff[1] = mix; + for (i = 33; i < 36; i++) { - slot_calc_fb(&dev->slot[i]); - env_calc(&dev->slot[i]); - phase_generate(&dev->slot[i]); - slot_generate(&dev->slot[i]); + slot = &dev->slot[i]; + slot_calc_fb(slot); + env_calc(slot); + phase_generate(slot); + slot_generate(slot); } if ((dev->timer & 0x3f) == 0x3f) @@ -1359,14 +1379,13 @@ nuked_generate(void *priv, int32_t *bufp) dev->eg_state ^= 1; - while (dev->wrbuf[dev->wrbuf_cur].time <= dev->wrbuf_samplecnt) { - if (!(dev->wrbuf[dev->wrbuf_cur].reg & 0x200)) + while (writebuf = &dev->wrbuf[dev->wrbuf_cur], writebuf->time <= dev->wrbuf_samplecnt) { + if (!(writebuf->reg & 0x200)) break; - dev->wrbuf[dev->wrbuf_cur].reg &= 0x01ff; + writebuf->reg &= 0x01ff; - nuked_write_reg(dev, dev->wrbuf[dev->wrbuf_cur].reg, - dev->wrbuf[dev->wrbuf_cur].data); + nuked_write_reg(dev, writebuf->reg, writebuf->data); dev->wrbuf_cur = (dev->wrbuf_cur + 1) % WRBUF_SIZE; } @@ -1415,42 +1434,48 @@ nuked_generate_stream(nuked_t *dev, int32_t *sndptr, uint32_t num) void nuked_init(nuked_t *dev, uint32_t samplerate) { + slot_t *slot; + chan_t *ch; uint8_t i; + uint8_t local_ch_slot; memset(dev, 0x00, sizeof(nuked_t)); for (i = 0; i < 36; i++) { - dev->slot[i].dev = dev; - dev->slot[i].mod = &dev->zeromod; - dev->slot[i].eg_rout = 0x01ff; - dev->slot[i].eg_out = 0x01ff; - dev->slot[i].eg_gen = envelope_gen_num_release; - dev->slot[i].trem = (uint8_t *) &dev->zeromod; - dev->slot[i].slot_num = i; + slot = &dev->slot[i]; + slot->dev = dev; + slot->mod = &dev->zeromod; + slot->eg_rout = 0x01ff; + slot->eg_out = 0x01ff; + slot->eg_gen = envelope_gen_num_release; + slot->trem = (uint8_t *) &dev->zeromod; + slot->slot_num = i; } for (i = 0; i < 18; i++) { - dev->chan[i].slots[0] = &dev->slot[ch_slot[i]]; - dev->chan[i].slots[1] = &dev->slot[ch_slot[i] + 3]; - dev->slot[ch_slot[i]].chan = &dev->chan[i]; - dev->slot[ch_slot[i] + 3].chan = &dev->chan[i]; + ch = &dev->chan[i]; + local_ch_slot = ch_slot[i]; + ch->slots[0] = &dev->slot[local_ch_slot]; + ch->slots[1] = &dev->slot[local_ch_slot + 3]; + dev->slot[local_ch_slot].chan = ch; + dev->slot[local_ch_slot + 3].chan = ch; if ((i % 9) < 3) - dev->chan[i].pair = &dev->chan[i + 3]; + ch->pair = &dev->chan[i + 3]; else if ((i % 9) < 6) - dev->chan[i].pair = &dev->chan[i - 3]; + ch->pair = &dev->chan[i - 3]; - dev->chan[i].dev = dev; - dev->chan[i].out[0] = &dev->zeromod; - dev->chan[i].out[1] = &dev->zeromod; - dev->chan[i].out[2] = &dev->zeromod; - dev->chan[i].out[3] = &dev->zeromod; - dev->chan[i].chtype = ch_2op; - dev->chan[i].cha = 0xffff; - dev->chan[i].chb = 0xffff; - dev->chan[i].ch_num = i; + ch->dev = dev; + ch->out[0] = &dev->zeromod; + ch->out[1] = &dev->zeromod; + ch->out[2] = &dev->zeromod; + ch->out[3] = &dev->zeromod; + ch->chtype = ch_2op; + ch->cha = 0xffff; + ch->chb = 0xffff; + ch->ch_num = i; - channel_setup_alg(&dev->chan[i]); + channel_setup_alg(ch); } dev->noise = 1; @@ -1684,4 +1709,4 @@ const fm_drv_t nuked_opl_drv = { &nuked_drv_set_do_cycles, NULL, NULL, -}; \ No newline at end of file +}; From e6466c4d252c4b9d6a91da9c98a43183f32d2eb3 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Tue, 9 Jul 2024 22:40:24 -0400 Subject: [PATCH 06/28] Unroll accumulator loops Co-Authored-By: Evan Ramos <1585791+hendricks266@users.noreply.github.com> --- src/sound/snd_opl_nuked.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index baeb9da47..23da37a07 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -1277,7 +1277,6 @@ nuked_generate(void *priv, int32_t *bufp) int16_t accm; int16_t shift = 0; uint8_t i; - uint8_t j; bufp[1] = dev->mixbuff[1]; @@ -1294,9 +1293,7 @@ nuked_generate(void *priv, int32_t *bufp) for (i = 0; i < 18; i++) { ch = &dev->chan[i]; out = ch->out; - for (j = 0; j < 4; j++) - accm += *out[j]; - + accm = *out[0] + *out[1] + *out[2] + *out[3]; mix += (int16_t) (accm & ch->cha); } @@ -1325,11 +1322,7 @@ nuked_generate(void *priv, int32_t *bufp) for (i = 0; i < 18; i++) { ch = &dev->chan[i]; out = ch->out; - accm = 0; - - for (j = 0; j < 4; j++) - accm += *out[j]; - + accm = *out[0] + *out[1] + *out[2] + *out[3]; mix += (int16_t) (accm & ch->chb); } From 6332fd0b5ef4651f2e66bbf1b7b167b1b17705c6 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Tue, 9 Jul 2024 22:46:13 -0400 Subject: [PATCH 07/28] Factor slot loop bodies in nuked_generate into process_slot Co-Authored-By: Evan Ramos <1585791+hendricks266@users.noreply.github.com> --- src/sound/snd_opl_nuked.c | 45 ++++++++++++++------------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index 23da37a07..6134910b9 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -1265,11 +1265,18 @@ nuked_write_reg_buffered(void *priv, uint16_t reg, uint8_t val) dev->wrbuf_last = (writebuf_last + 1) % WRBUF_SIZE; } +static void process_slot(slot_t *slot) +{ + slot_calc_fb(slot); + env_calc(slot); + phase_generate(slot); + slot_generate(slot); +} + void nuked_generate(void *priv, int32_t *bufp) { nuked_t *dev = (nuked_t *) priv; - slot_t *slot; chan_t *ch; wrbuf_t *writebuf; int16_t **out; @@ -1280,13 +1287,8 @@ nuked_generate(void *priv, int32_t *bufp) bufp[1] = dev->mixbuff[1]; - for (i = 0; i < 15; i++) { - slot = &dev->slot[i]; - slot_calc_fb(slot); - env_calc(slot); - phase_generate(slot); - slot_generate(slot); - } + for (i = 0; i < 15; i++) + process_slot(&dev->slot[i]); mix = 0; @@ -1299,23 +1301,13 @@ nuked_generate(void *priv, int32_t *bufp) dev->mixbuff[0] = mix; - for (i = 15; i < 18; i++) { - slot = &dev->slot[i]; - slot_calc_fb(slot); - env_calc(slot); - phase_generate(slot); - slot_generate(slot); - } + for (i = 15; i < 18; i++) + process_slot(&dev->slot[i]); bufp[0] = dev->mixbuff[0]; - for (i = 18; i < 33; i++) { - slot = &dev->slot[i]; - slot_calc_fb(slot); - env_calc(slot); - phase_generate(slot); - slot_generate(slot); - } + for (i = 18; i < 33; i++) + process_slot(&dev->slot[i]); mix = 0; @@ -1328,13 +1320,8 @@ nuked_generate(void *priv, int32_t *bufp) dev->mixbuff[1] = mix; - for (i = 33; i < 36; i++) { - slot = &dev->slot[i]; - slot_calc_fb(slot); - env_calc(slot); - phase_generate(slot); - slot_generate(slot); - } + for (i = 33; i < 36; i++) + process_slot(&dev->slot[i]); if ((dev->timer & 0x3f) == 0x3f) dev->tremolopos = (dev->tremolopos + 1) % 210; From 87cdb6ac2892aa3e5a9525d7c21ea595f8ae131d Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Tue, 9 Jul 2024 23:15:51 -0400 Subject: [PATCH 08/28] Add toggle to disable uneven L/R sample timing quirk Co-Authored-By: Evan Ramos <1585791+hendricks266@users.noreply.github.com> --- src/sound/snd_opl_nuked.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index 6134910b9..9468622f4 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -51,6 +51,11 @@ #include <86box/device.h> #include <86box/snd_opl.h> +/* Quirk: Some FM channels are output one sample later on the left side than the right. */ +#ifndef OPL_QUIRK_CHANNELSAMPLEDELAY +#define OPL_QUIRK_CHANNELSAMPLEDELAY 1 +#endif + #define WRBUF_SIZE 1024 #define WRBUF_DELAY 1 #define RSM_FRAC 10 @@ -1287,7 +1292,11 @@ nuked_generate(void *priv, int32_t *bufp) bufp[1] = dev->mixbuff[1]; +#if OPL_QUIRK_CHANNELSAMPLEDELAY for (i = 0; i < 15; i++) +#else + for (i = 0; i < 36; i++) +#endif process_slot(&dev->slot[i]); mix = 0; @@ -1301,13 +1310,17 @@ nuked_generate(void *priv, int32_t *bufp) dev->mixbuff[0] = mix; +#if OPL_QUIRK_CHANNELSAMPLEDELAY for (i = 15; i < 18; i++) process_slot(&dev->slot[i]); +#endif bufp[0] = dev->mixbuff[0]; +#if OPL_QUIRK_CHANNELSAMPLEDELAY for (i = 18; i < 33; i++) process_slot(&dev->slot[i]); +#endif mix = 0; @@ -1320,8 +1333,10 @@ nuked_generate(void *priv, int32_t *bufp) dev->mixbuff[1] = mix; +#if OPL_QUIRK_CHANNELSAMPLEDELAY for (i = 33; i < 36; i++) process_slot(&dev->slot[i]); +#endif if ((dev->timer & 0x3f) == 0x3f) dev->tremolopos = (dev->tremolopos + 1) % 210; From 996e5cc7847a8a36a0d76fa4db1828e05650142d Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Wed, 10 Jul 2024 02:23:19 -0400 Subject: [PATCH 09/28] Add stereo extension Co-Authored-By: Evan Ramos <1585791+hendricks266@users.noreply.github.com> Co-Authored-By: nukeykt --- src/sound/snd_opl_nuked.c | 80 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index 9468622f4..59f9104f3 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -51,9 +51,21 @@ #include <86box/device.h> #include <86box/snd_opl.h> + +#ifndef OPL_ENABLE_STEREOEXT +#define OPL_ENABLE_STEREOEXT 0 +#endif + +#if OPL_ENABLE_STEREOEXT +#ifndef _USE_MATH_DEFINES +#define _USE_MATH_DEFINES 1 +#endif +#include +#endif + /* Quirk: Some FM channels are output one sample later on the left side than the right. */ #ifndef OPL_QUIRK_CHANNELSAMPLEDELAY -#define OPL_QUIRK_CHANNELSAMPLEDELAY 1 +#define OPL_QUIRK_CHANNELSAMPLEDELAY (!OPL_ENABLE_STEREOEXT) #endif #define WRBUF_SIZE 1024 @@ -124,6 +136,10 @@ typedef struct chan { struct chan *pair; struct chip *dev; int16_t *out[4]; +#if OPL_ENABLE_STEREOEXT + int32_t leftpan; + int32_t rightpan; +#endif uint8_t chtype; uint16_t f_num; uint8_t block; @@ -168,6 +184,10 @@ typedef struct chip { uint8_t rm_tc_bit3; uint8_t rm_tc_bit5; +#if OPL_ENABLE_STEREOEXT + uint8_t stereoext; +#endif + // OPL3L int32_t rateratio; int32_t samplecnt; @@ -343,6 +363,15 @@ static const uint8_t ch_slot[18] = { 0, 1, 2, 6, 7, 8, 12, 13, 14, 18, 19, 20, 24, 25, 26, 30, 31, 32 }; +#if OPL_ENABLE_STEREOEXT +/* + stereo extension panning table +*/ + +static int32_t panpot_lut[256]; +static uint8_t panpot_lut_build = 0; +#endif + // Envelope generator typedef int16_t (*env_sinfunc)(uint16_t phase, uint16_t envelope); typedef void (*env_genfunc)(slot_t *slot); @@ -1071,8 +1100,26 @@ channel_write_c0(chan_t *ch, uint8_t data) ch->chb = ((data >> 5) & 0x01) ? ~0 : 0; } else ch->cha = ch->chb = (uint16_t) ~0; + +#if OPL_ENABLE_STEREOEXT + if (!ch->dev->stereoext) { + ch->leftpan = ch->cha << 16; + ch->rightpan = ch->chb << 16; + } +#endif } +#if OPL_ENABLE_STEREOEXT +static void +channel_write_d0(chan_t *ch, uint8_t data) +{ + if (ch->dev->stereoext) { + ch->leftpan = panpot_lut[data ^ 0xff]; + ch->rightpan = panpot_lut[data]; + } +} +#endif + static void channel_key_on(chan_t *ch) { @@ -1162,6 +1209,9 @@ nuked_write_reg(void *priv, uint16_t reg, uint8_t val) case 0x05: dev->newm = val & 0x01; +#if OPL_ENABLE_STEREOEXT + dev->stereoext = (val >> 1) & 0x01; +#endif break; default: @@ -1227,6 +1277,13 @@ nuked_write_reg(void *priv, uint16_t reg, uint8_t val) channel_write_c0(&dev->chan[9 * high + (regm & 0x0f)], val); break; +#if OPL_ENABLE_STEREOEXT + case 0xd0: + if ((regm & 0x0f) < 9) + channel_write_d0(&dev->chan[9 * high + (regm & 0x0f)], val); + break; +#endif + case 0xe0: case 0xf0: if (ad_slot[regm & 0x1f] >= 0) @@ -1305,7 +1362,11 @@ nuked_generate(void *priv, int32_t *bufp) ch = &dev->chan[i]; out = ch->out; accm = *out[0] + *out[1] + *out[2] + *out[3]; +#if OPL_ENABLE_STEREOEXT + mix += (int16_t)((accm * ch->leftpan) >> 16); +#else mix += (int16_t) (accm & ch->cha); +#endif } dev->mixbuff[0] = mix; @@ -1328,7 +1389,11 @@ nuked_generate(void *priv, int32_t *bufp) ch = &dev->chan[i]; out = ch->out; accm = *out[0] + *out[1] + *out[2] + *out[3]; +#if OPL_ENABLE_STEREOEXT + mix += (int16_t)((accm * ch->leftpan) >> 16); +#else mix += (int16_t) (accm & ch->chb); +#endif } dev->mixbuff[1] = mix; @@ -1468,6 +1533,10 @@ nuked_init(nuked_t *dev, uint32_t samplerate) ch->chtype = ch_2op; ch->cha = 0xffff; ch->chb = 0xffff; +#if OPL_ENABLE_STEREOEXT + ch->leftpan = 0x10000; + ch->rightpan = 0x10000; +#endif ch->ch_num = i; channel_setup_alg(ch); @@ -1477,6 +1546,15 @@ nuked_init(nuked_t *dev, uint32_t samplerate) dev->rateratio = (samplerate << RSM_FRAC) / 49716; dev->tremoloshift = 4; dev->vibshift = 1; + + +#if OPL_ENABLE_STEREOEXT + if (!panpot_lut_build) { + for (int32_t i = 0; i < 256; i++) + panpot_lut[i] = (int32_t)(sin(i * M_PI / 512.0) * 65536.0); + panpot_lut_build = 1; + } +#endif } static void From e5aa36919bf62cb51dd06e720e769ce95fab51e8 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Wed, 10 Jul 2024 04:11:26 -0400 Subject: [PATCH 10/28] Allow replacement of the sin() function used to populate the panning table Co-Authored-By: Evan Ramos <1585791+hendricks266@users.noreply.github.com> --- src/sound/snd_opl_nuked.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index 59f9104f3..e990ba460 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -56,11 +56,13 @@ #define OPL_ENABLE_STEREOEXT 0 #endif -#if OPL_ENABLE_STEREOEXT +#if OPL_ENABLE_STEREOEXT && !defined OPL_SIN #ifndef _USE_MATH_DEFINES #define _USE_MATH_DEFINES 1 #endif #include +// input: [0, 256), output: [0, 65536] +#define OPL_SIN(x) ((int32_t)(sin((x) * M_PI / 512.0) * 65536.0)) #endif /* Quirk: Some FM channels are output one sample later on the left side than the right. */ @@ -1551,7 +1553,7 @@ nuked_init(nuked_t *dev, uint32_t samplerate) #if OPL_ENABLE_STEREOEXT if (!panpot_lut_build) { for (int32_t i = 0; i < 256; i++) - panpot_lut[i] = (int32_t)(sin(i * M_PI / 512.0) * 65536.0); + panpot_lut[i] = OPL_SIN(i); panpot_lut_build = 1; } #endif From 0646a322e18606196cb1df03931ea1e0cab14bef Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Wed, 10 Jul 2024 04:52:06 -0400 Subject: [PATCH 11/28] Add 4-channel output support Co-Authored-By: Kagamiin --- src/sound/snd_opl_nuked.c | 111 +++++++++++++++++++++++++++++--------- 1 file changed, 86 insertions(+), 25 deletions(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index e990ba460..03c7a6b01 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -151,6 +151,8 @@ typedef struct chan { uint8_t ksv; uint16_t cha; uint16_t chb; + uint16_t chc; + uint16_t chd; uint8_t ch_num; } chan_t; @@ -178,7 +180,7 @@ typedef struct chip { uint8_t tremoloshift; uint32_t noise; int16_t zeromod; - int32_t mixbuff[2]; + int32_t mixbuff[4]; uint8_t rm_hh_bit2; uint8_t rm_hh_bit3; uint8_t rm_hh_bit7; @@ -193,8 +195,8 @@ typedef struct chip { // OPL3L int32_t rateratio; int32_t samplecnt; - int32_t oldsamples[2]; - int32_t samples[2]; + int32_t oldsamples[4]; + int32_t samples[4]; uint64_t wrbuf_samplecnt; uint32_t wrbuf_cur; @@ -1077,10 +1079,8 @@ channel_setup_alg(chan_t *ch) } static void -channel_write_c0(chan_t *ch, uint8_t data) +channel_update_alg(chan_t *ch) { - ch->fb = (data & 0x0e) >> 1; - ch->con = data & 0x01; ch->alg = ch->con; if (ch->dev->newm) { @@ -1096,12 +1096,24 @@ channel_write_c0(chan_t *ch, uint8_t data) channel_setup_alg(ch); } else channel_setup_alg(ch); +} + +static void +channel_write_c0(chan_t *ch, uint8_t data) +{ + ch->fb = (data & 0x0e) >> 1; + ch->con = data & 0x01; if (ch->dev->newm) { ch->cha = ((data >> 4) & 0x01) ? ~0 : 0; ch->chb = ((data >> 5) & 0x01) ? ~0 : 0; - } else + ch->chc = ((data >> 6) & 0x01) ? ~0 : 0; + ch->chd = ((data >> 7) & 0x01) ? ~0 : 0; + } else { ch->cha = ch->chb = (uint16_t) ~0; + // TODO: Verify on real chip if DAC2 output is disabled in compat mode + ch->chc = ch->chd = 0; + } #if OPL_ENABLE_STEREOEXT if (!ch->dev->stereoext) { @@ -1174,9 +1186,12 @@ channel_set_4op(nuked_t *dev, uint8_t data) if ((data >> bit) & 0x01) { dev->chan[chnum].chtype = ch_4op; dev->chan[chnum + 3].chtype = ch_4op2; + channel_update_alg(&dev->chan[chnum]); } else { dev->chan[chnum].chtype = ch_2op; dev->chan[chnum + 3].chtype = ch_2op; + channel_update_alg(&dev->chan[chnum]); + channel_update_alg(&dev->chan[chnum + 3]); } } } @@ -1337,19 +1352,20 @@ static void process_slot(slot_t *slot) slot_generate(slot); } -void -nuked_generate(void *priv, int32_t *bufp) +inline void +nuked_generate_4ch(void *priv, int32_t *buf4) { nuked_t *dev = (nuked_t *) priv; chan_t *ch; wrbuf_t *writebuf; int16_t **out; - int32_t mix; + int32_t mix[2]; int16_t accm; int16_t shift = 0; uint8_t i; - bufp[1] = dev->mixbuff[1]; + buf4[1] = dev->mixbuff[1]; + buf4[3] = dev->mixbuff[3]; #if OPL_QUIRK_CHANNELSAMPLEDELAY for (i = 0; i < 15; i++) @@ -1358,47 +1374,52 @@ nuked_generate(void *priv, int32_t *bufp) #endif process_slot(&dev->slot[i]); - mix = 0; + mix[0] = mix[1] = 0; for (i = 0; i < 18; i++) { ch = &dev->chan[i]; out = ch->out; accm = *out[0] + *out[1] + *out[2] + *out[3]; #if OPL_ENABLE_STEREOEXT - mix += (int16_t)((accm * ch->leftpan) >> 16); + mix[0] += (int16_t)((accm * ch->leftpan) >> 16); #else - mix += (int16_t) (accm & ch->cha); + mix[0] += (int16_t) (accm & ch->cha); #endif + mix[1] += (int16_t) (accm & ch->chc); } - dev->mixbuff[0] = mix; + dev->mixbuff[0] = mix[0]; + dev->mixbuff[2] = mix[1]; #if OPL_QUIRK_CHANNELSAMPLEDELAY for (i = 15; i < 18; i++) process_slot(&dev->slot[i]); #endif - bufp[0] = dev->mixbuff[0]; + buf4[0] = dev->mixbuff[0]; + buf4[2] = dev->mixbuff[2]; #if OPL_QUIRK_CHANNELSAMPLEDELAY for (i = 18; i < 33; i++) process_slot(&dev->slot[i]); #endif - mix = 0; + mix[0] = mix[1] = 0; for (i = 0; i < 18; i++) { ch = &dev->chan[i]; out = ch->out; accm = *out[0] + *out[1] + *out[2] + *out[3]; #if OPL_ENABLE_STEREOEXT - mix += (int16_t)((accm * ch->leftpan) >> 16); + mix[0] += (int16_t)((accm * ch->leftpan) >> 16); #else - mix += (int16_t) (accm & ch->chb); + mix[0] += (int16_t) (accm & ch->chb); #endif + mix[1] += (int16_t) (accm & ch->chd); } - dev->mixbuff[1] = mix; + dev->mixbuff[1] = mix[0]; + dev->mixbuff[3] = mix[1]; #if OPL_QUIRK_CHANNELSAMPLEDELAY for (i = 33; i < 36; i++) @@ -1455,26 +1476,50 @@ nuked_generate(void *priv, int32_t *bufp) dev->wrbuf_samplecnt++; } +void nuked_generate(nuked_t *dev, int32_t *buf4) +{ + int32_t samples[4]; + nuked_generate_4ch(dev, samples); + buf4[0] = samples[0]; + buf4[1] = samples[1]; +} + void -nuked_generate_resampled(nuked_t *dev, int32_t *bufp) +nuked_generate_4ch_resampled(nuked_t *dev, int16_t *buf4) { while (dev->samplecnt >= dev->rateratio) { dev->oldsamples[0] = dev->samples[0]; dev->oldsamples[1] = dev->samples[1]; - nuked_generate(dev, dev->samples); + dev->oldsamples[2] = dev->samples[2]; + dev->oldsamples[3] = dev->samples[3]; + nuked_generate_4ch(dev, dev->samples); dev->samplecnt -= dev->rateratio; } - bufp[0] = (int32_t) ((dev->oldsamples[0] * (dev->rateratio - dev->samplecnt) + buf4[0] = (int32_t) ((dev->oldsamples[0] * (dev->rateratio - dev->samplecnt) + dev->samples[0] * dev->samplecnt) / dev->rateratio); - bufp[1] = (int32_t) ((dev->oldsamples[1] * (dev->rateratio - dev->samplecnt) + buf4[1] = (int32_t) ((dev->oldsamples[1] * (dev->rateratio - dev->samplecnt) + dev->samples[1] * dev->samplecnt) / dev->rateratio); + buf4[2] = (int32_t) ((dev->oldsamples[2] * (dev->rateratio - dev->samplecnt) + + dev->samples[2] * dev->samplecnt) + / dev->rateratio); + buf4[3] = (int32_t) ((dev->oldsamples[3] * (dev->rateratio - dev->samplecnt) + + dev->samples[3] * dev->samplecnt) + / dev->rateratio); dev->samplecnt += 1 << RSM_FRAC; } +void nuked_generate_resampled(nuked_t *dev, int16_t *buf4) +{ + int16_t samples[4]; + nuked_generate_4ch_resampled(dev, samples); + buf4[0] = samples[0]; + buf4[1] = samples[1]; +} + void nuked_generate_raw(nuked_t *dev, int32_t *bufp) { @@ -1484,10 +1529,26 @@ nuked_generate_raw(nuked_t *dev, int32_t *bufp) bufp[1] = (int32_t) dev->samples[1]; } +void +nuked_generate_4ch_stream(nuked_t *dev, int16_t *sndptr1, int16_t *sndptr2, uint32_t numsamples) +{ + int16_t samples[4]; + + for (uint_fast32_t i = 0; i < numsamples; i++) { + nuked_generate_4ch_resampled(dev, samples); + sndptr1[0] = samples[0]; + sndptr1[1] = samples[1]; + sndptr2[0] = samples[2]; + sndptr2[1] = samples[3]; + sndptr1 += 2; + sndptr2 += 2; + } +} + void nuked_generate_stream(nuked_t *dev, int32_t *sndptr, uint32_t num) { - for (uint32_t i = 0; i < num; i++) { + for (uint_fast32_t i = 0; i < num; i++) { nuked_generate_raw(dev, sndptr); sndptr += 2; } From b5b5353d46837dcb9d20c9a8716df0118012dd27 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Wed, 10 Jul 2024 06:53:48 -0400 Subject: [PATCH 12/28] Use only unsigned literals inside array index math expressions Avoids OOBs as detected by UBSan Co-Authored-By: Evan Ramos <1585791+hendricks266@users.noreply.github.com> --- src/sound/snd_opl_nuked.c | 88 +++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index 03c7a6b01..a64dc196b 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -386,7 +386,7 @@ env_calc_exp(uint32_t level) if (level > 0x1fff) level = 0x1fff; - return ((exprom[level & 0xff] << 1) >> (level >> 8)); + return ((exprom[level & 0xffu] << 1) >> (level >> 8)); } static int16_t @@ -401,9 +401,9 @@ env_calc_sin0(uint16_t phase, uint16_t env) neg = 0xffff; if (phase & 0x0100) - out = logsinrom[(phase & 0xff) ^ 0xff]; + out = logsinrom[(phase & 0xffu) ^ 0xffu]; else - out = logsinrom[phase & 0xff]; + out = logsinrom[phase & 0xffu]; return (env_calc_exp(out + (env << 3)) ^ neg); } @@ -418,9 +418,9 @@ env_calc_sin1(uint16_t phase, uint16_t env) if (phase & 0x0200) out = 0x1000; else if (phase & 0x0100) - out = logsinrom[(phase & 0xff) ^ 0xff]; + out = logsinrom[(phase & 0xffu) ^ 0xffu]; else - out = logsinrom[phase & 0xff]; + out = logsinrom[phase & 0xffu]; return (env_calc_exp(out + (env << 3))); } @@ -433,9 +433,9 @@ env_calc_sin2(uint16_t phase, uint16_t env) phase &= 0x03ff; if (phase & 0x0100) - out = logsinrom[(phase & 0xff) ^ 0xff]; + out = logsinrom[(phase & 0xffu) ^ 0xffu]; else - out = logsinrom[phase & 0xff]; + out = logsinrom[phase & 0xffu]; return (env_calc_exp(out + (env << 3))); } @@ -450,7 +450,7 @@ env_calc_sin3(uint16_t phase, uint16_t env) if (phase & 0x0100) out = 0x1000; else - out = logsinrom[phase & 0xff]; + out = logsinrom[phase & 0xffu]; return (env_calc_exp(out + (env << 3))); } @@ -469,9 +469,9 @@ env_calc_sin4(uint16_t phase, uint16_t env) if (phase & 0x0200) out = 0x1000; else if (phase & 0x80) - out = logsinrom[((phase ^ 0xff) << 1) & 0xff]; + out = logsinrom[((phase ^ 0xffu) << 1u) & 0xffu]; else - out = logsinrom[(phase << 1) & 0xff]; + out = logsinrom[(phase << 1u) & 0xffu]; return (env_calc_exp(out + (env << 3)) ^ neg); } @@ -486,9 +486,9 @@ env_calc_sin5(uint16_t phase, uint16_t env) if (phase & 0x0200) out = 0x1000; else if (phase & 0x80) - out = logsinrom[((phase ^ 0xff) << 1) & 0xff]; + out = logsinrom[((phase ^ 0xffu) << 1u) & 0xffu]; else - out = logsinrom[(phase << 1) & 0xff]; + out = logsinrom[(phase << 1u) & 0xffu]; return (env_calc_exp(out + (env << 3))); } @@ -538,7 +538,7 @@ static const env_sinfunc env_sin[8] = { static void env_update_ksl(slot_t *slot) { - int16_t ksl = (kslrom[slot->chan->f_num >> 6] << 2) + int16_t ksl = (kslrom[slot->chan->f_num >> 6u] << 2) - ((0x08 - slot->chan->block) << 5); if (ksl < 0) @@ -622,7 +622,7 @@ env_calc(slot_t *slot) break; } } else { - shift = (rate_hi & 0x03) + eg_incstep[rate_lo][slot->dev->timer & 0x03]; + shift = (rate_hi & 0x03) + eg_incstep[rate_lo][slot->dev->timer & 0x03u]; if (shift & 0x04) shift = 0x03; if (!shift) @@ -1128,7 +1128,7 @@ static void channel_write_d0(chan_t *ch, uint8_t data) { if (ch->dev->stereoext) { - ch->leftpan = panpot_lut[data ^ 0xff]; + ch->leftpan = panpot_lut[data ^ 0xffu]; ch->rightpan = panpot_lut[data]; } } @@ -1184,14 +1184,14 @@ channel_set_4op(nuked_t *dev, uint8_t data) chnum += 9 - 3; if ((data >> bit) & 0x01) { - dev->chan[chnum].chtype = ch_4op; - dev->chan[chnum + 3].chtype = ch_4op2; + dev->chan[chnum].chtype = ch_4op; + dev->chan[chnum + 3u].chtype = ch_4op2; channel_update_alg(&dev->chan[chnum]); } else { - dev->chan[chnum].chtype = ch_2op; - dev->chan[chnum + 3].chtype = ch_2op; + dev->chan[chnum].chtype = ch_2op; + dev->chan[chnum + 3u].chtype = ch_2op; channel_update_alg(&dev->chan[chnum]); - channel_update_alg(&dev->chan[chnum + 3]); + channel_update_alg(&dev->chan[chnum + 3u]); } } } @@ -1247,31 +1247,31 @@ nuked_write_reg(void *priv, uint16_t reg, uint8_t val) case 0x20: case 0x30: - if (ad_slot[regm & 0x1f] >= 0) - slot_write_20(&dev->slot[18 * high + ad_slot[regm & 0x1f]], val); + if (ad_slot[regm & 0x1fu] >= 0) + slot_write_20(&dev->slot[18u * high + ad_slot[regm & 0x1fu]], val); break; case 0x40: case 0x50: - if (ad_slot[regm & 0x1f] >= 0) - slot_write_40(&dev->slot[18 * high + ad_slot[regm & 0x1f]], val); + if (ad_slot[regm & 0x1fu] >= 0) + slot_write_40(&dev->slot[18u * high + ad_slot[regm & 0x1fu]], val); break; case 0x60: case 0x70: - if (ad_slot[regm & 0x1f] >= 0) - slot_write_60(&dev->slot[18 * high + ad_slot[regm & 0x1f]], val); + if (ad_slot[regm & 0x1fu] >= 0) + slot_write_60(&dev->slot[18u * high + ad_slot[regm & 0x1fu]], val); break; case 0x80: case 0x90: - if (ad_slot[regm & 0x1f] >= 0) - slot_write_80(&dev->slot[18 * high + ad_slot[regm & 0x1f]], val); + if (ad_slot[regm & 0x1fu] >= 0) + slot_write_80(&dev->slot[18u * high + ad_slot[regm & 0x1fu]], val); break; case 0xa0: if ((regm & 0x0f) < 9) - channel_write_a0(&dev->chan[9 * high + (regm & 0x0f)], val); + channel_write_a0(&dev->chan[9u * high + (regm & 0x0fu)], val); break; case 0xb0: @@ -1280,31 +1280,31 @@ nuked_write_reg(void *priv, uint16_t reg, uint8_t val) dev->vibshift = ((val >> 6) & 0x01) ^ 1; channel_update_rhythm(dev, val); } else if ((regm & 0x0f) < 9) { - channel_write_b0(&dev->chan[9 * high + (regm & 0x0f)], val); + channel_write_b0(&dev->chan[9u * high + (regm & 0x0fu)], val); if (val & 0x20) - channel_key_on(&dev->chan[9 * high + (regm & 0x0f)]); + channel_key_on(&dev->chan[9u * high + (regm & 0x0fu)]); else - channel_key_off(&dev->chan[9 * high + (regm & 0x0f)]); + channel_key_off(&dev->chan[9u * high + (regm & 0x0fu)]); } break; case 0xc0: if ((regm & 0x0f) < 9) - channel_write_c0(&dev->chan[9 * high + (regm & 0x0f)], val); + channel_write_c0(&dev->chan[9u * high + (regm & 0x0fu)], val); break; #if OPL_ENABLE_STEREOEXT case 0xd0: if ((regm & 0x0f) < 9) - channel_write_d0(&dev->chan[9 * high + (regm & 0x0f)], val); + channel_write_d0(&dev->chan[9u * high + (regm & 0x0fu)], val); break; #endif case 0xe0: case 0xf0: - if (ad_slot[regm & 0x1f] >= 0) - slot_write_e0(&dev->slot[18 * high + ad_slot[regm & 0x1f]], val); + if (ad_slot[regm & 0x1fu] >= 0) + slot_write_e0(&dev->slot[18u * high + ad_slot[regm & 0x1fu]], val); break; default: @@ -1576,17 +1576,17 @@ nuked_init(nuked_t *dev, uint32_t samplerate) } for (i = 0; i < 18; i++) { - ch = &dev->chan[i]; - local_ch_slot = ch_slot[i]; - ch->slots[0] = &dev->slot[local_ch_slot]; - ch->slots[1] = &dev->slot[local_ch_slot + 3]; - dev->slot[local_ch_slot].chan = ch; - dev->slot[local_ch_slot + 3].chan = ch; + ch = &dev->chan[i]; + local_ch_slot = ch_slot[i]; + ch->slots[0] = &dev->slot[local_ch_slot]; + ch->slots[1] = &dev->slot[local_ch_slot + 3u]; + dev->slot[local_ch_slot].chan = ch; + dev->slot[local_ch_slot + 3u].chan = ch; if ((i % 9) < 3) - ch->pair = &dev->chan[i + 3]; + ch->pair = &dev->chan[i + 3u]; else if ((i % 9) < 6) - ch->pair = &dev->chan[i - 3]; + ch->pair = &dev->chan[i - 3u]; ch->dev = dev; ch->out[0] = &dev->zeromod; From 5efc896a440b573109dbcce53354ed7d3fd2e52e Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Wed, 10 Jul 2024 07:05:29 -0400 Subject: [PATCH 13/28] snd_opl_nuked.c: Fixed the uint64_t comparison issue UINT64_C macro added because of comparison issues in some cases. Co-Authored-By: Vitaly Novichkov <6751442+wohlstand@users.noreply.github.com> Co-Authored-By: nukeykt --- src/sound/snd_opl_nuked.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index a64dc196b..c8dfe7609 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -1451,7 +1451,7 @@ nuked_generate_4ch(void *priv, int32_t *buf4) } if (dev->eg_timerrem || dev->eg_state) { - if (dev->eg_timer == 0xfffffffff) { + if (dev->eg_timer == UINT64_C(0xfffffffff)) { dev->eg_timer = 0; dev->eg_timerrem = 1; } else { From 5209e949ad66a9fa2eb20d7cd33fef4b01801f18 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Wed, 10 Jul 2024 07:13:25 -0400 Subject: [PATCH 14/28] Fixed conflict with Qt because of global "slots" macro Co-Authored-By: Vitaly Novichkov <6751442+wohlstand@users.noreply.github.com> Co-Authored-By: nukeykt --- src/sound/snd_opl_nuked.c | 176 +++++++++++++++++++------------------- 1 file changed, 88 insertions(+), 88 deletions(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index c8dfe7609..5449ebbd2 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -134,7 +134,7 @@ typedef struct slot { } slot_t; typedef struct chan { - slot_t *slots[2]; + slot_t *slotz[2]; // Don't use "slots" keyword to avoid conflict with Qt applications struct chan *pair; struct chip *dev; int16_t *out[4]; @@ -860,18 +860,18 @@ channel_update_rhythm(nuked_t *dev, uint8_t data) ch6 = &dev->chan[6]; ch7 = &dev->chan[7]; ch8 = &dev->chan[8]; - ch6->out[0] = &ch6->slots[1]->out; - ch6->out[1] = &ch6->slots[1]->out; + ch6->out[0] = &ch6->slotz[1]->out; + ch6->out[1] = &ch6->slotz[1]->out; ch6->out[2] = &dev->zeromod; ch6->out[3] = &dev->zeromod; - ch7->out[0] = &ch7->slots[0]->out; - ch7->out[1] = &ch7->slots[0]->out; - ch7->out[2] = &ch7->slots[1]->out; - ch7->out[3] = &ch7->slots[1]->out; - ch8->out[0] = &ch8->slots[0]->out; - ch8->out[1] = &ch8->slots[0]->out; - ch8->out[2] = &ch8->slots[1]->out; - ch8->out[3] = &ch8->slots[1]->out; + ch7->out[0] = &ch7->slotz[0]->out; + ch7->out[1] = &ch7->slotz[0]->out; + ch7->out[2] = &ch7->slotz[1]->out; + ch7->out[3] = &ch7->slotz[1]->out; + ch8->out[0] = &ch8->slotz[0]->out; + ch8->out[1] = &ch8->slotz[0]->out; + ch8->out[2] = &ch8->slotz[1]->out; + ch8->out[3] = &ch8->slotz[1]->out; for (chnum = 6; chnum < 9; chnum++) dev->chan[chnum].chtype = ch_drum; @@ -882,43 +882,43 @@ channel_update_rhythm(nuked_t *dev, uint8_t data) // hh if (dev->rhy & 0x01) - env_key_on(ch7->slots[0], egk_drum); + env_key_on(ch7->slotz[0], egk_drum); else - env_key_off(ch7->slots[0], egk_drum); + env_key_off(ch7->slotz[0], egk_drum); // tc if (dev->rhy & 0x02) - env_key_on(ch8->slots[1], egk_drum); + env_key_on(ch8->slotz[1], egk_drum); else - env_key_off(ch8->slots[1], egk_drum); + env_key_off(ch8->slotz[1], egk_drum); // tom if (dev->rhy & 0x04) - env_key_on(ch8->slots[0], egk_drum); + env_key_on(ch8->slotz[0], egk_drum); else - env_key_off(ch8->slots[0], egk_drum); + env_key_off(ch8->slotz[0], egk_drum); // sd if (dev->rhy & 0x08) - env_key_on(ch7->slots[1], egk_drum); + env_key_on(ch7->slotz[1], egk_drum); else - env_key_off(ch7->slots[1], egk_drum); + env_key_off(ch7->slotz[1], egk_drum); // bd if (dev->rhy & 0x10) { - env_key_on(ch6->slots[0], egk_drum); - env_key_on(ch6->slots[1], egk_drum); + env_key_on(ch6->slotz[0], egk_drum); + env_key_on(ch6->slotz[1], egk_drum); } else { - env_key_off(ch6->slots[0], egk_drum); - env_key_off(ch6->slots[1], egk_drum); + env_key_off(ch6->slotz[0], egk_drum); + env_key_off(ch6->slotz[1], egk_drum); } } else { for (chnum = 6; chnum < 9; chnum++) { dev->chan[chnum].chtype = ch_2op; channel_setup_alg(&dev->chan[chnum]); - env_key_off(dev->chan[chnum].slots[0], egk_drum); - env_key_off(dev->chan[chnum].slots[1], egk_drum); + env_key_off(dev->chan[chnum].slotz[0], egk_drum); + env_key_off(dev->chan[chnum].slotz[1], egk_drum); } } } @@ -933,15 +933,15 @@ channel_write_a0(chan_t *ch, uint8_t data) ch->ksv = (ch->block << 1) | ((ch->f_num >> (0x09 - ch->dev->nts)) & 0x01); - env_update_ksl(ch->slots[0]); - env_update_ksl(ch->slots[1]); + env_update_ksl(ch->slotz[0]); + env_update_ksl(ch->slotz[1]); if (ch->dev->newm && ch->chtype == ch_4op) { ch->pair->f_num = ch->f_num; ch->pair->ksv = ch->ksv; - env_update_ksl(ch->pair->slots[0]); - env_update_ksl(ch->pair->slots[1]); + env_update_ksl(ch->pair->slotz[0]); + env_update_ksl(ch->pair->slotz[1]); } } @@ -956,16 +956,16 @@ channel_write_b0(chan_t *ch, uint8_t data) ch->ksv = (ch->block << 1) | ((ch->f_num >> (0x09 - ch->dev->nts)) & 0x01); - env_update_ksl(ch->slots[0]); - env_update_ksl(ch->slots[1]); + env_update_ksl(ch->slotz[0]); + env_update_ksl(ch->slotz[1]); if (ch->dev->newm && ch->chtype == ch_4op) { ch->pair->f_num = ch->f_num; ch->pair->block = ch->block; ch->pair->ksv = ch->ksv; - env_update_ksl(ch->pair->slots[0]); - env_update_ksl(ch->pair->slots[1]); + env_update_ksl(ch->pair->slotz[0]); + env_update_ksl(ch->pair->slotz[1]); } } @@ -974,20 +974,20 @@ channel_setup_alg(chan_t *ch) { if (ch->chtype == ch_drum) { if (ch->ch_num == 7 || ch->ch_num == 8) { - ch->slots[0]->mod = &ch->dev->zeromod; - ch->slots[1]->mod = &ch->dev->zeromod; + ch->slotz[0]->mod = &ch->dev->zeromod; + ch->slotz[1]->mod = &ch->dev->zeromod; return; } switch (ch->alg & 0x01) { case 0x00: - ch->slots[0]->mod = &ch->slots[0]->fbmod; - ch->slots[1]->mod = &ch->slots[0]->out; + ch->slotz[0]->mod = &ch->slotz[0]->fbmod; + ch->slotz[1]->mod = &ch->slotz[0]->out; break; case 0x01: - ch->slots[0]->mod = &ch->slots[0]->fbmod; - ch->slots[1]->mod = &ch->dev->zeromod; + ch->slotz[0]->mod = &ch->slotz[0]->fbmod; + ch->slotz[1]->mod = &ch->dev->zeromod; break; default: @@ -1007,46 +1007,46 @@ channel_setup_alg(chan_t *ch) switch (ch->alg & 0x03) { case 0x00: - ch->pair->slots[0]->mod = &ch->pair->slots[0]->fbmod; - ch->pair->slots[1]->mod = &ch->pair->slots[0]->out; - ch->slots[0]->mod = &ch->pair->slots[1]->out; - ch->slots[1]->mod = &ch->slots[0]->out; - ch->out[0] = &ch->slots[1]->out; + ch->pair->slotz[0]->mod = &ch->pair->slotz[0]->fbmod; + ch->pair->slotz[1]->mod = &ch->pair->slotz[0]->out; + ch->slotz[0]->mod = &ch->pair->slotz[1]->out; + ch->slotz[1]->mod = &ch->slotz[0]->out; + ch->out[0] = &ch->slotz[1]->out; ch->out[1] = &ch->dev->zeromod; ch->out[2] = &ch->dev->zeromod; ch->out[3] = &ch->dev->zeromod; break; case 0x01: - ch->pair->slots[0]->mod = &ch->pair->slots[0]->fbmod; - ch->pair->slots[1]->mod = &ch->pair->slots[0]->out; - ch->slots[0]->mod = &ch->dev->zeromod; - ch->slots[1]->mod = &ch->slots[0]->out; - ch->out[0] = &ch->pair->slots[1]->out; - ch->out[1] = &ch->slots[1]->out; + ch->pair->slotz[0]->mod = &ch->pair->slotz[0]->fbmod; + ch->pair->slotz[1]->mod = &ch->pair->slotz[0]->out; + ch->slotz[0]->mod = &ch->dev->zeromod; + ch->slotz[1]->mod = &ch->slotz[0]->out; + ch->out[0] = &ch->pair->slotz[1]->out; + ch->out[1] = &ch->slotz[1]->out; ch->out[2] = &ch->dev->zeromod; ch->out[3] = &ch->dev->zeromod; break; case 0x02: - ch->pair->slots[0]->mod = &ch->pair->slots[0]->fbmod; - ch->pair->slots[1]->mod = &ch->dev->zeromod; - ch->slots[0]->mod = &ch->pair->slots[1]->out; - ch->slots[1]->mod = &ch->slots[0]->out; - ch->out[0] = &ch->pair->slots[0]->out; - ch->out[1] = &ch->slots[1]->out; + ch->pair->slotz[0]->mod = &ch->pair->slotz[0]->fbmod; + ch->pair->slotz[1]->mod = &ch->dev->zeromod; + ch->slotz[0]->mod = &ch->pair->slotz[1]->out; + ch->slotz[1]->mod = &ch->slotz[0]->out; + ch->out[0] = &ch->pair->slotz[0]->out; + ch->out[1] = &ch->slotz[1]->out; ch->out[2] = &ch->dev->zeromod; ch->out[3] = &ch->dev->zeromod; break; case 0x03: - ch->pair->slots[0]->mod = &ch->pair->slots[0]->fbmod; - ch->pair->slots[1]->mod = &ch->dev->zeromod; - ch->slots[0]->mod = &ch->pair->slots[1]->out; - ch->slots[1]->mod = &ch->dev->zeromod; - ch->out[0] = &ch->pair->slots[0]->out; - ch->out[1] = &ch->slots[0]->out; - ch->out[2] = &ch->slots[1]->out; + ch->pair->slotz[0]->mod = &ch->pair->slotz[0]->fbmod; + ch->pair->slotz[1]->mod = &ch->dev->zeromod; + ch->slotz[0]->mod = &ch->pair->slotz[1]->out; + ch->slotz[1]->mod = &ch->dev->zeromod; + ch->out[0] = &ch->pair->slotz[0]->out; + ch->out[1] = &ch->slotz[0]->out; + ch->out[2] = &ch->slotz[1]->out; ch->out[3] = &ch->dev->zeromod; break; @@ -1056,19 +1056,19 @@ channel_setup_alg(chan_t *ch) } else switch (ch->alg & 0x01) { case 0x00: - ch->slots[0]->mod = &ch->slots[0]->fbmod; - ch->slots[1]->mod = &ch->slots[0]->out; - ch->out[0] = &ch->slots[1]->out; + ch->slotz[0]->mod = &ch->slotz[0]->fbmod; + ch->slotz[1]->mod = &ch->slotz[0]->out; + ch->out[0] = &ch->slotz[1]->out; ch->out[1] = &ch->dev->zeromod; ch->out[2] = &ch->dev->zeromod; ch->out[3] = &ch->dev->zeromod; break; case 0x01: - ch->slots[0]->mod = &ch->slots[0]->fbmod; - ch->slots[1]->mod = &ch->dev->zeromod; - ch->out[0] = &ch->slots[0]->out; - ch->out[1] = &ch->slots[1]->out; + ch->slotz[0]->mod = &ch->slotz[0]->fbmod; + ch->slotz[1]->mod = &ch->dev->zeromod; + ch->out[0] = &ch->slotz[0]->out; + ch->out[1] = &ch->slotz[1]->out; ch->out[2] = &ch->dev->zeromod; ch->out[3] = &ch->dev->zeromod; break; @@ -1139,17 +1139,17 @@ channel_key_on(chan_t *ch) { if (ch->dev->newm) { if (ch->chtype == ch_4op) { - env_key_on(ch->slots[0], egk_norm); - env_key_on(ch->slots[1], egk_norm); - env_key_on(ch->pair->slots[0], egk_norm); - env_key_on(ch->pair->slots[1], egk_norm); + env_key_on(ch->slotz[0], egk_norm); + env_key_on(ch->slotz[1], egk_norm); + env_key_on(ch->pair->slotz[0], egk_norm); + env_key_on(ch->pair->slotz[1], egk_norm); } else if (ch->chtype == ch_2op || ch->chtype == ch_drum) { - env_key_on(ch->slots[0], egk_norm); - env_key_on(ch->slots[1], egk_norm); + env_key_on(ch->slotz[0], egk_norm); + env_key_on(ch->slotz[1], egk_norm); } } else { - env_key_on(ch->slots[0], egk_norm); - env_key_on(ch->slots[1], egk_norm); + env_key_on(ch->slotz[0], egk_norm); + env_key_on(ch->slotz[1], egk_norm); } } @@ -1158,17 +1158,17 @@ channel_key_off(chan_t *ch) { if (ch->dev->newm) { if (ch->chtype == ch_4op) { - env_key_off(ch->slots[0], egk_norm); - env_key_off(ch->slots[1], egk_norm); - env_key_off(ch->pair->slots[0], egk_norm); - env_key_off(ch->pair->slots[1], egk_norm); + env_key_off(ch->slotz[0], egk_norm); + env_key_off(ch->slotz[1], egk_norm); + env_key_off(ch->pair->slotz[0], egk_norm); + env_key_off(ch->pair->slotz[1], egk_norm); } else if (ch->chtype == ch_2op || ch->chtype == ch_drum) { - env_key_off(ch->slots[0], egk_norm); - env_key_off(ch->slots[1], egk_norm); + env_key_off(ch->slotz[0], egk_norm); + env_key_off(ch->slotz[1], egk_norm); } } else { - env_key_off(ch->slots[0], egk_norm); - env_key_off(ch->slots[1], egk_norm); + env_key_off(ch->slotz[0], egk_norm); + env_key_off(ch->slotz[1], egk_norm); } } @@ -1578,8 +1578,8 @@ nuked_init(nuked_t *dev, uint32_t samplerate) for (i = 0; i < 18; i++) { ch = &dev->chan[i]; local_ch_slot = ch_slot[i]; - ch->slots[0] = &dev->slot[local_ch_slot]; - ch->slots[1] = &dev->slot[local_ch_slot + 3u]; + ch->slotz[0] = &dev->slot[local_ch_slot]; + ch->slotz[1] = &dev->slot[local_ch_slot + 3u]; dev->slot[local_ch_slot].chan = ch; dev->slot[local_ch_slot + 3u].chan = ch; From 56f50f4ebe1ab479e15361a25e9c14724bee07bc Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Fri, 12 Jul 2024 00:46:30 -0400 Subject: [PATCH 15/28] Fix the envelope generator Co-Authored-By: nukeykt --- src/sound/snd_opl_nuked.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index 5449ebbd2..2bc6c2a67 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -170,6 +170,7 @@ typedef struct chip { uint8_t eg_timerrem; uint8_t eg_state; uint8_t eg_add; + uint64_t eg_timer_lo; uint8_t newm; uint8_t nts; uint8_t rhy; @@ -622,7 +623,7 @@ env_calc(slot_t *slot) break; } } else { - shift = (rate_hi & 0x03) + eg_incstep[rate_lo][slot->dev->timer & 0x03u]; + shift = (rate_hi & 0x03) + eg_incstep[rate_lo][slot->dev->eg_timer_lo]; if (shift & 0x04) shift = 0x03; if (!shift) @@ -1438,16 +1439,16 @@ nuked_generate_4ch(void *priv, int32_t *buf4) dev->vibpos = (dev->vibpos + 1) & 7; dev->timer++; - dev->eg_add = 0; - if (dev->eg_timer) { - while (shift < 36 && ((dev->eg_timer >> shift) & 1) == 0) + if (dev->eg_state) { + while (shift < 13 && ((dev->eg_timer >> shift) & 1) == 0) shift++; if (shift > 12) dev->eg_add = 0; else dev->eg_add = shift + 1; + dev->eg_timer_lo = (uint8_t) (dev->eg_timer & 0x3u); } if (dev->eg_timerrem || dev->eg_state) { From 2f097d2fe65a08d0958c27fcd6267148fde690ee Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Fri, 12 Jul 2024 19:34:24 -0400 Subject: [PATCH 16/28] reorganise snd_opl_nuked.c to closer match upstream --- src/sound/snd_opl_nuked.c | 376 +++++++++++++++++++------------------- 1 file changed, 190 insertions(+), 186 deletions(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index 2bc6c2a67..7527c5206 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -1197,6 +1197,195 @@ channel_set_4op(nuked_t *dev, uint8_t data) } } +static void +process_slot(slot_t *slot) +{ + slot_calc_fb(slot); + env_calc(slot); + phase_generate(slot); + slot_generate(slot); +} + +inline void +nuked_generate_4ch(void *priv, int32_t *buf4) +{ + nuked_t *dev = (nuked_t *) priv; + chan_t *ch; + wrbuf_t *writebuf; + int16_t **out; + int32_t mix[2]; + int16_t accm; + int16_t shift = 0; + uint8_t i; + + buf4[1] = dev->mixbuff[1]; + buf4[3] = dev->mixbuff[3]; + +#if OPL_QUIRK_CHANNELSAMPLEDELAY + for (i = 0; i < 15; i++) +#else + for (i = 0; i < 36; i++) +#endif + process_slot(&dev->slot[i]); + + mix[0] = mix[1] = 0; + + for (i = 0; i < 18; i++) { + ch = &dev->chan[i]; + out = ch->out; + accm = *out[0] + *out[1] + *out[2] + *out[3]; +#if OPL_ENABLE_STEREOEXT + mix[0] += (int32_t) ((accm * ch->leftpan) >> 16); +#else + mix[0] += (int32_t) (accm & ch->cha); +#endif + mix[1] += (int32_t) (accm & ch->chc); + } + + dev->mixbuff[0] = mix[0]; + dev->mixbuff[2] = mix[1]; + +#if OPL_QUIRK_CHANNELSAMPLEDELAY + for (i = 15; i < 18; i++) + process_slot(&dev->slot[i]); +#endif + + buf4[0] = dev->mixbuff[0]; + buf4[2] = dev->mixbuff[2]; + +#if OPL_QUIRK_CHANNELSAMPLEDELAY + for (i = 18; i < 33; i++) + process_slot(&dev->slot[i]); +#endif + + mix[0] = mix[1] = 0; + + for (i = 0; i < 18; i++) { + ch = &dev->chan[i]; + out = ch->out; + accm = *out[0] + *out[1] + *out[2] + *out[3]; +#if OPL_ENABLE_STEREOEXT + mix[0] += (int32_t) ((accm * ch->rightpan) >> 16); +#else + mix[0] += (int32_t) (accm & ch->chb); +#endif + mix[1] += (int32_t) (accm & ch->chd); + } + + dev->mixbuff[1] = mix[0]; + dev->mixbuff[3] = mix[1]; + +#if OPL_QUIRK_CHANNELSAMPLEDELAY + for (i = 33; i < 36; i++) + process_slot(&dev->slot[i]); +#endif + + if ((dev->timer & 0x3f) == 0x3f) + dev->tremolopos = (dev->tremolopos + 1) % 210; + + if (dev->tremolopos < 105) + dev->tremolo = dev->tremolopos >> dev->tremoloshift; + else + dev->tremolo = (210 - dev->tremolopos) >> dev->tremoloshift; + + if ((dev->timer & 0x03ff) == 0x03ff) + dev->vibpos = (dev->vibpos + 1) & 7; + + dev->timer++; + + if (dev->eg_state) { + while (shift < 13 && ((dev->eg_timer >> shift) & 1) == 0) + shift++; + + if (shift > 12) + dev->eg_add = 0; + else + dev->eg_add = shift + 1; + + dev->eg_timer_lo = (uint8_t) (dev->eg_timer & 0x3u); + } + + if (dev->eg_timerrem || dev->eg_state) { + if (dev->eg_timer == UINT64_C(0xfffffffff)) { + dev->eg_timer = 0; + dev->eg_timerrem = 1; + } else { + dev->eg_timer++; + dev->eg_timerrem = 0; + } + } + + dev->eg_state ^= 1; + + while (writebuf = &dev->wrbuf[dev->wrbuf_cur], writebuf->time <= dev->wrbuf_samplecnt) { + if (!(writebuf->reg & 0x200)) + break; + + writebuf->reg &= 0x01ff; + + nuked_write_reg(dev, writebuf->reg, writebuf->data); + + dev->wrbuf_cur = (dev->wrbuf_cur + 1) % WRBUF_SIZE; + } + + dev->wrbuf_samplecnt++; +} + +void +nuked_generate(nuked_t *dev, int32_t *buf) +{ + int32_t samples[4]; + nuked_generate_4ch(dev, samples); + buf[0] = samples[0]; + buf[1] = samples[1]; +} + +void +nuked_generate_4ch_resampled(nuked_t *dev, int32_t *buf4) +{ + while (dev->samplecnt >= dev->rateratio) { + dev->oldsamples[0] = dev->samples[0]; + dev->oldsamples[1] = dev->samples[1]; + dev->oldsamples[2] = dev->samples[2]; + dev->oldsamples[3] = dev->samples[3]; + nuked_generate_4ch(dev, dev->samples); + dev->samplecnt -= dev->rateratio; + } + + buf4[0] = (int32_t) ((dev->oldsamples[0] * (dev->rateratio - dev->samplecnt) + + dev->samples[0] * dev->samplecnt) + / dev->rateratio); + buf4[1] = (int32_t) ((dev->oldsamples[1] * (dev->rateratio - dev->samplecnt) + + dev->samples[1] * dev->samplecnt) + / dev->rateratio); + buf4[2] = (int32_t) ((dev->oldsamples[2] * (dev->rateratio - dev->samplecnt) + + dev->samples[2] * dev->samplecnt) + / dev->rateratio); + buf4[3] = (int32_t) ((dev->oldsamples[3] * (dev->rateratio - dev->samplecnt) + + dev->samples[3] * dev->samplecnt) + / dev->rateratio); + + dev->samplecnt += 1 << RSM_FRAC; +} + +void +nuked_generate_resampled(nuked_t *dev, int16_t *buf4) +{ + int16_t samples[4]; + nuked_generate_4ch_resampled(dev, samples); + buf4[0] = samples[0]; + buf4[1] = samples[1]; +} + +void +nuked_generate_raw(nuked_t *dev, int32_t *bufp) +{ + nuked_generate(dev, dev->samples); + + bufp[0] = (int32_t) dev->samples[0]; + bufp[1] = (int32_t) dev->samples[1]; +} + uint16_t nuked_write_addr(void *priv, uint16_t port, uint8_t val) { @@ -1345,191 +1534,6 @@ nuked_write_reg_buffered(void *priv, uint16_t reg, uint8_t val) dev->wrbuf_last = (writebuf_last + 1) % WRBUF_SIZE; } -static void process_slot(slot_t *slot) -{ - slot_calc_fb(slot); - env_calc(slot); - phase_generate(slot); - slot_generate(slot); -} - -inline void -nuked_generate_4ch(void *priv, int32_t *buf4) -{ - nuked_t *dev = (nuked_t *) priv; - chan_t *ch; - wrbuf_t *writebuf; - int16_t **out; - int32_t mix[2]; - int16_t accm; - int16_t shift = 0; - uint8_t i; - - buf4[1] = dev->mixbuff[1]; - buf4[3] = dev->mixbuff[3]; - -#if OPL_QUIRK_CHANNELSAMPLEDELAY - for (i = 0; i < 15; i++) -#else - for (i = 0; i < 36; i++) -#endif - process_slot(&dev->slot[i]); - - mix[0] = mix[1] = 0; - - for (i = 0; i < 18; i++) { - ch = &dev->chan[i]; - out = ch->out; - accm = *out[0] + *out[1] + *out[2] + *out[3]; -#if OPL_ENABLE_STEREOEXT - mix[0] += (int16_t)((accm * ch->leftpan) >> 16); -#else - mix[0] += (int16_t) (accm & ch->cha); -#endif - mix[1] += (int16_t) (accm & ch->chc); - } - - dev->mixbuff[0] = mix[0]; - dev->mixbuff[2] = mix[1]; - -#if OPL_QUIRK_CHANNELSAMPLEDELAY - for (i = 15; i < 18; i++) - process_slot(&dev->slot[i]); -#endif - - buf4[0] = dev->mixbuff[0]; - buf4[2] = dev->mixbuff[2]; - -#if OPL_QUIRK_CHANNELSAMPLEDELAY - for (i = 18; i < 33; i++) - process_slot(&dev->slot[i]); -#endif - - mix[0] = mix[1] = 0; - - for (i = 0; i < 18; i++) { - ch = &dev->chan[i]; - out = ch->out; - accm = *out[0] + *out[1] + *out[2] + *out[3]; -#if OPL_ENABLE_STEREOEXT - mix[0] += (int16_t)((accm * ch->leftpan) >> 16); -#else - mix[0] += (int16_t) (accm & ch->chb); -#endif - mix[1] += (int16_t) (accm & ch->chd); - } - - dev->mixbuff[1] = mix[0]; - dev->mixbuff[3] = mix[1]; - -#if OPL_QUIRK_CHANNELSAMPLEDELAY - for (i = 33; i < 36; i++) - process_slot(&dev->slot[i]); -#endif - - if ((dev->timer & 0x3f) == 0x3f) - dev->tremolopos = (dev->tremolopos + 1) % 210; - - if (dev->tremolopos < 105) - dev->tremolo = dev->tremolopos >> dev->tremoloshift; - else - dev->tremolo = (210 - dev->tremolopos) >> dev->tremoloshift; - - if ((dev->timer & 0x03ff) == 0x03ff) - dev->vibpos = (dev->vibpos + 1) & 7; - - dev->timer++; - - if (dev->eg_state) { - while (shift < 13 && ((dev->eg_timer >> shift) & 1) == 0) - shift++; - - if (shift > 12) - dev->eg_add = 0; - else - dev->eg_add = shift + 1; - dev->eg_timer_lo = (uint8_t) (dev->eg_timer & 0x3u); - } - - if (dev->eg_timerrem || dev->eg_state) { - if (dev->eg_timer == UINT64_C(0xfffffffff)) { - dev->eg_timer = 0; - dev->eg_timerrem = 1; - } else { - dev->eg_timer++; - dev->eg_timerrem = 0; - } - } - - dev->eg_state ^= 1; - - while (writebuf = &dev->wrbuf[dev->wrbuf_cur], writebuf->time <= dev->wrbuf_samplecnt) { - if (!(writebuf->reg & 0x200)) - break; - - writebuf->reg &= 0x01ff; - - nuked_write_reg(dev, writebuf->reg, writebuf->data); - - dev->wrbuf_cur = (dev->wrbuf_cur + 1) % WRBUF_SIZE; - } - - dev->wrbuf_samplecnt++; -} - -void nuked_generate(nuked_t *dev, int32_t *buf4) -{ - int32_t samples[4]; - nuked_generate_4ch(dev, samples); - buf4[0] = samples[0]; - buf4[1] = samples[1]; -} - -void -nuked_generate_4ch_resampled(nuked_t *dev, int16_t *buf4) -{ - while (dev->samplecnt >= dev->rateratio) { - dev->oldsamples[0] = dev->samples[0]; - dev->oldsamples[1] = dev->samples[1]; - dev->oldsamples[2] = dev->samples[2]; - dev->oldsamples[3] = dev->samples[3]; - nuked_generate_4ch(dev, dev->samples); - dev->samplecnt -= dev->rateratio; - } - - buf4[0] = (int32_t) ((dev->oldsamples[0] * (dev->rateratio - dev->samplecnt) - + dev->samples[0] * dev->samplecnt) - / dev->rateratio); - buf4[1] = (int32_t) ((dev->oldsamples[1] * (dev->rateratio - dev->samplecnt) - + dev->samples[1] * dev->samplecnt) - / dev->rateratio); - buf4[2] = (int32_t) ((dev->oldsamples[2] * (dev->rateratio - dev->samplecnt) - + dev->samples[2] * dev->samplecnt) - / dev->rateratio); - buf4[3] = (int32_t) ((dev->oldsamples[3] * (dev->rateratio - dev->samplecnt) - + dev->samples[3] * dev->samplecnt) - / dev->rateratio); - - dev->samplecnt += 1 << RSM_FRAC; -} - -void nuked_generate_resampled(nuked_t *dev, int16_t *buf4) -{ - int16_t samples[4]; - nuked_generate_4ch_resampled(dev, samples); - buf4[0] = samples[0]; - buf4[1] = samples[1]; -} - -void -nuked_generate_raw(nuked_t *dev, int32_t *bufp) -{ - nuked_generate(dev, dev->samples); - - bufp[0] = (int32_t) dev->samples[0]; - bufp[1] = (int32_t) dev->samples[1]; -} - void nuked_generate_4ch_stream(nuked_t *dev, int16_t *sndptr1, int16_t *sndptr2, uint32_t numsamples) { @@ -1550,7 +1554,7 @@ void nuked_generate_stream(nuked_t *dev, int32_t *sndptr, uint32_t num) { for (uint_fast32_t i = 0; i < num; i++) { - nuked_generate_raw(dev, sndptr); + nuked_generate_resampled(dev, sndptr); sndptr += 2; } } From f68e7d3eaebe2ead5227bd71a808740587a5edb9 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Fri, 12 Jul 2024 20:22:01 -0400 Subject: [PATCH 17/28] Nuked: env -> envelope --- src/sound/snd_opl_nuked.c | 148 +++++++++++++++++++------------------- 1 file changed, 74 insertions(+), 74 deletions(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index 7527c5206..9db7f086e 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -18,7 +18,7 @@ * siliconpr0n.org(John McMaster, digshadow): * YMF262 and VRC VII decaps and die shots. * - * Version: 1.8.0 + * Version: 1.8 * * Translation from C++ into C done by Miran Grca. * @@ -378,11 +378,11 @@ static uint8_t panpot_lut_build = 0; #endif // Envelope generator -typedef int16_t (*env_sinfunc)(uint16_t phase, uint16_t envelope); -typedef void (*env_genfunc)(slot_t *slot); +typedef int16_t (*envelope_sinfunc)(uint16_t phase, uint16_t envelope); +typedef void (*envelope_genfunc)(slot_t *slot); static int16_t -env_calc_exp(uint32_t level) +envelope_calc_exp(uint32_t level) { if (level > 0x1fff) level = 0x1fff; @@ -391,7 +391,7 @@ env_calc_exp(uint32_t level) } static int16_t -env_calc_sin0(uint16_t phase, uint16_t env) +envelope_calc_sin0(uint16_t phase, uint16_t envelope) { uint16_t out = 0; uint16_t neg = 0; @@ -406,11 +406,11 @@ env_calc_sin0(uint16_t phase, uint16_t env) else out = logsinrom[phase & 0xffu]; - return (env_calc_exp(out + (env << 3)) ^ neg); + return (envelope_calc_exp(out + (envelope << 3)) ^ neg); } static int16_t -env_calc_sin1(uint16_t phase, uint16_t env) +envelope_calc_sin1(uint16_t phase, uint16_t envelope) { uint16_t out = 0; @@ -423,11 +423,11 @@ env_calc_sin1(uint16_t phase, uint16_t env) else out = logsinrom[phase & 0xffu]; - return (env_calc_exp(out + (env << 3))); + return (envelope_calc_exp(out + (envelope << 3))); } static int16_t -env_calc_sin2(uint16_t phase, uint16_t env) +envelope_calc_sin2(uint16_t phase, uint16_t envelope) { uint16_t out = 0; @@ -438,11 +438,11 @@ env_calc_sin2(uint16_t phase, uint16_t env) else out = logsinrom[phase & 0xffu]; - return (env_calc_exp(out + (env << 3))); + return (envelope_calc_exp(out + (envelope << 3))); } static int16_t -env_calc_sin3(uint16_t phase, uint16_t env) +envelope_calc_sin3(uint16_t phase, uint16_t envelope) { uint16_t out = 0; @@ -453,11 +453,11 @@ env_calc_sin3(uint16_t phase, uint16_t env) else out = logsinrom[phase & 0xffu]; - return (env_calc_exp(out + (env << 3))); + return (envelope_calc_exp(out + (envelope << 3))); } static int16_t -env_calc_sin4(uint16_t phase, uint16_t env) +envelope_calc_sin4(uint16_t phase, uint16_t envelope) { uint16_t out = 0; uint16_t neg = 0; @@ -474,11 +474,11 @@ env_calc_sin4(uint16_t phase, uint16_t env) else out = logsinrom[(phase << 1u) & 0xffu]; - return (env_calc_exp(out + (env << 3)) ^ neg); + return (envelope_calc_exp(out + (envelope << 3)) ^ neg); } static int16_t -env_calc_sin5(uint16_t phase, uint16_t env) +envelope_calc_sin5(uint16_t phase, uint16_t envelope) { uint16_t out = 0; @@ -491,11 +491,11 @@ env_calc_sin5(uint16_t phase, uint16_t env) else out = logsinrom[(phase << 1u) & 0xffu]; - return (env_calc_exp(out + (env << 3))); + return (envelope_calc_exp(out + (envelope << 3))); } static int16_t -env_calc_sin6(uint16_t phase, uint16_t env) +envelope_calc_sin6(uint16_t phase, uint16_t envelope) { uint16_t neg = 0; @@ -504,11 +504,11 @@ env_calc_sin6(uint16_t phase, uint16_t env) if (phase & 0x0200) neg = 0xffff; - return (env_calc_exp(env << 3) ^ neg); + return (envelope_calc_exp(envelope << 3) ^ neg); } static int16_t -env_calc_sin7(uint16_t phase, uint16_t env) +envelope_calc_sin7(uint16_t phase, uint16_t envelope) { uint16_t out = 0; uint16_t neg = 0; @@ -522,22 +522,22 @@ env_calc_sin7(uint16_t phase, uint16_t env) out = phase << 3; - return (env_calc_exp(out + (env << 3)) ^ neg); + return (envelope_calc_exp(out + (envelope << 3)) ^ neg); } -static const env_sinfunc env_sin[8] = { - env_calc_sin0, - env_calc_sin1, - env_calc_sin2, - env_calc_sin3, - env_calc_sin4, - env_calc_sin5, - env_calc_sin6, - env_calc_sin7 +static const envelope_sinfunc envelope_sin[8] = { + envelope_calc_sin0, + envelope_calc_sin1, + envelope_calc_sin2, + envelope_calc_sin3, + envelope_calc_sin4, + envelope_calc_sin5, + envelope_calc_sin6, + envelope_calc_sin7 }; static void -env_update_ksl(slot_t *slot) +envelope_update_ksl(slot_t *slot) { int16_t ksl = (kslrom[slot->chan->f_num >> 6u] << 2) - ((0x08 - slot->chan->block) << 5); @@ -549,7 +549,7 @@ env_update_ksl(slot_t *slot) } static void -env_calc(slot_t *slot) +envelope_calc(slot_t *slot) { uint8_t nonzero; uint8_t rate; @@ -681,13 +681,13 @@ env_calc(slot_t *slot) } static void -env_key_on(slot_t *slot, uint8_t type) +envelope_key_on(slot_t *slot, uint8_t type) { slot->key |= type; } static void -env_key_off(slot_t *slot, uint8_t type) +envelope_key_off(slot_t *slot, uint8_t type) { slot->key &= ~type; } @@ -795,7 +795,7 @@ slot_write_40(slot_t *slot, uint8_t data) slot->reg_ksl = (data >> 6) & 0x03; slot->reg_tl = data & 0x3f; - env_update_ksl(slot); + envelope_update_ksl(slot); } static void @@ -828,7 +828,7 @@ slot_write_e0(slot_t *slot, uint8_t data) static void slot_generate(slot_t *slot) { - slot->out = env_sin[slot->reg_wf](slot->pg_phase_out + *slot->mod, + slot->out = envelope_sin[slot->reg_wf](slot->pg_phase_out + *slot->mod, slot->eg_out); } @@ -883,43 +883,43 @@ channel_update_rhythm(nuked_t *dev, uint8_t data) // hh if (dev->rhy & 0x01) - env_key_on(ch7->slotz[0], egk_drum); + envelope_key_on(ch7->slotz[0], egk_drum); else - env_key_off(ch7->slotz[0], egk_drum); + envelope_key_off(ch7->slotz[0], egk_drum); // tc if (dev->rhy & 0x02) - env_key_on(ch8->slotz[1], egk_drum); + envelope_key_on(ch8->slotz[1], egk_drum); else - env_key_off(ch8->slotz[1], egk_drum); + envelope_key_off(ch8->slotz[1], egk_drum); // tom if (dev->rhy & 0x04) - env_key_on(ch8->slotz[0], egk_drum); + envelope_key_on(ch8->slotz[0], egk_drum); else - env_key_off(ch8->slotz[0], egk_drum); + envelope_key_off(ch8->slotz[0], egk_drum); // sd if (dev->rhy & 0x08) - env_key_on(ch7->slotz[1], egk_drum); + envelope_key_on(ch7->slotz[1], egk_drum); else - env_key_off(ch7->slotz[1], egk_drum); + envelope_key_off(ch7->slotz[1], egk_drum); // bd if (dev->rhy & 0x10) { - env_key_on(ch6->slotz[0], egk_drum); - env_key_on(ch6->slotz[1], egk_drum); + envelope_key_on(ch6->slotz[0], egk_drum); + envelope_key_on(ch6->slotz[1], egk_drum); } else { - env_key_off(ch6->slotz[0], egk_drum); - env_key_off(ch6->slotz[1], egk_drum); + envelope_key_off(ch6->slotz[0], egk_drum); + envelope_key_off(ch6->slotz[1], egk_drum); } } else { for (chnum = 6; chnum < 9; chnum++) { dev->chan[chnum].chtype = ch_2op; channel_setup_alg(&dev->chan[chnum]); - env_key_off(dev->chan[chnum].slotz[0], egk_drum); - env_key_off(dev->chan[chnum].slotz[1], egk_drum); + envelope_key_off(dev->chan[chnum].slotz[0], egk_drum); + envelope_key_off(dev->chan[chnum].slotz[1], egk_drum); } } } @@ -934,15 +934,15 @@ channel_write_a0(chan_t *ch, uint8_t data) ch->ksv = (ch->block << 1) | ((ch->f_num >> (0x09 - ch->dev->nts)) & 0x01); - env_update_ksl(ch->slotz[0]); - env_update_ksl(ch->slotz[1]); + envelope_update_ksl(ch->slotz[0]); + envelope_update_ksl(ch->slotz[1]); if (ch->dev->newm && ch->chtype == ch_4op) { ch->pair->f_num = ch->f_num; ch->pair->ksv = ch->ksv; - env_update_ksl(ch->pair->slotz[0]); - env_update_ksl(ch->pair->slotz[1]); + envelope_update_ksl(ch->pair->slotz[0]); + envelope_update_ksl(ch->pair->slotz[1]); } } @@ -957,16 +957,16 @@ channel_write_b0(chan_t *ch, uint8_t data) ch->ksv = (ch->block << 1) | ((ch->f_num >> (0x09 - ch->dev->nts)) & 0x01); - env_update_ksl(ch->slotz[0]); - env_update_ksl(ch->slotz[1]); + envelope_update_ksl(ch->slotz[0]); + envelope_update_ksl(ch->slotz[1]); if (ch->dev->newm && ch->chtype == ch_4op) { ch->pair->f_num = ch->f_num; ch->pair->block = ch->block; ch->pair->ksv = ch->ksv; - env_update_ksl(ch->pair->slotz[0]); - env_update_ksl(ch->pair->slotz[1]); + envelope_update_ksl(ch->pair->slotz[0]); + envelope_update_ksl(ch->pair->slotz[1]); } } @@ -1140,17 +1140,17 @@ channel_key_on(chan_t *ch) { if (ch->dev->newm) { if (ch->chtype == ch_4op) { - env_key_on(ch->slotz[0], egk_norm); - env_key_on(ch->slotz[1], egk_norm); - env_key_on(ch->pair->slotz[0], egk_norm); - env_key_on(ch->pair->slotz[1], egk_norm); + envelope_key_on(ch->slotz[0], egk_norm); + envelope_key_on(ch->slotz[1], egk_norm); + envelope_key_on(ch->pair->slotz[0], egk_norm); + envelope_key_on(ch->pair->slotz[1], egk_norm); } else if (ch->chtype == ch_2op || ch->chtype == ch_drum) { - env_key_on(ch->slotz[0], egk_norm); - env_key_on(ch->slotz[1], egk_norm); + envelope_key_on(ch->slotz[0], egk_norm); + envelope_key_on(ch->slotz[1], egk_norm); } } else { - env_key_on(ch->slotz[0], egk_norm); - env_key_on(ch->slotz[1], egk_norm); + envelope_key_on(ch->slotz[0], egk_norm); + envelope_key_on(ch->slotz[1], egk_norm); } } @@ -1159,17 +1159,17 @@ channel_key_off(chan_t *ch) { if (ch->dev->newm) { if (ch->chtype == ch_4op) { - env_key_off(ch->slotz[0], egk_norm); - env_key_off(ch->slotz[1], egk_norm); - env_key_off(ch->pair->slotz[0], egk_norm); - env_key_off(ch->pair->slotz[1], egk_norm); + envelope_key_off(ch->slotz[0], egk_norm); + envelope_key_off(ch->slotz[1], egk_norm); + envelope_key_off(ch->pair->slotz[0], egk_norm); + envelope_key_off(ch->pair->slotz[1], egk_norm); } else if (ch->chtype == ch_2op || ch->chtype == ch_drum) { - env_key_off(ch->slotz[0], egk_norm); - env_key_off(ch->slotz[1], egk_norm); + envelope_key_off(ch->slotz[0], egk_norm); + envelope_key_off(ch->slotz[1], egk_norm); } } else { - env_key_off(ch->slotz[0], egk_norm); - env_key_off(ch->slotz[1], egk_norm); + envelope_key_off(ch->slotz[0], egk_norm); + envelope_key_off(ch->slotz[1], egk_norm); } } @@ -1201,7 +1201,7 @@ static void process_slot(slot_t *slot) { slot_calc_fb(slot); - env_calc(slot); + envelope_calc(slot); phase_generate(slot); slot_generate(slot); } From 29d72a82dc55529707ef9ae061cd760c487ea913 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Fri, 12 Jul 2024 20:39:01 -0400 Subject: [PATCH 18/28] Nuked: slot_t -> opl_slot --- src/sound/snd_opl_nuked.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index 9db7f086e..a634a2a82 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -131,10 +131,10 @@ typedef struct slot { uint32_t pg_phase; uint16_t pg_phase_out; uint8_t slot_num; -} slot_t; +} opl3_slot; typedef struct chan { - slot_t *slotz[2]; // Don't use "slots" keyword to avoid conflict with Qt applications + opl3_slot *slotz[2]; // Don't use "slots" keyword to avoid conflict with Qt applications struct chan *pair; struct chip *dev; int16_t *out[4]; @@ -164,7 +164,7 @@ typedef struct wrbuf { typedef struct chip { chan_t chan[18]; - slot_t slot[36]; + opl3_slot slot[36]; uint16_t timer; uint64_t eg_timer; uint8_t eg_timerrem; @@ -379,7 +379,7 @@ static uint8_t panpot_lut_build = 0; // Envelope generator typedef int16_t (*envelope_sinfunc)(uint16_t phase, uint16_t envelope); -typedef void (*envelope_genfunc)(slot_t *slot); +typedef void (*envelope_genfunc)(opl3_slot *slot); static int16_t envelope_calc_exp(uint32_t level) @@ -537,7 +537,7 @@ static const envelope_sinfunc envelope_sin[8] = { }; static void -envelope_update_ksl(slot_t *slot) +envelope_update_ksl(opl3_slot *slot) { int16_t ksl = (kslrom[slot->chan->f_num >> 6u] << 2) - ((0x08 - slot->chan->block) << 5); @@ -549,7 +549,7 @@ envelope_update_ksl(slot_t *slot) } static void -envelope_calc(slot_t *slot) +envelope_calc(opl3_slot *slot) { uint8_t nonzero; uint8_t rate; @@ -681,19 +681,19 @@ envelope_calc(slot_t *slot) } static void -envelope_key_on(slot_t *slot, uint8_t type) +envelope_key_on(opl3_slot *slot, uint8_t type) { slot->key |= type; } static void -envelope_key_off(slot_t *slot, uint8_t type) +envelope_key_off(opl3_slot *slot, uint8_t type) { slot->key &= ~type; } static void -phase_generate(slot_t *slot) +phase_generate(opl3_slot *slot) { uint16_t f_num; uint32_t basefreq; @@ -776,7 +776,7 @@ phase_generate(slot_t *slot) } static void -slot_write_20(slot_t *slot, uint8_t data) +slot_write_20(opl3_slot *slot, uint8_t data) { if ((data >> 7) & 0x01) slot->trem = &slot->dev->tremolo; @@ -790,7 +790,7 @@ slot_write_20(slot_t *slot, uint8_t data) } static void -slot_write_40(slot_t *slot, uint8_t data) +slot_write_40(opl3_slot *slot, uint8_t data) { slot->reg_ksl = (data >> 6) & 0x03; slot->reg_tl = data & 0x3f; @@ -799,14 +799,14 @@ slot_write_40(slot_t *slot, uint8_t data) } static void -slot_write_60(slot_t *slot, uint8_t data) +slot_write_60(opl3_slot *slot, uint8_t data) { slot->reg_ar = (data >> 4) & 0x0f; slot->reg_dr = data & 0x0f; } static void -slot_write_80(slot_t *slot, uint8_t data) +slot_write_80(opl3_slot *slot, uint8_t data) { slot->reg_sl = (data >> 4) & 0x0f; @@ -817,7 +817,7 @@ slot_write_80(slot_t *slot, uint8_t data) } static void -slot_write_e0(slot_t *slot, uint8_t data) +slot_write_e0(opl3_slot *slot, uint8_t data) { slot->reg_wf = data & 0x07; @@ -826,14 +826,14 @@ slot_write_e0(slot_t *slot, uint8_t data) } static void -slot_generate(slot_t *slot) +slot_generate(opl3_slot *slot) { slot->out = envelope_sin[slot->reg_wf](slot->pg_phase_out + *slot->mod, slot->eg_out); } static void -slot_calc_fb(slot_t *slot) +slot_calc_fb(opl3_slot *slot) { if (slot->chan->fb != 0x00) slot->fbmod = (slot->prout + slot->out) >> (0x09 - slot->chan->fb); @@ -1198,7 +1198,7 @@ channel_set_4op(nuked_t *dev, uint8_t data) } static void -process_slot(slot_t *slot) +process_slot(opl3_slot *slot) { slot_calc_fb(slot); envelope_calc(slot); @@ -1562,7 +1562,7 @@ nuked_generate_stream(nuked_t *dev, int32_t *sndptr, uint32_t num) void nuked_init(nuked_t *dev, uint32_t samplerate) { - slot_t *slot; + opl3_slot *slot; chan_t *ch; uint8_t i; uint8_t local_ch_slot; From 3c0387bfc1ea793356d4cc1aa9241db1ef387f0a Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Fri, 12 Jul 2024 21:02:50 -0400 Subject: [PATCH 19/28] Nuked: nuked_t -> opl3_chip --- src/include/86box/snd_opl_nuked.h | 2 ++ src/sound/snd_opl_nuked.c | 34 +++++++++++++++---------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/include/86box/snd_opl_nuked.h b/src/include/86box/snd_opl_nuked.h index e53f860f1..da2a8ae86 100644 --- a/src/include/86box/snd_opl_nuked.h +++ b/src/include/86box/snd_opl_nuked.h @@ -20,4 +20,6 @@ #ifndef SOUND_OPL_NUKED_H #define SOUND_OPL_NUKED_H +void nuked_write_reg(opl3_chip *dev, uint16_t reg, uint8_t val); + #endif /*SOUND_OPL_NUKED_H*/ diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index a634a2a82..4887c2a2b 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -204,10 +204,10 @@ typedef struct chip { uint32_t wrbuf_last; uint64_t wrbuf_lasttime; wrbuf_t wrbuf[WRBUF_SIZE]; -} nuked_t; +} opl3_chip; typedef struct { - nuked_t opl; + opl3_chip opl; int8_t flags; int8_t pad; @@ -703,7 +703,7 @@ phase_generate(opl3_slot *slot) uint16_t phase; int8_t range; uint8_t vibpos; - nuked_t *dev; + opl3_chip *dev; dev = slot->dev; f_num = slot->chan->f_num; @@ -849,7 +849,7 @@ static void channel_setup_alg(chan_t *ch); static void -channel_update_rhythm(nuked_t *dev, uint8_t data) +channel_update_rhythm(opl3_chip *dev, uint8_t data) { chan_t *ch6; chan_t *ch7; @@ -1174,7 +1174,7 @@ channel_key_off(chan_t *ch) } static void -channel_set_4op(nuked_t *dev, uint8_t data) +channel_set_4op(opl3_chip *dev, uint8_t data) { uint8_t chnum; @@ -1209,7 +1209,7 @@ process_slot(opl3_slot *slot) inline void nuked_generate_4ch(void *priv, int32_t *buf4) { - nuked_t *dev = (nuked_t *) priv; + opl3_chip *dev = (opl3_chip *) priv; chan_t *ch; wrbuf_t *writebuf; int16_t **out; @@ -1332,7 +1332,7 @@ nuked_generate_4ch(void *priv, int32_t *buf4) } void -nuked_generate(nuked_t *dev, int32_t *buf) +nuked_generate(opl3_chip *dev, int32_t *buf) { int32_t samples[4]; nuked_generate_4ch(dev, samples); @@ -1341,7 +1341,7 @@ nuked_generate(nuked_t *dev, int32_t *buf) } void -nuked_generate_4ch_resampled(nuked_t *dev, int32_t *buf4) +nuked_generate_4ch_resampled(opl3_chip *dev, int32_t *buf4) { while (dev->samplecnt >= dev->rateratio) { dev->oldsamples[0] = dev->samples[0]; @@ -1369,7 +1369,7 @@ nuked_generate_4ch_resampled(nuked_t *dev, int32_t *buf4) } void -nuked_generate_resampled(nuked_t *dev, int16_t *buf4) +nuked_generate_resampled(opl3_chip *dev, int16_t *buf4) { int16_t samples[4]; nuked_generate_4ch_resampled(dev, samples); @@ -1378,7 +1378,7 @@ nuked_generate_resampled(nuked_t *dev, int16_t *buf4) } void -nuked_generate_raw(nuked_t *dev, int32_t *bufp) +nuked_generate_raw(opl3_chip *dev, int32_t *bufp) { nuked_generate(dev, dev->samples); @@ -1389,7 +1389,7 @@ nuked_generate_raw(nuked_t *dev, int32_t *bufp) uint16_t nuked_write_addr(void *priv, uint16_t port, uint8_t val) { - const nuked_t *dev = (nuked_t *) priv; + const opl3_chip *dev = (opl3_chip *) priv; uint16_t addr; addr = val; @@ -1402,7 +1402,7 @@ nuked_write_addr(void *priv, uint16_t port, uint8_t val) void nuked_write_reg(void *priv, uint16_t reg, uint8_t val) { - nuked_t *dev = (nuked_t *) priv; + opl3_chip *dev = (opl3_chip *) priv; uint8_t high = (reg >> 8) & 0x01; uint8_t regm = reg & 0xff; @@ -1505,7 +1505,7 @@ nuked_write_reg(void *priv, uint16_t reg, uint8_t val) void nuked_write_reg_buffered(void *priv, uint16_t reg, uint8_t val) { - nuked_t *dev = (nuked_t *) priv; + opl3_chip *dev = (opl3_chip *) priv; uint64_t time1; uint64_t time2; wrbuf_t *writebuf; @@ -1535,7 +1535,7 @@ nuked_write_reg_buffered(void *priv, uint16_t reg, uint8_t val) } void -nuked_generate_4ch_stream(nuked_t *dev, int16_t *sndptr1, int16_t *sndptr2, uint32_t numsamples) +nuked_generate_4ch_stream(opl3_chip *dev, int16_t *sndptr1, int16_t *sndptr2, uint32_t numsamples) { int16_t samples[4]; @@ -1551,7 +1551,7 @@ nuked_generate_4ch_stream(nuked_t *dev, int16_t *sndptr1, int16_t *sndptr2, uint } void -nuked_generate_stream(nuked_t *dev, int32_t *sndptr, uint32_t num) +nuked_generate_stream(opl3_chip *dev, int32_t *sndptr, uint32_t num) { for (uint_fast32_t i = 0; i < num; i++) { nuked_generate_resampled(dev, sndptr); @@ -1560,14 +1560,14 @@ nuked_generate_stream(nuked_t *dev, int32_t *sndptr, uint32_t num) } void -nuked_init(nuked_t *dev, uint32_t samplerate) +nuked_init(opl3_chip *dev, uint32_t samplerate) { opl3_slot *slot; chan_t *ch; uint8_t i; uint8_t local_ch_slot; - memset(dev, 0x00, sizeof(nuked_t)); + memset(dev, 0x00, sizeof(opl3_chip)); for (i = 0; i < 36; i++) { slot = &dev->slot[i]; From 483fe43fb4d4d3cd2e93d8870a21a9b639e41cf7 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Fri, 12 Jul 2024 20:28:12 -0400 Subject: [PATCH 20/28] Nuked: Compile fixes Update snd_opl_nuked.h --- src/include/86box/snd_opl_nuked.h | 2 +- src/sound/snd_opl_nuked.c | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/include/86box/snd_opl_nuked.h b/src/include/86box/snd_opl_nuked.h index da2a8ae86..1cc4fe64e 100644 --- a/src/include/86box/snd_opl_nuked.h +++ b/src/include/86box/snd_opl_nuked.h @@ -20,6 +20,6 @@ #ifndef SOUND_OPL_NUKED_H #define SOUND_OPL_NUKED_H -void nuked_write_reg(opl3_chip *dev, uint16_t reg, uint8_t val); +void nuked_write_reg(void *priv, uint16_t reg, uint8_t val); #endif /*SOUND_OPL_NUKED_H*/ diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index 4887c2a2b..0cddb33d0 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -1206,7 +1206,7 @@ process_slot(opl3_slot *slot) slot_generate(slot); } -inline void +static inline void nuked_generate_4ch(void *priv, int32_t *buf4) { opl3_chip *dev = (opl3_chip *) priv; @@ -1369,9 +1369,9 @@ nuked_generate_4ch_resampled(opl3_chip *dev, int32_t *buf4) } void -nuked_generate_resampled(opl3_chip *dev, int16_t *buf4) +nuked_generate_resampled(opl3_chip *dev, int32_t *buf4) { - int16_t samples[4]; + int32_t samples[4]; nuked_generate_4ch_resampled(dev, samples); buf4[0] = samples[0]; buf4[1] = samples[1]; @@ -1537,7 +1537,7 @@ nuked_write_reg_buffered(void *priv, uint16_t reg, uint8_t val) void nuked_generate_4ch_stream(opl3_chip *dev, int16_t *sndptr1, int16_t *sndptr2, uint32_t numsamples) { - int16_t samples[4]; + int32_t samples[4]; for (uint_fast32_t i = 0; i < numsamples; i++) { nuked_generate_4ch_resampled(dev, samples); From d3142e77936d293be8c7c090c416fd50d2c55c61 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Fri, 12 Jul 2024 21:15:34 -0400 Subject: [PATCH 21/28] More reorganization in snd_opl_nuked.c --- src/sound/snd_opl_nuked.c | 132 +++++++++++++++++++------------------- 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index 0cddb33d0..7e535eb04 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -1377,6 +1377,72 @@ nuked_generate_resampled(opl3_chip *dev, int32_t *buf4) buf4[1] = samples[1]; } +void +nuked_init(opl3_chip *dev, uint32_t samplerate) +{ + opl3_slot *slot; + chan_t *ch; + uint8_t i; + uint8_t local_ch_slot; + + memset(dev, 0x00, sizeof(opl3_chip)); + + for (i = 0; i < 36; i++) { + slot = &dev->slot[i]; + slot->dev = dev; + slot->mod = &dev->zeromod; + slot->eg_rout = 0x01ff; + slot->eg_out = 0x01ff; + slot->eg_gen = envelope_gen_num_release; + slot->trem = (uint8_t *) &dev->zeromod; + slot->slot_num = i; + } + + for (i = 0; i < 18; i++) { + ch = &dev->chan[i]; + local_ch_slot = ch_slot[i]; + ch->slotz[0] = &dev->slot[local_ch_slot]; + ch->slotz[1] = &dev->slot[local_ch_slot + 3u]; + dev->slot[local_ch_slot].chan = ch; + dev->slot[local_ch_slot + 3u].chan = ch; + + if ((i % 9) < 3) + ch->pair = &dev->chan[i + 3u]; + else if ((i % 9) < 6) + ch->pair = &dev->chan[i - 3u]; + + ch->dev = dev; + ch->out[0] = &dev->zeromod; + ch->out[1] = &dev->zeromod; + ch->out[2] = &dev->zeromod; + ch->out[3] = &dev->zeromod; + ch->chtype = ch_2op; + ch->cha = 0xffff; + ch->chb = 0xffff; +#if OPL_ENABLE_STEREOEXT + ch->leftpan = 0x10000; + ch->rightpan = 0x10000; +#endif + ch->ch_num = i; + + channel_setup_alg(ch); + } + + dev->noise = 1; + dev->rateratio = (samplerate << RSM_FRAC) / 49716; + dev->tremoloshift = 4; + dev->vibshift = 1; + + +#if OPL_ENABLE_STEREOEXT + if (!panpot_lut_build) { + for (int32_t i = 0; i < 256; i++) + panpot_lut[i] = OPL_SIN(i); + panpot_lut_build = 1; + } +#endif +} + void nuked_generate_raw(opl3_chip *dev, int32_t *bufp) { @@ -1559,72 +1625,6 @@ nuked_generate_stream(opl3_chip *dev, int32_t *sndptr, uint32_t num) } } -void -nuked_init(opl3_chip *dev, uint32_t samplerate) -{ - opl3_slot *slot; - chan_t *ch; - uint8_t i; - uint8_t local_ch_slot; - - memset(dev, 0x00, sizeof(opl3_chip)); - - for (i = 0; i < 36; i++) { - slot = &dev->slot[i]; - slot->dev = dev; - slot->mod = &dev->zeromod; - slot->eg_rout = 0x01ff; - slot->eg_out = 0x01ff; - slot->eg_gen = envelope_gen_num_release; - slot->trem = (uint8_t *) &dev->zeromod; - slot->slot_num = i; - } - - for (i = 0; i < 18; i++) { - ch = &dev->chan[i]; - local_ch_slot = ch_slot[i]; - ch->slotz[0] = &dev->slot[local_ch_slot]; - ch->slotz[1] = &dev->slot[local_ch_slot + 3u]; - dev->slot[local_ch_slot].chan = ch; - dev->slot[local_ch_slot + 3u].chan = ch; - - if ((i % 9) < 3) - ch->pair = &dev->chan[i + 3u]; - else if ((i % 9) < 6) - ch->pair = &dev->chan[i - 3u]; - - ch->dev = dev; - ch->out[0] = &dev->zeromod; - ch->out[1] = &dev->zeromod; - ch->out[2] = &dev->zeromod; - ch->out[3] = &dev->zeromod; - ch->chtype = ch_2op; - ch->cha = 0xffff; - ch->chb = 0xffff; -#if OPL_ENABLE_STEREOEXT - ch->leftpan = 0x10000; - ch->rightpan = 0x10000; -#endif - ch->ch_num = i; - - channel_setup_alg(ch); - } - - dev->noise = 1; - dev->rateratio = (samplerate << RSM_FRAC) / 49716; - dev->tremoloshift = 4; - dev->vibshift = 1; - - -#if OPL_ENABLE_STEREOEXT - if (!panpot_lut_build) { - for (int32_t i = 0; i < 256; i++) - panpot_lut[i] = OPL_SIN(i); - panpot_lut_build = 1; - } -#endif -} - static void nuked_timer_tick(nuked_drv_t *dev, int tmr) { From 8cb40b8b68f0ae735a17c3a31613fe01f4b91b5f Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Fri, 12 Jul 2024 21:27:04 -0400 Subject: [PATCH 22/28] Nuked: Move stuff out to it's header --- src/include/86box/snd_opl_nuked.h | 153 +++++++++++++++++++++++++++++ src/sound/snd_opl_nuked.c | 154 +----------------------------- 2 files changed, 154 insertions(+), 153 deletions(-) diff --git a/src/include/86box/snd_opl_nuked.h b/src/include/86box/snd_opl_nuked.h index 1cc4fe64e..801dd238c 100644 --- a/src/include/86box/snd_opl_nuked.h +++ b/src/include/86box/snd_opl_nuked.h @@ -20,6 +20,159 @@ #ifndef SOUND_OPL_NUKED_H #define SOUND_OPL_NUKED_H +#ifndef OPL_ENABLE_STEREOEXT +#define OPL_ENABLE_STEREOEXT 0 +#endif + +#define WRBUF_SIZE 1024 +#define WRBUF_DELAY 1 + +struct chan; +struct chip; + +typedef struct slot { + struct chan *chan; + struct chip *dev; + int16_t out; + int16_t fbmod; + int16_t *mod; + int16_t prout; + int16_t eg_rout; + int16_t eg_out; + uint8_t eg_inc; + uint8_t eg_gen; + uint8_t eg_rate; + uint8_t eg_ksl; + uint8_t *trem; + uint8_t reg_vib; + uint8_t reg_type; + uint8_t reg_ksr; + uint8_t reg_mult; + uint8_t reg_ksl; + uint8_t reg_tl; + uint8_t reg_ar; + uint8_t reg_dr; + uint8_t reg_sl; + uint8_t reg_rr; + uint8_t reg_wf; + uint8_t key; + uint32_t pg_reset; + uint32_t pg_phase; + uint16_t pg_phase_out; + uint8_t slot_num; +} opl3_slot; + +typedef struct chan { + opl3_slot *slotz[2]; // Don't use "slots" keyword to avoid conflict with Qt applications + struct chan *pair; + struct chip *dev; + int16_t *out[4]; +#if OPL_ENABLE_STEREOEXT + int32_t leftpan; + int32_t rightpan; +#endif + uint8_t chtype; + uint16_t f_num; + uint8_t block; + uint8_t fb; + uint8_t con; + uint8_t alg; + uint8_t ksv; + uint16_t cha; + uint16_t chb; + uint16_t chc; + uint16_t chd; + uint8_t ch_num; +} chan_t; + +typedef struct wrbuf { + uint64_t time; + uint16_t reg; + uint8_t data; +} wrbuf_t; + +typedef struct chip { + chan_t chan[18]; + opl3_slot slot[36]; + uint16_t timer; + uint64_t eg_timer; + uint8_t eg_timerrem; + uint8_t eg_state; + uint8_t eg_add; + uint64_t eg_timer_lo; + uint8_t newm; + uint8_t nts; + uint8_t rhy; + uint8_t vibpos; + uint8_t vibshift; + uint8_t tremolo; + uint8_t tremolopos; + uint8_t tremoloshift; + uint32_t noise; + int16_t zeromod; + int32_t mixbuff[4]; + uint8_t rm_hh_bit2; + uint8_t rm_hh_bit3; + uint8_t rm_hh_bit7; + uint8_t rm_hh_bit8; + uint8_t rm_tc_bit3; + uint8_t rm_tc_bit5; + +#if OPL_ENABLE_STEREOEXT + uint8_t stereoext; +#endif + + // OPL3L + int32_t rateratio; + int32_t samplecnt; + int32_t oldsamples[4]; + int32_t samples[4]; + + uint64_t wrbuf_samplecnt; + uint32_t wrbuf_cur; + uint32_t wrbuf_last; + uint64_t wrbuf_lasttime; + wrbuf_t wrbuf[WRBUF_SIZE]; +} opl3_chip; + +typedef struct { + opl3_chip opl; + int8_t flags; + int8_t pad; + + uint16_t port; + uint8_t status; + uint8_t timer_ctrl; + uint16_t timer_count[2]; + uint16_t timer_cur_count[2]; + + pc_timer_t timers[2]; + + int pos; + int32_t buffer[MUSICBUFLEN * 2]; +} nuked_drv_t; + +enum { + FLAG_CYCLES = 0x02, + FLAG_OPL3 = 0x01 +}; + +enum { + STAT_TMR_OVER = 0x60, + STAT_TMR1_OVER = 0x40, + STAT_TMR2_OVER = 0x20, + STAT_TMR_ANY = 0x80 +}; + +enum { + CTRL_RESET = 0x80, + CTRL_TMR_MASK = 0x60, + CTRL_TMR1_MASK = 0x40, + CTRL_TMR2_MASK = 0x20, + CTRL_TMR2_START = 0x02, + CTRL_TMR1_START = 0x01 +}; + void nuked_write_reg(void *priv, uint16_t reg, uint8_t val); #endif /*SOUND_OPL_NUKED_H*/ diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index 7e535eb04..2ca41f53d 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -45,17 +45,13 @@ #define HAVE_STDARG_H #include <86box/86box.h> -#include <86box/snd_opl_nuked.h> #include <86box/sound.h> #include <86box/timer.h> #include <86box/device.h> #include <86box/snd_opl.h> +#include <86box/snd_opl_nuked.h> -#ifndef OPL_ENABLE_STEREOEXT -#define OPL_ENABLE_STEREOEXT 0 -#endif - #if OPL_ENABLE_STEREOEXT && !defined OPL_SIN #ifndef _USE_MATH_DEFINES #define _USE_MATH_DEFINES 1 @@ -70,8 +66,6 @@ #define OPL_QUIRK_CHANNELSAMPLEDELAY (!OPL_ENABLE_STEREOEXT) #endif -#define WRBUF_SIZE 1024 -#define WRBUF_DELAY 1 #define RSM_FRAC 10 // #define OPL_FREQ FREQ_48000 @@ -98,152 +92,6 @@ enum envelope_gen_num { envelope_gen_num_release = 3 }; -struct chan; -struct chip; - -typedef struct slot { - struct chan *chan; - struct chip *dev; - int16_t out; - int16_t fbmod; - int16_t *mod; - int16_t prout; - int16_t eg_rout; - int16_t eg_out; - uint8_t eg_inc; - uint8_t eg_gen; - uint8_t eg_rate; - uint8_t eg_ksl; - uint8_t *trem; - uint8_t reg_vib; - uint8_t reg_type; - uint8_t reg_ksr; - uint8_t reg_mult; - uint8_t reg_ksl; - uint8_t reg_tl; - uint8_t reg_ar; - uint8_t reg_dr; - uint8_t reg_sl; - uint8_t reg_rr; - uint8_t reg_wf; - uint8_t key; - uint32_t pg_reset; - uint32_t pg_phase; - uint16_t pg_phase_out; - uint8_t slot_num; -} opl3_slot; - -typedef struct chan { - opl3_slot *slotz[2]; // Don't use "slots" keyword to avoid conflict with Qt applications - struct chan *pair; - struct chip *dev; - int16_t *out[4]; -#if OPL_ENABLE_STEREOEXT - int32_t leftpan; - int32_t rightpan; -#endif - uint8_t chtype; - uint16_t f_num; - uint8_t block; - uint8_t fb; - uint8_t con; - uint8_t alg; - uint8_t ksv; - uint16_t cha; - uint16_t chb; - uint16_t chc; - uint16_t chd; - uint8_t ch_num; -} chan_t; - -typedef struct wrbuf { - uint64_t time; - uint16_t reg; - uint8_t data; -} wrbuf_t; - -typedef struct chip { - chan_t chan[18]; - opl3_slot slot[36]; - uint16_t timer; - uint64_t eg_timer; - uint8_t eg_timerrem; - uint8_t eg_state; - uint8_t eg_add; - uint64_t eg_timer_lo; - uint8_t newm; - uint8_t nts; - uint8_t rhy; - uint8_t vibpos; - uint8_t vibshift; - uint8_t tremolo; - uint8_t tremolopos; - uint8_t tremoloshift; - uint32_t noise; - int16_t zeromod; - int32_t mixbuff[4]; - uint8_t rm_hh_bit2; - uint8_t rm_hh_bit3; - uint8_t rm_hh_bit7; - uint8_t rm_hh_bit8; - uint8_t rm_tc_bit3; - uint8_t rm_tc_bit5; - -#if OPL_ENABLE_STEREOEXT - uint8_t stereoext; -#endif - - // OPL3L - int32_t rateratio; - int32_t samplecnt; - int32_t oldsamples[4]; - int32_t samples[4]; - - uint64_t wrbuf_samplecnt; - uint32_t wrbuf_cur; - uint32_t wrbuf_last; - uint64_t wrbuf_lasttime; - wrbuf_t wrbuf[WRBUF_SIZE]; -} opl3_chip; - -typedef struct { - opl3_chip opl; - int8_t flags; - int8_t pad; - - uint16_t port; - uint8_t status; - uint8_t timer_ctrl; - uint16_t timer_count[2]; - uint16_t timer_cur_count[2]; - - pc_timer_t timers[2]; - - int pos; - int32_t buffer[MUSICBUFLEN * 2]; -} nuked_drv_t; - -enum { - FLAG_CYCLES = 0x02, - FLAG_OPL3 = 0x01 -}; - -enum { - STAT_TMR_OVER = 0x60, - STAT_TMR1_OVER = 0x40, - STAT_TMR2_OVER = 0x20, - STAT_TMR_ANY = 0x80 -}; - -enum { - CTRL_RESET = 0x80, - CTRL_TMR_MASK = 0x60, - CTRL_TMR1_MASK = 0x40, - CTRL_TMR2_MASK = 0x20, - CTRL_TMR2_START = 0x02, - CTRL_TMR1_START = 0x01 -}; - #ifdef ENABLE_OPL_LOG int nuked_do_log = ENABLE_OPL_LOG; From 2bcfc8e697a02d8b12e5b475e2805dff5b3146ec Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Sat, 13 Jul 2024 23:51:59 -0400 Subject: [PATCH 23/28] Nuked: dev -> chip --- src/include/86box/snd_opl_nuked.h | 4 +- src/sound/snd_opl_nuked.c | 457 +++++++++++++++--------------- 2 files changed, 230 insertions(+), 231 deletions(-) diff --git a/src/include/86box/snd_opl_nuked.h b/src/include/86box/snd_opl_nuked.h index 801dd238c..082f16f5d 100644 --- a/src/include/86box/snd_opl_nuked.h +++ b/src/include/86box/snd_opl_nuked.h @@ -32,7 +32,7 @@ struct chip; typedef struct slot { struct chan *chan; - struct chip *dev; + struct chip *chip; int16_t out; int16_t fbmod; int16_t *mod; @@ -65,7 +65,7 @@ typedef struct slot { typedef struct chan { opl3_slot *slotz[2]; // Don't use "slots" keyword to avoid conflict with Qt applications struct chan *pair; - struct chip *dev; + struct chip *chip; int16_t *out[4]; #if OPL_ENABLE_STEREOEXT int32_t leftpan; diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index 2ca41f53d..c3e709948 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -448,12 +448,12 @@ envelope_calc(opl3_slot *slot) rate_lo = rate & 0x03; if (rate_hi & 0x10) rate_hi = 0x0f; - eg_shift = rate_hi + slot->dev->eg_add; + eg_shift = rate_hi + slot->chip->eg_add; shift = 0; if (nonzero) { if (rate_hi < 12) { - if (slot->dev->eg_state) + if (slot->chip->eg_state) switch (eg_shift) { case 12: shift = 1; @@ -471,11 +471,11 @@ envelope_calc(opl3_slot *slot) break; } } else { - shift = (rate_hi & 0x03) + eg_incstep[rate_lo][slot->dev->eg_timer_lo]; + shift = (rate_hi & 0x03) + eg_incstep[rate_lo][slot->chip->eg_timer_lo]; if (shift & 0x04) shift = 0x03; if (!shift) - shift = slot->dev->eg_state; + shift = slot->chip->eg_state; } } @@ -551,19 +551,19 @@ phase_generate(opl3_slot *slot) uint16_t phase; int8_t range; uint8_t vibpos; - opl3_chip *dev; + opl3_chip *chip; - dev = slot->dev; + chip = slot->chip; f_num = slot->chan->f_num; if (slot->reg_vib) { range = (f_num >> 7) & 7; - vibpos = dev->vibpos; + vibpos = chip->vibpos; if (!(vibpos & 3)) range = 0; else if (vibpos & 1) range >>= 1; - range >>= dev->vibshift; + range >>= chip->vibshift; if (vibpos & 4) range = -range; @@ -578,22 +578,22 @@ phase_generate(opl3_slot *slot) slot->pg_phase += (basefreq * mt[slot->reg_mult]) >> 1; // Rhythm mode - noise = dev->noise; + noise = chip->noise; slot->pg_phase_out = phase; if (slot->slot_num == 13) { // hh - dev->rm_hh_bit2 = (phase >> 2) & 1; - dev->rm_hh_bit3 = (phase >> 3) & 1; - dev->rm_hh_bit7 = (phase >> 7) & 1; - dev->rm_hh_bit8 = (phase >> 8) & 1; + chip->rm_hh_bit2 = (phase >> 2) & 1; + chip->rm_hh_bit3 = (phase >> 3) & 1; + chip->rm_hh_bit7 = (phase >> 7) & 1; + chip->rm_hh_bit8 = (phase >> 8) & 1; } - if (slot->slot_num == 17 && (dev->rhy & 0x20)) { // tc - dev->rm_tc_bit3 = (phase >> 3) & 1; - dev->rm_tc_bit5 = (phase >> 5) & 1; + if (slot->slot_num == 17 && (chip->rhy & 0x20)) { // tc + chip->rm_tc_bit3 = (phase >> 3) & 1; + chip->rm_tc_bit5 = (phase >> 5) & 1; } - if (dev->rhy & 0x20) { - rm_xor = (dev->rm_hh_bit2 ^ dev->rm_hh_bit7) - | (dev->rm_hh_bit3 ^ dev->rm_tc_bit5) - | (dev->rm_tc_bit3 ^ dev->rm_tc_bit5); + if (chip->rhy & 0x20) { + rm_xor = (chip->rm_hh_bit2 ^ chip->rm_hh_bit7) + | (chip->rm_hh_bit3 ^ chip->rm_tc_bit5) + | (chip->rm_tc_bit3 ^ chip->rm_tc_bit5); switch (slot->slot_num) { case 13: // hh @@ -605,8 +605,8 @@ phase_generate(opl3_slot *slot) break; case 16: // sd - slot->pg_phase_out = (dev->rm_hh_bit8 << 9) - | ((dev->rm_hh_bit8 ^ (noise & 1)) << 8); + slot->pg_phase_out = (chip->rm_hh_bit8 << 9) + | ((chip->rm_hh_bit8 ^ (noise & 1)) << 8); break; case 17: // tc @@ -620,16 +620,16 @@ phase_generate(opl3_slot *slot) n_bit = ((noise >> 14) ^ noise) & 0x01; - dev->noise = (noise >> 1) | (n_bit << 22); + chip->noise = (noise >> 1) | (n_bit << 22); } static void slot_write_20(opl3_slot *slot, uint8_t data) { if ((data >> 7) & 0x01) - slot->trem = &slot->dev->tremolo; + slot->trem = &slot->chip->tremolo; else - slot->trem = (uint8_t *) &slot->dev->zeromod; + slot->trem = (uint8_t *) &slot->chip->zeromod; slot->reg_vib = (data >> 6) & 0x01; slot->reg_type = (data >> 5) & 0x01; @@ -669,7 +669,7 @@ slot_write_e0(opl3_slot *slot, uint8_t data) { slot->reg_wf = data & 0x07; - if (slot->dev->newm == 0x00) + if (slot->chip->newm == 0x00) slot->reg_wf &= 0x03; } @@ -697,22 +697,22 @@ static void channel_setup_alg(chan_t *ch); static void -channel_update_rhythm(opl3_chip *dev, uint8_t data) +channel_update_rhythm(opl3_chip *chip, uint8_t data) { chan_t *ch6; chan_t *ch7; chan_t *ch8; uint8_t chnum; - dev->rhy = data & 0x3f; - if (dev->rhy & 0x20) { - ch6 = &dev->chan[6]; - ch7 = &dev->chan[7]; - ch8 = &dev->chan[8]; + chip->rhy = data & 0x3f; + if (chip->rhy & 0x20) { + ch6 = &chip->chan[6]; + ch7 = &chip->chan[7]; + ch8 = &chip->chan[8]; ch6->out[0] = &ch6->slotz[1]->out; ch6->out[1] = &ch6->slotz[1]->out; - ch6->out[2] = &dev->zeromod; - ch6->out[3] = &dev->zeromod; + ch6->out[2] = &chip->zeromod; + ch6->out[3] = &chip->zeromod; ch7->out[0] = &ch7->slotz[0]->out; ch7->out[1] = &ch7->slotz[0]->out; ch7->out[2] = &ch7->slotz[1]->out; @@ -723,38 +723,38 @@ channel_update_rhythm(opl3_chip *dev, uint8_t data) ch8->out[3] = &ch8->slotz[1]->out; for (chnum = 6; chnum < 9; chnum++) - dev->chan[chnum].chtype = ch_drum; + chip->chan[chnum].chtype = ch_drum; channel_setup_alg(ch6); channel_setup_alg(ch7); channel_setup_alg(ch8); // hh - if (dev->rhy & 0x01) + if (chip->rhy & 0x01) envelope_key_on(ch7->slotz[0], egk_drum); else envelope_key_off(ch7->slotz[0], egk_drum); // tc - if (dev->rhy & 0x02) + if (chip->rhy & 0x02) envelope_key_on(ch8->slotz[1], egk_drum); else envelope_key_off(ch8->slotz[1], egk_drum); // tom - if (dev->rhy & 0x04) + if (chip->rhy & 0x04) envelope_key_on(ch8->slotz[0], egk_drum); else envelope_key_off(ch8->slotz[0], egk_drum); // sd - if (dev->rhy & 0x08) + if (chip->rhy & 0x08) envelope_key_on(ch7->slotz[1], egk_drum); else envelope_key_off(ch7->slotz[1], egk_drum); // bd - if (dev->rhy & 0x10) { + if (chip->rhy & 0x10) { envelope_key_on(ch6->slotz[0], egk_drum); envelope_key_on(ch6->slotz[1], egk_drum); } else { @@ -763,11 +763,11 @@ channel_update_rhythm(opl3_chip *dev, uint8_t data) } } else { for (chnum = 6; chnum < 9; chnum++) { - dev->chan[chnum].chtype = ch_2op; + chip->chan[chnum].chtype = ch_2op; - channel_setup_alg(&dev->chan[chnum]); - envelope_key_off(dev->chan[chnum].slotz[0], egk_drum); - envelope_key_off(dev->chan[chnum].slotz[1], egk_drum); + channel_setup_alg(&chip->chan[chnum]); + envelope_key_off(chip->chan[chnum].slotz[0], egk_drum); + envelope_key_off(chip->chan[chnum].slotz[1], egk_drum); } } } @@ -775,17 +775,17 @@ channel_update_rhythm(opl3_chip *dev, uint8_t data) static void channel_write_a0(chan_t *ch, uint8_t data) { - if (ch->dev->newm && ch->chtype == ch_4op2) + if (ch->chip->newm && ch->chtype == ch_4op2) return; ch->f_num = (ch->f_num & 0x300) | data; ch->ksv = (ch->block << 1) - | ((ch->f_num >> (0x09 - ch->dev->nts)) & 0x01); + | ((ch->f_num >> (0x09 - ch->chip->nts)) & 0x01); envelope_update_ksl(ch->slotz[0]); envelope_update_ksl(ch->slotz[1]); - if (ch->dev->newm && ch->chtype == ch_4op) { + if (ch->chip->newm && ch->chtype == ch_4op) { ch->pair->f_num = ch->f_num; ch->pair->ksv = ch->ksv; @@ -797,18 +797,18 @@ channel_write_a0(chan_t *ch, uint8_t data) static void channel_write_b0(chan_t *ch, uint8_t data) { - if (ch->dev->newm && ch->chtype == ch_4op2) + if (ch->chip->newm && ch->chtype == ch_4op2) return; ch->f_num = (ch->f_num & 0xff) | ((data & 0x03) << 8); ch->block = (data >> 2) & 0x07; ch->ksv = (ch->block << 1) - | ((ch->f_num >> (0x09 - ch->dev->nts)) & 0x01); + | ((ch->f_num >> (0x09 - ch->chip->nts)) & 0x01); envelope_update_ksl(ch->slotz[0]); envelope_update_ksl(ch->slotz[1]); - if (ch->dev->newm && ch->chtype == ch_4op) { + if (ch->chip->newm && ch->chtype == ch_4op) { ch->pair->f_num = ch->f_num; ch->pair->block = ch->block; ch->pair->ksv = ch->ksv; @@ -823,8 +823,8 @@ channel_setup_alg(chan_t *ch) { if (ch->chtype == ch_drum) { if (ch->ch_num == 7 || ch->ch_num == 8) { - ch->slotz[0]->mod = &ch->dev->zeromod; - ch->slotz[1]->mod = &ch->dev->zeromod; + ch->slotz[0]->mod = &ch->chip->zeromod; + ch->slotz[1]->mod = &ch->chip->zeromod; return; } @@ -836,7 +836,7 @@ channel_setup_alg(chan_t *ch) case 0x01: ch->slotz[0]->mod = &ch->slotz[0]->fbmod; - ch->slotz[1]->mod = &ch->dev->zeromod; + ch->slotz[1]->mod = &ch->chip->zeromod; break; default: @@ -849,10 +849,10 @@ channel_setup_alg(chan_t *ch) return; if (ch->alg & 0x04) { - ch->pair->out[0] = &ch->dev->zeromod; - ch->pair->out[1] = &ch->dev->zeromod; - ch->pair->out[2] = &ch->dev->zeromod; - ch->pair->out[3] = &ch->dev->zeromod; + ch->pair->out[0] = &ch->chip->zeromod; + ch->pair->out[1] = &ch->chip->zeromod; + ch->pair->out[2] = &ch->chip->zeromod; + ch->pair->out[3] = &ch->chip->zeromod; switch (ch->alg & 0x03) { case 0x00: @@ -861,42 +861,42 @@ channel_setup_alg(chan_t *ch) ch->slotz[0]->mod = &ch->pair->slotz[1]->out; ch->slotz[1]->mod = &ch->slotz[0]->out; ch->out[0] = &ch->slotz[1]->out; - ch->out[1] = &ch->dev->zeromod; - ch->out[2] = &ch->dev->zeromod; - ch->out[3] = &ch->dev->zeromod; + ch->out[1] = &ch->chip->zeromod; + ch->out[2] = &ch->chip->zeromod; + ch->out[3] = &ch->chip->zeromod; break; case 0x01: ch->pair->slotz[0]->mod = &ch->pair->slotz[0]->fbmod; ch->pair->slotz[1]->mod = &ch->pair->slotz[0]->out; - ch->slotz[0]->mod = &ch->dev->zeromod; + ch->slotz[0]->mod = &ch->chip->zeromod; ch->slotz[1]->mod = &ch->slotz[0]->out; ch->out[0] = &ch->pair->slotz[1]->out; ch->out[1] = &ch->slotz[1]->out; - ch->out[2] = &ch->dev->zeromod; - ch->out[3] = &ch->dev->zeromod; + ch->out[2] = &ch->chip->zeromod; + ch->out[3] = &ch->chip->zeromod; break; case 0x02: ch->pair->slotz[0]->mod = &ch->pair->slotz[0]->fbmod; - ch->pair->slotz[1]->mod = &ch->dev->zeromod; + ch->pair->slotz[1]->mod = &ch->chip->zeromod; ch->slotz[0]->mod = &ch->pair->slotz[1]->out; ch->slotz[1]->mod = &ch->slotz[0]->out; ch->out[0] = &ch->pair->slotz[0]->out; ch->out[1] = &ch->slotz[1]->out; - ch->out[2] = &ch->dev->zeromod; - ch->out[3] = &ch->dev->zeromod; + ch->out[2] = &ch->chip->zeromod; + ch->out[3] = &ch->chip->zeromod; break; case 0x03: ch->pair->slotz[0]->mod = &ch->pair->slotz[0]->fbmod; - ch->pair->slotz[1]->mod = &ch->dev->zeromod; + ch->pair->slotz[1]->mod = &ch->chip->zeromod; ch->slotz[0]->mod = &ch->pair->slotz[1]->out; - ch->slotz[1]->mod = &ch->dev->zeromod; + ch->slotz[1]->mod = &ch->chip->zeromod; ch->out[0] = &ch->pair->slotz[0]->out; ch->out[1] = &ch->slotz[0]->out; ch->out[2] = &ch->slotz[1]->out; - ch->out[3] = &ch->dev->zeromod; + ch->out[3] = &ch->chip->zeromod; break; default: @@ -908,18 +908,18 @@ channel_setup_alg(chan_t *ch) ch->slotz[0]->mod = &ch->slotz[0]->fbmod; ch->slotz[1]->mod = &ch->slotz[0]->out; ch->out[0] = &ch->slotz[1]->out; - ch->out[1] = &ch->dev->zeromod; - ch->out[2] = &ch->dev->zeromod; - ch->out[3] = &ch->dev->zeromod; + ch->out[1] = &ch->chip->zeromod; + ch->out[2] = &ch->chip->zeromod; + ch->out[3] = &ch->chip->zeromod; break; case 0x01: ch->slotz[0]->mod = &ch->slotz[0]->fbmod; - ch->slotz[1]->mod = &ch->dev->zeromod; + ch->slotz[1]->mod = &ch->chip->zeromod; ch->out[0] = &ch->slotz[0]->out; ch->out[1] = &ch->slotz[1]->out; - ch->out[2] = &ch->dev->zeromod; - ch->out[3] = &ch->dev->zeromod; + ch->out[2] = &ch->chip->zeromod; + ch->out[3] = &ch->chip->zeromod; break; default: @@ -932,7 +932,7 @@ channel_update_alg(chan_t *ch) { ch->alg = ch->con; - if (ch->dev->newm) { + if (ch->chip->newm) { if (ch->chtype == ch_4op) { ch->pair->alg = 0x04 | (ch->con << 1) | ch->pair->con; ch->alg = 0x08; @@ -953,7 +953,7 @@ channel_write_c0(chan_t *ch, uint8_t data) ch->fb = (data & 0x0e) >> 1; ch->con = data & 0x01; - if (ch->dev->newm) { + if (ch->chip->newm) { ch->cha = ((data >> 4) & 0x01) ? ~0 : 0; ch->chb = ((data >> 5) & 0x01) ? ~0 : 0; ch->chc = ((data >> 6) & 0x01) ? ~0 : 0; @@ -965,7 +965,7 @@ channel_write_c0(chan_t *ch, uint8_t data) } #if OPL_ENABLE_STEREOEXT - if (!ch->dev->stereoext) { + if (!ch->chip->stereoext) { ch->leftpan = ch->cha << 16; ch->rightpan = ch->chb << 16; } @@ -976,7 +976,7 @@ channel_write_c0(chan_t *ch, uint8_t data) static void channel_write_d0(chan_t *ch, uint8_t data) { - if (ch->dev->stereoext) { + if (ch->chip->stereoext) { ch->leftpan = panpot_lut[data ^ 0xffu]; ch->rightpan = panpot_lut[data]; } @@ -986,7 +986,7 @@ channel_write_d0(chan_t *ch, uint8_t data) static void channel_key_on(chan_t *ch) { - if (ch->dev->newm) { + if (ch->chip->newm) { if (ch->chtype == ch_4op) { envelope_key_on(ch->slotz[0], egk_norm); envelope_key_on(ch->slotz[1], egk_norm); @@ -1005,7 +1005,7 @@ channel_key_on(chan_t *ch) static void channel_key_off(chan_t *ch) { - if (ch->dev->newm) { + if (ch->chip->newm) { if (ch->chtype == ch_4op) { envelope_key_off(ch->slotz[0], egk_norm); envelope_key_off(ch->slotz[1], egk_norm); @@ -1022,7 +1022,7 @@ channel_key_off(chan_t *ch) } static void -channel_set_4op(opl3_chip *dev, uint8_t data) +channel_set_4op(opl3_chip *chip, uint8_t data) { uint8_t chnum; @@ -1033,14 +1033,14 @@ channel_set_4op(opl3_chip *dev, uint8_t data) chnum += 9 - 3; if ((data >> bit) & 0x01) { - dev->chan[chnum].chtype = ch_4op; - dev->chan[chnum + 3u].chtype = ch_4op2; - channel_update_alg(&dev->chan[chnum]); + chip->chan[chnum].chtype = ch_4op; + chip->chan[chnum + 3u].chtype = ch_4op2; + channel_update_alg(&chip->chan[chnum]); } else { - dev->chan[chnum].chtype = ch_2op; - dev->chan[chnum + 3u].chtype = ch_2op; - channel_update_alg(&dev->chan[chnum]); - channel_update_alg(&dev->chan[chnum + 3u]); + chip->chan[chnum].chtype = ch_2op; + chip->chan[chnum + 3u].chtype = ch_2op; + channel_update_alg(&chip->chan[chnum]); + channel_update_alg(&chip->chan[chnum + 3u]); } } } @@ -1057,29 +1057,29 @@ process_slot(opl3_slot *slot) static inline void nuked_generate_4ch(void *priv, int32_t *buf4) { - opl3_chip *dev = (opl3_chip *) priv; - chan_t *ch; - wrbuf_t *writebuf; - int16_t **out; - int32_t mix[2]; - int16_t accm; - int16_t shift = 0; - uint8_t i; + opl3_chip *chip = (opl3_chip *) priv; + chan_t *ch; + wrbuf_t *writebuf; + int16_t **out; + int32_t mix[2]; + int16_t accm; + int16_t shift = 0; + uint8_t i; - buf4[1] = dev->mixbuff[1]; - buf4[3] = dev->mixbuff[3]; + buf4[1] = chip->mixbuff[1]; + buf4[3] = chip->mixbuff[3]; #if OPL_QUIRK_CHANNELSAMPLEDELAY for (i = 0; i < 15; i++) #else for (i = 0; i < 36; i++) #endif - process_slot(&dev->slot[i]); + process_slot(&chip->slot[i]); mix[0] = mix[1] = 0; for (i = 0; i < 18; i++) { - ch = &dev->chan[i]; + ch = &chip->chan[i]; out = ch->out; accm = *out[0] + *out[1] + *out[2] + *out[3]; #if OPL_ENABLE_STEREOEXT @@ -1090,26 +1090,26 @@ nuked_generate_4ch(void *priv, int32_t *buf4) mix[1] += (int32_t) (accm & ch->chc); } - dev->mixbuff[0] = mix[0]; - dev->mixbuff[2] = mix[1]; + chip->mixbuff[0] = mix[0]; + chip->mixbuff[2] = mix[1]; #if OPL_QUIRK_CHANNELSAMPLEDELAY for (i = 15; i < 18; i++) - process_slot(&dev->slot[i]); + process_slot(&chip->slot[i]); #endif - buf4[0] = dev->mixbuff[0]; - buf4[2] = dev->mixbuff[2]; + buf4[0] = chip->mixbuff[0]; + buf4[2] = chip->mixbuff[2]; #if OPL_QUIRK_CHANNELSAMPLEDELAY for (i = 18; i < 33; i++) - process_slot(&dev->slot[i]); + process_slot(&chip->slot[i]); #endif mix[0] = mix[1] = 0; for (i = 0; i < 18; i++) { - ch = &dev->chan[i]; + ch = &chip->chan[i]; out = ch->out; accm = *out[0] + *out[1] + *out[2] + *out[3]; #if OPL_ENABLE_STEREOEXT @@ -1120,150 +1120,150 @@ nuked_generate_4ch(void *priv, int32_t *buf4) mix[1] += (int32_t) (accm & ch->chd); } - dev->mixbuff[1] = mix[0]; - dev->mixbuff[3] = mix[1]; + chip->mixbuff[1] = mix[0]; + chip->mixbuff[3] = mix[1]; #if OPL_QUIRK_CHANNELSAMPLEDELAY for (i = 33; i < 36; i++) - process_slot(&dev->slot[i]); + process_slot(&chip->slot[i]); #endif - if ((dev->timer & 0x3f) == 0x3f) - dev->tremolopos = (dev->tremolopos + 1) % 210; + if ((chip->timer & 0x3f) == 0x3f) + chip->tremolopos = (chip->tremolopos + 1) % 210; - if (dev->tremolopos < 105) - dev->tremolo = dev->tremolopos >> dev->tremoloshift; + if (chip->tremolopos < 105) + chip->tremolo = chip->tremolopos >> chip->tremoloshift; else - dev->tremolo = (210 - dev->tremolopos) >> dev->tremoloshift; + chip->tremolo = (210 - chip->tremolopos) >> chip->tremoloshift; - if ((dev->timer & 0x03ff) == 0x03ff) - dev->vibpos = (dev->vibpos + 1) & 7; + if ((chip->timer & 0x03ff) == 0x03ff) + chip->vibpos = (chip->vibpos + 1) & 7; - dev->timer++; + chip->timer++; - if (dev->eg_state) { - while (shift < 13 && ((dev->eg_timer >> shift) & 1) == 0) + if (chip->eg_state) { + while (shift < 13 && ((chip->eg_timer >> shift) & 1) == 0) shift++; if (shift > 12) - dev->eg_add = 0; + chip->eg_add = 0; else - dev->eg_add = shift + 1; + chip->eg_add = shift + 1; - dev->eg_timer_lo = (uint8_t) (dev->eg_timer & 0x3u); + chip->eg_timer_lo = (uint8_t) (chip->eg_timer & 0x3u); } - if (dev->eg_timerrem || dev->eg_state) { - if (dev->eg_timer == UINT64_C(0xfffffffff)) { - dev->eg_timer = 0; - dev->eg_timerrem = 1; + if (chip->eg_timerrem || chip->eg_state) { + if (chip->eg_timer == UINT64_C(0xfffffffff)) { + chip->eg_timer = 0; + chip->eg_timerrem = 1; } else { - dev->eg_timer++; - dev->eg_timerrem = 0; + chip->eg_timer++; + chip->eg_timerrem = 0; } } - dev->eg_state ^= 1; + chip->eg_state ^= 1; - while (writebuf = &dev->wrbuf[dev->wrbuf_cur], writebuf->time <= dev->wrbuf_samplecnt) { + while (writebuf = &chip->wrbuf[chip->wrbuf_cur], writebuf->time <= chip->wrbuf_samplecnt) { if (!(writebuf->reg & 0x200)) break; writebuf->reg &= 0x01ff; - nuked_write_reg(dev, writebuf->reg, writebuf->data); + nuked_write_reg(chip, writebuf->reg, writebuf->data); - dev->wrbuf_cur = (dev->wrbuf_cur + 1) % WRBUF_SIZE; + chip->wrbuf_cur = (chip->wrbuf_cur + 1) % WRBUF_SIZE; } - dev->wrbuf_samplecnt++; + chip->wrbuf_samplecnt++; } void -nuked_generate(opl3_chip *dev, int32_t *buf) +nuked_generate(opl3_chip *chip, int32_t *buf) { int32_t samples[4]; - nuked_generate_4ch(dev, samples); + nuked_generate_4ch(chip, samples); buf[0] = samples[0]; buf[1] = samples[1]; } void -nuked_generate_4ch_resampled(opl3_chip *dev, int32_t *buf4) +nuked_generate_4ch_resampled(opl3_chip *chip, int32_t *buf4) { - while (dev->samplecnt >= dev->rateratio) { - dev->oldsamples[0] = dev->samples[0]; - dev->oldsamples[1] = dev->samples[1]; - dev->oldsamples[2] = dev->samples[2]; - dev->oldsamples[3] = dev->samples[3]; - nuked_generate_4ch(dev, dev->samples); - dev->samplecnt -= dev->rateratio; + while (chip->samplecnt >= chip->rateratio) { + chip->oldsamples[0] = chip->samples[0]; + chip->oldsamples[1] = chip->samples[1]; + chip->oldsamples[2] = chip->samples[2]; + chip->oldsamples[3] = chip->samples[3]; + nuked_generate_4ch(chip, chip->samples); + chip->samplecnt -= chip->rateratio; } - buf4[0] = (int32_t) ((dev->oldsamples[0] * (dev->rateratio - dev->samplecnt) - + dev->samples[0] * dev->samplecnt) - / dev->rateratio); - buf4[1] = (int32_t) ((dev->oldsamples[1] * (dev->rateratio - dev->samplecnt) - + dev->samples[1] * dev->samplecnt) - / dev->rateratio); - buf4[2] = (int32_t) ((dev->oldsamples[2] * (dev->rateratio - dev->samplecnt) - + dev->samples[2] * dev->samplecnt) - / dev->rateratio); - buf4[3] = (int32_t) ((dev->oldsamples[3] * (dev->rateratio - dev->samplecnt) - + dev->samples[3] * dev->samplecnt) - / dev->rateratio); + buf4[0] = (int32_t) ((chip->oldsamples[0] * (chip->rateratio - chip->samplecnt) + + chip->samples[0] * chip->samplecnt) + / chip->rateratio); + buf4[1] = (int32_t) ((chip->oldsamples[1] * (chip->rateratio - chip->samplecnt) + + chip->samples[1] * chip->samplecnt) + / chip->rateratio); + buf4[2] = (int32_t) ((chip->oldsamples[2] * (chip->rateratio - chip->samplecnt) + + chip->samples[2] * chip->samplecnt) + / chip->rateratio); + buf4[3] = (int32_t) ((chip->oldsamples[3] * (chip->rateratio - chip->samplecnt) + + chip->samples[3] * chip->samplecnt) + / chip->rateratio); - dev->samplecnt += 1 << RSM_FRAC; + chip->samplecnt += 1 << RSM_FRAC; } void -nuked_generate_resampled(opl3_chip *dev, int32_t *buf4) +nuked_generate_resampled(opl3_chip *chip, int32_t *buf4) { int32_t samples[4]; - nuked_generate_4ch_resampled(dev, samples); + nuked_generate_4ch_resampled(chip, samples); buf4[0] = samples[0]; buf4[1] = samples[1]; } void -nuked_init(opl3_chip *dev, uint32_t samplerate) +nuked_init(opl3_chip *chip, uint32_t samplerate) { opl3_slot *slot; chan_t *ch; uint8_t i; uint8_t local_ch_slot; - memset(dev, 0x00, sizeof(opl3_chip)); + memset(chip, 0x00, sizeof(opl3_chip)); for (i = 0; i < 36; i++) { - slot = &dev->slot[i]; - slot->dev = dev; - slot->mod = &dev->zeromod; + slot = &chip->slot[i]; + slot->chip = chip; + slot->mod = &chip->zeromod; slot->eg_rout = 0x01ff; slot->eg_out = 0x01ff; slot->eg_gen = envelope_gen_num_release; - slot->trem = (uint8_t *) &dev->zeromod; + slot->trem = (uint8_t *) &chip->zeromod; slot->slot_num = i; } for (i = 0; i < 18; i++) { - ch = &dev->chan[i]; - local_ch_slot = ch_slot[i]; - ch->slotz[0] = &dev->slot[local_ch_slot]; - ch->slotz[1] = &dev->slot[local_ch_slot + 3u]; - dev->slot[local_ch_slot].chan = ch; - dev->slot[local_ch_slot + 3u].chan = ch; + ch = &chip->chan[i]; + local_ch_slot = ch_slot[i]; + ch->slotz[0] = &chip->slot[local_ch_slot]; + ch->slotz[1] = &chip->slot[local_ch_slot + 3u]; + chip->slot[local_ch_slot].chan = ch; + chip->slot[local_ch_slot + 3u].chan = ch; if ((i % 9) < 3) - ch->pair = &dev->chan[i + 3u]; + ch->pair = &chip->chan[i + 3u]; else if ((i % 9) < 6) - ch->pair = &dev->chan[i - 3u]; + ch->pair = &chip->chan[i - 3u]; - ch->dev = dev; - ch->out[0] = &dev->zeromod; - ch->out[1] = &dev->zeromod; - ch->out[2] = &dev->zeromod; - ch->out[3] = &dev->zeromod; + ch->chip = chip; + ch->out[0] = &chip->zeromod; + ch->out[1] = &chip->zeromod; + ch->out[2] = &chip->zeromod; + ch->out[3] = &chip->zeromod; ch->chtype = ch_2op; ch->cha = 0xffff; ch->chb = 0xffff; @@ -1276,11 +1276,10 @@ nuked_init(opl3_chip *dev, uint32_t samplerate) channel_setup_alg(ch); } - dev->noise = 1; - dev->rateratio = (samplerate << RSM_FRAC) / 49716; - dev->tremoloshift = 4; - dev->vibshift = 1; - + chip->noise = 1; + chip->rateratio = (samplerate << RSM_FRAC) / 49716; + chip->tremoloshift = 4; + chip->vibshift = 1; #if OPL_ENABLE_STEREOEXT if (!panpot_lut_build) { @@ -1292,22 +1291,22 @@ nuked_init(opl3_chip *dev, uint32_t samplerate) } void -nuked_generate_raw(opl3_chip *dev, int32_t *bufp) +nuked_generate_raw(opl3_chip *chip, int32_t *bufp) { - nuked_generate(dev, dev->samples); + nuked_generate(chip, chip->samples); - bufp[0] = (int32_t) dev->samples[0]; - bufp[1] = (int32_t) dev->samples[1]; + bufp[0] = (int32_t) chip->samples[0]; + bufp[1] = (int32_t) chip->samples[1]; } uint16_t nuked_write_addr(void *priv, uint16_t port, uint8_t val) { - const opl3_chip *dev = (opl3_chip *) priv; + const opl3_chip *chip = (opl3_chip *) priv; uint16_t addr; addr = val; - if ((port & 0x0002) && ((addr == 0x0005) || dev->newm)) + if ((port & 0x0002) && ((addr == 0x0005) || chip->newm)) addr |= 0x0100; return addr; @@ -1316,22 +1315,22 @@ nuked_write_addr(void *priv, uint16_t port, uint8_t val) void nuked_write_reg(void *priv, uint16_t reg, uint8_t val) { - opl3_chip *dev = (opl3_chip *) priv; - uint8_t high = (reg >> 8) & 0x01; - uint8_t regm = reg & 0xff; + opl3_chip *chip = (opl3_chip *) priv; + uint8_t high = (reg >> 8) & 0x01; + uint8_t regm = reg & 0xff; switch (regm & 0xf0) { case 0x00: if (high) switch (regm & 0x0f) { case 0x04: - channel_set_4op(dev, val); + channel_set_4op(chip, val); break; case 0x05: - dev->newm = val & 0x01; + chip->newm = val & 0x01; #if OPL_ENABLE_STEREOEXT - dev->stereoext = (val >> 1) & 0x01; + chip->stereoext = (val >> 1) & 0x01; #endif break; @@ -1341,7 +1340,7 @@ nuked_write_reg(void *priv, uint16_t reg, uint8_t val) else switch (regm & 0x0f) { case 0x08: - dev->nts = (val >> 6) & 0x01; + chip->nts = (val >> 6) & 0x01; break; default: @@ -1352,63 +1351,63 @@ nuked_write_reg(void *priv, uint16_t reg, uint8_t val) case 0x20: case 0x30: if (ad_slot[regm & 0x1fu] >= 0) - slot_write_20(&dev->slot[18u * high + ad_slot[regm & 0x1fu]], val); + slot_write_20(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); break; case 0x40: case 0x50: if (ad_slot[regm & 0x1fu] >= 0) - slot_write_40(&dev->slot[18u * high + ad_slot[regm & 0x1fu]], val); + slot_write_40(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); break; case 0x60: case 0x70: if (ad_slot[regm & 0x1fu] >= 0) - slot_write_60(&dev->slot[18u * high + ad_slot[regm & 0x1fu]], val); + slot_write_60(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); break; case 0x80: case 0x90: if (ad_slot[regm & 0x1fu] >= 0) - slot_write_80(&dev->slot[18u * high + ad_slot[regm & 0x1fu]], val); + slot_write_80(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); break; case 0xa0: if ((regm & 0x0f) < 9) - channel_write_a0(&dev->chan[9u * high + (regm & 0x0fu)], val); + channel_write_a0(&chip->chan[9u * high + (regm & 0x0fu)], val); break; case 0xb0: if (regm == 0xbd && !high) { - dev->tremoloshift = (((val >> 7) ^ 1) << 1) + 2; - dev->vibshift = ((val >> 6) & 0x01) ^ 1; - channel_update_rhythm(dev, val); + chip->tremoloshift = (((val >> 7) ^ 1) << 1) + 2; + chip->vibshift = ((val >> 6) & 0x01) ^ 1; + channel_update_rhythm(chip, val); } else if ((regm & 0x0f) < 9) { - channel_write_b0(&dev->chan[9u * high + (regm & 0x0fu)], val); + channel_write_b0(&chip->chan[9u * high + (regm & 0x0fu)], val); if (val & 0x20) - channel_key_on(&dev->chan[9u * high + (regm & 0x0fu)]); + channel_key_on(&chip->chan[9u * high + (regm & 0x0fu)]); else - channel_key_off(&dev->chan[9u * high + (regm & 0x0fu)]); + channel_key_off(&chip->chan[9u * high + (regm & 0x0fu)]); } break; case 0xc0: if ((regm & 0x0f) < 9) - channel_write_c0(&dev->chan[9u * high + (regm & 0x0fu)], val); + channel_write_c0(&chip->chan[9u * high + (regm & 0x0fu)], val); break; #if OPL_ENABLE_STEREOEXT case 0xd0: if ((regm & 0x0f) < 9) - channel_write_d0(&dev->chan[9u * high + (regm & 0x0fu)], val); + channel_write_d0(&chip->chan[9u * high + (regm & 0x0fu)], val); break; #endif case 0xe0: case 0xf0: if (ad_slot[regm & 0x1fu] >= 0) - slot_write_e0(&dev->slot[18u * high + ad_slot[regm & 0x1fu]], val); + slot_write_e0(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); break; default: @@ -1419,42 +1418,42 @@ nuked_write_reg(void *priv, uint16_t reg, uint8_t val) void nuked_write_reg_buffered(void *priv, uint16_t reg, uint8_t val) { - opl3_chip *dev = (opl3_chip *) priv; - uint64_t time1; - uint64_t time2; - wrbuf_t *writebuf; - uint32_t writebuf_last; + opl3_chip *chip = (opl3_chip *) priv; + uint64_t time1; + uint64_t time2; + wrbuf_t *writebuf; + uint32_t writebuf_last; - writebuf_last = dev->wrbuf_last; - writebuf = &dev->wrbuf[writebuf_last]; + writebuf_last = chip->wrbuf_last; + writebuf = &chip->wrbuf[writebuf_last]; if (writebuf->reg & 0x0200) { - nuked_write_reg(dev, writebuf->reg & 0x01ff, writebuf->data); + nuked_write_reg(chip, writebuf->reg & 0x01ff, writebuf->data); - dev->wrbuf_cur = (writebuf_last + 1) % WRBUF_SIZE; - dev->wrbuf_samplecnt = writebuf->time; + chip->wrbuf_cur = (writebuf_last + 1) % WRBUF_SIZE; + chip->wrbuf_samplecnt = writebuf->time; } writebuf->reg = reg | 0x0200; writebuf->data = val; - time1 = dev->wrbuf_lasttime + WRBUF_DELAY; - time2 = dev->wrbuf_samplecnt; + time1 = chip->wrbuf_lasttime + WRBUF_DELAY; + time2 = chip->wrbuf_samplecnt; if (time1 < time2) time1 = time2; writebuf->time = time1; - dev->wrbuf_lasttime = time1; - dev->wrbuf_last = (writebuf_last + 1) % WRBUF_SIZE; + chip->wrbuf_lasttime = time1; + chip->wrbuf_last = (writebuf_last + 1) % WRBUF_SIZE; } void -nuked_generate_4ch_stream(opl3_chip *dev, int16_t *sndptr1, int16_t *sndptr2, uint32_t numsamples) +nuked_generate_4ch_stream(opl3_chip *chip, int16_t *sndptr1, int16_t *sndptr2, uint32_t numsamples) { int32_t samples[4]; for (uint_fast32_t i = 0; i < numsamples; i++) { - nuked_generate_4ch_resampled(dev, samples); + nuked_generate_4ch_resampled(chip, samples); sndptr1[0] = samples[0]; sndptr1[1] = samples[1]; sndptr2[0] = samples[2]; @@ -1465,10 +1464,10 @@ nuked_generate_4ch_stream(opl3_chip *dev, int16_t *sndptr1, int16_t *sndptr2, ui } void -nuked_generate_stream(opl3_chip *dev, int32_t *sndptr, uint32_t num) +nuked_generate_stream(opl3_chip *chip, int32_t *sndptr, uint32_t num) { for (uint_fast32_t i = 0; i < num; i++) { - nuked_generate_resampled(dev, sndptr); + nuked_generate_resampled(chip, sndptr); sndptr += 2; } } From c7a268a01e5751d6bd10be13fe6019f9201fc0d6 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Sun, 14 Jul 2024 00:41:43 -0400 Subject: [PATCH 24/28] Nuked: ch/chan -> channel --- src/include/86box/snd_opl_nuked.h | 4 +- src/sound/snd_opl_nuked.c | 473 +++++++++++++++--------------- 2 files changed, 239 insertions(+), 238 deletions(-) diff --git a/src/include/86box/snd_opl_nuked.h b/src/include/86box/snd_opl_nuked.h index 082f16f5d..d0b6924d9 100644 --- a/src/include/86box/snd_opl_nuked.h +++ b/src/include/86box/snd_opl_nuked.h @@ -31,7 +31,7 @@ struct chan; struct chip; typedef struct slot { - struct chan *chan; + struct chan *channel; struct chip *chip; int16_t out; int16_t fbmod; @@ -92,7 +92,7 @@ typedef struct wrbuf { } wrbuf_t; typedef struct chip { - chan_t chan[18]; + chan_t channel[18]; opl3_slot slot[36]; uint16_t timer; uint64_t eg_timer; diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index c3e709948..a9a91d962 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -387,8 +387,8 @@ static const envelope_sinfunc envelope_sin[8] = { static void envelope_update_ksl(opl3_slot *slot) { - int16_t ksl = (kslrom[slot->chan->f_num >> 6u] << 2) - - ((0x08 - slot->chan->block) << 5); + int16_t ksl = (kslrom[slot->channel->f_num >> 6u] << 2) + - ((0x08 - slot->channel->block) << 5); if (ksl < 0) ksl = 0; @@ -441,13 +441,15 @@ envelope_calc(opl3_slot *slot) } slot->pg_reset = reset; - ks = slot->chan->ksv >> ((slot->reg_ksr ^ 1) << 1); + ks = slot->channel->ksv >> ((slot->reg_ksr ^ 1) << 1); nonzero = (reg_rate != 0); rate = ks + (reg_rate << 2); rate_hi = rate >> 2; rate_lo = rate & 0x03; + if (rate_hi & 0x10) rate_hi = 0x0f; + eg_shift = rate_hi + slot->chip->eg_add; shift = 0; @@ -554,7 +556,7 @@ phase_generate(opl3_slot *slot) opl3_chip *chip; chip = slot->chip; - f_num = slot->chan->f_num; + f_num = slot->channel->f_num; if (slot->reg_vib) { range = (f_num >> 7) & 7; vibpos = chip->vibpos; @@ -570,7 +572,7 @@ phase_generate(opl3_slot *slot) f_num += range; } - basefreq = (f_num << slot->chan->block) >> 1; + basefreq = (f_num << slot->channel->block) >> 1; phase = (uint16_t) (slot->pg_phase >> 9); if (slot->pg_reset) @@ -683,7 +685,7 @@ slot_generate(opl3_slot *slot) static void slot_calc_fb(opl3_slot *slot) { - if (slot->chan->fb != 0x00) + if (slot->channel->fb != 0x00) slot->fbmod = (slot->prout + slot->out) >> (0x09 - slot->chan->fb); else slot->fbmod = 0; @@ -692,151 +694,150 @@ slot_calc_fb(opl3_slot *slot) } // Channel - static void -channel_setup_alg(chan_t *ch); +channel_setup_alg(chan_t *channel); static void channel_update_rhythm(opl3_chip *chip, uint8_t data) { - chan_t *ch6; - chan_t *ch7; - chan_t *ch8; + chan_t *channel6; + chan_t *channel7; + chan_t *channel8; uint8_t chnum; chip->rhy = data & 0x3f; if (chip->rhy & 0x20) { - ch6 = &chip->chan[6]; - ch7 = &chip->chan[7]; - ch8 = &chip->chan[8]; - ch6->out[0] = &ch6->slotz[1]->out; - ch6->out[1] = &ch6->slotz[1]->out; - ch6->out[2] = &chip->zeromod; - ch6->out[3] = &chip->zeromod; - ch7->out[0] = &ch7->slotz[0]->out; - ch7->out[1] = &ch7->slotz[0]->out; - ch7->out[2] = &ch7->slotz[1]->out; - ch7->out[3] = &ch7->slotz[1]->out; - ch8->out[0] = &ch8->slotz[0]->out; - ch8->out[1] = &ch8->slotz[0]->out; - ch8->out[2] = &ch8->slotz[1]->out; - ch8->out[3] = &ch8->slotz[1]->out; + channel6 = &chip->channel[6]; + channel7 = &chip->channel[7]; + channel8 = &chip->channel[8]; + channel6->out[0] = &channel6->slotz[1]->out; + channel6->out[1] = &channel6->slotz[1]->out; + channel6->out[2] = &chip->zeromod; + channel6->out[3] = &chip->zeromod; + channel7->out[0] = &channel7->slotz[0]->out; + channel7->out[1] = &channel7->slotz[0]->out; + channel7->out[2] = &channel7->slotz[1]->out; + channel7->out[3] = &channel7->slotz[1]->out; + channel8->out[0] = &channel8->slotz[0]->out; + channel8->out[1] = &channel8->slotz[0]->out; + channel8->out[2] = &channel8->slotz[1]->out; + channel8->out[3] = &channel8->slotz[1]->out; for (chnum = 6; chnum < 9; chnum++) - chip->chan[chnum].chtype = ch_drum; + chip->channel[chnum].chtype = ch_drum; - channel_setup_alg(ch6); - channel_setup_alg(ch7); - channel_setup_alg(ch8); + channel_setup_alg(channel6); + channel_setup_alg(channel7); + channel_setup_alg(channel8); // hh if (chip->rhy & 0x01) - envelope_key_on(ch7->slotz[0], egk_drum); + envelope_key_on(channel7->slotz[0], egk_drum); else - envelope_key_off(ch7->slotz[0], egk_drum); + envelope_key_off(channel7->slotz[0], egk_drum); // tc if (chip->rhy & 0x02) - envelope_key_on(ch8->slotz[1], egk_drum); + envelope_key_on(channel8->slotz[1], egk_drum); else - envelope_key_off(ch8->slotz[1], egk_drum); + envelope_key_off(channel8->slotz[1], egk_drum); // tom if (chip->rhy & 0x04) - envelope_key_on(ch8->slotz[0], egk_drum); + envelope_key_on(channel8->slotz[0], egk_drum); else - envelope_key_off(ch8->slotz[0], egk_drum); + envelope_key_off(channel8->slotz[0], egk_drum); // sd if (chip->rhy & 0x08) - envelope_key_on(ch7->slotz[1], egk_drum); + envelope_key_on(channel7->slotz[1], egk_drum); else - envelope_key_off(ch7->slotz[1], egk_drum); + envelope_key_off(channel7->slotz[1], egk_drum); // bd if (chip->rhy & 0x10) { - envelope_key_on(ch6->slotz[0], egk_drum); - envelope_key_on(ch6->slotz[1], egk_drum); + envelope_key_on(channel6->slotz[0], egk_drum); + envelope_key_on(channel6->slotz[1], egk_drum); } else { - envelope_key_off(ch6->slotz[0], egk_drum); - envelope_key_off(ch6->slotz[1], egk_drum); + envelope_key_off(channel6->slotz[0], egk_drum); + envelope_key_off(channel6->slotz[1], egk_drum); } } else { for (chnum = 6; chnum < 9; chnum++) { - chip->chan[chnum].chtype = ch_2op; + chip->channel[chnum].chtype = ch_2op; - channel_setup_alg(&chip->chan[chnum]); - envelope_key_off(chip->chan[chnum].slotz[0], egk_drum); - envelope_key_off(chip->chan[chnum].slotz[1], egk_drum); + channel_setup_alg(&chip->channel[chnum]); + envelope_key_off(chip->channel[chnum].slotz[0], egk_drum); + envelope_key_off(chip->channel[chnum].slotz[1], egk_drum); } } } static void -channel_write_a0(chan_t *ch, uint8_t data) +channel_write_a0(chan_t *channel, uint8_t data) { - if (ch->chip->newm && ch->chtype == ch_4op2) + if (channel->chip->newm && channel->chtype == ch_4op2) return; - ch->f_num = (ch->f_num & 0x300) | data; - ch->ksv = (ch->block << 1) - | ((ch->f_num >> (0x09 - ch->chip->nts)) & 0x01); + channel->f_num = (channel->f_num & 0x300) | data; + channel->ksv = (channel->block << 1) + | ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01); - envelope_update_ksl(ch->slotz[0]); - envelope_update_ksl(ch->slotz[1]); + envelope_update_ksl(channel->slotz[0]); + envelope_update_ksl(channel->slotz[1]); - if (ch->chip->newm && ch->chtype == ch_4op) { - ch->pair->f_num = ch->f_num; - ch->pair->ksv = ch->ksv; + if (channel->chip->newm && channel->chtype == ch_4op) { + channel->pair->f_num = channel->f_num; + channel->pair->ksv = channel->ksv; - envelope_update_ksl(ch->pair->slotz[0]); - envelope_update_ksl(ch->pair->slotz[1]); + envelope_update_ksl(channel->pair->slotz[0]); + envelope_update_ksl(channel->pair->slotz[1]); } } static void -channel_write_b0(chan_t *ch, uint8_t data) +channel_write_b0(chan_t *channel, uint8_t data) { - if (ch->chip->newm && ch->chtype == ch_4op2) + if (channel->chip->newm && channel->chtype == ch_4op2) return; - ch->f_num = (ch->f_num & 0xff) | ((data & 0x03) << 8); - ch->block = (data >> 2) & 0x07; - ch->ksv = (ch->block << 1) - | ((ch->f_num >> (0x09 - ch->chip->nts)) & 0x01); + channel->f_num = (channel->f_num & 0xff) | ((data & 0x03) << 8); + channel->block = (data >> 2) & 0x07; + channel->ksv = (channel->block << 1) + | ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01); - envelope_update_ksl(ch->slotz[0]); - envelope_update_ksl(ch->slotz[1]); + envelope_update_ksl(channel->slotz[0]); + envelope_update_ksl(channel->slotz[1]); - if (ch->chip->newm && ch->chtype == ch_4op) { - ch->pair->f_num = ch->f_num; - ch->pair->block = ch->block; - ch->pair->ksv = ch->ksv; + if (channel->chip->newm && channel->chtype == ch_4op) { + channel->pair->f_num = channel->f_num; + channel->pair->block = channel->block; + channel->pair->ksv = channel->ksv; - envelope_update_ksl(ch->pair->slotz[0]); - envelope_update_ksl(ch->pair->slotz[1]); + envelope_update_ksl(channel->pair->slotz[0]); + envelope_update_ksl(channel->pair->slotz[1]); } } static void -channel_setup_alg(chan_t *ch) +channel_setup_alg(chan_t *channel) { - if (ch->chtype == ch_drum) { - if (ch->ch_num == 7 || ch->ch_num == 8) { - ch->slotz[0]->mod = &ch->chip->zeromod; - ch->slotz[1]->mod = &ch->chip->zeromod; + if (channel->chtype == ch_drum) { + if (channel->ch_num == 7 || channel->ch_num == 8) { + channel->slotz[0]->mod = &channel->chip->zeromod; + channel->slotz[1]->mod = &channel->chip->zeromod; return; } - switch (ch->alg & 0x01) { + switch (channel->alg & 0x01) { case 0x00: - ch->slotz[0]->mod = &ch->slotz[0]->fbmod; - ch->slotz[1]->mod = &ch->slotz[0]->out; + channel->slotz[0]->mod = &channel->slotz[0]->fbmod; + channel->slotz[1]->mod = &channel->slotz[0]->out; break; case 0x01: - ch->slotz[0]->mod = &ch->slotz[0]->fbmod; - ch->slotz[1]->mod = &ch->chip->zeromod; + channel->slotz[0]->mod = &channel->slotz[0]->fbmod; + channel->slotz[1]->mod = &channel->chip->zeromod; break; default: @@ -845,81 +846,81 @@ channel_setup_alg(chan_t *ch) return; } - if (ch->alg & 0x08) + if (channel->alg & 0x08) return; - if (ch->alg & 0x04) { - ch->pair->out[0] = &ch->chip->zeromod; - ch->pair->out[1] = &ch->chip->zeromod; - ch->pair->out[2] = &ch->chip->zeromod; - ch->pair->out[3] = &ch->chip->zeromod; + if (channel->alg & 0x04) { + channel->pair->out[0] = &channel->chip->zeromod; + channel->pair->out[1] = &channel->chip->zeromod; + channel->pair->out[2] = &channel->chip->zeromod; + channel->pair->out[3] = &channel->chip->zeromod; - switch (ch->alg & 0x03) { + switch (channel->alg & 0x03) { case 0x00: - ch->pair->slotz[0]->mod = &ch->pair->slotz[0]->fbmod; - ch->pair->slotz[1]->mod = &ch->pair->slotz[0]->out; - ch->slotz[0]->mod = &ch->pair->slotz[1]->out; - ch->slotz[1]->mod = &ch->slotz[0]->out; - ch->out[0] = &ch->slotz[1]->out; - ch->out[1] = &ch->chip->zeromod; - ch->out[2] = &ch->chip->zeromod; - ch->out[3] = &ch->chip->zeromod; + channel->pair->slotz[0]->mod = &channel->pair->slotz[0]->fbmod; + channel->pair->slotz[1]->mod = &channel->pair->slotz[0]->out; + channel->slotz[0]->mod = &channel->pair->slotz[1]->out; + channel->slotz[1]->mod = &channel->slotz[0]->out; + channel->out[0] = &channel->slotz[1]->out; + channel->out[1] = &channel->chip->zeromod; + channel->out[2] = &channel->chip->zeromod; + channel->out[3] = &channel->chip->zeromod; break; case 0x01: - ch->pair->slotz[0]->mod = &ch->pair->slotz[0]->fbmod; - ch->pair->slotz[1]->mod = &ch->pair->slotz[0]->out; - ch->slotz[0]->mod = &ch->chip->zeromod; - ch->slotz[1]->mod = &ch->slotz[0]->out; - ch->out[0] = &ch->pair->slotz[1]->out; - ch->out[1] = &ch->slotz[1]->out; - ch->out[2] = &ch->chip->zeromod; - ch->out[3] = &ch->chip->zeromod; + channel->pair->slotz[0]->mod = &channel->pair->slotz[0]->fbmod; + channel->pair->slotz[1]->mod = &channel->pair->slotz[0]->out; + channel->slotz[0]->mod = &channel->chip->zeromod; + channel->slotz[1]->mod = &channel->slotz[0]->out; + channel->out[0] = &channel->pair->slotz[1]->out; + channel->out[1] = &channel->slotz[1]->out; + channel->out[2] = &channel->chip->zeromod; + channel->out[3] = &channel->chip->zeromod; break; case 0x02: - ch->pair->slotz[0]->mod = &ch->pair->slotz[0]->fbmod; - ch->pair->slotz[1]->mod = &ch->chip->zeromod; - ch->slotz[0]->mod = &ch->pair->slotz[1]->out; - ch->slotz[1]->mod = &ch->slotz[0]->out; - ch->out[0] = &ch->pair->slotz[0]->out; - ch->out[1] = &ch->slotz[1]->out; - ch->out[2] = &ch->chip->zeromod; - ch->out[3] = &ch->chip->zeromod; + channel->pair->slotz[0]->mod = &channel->pair->slotz[0]->fbmod; + channel->pair->slotz[1]->mod = &channel->chip->zeromod; + channel->slotz[0]->mod = &channel->pair->slotz[1]->out; + channel->slotz[1]->mod = &channel->slotz[0]->out; + channel->out[0] = &channel->pair->slotz[0]->out; + channel->out[1] = &channel->slotz[1]->out; + channel->out[2] = &channel->chip->zeromod; + channel->out[3] = &channel->chip->zeromod; break; case 0x03: - ch->pair->slotz[0]->mod = &ch->pair->slotz[0]->fbmod; - ch->pair->slotz[1]->mod = &ch->chip->zeromod; - ch->slotz[0]->mod = &ch->pair->slotz[1]->out; - ch->slotz[1]->mod = &ch->chip->zeromod; - ch->out[0] = &ch->pair->slotz[0]->out; - ch->out[1] = &ch->slotz[0]->out; - ch->out[2] = &ch->slotz[1]->out; - ch->out[3] = &ch->chip->zeromod; + channel->pair->slotz[0]->mod = &channel->pair->slotz[0]->fbmod; + channel->pair->slotz[1]->mod = &channel->chip->zeromod; + channel->slotz[0]->mod = &channel->pair->slotz[1]->out; + channel->slotz[1]->mod = &channel->chip->zeromod; + channel->out[0] = &channel->pair->slotz[0]->out; + channel->out[1] = &channel->slotz[0]->out; + channel->out[2] = &channel->slotz[1]->out; + channel->out[3] = &channel->chip->zeromod; break; default: break; } } else - switch (ch->alg & 0x01) { + switch (channel->alg & 0x01) { case 0x00: - ch->slotz[0]->mod = &ch->slotz[0]->fbmod; - ch->slotz[1]->mod = &ch->slotz[0]->out; - ch->out[0] = &ch->slotz[1]->out; - ch->out[1] = &ch->chip->zeromod; - ch->out[2] = &ch->chip->zeromod; - ch->out[3] = &ch->chip->zeromod; + channel->slotz[0]->mod = &channel->slotz[0]->fbmod; + channel->slotz[1]->mod = &channel->slotz[0]->out; + channel->out[0] = &channel->slotz[1]->out; + channel->out[1] = &channel->chip->zeromod; + channel->out[2] = &channel->chip->zeromod; + channel->out[3] = &channel->chip->zeromod; break; case 0x01: - ch->slotz[0]->mod = &ch->slotz[0]->fbmod; - ch->slotz[1]->mod = &ch->chip->zeromod; - ch->out[0] = &ch->slotz[0]->out; - ch->out[1] = &ch->slotz[1]->out; - ch->out[2] = &ch->chip->zeromod; - ch->out[3] = &ch->chip->zeromod; + channel->slotz[0]->mod = &channel->slotz[0]->fbmod; + channel->slotz[1]->mod = &channel->chip->zeromod; + channel->out[0] = &channel->slotz[0]->out; + channel->out[1] = &channel->slotz[1]->out; + channel->out[2] = &channel->chip->zeromod; + channel->out[3] = &channel->chip->zeromod; break; default: @@ -928,96 +929,96 @@ channel_setup_alg(chan_t *ch) } static void -channel_update_alg(chan_t *ch) +channel_update_alg(chan_t *channel) { - ch->alg = ch->con; + channel->alg = channel->con; - if (ch->chip->newm) { - if (ch->chtype == ch_4op) { - ch->pair->alg = 0x04 | (ch->con << 1) | ch->pair->con; - ch->alg = 0x08; - channel_setup_alg(ch->pair); - } else if (ch->chtype == ch_4op2) { - ch->alg = 0x04 | (ch->pair->con << 1) | ch->con; - ch->pair->alg = 0x08; - channel_setup_alg(ch); + if (channel->chip->newm) { + if (channel->chtype == ch_4op) { + channel->pair->alg = 0x04 | (channel->con << 1) | channel->pair->con; + channel->alg = 0x08; + channel_setup_alg(channel->pair); + } else if (channel->chtype == ch_4op2) { + channel->alg = 0x04 | (channel->pair->con << 1) | channel->con; + channel->pair->alg = 0x08; + channel_setup_alg(channel); } else - channel_setup_alg(ch); + channel_setup_alg(channel); } else - channel_setup_alg(ch); + channel_setup_alg(channel); } static void -channel_write_c0(chan_t *ch, uint8_t data) +channel_write_c0(chan_t *channel, uint8_t data) { - ch->fb = (data & 0x0e) >> 1; - ch->con = data & 0x01; + channel->fb = (data & 0x0e) >> 1; + channel->con = data & 0x01; - if (ch->chip->newm) { - ch->cha = ((data >> 4) & 0x01) ? ~0 : 0; - ch->chb = ((data >> 5) & 0x01) ? ~0 : 0; - ch->chc = ((data >> 6) & 0x01) ? ~0 : 0; - ch->chd = ((data >> 7) & 0x01) ? ~0 : 0; + if (channel->chip->newm) { + channel->cha = ((data >> 4) & 0x01) ? ~0 : 0; + channel->chb = ((data >> 5) & 0x01) ? ~0 : 0; + channel->chc = ((data >> 6) & 0x01) ? ~0 : 0; + channel->chd = ((data >> 7) & 0x01) ? ~0 : 0; } else { - ch->cha = ch->chb = (uint16_t) ~0; + channel->cha = ch->chb = (uint16_t) ~0; // TODO: Verify on real chip if DAC2 output is disabled in compat mode - ch->chc = ch->chd = 0; + channel->chc = ch->chd = 0; } #if OPL_ENABLE_STEREOEXT - if (!ch->chip->stereoext) { - ch->leftpan = ch->cha << 16; - ch->rightpan = ch->chb << 16; + if (!channel->chip->stereoext) { + channel->leftpan = channel->cha << 16; + channel->rightpan = channel->chb << 16; } #endif } #if OPL_ENABLE_STEREOEXT static void -channel_write_d0(chan_t *ch, uint8_t data) +channel_write_d0(chan_t *channel, uint8_t data) { - if (ch->chip->stereoext) { - ch->leftpan = panpot_lut[data ^ 0xffu]; - ch->rightpan = panpot_lut[data]; + if (channel->chip->stereoext) { + channel->leftpan = panpot_lut[data ^ 0xffu]; + channel->rightpan = panpot_lut[data]; } } #endif static void -channel_key_on(chan_t *ch) +channel_key_on(chan_t *channel) { - if (ch->chip->newm) { - if (ch->chtype == ch_4op) { - envelope_key_on(ch->slotz[0], egk_norm); - envelope_key_on(ch->slotz[1], egk_norm); - envelope_key_on(ch->pair->slotz[0], egk_norm); - envelope_key_on(ch->pair->slotz[1], egk_norm); - } else if (ch->chtype == ch_2op || ch->chtype == ch_drum) { - envelope_key_on(ch->slotz[0], egk_norm); - envelope_key_on(ch->slotz[1], egk_norm); + if (channel->chip->newm) { + if (channel->chtype == ch_4op) { + envelope_key_on(channel->slotz[0], egk_norm); + envelope_key_on(channel->slotz[1], egk_norm); + envelope_key_on(channel->pair->slotz[0], egk_norm); + envelope_key_on(channel->pair->slotz[1], egk_norm); + } else if (channel->chtype == ch_2op || channel->chtype == ch_drum) { + envelope_key_on(channel->slotz[0], egk_norm); + envelope_key_on(channel->slotz[1], egk_norm); } } else { - envelope_key_on(ch->slotz[0], egk_norm); - envelope_key_on(ch->slotz[1], egk_norm); + envelope_key_on(channel->slotz[0], egk_norm); + envelope_key_on(channel->slotz[1], egk_norm); } } static void -channel_key_off(chan_t *ch) +channel_key_off(chan_t *channel) { - if (ch->chip->newm) { - if (ch->chtype == ch_4op) { - envelope_key_off(ch->slotz[0], egk_norm); - envelope_key_off(ch->slotz[1], egk_norm); - envelope_key_off(ch->pair->slotz[0], egk_norm); - envelope_key_off(ch->pair->slotz[1], egk_norm); - } else if (ch->chtype == ch_2op || ch->chtype == ch_drum) { - envelope_key_off(ch->slotz[0], egk_norm); - envelope_key_off(ch->slotz[1], egk_norm); + if (channel->chip->newm) { + if (channel->chtype == ch_4op) { + envelope_key_off(channel->slotz[0], egk_norm); + envelope_key_off(channel->slotz[1], egk_norm); + envelope_key_off(channel->pair->slotz[0], egk_norm); + envelope_key_off(channel->pair->slotz[1], egk_norm); + } else if (channel->chtype == ch_2op || channel->chtype == ch_drum) { + envelope_key_off(channel->slotz[0], egk_norm); + envelope_key_off(channel->slotz[1], egk_norm); } } else { - envelope_key_off(ch->slotz[0], egk_norm); - envelope_key_off(ch->slotz[1], egk_norm); + envelope_key_off(channel->slotz[0], egk_norm); + envelope_key_off(channel->slotz[1], egk_norm); } } @@ -1033,14 +1034,14 @@ channel_set_4op(opl3_chip *chip, uint8_t data) chnum += 9 - 3; if ((data >> bit) & 0x01) { - chip->chan[chnum].chtype = ch_4op; - chip->chan[chnum + 3u].chtype = ch_4op2; - channel_update_alg(&chip->chan[chnum]); + chip->channel[chnum].chtype = ch_4op; + chip->channel[chnum + 3u].chtype = ch_4op2; + channel_update_alg(&chip->channel[chnum]); } else { - chip->chan[chnum].chtype = ch_2op; - chip->chan[chnum + 3u].chtype = ch_2op; - channel_update_alg(&chip->chan[chnum]); - channel_update_alg(&chip->chan[chnum + 3u]); + chip->channel[chnum].chtype = ch_2op; + chip->channel[chnum + 3u].chtype = ch_2op; + channel_update_alg(&chip->channel[chnum]); + channel_update_alg(&chip->channel[chnum + 3u]); } } } @@ -1058,7 +1059,7 @@ static inline void nuked_generate_4ch(void *priv, int32_t *buf4) { opl3_chip *chip = (opl3_chip *) priv; - chan_t *ch; + chan_t *channel; wrbuf_t *writebuf; int16_t **out; int32_t mix[2]; @@ -1079,15 +1080,15 @@ nuked_generate_4ch(void *priv, int32_t *buf4) mix[0] = mix[1] = 0; for (i = 0; i < 18; i++) { - ch = &chip->chan[i]; - out = ch->out; - accm = *out[0] + *out[1] + *out[2] + *out[3]; + channel = &chip->channel[i]; + out = channel->out; + accm = *out[0] + *out[1] + *out[2] + *out[3]; #if OPL_ENABLE_STEREOEXT - mix[0] += (int32_t) ((accm * ch->leftpan) >> 16); + mix[0] += (int32_t) ((accm * channel->leftpan) >> 16); #else - mix[0] += (int32_t) (accm & ch->cha); + mix[0] += (int32_t) (accm & channel->cha); #endif - mix[1] += (int32_t) (accm & ch->chc); + mix[1] += (int32_t) (accm & channel->chc); } chip->mixbuff[0] = mix[0]; @@ -1109,15 +1110,15 @@ nuked_generate_4ch(void *priv, int32_t *buf4) mix[0] = mix[1] = 0; for (i = 0; i < 18; i++) { - ch = &chip->chan[i]; - out = ch->out; + channel = &chip->channel[i]; + out = channel->out; accm = *out[0] + *out[1] + *out[2] + *out[3]; #if OPL_ENABLE_STEREOEXT - mix[0] += (int32_t) ((accm * ch->rightpan) >> 16); + mix[0] += (int32_t) ((accm * channel->rightpan) >> 16); #else - mix[0] += (int32_t) (accm & ch->chb); + mix[0] += (int32_t) (accm & channel->chb); #endif - mix[1] += (int32_t) (accm & ch->chd); + mix[1] += (int32_t) (accm & channel->chd); } chip->mixbuff[1] = mix[0]; @@ -1229,9 +1230,9 @@ void nuked_init(opl3_chip *chip, uint32_t samplerate) { opl3_slot *slot; - chan_t *ch; - uint8_t i; - uint8_t local_ch_slot; + chan_t *channel; + uint8_t i; + uint8_t local_ch_slot; memset(chip, 0x00, sizeof(opl3_chip)); @@ -1247,33 +1248,33 @@ nuked_init(opl3_chip *chip, uint32_t samplerate) } for (i = 0; i < 18; i++) { - ch = &chip->chan[i]; - local_ch_slot = ch_slot[i]; - ch->slotz[0] = &chip->slot[local_ch_slot]; - ch->slotz[1] = &chip->slot[local_ch_slot + 3u]; - chip->slot[local_ch_slot].chan = ch; - chip->slot[local_ch_slot + 3u].chan = ch; + channel = &chip->channel[i]; + local_ch_slot = ch_slot[i]; + channel->slotz[0] = &chip->slot[local_ch_slot]; + channel->slotz[1] = &chip->slot[local_ch_slot + 3u]; + chip->slot[local_ch_slot].channel = channel; + chip->slot[local_ch_slot + 3u].channel = channel; if ((i % 9) < 3) - ch->pair = &chip->chan[i + 3u]; + channel->pair = &chip->channel[i + 3u]; else if ((i % 9) < 6) - ch->pair = &chip->chan[i - 3u]; + channel->pair = &chip->channel[i - 3u]; - ch->chip = chip; - ch->out[0] = &chip->zeromod; - ch->out[1] = &chip->zeromod; - ch->out[2] = &chip->zeromod; - ch->out[3] = &chip->zeromod; - ch->chtype = ch_2op; - ch->cha = 0xffff; - ch->chb = 0xffff; + channel->chip = chip; + channel->out[0] = &chip->zeromod; + channel->out[1] = &chip->zeromod; + channel->out[2] = &chip->zeromod; + channel->out[3] = &chip->zeromod; + channel->chtype = ch_2op; + channel->cha = 0xffff; + channel->chb = 0xffff; #if OPL_ENABLE_STEREOEXT - ch->leftpan = 0x10000; - ch->rightpan = 0x10000; + channel->leftpan = 0x10000; + channel->rightpan = 0x10000; #endif - ch->ch_num = i; + channel->ch_num = i; - channel_setup_alg(ch); + channel_setup_alg(channel); } chip->noise = 1; @@ -1374,7 +1375,7 @@ nuked_write_reg(void *priv, uint16_t reg, uint8_t val) case 0xa0: if ((regm & 0x0f) < 9) - channel_write_a0(&chip->chan[9u * high + (regm & 0x0fu)], val); + channel_write_a0(&chip->channel[9u * high + (regm & 0x0fu)], val); break; case 0xb0: @@ -1383,24 +1384,24 @@ nuked_write_reg(void *priv, uint16_t reg, uint8_t val) chip->vibshift = ((val >> 6) & 0x01) ^ 1; channel_update_rhythm(chip, val); } else if ((regm & 0x0f) < 9) { - channel_write_b0(&chip->chan[9u * high + (regm & 0x0fu)], val); + channel_write_b0(&chip->channel[9u * high + (regm & 0x0fu)], val); if (val & 0x20) - channel_key_on(&chip->chan[9u * high + (regm & 0x0fu)]); + channel_key_on(&chip->channel[9u * high + (regm & 0x0fu)]); else - channel_key_off(&chip->chan[9u * high + (regm & 0x0fu)]); + channel_key_off(&chip->channel[9u * high + (regm & 0x0fu)]); } break; case 0xc0: if ((regm & 0x0f) < 9) - channel_write_c0(&chip->chan[9u * high + (regm & 0x0fu)], val); + channel_write_c0(&chip->channel[9u * high + (regm & 0x0fu)], val); break; #if OPL_ENABLE_STEREOEXT case 0xd0: if ((regm & 0x0f) < 9) - channel_write_d0(&chip->chan[9u * high + (regm & 0x0fu)], val); + channel_write_d0(&chip->channel[9u * high + (regm & 0x0fu)], val); break; #endif From e4ee5d2d01d2db8571fca4fb38f77b6ea9fa5978 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Sun, 14 Jul 2024 00:51:26 -0400 Subject: [PATCH 25/28] Update snd_opl_nuked.c --- src/sound/snd_opl_nuked.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index a9a91d962..8125341e2 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -686,7 +686,7 @@ static void slot_calc_fb(opl3_slot *slot) { if (slot->channel->fb != 0x00) - slot->fbmod = (slot->prout + slot->out) >> (0x09 - slot->chan->fb); + slot->fbmod = (slot->prout + slot->out) >> (0x09 - slot->channel->fb); else slot->fbmod = 0; @@ -960,9 +960,9 @@ channel_write_c0(chan_t *channel, uint8_t data) channel->chc = ((data >> 6) & 0x01) ? ~0 : 0; channel->chd = ((data >> 7) & 0x01) ? ~0 : 0; } else { - channel->cha = ch->chb = (uint16_t) ~0; + channel->cha = channel->chb = (uint16_t) ~0; // TODO: Verify on real chip if DAC2 output is disabled in compat mode - channel->chc = ch->chd = 0; + channel->chc = channel->chd = 0; } #if OPL_ENABLE_STEREOEXT From e654d01bd0ff38831381160afa43e52927eb3ce9 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Sun, 14 Jul 2024 01:04:35 -0400 Subject: [PATCH 26/28] Nuked: chan_t -> opl3_channel --- src/include/86box/snd_opl_nuked.h | 4 +-- src/sound/snd_opl_nuked.c | 48 +++++++++++++++---------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/include/86box/snd_opl_nuked.h b/src/include/86box/snd_opl_nuked.h index d0b6924d9..b6e128d09 100644 --- a/src/include/86box/snd_opl_nuked.h +++ b/src/include/86box/snd_opl_nuked.h @@ -83,7 +83,7 @@ typedef struct chan { uint16_t chc; uint16_t chd; uint8_t ch_num; -} chan_t; +} opl3_channel; typedef struct wrbuf { uint64_t time; @@ -92,7 +92,7 @@ typedef struct wrbuf { } wrbuf_t; typedef struct chip { - chan_t channel[18]; + opl3_channel channel[18]; opl3_slot slot[36]; uint16_t timer; uint64_t eg_timer; diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index 8125341e2..f578b460c 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -695,14 +695,14 @@ slot_calc_fb(opl3_slot *slot) // Channel static void -channel_setup_alg(chan_t *channel); +channel_setup_alg(opl3_channel *channel); static void channel_update_rhythm(opl3_chip *chip, uint8_t data) { - chan_t *channel6; - chan_t *channel7; - chan_t *channel8; + opl3_channel *channel6; + opl3_channel *channel7; + opl3_channel *channel8; uint8_t chnum; chip->rhy = data & 0x3f; @@ -774,7 +774,7 @@ channel_update_rhythm(opl3_chip *chip, uint8_t data) } static void -channel_write_a0(chan_t *channel, uint8_t data) +channel_write_a0(opl3_channel *channel, uint8_t data) { if (channel->chip->newm && channel->chtype == ch_4op2) return; @@ -796,7 +796,7 @@ channel_write_a0(chan_t *channel, uint8_t data) } static void -channel_write_b0(chan_t *channel, uint8_t data) +channel_write_b0(opl3_channel *channel, uint8_t data) { if (channel->chip->newm && channel->chtype == ch_4op2) return; @@ -820,7 +820,7 @@ channel_write_b0(chan_t *channel, uint8_t data) } static void -channel_setup_alg(chan_t *channel) +channel_setup_alg(opl3_channel *channel) { if (channel->chtype == ch_drum) { if (channel->ch_num == 7 || channel->ch_num == 8) { @@ -929,7 +929,7 @@ channel_setup_alg(chan_t *channel) } static void -channel_update_alg(chan_t *channel) +channel_update_alg(opl3_channel *channel) { channel->alg = channel->con; @@ -949,7 +949,7 @@ channel_update_alg(chan_t *channel) } static void -channel_write_c0(chan_t *channel, uint8_t data) +channel_write_c0(opl3_channel *channel, uint8_t data) { channel->fb = (data & 0x0e) >> 1; channel->con = data & 0x01; @@ -975,7 +975,7 @@ channel_write_c0(chan_t *channel, uint8_t data) #if OPL_ENABLE_STEREOEXT static void -channel_write_d0(chan_t *channel, uint8_t data) +channel_write_d0(opl3_channel *channel, uint8_t data) { if (channel->chip->stereoext) { channel->leftpan = panpot_lut[data ^ 0xffu]; @@ -985,7 +985,7 @@ channel_write_d0(chan_t *channel, uint8_t data) #endif static void -channel_key_on(chan_t *channel) +channel_key_on(opl3_channel *channel) { if (channel->chip->newm) { if (channel->chtype == ch_4op) { @@ -1004,7 +1004,7 @@ channel_key_on(chan_t *channel) } static void -channel_key_off(chan_t *channel) +channel_key_off(opl3_channel *channel) { if (channel->chip->newm) { if (channel->chtype == ch_4op) { @@ -1058,14 +1058,14 @@ process_slot(opl3_slot *slot) static inline void nuked_generate_4ch(void *priv, int32_t *buf4) { - opl3_chip *chip = (opl3_chip *) priv; - chan_t *channel; - wrbuf_t *writebuf; - int16_t **out; - int32_t mix[2]; - int16_t accm; - int16_t shift = 0; - uint8_t i; + opl3_chip *chip = (opl3_chip *) priv; + opl3_channel *channel; + wrbuf_t *writebuf; + int16_t **out; + int32_t mix[2]; + int16_t accm; + int16_t shift = 0; + uint8_t i; buf4[1] = chip->mixbuff[1]; buf4[3] = chip->mixbuff[3]; @@ -1229,10 +1229,10 @@ nuked_generate_resampled(opl3_chip *chip, int32_t *buf4) void nuked_init(opl3_chip *chip, uint32_t samplerate) { - opl3_slot *slot; - chan_t *channel; - uint8_t i; - uint8_t local_ch_slot; + opl3_slot *slot; + opl3_channel *channel; + uint8_t i; + uint8_t local_ch_slot; memset(chip, 0x00, sizeof(opl3_chip)); From 8056c1bc9afae88ea1399834674474108e9261c7 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Sun, 14 Jul 2024 14:48:43 -0400 Subject: [PATCH 27/28] Nuked: wrbuf -> writebuf --- src/include/86box/snd_opl_nuked.h | 18 +++++------ src/sound/snd_opl_nuked.c | 50 +++++++++++++++---------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/include/86box/snd_opl_nuked.h b/src/include/86box/snd_opl_nuked.h index b6e128d09..508affde2 100644 --- a/src/include/86box/snd_opl_nuked.h +++ b/src/include/86box/snd_opl_nuked.h @@ -24,8 +24,8 @@ #define OPL_ENABLE_STEREOEXT 0 #endif -#define WRBUF_SIZE 1024 -#define WRBUF_DELAY 1 +#define OPL_WRITEBUF_SIZE 1024 +#define OPL_WRITEBUF_DELAY 2 struct chan; struct chip; @@ -85,11 +85,11 @@ typedef struct chan { uint8_t ch_num; } opl3_channel; -typedef struct wrbuf { +typedef struct _opl3_writebuf { uint64_t time; uint16_t reg; uint8_t data; -} wrbuf_t; +} opl3_writebuf; typedef struct chip { opl3_channel channel[18]; @@ -128,11 +128,11 @@ typedef struct chip { int32_t oldsamples[4]; int32_t samples[4]; - uint64_t wrbuf_samplecnt; - uint32_t wrbuf_cur; - uint32_t wrbuf_last; - uint64_t wrbuf_lasttime; - wrbuf_t wrbuf[WRBUF_SIZE]; + uint64_t writebuf_samplecnt; + uint32_t writebuf_cur; + uint32_t writebuf_last; + uint64_t writebuf_lasttime; + opl3_writebuf writebuf[OPL_WRITEBUF_SIZE]; } opl3_chip; typedef struct { diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index f578b460c..958dc87d5 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -1058,14 +1058,14 @@ process_slot(opl3_slot *slot) static inline void nuked_generate_4ch(void *priv, int32_t *buf4) { - opl3_chip *chip = (opl3_chip *) priv; - opl3_channel *channel; - wrbuf_t *writebuf; - int16_t **out; - int32_t mix[2]; - int16_t accm; - int16_t shift = 0; - uint8_t i; + opl3_chip *chip = (opl3_chip *) priv; + opl3_channel *channel; + opl3_writebuf *writebuf; + int16_t **out; + int32_t mix[2]; + int16_t accm; + int16_t shift = 0; + uint8_t i; buf4[1] = chip->mixbuff[1]; buf4[3] = chip->mixbuff[3]; @@ -1166,7 +1166,7 @@ nuked_generate_4ch(void *priv, int32_t *buf4) chip->eg_state ^= 1; - while (writebuf = &chip->wrbuf[chip->wrbuf_cur], writebuf->time <= chip->wrbuf_samplecnt) { + while (writebuf = &chip->writebuf[chip->writebuf_cur], writebuf->time <= chip->writebuf_samplecnt) { if (!(writebuf->reg & 0x200)) break; @@ -1174,10 +1174,10 @@ nuked_generate_4ch(void *priv, int32_t *buf4) nuked_write_reg(chip, writebuf->reg, writebuf->data); - chip->wrbuf_cur = (chip->wrbuf_cur + 1) % WRBUF_SIZE; + chip->writebuf_cur = (chip->writebuf_cur + 1) % OPL_WRITEBUF_SIZE; } - chip->wrbuf_samplecnt++; + chip->writebuf_samplecnt++; } void @@ -1419,33 +1419,33 @@ nuked_write_reg(void *priv, uint16_t reg, uint8_t val) void nuked_write_reg_buffered(void *priv, uint16_t reg, uint8_t val) { - opl3_chip *chip = (opl3_chip *) priv; - uint64_t time1; - uint64_t time2; - wrbuf_t *writebuf; - uint32_t writebuf_last; + opl3_chip *chip = (opl3_chip *) priv; + uint64_t time1; + uint64_t time2; + opl3_writebuf *writebuf; + uint32_t writebuf_last; - writebuf_last = chip->wrbuf_last; - writebuf = &chip->wrbuf[writebuf_last]; + writebuf_last = chip->writebuf_last; + writebuf = &chip->writebuf[writebuf_last]; if (writebuf->reg & 0x0200) { nuked_write_reg(chip, writebuf->reg & 0x01ff, writebuf->data); - chip->wrbuf_cur = (writebuf_last + 1) % WRBUF_SIZE; - chip->wrbuf_samplecnt = writebuf->time; + chip->writebuf_cur = (writebuf_last + 1) % OPL_WRITEBUF_SIZE; + chip->writebuf_samplecnt = writebuf->time; } writebuf->reg = reg | 0x0200; writebuf->data = val; - time1 = chip->wrbuf_lasttime + WRBUF_DELAY; - time2 = chip->wrbuf_samplecnt; + time1 = chip->writebuf_lasttime + OPL_WRITEBUF_DELAY; + time2 = chip->writebuf_samplecnt; if (time1 < time2) time1 = time2; - writebuf->time = time1; - chip->wrbuf_lasttime = time1; - chip->wrbuf_last = (writebuf_last + 1) % WRBUF_SIZE; + writebuf->time = time1; + chip->writebuf_lasttime = time1; + chip->writebuf_last = (writebuf_last + 1) % OPL_WRITEBUF_SIZE; } void From b5c7e59ab7e0b90c65c5405704be6f7d6a4e0448 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Sun, 14 Jul 2024 15:16:17 -0400 Subject: [PATCH 28/28] Nuked: Last of the update --- src/include/86box/snd_opl_nuked.h | 150 ++++++----- src/sound/snd_opl_nuked.c | 413 +++++++++++++++--------------- 2 files changed, 287 insertions(+), 276 deletions(-) diff --git a/src/include/86box/snd_opl_nuked.h b/src/include/86box/snd_opl_nuked.h index 508affde2..0b203fe31 100644 --- a/src/include/86box/snd_opl_nuked.h +++ b/src/include/86box/snd_opl_nuked.h @@ -20,6 +20,12 @@ #ifndef SOUND_OPL_NUKED_H #define SOUND_OPL_NUKED_H +#ifdef __cplusplus +extern "C" { +#endif + +#include + #ifndef OPL_ENABLE_STEREOEXT #define OPL_ENABLE_STEREOEXT 0 #endif @@ -27,63 +33,66 @@ #define OPL_WRITEBUF_SIZE 1024 #define OPL_WRITEBUF_DELAY 2 -struct chan; -struct chip; +typedef struct _opl3_slot opl3_slot; +typedef struct _opl3_channel opl3_channel; +typedef struct _opl3_chip opl3_chip; -typedef struct slot { - struct chan *channel; - struct chip *chip; - int16_t out; - int16_t fbmod; - int16_t *mod; - int16_t prout; - int16_t eg_rout; - int16_t eg_out; - uint8_t eg_inc; - uint8_t eg_gen; - uint8_t eg_rate; - uint8_t eg_ksl; - uint8_t *trem; - uint8_t reg_vib; - uint8_t reg_type; - uint8_t reg_ksr; - uint8_t reg_mult; - uint8_t reg_ksl; - uint8_t reg_tl; - uint8_t reg_ar; - uint8_t reg_dr; - uint8_t reg_sl; - uint8_t reg_rr; - uint8_t reg_wf; - uint8_t key; - uint32_t pg_reset; - uint32_t pg_phase; - uint16_t pg_phase_out; - uint8_t slot_num; -} opl3_slot; +struct _opl3_slot { + opl3_channel *channel; + opl3_chip *chip; + int16_t out; + int16_t fbmod; + int16_t *mod; + int16_t prout; + uint16_t eg_rout; + uint16_t eg_out; + uint8_t eg_inc; + uint8_t eg_gen; + uint8_t eg_rate; + uint8_t eg_ksl; + uint8_t *trem; + uint8_t reg_vib; + uint8_t reg_type; + uint8_t reg_ksr; + uint8_t reg_mult; + uint8_t reg_ksl; + uint8_t reg_tl; + uint8_t reg_ar; + uint8_t reg_dr; + uint8_t reg_sl; + uint8_t reg_rr; + uint8_t reg_wf; + uint8_t key; + uint32_t pg_reset; + uint32_t pg_phase; + uint16_t pg_phase_out; + uint8_t slot_num; +}; + +struct _opl3_channel { + opl3_slot *slotz[2]; // Don't use "slots" keyword to avoid conflict with Qt applications + opl3_channel *pair; + opl3_chip *chip; + int16_t *out[4]; -typedef struct chan { - opl3_slot *slotz[2]; // Don't use "slots" keyword to avoid conflict with Qt applications - struct chan *pair; - struct chip *chip; - int16_t *out[4]; #if OPL_ENABLE_STEREOEXT - int32_t leftpan; - int32_t rightpan; + int32_t leftpan; + int32_t rightpan; #endif - uint8_t chtype; - uint16_t f_num; - uint8_t block; - uint8_t fb; - uint8_t con; - uint8_t alg; - uint8_t ksv; - uint16_t cha; - uint16_t chb; - uint16_t chc; - uint16_t chd; - uint8_t ch_num; -} opl3_channel; + + uint8_t chtype; + uint16_t f_num; + uint8_t block; + uint8_t fb; + uint8_t con; + uint8_t alg; + uint8_t ksv; + uint16_t cha; + uint16_t chb; + uint16_t chc; + uint16_t chd; + uint8_t ch_num; +}; typedef struct _opl3_writebuf { uint64_t time; @@ -91,7 +100,7 @@ typedef struct _opl3_writebuf { uint8_t data; } opl3_writebuf; -typedef struct chip { +struct _opl3_chip { opl3_channel channel[18]; opl3_slot slot[36]; uint16_t timer; @@ -99,7 +108,7 @@ typedef struct chip { uint8_t eg_timerrem; uint8_t eg_state; uint8_t eg_add; - uint64_t eg_timer_lo; + uint8_t eg_timer_lo; uint8_t newm; uint8_t nts; uint8_t rhy; @@ -119,26 +128,26 @@ typedef struct chip { uint8_t rm_tc_bit5; #if OPL_ENABLE_STEREOEXT - uint8_t stereoext; + uint8_t stereoext; #endif // OPL3L - int32_t rateratio; - int32_t samplecnt; - int32_t oldsamples[4]; - int32_t samples[4]; + int32_t rateratio; + int32_t samplecnt; + int32_t oldsamples[4]; + int32_t samples[4]; uint64_t writebuf_samplecnt; uint32_t writebuf_cur; uint32_t writebuf_last; uint64_t writebuf_lasttime; opl3_writebuf writebuf[OPL_WRITEBUF_SIZE]; -} opl3_chip; +}; typedef struct { opl3_chip opl; - int8_t flags; - int8_t pad; + int8_t flags; + int8_t pad; uint16_t port; uint8_t status; @@ -173,6 +182,19 @@ enum { CTRL_TMR1_START = 0x01 }; -void nuked_write_reg(void *priv, uint16_t reg, uint8_t val); +void OPL3_Generate(opl3_chip *chip, int32_t *buf); +void OPL3_GenerateResampled(opl3_chip *chip, int32_t *buf); +void OPL3_Reset(opl3_chip *chip, uint32_t samplerate); +void OPL3_WriteReg(void *priv, uint16_t reg, uint8_t val); +void OPL3_WriteRegBuffered(void *priv, uint16_t reg, uint8_t val); +void OPL3_GenerateStream(opl3_chip *chip, int32_t *sndptr, uint32_t numsamples); + +static void OPL3_Generate4Ch(void *priv, int32_t *buf4); +void OPL3_Generate4Ch_Resampled(opl3_chip *chip, int32_t *buf4); +void OPL3_Generate4Ch_Stream(opl3_chip *chip, int32_t *sndptr1, int32_t *sndptr2, uint32_t numsamples); + +#ifdef __cplusplus +} +#endif #endif /*SOUND_OPL_NUKED_H*/ diff --git a/src/sound/snd_opl_nuked.c b/src/sound/snd_opl_nuked.c index 958dc87d5..f1132867b 100644 --- a/src/sound/snd_opl_nuked.c +++ b/src/sound/snd_opl_nuked.c @@ -85,13 +85,6 @@ enum { egk_drum = 0x02 }; -enum envelope_gen_num { - envelope_gen_num_attack = 0, - envelope_gen_num_decay = 1, - envelope_gen_num_sustain = 2, - envelope_gen_num_release = 3 -}; - #ifdef ENABLE_OPL_LOG int nuked_do_log = ENABLE_OPL_LOG; @@ -230,7 +223,7 @@ typedef int16_t (*envelope_sinfunc)(uint16_t phase, uint16_t envelope); typedef void (*envelope_genfunc)(opl3_slot *slot); static int16_t -envelope_calc_exp(uint32_t level) +OPL3_EnvelopeCalcExp(uint32_t level) { if (level > 0x1fff) level = 0x1fff; @@ -239,7 +232,7 @@ envelope_calc_exp(uint32_t level) } static int16_t -envelope_calc_sin0(uint16_t phase, uint16_t envelope) +OPL3_EnvelopeCalcSin0(uint16_t phase, uint16_t envelope) { uint16_t out = 0; uint16_t neg = 0; @@ -254,11 +247,11 @@ envelope_calc_sin0(uint16_t phase, uint16_t envelope) else out = logsinrom[phase & 0xffu]; - return (envelope_calc_exp(out + (envelope << 3)) ^ neg); + return (OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg); } static int16_t -envelope_calc_sin1(uint16_t phase, uint16_t envelope) +OPL3_EnvelopeCalcSin1(uint16_t phase, uint16_t envelope) { uint16_t out = 0; @@ -271,11 +264,11 @@ envelope_calc_sin1(uint16_t phase, uint16_t envelope) else out = logsinrom[phase & 0xffu]; - return (envelope_calc_exp(out + (envelope << 3))); + return (OPL3_EnvelopeCalcExp(out + (envelope << 3))); } static int16_t -envelope_calc_sin2(uint16_t phase, uint16_t envelope) +OPL3_EnvelopeCalcSin2(uint16_t phase, uint16_t envelope) { uint16_t out = 0; @@ -286,11 +279,11 @@ envelope_calc_sin2(uint16_t phase, uint16_t envelope) else out = logsinrom[phase & 0xffu]; - return (envelope_calc_exp(out + (envelope << 3))); + return (OPL3_EnvelopeCalcExp(out + (envelope << 3))); } static int16_t -envelope_calc_sin3(uint16_t phase, uint16_t envelope) +OPL3_EnvelopeCalcSin3(uint16_t phase, uint16_t envelope) { uint16_t out = 0; @@ -301,11 +294,11 @@ envelope_calc_sin3(uint16_t phase, uint16_t envelope) else out = logsinrom[phase & 0xffu]; - return (envelope_calc_exp(out + (envelope << 3))); + return (OPL3_EnvelopeCalcExp(out + (envelope << 3))); } static int16_t -envelope_calc_sin4(uint16_t phase, uint16_t envelope) +OPL3_EnvelopeCalcSin4(uint16_t phase, uint16_t envelope) { uint16_t out = 0; uint16_t neg = 0; @@ -322,11 +315,11 @@ envelope_calc_sin4(uint16_t phase, uint16_t envelope) else out = logsinrom[(phase << 1u) & 0xffu]; - return (envelope_calc_exp(out + (envelope << 3)) ^ neg); + return (OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg); } static int16_t -envelope_calc_sin5(uint16_t phase, uint16_t envelope) +OPL3_EnvelopeCalcSin5(uint16_t phase, uint16_t envelope) { uint16_t out = 0; @@ -339,11 +332,11 @@ envelope_calc_sin5(uint16_t phase, uint16_t envelope) else out = logsinrom[(phase << 1u) & 0xffu]; - return (envelope_calc_exp(out + (envelope << 3))); + return (OPL3_EnvelopeCalcExp(out + (envelope << 3))); } static int16_t -envelope_calc_sin6(uint16_t phase, uint16_t envelope) +OPL3_EnvelopeCalcSin6(uint16_t phase, uint16_t envelope) { uint16_t neg = 0; @@ -352,11 +345,11 @@ envelope_calc_sin6(uint16_t phase, uint16_t envelope) if (phase & 0x0200) neg = 0xffff; - return (envelope_calc_exp(envelope << 3) ^ neg); + return (OPL3_EnvelopeCalcExp(envelope << 3) ^ neg); } static int16_t -envelope_calc_sin7(uint16_t phase, uint16_t envelope) +OPL3_EnvelopeCalcSin7(uint16_t phase, uint16_t envelope) { uint16_t out = 0; uint16_t neg = 0; @@ -370,22 +363,29 @@ envelope_calc_sin7(uint16_t phase, uint16_t envelope) out = phase << 3; - return (envelope_calc_exp(out + (envelope << 3)) ^ neg); + return (OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg); } static const envelope_sinfunc envelope_sin[8] = { - envelope_calc_sin0, - envelope_calc_sin1, - envelope_calc_sin2, - envelope_calc_sin3, - envelope_calc_sin4, - envelope_calc_sin5, - envelope_calc_sin6, - envelope_calc_sin7 + OPL3_EnvelopeCalcSin0, + OPL3_EnvelopeCalcSin1, + OPL3_EnvelopeCalcSin2, + OPL3_EnvelopeCalcSin3, + OPL3_EnvelopeCalcSin4, + OPL3_EnvelopeCalcSin5, + OPL3_EnvelopeCalcSin6, + OPL3_EnvelopeCalcSin7 +}; + +enum envelope_gen_num { + envelope_gen_num_attack = 0, + envelope_gen_num_decay = 1, + envelope_gen_num_sustain = 2, + envelope_gen_num_release = 3 }; static void -envelope_update_ksl(opl3_slot *slot) +OPL3_EnvelopeUpdateKSL(opl3_slot *slot) { int16_t ksl = (kslrom[slot->channel->f_num >> 6u] << 2) - ((0x08 - slot->channel->block) << 5); @@ -397,7 +397,7 @@ envelope_update_ksl(opl3_slot *slot) } static void -envelope_calc(opl3_slot *slot) +OPL3_EnvelopeCalc(opl3_slot *slot) { uint8_t nonzero; uint8_t rate; @@ -531,33 +531,35 @@ envelope_calc(opl3_slot *slot) } static void -envelope_key_on(opl3_slot *slot, uint8_t type) +OPL3_EnvelopeKeyOn(opl3_slot *slot, uint8_t type) { slot->key |= type; } static void -envelope_key_off(opl3_slot *slot, uint8_t type) +OPL3_EnvelopeKeyOff(opl3_slot *slot, uint8_t type) { slot->key &= ~type; } +// Phase Generator static void -phase_generate(opl3_slot *slot) +OPL3_PhaseGenerate(opl3_slot *slot) { - uint16_t f_num; - uint32_t basefreq; - uint8_t rm_xor; - uint8_t n_bit; - uint32_t noise; - uint16_t phase; - int8_t range; - uint8_t vibpos; opl3_chip *chip; + uint16_t f_num; + uint32_t basefreq; + uint8_t rm_xor; + uint8_t n_bit; + uint32_t noise; + uint16_t phase; chip = slot->chip; f_num = slot->channel->f_num; if (slot->reg_vib) { + int8_t range; + uint8_t vibpos; + range = (f_num >> 7) & 7; vibpos = chip->vibpos; @@ -625,8 +627,9 @@ phase_generate(opl3_slot *slot) chip->noise = (noise >> 1) | (n_bit << 22); } +// Slot static void -slot_write_20(opl3_slot *slot, uint8_t data) +OPL3_SlotWrite20(opl3_slot *slot, uint8_t data) { if ((data >> 7) & 0x01) slot->trem = &slot->chip->tremolo; @@ -640,23 +643,23 @@ slot_write_20(opl3_slot *slot, uint8_t data) } static void -slot_write_40(opl3_slot *slot, uint8_t data) +OPL3_SlotWrite40(opl3_slot *slot, uint8_t data) { slot->reg_ksl = (data >> 6) & 0x03; slot->reg_tl = data & 0x3f; - envelope_update_ksl(slot); + OPL3_EnvelopeUpdateKSL(slot); } static void -slot_write_60(opl3_slot *slot, uint8_t data) +OPL3_SlotWrite60(opl3_slot *slot, uint8_t data) { slot->reg_ar = (data >> 4) & 0x0f; slot->reg_dr = data & 0x0f; } static void -slot_write_80(opl3_slot *slot, uint8_t data) +OPL3_SlotWrite80(opl3_slot *slot, uint8_t data) { slot->reg_sl = (data >> 4) & 0x0f; @@ -667,7 +670,7 @@ slot_write_80(opl3_slot *slot, uint8_t data) } static void -slot_write_e0(opl3_slot *slot, uint8_t data) +OPL3_SlotWriteE0(opl3_slot *slot, uint8_t data) { slot->reg_wf = data & 0x07; @@ -676,14 +679,13 @@ slot_write_e0(opl3_slot *slot, uint8_t data) } static void -slot_generate(opl3_slot *slot) +OPL3_SlotGenerate(opl3_slot *slot) { - slot->out = envelope_sin[slot->reg_wf](slot->pg_phase_out + *slot->mod, - slot->eg_out); + slot->out = envelope_sin[slot->reg_wf](slot->pg_phase_out + *slot->mod, slot->eg_out); } static void -slot_calc_fb(opl3_slot *slot) +OPL3_SlotCalcFB(opl3_slot *slot) { if (slot->channel->fb != 0x00) slot->fbmod = (slot->prout + slot->out) >> (0x09 - slot->channel->fb); @@ -695,10 +697,10 @@ slot_calc_fb(opl3_slot *slot) // Channel static void -channel_setup_alg(opl3_channel *channel); +OPL3_ChannelSetupAlg(opl3_channel *channel); static void -channel_update_rhythm(opl3_chip *chip, uint8_t data) +OPL3_ChannelUpdateRhythm(opl3_chip *chip, uint8_t data) { opl3_channel *channel6; opl3_channel *channel7; @@ -726,55 +728,55 @@ channel_update_rhythm(opl3_chip *chip, uint8_t data) for (chnum = 6; chnum < 9; chnum++) chip->channel[chnum].chtype = ch_drum; - channel_setup_alg(channel6); - channel_setup_alg(channel7); - channel_setup_alg(channel8); + OPL3_ChannelSetupAlg(channel6); + OPL3_ChannelSetupAlg(channel7); + OPL3_ChannelSetupAlg(channel8); // hh if (chip->rhy & 0x01) - envelope_key_on(channel7->slotz[0], egk_drum); + OPL3_EnvelopeKeyOn(channel7->slotz[0], egk_drum); else - envelope_key_off(channel7->slotz[0], egk_drum); + OPL3_EnvelopeKeyOff(channel7->slotz[0], egk_drum); // tc if (chip->rhy & 0x02) - envelope_key_on(channel8->slotz[1], egk_drum); + OPL3_EnvelopeKeyOn(channel8->slotz[1], egk_drum); else - envelope_key_off(channel8->slotz[1], egk_drum); + OPL3_EnvelopeKeyOff(channel8->slotz[1], egk_drum); // tom if (chip->rhy & 0x04) - envelope_key_on(channel8->slotz[0], egk_drum); + OPL3_EnvelopeKeyOn(channel8->slotz[0], egk_drum); else - envelope_key_off(channel8->slotz[0], egk_drum); + OPL3_EnvelopeKeyOff(channel8->slotz[0], egk_drum); // sd if (chip->rhy & 0x08) - envelope_key_on(channel7->slotz[1], egk_drum); + OPL3_EnvelopeKeyOn(channel7->slotz[1], egk_drum); else - envelope_key_off(channel7->slotz[1], egk_drum); + OPL3_EnvelopeKeyOff(channel7->slotz[1], egk_drum); // bd if (chip->rhy & 0x10) { - envelope_key_on(channel6->slotz[0], egk_drum); - envelope_key_on(channel6->slotz[1], egk_drum); + OPL3_EnvelopeKeyOn(channel6->slotz[0], egk_drum); + OPL3_EnvelopeKeyOn(channel6->slotz[1], egk_drum); } else { - envelope_key_off(channel6->slotz[0], egk_drum); - envelope_key_off(channel6->slotz[1], egk_drum); + OPL3_EnvelopeKeyOff(channel6->slotz[0], egk_drum); + OPL3_EnvelopeKeyOff(channel6->slotz[1], egk_drum); } } else { for (chnum = 6; chnum < 9; chnum++) { chip->channel[chnum].chtype = ch_2op; - channel_setup_alg(&chip->channel[chnum]); - envelope_key_off(chip->channel[chnum].slotz[0], egk_drum); - envelope_key_off(chip->channel[chnum].slotz[1], egk_drum); + OPL3_ChannelSetupAlg(&chip->channel[chnum]); + OPL3_EnvelopeKeyOff(chip->channel[chnum].slotz[0], egk_drum); + OPL3_EnvelopeKeyOff(chip->channel[chnum].slotz[1], egk_drum); } } } static void -channel_write_a0(opl3_channel *channel, uint8_t data) +OPL3_ChannelWriteA0(opl3_channel *channel, uint8_t data) { if (channel->chip->newm && channel->chtype == ch_4op2) return; @@ -783,20 +785,20 @@ channel_write_a0(opl3_channel *channel, uint8_t data) channel->ksv = (channel->block << 1) | ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01); - envelope_update_ksl(channel->slotz[0]); - envelope_update_ksl(channel->slotz[1]); + OPL3_EnvelopeUpdateKSL(channel->slotz[0]); + OPL3_EnvelopeUpdateKSL(channel->slotz[1]); if (channel->chip->newm && channel->chtype == ch_4op) { channel->pair->f_num = channel->f_num; channel->pair->ksv = channel->ksv; - envelope_update_ksl(channel->pair->slotz[0]); - envelope_update_ksl(channel->pair->slotz[1]); + OPL3_EnvelopeUpdateKSL(channel->pair->slotz[0]); + OPL3_EnvelopeUpdateKSL(channel->pair->slotz[1]); } } static void -channel_write_b0(opl3_channel *channel, uint8_t data) +OPL3_ChannelWriteB0(opl3_channel *channel, uint8_t data) { if (channel->chip->newm && channel->chtype == ch_4op2) return; @@ -806,21 +808,21 @@ channel_write_b0(opl3_channel *channel, uint8_t data) channel->ksv = (channel->block << 1) | ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01); - envelope_update_ksl(channel->slotz[0]); - envelope_update_ksl(channel->slotz[1]); + OPL3_EnvelopeUpdateKSL(channel->slotz[0]); + OPL3_EnvelopeUpdateKSL(channel->slotz[1]); if (channel->chip->newm && channel->chtype == ch_4op) { channel->pair->f_num = channel->f_num; channel->pair->block = channel->block; channel->pair->ksv = channel->ksv; - envelope_update_ksl(channel->pair->slotz[0]); - envelope_update_ksl(channel->pair->slotz[1]); + OPL3_EnvelopeUpdateKSL(channel->pair->slotz[0]); + OPL3_EnvelopeUpdateKSL(channel->pair->slotz[1]); } } static void -channel_setup_alg(opl3_channel *channel) +OPL3_ChannelSetupAlg(opl3_channel *channel) { if (channel->chtype == ch_drum) { if (channel->ch_num == 7 || channel->ch_num == 8) { @@ -929,30 +931,31 @@ channel_setup_alg(opl3_channel *channel) } static void -channel_update_alg(opl3_channel *channel) +OPL3_ChannelUpdateAlg(opl3_channel *channel) { channel->alg = channel->con; if (channel->chip->newm) { if (channel->chtype == ch_4op) { - channel->pair->alg = 0x04 | (channel->con << 1) | channel->pair->con; + channel->pair->alg = 0x04 | (channel->con << 1) | (channel->pair->con); channel->alg = 0x08; - channel_setup_alg(channel->pair); + OPL3_ChannelSetupAlg(channel->pair); } else if (channel->chtype == ch_4op2) { - channel->alg = 0x04 | (channel->pair->con << 1) | channel->con; + channel->alg = 0x04 | (channel->pair->con << 1) | (channel->con); channel->pair->alg = 0x08; - channel_setup_alg(channel); + OPL3_ChannelSetupAlg(channel); } else - channel_setup_alg(channel); + OPL3_ChannelSetupAlg(channel); } else - channel_setup_alg(channel); + OPL3_ChannelSetupAlg(channel); } static void -channel_write_c0(opl3_channel *channel, uint8_t data) +OPL3_ChannelWriteC0(opl3_channel *channel, uint8_t data) { channel->fb = (data & 0x0e) >> 1; channel->con = data & 0x01; + OPL3_ChannelUpdateAlg(channel); if (channel->chip->newm) { channel->cha = ((data >> 4) & 0x01) ? ~0 : 0; @@ -975,7 +978,7 @@ channel_write_c0(opl3_channel *channel, uint8_t data) #if OPL_ENABLE_STEREOEXT static void -channel_write_d0(opl3_channel *channel, uint8_t data) +OPL3_ChannelWriteD0(opl3_channel *channel, uint8_t data) { if (channel->chip->stereoext) { channel->leftpan = panpot_lut[data ^ 0xffu]; @@ -985,45 +988,45 @@ channel_write_d0(opl3_channel *channel, uint8_t data) #endif static void -channel_key_on(opl3_channel *channel) +OPL3_ChannelKeyOn(opl3_channel *channel) { if (channel->chip->newm) { if (channel->chtype == ch_4op) { - envelope_key_on(channel->slotz[0], egk_norm); - envelope_key_on(channel->slotz[1], egk_norm); - envelope_key_on(channel->pair->slotz[0], egk_norm); - envelope_key_on(channel->pair->slotz[1], egk_norm); + OPL3_EnvelopeKeyOn(channel->slotz[0], egk_norm); + OPL3_EnvelopeKeyOn(channel->slotz[1], egk_norm); + OPL3_EnvelopeKeyOn(channel->pair->slotz[0], egk_norm); + OPL3_EnvelopeKeyOn(channel->pair->slotz[1], egk_norm); } else if (channel->chtype == ch_2op || channel->chtype == ch_drum) { - envelope_key_on(channel->slotz[0], egk_norm); - envelope_key_on(channel->slotz[1], egk_norm); + OPL3_EnvelopeKeyOn(channel->slotz[0], egk_norm); + OPL3_EnvelopeKeyOn(channel->slotz[1], egk_norm); } } else { - envelope_key_on(channel->slotz[0], egk_norm); - envelope_key_on(channel->slotz[1], egk_norm); + OPL3_EnvelopeKeyOn(channel->slotz[0], egk_norm); + OPL3_EnvelopeKeyOn(channel->slotz[1], egk_norm); } } static void -channel_key_off(opl3_channel *channel) +OPL3_ChannelKeyOff(opl3_channel *channel) { if (channel->chip->newm) { if (channel->chtype == ch_4op) { - envelope_key_off(channel->slotz[0], egk_norm); - envelope_key_off(channel->slotz[1], egk_norm); - envelope_key_off(channel->pair->slotz[0], egk_norm); - envelope_key_off(channel->pair->slotz[1], egk_norm); + OPL3_EnvelopeKeyOff(channel->slotz[0], egk_norm); + OPL3_EnvelopeKeyOff(channel->slotz[1], egk_norm); + OPL3_EnvelopeKeyOff(channel->pair->slotz[0], egk_norm); + OPL3_EnvelopeKeyOff(channel->pair->slotz[1], egk_norm); } else if (channel->chtype == ch_2op || channel->chtype == ch_drum) { - envelope_key_off(channel->slotz[0], egk_norm); - envelope_key_off(channel->slotz[1], egk_norm); + OPL3_EnvelopeKeyOff(channel->slotz[0], egk_norm); + OPL3_EnvelopeKeyOff(channel->slotz[1], egk_norm); } } else { - envelope_key_off(channel->slotz[0], egk_norm); - envelope_key_off(channel->slotz[1], egk_norm); + OPL3_EnvelopeKeyOff(channel->slotz[0], egk_norm); + OPL3_EnvelopeKeyOff(channel->slotz[1], egk_norm); } } static void -channel_set_4op(opl3_chip *chip, uint8_t data) +OPL3_ChannelSet4Op(opl3_chip *chip, uint8_t data) { uint8_t chnum; @@ -1036,36 +1039,36 @@ channel_set_4op(opl3_chip *chip, uint8_t data) if ((data >> bit) & 0x01) { chip->channel[chnum].chtype = ch_4op; chip->channel[chnum + 3u].chtype = ch_4op2; - channel_update_alg(&chip->channel[chnum]); + OPL3_ChannelUpdateAlg(&chip->channel[chnum]); } else { chip->channel[chnum].chtype = ch_2op; chip->channel[chnum + 3u].chtype = ch_2op; - channel_update_alg(&chip->channel[chnum]); - channel_update_alg(&chip->channel[chnum + 3u]); + OPL3_ChannelUpdateAlg(&chip->channel[chnum]); + OPL3_ChannelUpdateAlg(&chip->channel[chnum + 3u]); } } } static void -process_slot(opl3_slot *slot) +OPL3_ProcessSlot(opl3_slot *slot) { - slot_calc_fb(slot); - envelope_calc(slot); - phase_generate(slot); - slot_generate(slot); + OPL3_SlotCalcFB(slot); + OPL3_EnvelopeCalc(slot); + OPL3_PhaseGenerate(slot); + OPL3_SlotGenerate(slot); } static inline void -nuked_generate_4ch(void *priv, int32_t *buf4) +OPL3_Generate4Ch(void *priv, int32_t *buf4) { opl3_chip *chip = (opl3_chip *) priv; opl3_channel *channel; opl3_writebuf *writebuf; int16_t **out; int32_t mix[2]; - int16_t accm; - int16_t shift = 0; uint8_t i; + int16_t accm; + uint8_t shift = 0; buf4[1] = chip->mixbuff[1]; buf4[3] = chip->mixbuff[3]; @@ -1075,7 +1078,7 @@ nuked_generate_4ch(void *priv, int32_t *buf4) #else for (i = 0; i < 36; i++) #endif - process_slot(&chip->slot[i]); + OPL3_ProcessSlot(&chip->slot[i]); mix[0] = mix[1] = 0; @@ -1084,11 +1087,11 @@ nuked_generate_4ch(void *priv, int32_t *buf4) out = channel->out; accm = *out[0] + *out[1] + *out[2] + *out[3]; #if OPL_ENABLE_STEREOEXT - mix[0] += (int32_t) ((accm * channel->leftpan) >> 16); + mix[0] += (int16_t) ((accm * channel->leftpan) >> 16); #else - mix[0] += (int32_t) (accm & channel->cha); + mix[0] += (int16_t) (accm & channel->cha); #endif - mix[1] += (int32_t) (accm & channel->chc); + mix[1] += (int16_t) (accm & channel->chc); } chip->mixbuff[0] = mix[0]; @@ -1096,7 +1099,7 @@ nuked_generate_4ch(void *priv, int32_t *buf4) #if OPL_QUIRK_CHANNELSAMPLEDELAY for (i = 15; i < 18; i++) - process_slot(&chip->slot[i]); + OPL3_ProcessSlot(&chip->slot[i]); #endif buf4[0] = chip->mixbuff[0]; @@ -1104,7 +1107,7 @@ nuked_generate_4ch(void *priv, int32_t *buf4) #if OPL_QUIRK_CHANNELSAMPLEDELAY for (i = 18; i < 33; i++) - process_slot(&chip->slot[i]); + OPL3_ProcessSlot(&chip->slot[i]); #endif mix[0] = mix[1] = 0; @@ -1112,13 +1115,13 @@ nuked_generate_4ch(void *priv, int32_t *buf4) for (i = 0; i < 18; i++) { channel = &chip->channel[i]; out = channel->out; - accm = *out[0] + *out[1] + *out[2] + *out[3]; + accm = *out[0] + *out[1] + *out[2] + *out[3]; #if OPL_ENABLE_STEREOEXT - mix[0] += (int32_t) ((accm * channel->rightpan) >> 16); + mix[0] += (int16_t) ((accm * channel->rightpan) >> 16); #else - mix[0] += (int32_t) (accm & channel->chb); + mix[0] += (int16_t) (accm & channel->chb); #endif - mix[1] += (int32_t) (accm & channel->chd); + mix[1] += (int16_t) (accm & channel->chd); } chip->mixbuff[1] = mix[0]; @@ -1126,7 +1129,7 @@ nuked_generate_4ch(void *priv, int32_t *buf4) #if OPL_QUIRK_CHANNELSAMPLEDELAY for (i = 33; i < 36; i++) - process_slot(&chip->slot[i]); + OPL3_ProcessSlot(&chip->slot[i]); #endif if ((chip->timer & 0x3f) == 0x3f) @@ -1166,13 +1169,13 @@ nuked_generate_4ch(void *priv, int32_t *buf4) chip->eg_state ^= 1; - while (writebuf = &chip->writebuf[chip->writebuf_cur], writebuf->time <= chip->writebuf_samplecnt) { + while ((writebuf = &chip->writebuf[chip->writebuf_cur]), writebuf->time <= chip->writebuf_samplecnt) { if (!(writebuf->reg & 0x200)) break; writebuf->reg &= 0x01ff; - nuked_write_reg(chip, writebuf->reg, writebuf->data); + OPL3_WriteReg(chip, writebuf->reg, writebuf->data); chip->writebuf_cur = (chip->writebuf_cur + 1) % OPL_WRITEBUF_SIZE; } @@ -1181,84 +1184,79 @@ nuked_generate_4ch(void *priv, int32_t *buf4) } void -nuked_generate(opl3_chip *chip, int32_t *buf) +OPL3_Generate(opl3_chip *chip, int32_t *buf) { int32_t samples[4]; - nuked_generate_4ch(chip, samples); + OPL3_Generate4Ch(chip, samples); buf[0] = samples[0]; buf[1] = samples[1]; } void -nuked_generate_4ch_resampled(opl3_chip *chip, int32_t *buf4) +OPL3_Generate4ChResampled(opl3_chip *chip, int32_t *buf4) { while (chip->samplecnt >= chip->rateratio) { chip->oldsamples[0] = chip->samples[0]; chip->oldsamples[1] = chip->samples[1]; chip->oldsamples[2] = chip->samples[2]; chip->oldsamples[3] = chip->samples[3]; - nuked_generate_4ch(chip, chip->samples); + OPL3_Generate4Ch(chip, chip->samples); chip->samplecnt -= chip->rateratio; } buf4[0] = (int32_t) ((chip->oldsamples[0] * (chip->rateratio - chip->samplecnt) - + chip->samples[0] * chip->samplecnt) - / chip->rateratio); + + chip->samples[0] * chip->samplecnt) / chip->rateratio); buf4[1] = (int32_t) ((chip->oldsamples[1] * (chip->rateratio - chip->samplecnt) - + chip->samples[1] * chip->samplecnt) - / chip->rateratio); + + chip->samples[1] * chip->samplecnt) / chip->rateratio); buf4[2] = (int32_t) ((chip->oldsamples[2] * (chip->rateratio - chip->samplecnt) - + chip->samples[2] * chip->samplecnt) - / chip->rateratio); + + chip->samples[2] * chip->samplecnt) / chip->rateratio); buf4[3] = (int32_t) ((chip->oldsamples[3] * (chip->rateratio - chip->samplecnt) - + chip->samples[3] * chip->samplecnt) - / chip->rateratio); + + chip->samples[3] * chip->samplecnt) / chip->rateratio); chip->samplecnt += 1 << RSM_FRAC; } void -nuked_generate_resampled(opl3_chip *chip, int32_t *buf4) +OPL3_GenerateResampled(opl3_chip *chip, int32_t *buf) { int32_t samples[4]; - nuked_generate_4ch_resampled(chip, samples); - buf4[0] = samples[0]; - buf4[1] = samples[1]; + OPL3_Generate4ChResampled(chip, samples); + buf[0] = samples[0]; + buf[1] = samples[1]; } void -nuked_init(opl3_chip *chip, uint32_t samplerate) +OPL3_Reset(opl3_chip *chip, uint32_t samplerate) { opl3_slot *slot; opl3_channel *channel; - uint8_t i; uint8_t local_ch_slot; memset(chip, 0x00, sizeof(opl3_chip)); - for (i = 0; i < 36; i++) { - slot = &chip->slot[i]; - slot->chip = chip; - slot->mod = &chip->zeromod; - slot->eg_rout = 0x01ff; - slot->eg_out = 0x01ff; - slot->eg_gen = envelope_gen_num_release; - slot->trem = (uint8_t *) &chip->zeromod; - slot->slot_num = i; + for (uint8_t slotnum = 0; slotnum < 36; slotnum++) { + slot = &chip->slot[slotnum]; + slot->chip = chip; + slot->mod = &chip->zeromod; + slot->eg_rout = 0x01ff; + slot->eg_out = 0x01ff; + slot->eg_gen = envelope_gen_num_release; + slot->trem = (uint8_t *) &chip->zeromod; + slot->slot_num = slotnum; } - for (i = 0; i < 18; i++) { - channel = &chip->channel[i]; - local_ch_slot = ch_slot[i]; + for (uint8_t channum = 0; channum < 18; channum++) { + channel = &chip->channel[channum]; + local_ch_slot = ch_slot[channum]; channel->slotz[0] = &chip->slot[local_ch_slot]; channel->slotz[1] = &chip->slot[local_ch_slot + 3u]; chip->slot[local_ch_slot].channel = channel; chip->slot[local_ch_slot + 3u].channel = channel; - if ((i % 9) < 3) - channel->pair = &chip->channel[i + 3u]; - else if ((i % 9) < 6) - channel->pair = &chip->channel[i - 3u]; + if ((channum % 9) < 3) + channel->pair = &chip->channel[channum + 3u]; + else if ((channum % 9) < 6) + channel->pair = &chip->channel[channum - 3u]; channel->chip = chip; channel->out[0] = &chip->zeromod; @@ -1272,9 +1270,9 @@ nuked_init(opl3_chip *chip, uint32_t samplerate) channel->leftpan = 0x10000; channel->rightpan = 0x10000; #endif - channel->ch_num = i; + channel->ch_num = channum; - channel_setup_alg(channel); + OPL3_ChannelSetupAlg(channel); } chip->noise = 1; @@ -1291,15 +1289,6 @@ nuked_init(opl3_chip *chip, uint32_t samplerate) #endif } -void -nuked_generate_raw(opl3_chip *chip, int32_t *bufp) -{ - nuked_generate(chip, chip->samples); - - bufp[0] = (int32_t) chip->samples[0]; - bufp[1] = (int32_t) chip->samples[1]; -} - uint16_t nuked_write_addr(void *priv, uint16_t port, uint8_t val) { @@ -1314,7 +1303,7 @@ nuked_write_addr(void *priv, uint16_t port, uint8_t val) } void -nuked_write_reg(void *priv, uint16_t reg, uint8_t val) +OPL3_WriteReg(void *priv, uint16_t reg, uint8_t val) { opl3_chip *chip = (opl3_chip *) priv; uint8_t high = (reg >> 8) & 0x01; @@ -1325,7 +1314,7 @@ nuked_write_reg(void *priv, uint16_t reg, uint8_t val) if (high) switch (regm & 0x0f) { case 0x04: - channel_set_4op(chip, val); + OPL3_ChannelSet4Op(chip, val); break; case 0x05: @@ -1352,72 +1341,72 @@ nuked_write_reg(void *priv, uint16_t reg, uint8_t val) case 0x20: case 0x30: if (ad_slot[regm & 0x1fu] >= 0) - slot_write_20(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); + OPL3_SlotWrite20(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); break; case 0x40: case 0x50: if (ad_slot[regm & 0x1fu] >= 0) - slot_write_40(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); + OPL3_SlotWrite40(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); break; case 0x60: case 0x70: if (ad_slot[regm & 0x1fu] >= 0) - slot_write_60(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); + OPL3_SlotWrite60(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); break; case 0x80: case 0x90: if (ad_slot[regm & 0x1fu] >= 0) - slot_write_80(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); + OPL3_SlotWrite80(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); + break; + + case 0xe0: + case 0xf0: + if (ad_slot[regm & 0x1fu] >= 0) + OPL3_SlotWriteE0(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); break; case 0xa0: if ((regm & 0x0f) < 9) - channel_write_a0(&chip->channel[9u * high + (regm & 0x0fu)], val); + OPL3_ChannelWriteA0(&chip->channel[9u * high + (regm & 0x0fu)], val); break; case 0xb0: if (regm == 0xbd && !high) { chip->tremoloshift = (((val >> 7) ^ 1) << 1) + 2; chip->vibshift = ((val >> 6) & 0x01) ^ 1; - channel_update_rhythm(chip, val); + OPL3_ChannelUpdateRhythm(chip, val); } else if ((regm & 0x0f) < 9) { - channel_write_b0(&chip->channel[9u * high + (regm & 0x0fu)], val); + OPL3_ChannelWriteB0(&chip->channel[9u * high + (regm & 0x0fu)], val); if (val & 0x20) - channel_key_on(&chip->channel[9u * high + (regm & 0x0fu)]); + OPL3_ChannelKeyOn(&chip->channel[9u * high + (regm & 0x0fu)]); else - channel_key_off(&chip->channel[9u * high + (regm & 0x0fu)]); + OPL3_ChannelKeyOff(&chip->channel[9u * high + (regm & 0x0fu)]); } break; case 0xc0: if ((regm & 0x0f) < 9) - channel_write_c0(&chip->channel[9u * high + (regm & 0x0fu)], val); + OPL3_ChannelWriteC0(&chip->channel[9u * high + (regm & 0x0fu)], val); break; #if OPL_ENABLE_STEREOEXT case 0xd0: if ((regm & 0x0f) < 9) - channel_write_d0(&chip->channel[9u * high + (regm & 0x0fu)], val); + OPL3_ChannelWriteD0(&chip->channel[9u * high + (regm & 0x0fu)], val); break; #endif - case 0xe0: - case 0xf0: - if (ad_slot[regm & 0x1fu] >= 0) - slot_write_e0(&chip->slot[18u * high + ad_slot[regm & 0x1fu]], val); - break; - default: break; } } void -nuked_write_reg_buffered(void *priv, uint16_t reg, uint8_t val) +OPL3_WriteRegBuffered(void *priv, uint16_t reg, uint8_t val) { opl3_chip *chip = (opl3_chip *) priv; uint64_t time1; @@ -1429,7 +1418,7 @@ nuked_write_reg_buffered(void *priv, uint16_t reg, uint8_t val) writebuf = &chip->writebuf[writebuf_last]; if (writebuf->reg & 0x0200) { - nuked_write_reg(chip, writebuf->reg & 0x01ff, writebuf->data); + OPL3_WriteReg(chip, writebuf->reg & 0x01ff, writebuf->data); chip->writebuf_cur = (writebuf_last + 1) % OPL_WRITEBUF_SIZE; chip->writebuf_samplecnt = writebuf->time; @@ -1449,12 +1438,12 @@ nuked_write_reg_buffered(void *priv, uint16_t reg, uint8_t val) } void -nuked_generate_4ch_stream(opl3_chip *chip, int16_t *sndptr1, int16_t *sndptr2, uint32_t numsamples) +OPL3_Generate4ChStream(opl3_chip *chip, int32_t *sndptr1, int32_t *sndptr2, uint32_t numsamples) { int32_t samples[4]; for (uint_fast32_t i = 0; i < numsamples; i++) { - nuked_generate_4ch_resampled(chip, samples); + OPL3_Generate4Ch(chip, samples); sndptr1[0] = samples[0]; sndptr1[1] = samples[1]; sndptr2[0] = samples[2]; @@ -1465,10 +1454,10 @@ nuked_generate_4ch_stream(opl3_chip *chip, int16_t *sndptr1, int16_t *sndptr2, u } void -nuked_generate_stream(opl3_chip *chip, int32_t *sndptr, uint32_t num) +OPL3_GenerateStream(opl3_chip *chip, int32_t *sndptr, uint32_t numsamples) { - for (uint_fast32_t i = 0; i < num; i++) { - nuked_generate_resampled(chip, sndptr); + for (uint_fast32_t i = 0; i < numsamples; i++) { + OPL3_Generate(chip, sndptr); sndptr += 2; } } @@ -1549,7 +1538,7 @@ nuked_drv_init(const device_t *info) dev->status = 0x06; /* Initialize the NukedOPL object. */ - nuked_init(&dev->opl, OPL_FREQ); + OPL3_Reset(&dev->opl, OPL_FREQ); timer_add(&dev->timers[0], nuked_timer_1, dev, 0); timer_add(&dev->timers[1], nuked_timer_2, dev, 0); @@ -1572,7 +1561,7 @@ nuked_drv_update(void *priv) if (dev->pos >= music_pos_global) return dev->buffer; - nuked_generate_stream(&dev->opl, + OPL3_GenerateStream(&dev->opl, &dev->buffer[dev->pos * 2], music_pos_global - dev->pos); @@ -1614,7 +1603,7 @@ nuked_drv_write(uint16_t port, uint8_t val, void *priv) nuked_drv_update(dev); if ((port & 0x0001) == 0x0001) { - nuked_write_reg_buffered(&dev->opl, dev->port, val); + OPL3_WriteRegBuffered(&dev->opl, dev->port, val); switch (dev->port) { case 0x002: /* Timer 1 */