aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt29
-rw-r--r--README.md10
-rw-r--r--include/adlmidi.h17
-rw-r--r--libADLMIDI-test.pro18
-rw-r--r--src/adlmidi.cpp71
-rw-r--r--src/adlmidi_load.cpp4
-rw-r--r--src/adlmidi_midiplay.cpp4
-rw-r--r--src/adlmidi_opl3.cpp91
-rw-r--r--src/adlmidi_private.hpp17
-rw-r--r--src/chips/dosbox/dbopl.cpp (renamed from src/dbopl.cpp)4
-rw-r--r--src/chips/dosbox/dbopl.h (renamed from src/dbopl.h)0
-rw-r--r--src/chips/dosbox_opl3.cpp84
-rw-r--r--src/chips/dosbox_opl3.h23
-rw-r--r--src/chips/nuked/nukedopl3.c1391
-rw-r--r--src/chips/nuked/nukedopl3.h160
-rw-r--r--src/chips/nuked/nukedopl3_174.c1391
-rw-r--r--src/chips/nuked/nukedopl3_174.h154
-rw-r--r--src/chips/nuked/old/nukedopl3.c (renamed from src/nukedopl3.c)0
-rw-r--r--src/chips/nuked/old/nukedopl3.h (renamed from src/nukedopl3.h)0
-rw-r--r--src/chips/nuked_opl3.cpp67
-rw-r--r--src/chips/nuked_opl3.h23
-rw-r--r--src/chips/nuked_opl3_v174.cpp67
-rw-r--r--src/chips/nuked_opl3_v174.h23
-rw-r--r--src/chips/opl_chip_base.cpp22
-rw-r--r--src/chips/opl_chip_base.h25
-rw-r--r--utils/midiplay/adlmidiplay.cpp28
26 files changed, 3618 insertions, 105 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 89194ed..2228353 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -61,7 +61,8 @@ option(WITH_OLD_UTILS "Build also old utilities" OFF)
option(WITH_EMBEDDED_BANKS "Use embedded banks" ON)
option(WITH_GENADLDATA "Build and run full rebuild of embedded banks cache" OFF)
option(WITH_GENADLDATA_COMMENTS "Enable comments in generated ADLDATA cache file" OFF)
-option(USE_DOSBOX_EMULATOR "Use DosBox emulator" OFF)
+option(USE_DOSBOX_EMULATOR "Use DosBox 0.74 OPL3 emulator (semi-accurate, suggested for slow or mobile platforms)" ON)
+option(USE_NUKED_EMULATOR "Use Nuked OPL3 emulator (most accurate, powerful)" ON)
option(WITH_CPP_EXTRAS "Build with support for C++ extras (features are can be found in 'adlmidi.hpp' header)" OFF)
option(WITH_MIDI_SEQUENCER "Build with embedded MIDI sequencer. Disable this if you want use library in real-time MIDI drivers or plugins.)" ON)
option(WITH_MUS_SUPPORT "Build with support for DMX MUS files)" ON)
@@ -162,15 +163,34 @@ else()
endif()
if(NOT DJGPP AND NOT MSDOS)
+ list(APPEND libADLMIDI_SOURCES
+ ${libADLMIDI_SOURCE_DIR}/src/chips/opl_chip_base.cpp
+ )
+
if(USE_DOSBOX_EMULATOR)
- add_definitions(-DADLMIDI_USE_DOSBOX_OPL)
+ set(HAS_EMULATOR TRUE)
list(APPEND libADLMIDI_SOURCES
- ${libADLMIDI_SOURCE_DIR}/src/dbopl.cpp
+ ${libADLMIDI_SOURCE_DIR}/src/chips/dosbox_opl3.cpp
+ ${libADLMIDI_SOURCE_DIR}/src/chips/dosbox/dbopl.cpp
)
else()
+ add_definitions(-DADLMIDI_DISABLE_DOSBOX_EMULATOR)
+ endif()
+
+ if(USE_NUKED_EMULATOR)
+ set(HAS_EMULATOR TRUE)
list(APPEND libADLMIDI_SOURCES
- ${libADLMIDI_SOURCE_DIR}/src/nukedopl3.c
+ ${libADLMIDI_SOURCE_DIR}/src/chips/nuked_opl3.cpp # v 1.8
+ ${libADLMIDI_SOURCE_DIR}/src/chips/nuked/nukedopl3.c
+ ${libADLMIDI_SOURCE_DIR}/src/chips/nuked_opl3_v174.cpp # v 1.7.4
+ ${libADLMIDI_SOURCE_DIR}/src/chips/nuked/nukedopl3_174.c
)
+ else()
+ add_definitions(-DADLMIDI_DISABLE_NUKED_EMULATOR)
+ endif()
+
+ if(NOT HAS_EMULATOR)
+ message(FATAL_ERROR "No emulators enabled! You must enable at least one emulator!")
endif()
endif()
@@ -434,6 +454,7 @@ message("WITH_EMBEDDED_BANKS = ${WITH_EMBEDDED_BANKS}")
message("WITH_GENADLDATA = ${WITH_GENADLDATA}")
message("WITH_GENADLDATA_COMMENTS = ${WITH_GENADLDATA_COMMENTS}")
message("USE_DOSBOX_EMULATOR = ${USE_DOSBOX_EMULATOR}")
+message("USE_NUKED_EMULATOR = ${USE_NUKED_EMULATOR}")
message("WITH_MIDI_SEQUENCER = ${WITH_MIDI_SEQUENCER}")
message("WITH_CPP_EXTRAS = ${WITH_CPP_EXTRAS}")
message("WITH_MUS_SUPPORT = ${WITH_MUS_SUPPORT}")
diff --git a/README.md b/README.md
index e56b475..16b1b83 100644
--- a/README.md
+++ b/README.md
@@ -78,7 +78,8 @@ You need to make in the any IDE a library project and put into it next files
* `ADLMIDI_DISABLE_XMI_SUPPORT` - Disables XMI to MIDI converter
* `ADLMIDI_DISABLE_MUS_SUPPORT` - Disables MUS to MIDI converter
* `ADLMIDI_DISABLE_MIDI_SEQUENCER` - Completely disables built-in MIDI sequencer.
-* `ADLMIDI_USE_DOSBOX_OPL` - Enables DosBox 0.74 OPL3 emulator to be used. Nuked OPL3 is used by default if macro is not defined.
+* `ADLMIDI_DISABLE_DOSBOX_EMULATOR` - Disables DosBox 0.74 OPL3 emulator.
+* `ADLMIDI_DISABLE_NUKED_EMULATOR` - Disables Nuked OPL3 emulator.
* `DISABLE_EMBEDDED_BANKS` - Disables usage of embedded banks. Use it to use custom-only banks.
### Public header (include)
@@ -86,11 +87,10 @@ You need to make in the any IDE a library project and put into it next files
* adlmidi.hpp - Public additional C++ API header, optional
### Internal code (src)
+* chips/* - Various OPL3 chip emulators and commonized interface over them
* adldata.hh - bank structures definition
* adlmidi_private.hpp - header of internal private APIs
-* dbopl.h - DOSBOX OPL Emulation header
* fraction.hpp - Fraction number handling
-* nukedopl3.h - Nuked OPL3 Emulation header
* adldata.cpp - Automatically generated database of FM banks from "fm_banks" directory via "gen_adldata" tool. **Don't build it if you have defined `DISABLE_EMBEDDED_BANKS` macro!**
* adlmidi.cpp - code of library
@@ -98,8 +98,7 @@ You need to make in the any IDE a library project and put into it next files
* adlmidi_midiplay.cpp - MIDI event sequencer
* adlmidi_opl3.cpp - OPL3 chips manager
* adlmidi_private.cpp - some internal functions sources
-* dbopl.cpp - DOSBOX OPL Emulation code (used when `ADLMIDI_USE_DOSBOX_OPL` macro is defined)
-* nukedopl3.c - Nuked OPL3 Emulation code (used by default, disabled when `ADLMIDI_USE_DOSBOX_OPL` macro is defined)
+
#### MUS2MIDI converter
To remove MUS support, define `ADLMIDI_DISABLE_MUS_SUPPORT` macro and remove those files:
@@ -138,6 +137,7 @@ To build that example you will need to have installed SDL2 library.
* Added ability to disable MUS and XMI converters
* Added ability to disable embedded MIDI sequencer to use library as RealTime synthesizer only or use any custom MIDI sequencer plugins.
* Fixed blank instruments fallback in multi-bank support. When using non-zero bank, if instrument is blank, then, instrument will be taken from a root (I.e. zero bank).
+ * Added support for real-time switching the emulator
## 1.3.1 2017-12-16
* Added Real-Time MIDI API (MIDI event functions and adl_generate() to generate PCM between of event rows) which allows you to implement plugin for media players or even a real time MIDI playing driver.
diff --git a/include/adlmidi.h b/include/adlmidi.h
index 1c2613b..4dbd476 100644
--- a/include/adlmidi.h
+++ b/include/adlmidi.h
@@ -126,9 +126,24 @@ extern int adl_openBankFile(struct ADL_MIDIPlayer *device, const char *filePath)
extern int adl_openBankData(struct ADL_MIDIPlayer *device, const void *mem, unsigned long size);
-/*Returns name of currently used OPL3 emulator*/
+/* DEPRECATED */
extern const char *adl_emulatorName();
+/*Returns chip emulator name string*/
+extern const char *adl_chipEmulatorName(struct ADL_MIDIPlayer *device);
+
+enum ADL_Emulator
+{
+ ADLMIDI_EMU_NUKED = 0,
+ ADLMIDI_EMU_NUKED_8,
+ ADLMIDI_EMU_DOSBOX,
+ ADLMIDI_EMU_end
+};
+
+/* Switch the emulation core */
+extern int adl_switchEmulator(struct ADL_MIDIPlayer *device, int emulator);
+
+
typedef struct {
ADL_UInt16 major;
ADL_UInt16 minor;
diff --git a/libADLMIDI-test.pro b/libADLMIDI-test.pro
index 7de3ee8..805474a 100644
--- a/libADLMIDI-test.pro
+++ b/libADLMIDI-test.pro
@@ -30,9 +30,14 @@ HEADERS += \
src/adlmidi_mus2mid.h \
src/adlmidi_private.hpp \
src/adlmidi_xmi2mid.h \
+ src/chips/dosbox/dbopl.h \
+ src/chips/dosbox_opl3.h \
+ src/chips/nuked/nukedopl3_174.h \
+ src/chips/nuked/nukedopl3.h \
+ src/chips/nuked_opl3.h \
+ src/chips/nuked_opl3_v174.h \
+ src/chips/opl_chip_base.h \
src/fraction.hpp \
- src/nukedopl3.h \
- src/dbopl.h \
src/midiplay/wave_writer.h
SOURCES += \
@@ -45,8 +50,13 @@ SOURCES += \
src/adlmidi_opl3.cpp \
src/adlmidi_private.cpp \
src/adlmidi_xmi2mid.c \
- src/nukedopl3.c \
- src/dbopl.cpp \
+ src/chips/dosbox/dbopl.cpp \
+ src/chips/dosbox_opl3.cpp \
+ src/chips/nuked/nukedopl3_174.c \
+ src/chips/nuked/nukedopl3.c \
+ src/chips/nuked_opl3.cpp \
+ src/chips/nuked_opl3_v174.cpp \
+ src/chips/opl_chip_base.cpp \
utils/midiplay/adlmidiplay.cpp \
utils/midiplay/wave_writer.c
diff --git a/src/adlmidi.cpp b/src/adlmidi.cpp
index db97447..d26911d 100644
--- a/src/adlmidi.cpp
+++ b/src/adlmidi.cpp
@@ -312,11 +312,34 @@ ADLMIDI_EXPORT int adl_openData(ADL_MIDIPlayer *device, const void *mem, unsigne
ADLMIDI_EXPORT const char *adl_emulatorName()
{
- #ifdef ADLMIDI_USE_DOSBOX_OPL
- return "DosBox";
- #else
- return "Nuked";
- #endif
+ return "<adl_emulatorName() is deprecated! Use adl_chipEmulatorName() instead!>";
+}
+
+ADLMIDI_EXPORT const char *adl_chipEmulatorName(struct ADL_MIDIPlayer *device)
+{
+ if(device)
+ {
+ MIDIplay *play = reinterpret_cast<MIDIplay *>(device->adl_midiPlayer);
+ if(play && !play->opl.cardsOP2.empty())
+ return play->opl.cardsOP2[0]->emulatorName();
+ }
+ return "Unknown";
+}
+
+ADLMIDI_EXPORT int adl_switchEmulator(struct ADL_MIDIPlayer *device, int emulator)
+{
+ if(device)
+ {
+ MIDIplay *play = reinterpret_cast<MIDIplay *>(device->adl_midiPlayer);
+ if(play && (emulator >= 0) && (emulator < ADLMIDI_EMU_end))
+ {
+ play->m_setup.emulator = emulator;
+ adl_reset(device);
+ return 0;
+ }
+ play->setErrorString("OPN2 MIDI: Unknown emulation core!");
+ }
+ return -1;
}
ADLMIDI_EXPORT const char *adl_linkedLibraryVersion()
@@ -373,7 +396,7 @@ ADLMIDI_EXPORT void adl_reset(struct ADL_MIDIPlayer *device)
return;
MIDIplay *play = reinterpret_cast<MIDIplay *>(device->adl_midiPlayer);
play->m_setup.tick_skip_samples_delay = 0;
- play->opl.Reset(play->m_setup.PCM_RATE);
+ play->opl.Reset(play->m_setup.emulator, play->m_setup.PCM_RATE);
play->ch.clear();
play->ch.resize(play->opl.NumChannels);
}
@@ -651,25 +674,13 @@ ADLMIDI_EXPORT int adl_play(ADL_MIDIPlayer *device, int sampleCount, short *out)
unsigned int chips = player->opl.NumCards;
if(chips == 1)
{
- #ifdef ADLMIDI_USE_DOSBOX_OPL
- player->opl.cards[0].GenerateArr(out_buf, &in_generatedStereo);
- in_generatedPhys = in_generatedStereo * 2;
- #else
- OPL3_GenerateStream(&player->opl.cards[0], out_buf, static_cast<Bit32u>(in_generatedStereo));
- #endif
+ player->opl.cardsOP2[0]->generate(out_buf, (size_t)in_generatedStereo);
}
else if(n_periodCountStereo > 0)
{
/* Generate data from every chip and mix result */
- for(unsigned card = 0; card < chips; ++card)
- {
- #ifdef ADLMIDI_USE_DOSBOX_OPL
- player->opl.cards[card].GenerateArrMix(out_buf, &in_generatedStereo);
- in_generatedPhys = in_generatedStereo * 2;
- #else
- OPL3_GenerateStreamMix(&player->opl.cards[card], out_buf, static_cast<Bit32u>(in_generatedStereo));
- #endif
- }
+ for(size_t card = 0; card < chips; ++card)
+ player->opl.cardsOP2[card]->generateAndMix(out_buf, (size_t)in_generatedStereo);
}
/* Process it */
SendStereoAudio(sampleCount, in_generatedStereo, out_buf, gotten_len, out);
@@ -743,26 +754,12 @@ ADLMIDI_EXPORT int adl_generate(struct ADL_MIDIPlayer *device, int sampleCount,
std::memset(out_buf, 0, static_cast<size_t>(in_generatedPhys) * sizeof(int16_t));
unsigned int chips = player->opl.NumCards;
if(chips == 1)
- {
- #ifdef ADLMIDI_USE_DOSBOX_OPL
- player->opl.cards[0].GenerateArr(out_buf, &in_generatedStereo);
- in_generatedPhys = in_generatedStereo * 2;
- #else
- OPL3_GenerateStream(&player->opl.cards[0], out_buf, static_cast<Bit32u>(in_generatedStereo));
- #endif
- }
+ player->opl.cardsOP2[0]->generate(out_buf, (size_t)in_generatedStereo);
else if(n_periodCountStereo > 0)
{
/* Generate data from every chip and mix result */
for(unsigned card = 0; card < chips; ++card)
- {
- #ifdef ADLMIDI_USE_DOSBOX_OPL
- player->opl.cards[card].GenerateArrMix(out_buf, &in_generatedStereo);
- in_generatedPhys = in_generatedStereo * 2;
- #else
- OPL3_GenerateStreamMix(&player->opl.cards[card], out_buf, static_cast<Bit32u>(in_generatedStereo));
- #endif
- }
+ player->opl.cardsOP2[card]->generate(out_buf, (size_t)in_generatedStereo);
}
/* Process it */
SendStereoAudio(sampleCount, in_generatedStereo, out_buf, gotten_len, out);
diff --git a/src/adlmidi_load.cpp b/src/adlmidi_load.cpp
index bc1fd8d..77e6f69 100644
--- a/src/adlmidi_load.cpp
+++ b/src/adlmidi_load.cpp
@@ -761,14 +761,14 @@ riffskip:
return false;
}
- //Build new MIDI events table (ALPHA!!!)
+ //Build new MIDI events table
if(!buildTrackData())
{
errorStringOut = fr._fileName + ": MIDI data parsing error has occouped!\n" + errorString;
return false;
}
- opl.Reset(m_setup.PCM_RATE); // Reset AdLib
+ opl.Reset(m_setup.emulator, m_setup.PCM_RATE); // Reset OPL3 chip
//opl.Reset(); // ...twice (just in case someone misprogrammed OPL3 previously)
ch.clear();
ch.resize(opl.NumChannels);
diff --git a/src/adlmidi_midiplay.cpp b/src/adlmidi_midiplay.cpp
index 13e1008..7b15349 100644
--- a/src/adlmidi_midiplay.cpp
+++ b/src/adlmidi_midiplay.cpp
@@ -710,6 +710,8 @@ MIDIplay::MIDIplay(unsigned long sampleRate):
{
devices.clear();
+ m_setup.emulator = ADLMIDI_EMU_NUKED;
+
m_setup.PCM_RATE = sampleRate;
m_setup.mindelay = 1.0 / (double)m_setup.PCM_RATE;
m_setup.maxdelay = 512.0 / (double)m_setup.PCM_RATE;
@@ -752,7 +754,7 @@ void MIDIplay::applySetup()
opl.NumFourOps = m_setup.NumFourOps;
cmf_percussion_mode = false;
- opl.Reset(m_setup.PCM_RATE);
+ opl.Reset(m_setup.emulator, m_setup.PCM_RATE);
ch.clear();
ch.resize(opl.NumChannels);
}
diff --git a/src/adlmidi_opl3.cpp b/src/adlmidi_opl3.cpp
index 7ac8f30..7fc7f4e 100644
--- a/src/adlmidi_opl3.cpp
+++ b/src/adlmidi_opl3.cpp
@@ -25,6 +25,21 @@
#ifdef ADLMIDI_HW_OPL
static const unsigned OPLBase = 0x388;
+#else
+# if defined(ADLMIDI_DISABLE_NUKED_EMULATOR) && defined(ADLMIDI_DISABLE_DOSBOX_EMULATOR)
+# error "No emulators enabled. You must enable at least one emulator to use this library!"
+# endif
+
+// Nuked OPL3 emulator, Most accurate, but requires the powerful CPU
+# ifndef ADLMIDI_DISABLE_NUKED_EMULATOR
+# include "chips/nuked_opl3.h"
+# include "chips/nuked_opl3_v174.h"
+# endif
+
+// DosBox 0.74 OPL3 emulator, Well-accurate and fast
+# ifndef ADLMIDI_DISABLE_DOSBOX_EMULATOR
+# include "chips/dosbox_opl3.h"
+# endif
#endif
#ifdef DISABLE_EMBEDDED_BANKS
@@ -184,13 +199,7 @@ void OPL3::Poke(size_t card, uint32_t index, uint32_t value)
#endif//__WATCOMC__
#else//ADLMIDI_HW_OPL
-
- #ifdef ADLMIDI_USE_DOSBOX_OPL
- cards[card].WriteReg(index, static_cast<uint8_t>(value));
- #else
- OPL3_WriteReg(&cards[card], static_cast<Bit16u>(index), static_cast<Bit8u>(value));
- #endif
-
+ cardsOP2[card]->writeReg(static_cast<uint16_t>(index), static_cast<uint8_t>(value));
#endif//ADLMIDI_HW_OPL
}
@@ -216,11 +225,7 @@ void OPL3::PokeN(size_t card, uint16_t index, uint8_t value)
#endif//__WATCOMC__
#else
- #ifdef ADLMIDI_USE_DOSBOX_OPL
- cards[card].WriteReg(static_cast<Bit32u>(index), value);
- #else
- OPL3_WriteReg(&cards[card], index, value);
- #endif
+ cardsOP2[card]->writeReg(index, value);
#endif
}
@@ -498,24 +503,27 @@ void OPL3::ChangeVolumeRangesModel(ADLMIDI_VolumeModels volumeModel)
}
}
-void OPL3::Reset(unsigned long PCM_RATE)
+void OPL3::ClearChips()
+{
+ for(size_t i = 0; i < cardsOP2.size(); i++)
+ cardsOP2[i].reset(NULL);
+ cardsOP2.clear();
+}
+
+void OPL3::Reset(int emulator, unsigned long PCM_RATE)
{
#ifndef ADLMIDI_HW_OPL
- #ifdef ADLMIDI_USE_DOSBOX_OPL
- DBOPL::Handler emptyChip; //Constructors inside are will initialize necessary fields
- #else
- _opl3_chip emptyChip;
- std::memset(&emptyChip, 0, sizeof(_opl3_chip));
- #endif
- cards.clear();
+ ClearChips();
#endif
(void)PCM_RATE;
ins.clear();
pit.clear();
regBD.clear();
+
#ifndef ADLMIDI_HW_OPL
- cards.resize(NumCards, emptyChip);
+ cardsOP2.resize(NumCards, AdlMIDI_CPtr<OPLChipBase>());
#endif
+
NumChannels = NumCards * 23;
ins.resize(NumChannels, 189);
pit.resize(NumChannels, 0);
@@ -536,24 +544,35 @@ void OPL3::Reset(unsigned long PCM_RATE)
};
unsigned fours = NumFourOps;
- for(unsigned card = 0; card < NumCards; ++card)
+ for(size_t i = 0; i < cardsOP2.size(); ++i)
{
- #ifndef ADLMIDI_HW_OPL
- # ifdef ADLMIDI_USE_DOSBOX_OPL
- cards[card].Init(PCM_RATE);
- # else
- OPL3_Reset(&cards[card], static_cast<Bit32u>(PCM_RATE));
- # endif
- #endif
-
- for(unsigned a = 0; a < 18; ++a) Poke(card, 0xB0 + Channels[a], 0x00);
+ switch(emulator)
+ {
+ default:
+#ifndef ADLMIDI_DISABLE_NUKED_EMULATOR
+ case ADLMIDI_EMU_NUKED:
+ cardsOP2[i].reset(new NukedOPL3v174());
+ break;
+ case ADLMIDI_EMU_NUKED_8:
+ cardsOP2[i].reset(new NukedOPL3());
+ break;
+#endif
+#ifndef ADLMIDI_DISABLE_DOSBOX_EMULATOR
+ case ADLMIDI_EMU_DOSBOX:
+ cardsOP2[i].reset(new DosBoxOPL3());
+ break;
+#endif
+ }
+ cardsOP2[i]->setRate((uint32_t)PCM_RATE);
+
+ for(unsigned a = 0; a < 18; ++a) Poke(i, 0xB0 + Channels[a], 0x00);
for(unsigned a = 0; a < sizeof(data) / sizeof(*data); a += 2)
- PokeN(card, data[a], static_cast<uint8_t>(data[a + 1]));
- Poke(card, 0x0BD, regBD[card] = (HighTremoloMode * 0x80
- + HighVibratoMode * 0x40
- + AdlPercussionMode * 0x20));
+ PokeN(i, data[a], static_cast<uint8_t>(data[a + 1]));
+ Poke(i, 0x0BD, regBD[i] = (HighTremoloMode * 0x80
+ + HighVibratoMode * 0x40
+ + AdlPercussionMode * 0x20));
unsigned fours_this_card = std::min(fours, 6u);
- Poke(card, 0x104, (1 << fours_this_card) - 1);
+ Poke(i, 0x104, (1 << fours_this_card) - 1);
//fprintf(stderr, "Card %u: %u four-ops.\n", card, fours_this_card);
fours -= fours_this_card;
}
diff --git a/src/adlmidi_private.hpp b/src/adlmidi_private.hpp
index 8523343..82a6627 100644
--- a/src/adlmidi_private.hpp
+++ b/src/adlmidi_private.hpp
@@ -112,13 +112,8 @@ typedef int32_t ssize_t;
#endif
#include "fraction.hpp"
-
#ifndef ADLMIDI_HW_OPL
-#ifdef ADLMIDI_USE_DOSBOX_OPL
-#include "dbopl.h"
-#else
-#include "nukedopl3.h"
-#endif
+#include "chips/opl_chip_base.h"
#endif
#include "adldata.hh"
@@ -178,11 +173,7 @@ public:
char ____padding[4];
ADL_MIDIPlayer *_parent;
#ifndef ADLMIDI_HW_OPL
-# ifdef ADLMIDI_USE_DOSBOX_OPL
- std::vector<DBOPL::Handler> cards;
-# else
- std::vector<_opl3_chip> cards;
-# endif
+ std::vector<AdlMIDI_CPtr<OPLChipBase > > cardsOP2;
#endif
private:
std::vector<size_t> ins; // index to adl[], cached, needed by Touch()
@@ -268,7 +259,8 @@ public:
void updateFlags();
void updateDeepFlags();
void ChangeVolumeRangesModel(ADLMIDI_VolumeModels volumeModel);
- void Reset(unsigned long PCM_RATE);
+ void ClearChips();
+ void Reset(int emulator, unsigned long PCM_RATE);
};
@@ -712,6 +704,7 @@ public:
struct Setup
{
+ int emulator;
unsigned int AdlBank;
unsigned int NumFourOps;
unsigned int NumCards;
diff --git a/src/dbopl.cpp b/src/chips/dosbox/dbopl.cpp
index fb28e20..8834524 100644
--- a/src/dbopl.cpp
+++ b/src/chips/dosbox/dbopl.cpp
@@ -1,4 +1,4 @@
-#ifdef ADLMIDI_USE_DOSBOX_OPL
+//#ifdef ADLMIDI_USE_DOSBOX_OPL
#ifdef __MINGW32__
typedef struct vswprintf {} swprintf;
@@ -2042,4 +2042,4 @@ namespace DBOPL
} //Namespace DBOPL
-#endif //ADLMIDI_USE_DOSBOX_OPL
+//#endif //ADLMIDI_USE_DOSBOX_OPL
diff --git a/src/dbopl.h b/src/chips/dosbox/dbopl.h
index b5b2ba1..b5b2ba1 100644
--- a/src/dbopl.h
+++ b/src/chips/dosbox/dbopl.h
diff --git a/src/chips/dosbox_opl3.cpp b/src/chips/dosbox_opl3.cpp
new file mode 100644
index 0000000..b696343
--- /dev/null
+++ b/src/chips/dosbox_opl3.cpp
@@ -0,0 +1,84 @@
+#include "dosbox_opl3.h"
+#include "dosbox/dbopl.h"
+#include <cstdlib>
+#include <assert.h>
+
+DosBoxOPL3::DosBoxOPL3() :
+ OPLChipBase(),
+ m_chip(nullptr)
+{
+ reset();
+}
+
+DosBoxOPL3::DosBoxOPL3(const DosBoxOPL3 &c) :
+ OPLChipBase(c),
+ m_chip(nullptr)
+{
+ setRate(c.m_rate);
+}
+
+DosBoxOPL3::~DosBoxOPL3()
+{
+ DBOPL::Handler *chip_r = reinterpret_cast<DBOPL::Handler*>(m_chip);
+ delete chip_r;
+}
+
+void DosBoxOPL3::setRate(uint32_t rate)
+{
+ OPLChipBase::setRate(rate);
+ reset();
+}
+
+void DosBoxOPL3::reset()
+{
+ DBOPL::Handler *chip_r = reinterpret_cast<DBOPL::Handler*>(m_chip);
+ if(m_chip && chip_r)
+ delete chip_r;
+ m_chip = new DBOPL::Handler;
+ chip_r = reinterpret_cast<DBOPL::Handler*>(m_chip);
+ chip_r->Init(m_rate);
+}
+
+void DosBoxOPL3::reset(uint32_t rate)
+{
+ setRate(rate);
+}
+
+void DosBoxOPL3::writeReg(uint16_t addr, uint8_t data)
+{
+ DBOPL::Handler *chip_r = reinterpret_cast<DBOPL::Handler*>(m_chip);
+ chip_r->WriteReg(static_cast<Bit32u>(addr), data);
+}
+
+int DosBoxOPL3::generate(int16_t *output, size_t frames)
+{
+ DBOPL::Handler *chip_r = reinterpret_cast<DBOPL::Handler*>(m_chip);
+ ssize_t left = (ssize_t)frames;
+ while(left > 0)
+ {
+ ssize_t frames_i = left;
+ chip_r->GenerateArr(output, &frames_i);
+ output += (frames_i * 2);
+ left -= frames_i;
+ }
+ return (int)frames;
+}
+
+int DosBoxOPL3::generateAndMix(int16_t *output, size_t frames)
+{
+ DBOPL::Handler *chip_r = reinterpret_cast<DBOPL::Handler*>(m_chip);
+ ssize_t left = (ssize_t)frames;
+ while(left > 0)
+ {
+ ssize_t frames_i = left;
+ chip_r->GenerateArrMix(output, &frames_i);
+ output += (frames_i * 2);
+ left -= frames_i;
+ }
+ return (int)frames;
+}
+
+const char *DosBoxOPL3::emulatorName()
+{
+ return "DosBox 0.74 OPL3";
+}
diff --git a/src/chips/dosbox_opl3.h b/src/chips/dosbox_opl3.h
new file mode 100644
index 0000000..46c20fa
--- /dev/null
+++ b/src/chips/dosbox_opl3.h
@@ -0,0 +1,23 @@
+#ifndef DOSBOX_OPL3_H
+#define DOSBOX_OPL3_H
+
+#include "opl_chip_base.h"
+
+class DosBoxOPL3 final : public OPLChipBase
+{
+ void *m_chip;
+public:
+ DosBoxOPL3();
+ DosBoxOPL3(const DosBoxOPL3 &c);
+ virtual ~DosBoxOPL3() override;
+
+ virtual void setRate(uint32_t rate) override;
+ virtual void reset() override;
+ virtual void reset(uint32_t rate) override;
+ virtual void writeReg(uint16_t addr, uint8_t data) override;
+ virtual int generate(int16_t *output, size_t frames) override;
+ virtual int generateAndMix(int16_t *output, size_t frames) override;
+ virtual const char *emulatorName() override;
+};
+
+#endif // DOSBOX_OPL3_H
diff --git a/src/chips/nuked/nukedopl3.c b/src/chips/nuked/nukedopl3.c
new file mode 100644
index 0000000..eadb6b9
--- /dev/null
+++ b/src/chips/nuked/nukedopl3.c
@@ -0,0 +1,1391 @@
+/*
+ * Copyright (C) 2013-2018 Alexey Khokholov (Nuke.YKT)
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ *
+ * Nuked OPL3 emulator.
+ * Thanks:
+ * MAME Development Team(Jarek Burczynski, Tatsuyuki Satoh):
+ * Feedback and Rhythm part calculation information.
+ * forums.submarine.org.uk(carbon14, opl3):
+ * Tremolo and phase generator calculation information.
+ * OPLx decapsulated(Matthew Gambrell, Olli Niemitalo):
+ * OPL2 ROMs.
+ * siliconpr0n.org(John McMaster, digshadow):
+ * YMF262 and VRC VII decaps and die shots.
+ *
+ * version: 1.8
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "nukedopl3.h"
+
+#define RSM_FRAC 10
+
+/* Channel types */
+
+enum {
+ ch_2op = 0,
+ ch_4op = 1,
+ ch_4op2 = 2,
+ ch_drum = 3
+};
+
+/* Envelope key types */
+
+enum {
+ egk_norm = 0x01,
+ egk_drum = 0x02
+};
+
+
+/*
+ * logsin table
+ */
+
+static const Bit16u logsinrom[256] = {
+ 0x859, 0x6c3, 0x607, 0x58b, 0x52e, 0x4e4, 0x4a6, 0x471,
+ 0x443, 0x41a, 0x3f5, 0x3d3, 0x3b5, 0x398, 0x37e, 0x365,
+ 0x34e, 0x339, 0x324, 0x311, 0x2ff, 0x2ed, 0x2dc, 0x2cd,
+ 0x2bd, 0x2af, 0x2a0, 0x293, 0x286, 0x279, 0x26d, 0x261,
+ 0x256, 0x24b, 0x240, 0x236, 0x22c, 0x222, 0x218, 0x20f,
+ 0x206, 0x1fd, 0x1f5, 0x1ec, 0x1e4, 0x1dc, 0x1d4, 0x1cd,
+ 0x1c5, 0x1be, 0x1b7, 0x1b0, 0x1a9, 0x1a2, 0x19b, 0x195,
+ 0x18f, 0x188, 0x182, 0x17c, 0x177, 0x171, 0x16b, 0x166,
+ 0x160, 0x15b, 0x155, 0x150, 0x14b, 0x146, 0x141, 0x13c,
+ 0x137, 0x133, 0x12e, 0x129, 0x125, 0x121, 0x11c, 0x118,
+ 0x114, 0x10f, 0x10b, 0x107, 0x103, 0x0ff, 0x0fb, 0x0f8,
+ 0x0f4, 0x0f0, 0x0ec, 0x0e9, 0x0e5, 0x0e2, 0x0de, 0x0db,
+ 0x0d7, 0x0d4, 0x0d1, 0x0cd, 0x0ca, 0x0c7, 0x0c4, 0x0c1,
+ 0x0be, 0x0bb, 0x0b8, 0x0b5, 0x0b2, 0x0af, 0x0ac, 0x0a9,
+ 0x0a7, 0x0a4, 0x0a1, 0x09f, 0x09c, 0x099, 0x097, 0x094,
+ 0x092, 0x08f, 0x08d, 0x08a, 0x088, 0x086, 0x083, 0x081,
+ 0x07f, 0x07d, 0x07a, 0x078, 0x076, 0x074, 0x072, 0x070,
+ 0x06e, 0x06c, 0x06a, 0x068, 0x066, 0x064, 0x062, 0x060,
+ 0x05e, 0x05c, 0x05b, 0x059, 0x057, 0x055, 0x053, 0x052,
+ 0x050, 0x04e, 0x04d, 0x04b, 0x04a, 0x048, 0x046, 0x045,
+ 0x043, 0x042, 0x040, 0x03f, 0x03e, 0x03c, 0x03b, 0x039,
+ 0x038, 0x037, 0x035, 0x034, 0x033, 0x031, 0x030, 0x02f,
+ 0x02e, 0x02d, 0x02b, 0x02a, 0x029, 0x028, 0x027, 0x026,
+ 0x025, 0x024, 0x023, 0x022, 0x021, 0x020, 0x01f, 0x01e,
+ 0x01d, 0x01c, 0x01b, 0x01a, 0x019, 0x018, 0x017, 0x017,
+ 0x016, 0x015, 0x014, 0x014, 0x013, 0x012, 0x011, 0x011,
+ 0x010, 0x00f, 0x00f, 0x00e, 0x00d, 0x00d, 0x00c, 0x00c,
+ 0x00b, 0x00a, 0x00a, 0x009, 0x009, 0x008, 0x008, 0x007,
+ 0x007, 0x007, 0x006, 0x006, 0x005, 0x005, 0x005, 0x004,
+ 0x004, 0x004, 0x003, 0x003, 0x003, 0x002, 0x002, 0x002,
+ 0x002, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,
+ 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000
+};
+
+/*
+ * exp table
+ */
+
+static const Bit16u exprom[256] = {
+ 0x7fa, 0x7f5, 0x7ef, 0x7ea, 0x7e4, 0x7df, 0x7da, 0x7d4,
+ 0x7cf, 0x7c9, 0x7c4, 0x7bf, 0x7b9, 0x7b4, 0x7ae, 0x7a9,
+ 0x7a4, 0x79f, 0x799, 0x794, 0x78f, 0x78a, 0x784, 0x77f,
+ 0x77a, 0x775, 0x770, 0x76a, 0x765, 0x760, 0x75b, 0x756,
+ 0x751, 0x74c, 0x747, 0x742, 0x73d, 0x738, 0x733, 0x72e,
+ 0x729, 0x724, 0x71f, 0x71a, 0x715, 0x710, 0x70b, 0x706,
+ 0x702, 0x6fd, 0x6f8, 0x6f3, 0x6ee, 0x6e9, 0x6e5, 0x6e0,
+ 0x6db, 0x6d6, 0x6d2, 0x6cd, 0x6c8, 0x6c4, 0x6bf, 0x6ba,
+ 0x6b5, 0x6b1, 0x6ac, 0x6a8, 0x6a3, 0x69e, 0x69a, 0x695,
+ 0x691, 0x68c, 0x688, 0x683, 0x67f, 0x67a, 0x676, 0x671,
+ 0x66d, 0x668, 0x664, 0x65f, 0x65b, 0x657, 0x652, 0x64e,
+ 0x649, 0x645, 0x641, 0x63c, 0x638, 0x634, 0x630, 0x62b,
+ 0x627, 0x623, 0x61e, 0x61a, 0x616, 0x612, 0x60e, 0x609,
+ 0x605, 0x601, 0x5fd, 0x5f9, 0x5f5, 0x5f0, 0x5ec, 0x5e8,
+ 0x5e4, 0x5e0, 0x5dc, 0x5d8, 0x5d4, 0x5d0, 0x5cc, 0x5c8,
+ 0x5c4, 0x5c0, 0x5bc, 0x5b8, 0x5b4, 0x5b0, 0x5ac, 0x5a8,
+ 0x5a4, 0x5a0, 0x59c, 0x599, 0x595, 0x591, 0x58d, 0x589,
+ 0x585, 0x581, 0x57e, 0x57a, 0x576, 0x572, 0x56f, 0x56b,
+ 0x567, 0x563, 0x560, 0x55c, 0x558, 0x554, 0x551, 0x54d,
+ 0x549, 0x546, 0x542, 0x53e, 0x53b, 0x537, 0x534, 0x530,
+ 0x52c, 0x529, 0x525, 0x522, 0x51e, 0x51b, 0x517, 0x514,
+ 0x510, 0x50c, 0x509, 0x506, 0x502, 0x4ff, 0x4fb, 0x4f8,
+ 0x4f4, 0x4f1, 0x4ed, 0x4ea, 0x4e7, 0x4e3, 0x4e0, 0x4dc,
+ 0x4d9, 0x4d6, 0x4d2, 0x4cf, 0x4cc, 0x4c8, 0x4c5, 0x4c2,
+ 0x4be, 0x4bb, 0x4b8, 0x4b5, 0x4b1, 0x4ae, 0x4ab, 0x4a8,
+ 0x4a4, 0x4a1, 0x49e, 0x49b, 0x498, 0x494, 0x491, 0x48e,
+ 0x48b, 0x488, 0x485, 0x482, 0x47e, 0x47b, 0x478, 0x475,
+ 0x472, 0x46f, 0x46c, 0x469, 0x466, 0x463, 0x460, 0x45d,
+ 0x45a, 0x457, 0x454, 0x451, 0x44e, 0x44b, 0x448, 0x445,
+ 0x442, 0x43f, 0x43c, 0x439, 0x436, 0x433, 0x430, 0x42d,
+ 0x42a, 0x428, 0x425, 0x422, 0x41f, 0x41c, 0x419, 0x416,
+ 0x414, 0x411, 0x40e, 0x40b, 0x408, 0x406, 0x403, 0x400
+};
+
+/*
+ * freq mult table multiplied by 2
+ *
+ * 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 12, 12, 15, 15
+ */
+
+static const Bit8u mt[16] = {
+ 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 24, 24, 30, 30
+};
+
+/*
+ * ksl table
+ */
+
+static const Bit8u kslrom[16] = {
+ 0, 32, 40, 45, 48, 51, 53, 55, 56, 58, 59, 60, 61, 62, 63, 64
+};
+
+static const Bit8u kslshift[4] = {
+ 8, 1, 2, 0
+};
+
+/*
+ * envelope generator constants
+ */
+
+static const Bit8u eg_incstep[4][4] = {
+ { 0, 0, 0, 0 },
+ { 1, 0, 0, 0 },
+ { 1, 0, 1, 0 },
+ { 1, 1, 1, 0 }
+};
+
+/*
+ * address decoding
+ */
+
+static const Bit8s ad_slot[0x20] = {
+ 0, 1, 2, 3, 4, 5, -1, -1, 6, 7, 8, 9, 10, 11, -1, -1,
+ 12, 13, 14, 15, 16, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
+};
+
+static const Bit8u ch_slot[18] = {
+ 0, 1, 2, 6, 7, 8, 12, 13, 14, 18, 19, 20, 24, 25, 26, 30, 31, 32
+};
+
+/*
+ * Envelope generator
+ */
+
+typedef Bit16s(*envelope_sinfunc)(Bit16u phase, Bit16u envelope);
+typedef void(*envelope_genfunc)(opl3_slot *slott);
+
+static Bit16s OPL3_EnvelopeCalcExp(Bit32u level)
+{
+ if (level > 0x1fff)
+ {
+ level = 0x1fff;
+ }
+ return (exprom[level & 0xff] << 1) >> (level >> 8);
+}
+
+static Bit16s OPL3_EnvelopeCalcSin0(Bit16u phase, Bit16u envelope)
+{
+ Bit16u out = 0;
+ Bit16u neg = 0;
+ phase &= 0x3ff;
+ if (phase & 0x200)
+ {
+ neg = 0xffff;
+ }
+ if (phase & 0x100)
+ {
+ out = logsinrom[(phase & 0xff) ^ 0xff];
+ }
+ else
+ {
+ out = logsinrom[phase & 0xff];
+ }
+ return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg;
+}
+
+static Bit16s OPL3_EnvelopeCalcSin1(Bit16u phase, Bit16u envelope)
+{
+ Bit16u out = 0;
+ phase &= 0x3ff;
+ if (phase & 0x200)
+ {
+ out = 0x1000;
+ }
+ else if (phase & 0x100)
+ {
+ out = logsinrom[(phase & 0xff) ^ 0xff];
+ }
+ else
+ {
+ out = logsinrom[phase & 0xff];
+ }
+ return OPL3_EnvelopeCalcExp(out + (envelope << 3));
+}
+
+static Bit16s OPL3_EnvelopeCalcSin2(Bit16u phase, Bit16u envelope)
+{
+ Bit16u out = 0;
+ phase &= 0x3ff;
+ if (phase & 0x100)
+ {
+ out = logsinrom[(phase & 0xff) ^ 0xff];
+ }
+ else
+ {
+ out = logsinrom[phase & 0xff];
+ }
+ return OPL3_EnvelopeCalcExp(out + (envelope << 3));
+}
+
+static Bit16s OPL3_EnvelopeCalcSin3(Bit16u phase, Bit16u envelope)
+{
+ Bit16u out = 0;
+ phase &= 0x3ff;
+ if (phase & 0x100)
+ {
+ out = 0x1000;
+ }
+ else
+ {
+ out = logsinrom[phase & 0xff];
+ }
+ return OPL3_EnvelopeCalcExp(out + (envelope << 3));
+}
+
+static Bit16s OPL3_EnvelopeCalcSin4(Bit16u phase, Bit16u envelope)
+{
+ Bit16u out = 0;
+ Bit16u neg = 0;
+ phase &= 0x3ff;
+ if ((phase & 0x300) == 0x100)
+ {
+ neg = 0xffff;
+ }
+ if (phase & 0x200)
+ {
+ out = 0x1000;
+ }
+ else if (phase & 0x80)
+ {
+ out = logsinrom[((phase ^ 0xff) << 1) & 0xff];
+ }
+ else
+ {
+ out = logsinrom[(phase << 1) & 0xff];
+ }
+ return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg;
+}
+
+static Bit16s OPL3_EnvelopeCalcSin5(Bit16u phase, Bit16u envelope)
+{
+ Bit16u out = 0;
+ phase &= 0x3ff;
+ if (phase & 0x200)
+ {
+ out = 0x1000;
+ }
+ else if (phase & 0x80)
+ {
+ out = logsinrom[((phase ^ 0xff) << 1) & 0xff];
+ }
+ else
+ {
+ out = logsinrom[(phase << 1) & 0xff];
+ }
+ return OPL3_EnvelopeCalcExp(out + (envelope << 3));
+}
+
+static Bit16s OPL3_EnvelopeCalcSin6(Bit16u phase, Bit16u envelope)
+{
+ Bit16u neg = 0;
+ phase &= 0x3ff;
+ if (phase & 0x200)
+ {
+ neg = 0xffff;
+ }
+ return OPL3_EnvelopeCalcExp(envelope << 3) ^ neg;
+}
+
+static Bit16s OPL3_EnvelopeCalcSin7(Bit16u phase, Bit16u envelope)
+{
+ Bit16u out = 0;
+ Bit16u neg = 0;
+ phase &= 0x3ff;
+ if (phase & 0x200)
+ {
+ neg = 0xffff;
+ phase = (phase & 0x1ff) ^ 0x1ff;
+ }
+ out = phase << 3;
+ return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg;
+}
+
+static const envelope_sinfunc envelope_sin[8] = {
+ 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 OPL3_EnvelopeUpdateKSL(opl3_slot *slot)
+{
+ Bit16s ksl = (kslrom[slot->channel->f_num >> 6] << 2)
+ - ((0x08 - slot->channel->block) << 5);
+ if (ksl < 0)
+ {
+ ksl = 0;
+ }
+ slot->eg_ksl = (Bit8u)ksl;
+}
+
+static void OPL3_EnvelopeCalc(opl3_slot *slot)
+{
+ Bit8u nonzero;
+ Bit8u rate;
+ Bit8u rate_hi;
+ Bit8u rate_lo;
+ Bit8u reg_rate = 0;
+ Bit8u ks;
+ Bit8u eg_shift, shift;
+ Bit16u eg_rout;
+ Bit16s eg_inc;
+ Bit8u eg_off;
+ Bit8u reset = 0;
+ 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;
+ }
+ else
+ {
+ switch (slot->eg_gen)
+ {
+ case envelope_gen_num_attack:
+ reg_rate = slot->reg_ar;
+ break;
+ case envelope_gen_num_decay:
+ reg_rate = slot->reg_dr;
+ break;
+ case envelope_gen_num_sustain:
+ if (!slot->reg_type)
+ {
+ reg_rate = slot->reg_rr;
+ }
+ break;
+ case envelope_gen_num_release:
+ reg_rate = slot->reg_rr;
+ break;
+ }
+ }
+ slot->pg_reset = reset;
+ 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;
+ if (nonzero)
+ {
+ if (rate_hi < 12)
+ {
+ if (slot->chip->eg_state)
+ {
+ switch (eg_shift)
+ {
+ case 12:
+ shift = 1;
+ break;
+ case 13:
+ shift = (rate_lo >> 1) & 0x01;
+ break;
+ case 14:
+ shift = rate_lo & 0x01;
+ break;
+ default:
+ break;
+ }
+ }
+ }
+ else
+ {
+ shift = (rate_hi & 0x03) + eg_incstep[rate_lo][slot->chip->timer & 0x03];
+ if (shift & 0x04)
+ {
+ shift = 0x03;
+ }
+ if (!shift)
+ {
+ shift = slot->chip->eg_state;
+ }
+ }
+ }
+ eg_rout = slot->eg_rout;
+ eg_inc = 0;
+ eg_off = 0;
+ /* Instant attack */
+ if (reset && rate_hi == 0x0f)
+ {
+ eg_rout = 0x00;
+ }
+ /* Envelope off */
+ if ((slot->eg_rout & 0x1f8) == 0x1f8)
+ {
+ eg_off = 1;
+ }
+ if (slot->eg_gen != envelope_gen_num_attack && !reset && eg_off)
+ {
+ eg_rout = 0x1ff;
+ }
+ switch (slot->eg_gen)
+ {
+ case envelope_gen_num_attack:
+ 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;
+ }
+ break;
+ case envelope_gen_num_decay:
+ if ((slot->eg_rout >> 4) == slot->reg_sl)
+ {
+ slot->eg_gen = envelope_gen_num_sustain;
+ }
+ else if (!eg_off && !reset && shift > 0)
+ {
+ eg_inc = 1 << (shift - 1);
+ }
+ break;
+ case envelope_gen_num_sustain:
+ case envelope_gen_num_release:
+ if (!eg_off && !reset && shift > 0)
+ {
+ eg_inc = 1 << (shift - 1);
+ }
+ break;
+ }
+ slot->eg_rout = (eg_rout + eg_inc) & 0x1ff;
+ /* Key off */
+ if (reset)
+ {
+ slot->eg_gen = envelope_gen_num_attack;
+ }
+ if (!slot->key)
+ {
+ slot->eg_gen = envelope_gen_num_release;
+ }
+}
+
+static void OPL3_EnvelopeKeyOn(opl3_slot *slot, Bit8u type)
+{
+ slot->key |= type;
+}
+
+static void OPL3_EnvelopeKeyOff(opl3_slot *slot, Bit8u type)
+{
+ slot->key &= ~type;
+}
+
+/*
+ * Phase Generator
+ */
+
+static void OPL3_PhaseGenerate(opl3_slot *slot)
+{
+ opl3_chip *chip;
+ Bit16u f_num;
+ Bit32u basefreq;
+ Bit8u rm_xor, n_bit;
+ Bit32u noise;
+ Bit16u phase;
+
+ chip = slot->chip;
+ f_num = slot->channel->f_num;
+ if (slot->reg_vib)
+ {
+ Bit8s range;
+ Bit8u vibpos;
+
+ range = (f_num >> 7) & 7;
+ vibpos = slot->chip->vibpos;
+
+ if (!(vibpos & 3))
+ {
+ range = 0;
+ }
+ else if (vibpos & 1)
+ {
+ range >>= 1;
+ }
+ range >>= slot->chip->vibshift;
+
+ if (vibpos & 4)
+ {
+ range = -range;
+ }
+ f_num += range;
+ }
+ basefreq = (f_num << slot->channel->block) >> 1;
+ phase = (Bit16u)(slot->pg_phase >> 9);
+ if (slot->pg_reset)
+ {
+ slot->pg_phase = 0;
+ }
+ slot->pg_phase += (basefreq * mt[slot->reg_mult]) >> 1;
+ /* Rhythm mode */
+ noise = chip->noise;
+ slot->pg_phase_out = phase;
+ if (slot->slot_num == 13) /* hh */
+ {
+ 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 && (chip->rhy & 0x20)) /* tc */
+ {
+ chip->rm_tc_bit3 = (phase >> 3) & 1;
+ chip->rm_tc_bit5 = (phase >> 5) & 1;
+ }
+ 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 */
+ slot->pg_phase_out = rm_xor << 9;
+ if (rm_xor ^ (noise & 1))
+ {
+ slot->pg_phase_out |= 0xd0;
+ }
+ else
+ {
+ slot->pg_phase_out |= 0x34;
+ }
+ break;
+ case 16: /* sd */
+ slot->pg_phase_out = (chip->rm_hh_bit8 << 9)
+ | ((chip->rm_hh_bit8 ^ (noise & 1)) << 8);
+ break;
+ case 17: /* tc */
+ slot->pg_phase_out = (rm_xor << 9) | 0x80;
+ break;
+ default:
+ break;
+ }
+ }
+ n_bit = ((noise >> 14) ^ noise) & 0x01;
+ chip->noise = (noise >> 1) | (n_bit << 22);
+}
+
+/*
+ * Slot
+ */
+
+static void OPL3_SlotWrite20(opl3_slot *slot, Bit8u data)
+{
+ if ((data >> 7) & 0x01)
+ {
+ slot->trem = &slot->chip->tremolo;
+ }
+ else
+ {
+ slot->trem = (Bit8u*)&slot->chip->zeromod;
+ }
+ slot->reg_vib = (data >> 6) & 0x01;
+ slot->reg_type = (data >> 5) & 0x01;
+ slot->reg_ksr = (data >> 4) & 0x01;
+ slot->reg_mult = data & 0x0f;
+}
+
+static void OPL3_SlotWrite40(opl3_slot *slot, Bit8u data)
+{
+ slot->reg_ksl = (data >> 6) & 0x03;
+ slot->reg_tl = data & 0x3f;
+ OPL3_EnvelopeUpdateKSL(slot);
+}
+
+static void OPL3_SlotWrite60(opl3_slot *slot, Bit8u data)
+{
+ slot->reg_ar = (data >> 4) & 0x0f;
+ slot->reg_dr = data & 0x0f;
+}
+
+static void OPL3_SlotWrite80(opl3_slot *slot, Bit8u data)
+{
+ slot->reg_sl = (data >> 4) & 0x0f;
+ if (slot->reg_sl == 0x0f)
+ {
+ slot->reg_sl = 0x1f;
+ }
+ slot->reg_rr = data & 0x0f;
+}
+
+static void OPL3_SlotWriteE0(opl3_slot *slot, Bit8u data)
+{
+ slot->reg_wf = data & 0x07;
+ if (slot->chip->newm == 0x00)
+ {
+ slot->reg_wf &= 0x03;
+ }
+}
+
+static void OPL3_SlotGenerate(opl3_slot *slot)
+{
+ slot->out = envelope_sin[slot->reg_wf](slot->pg_phase_out + *slot->mod, slot->eg_out);
+}
+
+static void OPL3_SlotCalcFB(opl3_slot *slot)
+{
+ if (slot->channel->fb != 0x00)
+ {
+ slot->fbmod = (slot->prout + slot->out) >> (0x09 - slot->channel->fb);
+ }
+ else
+ {
+ slot->fbmod = 0;
+ }
+ slot->prout = slot->out;
+}
+
+/*
+ * Channel
+ */
+
+static void OPL3_ChannelSetupAlg(opl3_channel *channel);
+
+static void OPL3_ChannelUpdateRhythm(opl3_chip *chip, Bit8u data)
+{
+ opl3_channel *channel6;
+ opl3_channel *channel7;
+ opl3_channel *channel8;
+ Bit8u chnum;
+
+ chip->rhy = data & 0x3f;
+ if (chip->rhy & 0x20)
+ {
+ channel6 = &chip->channel[6];
+ channel7 = &chip->channel[7];
+ channel8 = &chip->channel[8];
+ channel6->out[0] = &channel6->slots[1]->out;
+ channel6->out[1] = &channel6->slots[1]->out;
+ channel6->out[2] = &chip->zeromod;
+ channel6->out[3] = &chip->zeromod;
+ channel7->out[0] = &channel7->slots[0]->out;
+ channel7->out[1] = &channel7->slots[0]->out;
+ channel7->out[2] = &channel7->slots[1]->out;
+ channel7->out[3] = &channel7->slots[1]->out;
+ channel8->out[0] = &channel8->slots[0]->out;
+ channel8->out[1] = &channel8->slots[0]->out;
+ channel8->out[2] = &channel8->slots[1]->out;
+ channel8->out[3] = &channel8->slots[1]->out;
+ for (chnum = 6; chnum < 9; chnum++)
+ {
+ chip->channel[chnum].chtype = ch_drum;
+ }
+ OPL3_ChannelSetupAlg(channel6);
+ OPL3_ChannelSetupAlg(channel7);
+ OPL3_ChannelSetupAlg(channel8);
+ /* hh */
+ if (chip->rhy & 0x01)
+ {
+ OPL3_EnvelopeKeyOn(channel7->slots[0], egk_drum);
+ }
+ else
+ {
+ OPL3_EnvelopeKeyOff(channel7->slots[0], egk_drum);
+ }
+ /* tc */
+ if (chip->rhy & 0x02)
+ {
+ OPL3_EnvelopeKeyOn(channel8->slots[1], egk_drum);
+ }
+ else
+ {
+ OPL3_EnvelopeKeyOff(channel8->slots[1], egk_drum);
+ }
+ /* tom */
+ if (chip->rhy & 0x04)
+ {
+ OPL3_EnvelopeKeyOn(channel8->slots[0], egk_drum);
+ }
+ else
+ {
+ OPL3_EnvelopeKeyOff(channel8->slots[0], egk_drum);
+ }
+ /* sd */
+ if (chip->rhy & 0x08)
+ {
+ OPL3_EnvelopeKeyOn(channel7->slots[1], egk_drum);
+ }
+ else
+ {
+ OPL3_EnvelopeKeyOff(channel7->slots[1], egk_drum);
+ }
+ /* bd */
+ if (chip->rhy & 0x10)
+ {
+ OPL3_EnvelopeKeyOn(channel6->slots[0], egk_drum);
+ OPL3_EnvelopeKeyOn(channel6->slots[1], egk_drum);
+ }
+ else
+ {
+ OPL3_EnvelopeKeyOff(channel6->slots[0], egk_drum);
+ OPL3_EnvelopeKeyOff(channel6->slots[1], egk_drum);
+ }
+ }
+ else
+ {
+ for (chnum = 6; chnum < 9; chnum++)
+ {
+ chip->channel[chnum].chtype = ch_2op;
+ OPL3_ChannelSetupAlg(&chip->channel[chnum]);
+ OPL3_EnvelopeKeyOff(chip->channel[chnum].slots[0], egk_drum);
+ OPL3_EnvelopeKeyOff(chip->channel[chnum].slots[1], egk_drum);
+ }
+ }
+}
+
+static void OPL3_ChannelWriteA0(opl3_channel *channel, Bit8u data)
+{
+ if (channel->chip->newm && channel->chtype == ch_4op2)
+ {
+ return;
+ }
+ channel->f_num = (channel->f_num & 0x300) | data;
+ channel->ksv = (channel->block << 1)
+ | ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01);
+ OPL3_EnvelopeUpdateKSL(channel->slots[0]);
+ OPL3_EnvelopeUpdateKSL(channel->slots[1]);
+ if (channel->chip->newm && channel->chtype == ch_4op)
+ {
+ channel->pair->f_num = channel->f_num;
+ channel->pair->ksv = channel->ksv;
+ OPL3_EnvelopeUpdateKSL(channel->pair->slots[0]);
+ OPL3_EnvelopeUpdateKSL(channel->pair->slots[1]);
+ }
+}
+
+static void OPL3_ChannelWriteB0(opl3_channel *channel, Bit8u data)
+{
+ if (channel->chip->newm && channel->chtype == ch_4op2)
+ {
+ return;
+ }
+ 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);
+ OPL3_EnvelopeUpdateKSL(channel->slots[0]);
+ OPL3_EnvelopeUpdateKSL(channel->slots[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;
+ OPL3_EnvelopeUpdateKSL(channel->pair->slots[0]);
+ OPL3_EnvelopeUpdateKSL(channel->pair->slots[1]);
+ }
+}
+
+static void OPL3_ChannelSetupAlg(opl3_channel *channel)
+{
+ if (channel->chtype == ch_drum)
+ {
+ if (channel->ch_num == 7 || channel->ch_num == 8)
+ {
+ channel->slots[0]->mod = &channel->chip->zeromod;
+ channel->slots[1]->mod = &channel->chip->zeromod;
+ return;
+ }
+ switch (channel->alg & 0x01)
+ {
+ case 0x00:
+ channel->slots[0]->mod = &channel->slots[0]->fbmod;
+ channel->slots[1]->mod = &channel->slots[0]->out;
+ break;
+ case 0x01:
+ channel->slots[0]->mod = &channel->slots[0]->fbmod;
+ channel->slots[1]->mod = &channel->chip->zeromod;
+ break;
+ }
+ return;
+ }
+ if (channel->alg & 0x08)
+ {
+ return;
+ }
+ 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 (channel->alg & 0x03)
+ {
+ case 0x00:
+ channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
+ channel->pair->slots[1]->mod = &channel->pair->slots[0]->out;
+ channel->slots[0]->mod = &channel->pair->slots[1]->out;
+ channel->slots[1]->mod = &channel->slots[0]->out;
+ channel->out[0] = &channel->slots[1]->out;
+ channel->out[1] = &channel->chip->zeromod;
+ channel->out[2] = &channel->chip->zeromod;
+ channel->out[3] = &channel->chip->zeromod;
+ break;
+ case 0x01:
+ channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
+ channel->pair->slots[1]->mod = &channel->pair->slots[0]->out;
+ channel->slots[0]->mod = &channel->chip->zeromod;
+ channel->slots[1]->mod = &channel->slots[0]->out;
+ channel->out[0] = &channel->pair->slots[1]->out;
+ channel->out[1] = &channel->slots[1]->out;
+ channel->out[2] = &channel->chip->zeromod;
+ channel->out[3] = &channel->chip->zeromod;
+ break;
+ case 0x02:
+ channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
+ channel->pair->slots[1]->mod = &channel->chip->zeromod;
+ channel->slots[0]->mod = &channel->pair->slots[1]->out;
+ channel->slots[1]->mod = &channel->slots[0]->out;
+ channel->out[0] = &channel->pair->slots[0]->out;
+ channel->out[1] = &channel->slots[1]->out;
+ channel->out[2] = &channel->chip->zeromod;
+ channel->out[3] = &channel->chip->zeromod;
+ break;
+ case 0x03:
+ channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
+ channel->pair->slots[1]->mod = &channel->chip->zeromod;
+ channel->slots[0]->mod = &channel->pair->slots[1]->out;
+ channel->slots[1]->mod = &channel->chip->zeromod;
+ channel->out[0] = &channel->pair->slots[0]->out;
+ channel->out[1] = &channel->slots[0]->out;
+ channel->out[2] = &channel->slots[1]->out;
+ channel->out[3] = &channel->chip->zeromod;
+ break;
+ }
+ }
+ else
+ {
+ switch (channel->alg & 0x01)
+ {
+ case 0x00:
+ channel->slots[0]->mod = &channel->slots[0]->fbmod;
+ channel->slots[1]->mod = &channel->slots[0]->out;
+ channel->out[0] = &channel->slots[1]->out;
+ channel->out[1] = &channel->chip->zeromod;
+ channel->out[2] = &channel->chip->zeromod;
+ channel->out[3] = &channel->chip->zeromod;
+ break;
+ case 0x01:
+ channel->slots[0]->mod = &channel->slots[0]->fbmod;
+ channel->slots[1]->mod = &channel->chip->zeromod;
+ channel->out[0] = &channel->slots[0]->out;
+ channel->out[1] = &channel->slots[1]->out;
+ channel->out[2] = &channel->chip->zeromod;
+ channel->out[3] = &channel->chip->zeromod;
+ break;
+ }
+ }
+}
+
+static void OPL3_ChannelWriteC0(opl3_channel *channel, Bit8u data)
+{
+ channel->fb = (data & 0x0e) >> 1;
+ channel->con = data & 0x01;
+ channel->alg = channel->con;
+ if (channel->chip->newm)
+ {
+ if (channel->chtype == ch_4op)
+ {
+ channel->pair->alg = 0x04 | (channel->con << 1) | (channel->pair->con);
+ channel->alg = 0x08;
+ OPL3_ChannelSetupAlg(channel->pair);
+ }
+ else if (channel->chtype == ch_4op2)
+ {
+ channel->alg = 0x04 | (channel->pair->con << 1) | (channel->con);
+ channel->pair->alg = 0x08;
+ OPL3_ChannelSetupAlg(channel);
+ }
+ else
+ {
+ OPL3_ChannelSetupAlg(channel);
+ }
+ }
+ else
+ {
+ OPL3_ChannelSetupAlg(channel);
+ }
+ if (channel->chip->newm)
+ {
+ channel->cha = ((data >> 4) & 0x01) ? ~0 : 0;
+ channel->chb = ((data >> 5) & 0x01) ? ~0 : 0;
+ }
+ else
+ {
+ channel->cha = channel->chb = (Bit16u)~0;
+ }
+}
+
+static void OPL3_ChannelKeyOn(opl3_channel *channel)
+{
+ if (channel->chip->newm)
+ {
+ if (channel->chtype == ch_4op)
+ {
+ OPL3_EnvelopeKeyOn(channel->slots[0], egk_norm);
+ OPL3_EnvelopeKeyOn(channel->slots[1], egk_norm);
+ OPL3_EnvelopeKeyOn(channel->pair->slots[0], egk_norm);
+ OPL3_EnvelopeKeyOn(channel->pair->slots[1], egk_norm);
+ }
+ else if (channel->chtype == ch_2op || channel->chtype == ch_drum)
+ {
+ OPL3_EnvelopeKeyOn(channel->slots[0], egk_norm);
+ OPL3_EnvelopeKeyOn(channel->slots[1], egk_norm);
+ }
+ }
+ else
+ {
+ OPL3_EnvelopeKeyOn(channel->slots[0], egk_norm);
+ OPL3_EnvelopeKeyOn(channel->slots[1], egk_norm);
+ }
+}
+
+static void OPL3_ChannelKeyOff(opl3_channel *channel)
+{
+ if (channel->chip->newm)
+ {
+ if (channel->chtype == ch_4op)
+ {
+ OPL3_EnvelopeKeyOff(channel->slots[0], egk_norm);
+ OPL3_EnvelopeKeyOff(channel->slots[1], egk_norm);
+ OPL3_EnvelopeKeyOff(channel->pair->slots[0], egk_norm);
+ OPL3_EnvelopeKeyOff(channel->pair->slots[1], egk_norm);
+ }
+ else if (channel->chtype == ch_2op || channel->chtype == ch_drum)
+ {
+ OPL3_EnvelopeKeyOff(channel->slots[0], egk_norm);
+ OPL3_EnvelopeKeyOff(channel->slots[1], egk_norm);
+ }
+ }
+ else
+ {
+ OPL3_EnvelopeKeyOff(channel->slots[0], egk_norm);
+ OPL3_EnvelopeKeyOff(channel->slots[1], egk_norm);
+ }
+}
+
+static void OPL3_ChannelSet4Op(opl3_chip *chip, Bit8u data)
+{
+ Bit8u bit;
+ Bit8u chnum;
+ for (bit = 0; bit < 6; bit++)
+ {
+ chnum = bit;
+ if (bit >= 3)
+ {
+ chnum += 9 - 3;
+ }
+ if ((data >> bit) & 0x01)
+ {
+ chip->channel[chnum].chtype = ch_4op;
+ chip->channel[chnum + 3].chtype = ch_4op2;
+ }
+ else
+ {
+ chip->channel[chnum].chtype = ch_2op;
+ chip->channel[chnum + 3].chtype = ch_2op;
+ }
+ }
+}
+
+static Bit16s OPL3_ClipSample(Bit32s sample)
+{
+ if (sample > 32767)
+ {
+ sample = 32767;
+ }
+ else if (sample < -32768)
+ {
+ sample = -32768;
+ }
+ return (Bit16s)sample;
+}
+
+void OPL3_Generate(opl3_chip *chip, Bit16s *buf)
+{
+ Bit8u ii;
+ Bit8u jj;
+ Bit16s accm;
+ Bit8u shift = 0;
+
+ buf[1] = OPL3_ClipSample(chip->mixbuff[1]);
+
+ for (ii = 0; ii < 15; ii++)
+ {
+ OPL3_SlotCalcFB(&chip->slot[ii]);
+ OPL3_EnvelopeCalc(&chip->slot[ii]);
+ OPL3_PhaseGenerate(&chip->slot[ii]);
+ OPL3_SlotGenerate(&chip->slot[ii]);
+ }
+
+ chip->mixbuff[0] = 0;
+ for (ii = 0; ii < 18; ii++)
+ {
+ accm = 0;
+ for (jj = 0; jj < 4; jj++)
+ {
+ accm += *chip->channel[ii].out[jj];
+ }
+ chip->mixbuff[0] += (Bit16s)(accm & chip->channel[ii].cha);
+ }
+
+ for (ii = 15; ii < 18; ii++)
+ {
+ OPL3_SlotCalcFB(&chip->slot[ii]);
+ OPL3_EnvelopeCalc(&chip->slot[ii]);
+ OPL3_PhaseGenerate(&chip->slot[ii]);
+ OPL3_SlotGenerate(&chip->slot[ii]);
+ }
+
+ buf[0] = OPL3_ClipSample(chip->mixbuff[0]);
+
+ for (ii = 18; ii < 33; ii++)
+ {
+ OPL3_SlotCalcFB(&chip->slot[ii]);
+ OPL3_EnvelopeCalc(&chip->slot[ii]);
+ OPL3_PhaseGenerate(&chip->slot[ii]);
+ OPL3_SlotGenerate(&chip->slot[ii]);
+ }
+
+ chip->mixbuff[1] = 0;
+ for (ii = 0; ii < 18; ii++)
+ {
+ accm = 0;
+ for (jj = 0; jj < 4; jj++)
+ {
+ accm += *chip->channel[ii].out[jj];
+ }
+ chip->mixbuff[1] += (Bit16s)(accm & chip->channel[ii].chb);
+ }
+
+ for (ii = 33; ii < 36; ii++)
+ {
+ OPL3_SlotCalcFB(&chip->slot[ii]);
+ OPL3_EnvelopeCalc(&chip->slot[ii]);
+ OPL3_PhaseGenerate(&chip->slot[ii]);
+ OPL3_SlotGenerate(&chip->slot[ii]);
+ }
+
+ if ((chip->timer & 0x3f) == 0x3f)
+ {
+ chip->tremolopos = (chip->tremolopos + 1) % 210;
+ }
+ if (chip->tremolopos < 105)
+ {
+ chip->tremolo = chip->tremolopos >> chip->tremoloshift;
+ }
+ else
+ {
+ chip->tremolo = (210 - chip->tremolopos) >> chip->tremoloshift;
+ }
+
+ if ((chip->timer & 0x3ff) == 0x3ff)
+ {
+ chip->vibpos = (chip->vibpos + 1) & 7;
+ }
+
+ chip->timer++;
+
+ chip->eg_add = 0;
+ if (chip->eg_timer)
+ {
+ while (shift < 36 && ((chip->eg_timer >> shift) & 1) == 0)
+ {
+ shift++;
+ }
+ if (shift > 12)
+ {
+ chip->eg_add = 0;
+ }
+ else
+ {
+ chip->eg_add = shift + 1;
+ }
+ }
+
+ if (chip->eg_timerrem || chip->eg_state)
+ {
+ if (chip->eg_timer == 0xfffffffff)
+ {
+ chip->eg_timer = 0;
+ chip->eg_timerrem = 1;
+ }
+ else
+ {
+ chip->eg_timer++;
+ chip->eg_timerrem = 0;
+ }
+ }
+
+ chip->eg_state ^= 1;
+
+ while (chip->writebuf[chip->writebuf_cur].time <= chip->writebuf_samplecnt)
+ {
+ if (!(chip->writebuf[chip->writebuf_cur].reg & 0x200))
+ {
+ break;
+ }
+ chip->writebuf[chip->writebuf_cur].reg &= 0x1ff;
+ OPL3_WriteReg(chip, chip->writebuf[chip->writebuf_cur].reg,
+ chip->writebuf[chip->writebuf_cur].data);
+ chip->writebuf_cur = (chip->writebuf_cur + 1) % OPL_WRITEBUF_SIZE;
+ }
+ chip->writebuf_samplecnt++;
+}
+
+void OPL3_GenerateResampled(opl3_chip *chip, Bit16s *buf)
+{
+ while (chip->samplecnt >= chip->rateratio)
+ {
+ chip->oldsamples[0] = chip->samples[0];
+ chip->oldsamples[1] = chip->samples[1];
+ OPL3_Generate(chip, chip->samples);
+ chip->samplecnt -= chip->rateratio;
+ }
+ buf[0] = (Bit16s)((chip->oldsamples[0] * (chip->rateratio - chip->samplecnt)
+ + chip->samples[0] * chip->samplecnt) / chip->rateratio);
+ buf[1] = (Bit16s)((chip->oldsamples[1] * (chip->rateratio - chip->samplecnt)
+ + chip->samples[1] * chip->samplecnt) / chip->rateratio);
+ chip->samplecnt += 1 << RSM_FRAC;
+}
+
+void OPL3_Reset(opl3_chip *chip, Bit32u samplerate)
+{
+ Bit8u slotnum;
+ Bit8u channum;
+
+ memset(chip, 0, sizeof(opl3_chip));
+ for (slotnum = 0; slotnum < 36; slotnum++)
+ {
+ chip->slot[slotnum].chip = chip;
+ chip->slot[slotnum].mod = &chip->zeromod;
+ chip->slot[slotnum].eg_rout = 0x1ff;
+ chip->slot[slotnum].eg_out = 0x1ff;
+ chip->slot[slotnum].eg_gen = envelope_gen_num_release;
+ chip->slot[slotnum].trem = (Bit8u*)&chip->zeromod;
+ chip->slot[slotnum].slot_num = slotnum;
+ }
+ for (channum = 0; channum < 18; channum++)
+ {
+ chip->channel[channum].slots[0] = &chip->slot[ch_slot[channum]];
+ chip->channel[channum].slots[1] = &chip->slot[ch_slot[channum] + 3];
+ chip->slot[ch_slot[channum]].channel = &chip->channel[channum];
+ chip->slot[ch_slot[channum] + 3].channel = &chip->channel[channum];
+ if ((channum % 9) < 3)
+ {
+ chip->channel[channum].pair = &chip->channel[channum + 3];
+ }
+ else if ((channum % 9) < 6)
+ {
+ chip->channel[channum].pair = &chip->channel[channum - 3];
+ }
+ chip->channel[channum].chip = chip;
+ chip->channel[channum].out[0] = &chip->zeromod;
+ chip->channel[channum].out[1] = &chip->zeromod;
+ chip->channel[channum].out[2] = &chip->zeromod;
+ chip->channel[channum].out[3] = &chip->zeromod;
+ chip->channel[channum].chtype = ch_2op;
+ chip->channel[channum].cha = 0xffff;
+ chip->channel[channum].chb = 0xffff;
+ chip->channel[channum].ch_num = channum;
+ OPL3_ChannelSetupAlg(&chip->channel[channum]);
+ }
+ chip->noise = 1;
+ chip->rateratio = (samplerate << RSM_FRAC) / 49716;
+ chip->tremoloshift = 4;
+ chip->vibshift = 1;
+}
+
+void OPL3_WriteReg(opl3_chip *chip, Bit16u reg, Bit8u v)
+{
+ Bit8u high = (reg >> 8) & 0x01;
+ Bit8u regm = reg & 0xff;
+ switch (regm & 0xf0)
+ {
+ case 0x00:
+ if (high)
+ {
+ switch (regm & 0x0f)
+ {
+ case 0x04:
+ OPL3_ChannelSet4Op(chip, v);
+ break;
+ case 0x05:
+ chip->newm = v & 0x01;
+ break;
+ }
+ }
+ else
+ {
+ switch (regm & 0x0f)
+ {
+ case 0x08:
+ chip->nts = (v >> 6) & 0x01;
+ break;
+ }
+ }
+ break;
+ case 0x20:
+ case 0x30:
+ if (ad_slot[regm & 0x1f] >= 0)
+ {
+ OPL3_SlotWrite20(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
+ }
+ break;
+ case 0x40:
+ case 0x50:
+ if (ad_slot[regm & 0x1f] >= 0)
+ {
+ OPL3_SlotWrite40(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
+ }
+ break;
+ case 0x60:
+ case 0x70:
+ if (ad_slot[regm & 0x1f] >= 0)
+ {
+ OPL3_SlotWrite60(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
+ }
+ break;
+ case 0x80:
+ case 0x90:
+ if (ad_slot[regm & 0x1f] >= 0)
+ {
+ OPL3_SlotWrite80(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
+ }
+ break;
+ case 0xe0:
+ case 0xf0:
+ if (ad_slot[regm & 0x1f] >= 0)
+ {
+ OPL3_SlotWriteE0(&chip->slot[18 * high + ad_slot[regm & 0x1f]], v);
+ }
+ break;
+ case 0xa0:
+ if ((regm & 0x0f) < 9)
+ {
+ OPL3_ChannelWriteA0(&chip->channel[9 * high + (regm & 0x0f)], v);
+ }
+ break;
+ case 0xb0:
+ if (regm == 0xbd && !high)
+ {
+ chip->tremoloshift = (((v >> 7) ^ 1) << 1) + 2;
+ chip->vibshift = ((v >> 6) & 0x01) ^ 1;
+ OPL3_ChannelUpdateRhythm(chip, v);
+ }
+ else if ((regm & 0x0f) < 9)
+ {
+ OPL3_ChannelWriteB0(&chip->channel[9 * high + (regm & 0x0f)], v);
+ if (v & 0x20)
+ {
+ OPL3_ChannelKeyOn(&chip->channel[9 * high + (regm & 0x0f)]);
+ }
+ else
+ {
+ OPL3_ChannelKeyOff(&chip->channel[9 * high + (regm & 0x0f)]);
+ }
+ }
+ break;
+ case 0xc0:
+ if ((regm & 0x0f) < 9)
+ {
+ OPL3_ChannelWriteC0(&chip->channel[9 * high + (regm & 0x0f)], v);
+ }
+ break;
+ }
+}
+
+void OPL3_WriteRegBuffered(opl3_chip *chip, Bit16u reg, Bit8u v)
+{
+ Bit64u time1, time2;
+
+ if (chip->writebuf[chip->writebuf_last].reg & 0x200)
+ {
+ OPL3_WriteReg(chip, chip->writebuf[chip->writebuf_last].reg & 0x1ff,
+ chip->writebuf[chip->writebuf_last].data);
+
+ chip->writebuf_cur = (chip->writebuf_last + 1) % OPL_WRITEBUF_SIZE;
+ chip->writebuf_samplecnt = chip->writebuf[chip->writebuf_last].time;
+ }
+
+ chip->writebuf[chip->writebuf_last].reg = reg | 0x200;
+ chip->writebuf[chip->writebuf_last].data = v;
+ time1 = chip->writebuf_lasttime + OPL_WRITEBUF_DELAY;
+ time2 = chip->writebuf_samplecnt;
+
+ if (time1 < time2)
+ {
+ time1 = time2;
+ }
+
+ chip->writebuf[chip->writebuf_last].time = time1;
+ chip->writebuf_lasttime = time1;
+ chip->writebuf_last = (chip->writebuf_last + 1) % OPL_WRITEBUF_SIZE;
+}
+
+void OPL3_GenerateStream(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples)
+{
+ Bit32u i;
+
+ for(i = 0; i < numsamples; i++)
+ {
+ OPL3_GenerateResampled(chip, sndptr);
+ sndptr += 2;
+ }
+}
+
+void OPL3_GenerateStreamMix(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples)
+{
+ Bit32u i;
+ Bit16s sample[2];
+
+ for(i = 0; i < numsamples; i++)
+ {
+ OPL3_GenerateResampled(chip, sample);
+ sndptr[0] += sample[0];
+ sndptr[1] += sample[1];
+ sndptr += 2;
+ }
+}
diff --git a/src/chips/nuked/nukedopl3.h b/src/chips/nuked/nukedopl3.h
new file mode 100644
index 0000000..ce748b1
--- /dev/null
+++ b/src/chips/nuked/nukedopl3.h
@@ -0,0 +1,160 @@
+/*
+ * Copyright (C) 2013-2018 Alexey Khokholov (Nuke.YKT)
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ *
+ * Nuked OPL3 emulator.
+ * Thanks:
+ * MAME Development Team(Jarek Burczynski, Tatsuyuki Satoh):
+ * Feedback and Rhythm part calculation information.
+ * forums.submarine.org.uk(carbon14, opl3):
+ * Tremolo and phase generator calculation information.
+ * OPLx decapsulated(Matthew Gambrell, Olli Niemitalo):
+ * OPL2 ROMs.
+ * siliconpr0n.org(John McMaster, digshadow):
+ * YMF262 and VRC VII decaps and die shots.
+ *
+ * version: 1.8
+ */
+
+#ifndef OPL_OPL3_H
+#define OPL_OPL3_H
+
+#include <inttypes.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define OPL_WRITEBUF_SIZE 1024
+#define OPL_WRITEBUF_DELAY 2
+
+typedef uintptr_t Bitu;
+typedef intptr_t Bits;
+typedef uint64_t Bit64u;
+typedef int64_t Bit64s;
+typedef uint32_t Bit32u;
+typedef int32_t Bit32s;
+typedef uint16_t Bit16u;
+typedef int16_t Bit16s;
+typedef uint8_t Bit8u;
+typedef int8_t Bit8s;
+
+typedef struct _opl3_slot opl3_slot;
+typedef struct _opl3_channel opl3_channel;
+typedef struct _opl3_chip opl3_chip;
+
+struct _opl3_slot {
+ opl3_channel *channel;
+ opl3_chip *chip;
+ Bit16s out;
+ Bit16s fbmod;
+ Bit16s *mod;
+ Bit16s prout;
+ Bit16s eg_rout;
+ Bit16s eg_out;
+ Bit8u eg_inc;
+ Bit8u eg_gen;
+ Bit8u eg_rate;
+ Bit8u eg_ksl;
+ Bit8u *trem;
+ Bit8u reg_vib;
+ Bit8u reg_type;
+ Bit8u reg_ksr;
+ Bit8u reg_mult;
+ Bit8u reg_ksl;
+ Bit8u reg_tl;
+ Bit8u reg_ar;
+ Bit8u reg_dr;
+ Bit8u reg_sl;
+ Bit8u reg_rr;
+ Bit8u reg_wf;
+ Bit8u key;
+ Bit32u pg_reset;
+ Bit32u pg_phase;
+ Bit16u pg_phase_out;
+ Bit8u slot_num;
+};
+
+struct _opl3_channel {
+ opl3_slot *slots[2];
+ opl3_channel *pair;
+ opl3_chip *chip;
+ Bit16s *out[4];
+ Bit8u chtype;
+ Bit16u f_num;
+ Bit8u block;
+ Bit8u fb;
+ Bit8u con;
+ Bit8u alg;
+ Bit8u ksv;
+ Bit16u cha, chb;
+ Bit8u ch_num;
+};
+
+typedef struct _opl3_writebuf {
+ Bit64u time;
+ Bit16u reg;
+ Bit8u data;
+} opl3_writebuf;
+
+struct _opl3_chip {
+ opl3_channel channel[18];
+ opl3_slot slot[36];
+ Bit16u timer;
+ Bit64u eg_timer;
+ Bit8u eg_timerrem;
+ Bit8u eg_state;
+ Bit8u eg_add;
+ Bit8u newm;
+ Bit8u nts;
+ Bit8u rhy;
+ Bit8u vibpos;
+ Bit8u vibshift;
+ Bit8u tremolo;
+ Bit8u tremolopos;
+ Bit8u tremoloshift;
+ Bit32u noise;
+ Bit16s zeromod;
+ Bit32s mixbuff[2];
+ Bit8u rm_hh_bit2;
+ Bit8u rm_hh_bit3;
+ Bit8u rm_hh_bit7;
+ Bit8u rm_hh_bit8;
+ Bit8u rm_tc_bit3;
+ Bit8u rm_tc_bit5;
+ /* OPL3L */
+ Bit32s rateratio;
+ Bit32s samplecnt;
+ Bit16s oldsamples[2];
+ Bit16s samples[2];
+
+ Bit64u writebuf_samplecnt;
+ Bit32u writebuf_cur;
+ Bit32u writebuf_last;
+ Bit64u writebuf_lasttime;
+ opl3_writebuf writebuf[OPL_WRITEBUF_SIZE];
+};
+
+void OPL3_Generate(opl3_chip *chip, Bit16s *buf);
+void OPL3_GenerateResampled(opl3_chip *chip, Bit16s *buf);
+void OPL3_Reset(opl3_chip *chip, Bit32u samplerate);
+void OPL3_WriteReg(opl3_chip *chip, Bit16u reg, Bit8u v);
+void OPL3_WriteRegBuffered(opl3_chip *chip, Bit16u reg, Bit8u v);
+void OPL3_GenerateStream(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples);
+void OPL3_GenerateStreamMix(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/src/chips/nuked/nukedopl3_174.c b/src/chips/nuked/nukedopl3_174.c
new file mode 100644
index 0000000..401089c
--- /dev/null
+++ b/src/chips/nuked/nukedopl3_174.c
@@ -0,0 +1,1391 @@
+/*
+ * Copyright (C) 2013-2016 Alexey Khokholov (Nuke.YKT)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Nuked OPL3 emulator.
+ * Thanks:
+ * MAME Development Team(Jarek Burczynski, Tatsuyuki Satoh):
+ * Feedback and Rhythm part calculation information.
+ * forums.submarine.org.uk(carbon14, opl3):
+ * Tremolo and phase generator calculation information.
+ * OPLx decapsulated(Matthew Gambrell, Olli Niemitalo):
+ * OPL2 ROMs.
+ *
+ * version: 1.7.4
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "nukedopl3_174.h"
+
+#define RSM_FRAC 10
+
+/* Channel types */
+
+enum {
+ ch_2op = 0,
+ ch_4op = 1,
+ ch_4op2 = 2,
+ ch_drum = 3
+};
+
+/* Envelope key types */
+
+enum {
+ egk_norm = 0x01,
+ egk_drum = 0x02
+};
+
+
+/*
+ * logsin table
+ */
+
+static const Bit16u logsinrom[512] = {
+ 0x859, 0x6c3, 0x607, 0x58b, 0x52e, 0x4e4, 0x4a6, 0x471,
+ 0x443, 0x41a, 0x3f5, 0x3d3, 0x3b5, 0x398, 0x37e, 0x365,
+ 0x34e, 0x339, 0x324, 0x311, 0x2ff, 0x2ed, 0x2dc, 0x2cd,
+ 0x2bd, 0x2af, 0x2a0, 0x293, 0x286, 0x279, 0x26d, 0x261,
+ 0x256, 0x24b, 0x240, 0x236, 0x22c, 0x222, 0x218, 0x20f,
+ 0x206, 0x1fd, 0x1f5, 0x1ec, 0x1e4, 0x1dc, 0x1d4, 0x1cd,
+ 0x1c5, 0x1be, 0x1b7, 0x1b0, 0x1a9, 0x1a2, 0x19b, 0x195,
+ 0x18f, 0x188, 0x182, 0x17c, 0x177, 0x171, 0x16b, 0x166,
+ 0x160, 0x15b, 0x155, 0x150, 0x14b, 0x146, 0x141, 0x13c,
+ 0x137, 0x133, 0x12e, 0x129, 0x125, 0x121, 0x11c, 0x118,
+ 0x114, 0x10f, 0x10b, 0x107, 0x103, 0x0ff, 0x0fb, 0x0f8,
+ 0x0f4, 0x0f0, 0x0ec, 0x0e9, 0x0e5, 0x0e2, 0x0de, 0x0db,
+ 0x0d7, 0x0d4, 0x0d1, 0x0cd, 0x0ca, 0x0c7, 0x0c4, 0x0c1,
+ 0x0be, 0x0bb, 0x0b8, 0x0b5, 0x0b2, 0x0af, 0x0ac, 0x0a9,
+ 0x0a7, 0x0a4, 0x0a1, 0x09f, 0x09c, 0x099, 0x097, 0x094,
+ 0x092, 0x08f, 0x08d, 0x08a, 0x088, 0x086, 0x083, 0x081,
+ 0x07f, 0x07d, 0x07a, 0x078, 0x076, 0x074, 0x072, 0x070,
+ 0x06e, 0x06c, 0x06a, 0x068, 0x066, 0x064, 0x062, 0x060,
+ 0x05e, 0x05c, 0x05b, 0x059, 0x057, 0x055, 0x053, 0x052,
+ 0x050, 0x04e, 0x04d, 0x04b, 0x04a, 0x048, 0x046, 0x045,
+ 0x043, 0x042, 0x040, 0x03f, 0x03e, 0x03c, 0x03b, 0x039,
+ 0x038, 0x037, 0x035, 0x034, 0x033, 0x031, 0x030, 0x02f,
+ 0x02e, 0x02d, 0x02b, 0x02a, 0x029, 0x028, 0x027, 0x026,
+ 0x025, 0x024, 0x023, 0x022, 0x021, 0x020, 0x01f, 0x01e,
+ 0x01d, 0x01c, 0x01b, 0x01a, 0x019, 0x018, 0x017, 0x017,
+ 0x016, 0x015, 0x014, 0x014, 0x013, 0x012, 0x011, 0x011,
+ 0x010, 0x00f, 0x00f, 0x00e, 0x00d, 0x00d, 0x00c, 0x00c,
+ 0x00b, 0x00a, 0x00a, 0x009, 0x009, 0x008, 0x008, 0x007,
+ 0x007, 0x007, 0x006, 0x006, 0x005, 0x005, 0x005, 0x004,
+ 0x004, 0x004, 0x003, 0x003, 0x003, 0x002, 0x002, 0x002,
+ 0x002, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,
+ 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000,
+ 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000,
+ 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x002,
+ 0x002, 0x002, 0x002, 0x003, 0x003, 0x003, 0x004, 0x004,
+ 0x004, 0x005, 0x005, 0x005, 0x006, 0x006, 0x007, 0x007,
+ 0x007, 0x008, 0x008, 0x009, 0x009, 0x00a, 0x00a, 0x00b,
+ 0x00c, 0x00c, 0x00d, 0x00d, 0x00e, 0x00f, 0x00f, 0x010,
+ 0x011, 0x011, 0x012, 0x013, 0x014, 0x014, 0x015, 0x016,
+ 0x017, 0x017, 0x018, 0x019, 0x01a, 0x01b, 0x01c, 0x01d,
+ 0x01e, 0x01f, 0x020, 0x021, 0x022, 0x023, 0x024, 0x025,
+ 0x026, 0x027, 0x028, 0x029, 0x02a, 0x02b, 0x02d, 0x02e,
+ 0x02f, 0x030, 0x031, 0x033, 0x034, 0x035, 0x037, 0x038,
+ 0x039, 0x03b, 0x03c, 0x03e, 0x03f, 0x040, 0x042, 0x043,
+ 0x045, 0x046, 0x048, 0x04a, 0x04b, 0x04d, 0x04e, 0x050,
+ 0x052, 0x053, 0x055, 0x057, 0x059, 0x05b, 0x05c, 0x05e,
+ 0x060, 0x062, 0x064, 0x066, 0x068, 0x06a, 0x06c, 0x06e,
+ 0x070, 0x072, 0x074, 0x076, 0x078, 0x07a, 0x07d, 0x07f,
+ 0x081, 0x083, 0x086, 0x088, 0x08a, 0x08d, 0x08f, 0x092,
+ 0x094, 0x097, 0x099, 0x09c, 0x09f, 0x0a1, 0x0a4, 0x0a7,
+ 0x0a9, 0x0ac, 0x0af, 0x0b2, 0x0b5, 0x0b8, 0x0bb, 0x0be,
+ 0x0c1, 0x0c4, 0x0c7, 0x0ca, 0x0cd, 0x0d1, 0x0d4, 0x0d7,
+ 0x0db, 0x0de, 0x0e2, 0x0e5, 0x0e9, 0x0ec, 0x0f0, 0x0f4,
+ 0x0f8, 0x0fb, 0x0ff, 0x103, 0x107, 0x10b, 0x10f, 0x114,
+ 0x118, 0x11c, 0x121, 0x125, 0x129, 0x12e, 0x133, 0x137,
+ 0x13c, 0x141, 0x146, 0x14b, 0x150, 0x155, 0x15b, 0x160,
+ 0x166, 0x16b, 0x171, 0x177, 0x17c, 0x182, 0x188, 0x18f,
+ 0x195, 0x19b, 0x1a2, 0x1a9, 0x1b0, 0x1b7, 0x1be, 0x1c5,
+ 0x1cd, 0x1d4, 0x1dc, 0x1e4, 0x1ec, 0x1f5, 0x1fd, 0x206,
+ 0x20f, 0x218, 0x222, 0x22c, 0x236, 0x240, 0x24b, 0x256,
+ 0x261, 0x26d, 0x279, 0x286, 0x293, 0x2a0, 0x2af, 0x2bd,
+ 0x2cd, 0x2dc, 0x2ed, 0x2ff, 0x311, 0x324, 0x339, 0x34e,
+ 0x365, 0x37e, 0x398, 0x3b5, 0x3d3, 0x3f5, 0x41a, 0x443,
+ 0x471, 0x4a6, 0x4e4, 0x52e, 0x58b, 0x607, 0x6c3, 0x859
+};
+
+/*
+ * exp table
+ */
+
+static const Bit16u exprom[256] = {
+ 0xff4, 0xfea, 0xfde, 0xfd4, 0xfc8, 0xfbe, 0xfb4, 0xfa8,
+ 0xf9e, 0xf92, 0xf88, 0xf7e, 0xf72, 0xf68, 0xf5c, 0xf52,
+ 0xf48, 0xf3e, 0xf32, 0xf28, 0xf1e, 0xf14, 0xf08, 0xefe,
+ 0xef4, 0xeea, 0xee0, 0xed4, 0xeca, 0xec0, 0xeb6, 0xeac,
+ 0xea2, 0xe98, 0xe8e, 0xe84, 0xe7a, 0xe70, 0xe66, 0xe5c,
+ 0xe52, 0xe48, 0xe3e, 0xe34, 0xe2a, 0xe20, 0xe16, 0xe0c,
+ 0xe04, 0xdfa, 0xdf0, 0xde6, 0xddc, 0xdd2, 0xdca, 0xdc0,
+ 0xdb6, 0xdac, 0xda4, 0xd9a, 0xd90, 0xd88, 0xd7e, 0xd74,
+ 0xd6a, 0xd62, 0xd58, 0xd50, 0xd46, 0xd3c, 0xd34, 0xd2a,
+ 0xd22, 0xd18, 0xd10, 0xd06, 0xcfe, 0xcf4, 0xcec, 0xce2,
+ 0xcda, 0xcd0, 0xcc8, 0xcbe, 0xcb6, 0xcae, 0xca4, 0xc9c,
+ 0xc92, 0xc8a, 0xc82, 0xc78, 0xc70, 0xc68, 0xc60, 0xc56,
+ 0xc4e, 0xc46, 0xc3c, 0xc34, 0xc2c, 0xc24, 0xc1c, 0xc12,
+ 0xc0a, 0xc02, 0xbfa, 0xbf2, 0xbea, 0xbe0, 0xbd8, 0xbd0,
+ 0xbc8, 0xbc0, 0xbb8, 0xbb0, 0xba8, 0xba0, 0xb98, 0xb90,
+ 0xb88, 0xb80, 0xb78, 0xb70, 0xb68, 0xb60, 0xb58, 0xb50,
+ 0xb48, 0xb40, 0xb38, 0xb32, 0xb2a, 0xb22, 0xb1a, 0xb12,
+ 0xb0a, 0xb02, 0xafc, 0xaf4, 0xaec, 0xae4, 0xade, 0xad6,
+ 0xace, 0xac6, 0xac0, 0xab8, 0xab0, 0xaa8, 0xaa2, 0xa9a,
+ 0xa92, 0xa8c, 0xa84, 0xa7c, 0xa76, 0xa6e, 0xa68, 0xa60,
+ 0xa58, 0xa52, 0xa4a, 0xa44, 0xa3c, 0xa36, 0xa2e, 0xa28,
+ 0xa20, 0xa18, 0xa12, 0xa0c, 0xa04, 0x9fe, 0x9f6, 0x9f0,
+ 0x9e8, 0x9e2, 0x9da, 0x9d4, 0x9ce, 0x9c6, 0x9c0, 0x9b8,
+ 0x9b2, 0x9ac, 0x9a4, 0x99e, 0x998, 0x990, 0x98a, 0x984,
+ 0x97c, 0x976, 0x970, 0x96a, 0x962, 0x95c, 0x956, 0x950,
+ 0x948, 0x942, 0x93c, 0x936, 0x930, 0x928, 0x922, 0x91c,
+ 0x916, 0x910, 0x90a, 0x904, 0x8fc, 0x8f6, 0x8f0, 0x8ea,
+ 0x8e4, 0x8de, 0x8d8, 0x8d2, 0x8cc, 0x8c6, 0x8c0, 0x8ba,
+ 0x8b4, 0x8ae, 0x8a8, 0x8a2, 0x89c, 0x896, 0x890, 0x88a,
+ 0x884, 0x87e, 0x878, 0x872, 0x86c, 0x866, 0x860, 0x85a,
+ 0x854, 0x850, 0x84a, 0x844, 0x83e, 0x838, 0x832, 0x82c,
+ 0x828, 0x822, 0x81c, 0x816, 0x810, 0x80c, 0x806, 0x800
+};
+
+/*
+ * freq mult table multiplied by 2
+ *
+ * 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 12, 12, 15, 15
+ */
+
+static const Bit8u mt[16] = {
+ 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 24, 24, 30, 30
+};
+
+/*
+ * ksl table
+ */
+
+static const Bit8u kslrom[16] = {
+ 0, 32, 40, 45, 48, 51, 53, 55, 56, 58, 59, 60, 61, 62, 63, 64
+};
+
+static const Bit8u kslshift[4] = {
+ 8, 1, 2, 0
+};
+
+/*
+ * envelope generator constants
+ */
+
+static const Bit8u eg_incstep[3][4][8] = {
+ {
+ { 0, 0, 0, 0, 0, 0, 0, 0 },
+ { 0, 0, 0, 0, 0, 0, 0, 0 },
+ { 0, 0, 0, 0, 0, 0, 0, 0 },
+ { 0, 0, 0, 0, 0, 0, 0, 0 }
+ },
+ {
+ { 0, 1, 0, 1, 0, 1, 0, 1 },
+ { 0, 1, 0, 1, 1, 1, 0, 1 },
+ { 0, 1, 1, 1, 0, 1, 1, 1 },
+ { 0, 1, 1, 1, 1, 1, 1, 1 }
+ },
+ {
+ { 1, 1, 1, 1, 1, 1, 1, 1 },
+ { 2, 2, 1, 1, 1, 1, 1, 1 },
+ { 2, 2, 1, 1, 2, 2, 1, 1 },
+ { 2, 2, 2, 2, 2, 2, 1, 1 }
+ }
+};
+
+static const Bit8u eg_incdesc[16] = {
+ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2
+};
+
+static const Bit8s eg_incsh[16] = {
+ 0, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, -1, -2
+};
+
+/*
+ * address decoding
+ */
+
+static const Bit8s ad_slot[0x20] = {
+ 0, 1, 2, 3, 4, 5, -1, -1, 6, 7, 8, 9, 10, 11, -1, -1,
+ 12, 13, 14, 15, 16, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
+};
+
+static const Bit8u ch_slot[18] = {
+ 0, 1, 2, 6, 7, 8, 12, 13, 14, 18, 19, 20, 24, 25, 26, 30, 31, 32
+};
+
+/*
+ * Envelope generator
+ */
+
+static void OPL3_EnvelopeGenOff(opl3_slot *slot);
+static void OPL3_EnvelopeGenAttack(opl3_slot *slot);
+static void OPL3_EnvelopeGenDecay(opl3_slot *slot);
+static void OPL3_EnvelopeGenSustain(opl3_slot *slot);
+static void OPL3_EnvelopeGenRelease(opl3_slot *slot);
+
+typedef void(*envelope_genfunc)(opl3_slot *slot);
+
+envelope_genfunc envelope_gen[5] = {
+ OPL3_EnvelopeGenOff,
+ OPL3_EnvelopeGenAttack,
+ OPL3_EnvelopeGenDecay,
+ OPL3_EnvelopeGenSustain,
+ OPL3_EnvelopeGenRelease
+};
+
+enum envelope_gen_num
+{
+ envelope_gen_num_off = 0,
+ envelope_gen_num_attack = 1,
+ envelope_gen_num_decay = 2,
+ envelope_gen_num_sustain = 3,
+ envelope_gen_num_release = 4
+};
+
+static Bit8u OPL3_EnvelopeCalcRate(opl3_slot *slot, Bit8u reg_rate)
+{
+ Bit8u rate;
+ if (reg_rate == 0x00)
+ {
+ return 0x00;
+ }
+ rate = (reg_rate << 2)
+ + (slot->reg_ksr ? slot->channel->ksv : (slot->channel->ksv >> 2));
+ if (rate > 0x3c)
+ {
+ rate = 0x3c;
+ }
+ return rate;
+}
+
+static void OPL3_EnvelopeUpdateKSL(opl3_slot *slot)
+{
+ Bit16s ksl = (kslrom[slot->channel->f_num >> 6] << 2)
+ - ((0x08 - slot->channel->block) << 5);
+ if (ksl < 0)
+ {
+ ksl = 0;
+ }
+ slot->eg_ksl = (Bit8u)ksl;
+}
+
+static void OPL3_EnvelopeUpdateRate(opl3_slot *slot)
+{
+ switch (slot->eg_gen)
+ {
+ case envelope_gen_num_off:
+ case envelope_gen_num_attack:
+ slot->eg_rate = OPL3_EnvelopeCalcRate(slot, slot->reg_ar);
+ break;
+ case envelope_gen_num_decay:
+ slot->eg_rate = OPL3_EnvelopeCalcRate(slot, slot->reg_dr);
+ break;
+ case envelope_gen_num_sustain:
+ case envelope_gen_num_release:
+ slot->eg_rate = OPL3_EnvelopeCalcRate(slot, slot->reg_rr);
+ break;
+ }
+}
+
+static void OPL3_EnvelopeGenOff(opl3_slot *slot)
+{
+ slot->eg_rout = 0x1ff;
+}
+
+static void OPL3_EnvelopeGenAttack(opl3_slot *slot)
+{
+ if (slot->eg_rout == 0x00)
+ {
+ slot->eg_gen = envelope_gen_num_decay;
+ OPL3_EnvelopeUpdateRate(slot);
+ return;
+ }
+ slot->eg_rout += ((~slot->eg_rout) * slot->eg_inc) >> 3;
+ if (slot->eg_rout < 0x00)
+ {
+ slot->eg_rout = 0x00;
+ }
+}
+
+static void OPL3_EnvelopeGenDecay(opl3_slot *slot)
+{
+ if (slot->eg_rout >= slot->reg_sl << 4)
+ {
+ slot->eg_gen = envelope_gen_num_sustain;
+ OPL3_EnvelopeUpdateRate(slot);
+ return;
+ }
+ slot->eg_rout += slot->eg_inc;
+}
+
+static void OPL3_EnvelopeGenSustain(opl3_slot *slot)
+{
+ if (!slot->reg_type)
+ {
+ OPL3_EnvelopeGenRelease(slot);
+ }
+}
+
+static void OPL3_EnvelopeGenRelease(opl3_slot *slot)
+{
+ if (slot->eg_rout >= 0x1ff)
+ {
+ slot->eg_gen = envelope_gen_num_off;
+ slot->eg_rout = 0x1ff;
+ OPL3_EnvelopeUpdateRate(slot);
+ return;
+ }
+ slot->eg_rout += slot->eg_inc;
+}
+
+static void OPL3_EnvelopeCalc(opl3_slot *slot)
+{
+ Bit8u rate_h, rate_l;
+ Bit8u inc = 0;
+ rate_h = slot->eg_rate >> 2;
+ rate_l = slot->eg_rate & 3;
+ if (eg_incsh[rate_h] > 0)
+ {
+ if ((slot->chip->timer & ((1 << eg_incsh[rate_h]) - 1)) == 0)
+ {
+ inc = eg_incstep[eg_incdesc[rate_h]][rate_l]
+ [((slot->chip->timer)>> eg_incsh[rate_h]) & 0x07];
+ }
+ }
+ else
+ {
+ inc = eg_incstep[eg_incdesc[rate_h]][rate_l]
+ [slot->chip->timer & 0x07] << (-eg_incsh[rate_h]);
+ }
+ slot->eg_inc = inc;
+ slot->eg_out = slot->eg_rout + (slot->reg_tl << 2)
+ + (slot->eg_ksl >> kslshift[slot->reg_ksl]) + *slot->trem;
+ if (slot->eg_out > 0x1ff) /* TODO: Remove this if possible */
+ {
+ slot->eg_out = 0x1ff;
+ }
+ slot->eg_out <<= 3;
+
+ envelope_gen[slot->eg_gen](slot);
+}
+
+static void OPL3_EnvelopeKeyOn(opl3_slot *slot, Bit8u type)
+{
+ if (!slot->key)
+ {
+ slot->eg_gen = envelope_gen_num_attack;
+ OPL3_EnvelopeUpdateRate(slot);
+ if ((slot->eg_rate >> 2) == 0x0f)
+ {
+ slot->eg_gen = envelope_gen_num_decay;
+ OPL3_EnvelopeUpdateRate(slot);
+ slot->eg_rout = 0x00;
+ }
+ slot->pg_phase = 0x00;
+ }
+ slot->key |= type;
+}
+
+static void OPL3_EnvelopeKeyOff(opl3_slot *slot, Bit8u type)
+{
+ if (slot->key)
+ {
+ slot->key &= (~type);
+ if (!slot->key)
+ {
+ slot->eg_gen = envelope_gen_num_release;
+ OPL3_EnvelopeUpdateRate(slot);
+ }
+ }
+}
+
+/*
+ * Phase Generator
+ */
+
+static void OPL3_PhaseGenerate(opl3_slot *slot)
+{
+ Bit16u f_num;
+ Bit32u basefreq;
+
+ f_num = slot->channel->f_num;
+ if (slot->reg_vib)
+ {
+ Bit8s range;
+ Bit8u vibpos;
+
+ range = (f_num >> 7) & 7;
+ vibpos = slot->chip->vibpos;
+
+ if (!(vibpos & 3))
+ {
+ range = 0;
+ }
+ else if (vibpos & 1)
+ {
+ range >>= 1;
+ }
+ range >>= slot->chip->vibshift;
+
+ if (vibpos & 4)
+ {
+ range = -range;
+ }
+ f_num += range;
+ }
+ basefreq = (f_num << slot->channel->block) >> 1;
+ slot->pg_phase += (basefreq * mt[slot->reg_mult]) >> 1;
+}
+
+/*
+ * Noise Generator
+ */
+
+static void OPL3_NoiseGenerate(opl3_chip *chip)
+{
+ if (chip->noise & 0x01)
+ {
+ chip->noise ^= 0x800302;
+ }
+ chip->noise >>= 1;
+}
+
+/*
+ * Slot
+ */
+
+static void OPL3_SlotWrite20(opl3_slot *slot, Bit8u data)
+{
+ if ((data >> 7) & 0x01)
+ {
+ slot->trem = &slot->chip->tremolo;
+ }
+ else
+ {
+ slot->trem = (Bit8u*)&slot->chip->zeromod;
+ }
+ slot->reg_vib = (data >> 6) & 0x01;
+ slot->reg_type = (data >> 5) & 0x01;
+ slot->reg_ksr = (data >> 4) & 0x01;
+ slot->reg_mult = data & 0x0f;
+ OPL3_EnvelopeUpdateRate(slot);
+}
+
+static void OPL3_SlotWrite40(opl3_slot *slot, Bit8u data)
+{
+ slot->reg_ksl = (data >> 6) & 0x03;
+ slot->reg_tl = data & 0x3f;
+ OPL3_EnvelopeUpdateKSL(slot);
+}
+
+static void OPL3_SlotWrite60(opl3_slot *slot, Bit8u data)
+{
+ slot->reg_ar = (data >> 4) & 0x0f;
+ slot->reg_dr = data & 0x0f;
+ OPL3_EnvelopeUpdateRate(slot);
+}
+
+static void OPL3_SlotWrite80(opl3_slot *slot, Bit8u data)
+{
+ slot->reg_sl = (data >> 4) & 0x0f;
+ if (slot->reg_sl == 0x0f)
+ {
+ slot->reg_sl = 0x1f;
+ }
+ slot->reg_rr = data & 0x0f;
+ OPL3_EnvelopeUpdateRate(slot);
+}
+
+static void OPL3_SlotWriteE0(opl3_slot *slot, Bit8u data)
+{
+ slot->reg_wf = data & 0x07;
+ if (slot->chip->newm == 0x00)
+ {
+ slot->reg_wf &= 0x03;
+ }
+
+ switch (slot->reg_wf)
+ {
+ case 1:
+ case 4:
+ case 5:
+ slot->maskzero = 0x200;
+ break;
+ case 3:
+ slot->maskzero = 0x100;
+ break;
+ default:
+ slot->maskzero = 0;
+ break;
+ }
+
+ switch (slot->reg_wf)
+ {
+ case 4:
+ slot->signpos = (31-8); /* sigext of (phase & 0x100) */
+ break;
+ case 0:
+ case 6:
+ case 7:
+ slot->signpos = (31-9); /* sigext of (phase & 0x200) */
+ break;
+ default:
+ slot->signpos = (31-16); /* set "neg" to zero */
+ break;
+ }
+
+ switch (slot->reg_wf)
+ {
+ case 4:
+ case 5:
+ slot->phaseshift = 1;
+ break;
+ case 6:
+ slot->phaseshift = 16; /* set phase to zero and flag for non-sin wave */
+ break;
+ case 7:
+ slot->phaseshift = 32; /* no shift (work by mod 32), but flag for non-sin wave */
+ break;
+ default:
+ slot->phaseshift = 0;
+ break;
+ }
+}
+
+static void OPL3_SlotGeneratePhase(opl3_slot *slot, Bit16u phase)
+{
+ Bit32u neg, level;
+ Bit8u phaseshift;
+
+ /* Fast paths for mute segments */
+ if (phase & slot->maskzero)
+ {
+ slot->out = 0;
+ return;
+ }
+
+ neg = (Bit32s)((Bit32u)phase << slot->signpos) >> 31;
+ phaseshift = slot->phaseshift;
+ level = slot->eg_out;
+
+ phase <<= phaseshift;
+ if (phaseshift <= 1)
+ {
+ level += logsinrom[phase & 0x1ff];
+ }
+ else
+ {
+ level += ((phase ^ neg) & 0x3ff) << 3;
+ }
+ slot->out = exprom[level & 0xff] >> (level >> 8) ^ neg;
+}
+
+static void OPL3_SlotGenerate(opl3_slot *slot)
+{
+ OPL3_SlotGeneratePhase(slot, (Bit16u)(slot->pg_phase >> 9) + *slot->mod);
+}
+
+static void OPL3_SlotGenerateZM(opl3_slot *slot)
+{
+ OPL3_SlotGeneratePhase(slot, (Bit16u)(slot->pg_phase >> 9));
+}
+
+static void OPL3_SlotCalcFB(opl3_slot *slot)
+{
+ if (slot->channel->fb != 0x00)
+ {
+ slot->fbmod = (slot->prout + slot->out) >> (0x09 - slot->channel->fb);
+ }
+ else
+ {
+ slot->fbmod = 0;
+ }
+ slot->prout = slot->out;
+}
+
+/*
+ * Channel
+ */
+
+static void OPL3_ChannelSetupAlg(opl3_channel *channel);
+
+static void OPL3_ChannelUpdateRhythm(opl3_chip *chip, Bit8u data)
+{
+ opl3_channel *channel6;
+ opl3_channel *channel7;
+ opl3_channel *channel8;
+ Bit8u chnum;
+
+ chip->rhy = data & 0x3f;
+ if (chip->rhy & 0x20)
+ {
+ channel6 = &chip->channel[6];
+ channel7 = &chip->channel[7];
+ channel8 = &chip->channel[8];
+ channel6->out[0] = &channel6->slots[1]->out;
+ channel6->out[1] = &channel6->slots[1]->out;
+ channel6->out[2] = &chip->zeromod;
+ channel6->out[3] = &chip->zeromod;
+ channel7->out[0] = &channel7->slots[0]->out;
+ channel7->out[1] = &channel7->slots[0]->out;
+ channel7->out[2] = &channel7->slots[1]->out;
+ channel7->out[3] = &channel7->slots[1]->out;
+ channel8->out[0] = &channel8->slots[0]->out;
+ channel8->out[1] = &channel8->slots[0]->out;
+ channel8->out[2] = &channel8->slots[1]->out;
+ channel8->out[3] = &channel8->slots[1]->out;
+ for (chnum = 6; chnum < 9; chnum++)
+ {
+ chip->channel[chnum].chtype = ch_drum;
+ }
+ OPL3_ChannelSetupAlg(channel6);
+ /*hh*/
+ if (chip->rhy & 0x01)
+ {
+ OPL3_EnvelopeKeyOn(channel7->slots[0], egk_drum);
+ }
+ else
+ {
+ OPL3_EnvelopeKeyOff(channel7->slots[0], egk_drum);
+ }
+ /*tc*/
+ if (chip->rhy & 0x02)
+ {
+ OPL3_EnvelopeKeyOn(channel8->slots[1], egk_drum);
+ }
+ else
+ {
+ OPL3_EnvelopeKeyOff(channel8->slots[1], egk_drum);
+ }
+ /*tom*/
+ if (chip->rhy & 0x04)
+ {
+ OPL3_EnvelopeKeyOn(channel8->slots[0], egk_drum);
+ }
+ else
+ {
+ OPL3_EnvelopeKeyOff(channel8->slots[0], egk_drum);
+ }
+ /*sd*/
+ if (chip->rhy & 0x08)
+ {
+ OPL3_EnvelopeKeyOn(channel7->slots[1], egk_drum);
+ }
+ else
+ {
+ OPL3_EnvelopeKeyOff(channel7->slots[1], egk_drum);
+ }
+ /*bd*/
+ if (chip->rhy & 0x10)
+ {
+ OPL3_EnvelopeKeyOn(channel6->slots[0], egk_drum);
+ OPL3_EnvelopeKeyOn(channel6->slots[1], egk_drum);
+ }
+ else
+ {
+ OPL3_EnvelopeKeyOff(channel6->slots[0], egk_drum);
+ OPL3_EnvelopeKeyOff(channel6->slots[1], egk_drum);
+ }
+ }
+ else
+ {
+ for (chnum = 6; chnum < 9; chnum++)
+ {
+ chip->channel[chnum].chtype = ch_2op;
+ OPL3_ChannelSetupAlg(&chip->channel[chnum]);
+ OPL3_EnvelopeKeyOff(chip->channel[chnum].slots[0], egk_drum);
+ OPL3_EnvelopeKeyOff(chip->channel[chnum].slots[1], egk_drum);
+ }
+ }
+}
+
+static void OPL3_ChannelWriteA0(opl3_channel *channel, Bit8u data)
+{
+ if (channel->chip->newm && channel->chtype == ch_4op2)
+ {
+ return;
+ }
+ channel->f_num = (channel->f_num & 0x300) | data;
+ channel->ksv = (channel->block << 1)
+ | ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01);
+ OPL3_EnvelopeUpdateKSL(channel->slots[0]);
+ OPL3_EnvelopeUpdateKSL(channel->slots[1]);
+ OPL3_EnvelopeUpdateRate(channel->slots[0]);
+ OPL3_EnvelopeUpdateRate(channel->slots[1]);
+ if (channel->chip->newm && channel->chtype == ch_4op)
+ {
+ channel->pair->f_num = channel->f_num;
+ channel->pair->ksv = channel->ksv;
+ OPL3_EnvelopeUpdateKSL(channel->pair->slots[0]);
+ OPL3_EnvelopeUpdateKSL(channel->pair->slots[1]);
+ OPL3_EnvelopeUpdateRate(channel->pair->slots[0]);
+ OPL3_EnvelopeUpdateRate(channel->pair->slots[1]);
+ }
+}
+
+static void OPL3_ChannelWriteB0(opl3_channel *channel, Bit8u data)
+{
+ if (channel->chip->newm && channel->chtype == ch_4op2)
+ {
+ return;
+ }
+ 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);
+ OPL3_EnvelopeUpdateKSL(channel->slots[0]);
+ OPL3_EnvelopeUpdateKSL(channel->slots[1]);
+ OPL3_EnvelopeUpdateRate(channel->slots[0]);
+ OPL3_EnvelopeUpdateRate(channel->slots[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;
+ OPL3_EnvelopeUpdateKSL(channel->pair->slots[0]);
+ OPL3_EnvelopeUpdateKSL(channel->pair->slots[1]);
+ OPL3_EnvelopeUpdateRate(channel->pair->slots[0]);
+ OPL3_EnvelopeUpdateRate(channel->pair->slots[1]);
+ }
+}
+
+static void OPL3_ChannelSetupAlg(opl3_channel *channel)
+{
+ if (channel->chtype == ch_drum)
+ {
+ switch (channel->alg & 0x01)
+ {
+ case 0x00:
+ channel->slots[0]->mod = &channel->slots[0]->fbmod;
+ channel->slots[1]->mod = &channel->slots[0]->out;
+ break;
+ case 0x01:
+ channel->slots[0]->mod = &channel->slots[0]->fbmod;
+ channel->slots[1]->mod = &channel->chip->zeromod;
+ break;
+ }
+ return;
+ }
+ if (channel->alg & 0x08)
+ {
+ return;
+ }
+ 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 (channel->alg & 0x03)
+ {
+ case 0x00:
+ channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
+ channel->pair->slots[1]->mod = &channel->pair->slots[0]->out;
+ channel->slots[0]->mod = &channel->pair->slots[1]->out;
+ channel->slots[1]->mod = &channel->slots[0]->out;
+ channel->out[0] = &channel->slots[1]->out;
+ channel->out[1] = &channel->chip->zeromod;
+ channel->out[2] = &channel->chip->zeromod;
+ channel->out[3] = &channel->chip->zeromod;
+ break;
+ case 0x01:
+ channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
+ channel->pair->slots[1]->mod = &channel->pair->slots[0]->out;
+ channel->slots[0]->mod = &channel->chip->zeromod;
+ channel->slots[1]->mod = &channel->slots[0]->out;
+ channel->out[0] = &channel->pair->slots[1]->out;
+ channel->out[1] = &channel->slots[1]->out;
+ channel->out[2] = &channel->chip->zeromod;
+ channel->out[3] = &channel->chip->zeromod;
+ break;
+ case 0x02:
+ channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
+ channel->pair->slots[1]->mod = &channel->chip->zeromod;
+ channel->slots[0]->mod = &channel->pair->slots[1]->out;
+ channel->slots[1]->mod = &channel->slots[0]->out;
+ channel->out[0] = &channel->pair->slots[0]->out;
+ channel->out[1] = &channel->slots[1]->out;
+ channel->out[2] = &channel->chip->zeromod;
+ channel->out[3] = &channel->chip->zeromod;
+ break;
+ case 0x03:
+ channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
+ channel->pair->slots[1]->mod = &channel->chip->zeromod;
+ channel->slots[0]->mod = &channel->pair->slots[1]->out;
+ channel->slots[1]->mod = &channel->chip->zeromod;
+ channel->out[0] = &channel->pair->slots[0]->out;
+ channel->out[1] = &channel->slots[0]->out;
+ channel->out[2] = &channel->slots[1]->out;
+ channel->out[3] = &channel->chip->zeromod;
+ break;
+ }
+ }
+ else
+ {
+ switch (channel->alg & 0x01)
+ {
+ case 0x00:
+ channel->slots[0]->mod = &channel->slots[0]->fbmod;
+ channel->slots[1]->mod = &channel->slots[0]->out;
+ channel->out[0] = &channel->slots[1]->out;
+ channel->out[1] = &channel->chip->zeromod;
+ channel->out[2] = &channel->chip->zeromod;
+ channel->out[3] = &channel->chip->zeromod;
+ break;
+ case 0x01:
+ channel->slots[0]->mod = &channel->slots[0]->fbmod;
+ channel->slots[1]->mod = &channel->chip->zeromod;
+ channel->out[0] = &channel->slots[0]->out;
+ channel->out[1] = &channel->slots[1]->out;
+ channel->out[2] = &channel->chip->zeromod;
+ channel->out[3] = &channel->chip->zeromod;
+ break;
+ }
+ }
+}
+
+static void OPL3_ChannelWriteC0(opl3_channel *channel, Bit8u data)
+{
+ channel->fb = (data & 0x0e) >> 1;
+ channel->con = data & 0x01;
+ channel->alg = channel->con;
+ if (channel->chip->newm)
+ {
+ if (channel->chtype == ch_4op)
+ {
+ channel->pair->alg = 0x04 | (channel->con << 1) | (channel->pair->con);
+ channel->alg = 0x08;
+ OPL3_ChannelSetupAlg(channel->pair);
+ }
+ else if (channel->chtype == ch_4op2)
+ {
+ channel->alg = 0x04 | (channel->pair->con << 1) | (channel->con);
+ channel->pair->alg = 0x08;
+ OPL3_ChannelSetupAlg(channel);
+ }
+ else
+ {
+ OPL3_ChannelSetupAlg(channel);
+ }
+ }
+ else
+ {
+ OPL3_ChannelSetupAlg(channel);
+ }
+ if (channel->chip->newm)
+ {
+ channel->cha = ((data >> 4) & 0x01) ? ~0 : 0;
+ channel->chb = ((data >> 5) & 0x01) ? ~0 : 0;
+ }
+ else
+ {
+ channel->cha = channel->chb = ~0;
+ }
+}
+
+static void OPL3_ChannelKeyOn(opl3_channel *channel)
+{
+ if (channel->chip->newm)
+ {
+ if (channel->chtype == ch_4op)
+ {
+ OPL3_EnvelopeKeyOn(channel->slots[0], egk_norm);
+ OPL3_EnvelopeKeyOn(channel->slots[1], egk_norm);
+ OPL3_EnvelopeKeyOn(channel->pair->slots[0], egk_norm);
+ OPL3_EnvelopeKeyOn(channel->pair->slots[1], egk_norm);
+ }
+ else if (channel->chtype == ch_2op || channel->chtype == ch_drum)
+ {
+ OPL3_EnvelopeKeyOn(channel->slots[0], egk_norm);
+ OPL3_EnvelopeKeyOn(channel->slots[1], egk_norm);
+ }
+ }
+ else
+ {
+ OPL3_EnvelopeKeyOn(channel->slots[0], egk_norm);
+ OPL3_EnvelopeKeyOn(channel->slots[1], egk_norm);
+ }
+}
+
+static void OPL3_ChannelKeyOff(opl3_channel *channel)
+{
+ if (channel->chip->newm)
+ {
+ if (channel->chtype == ch_4op)
+ {
+ OPL3_EnvelopeKeyOff(channel->slots[0], egk_norm);
+ OPL3_EnvelopeKeyOff(channel->slots[1], egk_norm);
+ OPL3_EnvelopeKeyOff(channel->pair->slots[0], egk_norm);
+ OPL3_EnvelopeKeyOff(channel->pair->slots[1], egk_norm);
+ }
+ else if (channel->chtype == ch_2op || channel->chtype == ch_drum)
+ {
+ OPL3_EnvelopeKeyOff(channel->slots[0], egk_norm);
+ OPL3_EnvelopeKeyOff(channel->slots[1], egk_norm);
+ }
+ }
+ else
+ {
+ OPL3_EnvelopeKeyOff(channel->slots[0], egk_norm);
+ OPL3_EnvelopeKeyOff(channel->slots[1], egk_norm);
+ }
+}
+
+static void OPL3_ChannelSet4Op(opl3_chip *chip, Bit8u data)
+{
+ Bit8u bit;
+ Bit8u chnum;
+ for (bit = 0; bit < 6; bit++)
+ {
+ chnum = bit;
+ if (bit >= 3)
+ {
+ chnum += 9 - 3;
+ }
+ if ((data >> bit) & 0x01)
+ {
+ chip->channel[chnum].chtype = ch_4op;
+ chip->channel[chnum + 3].chtype = ch_4op2;
+ }
+ else
+ {
+ chip->channel[chnum].chtype = ch_2op;
+ chip->channel[chnum + 3].chtype = ch_2op;
+ }
+ }
+}
+
+static Bit16s OPL3_ClipSample(Bit32s sample)
+{
+ if (sample > 32767)
+ {
+ sample = 32767;
+ }
+ else if (sample < -32768)
+ {
+ sample = -32768;
+ }
+ return (Bit16s)sample;
+}
+
+static void OPL3_GenerateRhythm1(opl3_chip *chip)
+{
+ opl3_channel *channel6;
+ opl3_channel *channel7;
+ opl3_channel *channel8;
+ Bit16u phase14;
+ Bit16u phase17;
+ Bit16u phase;
+ Bit16u phasebit;
+
+ channel6 = &chip->channel[6];
+ channel7 = &chip->channel[7];
+ channel8 = &chip->channel[8];
+ OPL3_SlotGenerate(channel6->slots[0]);
+ phase14 = (channel7->slots[0]->pg_phase >> 9) & 0x3ff;
+ phase17 = (channel8->slots[1]->pg_phase >> 9) & 0x3ff;
+ phase = 0x00;
+ /*hh tc phase bit*/
+ phasebit = ((phase14 & 0x08) | (((phase14 >> 5) ^ phase14) & 0x04)
+ | (((phase17 >> 2) ^ phase17) & 0x08)) ? 0x01 : 0x00;
+ /*hh*/
+ phase = (phasebit << 9)
+ | (0x34 << ((phasebit ^ (chip->noise & 0x01)) << 1));
+ OPL3_SlotGeneratePhase(channel7->slots[0], phase);
+ /*tt*/
+ OPL3_SlotGenerateZM(channel8->slots[0]);
+}
+
+static void OPL3_GenerateRhythm2(opl3_chip *chip)
+{
+ opl3_channel *channel6;
+ opl3_channel *channel7;
+ opl3_channel *channel8;
+ Bit16u phase14;
+ Bit16u phase17;
+ Bit16u phase;
+ Bit16u phasebit;
+
+ channel6 = &chip->channel[6];
+ channel7 = &chip->channel[7];
+ channel8 = &chip->channel[8];
+ OPL3_SlotGenerate(channel6->slots[1]);
+ phase14 = (channel7->slots[0]->pg_phase >> 9) & 0x3ff;
+ phase17 = (channel8->slots[1]->pg_phase >> 9) & 0x3ff;
+ phase = 0x00;
+ /*hh tc phase bit*/
+ phasebit = ((phase14 & 0x08) | (((phase14 >> 5) ^ phase14) & 0x04)
+ | (((phase17 >> 2) ^ phase17) & 0x08)) ? 0x01 : 0x00;
+ /*sd*/
+ phase = (0x100 << ((phase14 >> 8) & 0x01)) ^ ((chip->noise & 0x01) << 8);
+ OPL3_SlotGeneratePhase(channel7->slots[1], phase);
+ /*tc*/
+ phase = 0x100 | (phasebit << 9);
+ OPL3_SlotGeneratePhase(channel8->slots[1], phase);
+}
+
+void OPL3v17_Generate(opl3_chip *chip, Bit16s *buf)
+{
+ Bit8u ii;
+ Bit8u jj;
+ Bit16s accm;
+
+ buf[1] = OPL3_ClipSample(chip->mixbuff[1]);
+
+ for (ii = 0; ii < 12; ii++)
+ {
+ OPL3_SlotCalcFB(&chip->chipslot[ii]);
+ OPL3_PhaseGenerate(&chip->chipslot[ii]);
+ OPL3_EnvelopeCalc(&chip->chipslot[ii]);
+ OPL3_SlotGenerate(&chip->chipslot[ii]);
+ }
+
+ for (ii = 12; ii < 15; ii++)
+ {
+ OPL3_SlotCalcFB(&chip->chipslot[ii]);
+ OPL3_PhaseGenerate(&chip->chipslot[ii]);
+ OPL3_EnvelopeCalc(&chip->chipslot[ii]);
+ }
+
+ if (chip->rhy & 0x20)
+ {
+ OPL3_GenerateRhythm1(chip);
+ }
+ else
+ {
+ OPL3_SlotGenerate(&chip->chipslot[12]);
+ OPL3_SlotGenerate(&chip->chipslot[13]);
+ OPL3_SlotGenerate(&chip->chipslot[14]);
+ }
+
+ chip->mixbuff[0] = 0;
+ for (ii = 0; ii < 18; ii++)
+ {
+ accm = 0;
+ for (jj = 0; jj < 4; jj++)
+ {
+ accm += *chip->channel[ii].out[jj];
+ }
+ chip->mixbuff[0] += (Bit16s)(accm & chip->channel[ii].cha);
+ }
+
+ for (ii = 15; ii < 18; ii++)
+ {
+ OPL3_SlotCalcFB(&chip->chipslot[ii]);
+ OPL3_PhaseGenerate(&chip->chipslot[ii]);
+ OPL3_EnvelopeCalc(&chip->chipslot[ii]);
+ }
+
+ if (chip->rhy & 0x20)
+ {
+ OPL3_GenerateRhythm2(chip);
+ }
+ else
+ {
+ OPL3_SlotGenerate(&chip->chipslot[15]);
+ OPL3_SlotGenerate(&chip->chipslot[16]);
+ OPL3_SlotGenerate(&chip->chipslot[17]);
+ }
+
+ buf[0] = OPL3_ClipSample(chip->mixbuff[0]);
+
+ for (ii = 18; ii < 33; ii++)
+ {
+ OPL3_SlotCalcFB(&chip->chipslot[ii]);
+ OPL3_PhaseGenerate(&chip->chipslot[ii]);
+ OPL3_EnvelopeCalc(&chip->chipslot[ii]);
+ OPL3_SlotGenerate(&chip->chipslot[ii]);
+ }
+
+ chip->mixbuff[1] = 0;
+ for (ii = 0; ii < 18; ii++)
+ {
+ accm = 0;
+ for (jj = 0; jj < 4; jj++)
+ {
+ accm += *chip->channel[ii].out[jj];
+ }
+ chip->mixbuff[1] += (Bit16s)(accm & chip->channel[ii].chb);
+ }
+
+ for (ii = 33; ii < 36; ii++)
+ {
+ OPL3_SlotCalcFB(&chip->chipslot[ii]);
+ OPL3_PhaseGenerate(&chip->chipslot[ii]);
+ OPL3_EnvelopeCalc(&chip->chipslot[ii]);
+ OPL3_SlotGenerate(&chip->chipslot[ii]);
+ }
+
+ OPL3_NoiseGenerate(chip);
+
+ if ((chip->timer & 0x3f) == 0x3f)
+ {
+ chip->tremolopos = (chip->tremolopos + 1) % 210;
+ }
+ if (chip->tremolopos < 105)
+ {
+ chip->tremolo = chip->tremolopos >> chip->tremoloshift;
+ }
+ else
+ {
+ chip->tremolo = (210 - chip->tremolopos) >> chip->tremoloshift;
+ }
+
+ if ((chip->timer & 0x3ff) == 0x3ff)
+ {
+ chip->vibpos = (chip->vibpos + 1) & 7;
+ }
+
+ chip->timer++;
+
+ while (chip->writebuf[chip->writebuf_cur].time <= chip->writebuf_samplecnt)
+ {
+ if (!(chip->writebuf[chip->writebuf_cur].reg & 0x200))
+ {
+ break;
+ }
+ chip->writebuf[chip->writebuf_cur].reg &= 0x1ff;
+ OPL3v17_WriteReg(chip, chip->writebuf[chip->writebuf_cur].reg,
+ chip->writebuf[chip->writebuf_cur].data);
+ chip->writebuf_cur = (chip->writebuf_cur + 1) % OPL_WRITEBUF_SIZE;
+ }
+ chip->writebuf_samplecnt++;
+}
+
+void OPL3v17_GenerateResampled(opl3_chip *chip, Bit16s *buf)
+{
+ while (chip->samplecnt >= chip->rateratio)
+ {
+ chip->oldsamples[0] = chip->samples[0];
+ chip->oldsamples[1] = chip->samples[1];
+ OPL3v17_Generate(chip, chip->samples);
+ chip->samplecnt -= chip->rateratio;
+ }
+ buf[0] = (Bit16s)((chip->oldsamples[0] * (chip->rateratio - chip->samplecnt)
+ + chip->samples[0] * chip->samplecnt) / chip->rateratio);
+ buf[1] = (Bit16s)((chip->oldsamples[1] * (chip->rateratio - chip->samplecnt)
+ + chip->samples[1] * chip->samplecnt) / chip->rateratio);
+ chip->samplecnt += 1 << RSM_FRAC;
+}
+
+void OPL3v17_Reset(opl3_chip *chip, Bit32u samplerate)
+{
+ Bit8u slotnum;
+ Bit8u channum;
+
+ memset(chip, 0, sizeof(opl3_chip));
+ for (slotnum = 0; slotnum < 36; slotnum++)
+ {
+ chip->chipslot[slotnum].chip = chip;
+ chip->chipslot[slotnum].mod = &chip->zeromod;
+ chip->chipslot[slotnum].eg_rout = 0x1ff;
+ chip->chipslot[slotnum].eg_out = 0x1ff << 3;
+ chip->chipslot[slotnum].eg_gen = envelope_gen_num_off;
+ chip->chipslot[slotnum].trem = (Bit8u*)&chip->zeromod;
+ chip->chipslot[slotnum].signpos = (31-9); /* for wf=0 need use sigext of (phase & 0x200) */
+ }
+ for (channum = 0; channum < 18; channum++)
+ {
+ chip->channel[channum].slots[0] = &chip->chipslot[ch_slot[channum]];
+ chip->channel[channum].slots[1] = &chip->chipslot[ch_slot[channum] + 3];
+ chip->chipslot[ch_slot[channum]].channel = &chip->channel[channum];
+ chip->chipslot[ch_slot[channum] + 3].channel = &chip->channel[channum];
+ if ((channum % 9) < 3)
+ {
+ chip->channel[channum].pair = &chip->channel[channum + 3];
+ }
+ else if ((channum % 9) < 6)
+ {
+ chip->channel[channum].pair = &chip->channel[channum - 3];
+ }
+ chip->channel[channum].chip = chip;
+ chip->channel[channum].out[0] = &chip->zeromod;
+ chip->channel[channum].out[1] = &chip->zeromod;
+ chip->channel[channum].out[2] = &chip->zeromod;
+ chip->channel[channum].out[3] = &chip->zeromod;
+ chip->channel[channum].chtype = ch_2op;
+ chip->channel[channum].cha = ~0;
+ chip->channel[channum].chb = ~0;
+ OPL3_ChannelSetupAlg(&chip->channel[channum]);
+ }
+ chip->noise = 0x306600;
+ chip->rateratio = (samplerate << RSM_FRAC) / 49716;
+ chip->tremoloshift = 4;
+ chip->vibshift = 1;
+}
+
+void OPL3v17_WriteReg(opl3_chip *chip, Bit16u reg, Bit8u v)
+{
+ Bit8u high = (reg >> 8) & 0x01;
+ Bit8u regm = reg & 0xff;
+ switch (regm & 0xf0)
+ {
+ case 0x00:
+ if (high)
+ {
+ switch (regm & 0x0f)
+ {
+ case 0x04:
+ OPL3_ChannelSet4Op(chip, v);
+ break;
+ case 0x05:
+ chip->newm = v & 0x01;
+ break;
+ }
+ }
+ else
+ {
+ switch (regm & 0x0f)
+ {
+ case 0x08:
+ chip->nts = (v >> 6) & 0x01;
+ break;
+ }
+ }
+ break;
+ case 0x20:
+ case 0x30:
+ if (ad_slot[regm & 0x1f] >= 0)
+ {
+ OPL3_SlotWrite20(&chip->chipslot[18 * high + ad_slot[regm & 0x1f]], v);
+ }
+ break;
+ case 0x40:
+ case 0x50:
+ if (ad_slot[regm & 0x1f] >= 0)
+ {
+ OPL3_SlotWrite40(&chip->chipslot[18 * high + ad_slot[regm & 0x1f]], v);
+ }
+ break;
+ case 0x60:
+ case 0x70:
+ if (ad_slot[regm & 0x1f] >= 0)
+ {
+ OPL3_SlotWrite60(&chip->chipslot[18 * high + ad_slot[regm & 0x1f]], v);
+ }
+ break;
+ case 0x80:
+ case 0x90:
+ if (ad_slot[regm & 0x1f] >= 0)
+ {
+ OPL3_SlotWrite80(&chip->chipslot[18 * high + ad_slot[regm & 0x1f]], v);
+ }
+ break;
+ case 0xe0:
+ case 0xf0:
+ if (ad_slot[regm & 0x1f] >= 0)
+ {
+ OPL3_SlotWriteE0(&chip->chipslot[18 * high + ad_slot[regm & 0x1f]], v);
+ }
+ break;
+ case 0xa0:
+ if ((regm & 0x0f) < 9)
+ {
+ OPL3_ChannelWriteA0(&chip->channel[9 * high + (regm & 0x0f)], v);
+ }
+ break;
+ case 0xb0:
+ if (regm == 0xbd && !high)
+ {
+ chip->tremoloshift = (((v >> 7) ^ 1) << 1) + 2;
+ chip->vibshift = ((v >> 6) & 0x01) ^ 1;
+ OPL3_ChannelUpdateRhythm(chip, v);
+ }
+ else if ((regm & 0x0f) < 9)
+ {
+ OPL3_ChannelWriteB0(&chip->channel[9 * high + (regm & 0x0f)], v);
+ if (v & 0x20)
+ {
+ OPL3_ChannelKeyOn(&chip->channel[9 * high + (regm & 0x0f)]);
+ }
+ else
+ {
+ OPL3_ChannelKeyOff(&chip->channel[9 * high + (regm & 0x0f)]);
+ }
+ }
+ break;
+ case 0xc0:
+ if ((regm & 0x0f) < 9)
+ {
+ OPL3_ChannelWriteC0(&chip->channel[9 * high + (regm & 0x0f)], v);
+ }
+ break;
+ }
+}
+
+void OPL3v17_WriteRegBuffered(opl3_chip *chip, Bit16u reg, Bit8u v)
+{
+ Bit64u time1, time2;
+
+ if (chip->writebuf[chip->writebuf_last].reg & 0x200)
+ {
+ OPL3v17_WriteReg(chip, chip->writebuf[chip->writebuf_last].reg & 0x1ff,
+ chip->writebuf[chip->writebuf_last].data);
+
+ chip->writebuf_cur = (chip->writebuf_last + 1) % OPL_WRITEBUF_SIZE;
+ chip->writebuf_samplecnt = chip->writebuf[chip->writebuf_last].time;
+ }
+
+ chip->writebuf[chip->writebuf_last].reg = reg | 0x200;
+ chip->writebuf[chip->writebuf_last].data = v;
+ time1 = chip->writebuf_lasttime + OPL_WRITEBUF_DELAY;
+ time2 = chip->writebuf_samplecnt;
+
+ if (time1 < time2)
+ {
+ time1 = time2;
+ }
+
+ chip->writebuf[chip->writebuf_last].time = time1;
+ chip->writebuf_lasttime = time1;
+ chip->writebuf_last = (chip->writebuf_last + 1) % OPL_WRITEBUF_SIZE;
+}
+
+void OPL3v17_GenerateStream(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples)
+{
+ Bit32u i;
+
+ for(i = 0; i < numsamples; i++)
+ {
+ OPL3v17_GenerateResampled(chip, sndptr);
+ sndptr += 2;
+ }
+}
+
+#define OPL3_MIN(A, B) (((A) > (B)) ? (B) : (A))
+#define OPL3_MAX(A, B) (((A) < (B)) ? (B) : (A))
+#define OPL3_CLAMP(V, MIN, MAX) OPL3_MAX(OPL3_MIN(V, MAX), MIN)
+
+void OPL3v17_GenerateStreamMix(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples)
+{
+ Bit32u i;
+ Bit16s sample[2];
+ Bit32s mix[2];
+
+ for(i = 0; i < numsamples; i++)
+ {
+ OPL3v17_GenerateResampled(chip, sample);
+ mix[0] = sndptr[0] + sample[0];
+ mix[1] = sndptr[1] + sample[1];
+ sndptr[0] = OPL3_CLAMP(mix[0], INT16_MIN, INT16_MAX);
+ sndptr[1] = OPL3_CLAMP(mix[1], INT16_MIN, INT16_MAX);
+ sndptr += 2;
+ }
+}
+
diff --git a/src/chips/nuked/nukedopl3_174.h b/src/chips/nuked/nukedopl3_174.h
new file mode 100644
index 0000000..43e4a6e
--- /dev/null
+++ b/src/chips/nuked/nukedopl3_174.h
@@ -0,0 +1,154 @@
+/*
+ * Copyright (C) 2013-2016 Alexey Khokholov (Nuke.YKT)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Nuked OPL3 emulator.
+ * Thanks:
+ * MAME Development Team(Jarek Burczynski, Tatsuyuki Satoh):
+ * Feedback and Rhythm part calculation information.
+ * forums.submarine.org.uk(carbon14, opl3):
+ * Tremolo and phase generator calculation information.
+ * OPLx decapsulated(Matthew Gambrell, Olli Niemitalo):
+ * OPL2 ROMs.
+ *
+ * version: 1.7.4
+ */
+
+#ifndef OPL_OPL3_H
+#define OPL_OPL3_H
+
+#include <inttypes.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+#define OPL_WRITEBUF_SIZE 1024
+#define OPL_WRITEBUF_DELAY 2
+
+typedef uintptr_t Bitu;
+typedef intptr_t Bits;
+typedef uint64_t Bit64u;
+typedef int64_t Bit64s;
+typedef uint32_t Bit32u;
+typedef int32_t Bit32s;
+typedef uint16_t Bit16u;
+typedef int16_t Bit16s;
+typedef uint8_t Bit8u;
+typedef int8_t Bit8s;
+
+typedef struct _opl3_slot opl3_slot;
+typedef struct _opl3_channel opl3_channel;
+typedef struct _opl3_chip opl3_chip;
+
+struct _opl3_slot {
+ opl3_channel *channel;
+ opl3_chip *chip;
+ Bit16s out;
+ Bit16s fbmod;
+ Bit16s *mod;
+ Bit16s prout;
+ Bit16s eg_rout;
+ Bit16s eg_out;
+ Bit8u eg_inc;
+ Bit8u eg_gen;
+ Bit8u eg_rate;
+ Bit8u eg_ksl;
+ Bit8u *trem;
+ Bit8u reg_vib;
+ Bit8u reg_type;
+ Bit8u reg_ksr;
+ Bit8u reg_mult;
+ Bit8u reg_ksl;
+ Bit8u reg_tl;
+ Bit8u reg_ar;
+ Bit8u reg_dr;
+ Bit8u reg_sl;
+ Bit8u reg_rr;
+ Bit8u reg_wf;
+ Bit8u key;
+ Bit32u pg_phase;
+ Bit32u timer;
+
+ Bit16u maskzero;
+ Bit8u signpos;
+ Bit8u phaseshift;
+};
+
+struct _opl3_channel {
+ opl3_slot *slots[2];
+ opl3_channel *pair;
+ opl3_chip *chip;
+ Bit16s *out[4];
+ Bit8u chtype;
+ Bit16u f_num;
+ Bit8u block;
+ Bit8u fb;
+ Bit8u con;
+ Bit8u alg;
+ Bit8u ksv;
+ Bit16u cha, chb;
+};
+
+typedef struct _opl3_writebuf {
+ Bit64u time;
+ Bit16u reg;
+ Bit8u data;
+} opl3_writebuf;
+
+struct _opl3_chip {
+ opl3_channel channel[18];
+ opl3_slot chipslot[36];
+ Bit16u timer;
+ Bit8u newm;
+ Bit8u nts;
+ Bit8u rhy;
+ Bit8u vibpos;
+ Bit8u vibshift;
+ Bit8u tremolo;
+ Bit8u tremolopos;
+ Bit8u tremoloshift;
+ Bit32u noise;
+ Bit16s zeromod;
+ Bit32s mixbuff[2];
+ /* OPL3L */
+ Bit32s rateratio;
+ Bit32s samplecnt;
+ Bit16s oldsamples[2];
+ Bit16s samples[2];
+
+ Bit64u writebuf_samplecnt;
+ Bit32u writebuf_cur;
+ Bit32u writebuf_last;
+ Bit64u writebuf_lasttime;
+ opl3_writebuf writebuf[OPL_WRITEBUF_SIZE];
+};
+
+void OPL3v17_Generate(opl3_chip *chip, Bit16s *buf);
+void OPL3v17_GenerateResampled(opl3_chip *chip, Bit16s *buf);
+void OPL3v17_Reset(opl3_chip *chip, Bit32u samplerate);
+void OPL3v17_WriteReg(opl3_chip *chip, Bit16u reg, Bit8u v);
+void OPL3v17_WriteRegBuffered(opl3_chip *chip, Bit16u reg, Bit8u v);
+void OPL3v17_GenerateStream(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples);
+void OPL3v17_GenerateStreamMix(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/src/nukedopl3.c b/src/chips/nuked/old/nukedopl3.c
index 6ae1eb7..6ae1eb7 100644
--- a/src/nukedopl3.c
+++ b/src/chips/nuked/old/nukedopl3.c
diff --git a/src/nukedopl3.h b/src/chips/nuked/old/nukedopl3.h
index 254f275..254f275 100644
--- a/src/nukedopl3.h
+++ b/src/chips/nuked/old/nukedopl3.h
diff --git a/src/chips/nuked_opl3.cpp b/src/chips/nuked_opl3.cpp
new file mode 100644
index 0000000..7833227
--- /dev/null
+++ b/src/chips/nuked_opl3.cpp
@@ -0,0 +1,67 @@
+#include "nuked_opl3.h"
+#include "nuked/nukedopl3.h"
+#include <cstring>
+
+NukedOPL3::NukedOPL3() :
+ OPLChipBase()
+{
+ m_chip = new opl3_chip;
+ reset(m_rate);
+}
+
+NukedOPL3::NukedOPL3(const NukedOPL3 &c):
+ OPLChipBase(c)
+{
+ m_chip = new opl3_chip;
+ std::memset(m_chip, 0, sizeof(opl3_chip));
+ reset(c.m_rate);
+}
+
+NukedOPL3::~NukedOPL3()
+{
+ opl3_chip *chip_r = reinterpret_cast<opl3_chip*>(m_chip);
+ delete chip_r;
+}
+
+void NukedOPL3::setRate(uint32_t rate)
+{
+ OPLChipBase::setRate(rate);
+ opl3_chip *chip_r = reinterpret_cast<opl3_chip*>(m_chip);
+ std::memset(chip_r, 0, sizeof(opl3_chip));
+ OPL3_Reset(chip_r, rate);
+}
+
+void NukedOPL3::reset()
+{
+ setRate(m_rate);
+}
+
+void NukedOPL3::reset(uint32_t rate)
+{
+ setRate(rate);
+}
+
+void NukedOPL3::writeReg(uint16_t addr, uint8_t data)
+{
+ opl3_chip *chip_r = reinterpret_cast<opl3_chip*>(m_chip);
+ OPL3_WriteReg(chip_r, addr, data);
+}
+
+int NukedOPL3::generate(int16_t *output, size_t frames)
+{
+ opl3_chip *chip_r = reinterpret_cast<opl3_chip*>(m_chip);
+ OPL3_GenerateStream(chip_r, output, (Bit32u)frames);
+ return (int)frames;
+}
+
+int NukedOPL3::generateAndMix(int16_t *output, size_t frames)
+{
+ opl3_chip *chip_r = reinterpret_cast<opl3_chip*>(m_chip);
+ OPL3_GenerateStreamMix(chip_r, output, (Bit32u)frames);
+ return (int)frames;
+}
+
+const char *NukedOPL3::emulatorName()
+{
+ return "Nuked OPL3 (v 1.8)";
+}
diff --git a/src/chips/nuked_opl3.h b/src/chips/nuked_opl3.h
new file mode 100644
index 0000000..db665fe
--- /dev/null
+++ b/src/chips/nuked_opl3.h
@@ -0,0 +1,23 @@
+#ifndef NUKED_OPL3_H
+#define NUKED_OPL3_H
+
+#include "opl_chip_base.h"
+
+class NukedOPL3 final : public OPLChipBase
+{
+ void *m_chip;
+public:
+ NukedOPL3();
+ NukedOPL3(const NukedOPL3 &c);
+ virtual ~NukedOPL3() override;
+
+ virtual void setRate(uint32_t rate) override;
+ virtual void reset() override;
+ virtual void reset(uint32_t rate) override;
+ virtual void writeReg(uint16_t addr, uint8_t data) override;
+ virtual int generate(int16_t *output, size_t frames) override;
+ virtual int generateAndMix(int16_t *output, size_t frames) override;
+ virtual const char *emulatorName() override;
+};
+
+#endif // NUKED_OPL3_H
diff --git a/src/chips/nuked_opl3_v174.cpp b/src/chips/nuked_opl3_v174.cpp
new file mode 100644
index 0000000..7188e83
--- /dev/null
+++ b/src/chips/nuked_opl3_v174.cpp
@@ -0,0 +1,67 @@
+#include "nuked_opl3_v174.h"
+#include "nuked/nukedopl3_174.h"
+#include <cstring>
+
+NukedOPL3v174::NukedOPL3v174() :
+ OPLChipBase()
+{
+ m_chip = new opl3_chip;
+ reset(m_rate);
+}
+
+NukedOPL3v174::NukedOPL3v174(const NukedOPL3v174 &c):
+ OPLChipBase(c)
+{
+ m_chip = new opl3_chip;
+ std::memset(m_chip, 0, sizeof(opl3_chip));
+ reset(c.m_rate);
+}
+
+NukedOPL3v174::~NukedOPL3v174()
+{
+ opl3_chip *chip_r = reinterpret_cast<opl3_chip*>(m_chip);
+ delete chip_r;
+}
+
+void NukedOPL3v174::setRate(uint32_t rate)
+{
+ OPLChipBase::setRate(rate);
+ opl3_chip *chip_r = reinterpret_cast<opl3_chip*>(m_chip);
+ std::memset(chip_r, 0, sizeof(opl3_chip));
+ OPL3v17_Reset(chip_r, rate);
+}
+
+void NukedOPL3v174::reset()
+{
+ setRate(m_rate);
+}
+
+void NukedOPL3v174::reset(uint32_t rate)
+{
+ setRate(rate);
+}
+
+void NukedOPL3v174::writeReg(uint16_t addr, uint8_t data)
+{
+ opl3_chip *chip_r = reinterpret_cast<opl3_chip*>(m_chip);
+ OPL3v17_WriteReg(chip_r, addr, data);
+}
+
+int NukedOPL3v174::generate(int16_t *output, size_t frames)
+{
+ opl3_chip *chip_r = reinterpret_cast<opl3_chip*>(m_chip);
+ OPL3v17_GenerateStream(chip_r, output, (Bit32u)frames);
+ return (int)frames;
+}
+
+int NukedOPL3v174::generateAndMix(int16_t *output, size_t frames)
+{
+ opl3_chip *chip_r = reinterpret_cast<opl3_chip*>(m_chip);
+ OPL3v17_GenerateStreamMix(chip_r, output, (Bit32u)frames);
+ return (int)frames;
+}
+
+const char *NukedOPL3v174::emulatorName()
+{
+ return "Nuked OPL3 (v 1.7.4)";
+}
diff --git a/src/chips/nuked_opl3_v174.h b/src/chips/nuked_opl3_v174.h
new file mode 100644
index 0000000..cf23187
--- /dev/null
+++ b/src/chips/nuked_opl3_v174.h
@@ -0,0 +1,23 @@
+#ifndef NUKED_OPL3174_H
+#define NUKED_OPL3174_H
+
+#include "opl_chip_base.h"
+
+class NukedOPL3v174 final : public OPLChipBase
+{
+ void *m_chip;
+public:
+ NukedOPL3v174();
+ NukedOPL3v174(const NukedOPL3v174 &c);
+ virtual ~NukedOPL3v174() override;
+
+ virtual void setRate(uint32_t rate) override;
+ virtual void reset() override;
+ virtual void reset(uint32_t rate) override;
+ virtual void writeReg(uint16_t addr, uint8_t data) override;
+ virtual int generate(int16_t *output, size_t frames) override;
+ virtual int generateAndMix(int16_t *output, size_t frames) override;
+ virtual const char *emulatorName() override;
+};
+
+#endif // NUKED_OPL3174_H
diff --git a/src/chips/opl_chip_base.cpp b/src/chips/opl_chip_base.cpp
new file mode 100644
index 0000000..65d4b73
--- /dev/null
+++ b/src/chips/opl_chip_base.cpp
@@ -0,0 +1,22 @@
+#include "opl_chip_base.h"
+
+OPLChipBase::OPLChipBase() :
+ m_rate(44100)
+{}
+
+OPLChipBase::OPLChipBase(const OPLChipBase &c):
+ m_rate(c.m_rate)
+{}
+
+OPLChipBase::~OPLChipBase()
+{}
+
+void OPLChipBase::setRate(uint32_t rate)
+{
+ m_rate = rate;
+}
+
+void OPLChipBase::reset(uint32_t rate)
+{
+ setRate(rate);
+}
diff --git a/src/chips/opl_chip_base.h b/src/chips/opl_chip_base.h
new file mode 100644
index 0000000..aa6934b
--- /dev/null
+++ b/src/chips/opl_chip_base.h
@@ -0,0 +1,25 @@
+#ifndef ONP_CHIP_BASE_H
+#define ONP_CHIP_BASE_H
+
+#include <stdint.h>
+#include <stddef.h>
+
+class OPLChipBase
+{
+protected:
+ uint32_t m_rate;
+public:
+ OPLChipBase();
+ OPLChipBase(const OPLChipBase &c);
+ virtual ~OPLChipBase();
+
+ virtual void setRate(uint32_t rate);
+ virtual void reset() = 0;
+ virtual void reset(uint32_t rate);
+ virtual void writeReg(uint16_t addr, uint8_t data) = 0;
+ virtual int generate(int16_t *output, size_t frames) = 0;
+ virtual int generateAndMix(int16_t *output, size_t frames) = 0;
+ virtual const char* emulatorName() = 0;
+};
+
+#endif // ONP_CHIP_BASE_H
diff --git a/utils/midiplay/adlmidiplay.cpp b/utils/midiplay/adlmidiplay.cpp
index 69b3698..3734b13 100644
--- a/utils/midiplay/adlmidiplay.cpp
+++ b/utils/midiplay/adlmidiplay.cpp
@@ -195,6 +195,11 @@ int main(int argc, char **argv)
" -s Enables scaling of modulator volumes\n"
" -nl Quit without looping\n"
" -w Write WAV file rather than playing\n"
+ #ifndef HARDWARE_OPL3
+ " --emu-nuked Uses Nuked OPL3 v 1.7.4 emulator\n"
+ " --emu-nuked8 Uses Nuked OPL3 v 1.8 emulator\n"
+ " --emu-dosbox Uses DosBox 0.74 OPL3 emulator\n"
+ #endif
"\n"
"Where <bank> - number of embeeded bank or filepath to custom WOPL bank file\n"
"\n"
@@ -282,6 +287,10 @@ int main(int argc, char **argv)
bool recordWave = false;
int loopEnabled = 1;
#endif
+ #ifndef HARDWARE_OPL3
+ int emulator = ADLMIDI_EMU_NUKED;
+ #endif
+
while(argc > 2)
{
bool had_option = false;
@@ -290,16 +299,29 @@ int main(int argc, char **argv)
adl_setPercMode(myDevice, 1);//Turn on AdLib percussion mode
else if(!std::strcmp("-v", argv[2]))
adl_setHVibrato(myDevice, 1);//Turn on deep vibrato
+
#ifndef OUTPUT_WAVE_ONLY
else if(!std::strcmp("-w", argv[2]))
recordWave = true;//Record library output into WAV file
#endif
+
else if(!std::strcmp("-t", argv[2]))
adl_setHTremolo(myDevice, 1);//Turn on deep tremolo
+
#ifndef OUTPUT_WAVE_ONLY
else if(!std::strcmp("-nl", argv[2]))
loopEnabled = 0; //Enable loop
#endif
+
+ #ifndef HARDWARE_OPL3
+ else if(!std::strcmp("--emu-nuked", argv[2]))
+ emulator = ADLMIDI_EMU_NUKED;
+ else if(!std::strcmp("--emu-nuked8", argv[2]))
+ emulator = ADLMIDI_EMU_NUKED_8;
+ else if(!std::strcmp("--emu-dosbox", argv[2]))
+ emulator = ADLMIDI_EMU_DOSBOX;
+ #endif
+
else if(!std::strcmp("-s", argv[2]))
adl_setScaleModulators(myDevice, 1);//Turn on modulators scaling by volume
else break;
@@ -321,10 +343,14 @@ int main(int argc, char **argv)
adl_setRawEventHook(myDevice, debugPrintEvent, NULL);
#endif
+ #ifndef HARDWARE_OPL3
+ adl_switchEmulator(myDevice, emulator);
+ #endif
+
#ifdef HARDWARE_OPL3
std::fprintf(stdout, " - Hardware OPL3 chip in use\n");
#else
- std::fprintf(stdout, " - %s OPL3 Emulator in use\n", adl_emulatorName());
+ std::fprintf(stdout, " - %s Emulator in use\n", adl_chipEmulatorName(myDevice));
#endif
#if !defined(HARDWARE_OPL3) && !defined(OUTPUT_WAVE_ONLY)