WIP: adding ESFM (ESS ES1488/ESS ES1868 etc.) system

This commit is contained in:
Kagamiin~ 2023-10-14 08:10:37 -03:00
parent 256140bc32
commit 779afcb97a
24 changed files with 4705 additions and 167 deletions

1
.gitignore vendored
View file

@ -35,3 +35,4 @@ res/docpdf/manual.pdf
res/docpdf/.venv
res/docpdf/htmldoc/
res/furnace.appdata.xml
compile_commands.json

View file

@ -1,5 +1,7 @@
cmake_minimum_required(VERSION 3.0)
set(CMAKE_EXPORT_COMPILE_COMMANDS on)
if (APPLE)
set(MACOSX_DEPLOYMENT_TARGET 10.9)
endif()
@ -454,6 +456,8 @@ extern/Nuked-PSG/ympsg.c
extern/opm/opm.c
extern/Nuked-OPLL/opll.c
extern/opl/opl3.c
extern/ESFMu/esfm.c
extern/ESFMu/esfm_registers.c
src/pch.cpp
@ -660,6 +664,7 @@ src/engine/platform/pv1000.cpp
src/engine/platform/k053260.cpp
src/engine/platform/ted.cpp
src/engine/platform/c140.cpp
src/engine/platform/esfm.cpp
src/engine/platform/pcmdac.cpp
src/engine/platform/dummy.cpp

2
extern/ESFMu/.clangd vendored Normal file
View file

@ -0,0 +1,2 @@
CompileFlags:
Add: ["-xc", "-Wall", "-Wextra"]

1
extern/ESFMu/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
test_scripts/*

504
extern/ESFMu/LICENSE vendored Normal file
View file

@ -0,0 +1,504 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 2.1, February 1999
Copyright (C) 1991, 1999 Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
[This is the first released version of the Lesser GPL. It also counts
as the successor of the GNU Library Public License, version 2, hence
the version number 2.1.]
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
Licenses are intended to guarantee your freedom to share and change
free software--to make sure the software is free for all its users.
This license, the Lesser General Public License, applies to some
specially designated software packages--typically libraries--of the
Free Software Foundation and other authors who decide to use it. You
can use it too, but we suggest you first think carefully about whether
this license or the ordinary General Public License is the better
strategy to use in any particular case, based on the explanations below.
When we speak of free software, we are referring to freedom of use,
not price. Our General Public Licenses are designed to make sure that
you have the freedom to distribute copies of free software (and charge
for this service if you wish); that you receive source code or can get
it if you want it; that you can change the software and use pieces of
it in new free programs; and that you are informed that you can do
these things.
To protect your rights, we need to make restrictions that forbid
distributors to deny you these rights or to ask you to surrender these
rights. These restrictions translate to certain responsibilities for
you if you distribute copies of the library or if you modify it.
For example, if you distribute copies of the library, whether gratis
or for a fee, you must give the recipients all the rights that we gave
you. You must make sure that they, too, receive or can get the source
code. If you link other code with the library, you must provide
complete object files to the recipients, so that they can relink them
with the library after making changes to the library and recompiling
it. And you must show them these terms so they know their rights.
We protect your rights with a two-step method: (1) we copyright the
library, and (2) we offer you this license, which gives you legal
permission to copy, distribute and/or modify the library.
To protect each distributor, we want to make it very clear that
there is no warranty for the free library. Also, if the library is
modified by someone else and passed on, the recipients should know
that what they have is not the original version, so that the original
author's reputation will not be affected by problems that might be
introduced by others.
Finally, software patents pose a constant threat to the existence of
any free program. We wish to make sure that a company cannot
effectively restrict the users of a free program by obtaining a
restrictive license from a patent holder. Therefore, we insist that
any patent license obtained for a version of the library must be
consistent with the full freedom of use specified in this license.
Most GNU software, including some libraries, is covered by the
ordinary GNU General Public License. This license, the GNU Lesser
General Public License, applies to certain designated libraries, and
is quite different from the ordinary General Public License. We use
this license for certain libraries in order to permit linking those
libraries into non-free programs.
When a program is linked with a library, whether statically or using
a shared library, the combination of the two is legally speaking a
combined work, a derivative of the original library. The ordinary
General Public License therefore permits such linking only if the
entire combination fits its criteria of freedom. The Lesser General
Public License permits more lax criteria for linking other code with
the library.
We call this license the "Lesser" General Public License because it
does Less to protect the user's freedom than the ordinary General
Public License. It also provides other free software developers Less
of an advantage over competing non-free programs. These disadvantages
are the reason we use the ordinary General Public License for many
libraries. However, the Lesser license provides advantages in certain
special circumstances.
For example, on rare occasions, there may be a special need to
encourage the widest possible use of a certain library, so that it becomes
a de-facto standard. To achieve this, non-free programs must be
allowed to use the library. A more frequent case is that a free
library does the same job as widely used non-free libraries. In this
case, there is little to gain by limiting the free library to free
software only, so we use the Lesser General Public License.
In other cases, permission to use a particular library in non-free
programs enables a greater number of people to use a large body of
free software. For example, permission to use the GNU C Library in
non-free programs enables many more people to use the whole GNU
operating system, as well as its variant, the GNU/Linux operating
system.
Although the Lesser General Public License is Less protective of the
users' freedom, it does ensure that the user of a program that is
linked with the Library has the freedom and the wherewithal to run
that program using a modified version of the Library.
The precise terms and conditions for copying, distribution and
modification follow. Pay close attention to the difference between a
"work based on the library" and a "work that uses the library". The
former contains code derived from the library, whereas the latter must
be combined with the library in order to run.
GNU LESSER GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License Agreement applies to any software library or other
program which contains a notice placed by the copyright holder or
other authorized party saying it may be distributed under the terms of
this Lesser General Public License (also called "this License").
Each licensee is addressed as "you".
A "library" means a collection of software functions and/or data
prepared so as to be conveniently linked with application programs
(which use some of those functions and data) to form executables.
The "Library", below, refers to any such software library or work
which has been distributed under these terms. A "work based on the
Library" means either the Library or any derivative work under
copyright law: that is to say, a work containing the Library or a
portion of it, either verbatim or with modifications and/or translated
straightforwardly into another language. (Hereinafter, translation is
included without limitation in the term "modification".)
"Source code" for a work means the preferred form of the work for
making modifications to it. For a library, complete source code means
all the source code for all modules it contains, plus any associated
interface definition files, plus the scripts used to control compilation
and installation of the library.
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running a program using the Library is not restricted, and output from
such a program is covered only if its contents constitute a work based
on the Library (independent of the use of the Library in a tool for
writing it). Whether that is true depends on what the Library does
and what the program that uses the Library does.
1. You may copy and distribute verbatim copies of the Library's
complete source code as you receive it, in any medium, provided that
you conspicuously and appropriately publish on each copy an
appropriate copyright notice and disclaimer of warranty; keep intact
all the notices that refer to this License and to the absence of any
warranty; and distribute a copy of this License along with the
Library.
You may charge a fee for the physical act of transferring a copy,
and you may at your option offer warranty protection in exchange for a
fee.
2. You may modify your copy or copies of the Library or any portion
of it, thus forming a work based on the Library, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) The modified work must itself be a software library.
b) You must cause the files modified to carry prominent notices
stating that you changed the files and the date of any change.
c) You must cause the whole of the work to be licensed at no
charge to all third parties under the terms of this License.
d) If a facility in the modified Library refers to a function or a
table of data to be supplied by an application program that uses
the facility, other than as an argument passed when the facility
is invoked, then you must make a good faith effort to ensure that,
in the event an application does not supply such function or
table, the facility still operates, and performs whatever part of
its purpose remains meaningful.
(For example, a function in a library to compute square roots has
a purpose that is entirely well-defined independent of the
application. Therefore, Subsection 2d requires that any
application-supplied function or table used by this function must
be optional: if the application does not supply it, the square
root function must still compute square roots.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Library,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Library, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote
it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Library.
In addition, mere aggregation of another work not based on the Library
with the Library (or with a work based on the Library) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may opt to apply the terms of the ordinary GNU General Public
License instead of this License to a given copy of the Library. To do
this, you must alter all the notices that refer to this License, so
that they refer to the ordinary GNU General Public License, version 2,
instead of to this License. (If a newer version than version 2 of the
ordinary GNU General Public License has appeared, then you can specify
that version instead if you wish.) Do not make any other change in
these notices.
Once this change is made in a given copy, it is irreversible for
that copy, so the ordinary GNU General Public License applies to all
subsequent copies and derivative works made from that copy.
This option is useful when you wish to copy part of the code of
the Library into a program that is not a library.
4. You may copy and distribute the Library (or a portion or
derivative of it, under Section 2) in object code or executable form
under the terms of Sections 1 and 2 above provided that you accompany
it with the complete corresponding machine-readable source code, which
must be distributed under the terms of Sections 1 and 2 above on a
medium customarily used for software interchange.
If distribution of object code is made by offering access to copy
from a designated place, then offering equivalent access to copy the
source code from the same place satisfies the requirement to
distribute the source code, even though third parties are not
compelled to copy the source along with the object code.
5. A program that contains no derivative of any portion of the
Library, but is designed to work with the Library by being compiled or
linked with it, is called a "work that uses the Library". Such a
work, in isolation, is not a derivative work of the Library, and
therefore falls outside the scope of this License.
However, linking a "work that uses the Library" with the Library
creates an executable that is a derivative of the Library (because it
contains portions of the Library), rather than a "work that uses the
library". The executable is therefore covered by this License.
Section 6 states terms for distribution of such executables.
When a "work that uses the Library" uses material from a header file
that is part of the Library, the object code for the work may be a
derivative work of the Library even though the source code is not.
Whether this is true is especially significant if the work can be
linked without the Library, or if the work is itself a library. The
threshold for this to be true is not precisely defined by law.
If such an object file uses only numerical parameters, data
structure layouts and accessors, and small macros and small inline
functions (ten lines or less in length), then the use of the object
file is unrestricted, regardless of whether it is legally a derivative
work. (Executables containing this object code plus portions of the
Library will still fall under Section 6.)
Otherwise, if the work is a derivative of the Library, you may
distribute the object code for the work under the terms of Section 6.
Any executables containing that work also fall under Section 6,
whether or not they are linked directly with the Library itself.
6. As an exception to the Sections above, you may also combine or
link a "work that uses the Library" with the Library to produce a
work containing portions of the Library, and distribute that work
under terms of your choice, provided that the terms permit
modification of the work for the customer's own use and reverse
engineering for debugging such modifications.
You must give prominent notice with each copy of the work that the
Library is used in it and that the Library and its use are covered by
this License. You must supply a copy of this License. If the work
during execution displays copyright notices, you must include the
copyright notice for the Library among them, as well as a reference
directing the user to the copy of this License. Also, you must do one
of these things:
a) Accompany the work with the complete corresponding
machine-readable source code for the Library including whatever
changes were used in the work (which must be distributed under
Sections 1 and 2 above); and, if the work is an executable linked
with the Library, with the complete machine-readable "work that
uses the Library", as object code and/or source code, so that the
user can modify the Library and then relink to produce a modified
executable containing the modified Library. (It is understood
that the user who changes the contents of definitions files in the
Library will not necessarily be able to recompile the application
to use the modified definitions.)
b) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (1) uses at run time a
copy of the library already present on the user's computer system,
rather than copying library functions into the executable, and (2)
will operate properly with a modified version of the library, if
the user installs one, as long as the modified version is
interface-compatible with the version that the work was made with.
c) Accompany the work with a written offer, valid for at
least three years, to give the same user the materials
specified in Subsection 6a, above, for a charge no more
than the cost of performing this distribution.
d) If distribution of the work is made by offering access to copy
from a designated place, offer equivalent access to copy the above
specified materials from the same place.
e) Verify that the user has already received a copy of these
materials or that you have already sent this user a copy.
For an executable, the required form of the "work that uses the
Library" must include any data and utility programs needed for
reproducing the executable from it. However, as a special exception,
the materials to be distributed need not include anything that is
normally distributed (in either source or binary form) with the major
components (compiler, kernel, and so on) of the operating system on
which the executable runs, unless that component itself accompanies
the executable.
It may happen that this requirement contradicts the license
restrictions of other proprietary libraries that do not normally
accompany the operating system. Such a contradiction means you cannot
use both them and the Library together in an executable that you
distribute.
7. You may place library facilities that are a work based on the
Library side-by-side in a single library together with other library
facilities not covered by this License, and distribute such a combined
library, provided that the separate distribution of the work based on
the Library and of the other library facilities is otherwise
permitted, and provided that you do these two things:
a) Accompany the combined library with a copy of the same work
based on the Library, uncombined with any other library
facilities. This must be distributed under the terms of the
Sections above.
b) Give prominent notice with the combined library of the fact
that part of it is a work based on the Library, and explaining
where to find the accompanying uncombined form of the same work.
8. You may not copy, modify, sublicense, link with, or distribute
the Library except as expressly provided under this License. Any
attempt otherwise to copy, modify, sublicense, link with, or
distribute the Library is void, and will automatically terminate your
rights under this License. However, parties who have received copies,
or rights, from you under this License will not have their licenses
terminated so long as such parties remain in full compliance.
9. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Library or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Library (or any work based on the
Library), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Library or works based on it.
10. Each time you redistribute the Library (or any work based on the
Library), the recipient automatically receives a license from the
original licensor to copy, distribute, link with or modify the Library
subject to these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties with
this License.
11. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Library at all. For example, if a patent
license would not permit royalty-free redistribution of the Library by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Library.
If any portion of this section is held invalid or unenforceable under any
particular circumstance, the balance of the section is intended to apply,
and the section as a whole is intended to apply in other circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
12. If the distribution and/or use of the Library is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Library under this License may add
an explicit geographical distribution limitation excluding those countries,
so that distribution is permitted only in or among countries not thus
excluded. In such case, this License incorporates the limitation as if
written in the body of this License.
13. The Free Software Foundation may publish revised and/or new
versions of the Lesser General Public License from time to time.
Such new versions will be similar in spirit to the present version,
but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Library
specifies a version number of this License which applies to it and
"any later version", you have the option of following the terms and
conditions either of that version or of any later version published by
the Free Software Foundation. If the Library does not specify a
license version number, you may choose any version ever published by
the Free Software Foundation.
14. If you wish to incorporate parts of the Library into other free
programs whose distribution conditions are incompatible with these,
write to the author to ask for permission. For software which is
copyrighted by the Free Software Foundation, write to the Free
Software Foundation; we sometimes make exceptions for this. Our
decision will be guided by the two goals of preserving the free status
of all derivatives of our free software and of promoting the sharing
and reuse of software generally.
NO WARRANTY
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Libraries
If you develop a new library, and you want it to be of the greatest
possible use to the public, we recommend making it free software that
everyone can redistribute and change. You can do so by permitting
redistribution under these terms (or, alternatively, under the terms of the
ordinary General Public License).
To apply these terms, attach the following notices to the library. It is
safest to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.
<one line to give the library's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
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 GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
Also add information on how to contact you by electronic and paper mail.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the library, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the
library `Frob' (a library for tweaking knobs) written by James Random
Hacker.
<signature of Ty Coon>, 1 April 1990
Ty Coon, President of Vice
That's all there is to it!

6
extern/ESFMu/MODIFIED.md vendored Normal file
View file

@ -0,0 +1,6 @@
# Modification disclaimer
This is a modified version of ESFMu v1.0.1 which implements a function for reading the waveform output of the individual FM channels. The modification was made by Kagamiin~ (original author of ESFMu) herself.
Kagamiin~ notes:
> In hindsight, I might merge back this functionality into a future version of the core, as it could be be useful for generating oscilloscope views in other tools.

67
extern/ESFMu/README.md vendored Normal file
View file

@ -0,0 +1,67 @@
# ESFMu
An emulator for the ESS "ESFM" enhanced OPL3 clone, based on Nuke.YKT's **Nuked OPL3** and reverse-engineering efforts from the community.
## Acknowledgements
I'd like to thank:
- **Nuke.YKT**
- Developer of **Nuked OPL3**, which was the basis for **ESFMu**'s code and also a great learning resource on Yamaha FM synthesis for myself.
- Nuke.YKT also gives shoutouts on behalf of **Nuked OPL3** to:
>- 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.
- **rainwarrior**
- For performing the initial research on ESFM drivers and documenting ESS's patent on native mode operator organization.
- **jwt27**
- For kickstarting the ESFM research project and compiling rainwarrior's findings and more in an accessible document ("ESFM Demystified").
- **pachuco/CatButts**
- For documenting ESS's patent on ESFM's feedback implementation, which was vital in getting **ESFMu**'s sound output to be accurate.
- And everybody who helped out with real hardware testing
## Usage
To use **ESFMu**:
- include the **esfm.h** header file into your source code
- include the **esfm.c** and **esfm_registers.c** files into your build and link process
- declare or allocate a variable of type `esfm_chip` somewhere in your code - this will hold the chip's state
- use the function interface defined in **esfm.h** to interact with the `esfm_chip` structure
## Function interface
If you're familiar with **Nuked OPL3**, you'll find many similarities in the function interface provided by **ESFMu**. There are a few things to point out, however:
### Buffered writes
Just like **Nuked OPL3**, **ESFMu** offers buffered register writes. However, it offers them in two flavors: "legacy" and fast.
The fast buffered register writes (`ESFM_write_reg_buffered_fast`) are recommended, since they offer minimal latency which is close to the behavior you'd get with the actual ESS drivers on Windows.
The "legacy" buffered register writes are only recommended for specific cases, such as programs seeking for a shortcut to emulate the write delays from some sound drivers.
### Port-level access
Unlike **Nuked OPL3**, **ESFMu** actually allows port-level access to the ESFM interface. This is relevant because the ESFM port interface is actually modal, meaning that its behavior changes depending on whether the chip is set to emulation (OPL3 compatibility) mode or native (ESFM) mode.
Using port-level access allows for applications to not need to keep track of whether the chip is in native mode or not, nor to perform the port handling logic on their side.
Applications that use the register-level access, on the other hand, need to take care to either stick to only one of the operating modes (either native or emulation), or handle the port mapping logic on their own side.
### Register readback
ESFM allows for register contents to be read back through its ports, and **ESFMu** implements this functionality, both via dedicated register read functions and via the port read interface.
Note that in ESFM, register contents can only be read back when the chip is set to native (ESFM) mode, not when the chip is in emulation mode (i.e. OPL3 compatibility mode).
## Licensing
**ESFMu** is highly based on **Nuked OPL3**, which is licensed under the GNU Lesser General Public License version 2.1 or later. Therefore, **ESFMu** is licensed under the same license.
If you'd like to obtain a grant to use **ESFMu** under different terms, you should get in contact with [Nuke.YKT](https://github.com/nukeykt) (author of **Nuked OPL3**) as well as with [Kagamiin~](https://github.com/Kagamiin) (yours truly).

1164
extern/ESFMu/esfm.c vendored Normal file

File diff suppressed because it is too large Load diff

305
extern/ESFMu/esfm.h vendored Normal file
View file

@ -0,0 +1,305 @@
/*
* ESFMu: emulator for the ESS "ESFM" enhanced OPL3 clone
* Copyright (C) 2023 Kagamiin~
*
* ESFMu 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.
*
* ESFMu 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ESFMu. If not, see <https://www.gnu.org/licenses/>.
*/
/*
* ESFMu wouldn't have been possible without the hard work and dedication of
* the retro computer hardware research and preservation community.
*
* I'd like to thank:
* - Nuke.YKT
* Developer of Nuked OPL3, which was the basis for ESFMu's code and
* also a great learning resource on Yamaha FM synthesis for myself.
* Nuke.YKT also gives shoutouts on behalf of Nuked OPL3 to:
* - 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.
* - rainwarrior
* For performing the initial research on ESFM drivers and documenting
* ESS's patent on native mode operator organization.
* - jwt27
* For kickstarting the ESFM research project and compiling rainwarrior's
* findings and more in an accessible document ("ESFM Demystified").
* - pachuco/CatButts
* For documenting ESS's patent on ESFM's feedback implementation, which
* was vital in getting ESFMu's sound output to be accurate.
* - And everybody who helped out with real hardware testing
*/
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct _esfm_slot esfm_slot;
typedef struct _esfm_slot_internal esfm_slot_internal;
typedef struct _esfm_channel esfm_channel;
typedef struct _esfm_chip esfm_chip;
void ESFM_init (esfm_chip *chip);
void ESFM_write_reg (esfm_chip *chip, uint16_t address, uint8_t data);
void ESFM_write_reg_buffered (esfm_chip *chip, uint16_t address, uint8_t data);
void ESFM_write_reg_buffered_fast (esfm_chip *chip, uint16_t address, uint8_t data);
void ESFM_write_port (esfm_chip *chip, uint8_t offset, uint8_t data);
uint8_t ESFM_readback_reg (esfm_chip *chip, uint16_t address);
uint8_t ESFM_read_port (esfm_chip *chip, uint8_t offset);
void ESFM_generate(esfm_chip *chip, int16_t *buf);
void ESFM_generate_stream(esfm_chip *chip, int16_t *sndptr, uint32_t num_samples);
// Modification by Kagamiin~:
int16_t ESFM_get_channel_output_native(esfm_chip *chip, int channel_idx);
// These are fake types just for syntax sugar.
// Beware of their underlying types when reading/writing to them.
#ifndef __NO_ESFM_FAST_TYPES
#ifndef __ESFM_FAST_TYPES
#define __ESFM_FAST_TYPES
#endif
#endif
#ifdef __ESFM_FAST_TYPES
typedef uint_fast8_t flag;
typedef uint_fast8_t uint2;
typedef uint_fast8_t uint3;
typedef uint_fast8_t uint4;
typedef uint_fast8_t uint5;
typedef uint_fast8_t uint6;
typedef uint_fast8_t uint8;
typedef uint_fast16_t uint9;
typedef uint_fast16_t uint10;
typedef uint_fast16_t uint11;
typedef uint_fast16_t uint12;
typedef uint_fast16_t uint16;
typedef uint_fast32_t uint19;
typedef uint_fast32_t uint23;
typedef uint_fast32_t uint32;
typedef uint_fast64_t uint36;
typedef int_fast16_t int13;
typedef int_fast16_t int14;
typedef int_fast16_t int16;
typedef int_fast32_t int32;
#else
typedef uint8_t flag;
typedef uint8_t uint2;
typedef uint8_t uint3;
typedef uint8_t uint4;
typedef uint8_t uint5;
typedef uint8_t uint6;
typedef uint8_t uint8;
typedef uint16_t uint9;
typedef uint16_t uint10;
typedef uint16_t uint11;
typedef uint16_t uint12;
typedef uint16_t uint16;
typedef uint32_t uint19;
typedef uint32_t uint23;
typedef uint32_t uint32;
typedef uint64_t uint36;
typedef int16_t int13;
typedef int16_t int14;
typedef int16_t int16;
typedef int32_t int32;
#endif
enum eg_states
{
EG_ATTACK,
EG_DECAY,
EG_SUSTAIN,
EG_RELEASE
};
typedef struct _esfm_write_buf
{
uint64_t timestamp;
uint16_t address;
uint8_t data;
flag valid;
} esfm_write_buf;
typedef struct _emu_slot_channel_mapping
{
int channel_idx;
int slot_idx;
} emu_slot_channel_mapping;
typedef struct _esfm_slot_internal
{
uint9 eg_position;
uint9 eg_ksl_offset;
uint10 eg_output;
uint4 keyscale;
int13 output;
int13 emu_output_enable;
int13 emu_mod_enable;
int13 feedback_buf;
int13 *mod_input;
uint19 phase_acc;
uint10 phase_out;
flag phase_reset;
flag *key_on;
uint2 eg_state;
flag eg_delay_run;
uint9 eg_delay_counter;
} esfm_slot_internal;
struct _esfm_slot
{
// Metadata
esfm_channel *channel;
esfm_chip *chip;
uint2 slot_idx;
// Register data
int13 out_enable[2];
uint10 f_num;
uint3 block;
uint3 output_level;
// a.k.a. feedback level in emu mode
uint3 mod_in_level;
uint6 t_level;
uint4 mult;
uint3 waveform;
// Only for 4th slot
uint2 rhy_noise;
uint4 attack_rate;
uint4 decay_rate;
uint4 sustain_lvl;
uint4 release_rate;
flag tremolo_en;
flag tremolo_deep;
flag vibrato_en;
flag vibrato_deep;
flag emu_connection_typ;
flag env_sustaining;
flag ksr;
uint2 ksl;
uint3 env_delay;
// overlaps with env_delay bit 0
// TODO: check if emu mode only uses this, or if it actually overwrites the channel field used by native mode
flag emu_key_on;
// Internal state
esfm_slot_internal in;
};
struct _esfm_channel
{
esfm_chip *chip;
esfm_slot slots[4];
uint5 channel_idx;
int16 output[2];
flag key_on;
flag emu_mode_4op_enable;
// Only for 17th and 18th channels
flag key_on_2;
flag emu_mode_4op_enable_2;
};
#define ESFM_WRITEBUF_SIZE 1024
#define ESFM_WRITEBUF_DELAY 2
struct _esfm_chip
{
esfm_channel channels[18];
int32 output_accm[2];
uint_fast16_t addr_latch;
flag emu_wavesel_enable;
flag emu_newmode;
flag native_mode;
flag keyscale_mode;
// Global state
uint36 eg_timer;
uint10 global_timer;
uint8 eg_clocks;
flag eg_tick;
flag eg_timer_overflow;
uint8 tremolo;
uint8 tremolo_pos;
uint8 vibrato_pos;
uint23 lfsr;
flag rm_hh_bit2;
flag rm_hh_bit3;
flag rm_hh_bit7;
flag rm_hh_bit8;
flag rm_tc_bit3;
flag rm_tc_bit5;
// 0xbd register in emulation mode, exposed in 0x4bd in native mode
// ("bass drum" register)
uint8 emu_rhy_mode_flags;
flag emu_vibrato_deep;
flag emu_tremolo_deep;
uint8 timer_reload[2];
uint8 timer_counter[2];
flag timer_enable[2];
flag timer_mask[2];
flag timer_overflow[2];
flag irq_bit;
// Halts the envelope generators from advancing.
flag test_bit_eg_halt;
/*
* Activates some sort of waveform test mode that amplifies the output volume greatly
* and continuously shifts the waveform table downwards, possibly also outputting the
* waveform's derivative? (it's so weird!)
*/
flag test_bit_distort;
// Appears to attenuate the output by about 3 dB.
flag test_bit_attenuate;
// Resets all phase generators and holds them in the reset state while this bit is set.
flag test_bit_phase_stop_reset;
esfm_write_buf write_buf[ESFM_WRITEBUF_SIZE];
size_t write_buf_start;
size_t write_buf_end;
uint64_t write_buf_timestamp;
};
#ifdef __cplusplus
}
#endif

995
extern/ESFMu/esfm_registers.c vendored Normal file
View file

@ -0,0 +1,995 @@
/*
* ESFMu: emulator for the ESS "ESFM" enhanced OPL3 clone
* Copyright (C) 2023 Kagamiin~
*
* This file includes code and data from the Nuked OPL3 project, copyright (C)
* 2013-2023 Nuke.YKT. Its usage, modification and redistribution is allowed
* under the terms of the GNU Lesser General Public License version 2.1 or
* later.
*
* ESFMu 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.
*
* ESFMu 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ESFMu. If not, see <https://www.gnu.org/licenses/>.
*/
/*
* ESFMu wouldn't have been possible without the hard work and dedication of
* the retro computer hardware research and preservation community.
*
* I'd like to thank:
* - Nuke.YKT
* Developer of Nuked OPL3, which was the basis for ESFMu's code and
* also a great learning resource on Yamaha FM synthesis for myself.
* Nuke.YKT also gives shoutouts on behalf of Nuked OPL3 to:
* - 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.
* - rainwarrior
* For performing the initial research on ESFM drivers and documenting
* ESS's patent on native mode operator organization.
* - jwt27
* For kickstarting the ESFM research project and compiling rainwarrior's
* findings and more in an accessible document ("ESFM Demystified").
* - pachuco/CatButts
* For documenting ESS's patent on ESFM's feedback implementation, which
* was vital in getting ESFMu's sound output to be accurate.
* - And everybody who helped out with real hardware testing
*/
#include "esfm.h"
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdbool.h>
/*
* Table of KSL values extracted from OPL3 ROM; taken straight from Nuked OPL3
* source code.
* TODO: Check if ESFM uses the same KSL values.
*/
static const int16 kslrom[16] = {
0, 32, 40, 45, 48, 51, 53, 55, 56, 58, 59, 60, 61, 62, 63, 64
};
/*
* This maps the low 5 bits of emulation mode address to an emulation mode
* slot; taken straight from Nuked OPL3. Used for decoding certain emulation
* mode address ranges.
*/
static const int8_t 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
};
/*
* This maps an emulation mode slot index to a tuple representing the
* corresponding native mode channel and slot.
*/
static const emu_slot_channel_mapping emu_slot_map[36] =
{
{ 0, 0}, { 1, 0}, { 2, 0}, { 0, 1}, { 1, 1}, { 2, 1},
{ 3, 0}, { 4, 0}, { 5, 0}, { 3, 1}, { 4, 1}, { 5, 1},
{ 6, 0}, { 7, 0}, { 8, 0}, { 6, 1}, { 7, 1}, { 8, 1},
{ 9, 0}, {10, 0}, {11, 0}, { 9, 1}, {10, 1}, {11, 1},
{12, 0}, {13, 0}, {14, 0}, {12, 1}, {13, 1}, {14, 1},
{15, 0}, {16, 0}, {17, 0}, {15, 1}, {16, 1}, {17, 1}
};
/*
* This encodes which emulation mode channels are the secondary channel in a
* 4-op channel pair (where the entry is non-negative), and which is the
* corresponding primary channel for that secondary channel.
*/
static const int emu_4op_secondary_to_primary[18] =
{
-1, -1, -1, 0, 1, 2, -1, -1, -1,
-1, -1, -1, 9, 10, 11, -1, -1, -1
};
/*
* This encodes the operator outputs to be enabled or disabled for
* each 4-op algorithm in emulation mode.
* Indices: FM+FM, FM+AM, AM+FM, AM+AM (lower channel MSB, upper channel LSB)
* Values: enable OP1, OP2, OP3, OP4
*/
static const bool emu_4op_alg_output_enable[4][4] =
{
{0, 0, 0, 1},
{0, 1, 0, 1},
{1, 0, 0, 1},
{1, 0, 1, 1}
};
/*
* This encodes the operator interconnections to be enabled or disabled for
* each 4-op algorithm in emulation mode.
* Indices: FM+FM, FM+AM, AM+FM, AM+AM (lower channel MSB, upper channel LSB)
* Values: enable OP1FB, OP1->2, OP2->3, OP3->4
*/
static const bool emu_4op_alg_mod_enable[4][4] =
{
{1, 1, 1, 1},
{1, 1, 0, 1},
{1, 0, 1, 1},
{1, 0, 1, 0}
};
/* ------------------------------------------------------------------------- */
static void
ESFM_emu_rearrange_connections(esfm_channel *channel)
{
int secondary_to_primary;
secondary_to_primary = emu_4op_secondary_to_primary[channel->channel_idx];
if (secondary_to_primary >= 0)
{
esfm_channel *pair_primary = &channel->chip->channels[secondary_to_primary];
if (pair_primary->emu_mode_4op_enable)
{
// always work from primary channel in pair when dealing with 4-op
channel = pair_primary;
}
}
if (channel->emu_mode_4op_enable && (channel->channel_idx % 9) < 3 && channel->chip->emu_newmode)
{
esfm_channel *secondary = &channel->chip->channels[channel->channel_idx + 3];
uint2 algorithm = ((channel->slots[0].emu_connection_typ != 0) << 1)
| (secondary->slots[0].emu_connection_typ != 0);
int i;
secondary->slots[0].in.mod_input = &channel->slots[1].in.output;
for (i = 0; i < 2; i++)
{
channel->slots[i].in.emu_mod_enable =
emu_4op_alg_mod_enable[algorithm][i] ? ~((int13) 0) : 0;
channel->slots[i].in.emu_output_enable =
emu_4op_alg_output_enable[algorithm][i] ? ~((int13) 0) : 0;
secondary->slots[i].in.emu_mod_enable =
emu_4op_alg_mod_enable[algorithm][i + 2] ? ~((int13) 0) : 0;
secondary->slots[i].in.emu_output_enable =
emu_4op_alg_output_enable[algorithm][i + 2] ? ~((int13) 0) : 0;
}
}
else if ((channel->chip->emu_rhy_mode_flags & 0x20) != 0
&& (channel->channel_idx == 7 || channel->channel_idx == 8))
{
channel->slots[0].in.emu_mod_enable = 0;
channel->slots[1].in.emu_mod_enable = 0;
channel->slots[0].in.emu_output_enable = ~((int13) 0);
channel->slots[1].in.emu_output_enable = ~((int13) 0);
}
else
{
channel->slots[0].in.mod_input = &channel->slots[0].in.feedback_buf;
channel->slots[0].in.emu_mod_enable = ~((int13) 0);
channel->slots[0].in.emu_output_enable =
(channel->slots[0].emu_connection_typ != 0) ? ~((int13) 0) : 0;
channel->slots[1].in.emu_output_enable = ~((int13) 0);
channel->slots[1].in.emu_mod_enable =
(channel->slots[0].emu_connection_typ != 0) ? 0 : ~((int13) 0);
}
}
/* ------------------------------------------------------------------------- */
static void
ESFM_emu_to_native_switch(esfm_chip *chip)
{
size_t channel_idx, slot_idx;
for (channel_idx = 0; channel_idx < 18; channel_idx++)
{
for (slot_idx = 0; slot_idx < 4; slot_idx++)
{
esfm_channel *channel = &chip->channels[channel_idx];
esfm_slot *slot = &channel->slots[slot_idx];
if (slot_idx == 0)
{
slot->in.mod_input = &slot->in.feedback_buf;
}
else
{
esfm_slot *prev_slot = &channel->slots[slot_idx - 1];
slot->in.mod_input = &prev_slot->in.output;
}
}
}
}
/* ------------------------------------------------------------------------- */
static void
ESFM_native_to_emu_switch(esfm_chip *chip)
{
size_t channel_idx;
for (channel_idx = 0; channel_idx < 18; channel_idx++)
{
ESFM_emu_rearrange_connections(&chip->channels[channel_idx]);
}
}
/* ------------------------------------------------------------------------- */
static void
ESFM_slot_update_keyscale(esfm_slot *slot)
{
if (slot->slot_idx > 0 && !slot->chip->native_mode)
{
return;
}
int16 ksl = (kslrom[slot->f_num >> 6] << 2) - ((0x08 - slot->block) << 5);
if (ksl < 0)
{
ksl = 0;
}
slot->in.eg_ksl_offset = ksl;
slot->in.keyscale = (slot->block << 1)
| ((slot->f_num >> (8 + !slot->chip->keyscale_mode)) & 0x01);
}
/* ------------------------------------------------------------------------- */
static void
ESFM_emu_channel_update_keyscale(esfm_channel *channel)
{
int secondary_to_primary;
secondary_to_primary = emu_4op_secondary_to_primary[channel->channel_idx];
if (secondary_to_primary >= 0)
{
esfm_channel *pair_primary = &channel->chip->channels[secondary_to_primary];
if (pair_primary->emu_mode_4op_enable)
{
// always work from primary channel in pair when dealing with 4-op
channel = pair_primary;
}
}
ESFM_slot_update_keyscale(&channel->slots[0]);
channel->slots[1].in.eg_ksl_offset = channel->slots[0].in.eg_ksl_offset;
channel->slots[1].in.keyscale = channel->slots[0].in.keyscale;
if (channel->emu_mode_4op_enable && (channel->channel_idx % 9) < 3 && channel->chip->emu_newmode)
{
int i;
esfm_channel *secondary = &channel->chip->channels[channel->channel_idx + 3];
secondary->slots[0].f_num = channel->slots[0].f_num;
secondary->slots[0].block = channel->slots[0].block;
for (i = 0; i < 2; i++)
{
secondary->slots[i].in.eg_ksl_offset = channel->slots[0].in.eg_ksl_offset;
secondary->slots[i].in.keyscale = channel->slots[0].in.keyscale;
}
}
}
/* ------------------------------------------------------------------------- */
static inline uint8_t
ESFM_slot_readback (esfm_slot *slot, uint8_t register_idx)
{
uint8_t data = 0;
switch (register_idx & 0x07)
{
case 0x00:
data |= (slot->tremolo_en != 0) << 7;
data |= (slot->vibrato_en != 0) << 6;
data |= (slot->env_sustaining != 0) << 5;
data |= (slot->vibrato_en != 0) << 4;
data |= slot->mult & 0x0f;
break;
case 0x01:
data |= slot->ksl << 6;
data |= slot->t_level & 0x3f;
break;
case 0x02:
data |= slot->attack_rate << 4;
data |= slot->decay_rate & 0x0f;
break;
case 0x03:
data |= slot->sustain_lvl << 4;
data |= slot->release_rate & 0x0f;
break;
case 0x04:
data = slot->f_num & 0xff;
break;
case 0x05:
data |= slot->env_delay << 5;
data |= (slot->block & 0x07) << 2;
data |= (slot->f_num >> 8) & 0x03;
break;
case 0x06:
data |= (slot->tremolo_deep != 0) << 7;
data |= (slot->vibrato_deep != 0) << 6;
data |= (slot->out_enable[1] != 0) << 5;
data |= (slot->out_enable[0] != 0) << 4;
data |= (slot->mod_in_level & 0x07) << 1;
data |= slot->emu_connection_typ & 0x01;
break;
case 0x07:
data |= slot->output_level << 5;
data |= (slot->rhy_noise & 0x03) << 3;
data |= slot->waveform & 0x07;
break;
}
return data;
}
/* ------------------------------------------------------------------------- */
static inline void
ESFM_slot_write (esfm_slot *slot, uint8_t register_idx, uint8_t data)
{
switch (register_idx & 0x07)
{
case 0x00:
slot->tremolo_en = (data & 0x80) != 0;
slot->vibrato_en = (data & 0x40) != 0;
slot->env_sustaining = (data & 0x20) != 0;
slot->ksr = (data & 0x10) != 0;
slot->mult = data & 0x0f;
break;
case 0x01:
slot->ksl = data >> 6;
slot->t_level = data & 0x3f;
ESFM_slot_update_keyscale(slot);
break;
case 0x02:
slot->attack_rate = data >> 4;
slot->decay_rate = data & 0x0f;
break;
case 0x03:
slot->sustain_lvl = data >> 4;
slot->release_rate = data & 0x0f;
break;
case 0x04:
slot->f_num = (slot->f_num & 0x300) | data;
ESFM_slot_update_keyscale(slot);
break;
case 0x05:
slot->env_delay = data >> 5;
slot->emu_key_on = (data >> 5) & 0x01;
slot->block = (data >> 2) & 0x07;
slot->f_num = (slot->f_num & 0xff) | ((data & 0x03) << 8);
ESFM_slot_update_keyscale(slot);
break;
case 0x06:
slot->tremolo_deep = (data & 0x80) != 0;
slot->vibrato_deep = (data & 0x40) != 0;
slot->out_enable[1] = (data & 0x20) ? ~((int13) 0) : 0;
slot->out_enable[0] = (data & 0x10) ? ~((int13) 0) : 0;
slot->mod_in_level = (data >> 1) & 0x07;
slot->emu_connection_typ = data & 0x01;
break;
case 0x07:
slot->output_level = data >> 5;
slot->rhy_noise = (data >> 3) & 0x03;
slot->waveform = data & 0x07;
break;
}
}
#define KEY_ON_REGS_START (18 * 4 * 8)
#define TIMER1_REG (0x402)
#define TIMER2_REG (0x403)
#define TIMER_SETUP_REG (0x404)
#define CONFIG_REG (0x408)
#define BASSDRUM_REG (0x4bd)
#define TEST_REG (0x501)
#define FOUROP_CONN_REG (0x504)
#define NATIVE_MODE_REG (0x505)
/* ------------------------------------------------------------------------- */
static void
ESFM_write_reg_native (esfm_chip *chip, uint16_t address, uint8_t data)
{
int i;
address = address & 0x7ff;
if (address < KEY_ON_REGS_START)
{
// Slot register write
size_t channel_idx = address >> 5;
size_t slot_idx = (address >> 3) & 0x03;
size_t register_idx = address & 0x07;
esfm_slot *slot = &chip->channels[channel_idx].slots[slot_idx];
ESFM_slot_write(slot, register_idx, data);
}
else if (address < KEY_ON_REGS_START + 16)
{
// Key-on registers
size_t channel_idx = (address - KEY_ON_REGS_START);
esfm_channel *channel = &chip->channels[channel_idx];
channel->key_on = data & 0x01;
channel->emu_mode_4op_enable = (data & 0x02) != 0;
}
else if (address < KEY_ON_REGS_START + 20)
{
// Key-on channels 17 and 18 (each half)
size_t channel_idx = 16 + ((address & 0x02) >> 1);
bool second_half = address & 0x01;
esfm_channel *channel = &chip->channels[channel_idx];
if (second_half)
{
channel->key_on_2 = data & 0x01;
channel->emu_mode_4op_enable_2 = (data & 0x02) != 0;
}
else
{
channel->key_on = data & 0x01;
channel->emu_mode_4op_enable = (data & 0x02) != 0;
}
}
else
{
switch (address & 0x5ff)
{
case TIMER1_REG:
chip->timer_reload[0] = data;
break;
case TIMER2_REG:
chip->timer_reload[1] = data;
break;
case TIMER_SETUP_REG:
if (data & 0x80)
{
chip->timer_overflow[0] = 0;
chip->timer_overflow[1] = 0;
chip->irq_bit = 0;
}
chip->timer_enable[0] = (data & 0x01) != 0;
chip->timer_enable[1] = (data & 0x02) != 0;
chip->timer_mask[0] = (data & 0x20) != 0;
chip->timer_mask[1] = (data & 0x40) != 0;
break;
case CONFIG_REG:
chip->keyscale_mode = (data & 0x40) != 0;
break;
case BASSDRUM_REG:
chip->emu_rhy_mode_flags = data & 0x3f;
chip->emu_vibrato_deep = (data & 0x40) != 0;
chip->emu_tremolo_deep = (data & 0x80) != 0;
break;
case FOUROP_CONN_REG:
for (i = 0; i < 3; i++)
{
chip->channels[i].emu_mode_4op_enable = (data >> i) & 0x01;
chip->channels[i + 9].emu_mode_4op_enable = (data >> (i + 3)) & 0x01;
}
break;
case TEST_REG:
chip->test_bit_eg_halt = (data & 0x01) | ((data & 0x20) != 0);
chip->test_bit_distort = (data & 0x02) != 0;
chip->test_bit_attenuate = (data & 0x10) != 0;
chip->test_bit_phase_stop_reset = (data & 0x40) != 0;
break;
}
}
}
/* ------------------------------------------------------------------------- */
static uint8_t
ESFM_readback_reg_native (esfm_chip *chip, uint16_t address)
{
int i;
uint8_t data = 0;
address = address & 0x7ff;
if (address < KEY_ON_REGS_START)
{
// Slot register read
size_t channel_idx = address >> 5;
size_t slot_idx = (address >> 3) & 0x03;
size_t register_idx = address & 0x07;
esfm_slot *slot = &chip->channels[channel_idx].slots[slot_idx];
data = ESFM_slot_readback(slot, register_idx);
}
else if (address < KEY_ON_REGS_START + 16)
{
// Key-on registers
size_t channel_idx = (address - KEY_ON_REGS_START);
esfm_channel *channel = &chip->channels[channel_idx];
data |= channel->key_on != 0;
data |= (channel->emu_mode_4op_enable != 0) << 1;
}
else if (address < KEY_ON_REGS_START + 20)
{
// Key-on channels 17 and 18 (each half)
size_t channel_idx = 16 + ((address & 0x02) >> 1);
bool second_half = address & 0x01;
esfm_channel *channel = &chip->channels[channel_idx];
if (second_half)
{
data |= channel->key_on_2 != 0;
data |= (channel->emu_mode_4op_enable_2 != 0) << 1;
}
else
{
data |= channel->key_on != 0;
data |= (channel->emu_mode_4op_enable != 0) << 1;
}
}
else
{
switch (address & 0x5ff)
{
case TIMER1_REG:
data = chip->timer_reload[0];
break;
case TIMER2_REG:
data = chip->timer_reload[1];
break;
case TIMER_SETUP_REG:
data |= chip->timer_enable[0] != 0;
data |= (chip->timer_enable[1] != 0) << 1;
data |= (chip->timer_mask[0] != 0) << 5;
data |= (chip->timer_mask[1] != 0) << 6;
break;
case CONFIG_REG:
data |= (chip->keyscale_mode != 0) << 6;
break;
case BASSDRUM_REG:
data |= chip->emu_rhy_mode_flags;
data |= chip->emu_vibrato_deep << 6;
data |= chip->emu_tremolo_deep << 7;
break;
case TEST_REG:
data |= chip->test_bit_eg_halt != 0;
data |= (chip->test_bit_distort != 0) << 1;
data |= (chip->test_bit_attenuate != 0) << 4;
data |= (chip->test_bit_eg_halt != 0) << 5;
data |= (chip->test_bit_phase_stop_reset != 0) << 6;
break;
case FOUROP_CONN_REG:
for (i = 0; i < 3; i++)
{
data |= (chip->channels[i].emu_mode_4op_enable != 0) << i;
data |= (chip->channels[i + 9].emu_mode_4op_enable != 0) << (i + 3);
}
break;
case NATIVE_MODE_REG:
data |= (chip->native_mode != 0) << 7;
break;
}
}
return data;
}
/* ------------------------------------------------------------------------- */
static void
ESFM_write_reg_emu (esfm_chip *chip, uint16_t address, uint8_t data)
{
bool high = (address & 0x100) != 0;
uint8_t reg = address & 0xff;
int emu_slot_idx = ad_slot[address & 0x1f];
int natv_chan_idx = -1;
int natv_slot_idx = -1;
int emu_chan_idx = (reg & 0x0f) > 8 ? -1 : ((reg & 0x0f) + high * 9);
if (emu_slot_idx >= 0)
{
if (high)
{
emu_slot_idx += 18;
}
natv_chan_idx = emu_slot_map[emu_slot_idx].channel_idx;
natv_slot_idx = emu_slot_map[emu_slot_idx].slot_idx;
}
if (reg == 0xbd)
{
chip->emu_rhy_mode_flags = data & 0x3f;
chip->emu_vibrato_deep = (data & 0x40) != 0;
chip->emu_tremolo_deep = (data & 0x80) != 0;
if (chip->emu_rhy_mode_flags & 0x20)
{
// TODO: check if writes to 0xbd actually affect the readable key-on flags at
// 0x246, 0x247, 0x248; and if there's any visible effect from the SD and TC flags
chip->channels[6].key_on = (data & 0x10) != 0;
chip->channels[7].key_on = (data & 0x01) != 0;
chip->channels[8].key_on = (data & 0x04) != 0;
chip->channels[7].key_on_2 = (data & 0x08) != 0;
chip->channels[8].key_on_2 = (data & 0x02) != 0;
}
ESFM_emu_rearrange_connections(&chip->channels[7]);
ESFM_emu_rearrange_connections(&chip->channels[8]);
return;
}
switch(reg & 0xf0)
{
case 0x00:
if (high)
{
int i;
switch(reg & 0x0f)
{
case 0x01:
chip->emu_wavesel_enable = (data & 0x20) != 0;
break;
case 0x02:
chip->timer_reload[0] = data;
break;
case 0x03:
chip->timer_reload[1] = data;
break;
case 0x04:
for (i = 0; i < 3; i++)
{
chip->channels[i].emu_mode_4op_enable = (data >> i) & 0x01;
chip->channels[i + 9].emu_mode_4op_enable = (data >> (i + 3)) & 0x01;
}
for (i = 0; i < 6; i++)
{
ESFM_emu_rearrange_connections(&chip->channels[i]);
ESFM_emu_rearrange_connections(&chip->channels[i + 9]);
}
break;
case 0x05:
chip->emu_newmode = data & 0x01;
if ((data & 0x80) != 0)
{
chip->native_mode = 1;
ESFM_emu_to_native_switch(chip);
}
break;
case 0x08:
chip->keyscale_mode = (data & 0x40) != 0;
break;
}
}
else
{
switch(reg & 0x0f)
{
case 0x01:
chip->emu_wavesel_enable = (data & 0x20) != 0;
break;
case 0x02:
chip->timer_reload[0] = data;
break;
case 0x03:
chip->timer_reload[1] = data;
break;
case 0x04:
chip->timer_enable[0] = data & 0x01;
chip->timer_enable[1] = (data & 0x02) != 0;
chip->timer_mask[0] = (data & 0x20) != 0;
chip->timer_mask[1] = (data & 0x40) != 0;
if (data & 0x80)
{
chip->irq_bit = 0;
}
break;
case 0x08:
chip->keyscale_mode = (data & 0x40) != 0;
break;
}
}
break;
case 0x20: case 0x30:
if (emu_slot_idx >= 0)
{
ESFM_slot_write(&chip->channels[natv_chan_idx].slots[natv_slot_idx], 0x0, data);
}
break;
case 0x40: case 0x50:
if (emu_slot_idx >= 0)
{
ESFM_slot_write(&chip->channels[natv_chan_idx].slots[natv_slot_idx], 0x1, data);
ESFM_emu_channel_update_keyscale(&chip->channels[natv_chan_idx]);
}
break;
case 0x60: case 0x70:
if (emu_slot_idx >= 0)
{
ESFM_slot_write(&chip->channels[natv_chan_idx].slots[natv_slot_idx], 0x2, data);
}
break;
case 0x80: case 0x90:
if (emu_slot_idx >= 0)
{
ESFM_slot_write(&chip->channels[natv_chan_idx].slots[natv_slot_idx], 0x3, data);
}
break;
case 0xa0:
if (emu_chan_idx >= 0)
{
ESFM_slot_write(&chip->channels[emu_chan_idx].slots[0], 0x4, data);
ESFM_emu_channel_update_keyscale(&chip->channels[emu_chan_idx]);
}
break;
case 0xb0:
if (emu_chan_idx >= 0)
{
esfm_channel *channel = &chip->channels[emu_chan_idx];
// TODO: check if emulation mode actually writes to the native mode key on registers
// it might only use slot 0's emu key on field...
channel->key_on = (data & 0x20) != 0;
if (channel->channel_idx == 7 || channel->channel_idx == 8)
{
channel->key_on_2 = (data & 0x20) != 0;
}
ESFM_slot_write(&channel->slots[0], 0x5, data);
ESFM_emu_channel_update_keyscale(&chip->channels[emu_chan_idx]);
}
break;
case 0xc0:
if (emu_chan_idx >= 0)
{
ESFM_slot_write(&chip->channels[emu_chan_idx].slots[0], 0x6, data);
ESFM_emu_rearrange_connections(&chip->channels[emu_chan_idx]);
}
break;
case 0xe0: case 0xf0:
if (emu_slot_idx >= 0)
{
ESFM_slot_write(&chip->channels[natv_chan_idx].slots[natv_slot_idx], 0x7, data);
}
break;
}
}
/* ------------------------------------------------------------------------- */
void
ESFM_write_reg (esfm_chip *chip, uint16_t address, uint8_t data)
{
if (chip->native_mode)
{
ESFM_write_reg_native(chip, address, data);
return;
}
else
{
ESFM_write_reg_emu(chip, address, data);
return;
}
}
/* ------------------------------------------------------------------------- */
void
ESFM_write_reg_buffered (esfm_chip *chip, uint16_t address, uint8_t data)
{
uint64_t timestamp;
esfm_write_buf *new_entry, *last_entry;
new_entry = &chip->write_buf[chip->write_buf_end];
last_entry = &chip->write_buf[(chip->write_buf_end - 1) % ESFM_WRITEBUF_SIZE];
if (new_entry->valid) {
ESFM_write_reg(chip, new_entry->address, new_entry->data);
chip->write_buf_start = (chip->write_buf_end + 1) % ESFM_WRITEBUF_SIZE;
}
new_entry->valid = 1;
new_entry->address = address;
new_entry->data = data;
timestamp = last_entry->timestamp + ESFM_WRITEBUF_DELAY;
if (timestamp < chip->write_buf_timestamp)
{
timestamp = chip->write_buf_timestamp;
}
new_entry->timestamp = timestamp;
chip->write_buf_end = (chip->write_buf_end + 1) % ESFM_WRITEBUF_SIZE;
}
/* ------------------------------------------------------------------------- */
void
ESFM_write_reg_buffered_fast (esfm_chip *chip, uint16_t address, uint8_t data)
{
esfm_write_buf *new_entry;
new_entry = &chip->write_buf[chip->write_buf_end];
if (new_entry->valid) {
ESFM_write_reg(chip, new_entry->address, new_entry->data);
chip->write_buf_start = (chip->write_buf_end + 1) % ESFM_WRITEBUF_SIZE;
}
new_entry->valid = 1;
new_entry->address = address;
new_entry->data = data;
new_entry->timestamp = chip->write_buf_timestamp;
chip->write_buf_end = (chip->write_buf_end + 1) % ESFM_WRITEBUF_SIZE;
}
/* ------------------------------------------------------------------------- */
uint8_t
ESFM_readback_reg (esfm_chip *chip, uint16_t address)
{
if (chip->native_mode)
{
return ESFM_readback_reg_native(chip, address);
}
else
{
return 0;
}
}
/* ------------------------------------------------------------------------- */
void
ESFM_write_port (esfm_chip *chip, uint8_t offset, uint8_t data)
{
if (chip->native_mode)
{
switch(offset)
{
case 0:
chip->native_mode = 0;
ESFM_native_to_emu_switch(chip);
chip->addr_latch = data;
break;
case 1:
ESFM_write_reg_native(chip, chip->addr_latch, data);
break;
case 2:
chip->addr_latch = (chip->addr_latch & 0xff00) | data;
break;
case 3:
chip->addr_latch = chip->addr_latch & 0xff;
chip->addr_latch |= (uint16)data << 8;
break;
}
}
else
{
switch(offset)
{
case 0:
chip->addr_latch = data;
break;
case 1: case 3:
ESFM_write_reg_emu(chip, chip->addr_latch, data);
break;
case 2:
chip->addr_latch = (uint16)data | 0x100;
break;
}
}
}
/* ------------------------------------------------------------------------- */
uint8_t
ESFM_read_port (esfm_chip *chip, uint8_t offset)
{
uint8_t data = 0;
if (chip->native_mode)
{
switch(offset)
{
case 0:
// TODO: actually implement timer count, trigger and reset
data |= (chip->irq_bit != 0) << 7;
data |= (chip->timer_overflow[0] != 0) << 6;
data |= (chip->timer_overflow[1] != 0) << 5;
break;
case 1:
data = ESFM_readback_reg_native(chip, chip->addr_latch);
break;
// TODO: verify what the ESFM chip actually returns when reading
// from the other address ports
}
}
else
{
switch(offset)
{
case 0:
data |= (chip->irq_bit != 0) << 7;
data |= (chip->timer_overflow[0] != 0) << 6;
data |= (chip->timer_overflow[1] != 0) << 5;
break;
case 1:
data = 0;
break;
case 2: case 3:
// This matches OPL3 behavior.
// TODO: verify what the ESFM chip actually returns when reading
// from address ports in emulation mode
data = 0xff;
break;
}
}
return data;
}
/* ------------------------------------------------------------------------- */
void
ESFM_set_mode (esfm_chip *chip, bool native_mode)
{
native_mode = native_mode != 0;
if (native_mode != (chip->native_mode != 0))
{
chip->native_mode = native_mode;
if (native_mode)
{
ESFM_emu_to_native_switch(chip);
}
else
{
ESFM_native_to_emu_switch(chip);
}
}
}
/* ------------------------------------------------------------------------- */
void
ESFM_init (esfm_chip *chip)
{
esfm_slot *slot;
esfm_channel *channel;
size_t channel_idx, slot_idx;
memset(chip, 0, sizeof(esfm_chip));
for (channel_idx = 0; channel_idx < 18; channel_idx++)
{
for (slot_idx = 0; slot_idx < 4; slot_idx++)
{
channel = &chip->channels[channel_idx];
slot = &channel->slots[slot_idx];
channel->chip = chip;
channel->channel_idx = channel_idx;
slot->channel = channel;
slot->chip = chip;
slot->slot_idx = slot_idx;
slot->in.eg_position = slot->in.eg_output = 0x1ff;
slot->in.eg_state = EG_RELEASE;
slot->in.emu_mod_enable = ~((int13) 0);
if (slot_idx == 0)
{
slot->in.mod_input = &slot->in.feedback_buf;
}
else
{
esfm_slot *prev_slot = &channel->slots[slot_idx - 1];
slot->in.mod_input = &prev_slot->in.output;
}
if (slot_idx == 1)
{
slot->in.emu_output_enable = ~((int13) 0);
}
if (channel_idx > 15 && slot_idx & 0x02)
{
slot->in.key_on = &channel->key_on_2;
}
else
{
slot->in.key_on = &channel->key_on;
}
slot->out_enable[0] = slot->out_enable[1] = ~((int13) 0);
}
}
chip->lfsr = 1;
}

View file

@ -82,6 +82,7 @@
#include "platform/ted.h"
#include "platform/c140.h"
#include "platform/pcmdac.h"
#include "platform/esfm.h"
#include "platform/dummy.h"
#include "../ta-log.h"
#include "song.h"
@ -596,6 +597,9 @@ void DivDispatchContainer::init(DivSystem sys, DivEngine* eng, int chanCount, do
case DIV_SYSTEM_PCM_DAC:
dispatch=new DivPlatformPCMDAC;
break;
case DIV_SYSTEM_ESFM:
dispatch=new DivPlatformESFM;
break;
case DIV_SYSTEM_DUMMY:
dispatch=new DivPlatformDummy;
break;

View file

@ -1371,6 +1371,9 @@ DivInstrument* DivEngine::getIns(int index, DivInstrumentType fallbackType) {
case DIV_INS_OPL_DRUMS:
return &song.nullInsOPLDrums;
break;
case DIV_INS_ESFM:
return &song.nullInsESFM;
break;
default:
break;
}
@ -2328,6 +2331,9 @@ int DivEngine::addInstrument(int refChan, DivInstrumentType fallbackType) {
case DIV_INS_OPL_DRUMS:
*ins=song.nullInsOPLDrums;
break;
case DIV_INS_ESFM:
*ins=song.nullInsESFM;
break;
default:
break;
}

View file

@ -228,6 +228,28 @@ bool DivInstrumentSNES::operator==(const DivInstrumentSNES& other) {
);
}
bool DivInstrumentESFM::operator==(const DivInstrumentESFM& other) {
return (
_C(noise) &&
_C(op[0]) &&
_C(op[1]) &&
_C(op[2]) &&
_C(op[3])
);
}
bool DivInstrumentESFM::Operator::operator==(const DivInstrumentESFM::Operator& other) {
return (
_C(delay) &&
_C(outLvl) &&
_C(modIn) &&
_C(left) &&
_C(right) &&
_C(ct) &&
_C(dt)
);
}
#undef _C
#define FEATURE_BEGIN(x) \
@ -734,6 +756,22 @@ void DivInstrument::writeFeatureNE(SafeWriter* w) {
FEATURE_END;
}
void DivInstrument::writeFeatureEF(SafeWriter* w) {
FEATURE_BEGIN("EF");
w->writeC(esfm.noise&3);
for (int i=0; i<4; i++) {
DivInstrumentESFM::Operator& op=esfm.op[i];
w->writeC(((op.delay&7)<<5)|((op.outLvl&7)<<2)|((op.right&1)<<1)|(op.left&1));
w->writeC(op.modIn&7);
w->writeC(op.ct);
w->writeC(op.dt);
}
FEATURE_END;
}
void DivInstrument::putInsData2(SafeWriter* w, bool fui, const DivSong* song, bool insName) {
size_t blockStartSeek=0;
size_t blockEndSeek=0;
@ -777,6 +815,7 @@ void DivInstrument::putInsData2(SafeWriter* w, bool fui, const DivSong* song, bo
bool featureES=false;
bool featureX1=false;
bool featureNE=false;
bool featureEF=false;
bool checkForWL=false;
@ -990,7 +1029,10 @@ void DivInstrument::putInsData2(SafeWriter* w, bool fui, const DivSong* song, bo
featureSM=true;
featureSL=true;
break;
case DIV_INS_ESFM:
featureFM=true;
featureEF=true;
break;
case DIV_INS_MAX:
break;
case DIV_INS_NULL:
@ -1038,6 +1080,9 @@ void DivInstrument::putInsData2(SafeWriter* w, bool fui, const DivSong* song, bo
if (x1_010!=defaultIns.x1_010) {
featureX1=true;
}
if (esfm!=defaultIns.esfm) {
featureEF=true;
}
}
// check ins name
@ -1180,6 +1225,9 @@ void DivInstrument::putInsData2(SafeWriter* w, bool fui, const DivSong* song, bo
if (featureNE) {
writeFeatureNE(w);
}
if (featureEF) {
writeFeatureEF(w);
}
if (fui && (featureSL || featureWL)) {
w->write("EN",2);
@ -2579,6 +2627,31 @@ void DivInstrument::readFeatureNE(SafeReader& reader, short version) {
READ_FEAT_END;
}
void DivInstrument::readFeatureEF(SafeReader& reader, short version) {
READ_FEAT_BEGIN;
unsigned char next=reader.readC();
esfm.noise = next&3;
for (int i=0; i<4; i++) {
DivInstrumentESFM::Operator& op=esfm.op[i];
next=reader.readC();
op.delay=(next>>5)&7;
op.outLvl=(next>>2)&7;
op.right=(next>>1)&1;
op.left=next&1;
next=reader.readC();
op.modIn=next&7;
op.ct=reader.readC();
op.dt=reader.readC();
}
READ_FEAT_END;
}
DivDataErrors DivInstrument::readInsDataNew(SafeReader& reader, short version, bool fui, DivSong* song) {
unsigned char featCode[2];
@ -2646,6 +2719,8 @@ DivDataErrors DivInstrument::readInsDataNew(SafeReader& reader, short version, b
readFeatureX1(reader,version);
} else if (memcmp(featCode,"NE",2)==0) { // NES (DPCM)
readFeatureNE(reader,version);
} else if (memcmp(featCode,"EF",2)==0) { // ESFM
readFeatureEF(reader,version);
} else {
if (song==NULL && (memcmp(featCode,"SL",2)==0 || (memcmp(featCode,"WL",2)==0))) {
// nothing

View file

@ -85,6 +85,8 @@ enum DivInstrumentType: unsigned short {
DIV_INS_TED=52,
DIV_INS_C140=53,
DIV_INS_C219=54,
// TODO: Ask tilde to standardize this!!!
DIV_INS_ESFM=55,
DIV_INS_MAX,
DIV_INS_NULL
};
@ -150,6 +152,8 @@ enum DivMacroTypeOp: unsigned char {
// - AM, AR, DR, MULT (CRS), RR, SL, TL, DT2, RS, DT, D2R
// - WS, DVB = MULT (FINE), DAM = REV, KSL = EGShift, EGT = Fixed
struct DivInstrumentFM {
unsigned char alg, fb, fms, ams, fms2, ams2, ops, opllPreset;
bool fixedDrums;
@ -750,6 +754,108 @@ struct DivInstrumentSNES {
d2(0) {}
};
// ESFM operator structure:
// - DELAY, OUT, MOD, L, R, NOISE
// - Virtual: CT, DT, DTRAW
// - In FM struct: AM, DAM, AR, DR, MULT, RR, SL, TL
// - In FM struct: KSL, VIB, DVB, WS, SUS, KSR
// - Not in struct: FNUML, FNUMH, BLOCK
struct DivInstrumentESFM {
bool operator==(const DivInstrumentESFM& other);
bool operator!=(const DivInstrumentESFM& other) {
return !(*this==other);
}
// Only works on OP4, so putting it outside the Operator struct instead
unsigned char noise;
struct Operator {
unsigned char delay, outLvl, modIn, left, right;
signed char ct, dt;
bool operator==(const Operator& other);
bool operator!=(const Operator& other) {
return !(*this==other);
}
Operator():
delay(0),
outLvl(0),
modIn(0),
left(true),
right(true),
ct(0),
dt(0) {}
} op[4];
DivInstrumentESFM():
noise(0)
{
op[0].modIn=4;
op[0].outLvl=0;
op[1].modIn=7;
op[1].outLvl=0;
op[2].modIn=7;
op[2].outLvl=0;
op[3].modIn=7;
op[3].outLvl=7;
}
#if 0
void syncFrom(DivInstrumentFM &other) {
for (int i=0; i<4; i++) {
this->op[i].am=other.op[i].am;
this->op[i].dam=other.op[i].dam;
this->op[i].ar=other.op[i].ar;
this->op[i].dr=other.op[i].dr;
this->op[i].mult=other.op[i].mult;
this->op[i].rr=other.op[i].rr;
this->op[i].sl=other.op[i].sl;
this->op[i].tl=other.op[i].tl;
this->op[i].ssgEnv=other.op[i].ssgEnv;
this->op[i].ksl=other.op[i].ksl;
this->op[i].vib=other.op[i].vib;
this->op[i].dvb=other.op[i].dvb;
this->op[i].ws=other.op[i].ws;
this->op[i].ksr=other.op[i].ksr;
this->op[i].ct=0;
// from dtTable at fmsharedbase.h
if (other.op[i].dt>=7){
// trap out of range dt values
// TODO: check if any systems legitimately use dt values higher than 7
this->op[i].dt=0;
} else {
this->op[i].dt=other.op[i].dt - 3;
}
}
}
void syncTo(DivInstrumentFM &other) {
for (int i=0; i<4; i++) {
other.op[i].am=this->op[i].am;
other.op[i].dam=this->op[i].dam;
other.op[i].ar=this->op[i].ar;
other.op[i].dr=this->op[i].dr;
other.op[i].mult=this->op[i].mult;
other.op[i].rr=this->op[i].rr;
other.op[i].sl=this->op[i].sl;
other.op[i].tl=this->op[i].tl;
other.op[i].ssgEnv=this->op[i].ssgEnv;
other.op[i].ksl=this->op[i].ksl;
other.op[i].vib=this->op[i].vib;
other.op[i].dvb=this->op[i].dvb;
other.op[i].ws=this->op[i].ws;
other.op[i].ksr=this->op[i].ksr;
// dt field conversion is unfortunately lossy, as the dt field on DivInstrumentFM
// spans from 0..6 (eff. -3..3), while on DivInstrumentESFM it spans from -128..127.
other.op[i].dt=CLAMP(this->op[i].dt, -3, 3) + 3;
}
}
#endif
};
struct DivInstrument {
String name;
DivInstrumentType type;
@ -766,6 +872,8 @@ struct DivInstrument {
DivInstrumentSoundUnit su;
DivInstrumentES5506 es5506;
DivInstrumentSNES snes;
DivInstrumentESFM esfm;
// TODO I only added the esfm member here, still need to implement everything else related
/**
* these are internal functions.
@ -790,6 +898,7 @@ struct DivInstrument {
void writeFeatureES(SafeWriter* w);
void writeFeatureX1(SafeWriter* w);
void writeFeatureNE(SafeWriter* w);
void writeFeatureEF(SafeWriter* w);
void readFeatureNA(SafeReader& reader, short version);
void readFeatureFM(SafeReader& reader, short version);
@ -810,6 +919,7 @@ struct DivInstrument {
void readFeatureES(SafeReader& reader, short version);
void readFeatureX1(SafeReader& reader, short version);
void readFeatureNE(SafeReader& reader, short version);
void readFeatureEF(SafeReader& reader, short version);
DivDataErrors readInsDataOld(SafeReader& reader, short version);
DivDataErrors readInsDataNew(SafeReader& reader, short version, bool fui, DivSong* song);

View file

@ -0,0 +1,854 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2023 tildearrow and contributors
*
* 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.
*
* 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.
*/
#include "esfm.h"
#include "../engine.h"
#include "../../ta-log.h"
#include <string.h>
#include <stdio.h>
#include <math.h>
#define CHIP_FREQBASE (32768*288)
#define OFFSET_AM_VIB_SUS_KSR_MULT 0x00
#define OFFSET_KSL_TL 0x01
#define OFFSET_AR_DR 0x02
#define OFFSET_SL_RR 0x03
#define OFFSET_FREQL 0x04
#define OFFSET_FREQH_BLOCK_DELAY 0x05
#define OFFSET_DAM_DVB_LEFT_RIGHT_MODIN 0x06
#define OFFSET_OUTLVL_NOISE_WS 0x07
#define KEY_ON_REGS_START (18 * 8 * 4)
void DivPlatformESFM::acquire(short** buf, size_t len) {
thread_local short o[2];
for (size_t h=0; h<len; h++) {
if (!writes.empty()) {
QueuedWrite& w=writes.front();
ESFM_write_reg_buffered_fast(&chip,w.addr,w.val);
if (w.addr<ESFM_REG_POOL_SIZE) {
regPool[w.addr]=w.val;
}
writes.pop();
}
ESFM_generate(&chip,o);
for (int c=0; c<18; c++) {
oscBuf[c]->data[oscBuf[c]->needle++]=ESFM_get_channel_output_native(&chip,c);
}
buf[0][h]=o[0];
buf[1][h]=o[1];
}
}
void DivPlatformESFM::tick(bool sysTick) {
for (int i=0; i<18; i++) {
chan[i].std.next();
if (chan[i].std.vol.had) {
chan[i].outVol=VOL_SCALE_LOG_BROKEN(chan[i].vol,MIN(63,chan[i].std.vol.val),63);
for (int o=0; o<4; o++) {
unsigned short baseAddr=i*32 + o*8;
DivInstrumentFM::Operator& op=chan[i].state.fm.op[o];
DivInstrumentESFM::Operator& opE=chan[i].state.esfm.op[o];
unsigned char noise=chan[i].state.esfm.noise&3;
if (isMuted[i]) {
rWrite(baseAddr+OFFSET_OUTLVL_NOISE_WS,(op.ws&7)|((o==3?noise:0)<<3)|0);
} else {
rWrite(baseAddr+OFFSET_OUTLVL_NOISE_WS,(op.ws&7)|((o==3?noise:0)<<3)|((opE.outLvl&7)<<5));
if (KVS(i, o)) {
rWrite(baseAddr+OFFSET_KSL_TL,(63-VOL_SCALE_LOG_BROKEN(63-op.tl,chan[i].outVol&0x3f,63))|(op.ksl<<6));
} else {
rWrite(baseAddr+OFFSET_KSL_TL,(op.tl&0x3f)|(op.ksl<<6));
}
}
}
}
if (NEW_ARP_STRAT) {
chan[i].handleArp();
} else if (chan[i].std.arp.had) {
if (!chan[i].inPorta) {
chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val));
}
chan[i].freqChanged=true;
}
// TODO: check why I disabled globalPan here?
#if 0
if (chan[i].std.panL.had) {
chan[i].globalPan=((chan[i].std.panL.val&1)<<1)|((chan[i].std.panL.val&2)>>1);
}
#endif
if (chan[i].std.pitch.had) {
if (chan[i].std.pitch.mode) {
chan[i].pitch2+=chan[i].std.pitch.val;
CLAMP_VAR(chan[i].pitch2,-131071,131071);
} else {
chan[i].pitch2=chan[i].std.pitch.val;
}
chan[i].freqChanged=true;
}
if (chan[i].std.phaseReset.had) {
if (chan[i].std.phaseReset.val==1 && chan[i].active) {
chan[i].keyOn=true;
}
}
for (int o=0; o<4; o++) {
unsigned short baseAddr=i*32 + o*8;
DivInstrumentFM::Operator& op=chan[i].state.fm.op[o];
DivMacroInt::IntOp& m=chan[i].std.op[o];
if (m.am.had) {
op.am=m.am.val;
rWrite(baseAddr+OFFSET_AM_VIB_SUS_KSR_MULT,((op.am&1)<<7)|((op.vib&1)<<6)|((op.sus&1)<<5)|((op.ksr&1)<<4)|(op.mult&0xf));
}
if (m.vib.had) {
op.vib=m.vib.val;
rWrite(baseAddr+OFFSET_AM_VIB_SUS_KSR_MULT,((op.am&1)<<7)|((op.vib&1)<<6)|((op.sus&1)<<5)|((op.ksr&1)<<4)|(op.mult&0xf));
}
if (m.sus.had) {
op.sus=m.sus.val;
rWrite(baseAddr+OFFSET_AM_VIB_SUS_KSR_MULT,((op.am&1)<<7)|((op.vib&1)<<6)|((op.sus&1)<<5)|((op.ksr&1)<<4)|(op.mult&0xf));
}
if (m.ksr.had) {
op.ksr=m.ksr.val;
rWrite(baseAddr+OFFSET_AM_VIB_SUS_KSR_MULT,((op.am&1)<<7)|((op.vib&1)<<6)|((op.sus&1)<<5)|((op.ksr&1)<<4)|(op.mult&0xf));
}
if (m.mult.had) {
op.mult=m.mult.val;
rWrite(baseAddr+OFFSET_AM_VIB_SUS_KSR_MULT,((op.am&1)<<7)|((op.vib&1)<<6)|((op.sus&1)<<5)|((op.ksr&1)<<4)|(op.mult&0xf));
}
if (m.ar.had) {
op.ar=m.ar.val;
rWrite(baseAddr+OFFSET_AR_DR,(op.ar<<4)|(op.dr&0xf));
}
if (m.dr.had) {
op.dr=m.dr.val;
rWrite(baseAddr+OFFSET_AR_DR,(op.ar<<4)|(op.dr&0xf));
}
if (m.sl.had) {
op.sl=m.sl.val;
rWrite(baseAddr+OFFSET_SL_RR,(op.sl<<4)|(op.rr&0xf));
}
if (m.rr.had) {
op.rr=m.rr.val;
rWrite(baseAddr+OFFSET_SL_RR,(op.sl<<4)|(op.rr&0xf));
}
if (m.tl.had || m.ksl.had) {
if (m.tl.had) {
op.tl=m.tl.val&63;
}
if (m.ksl.had) {
op.ksl=m.ksl.val;
}
if (KVS(i, o)) {
rWrite(baseAddr+OFFSET_KSL_TL,(63-VOL_SCALE_LOG_BROKEN(63-op.tl,chan[i].outVol&0x3f,63))|(op.ksl<<6));
} else {
rWrite(baseAddr+OFFSET_KSL_TL,(op.tl&0x3f)|(op.ksl<<6));
}
}
}
}
for (int i=0; i<ESFM_REG_POOL_SIZE; i++) {
if (pendingWrites[i]!=oldWrites[i]) {
immWrite(i,pendingWrites[i]&0xff);
oldWrites[i]=pendingWrites[i];
}
}
bool mustHardReset=false;
for (int i=0; i<18; i++) {
if (chan[i].keyOn || chan[i].keyOff) {
// logI("chan[%d] key off", i);
if (i<16) {
immWrite(KEY_ON_REGS_START+i, 0);
} else {
// Handle writing to the split key-on registers of channels 16 and 17
immWrite(KEY_ON_REGS_START+16+(i-16)*2, 0);
immWrite(KEY_ON_REGS_START+16+1+(i-16)*2, 0);
}
chan[i].keyOff=false;
}
if (chan[i].hardReset && chan[i].keyOn) {
mustHardReset=true;
// logI("chan[%d] hard reset, slrr := 0x0f", i);
for (int o=0; o<4; o++) {
unsigned short baseAddr=i*32 + o*8;
immWrite(baseAddr+OFFSET_SL_RR,0x0f);
}
}
}
for (int i=0; i<18; i++) {
if (chan[i].freqChanged) {
chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,false,octave(chan[i].baseFreq)*2,chan[i].pitch2,chipClock,CHIP_FREQBASE);
if (chan[i].freq<0) chan[i].freq=0;
if (chan[i].freq>131071) chan[i].freq=131071;
for (int o=0; o<4; o++) {
unsigned short baseAddr=i*32 + o*8;
DivInstrumentESFM::Operator& opE=chan[i].state.esfm.op[o];
int ct=(int)opE.ct;
int dt=(int)opE.dt;
int opFreq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride+ct:chan[i].arpOff+ct,chan[i].fixedArp,false,octave(chan[i].baseFreq)*2,chan[i].pitch2+dt,chipClock,CHIP_FREQBASE);
if (opFreq<0) opFreq=0;
if (opFreq>131071) opFreq=131071;
int freqt=toFreq(opFreq);
chan[i].freqL[o]=freqt&0xff;
chan[i].freqH[o]=freqt>>8;
immWrite(baseAddr+OFFSET_FREQL,chan[i].freqL[o]);
immWrite(baseAddr+OFFSET_FREQH_BLOCK_DELAY,chan[i].freqH[o]|(opE.delay<<5));
}
chan[i].freqChanged=false;
}
if (chan[i].keyOn && !chan[i].hardReset) {
// logI("chan[%d] soft key on", i);
if (i<16) {
immWrite(KEY_ON_REGS_START+i, 1);
} else {
// Handle writing to the split key-on registers of channels 16 and 17
immWrite(KEY_ON_REGS_START+16+(i-16)*2, 1);
immWrite(KEY_ON_REGS_START+16+1+(i-16)*2, 1);
}
chan[i].keyOn=false;
}
}
if (mustHardReset) {
for (int i=0; i<18; i++) {
if (chan[i].hardReset && chan[i].keyOn) {
// logI("chan[%d] hard reset key on, writing original slrr back", i);
for (int o=0; o<4; o++) {
unsigned short baseAddr=i*32 + o*8;
DivInstrumentFM::Operator& op=chan[i].state.fm.op[o];
immWrite(baseAddr+OFFSET_SL_RR,(op.sl<<4)|(op.rr&0xf));
}
if (i<16) {
immWrite(KEY_ON_REGS_START+i, 1);
} else {
// Handle writing to the split key-on registers of channels 16 and 17
immWrite(KEY_ON_REGS_START+16+(i-16)*2, 1);
immWrite(KEY_ON_REGS_START+16+1+(i-16)*2, 1);
}
chan[i].keyOn=false;
}
}
}
}
int DivPlatformESFM::octave(int freq) {
if (freq>=0x3ff<<6) {
return 1<<7;
} else if (freq>=0x3ff<<5) {
return 1<<6;
} else if (freq>=0x3ff<<4) {
return 1<<5;
} else if (freq>=0x3ff<<3) {
return 1<<4;
} else if (freq>=0x3ff<<2) {
return 1<<3;
} else if (freq>=0x3ff<<1) {
return 1<<2;
} else if (freq>=0x3ff) {
return 1<<1;
} else {
return 1<<0;
}
return 1<<0;
}
int DivPlatformESFM::toFreq(int freq) {
if (freq>=0x3ff<<6) {
return 0x1c00|((freq>>7)&0x3ff);
} else if (freq>=0x3ff<<5) {
return 0x1800|((freq>>6)&0x3ff);
} else if (freq>=0x3ff<<4) {
return 0x1400|((freq>>5)&0x3ff);
} else if (freq>=0x3ff<<3) {
return 0x1000|((freq>>4)&0x3ff);
} else if (freq>=0x3ff<<2) {
return 0xc00|((freq>>3)&0x3ff);
} else if (freq>=0x3ff<<1) {
return 0x800|((freq>>2)&0x3ff);
} else if (freq>=0x3ff<<0) {
return 0x400|((freq>>1)&0x3ff);
} else {
return freq&0x3ff;
}
}
void DivPlatformESFM::muteChannel(int ch, bool mute) {
isMuted[ch]=mute;
for (int o=0; o<4; o++) {
unsigned short baseAddr=ch*32 + o*8;
DivInstrumentFM::Operator& op=chan[ch].state.fm.op[o];
DivInstrumentESFM::Operator& opE=chan[ch].state.esfm.op[o];
unsigned char noise=chan[ch].state.esfm.noise&3;
if (isMuted[ch]) {
rWrite(baseAddr+OFFSET_OUTLVL_NOISE_WS,(op.ws&7)|((o==3?noise:0)<<3)|0);
} else {
rWrite(baseAddr+OFFSET_OUTLVL_NOISE_WS,(op.ws&7)|((o==3?noise:0)<<3)|((opE.outLvl&7)<<5));
if (KVS(ch, o)) {
rWrite(baseAddr+OFFSET_KSL_TL,(63-VOL_SCALE_LOG_BROKEN(63-op.tl,chan[ch].outVol&0x3f,63))|(op.ksl<<6));
} else {
rWrite(baseAddr+OFFSET_KSL_TL,(op.tl&0x3f)|(op.ksl<<6));
}
}
}
}
void DivPlatformESFM::commitState(int ch, DivInstrument* ins) {
if (chan[ch].insChanged) {
chan[ch].state.fm=ins->fm;
chan[ch].state.esfm=ins->esfm;
for (int o=0; o<4; o++) {
unsigned short baseAddr=ch*32 + o*8;
DivInstrumentFM::Operator& op=chan[ch].state.fm.op[o];
DivInstrumentESFM::Operator& opE=chan[ch].state.esfm.op[o];
unsigned char noise=chan[ch].state.esfm.noise&3;
if (isMuted[ch]) {
rWrite(baseAddr+OFFSET_OUTLVL_NOISE_WS,(op.ws&7)|((o==3?noise:0)<<3)|0);
} else {
rWrite(baseAddr+OFFSET_OUTLVL_NOISE_WS,(op.ws&7)|((o==3?noise:0)<<3)|((opE.outLvl&7)<<5));
if (KVS(ch, o)) {
rWrite(baseAddr+OFFSET_KSL_TL,(63-VOL_SCALE_LOG_BROKEN(63-op.tl,chan[ch].outVol&0x3f,63))|(op.ksl<<6));
} else {
rWrite(baseAddr+OFFSET_KSL_TL,(op.tl&0x3f)|(op.ksl<<6));
}
}
rWrite(baseAddr+OFFSET_AM_VIB_SUS_KSR_MULT,((op.am&1)<<7)|((op.vib&1)<<6)|((op.sus&1)<<5)|((op.ksr&1)<<4)|(op.mult&0xf));
rWrite(baseAddr+OFFSET_AR_DR,(op.ar<<4)|(op.dr&0xf));
rWrite(baseAddr+OFFSET_SL_RR,(op.sl<<4)|(op.rr&0xf));
rWrite(baseAddr+OFFSET_FREQH_BLOCK_DELAY,chan[ch].freqH[o]|(opE.delay<<5));
rWrite(baseAddr+OFFSET_DAM_DVB_LEFT_RIGHT_MODIN,((opE.modIn&7)<<1)|(((opE.left&chan[ch].globalPan)&1)<<4)|(((opE.right&(chan[ch].globalPan>>1))&1)<<5)|((op.dvb&1)<<6)|(op.dam<<7));
}
}
}
int DivPlatformESFM::dispatch(DivCommand c) {
switch (c.cmd) {
case DIV_CMD_NOTE_ON: {
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_ESFM);
chan[c.chan].macroInit(ins);
if (!chan[c.chan].std.vol.will) {
chan[c.chan].outVol=chan[c.chan].vol;
}
commitState(c.chan,ins);
chan[c.chan].insChanged=false;
if (c.value!=DIV_NOTE_NULL) {
chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
chan[c.chan].note=c.value;
chan[c.chan].freqChanged=true;
}
chan[c.chan].keyOn=true;
chan[c.chan].active=true;
break;
}
case DIV_CMD_NOTE_OFF:
chan[c.chan].keyOff=true;
chan[c.chan].keyOn=false;
chan[c.chan].active=false;
break;
case DIV_CMD_NOTE_OFF_ENV:
chan[c.chan].keyOff=true;
chan[c.chan].keyOn=false;
chan[c.chan].active=false;
chan[c.chan].std.release();
break;
case DIV_CMD_ENV_RELEASE:
chan[c.chan].std.release();
break;
case DIV_CMD_VOLUME: {
chan[c.chan].vol=c.value;
if (!chan[c.chan].std.vol.has) {
chan[c.chan].outVol=c.value;
}
for (int o=0; o<4; o++) {
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
if (KVS(c.chan, o)) {
rWrite(baseAddr+OFFSET_KSL_TL,(63-VOL_SCALE_LOG_BROKEN(63-op.tl,chan[c.chan].outVol&0x3f,63))|(op.ksl<<6));
} else {
rWrite(baseAddr+OFFSET_KSL_TL,(op.tl&0x3f)|(op.ksl<<6));
}
}
break;
}
case DIV_CMD_GET_VOLUME:
return chan[c.chan].vol;
break;
case DIV_CMD_INSTRUMENT:
if (chan[c.chan].ins!=c.value || c.value2==1) {
chan[c.chan].insChanged=true;
}
chan[c.chan].ins=c.value;
break;
case DIV_CMD_PANNING: {
chan[c.chan].globalPan=(c.value>0)|((c.value2>0)<<1);
for (int o=0; o<4; o++) {
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
DivInstrumentESFM::Operator& opE=chan[c.chan].state.esfm.op[o];
rWrite(baseAddr+OFFSET_DAM_DVB_LEFT_RIGHT_MODIN,((opE.modIn&7)<<1)|(((opE.left&chan[c.chan].globalPan)&1)<<4)|(((opE.right&(chan[c.chan].globalPan>>1))&1)<<5)|((op.dvb&1)<<6)|(op.dam<<7));
}
break;
}
case DIV_CMD_PITCH:
chan[c.chan].pitch=c.value;
chan[c.chan].freqChanged=true;
break;
case DIV_CMD_NOTE_PORTA: {
int destFreq=NOTE_FREQUENCY(c.value2);
int newFreq;
bool return2=false;
if (destFreq>chan[c.chan].baseFreq) {
newFreq=chan[c.chan].baseFreq+c.value*((parent->song.linearPitch==2)?1:octave(chan[c.chan].baseFreq));
if (newFreq>=destFreq) {
newFreq=destFreq;
return2=true;
}
} else {
newFreq=chan[c.chan].baseFreq-c.value*((parent->song.linearPitch==2)?1:octave(chan[c.chan].baseFreq));
if (newFreq<=destFreq) {
newFreq=destFreq;
return2=true;
}
}
if (!chan[c.chan].portaPause && parent->song.linearPitch!=2) {
if (octave(chan[c.chan].baseFreq)!=octave(newFreq)) {
chan[c.chan].portaPause=true;
break;
}
}
chan[c.chan].baseFreq=newFreq;
chan[c.chan].portaPause=false;
chan[c.chan].freqChanged=true;
if (return2) {
chan[c.chan].inPorta=false;
return 2;
}
break;
}
case DIV_CMD_LEGATO: {
if (chan[c.chan].insChanged) {
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_ESFM);
commitState(c.chan,ins);
chan[c.chan].insChanged=false;
}
chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
chan[c.chan].note=c.value;
chan[c.chan].freqChanged=true;
break;
}
case DIV_CMD_FM_MULT: {
unsigned int o = c.value;
if (o >= 4) break;
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.mult=c.value2&15;
rWrite(baseAddr+OFFSET_AM_VIB_SUS_KSR_MULT,((op.am&1)<<7)|((op.vib&1)<<6)|((op.sus&1)<<5)|((op.ksr&1)<<4)|(op.mult&0xf));
break;
}
case DIV_CMD_FM_TL: {
unsigned int o = c.value;
if (o >= 4) break;
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.tl=c.value2&63;
if (KVS(c.chan, o)) {
rWrite(baseAddr+OFFSET_KSL_TL,(63-VOL_SCALE_LOG_BROKEN(63-op.tl,chan[c.chan].outVol&0x3f,63))|(op.ksl<<6));
} else {
rWrite(baseAddr+OFFSET_KSL_TL,(op.tl&0x3f)|(op.ksl<<6));
}
break;
}
case DIV_CMD_FM_AR: {
if (c.value<0) {
for (int o=0; o<4; o++) {
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.ar=c.value2&15;
rWrite(baseAddr+OFFSET_AR_DR,(op.ar<<4)|(op.dr&0xf));
}
} else {
unsigned int o = c.value;
if (o >= 4) break;
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.ar=c.value2&15;
rWrite(baseAddr+OFFSET_AR_DR,(op.ar<<4)|(op.dr&0xf));
}
break;
}
case DIV_CMD_FM_DR: {
if (c.value<0) {
for (int o=0; o<4; o++) {
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.dr=c.value2&15;
rWrite(baseAddr+OFFSET_AR_DR,(op.ar<<4)|(op.dr&0xf));
}
} else {
unsigned int o = c.value;
if (o >= 4) break;
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.dr=c.value2&15;
rWrite(baseAddr+OFFSET_AR_DR,(op.ar<<4)|(op.dr&0xf));
}
break;
}
case DIV_CMD_FM_SL: {
if (c.value<0) {
for (int o=0; o<4; o++) {
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.sl=c.value2&15;
rWrite(baseAddr+OFFSET_SL_RR,(op.sl<<4)|(op.rr&0xf));
}
} else {
unsigned int o = c.value;
if (o >= 4) break;
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.sl=c.value2&15;
rWrite(baseAddr+OFFSET_SL_RR,(op.sl<<4)|(op.rr&0xf));
}
break;
}
case DIV_CMD_FM_RR: {
if (c.value<0) {
for (int o=0; o<4; o++) {
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.rr=c.value2&15;
rWrite(baseAddr+OFFSET_SL_RR,(op.sl<<4)|op.rr);
}
} else {
unsigned int o = c.value;
if (o >= 4) break;
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.rr=c.value2&15;
rWrite(baseAddr+OFFSET_SL_RR,(op.sl<<4)|op.rr);
}
break;
}
case DIV_CMD_FM_AM: {
if (c.value<0) {
for (int o=0; o<4; o++) {
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.am=c.value2&1;
rWrite(baseAddr+OFFSET_AM_VIB_SUS_KSR_MULT,((op.am&1)<<7)|((op.vib&1)<<6)|((op.sus&1)<<5)|((op.ksr&1)<<4)|(op.mult&0xf));
}
} else {
unsigned int o = c.value;
if (o >= 4) break;
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.am=c.value2&1;
rWrite(baseAddr+OFFSET_AM_VIB_SUS_KSR_MULT,((op.am&1)<<7)|((op.vib&1)<<6)|((op.sus&1)<<5)|((op.ksr&1)<<4)|(op.mult&0xf));
}
break;
}
case DIV_CMD_FM_VIB: {
if (c.value<0) {
for (int o=0; o<4; o++) {
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.vib=c.value2&1;
rWrite(baseAddr+OFFSET_AM_VIB_SUS_KSR_MULT,((op.am&1)<<7)|((op.vib&1)<<6)|((op.sus&1)<<5)|((op.ksr&1)<<4)|(op.mult&0xf));
}
} else {
unsigned int o = c.value;
if (o >= 4) break;
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.vib=c.value2&1;
rWrite(baseAddr+OFFSET_AM_VIB_SUS_KSR_MULT,((op.am&1)<<7)|((op.vib&1)<<6)|((op.sus&1)<<5)|((op.ksr&1)<<4)|(op.mult&0xf));
}
break;
}
case DIV_CMD_FM_SUS: {
if (c.value<0) {
for (int o=0; o<4; o++) {
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.sus=c.value2&1;
rWrite(baseAddr+OFFSET_AM_VIB_SUS_KSR_MULT,((op.am&1)<<7)|((op.vib&1)<<6)|((op.sus&1)<<5)|((op.ksr&1)<<4)|(op.mult&0xf));
}
} else {
unsigned int o = c.value;
if (o >= 4) break;
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.sus=c.value2&1;
rWrite(baseAddr+OFFSET_AM_VIB_SUS_KSR_MULT,((op.am&1)<<7)|((op.vib&1)<<6)|((op.sus&1)<<5)|((op.ksr&1)<<4)|(op.mult&0xf));
}
break;
}
case DIV_CMD_FM_KSR: {
if (c.value<0) {
for (int o=0; o<4; o++) {
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.ksr=c.value2&1;
rWrite(baseAddr+OFFSET_AM_VIB_SUS_KSR_MULT,((op.am&1)<<7)|((op.vib&1)<<6)|((op.sus&1)<<5)|((op.ksr&1)<<4)|(op.mult&0xf));
}
} else {
unsigned int o = c.value;
if (o >= 4) break;
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.ksr=c.value2&1;
rWrite(baseAddr+OFFSET_AM_VIB_SUS_KSR_MULT,((op.am&1)<<7)|((op.vib&1)<<6)|((op.sus&1)<<5)|((op.ksr&1)<<4)|(op.mult&0xf));
}
break;
}
case DIV_CMD_FM_WS: {
if (c.value<0) {
for (int o=0; o<4; o++) {
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
DivInstrumentESFM::Operator& opE=chan[c.chan].state.esfm.op[o];
unsigned char noise=chan[c.chan].state.esfm.noise&3;
op.ws=c.value2&7;
if (isMuted[c.chan]) {
rWrite(baseAddr+OFFSET_OUTLVL_NOISE_WS,(op.ws&7)|((o==3?noise:0)<<3)|0);
} else {
rWrite(baseAddr+OFFSET_OUTLVL_NOISE_WS,(op.ws&7)|((o==3?noise:0)<<3)|((opE.outLvl&7)<<5));
}
}
} else {
unsigned int o = c.value;
if (o >= 4) break;
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
DivInstrumentESFM::Operator& opE=chan[c.chan].state.esfm.op[o];
unsigned char noise=chan[c.chan].state.esfm.noise&3;
op.ws=c.value2&7;
if (isMuted[c.chan]) {
rWrite(baseAddr+OFFSET_OUTLVL_NOISE_WS,(op.ws&7)|((o==3?noise:0)<<3)|0);
} else {
rWrite(baseAddr+OFFSET_OUTLVL_NOISE_WS,(op.ws&7)|((o==3?noise:0)<<3)|((opE.outLvl&7)<<5));
}
}
break;
}
// KSL
case DIV_CMD_FM_RS: {
if (c.value<0) {
for (int o=0; o<4; o++) {
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.ksl=c.value2&3;
if (KVS(c.chan, o)) {
rWrite(baseAddr+OFFSET_KSL_TL,(63-VOL_SCALE_LOG_BROKEN(63-op.tl,chan[c.chan].outVol&0x3f,63))|(op.ksl<<6));
} else {
rWrite(baseAddr+OFFSET_KSL_TL,(op.tl&0x3f)|(op.ksl<<6));
}
}
} else {
unsigned int o = c.value;
if (o >= 4) break;
unsigned short baseAddr=c.chan*32 + o*8;
DivInstrumentFM::Operator& op=chan[c.chan].state.fm.op[o];
op.ksl=c.value2&3;
if (KVS(c.chan, o)) {
rWrite(baseAddr+OFFSET_KSL_TL,(63-VOL_SCALE_LOG_BROKEN(63-op.tl,chan[c.chan].outVol&0x3f,63))|(op.ksl<<6));
} else {
rWrite(baseAddr+OFFSET_KSL_TL,(op.tl&0x3f)|(op.ksl<<6));
}
}
break;
}
case DIV_CMD_FM_HARD_RESET:
chan[c.chan].hardReset=c.value;
break;
case DIV_CMD_MACRO_OFF:
chan[c.chan].std.mask(c.value,true);
break;
case DIV_CMD_MACRO_ON:
chan[c.chan].std.mask(c.value,false);
break;
case DIV_ALWAYS_SET_VOLUME:
return 0;
break;
case DIV_CMD_GET_VOLMAX:
return 63;
break;
case DIV_CMD_PRE_PORTA:
if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) {
chan[c.chan].baseFreq=NOTE_FREQUENCY(chan[c.chan].note);
}
chan[c.chan].inPorta=c.value;
break;
default:
break;
}
return 1;
}
void DivPlatformESFM::forceIns() {
for (int i=0; i<18; i++) {
chan[i].insChanged=true;
chan[i].freqChanged=true;
}
for (int i=0; i<ESFM_REG_POOL_SIZE; i++) {
oldWrites[i]=-1;
}
}
void DivPlatformESFM::toggleRegisterDump(bool enable) {
DivDispatch::toggleRegisterDump(enable);
}
void* DivPlatformESFM::getChanState(int ch) {
return &chan[ch];
}
DivMacroInt* DivPlatformESFM::getChanMacroInt(int ch) {
return &chan[ch].std;
}
unsigned short DivPlatformESFM::getPan(int ch) {
return ((chan[ch].globalPan&1)<<8)|((chan[ch].globalPan&2)>>1);
}
DivDispatchOscBuffer* DivPlatformESFM::getOscBuffer(int ch) {
return oscBuf[ch];
}
unsigned char* DivPlatformESFM::getRegisterPool() {
// TODO: DEBUG, remove this, it impacts performance
for (int i=0; i<ESFM_REG_POOL_SIZE; i++) {
regPool[i] = ESFM_readback_reg(&chip, i);
}
return regPool;
}
int DivPlatformESFM::getRegisterPoolSize() {
return ESFM_REG_POOL_SIZE;
}
void DivPlatformESFM::reset() {
while (!writes.empty()) writes.pop();
ESFM_init(&chip);
// set chip to native mode
ESFM_write_reg(&chip, 0x105, 0x80);
// ensure NTS bit in register 0x408 is reset, for smooth envelope rate scaling
ESFM_write_reg(&chip, 0x408, 0x00);
for (int i=0; i<ESFM_REG_POOL_SIZE; i++) {
regPool[i] = ESFM_readback_reg(&chip, i);
oldWrites[i] = -1;
pendingWrites[i] = -1;
}
for (int i=0; i<18; i++) {
chan[i]=DivPlatformESFM::Channel();
chan[i].std.setEngine(parent);
chan[i].vol=0x3f;
chan[i].outVol=0x3f;
}
}
int DivPlatformESFM::getOutputCount() {
return 2;
}
bool DivPlatformESFM::keyOffAffectsArp(int ch) {
return false;
}
bool DivPlatformESFM::keyOffAffectsPorta(int ch) {
return false;
}
void DivPlatformESFM::notifyInsChange(int ins) {
for (int i=0; i<18; i++) {
if (chan[i].ins==ins) {
chan[i].insChanged=true;
}
}
}
void DivPlatformESFM::notifyInsDeletion(void* ins) {
for (int i=0; i<18; i++) {
chan[i].std.notifyInsDeletion((DivInstrument*)ins);
}
}
void DivPlatformESFM::poke(unsigned int addr, unsigned short val) {
immWrite(addr,val);
}
void DivPlatformESFM::poke(std::vector<DivRegWrite>& wlist) {
for (DivRegWrite& i: wlist) immWrite(i.addr,i.val);
}
void DivPlatformESFM::setFlags(const DivConfig& flags) {
rate=49716;
chipClock=COLOR_NTSC*4.0;
}
int DivPlatformESFM::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
parent=p;
dumpWrites=false;
skipRegisterWrites=false;
for (int i=0; i<18; i++) {
isMuted[i]=false;
oscBuf[i]=new DivDispatchOscBuffer;
}
setFlags(flags);
reset();
return 18;
}
void DivPlatformESFM::quit() {
for (int i=0; i<18; i++) {
delete oscBuf[i];
}
}
DivPlatformESFM::~DivPlatformESFM() {
}

144
src/engine/platform/esfm.h Normal file
View file

@ -0,0 +1,144 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2023 tildearrow and contributors
*
* 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.
*
* 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.
*/
#include "../dispatch.h"
#include "../../fixedQueue.h"
#include "../../../extern/ESFMu/esfm.h"
// ESFM register address space technically spans 0x800 (2048) bytes,
// but we only need the first 0x254 (596) during normal use.
// Rounding it up to 0x260 (608) bytes, the nearest multiple of 16.
#define ESFM_REG_POOL_SIZE 0x260
class DivPlatformESFM: public DivDispatch {
struct Channel: public SharedChannel<int> {
struct {
DivInstrumentFM fm;
DivInstrumentESFM esfm;
} state;
unsigned char freqL[4], freqH[4];
bool hardReset;
unsigned char globalPan;
int macroVolMul;
Channel():
SharedChannel<int>(0),
freqL{0, 0, 0, 0},
freqH{0, 0, 0, 0},
globalPan(3),
macroVolMul(64) {}
};
Channel chan[18];
DivDispatchOscBuffer* oscBuf[18];
bool isMuted[18];
struct QueuedWrite {
unsigned short addr;
unsigned char val;
bool addrOrVal;
QueuedWrite(): addr(0), val(0), addrOrVal(false) {}
QueuedWrite(unsigned short a, unsigned char v): addr(a), val(v), addrOrVal(false) {}
};
FixedQueue<QueuedWrite,2048> writes;
esfm_chip chip;
unsigned char regPool[ESFM_REG_POOL_SIZE];
short oldWrites[ESFM_REG_POOL_SIZE];
short pendingWrites[ESFM_REG_POOL_SIZE];
int octave(int freq);
int toFreq(int freq);
void commitState(int ch, DivInstrument* ins);
friend void putDispatchChip(void*,int);
friend void putDispatchChan(void*,int,int);
inline void rWrite(unsigned short a, short v) {
if (!skipRegisterWrites && a<ESFM_REG_POOL_SIZE) {
pendingWrites[a]=v;
}
}
inline void immWrite(unsigned short a, unsigned char v) {
if (!skipRegisterWrites) {
writes.push_back(QueuedWrite(a,v));
if (dumpWrites) {
addWrite(a,v);
}
}
}
#ifdef KVS
#undef KVS
#endif
/**
* ESFM doesn't have predef algorithms, so a custom KVS heuristic for auto mode is needed.
* This is a bit too complex for a macro.
* The heuristic for auto mode is expressed as:
* true for an operator o
* where op[o].outLvl = 7,
* or op[o].outLvl > 0 and o == 3 (last operator),
* or op[o].outLvl > 0 and (op[o].outLvl - op[o + 1].modIn) >= 2,
* or op[o].outLvl > 0 and op[o + 1].modIn == 0.
*/
inline bool KVS(int c, int o) {
if (c < 0 || c >= 18 || o < 0 || o >= 4) return false;
if (chan[c].state.fm.op[o].kvs==1) return true;
if (chan[c].state.fm.op[o].kvs==2) {
if (chan[c].state.esfm.op[o].outLvl==7) return true;
else if (chan[c].state.esfm.op[o].outLvl>0) {
if (o==3) return true;
else if ((chan[c].state.esfm.op[o].outLvl-chan[c].state.esfm.op[o+1].modIn) >= 2) {
return true;
}
else if (chan[c].state.esfm.op[o+1].modIn==0) {
return true;
}
}
}
return false;
}
public:
void acquire(short** buf, size_t len);
int dispatch(DivCommand c);
void* getChanState(int chan);
DivMacroInt* getChanMacroInt(int ch);
unsigned short getPan(int ch);
DivDispatchOscBuffer* getOscBuffer(int chan);
unsigned char* getRegisterPool();
int getRegisterPoolSize();
int getOutputCount();
void reset();
void forceIns();
void tick(bool sysTick=true);
void muteChannel(int ch, bool mute);
bool keyOffAffectsArp(int ch);
bool keyOffAffectsPorta(int ch);
void toggleRegisterDump(bool enable);
void notifyInsChange(int ins);
void notifyInsDeletion(void* ins);
void poke(unsigned int addr, unsigned short val);
void poke(std::vector<DivRegWrite>& wlist);
void setFlags(const DivConfig& flags);
int init(DivEngine* parent, int channels, int sugRate, const DivConfig& flags);
void quit();
~DivPlatformESFM();
};

View file

@ -131,7 +131,9 @@ enum DivSystem {
DIV_SYSTEM_K053260,
DIV_SYSTEM_TED,
DIV_SYSTEM_C140,
DIV_SYSTEM_C219
DIV_SYSTEM_C219,
// TODO: Ask Tilde to standardize!
DIV_SYSTEM_ESFM,
};
enum DivEffectType: unsigned short {
@ -394,7 +396,7 @@ struct DivSong {
std::vector<DivEffectStorage> effects;
DivInstrument nullIns, nullInsOPLL, nullInsOPL, nullInsOPLDrums, nullInsQSound;
DivInstrument nullIns, nullInsOPLL, nullInsOPL, nullInsOPLDrums, nullInsQSound, nullInsESFM;
DivWavetable nullWave;
DivSample nullSample;
@ -603,6 +605,45 @@ struct DivSong {
nullInsOPLDrums.fm.op[3].mult=2;
nullInsQSound.std.panLMacro.mode=true;
// ESFM default instrument - port of OPM default instrument?
// TODO: adjust these values so they actually match
nullInsESFM.esfm.noise=0;
nullInsESFM.esfm.op[0].outLvl=0;
nullInsESFM.esfm.op[0].modIn=4;
nullInsESFM.fm.op[0].tl=21;
nullInsESFM.fm.op[0].ar=15;
nullInsESFM.fm.op[0].dr=9;
nullInsESFM.fm.op[0].sl=7;
nullInsESFM.fm.op[0].rr=3;
nullInsESFM.fm.op[0].mult=7;
nullInsESFM.esfm.op[1].outLvl=0;
nullInsESFM.esfm.op[1].modIn=7;
nullInsESFM.fm.op[1].tl=24;
nullInsESFM.fm.op[1].ar=15;
nullInsESFM.fm.op[1].dr=2;
nullInsESFM.fm.op[1].sl=11;
nullInsESFM.fm.op[1].rr=2;
nullInsESFM.fm.op[1].mult=1;
nullInsESFM.esfm.op[2].outLvl=0;
nullInsESFM.esfm.op[2].modIn=7;
nullInsESFM.fm.op[2].tl=17;
nullInsESFM.fm.op[2].ar=15;
nullInsESFM.fm.op[2].dr=4;
nullInsESFM.fm.op[2].sl=15;
nullInsESFM.fm.op[2].rr=3;
nullInsESFM.fm.op[2].mult=2;
nullInsESFM.esfm.op[3].outLvl=7;
nullInsESFM.esfm.op[3].modIn=7;
nullInsESFM.fm.op[3].tl=0;
nullInsESFM.fm.op[3].ar=15;
nullInsESFM.fm.op[3].dr=3;
nullInsESFM.fm.op[3].sl=15;
nullInsESFM.fm.op[3].rr=9;
nullInsESFM.fm.op[3].mult=1;
}
};

View file

@ -1917,6 +1917,18 @@ void DivEngine::registerSystems() {
{0x12, {DIV_CMD_SNES_INVERT, "12xy: Set invert mode (x: surround; y: invert)"}},
}
);
// TODO: ask Tilde to standardize!
sysDefs[DIV_SYSTEM_ESFM]=new DivSysDef(
"ESFM (TODO: gimme a better name)", NULL, 0xd0, 0, 18, true, false, 0, false, 0,
"TODO: give ESFM the beautiful description it deserves",
{"FM 1", "FM 2", "FM 3", "FM 4", "FM 5", "FM 6", "FM 7", "FM 8", "FM 9", "FM 10", "FM 11", "FM 12", "FM 13", "FM 14", "FM 15", "FM 16", "FM 17", "FM 18"},
{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18"},
{DIV_CH_FM, DIV_CH_FM, DIV_CH_FM, DIV_CH_FM, DIV_CH_FM, DIV_CH_FM, DIV_CH_FM, DIV_CH_FM, DIV_CH_FM, DIV_CH_FM, DIV_CH_FM, DIV_CH_FM, DIV_CH_FM, DIV_CH_FM, DIV_CH_FM, DIV_CH_FM, DIV_CH_FM, DIV_CH_FM},
{DIV_INS_ESFM, DIV_INS_ESFM, DIV_INS_ESFM, DIV_INS_ESFM, DIV_INS_ESFM, DIV_INS_ESFM, DIV_INS_ESFM, DIV_INS_ESFM, DIV_INS_ESFM, DIV_INS_ESFM, DIV_INS_ESFM, DIV_INS_ESFM, DIV_INS_ESFM, DIV_INS_ESFM, DIV_INS_ESFM, DIV_INS_ESFM, DIV_INS_ESFM, DIV_INS_ESFM},
{},
{}
);
sysDefs[DIV_SYSTEM_DUMMY]=new DivSysDef(
"Dummy System", NULL, 0xfd, 0, 8, false, true, 0, false, 0,

View file

@ -22,6 +22,7 @@
#include "../../extern/opn/ym3438.h"
#include "../../extern/opm/opm.h"
#include "../../extern/opl/opl3.h"
#include "../../extern/ESFMu/esfm.h"
extern "C" {
#include "../../extern/Nuked-OPLL/opll.h"
}
@ -337,6 +338,60 @@ void FurnaceGUI::renderFMPreviewOPZ(const DivInstrumentFM& params, int pos) {
}
}
#define ESFM_WRITE(addr,val) \
ESFM_write_reg_buffered_fast((esfm_chip*)fmPreviewESFM,(addr),(val))
void FurnaceGUI::renderFMPreviewESFM(const DivInstrumentFM& params, const DivInstrumentESFM& esfmParams, int pos) {
if (fmPreviewESFM==NULL) {
fmPreviewESFM=new esfm_chip;
pos=0;
}
short out[4];
bool mult0=false;
if (pos==0) {
ESFM_init((esfm_chip*)fmPreviewESFM);
// set native mode
ESFM_WRITE(0x105, 0x80);
// set params
for (int i=0; i<4; i++) {
if ((params.op[i].mult&15)==0) {
mult0=true;
break;
}
}
for (int i=0; i<4; i++) {
const DivInstrumentFM::Operator& op=params.op[i];
const DivInstrumentESFM::Operator& opE=esfmParams.op[i];
unsigned short baseAddr=i*8;
ESFM_WRITE(baseAddr+0,(op.am<<7)|((op.vib&1)<<6)|((op.sus&1)<<5)|((op.ksr&1)<<4)|(op.mult&0x0f));
ESFM_WRITE(baseAddr+1,(op.ksl<<6)|(op.tl&0x3f));
ESFM_WRITE(baseAddr+2,(op.ar<<4)|(op.dr&0x0f));
ESFM_WRITE(baseAddr+3,(op.sl<<4)|(op.rr&0x0f));
// TODO: implement ct/dt detune... how will we do that?
ESFM_WRITE(baseAddr+4,0);
ESFM_WRITE(baseAddr+5,(opE.delay<<5)|(mult0?0x0a:0x06));
ESFM_WRITE(baseAddr+6,(op.dam<<7)|((op.dvb&1)<<6)|((opE.right&1)<<5)|((opE.left&1)<<4)|((opE.modIn&7)<<1));
ESFM_WRITE(baseAddr+7,(opE.outLvl<<5)|((i==3?esfmParams.noise:0)<<3)|(op.ws&7));
}
}
// note on
ESFM_WRITE(0x240, 1);
// render
for (int i=0; i<FM_PREVIEW_SIZE; i++) {
ESFM_generate((esfm_chip*)fmPreviewESFM,out);
ESFM_generate((esfm_chip*)fmPreviewESFM,out);
fmPreview[i]=CLAMP(out[0]+out[1],-32768,32767);
}
}
void FurnaceGUI::renderFMPreview(const DivInstrument* ins, int pos) {
switch (ins->type) {
case DIV_INS_FM:
@ -354,6 +409,8 @@ void FurnaceGUI::renderFMPreview(const DivInstrument* ins, int pos) {
case DIV_INS_OPZ:
renderFMPreviewOPZ(ins->fm,pos);
break;
case DIV_INS_ESFM:
renderFMPreviewESFM(ins->fm,ins->esfm,pos);
default:
break;
}

View file

@ -258,6 +258,7 @@ enum FurnaceGUIColors {
GUI_COLOR_INSTR_TED,
GUI_COLOR_INSTR_C140,
GUI_COLOR_INSTR_C219,
GUI_COLOR_INSTR_ESFM,
GUI_COLOR_INSTR_UNKNOWN,
GUI_COLOR_CHANNEL_BG,
@ -1379,6 +1380,7 @@ class FurnaceGUI {
void* fmPreviewOPLL;
void* fmPreviewOPZ;
void* fmPreviewOPZInterface;
void* fmPreviewESFM;
String* editString;
SDL_Event userEvent;
@ -2218,6 +2220,7 @@ class FurnaceGUI {
void renderFMPreviewOPLL(const DivInstrumentFM& params, int pos=0);
void renderFMPreviewOPL(const DivInstrumentFM& params, int pos=0);
void renderFMPreviewOPZ(const DivInstrumentFM& params, int pos=0);
void renderFMPreviewESFM(const DivInstrumentFM& params, const DivInstrumentESFM& esfmParams, int pos=0);
// these ones offer ctrl-wheel fine value changes.
bool CWSliderScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format=NULL, ImGuiSliderFlags flags=0);

View file

@ -175,6 +175,8 @@ const char* insTypes[DIV_INS_MAX+1][3]={
{"TED",ICON_FA_BAR_CHART,ICON_FUR_INS_TED},
{"C140",ICON_FA_VOLUME_UP,ICON_FUR_INS_C140},
{"C219",ICON_FA_VOLUME_UP,ICON_FUR_INS_C219},
// TODO: add ICON_FUR_INS_ESFM to furIcons.h and font_furicon.cpp and update here
{"FM (ESFM)",ICON_FA_AREA_CHART,ICON_FA_AREA_CHART},
{NULL,ICON_FA_QUESTION,ICON_FA_QUESTION}
};
@ -941,6 +943,7 @@ const FurnaceGUIColorDef guiColors[GUI_COLOR_MAX]={
D(GUI_COLOR_INSTR_TED,"",ImVec4(0.7f,0.6f,1.0f,1.0f)),
D(GUI_COLOR_INSTR_C140,"",ImVec4(1.0f,1.0f,0.0f,1.0f)),
D(GUI_COLOR_INSTR_C219,"",ImVec4(1.0f,0.8f,0.0f,1.0f)),
D(GUI_COLOR_INSTR_ESFM,"",ImVec4(0.3f,1.0f,0.9f,1.0f)),
D(GUI_COLOR_INSTR_UNKNOWN,"",ImVec4(0.3f,0.3f,0.3f,1.0f)),
D(GUI_COLOR_CHANNEL_BG,"",ImVec4(0.4f,0.6f,0.8f,1.0f)),
@ -1136,6 +1139,7 @@ const int availableSystems[]={
DIV_SYSTEM_C140,
DIV_SYSTEM_C219,
DIV_SYSTEM_PCM_DAC,
DIV_SYSTEM_ESFM,
DIV_SYSTEM_PONG,
0 // don't remove this last one!
};
@ -1171,6 +1175,7 @@ const int chipsFM[]={
DIV_SYSTEM_OPL3,
DIV_SYSTEM_OPL3_DRUMS,
DIV_SYSTEM_OPZ,
DIV_SYSTEM_ESFM,
0 // don't remove this last one!
};

View file

@ -44,6 +44,14 @@ const char* fmParamNames[3][32]={
{"ALG", "FB", "FMS/PMS", "AMS", "AR", "DR", "D2R", "RR", "SL", "TL", "RS", "MULT", "DT", "DT2", "SSG-EG", "AM", "DAM", "DVB", "EGT", "EGS", "KSL", "SUS", "VIB", "WS", "KSR", "DC", "DM", "EGS", "REV", "Fine", "FMS/PMS2", "AMS2"}
};
const char* esfmParamNames[8]={
"OP4 Noise Mode", "Envelope Delay", "Output Level", "Modulation Input Level", "Left Output", "Right Output", "Coarse Tune (semitones)", "Detune"
};
const char* esfmParamShortNames[8]={
"RHY", "DLY", "OL", "MI", "L", "R", "CT", "DT"
};
const char* fmParamShortNames[3][32]={
{"ALG", "FB", "FMS", "AMS", "A", "D", "D2", "R", "S", "TL", "RS", "ML", "DT", "DT2", "SSG", "AM", "DAM", "DVB", "SUS", "SUS", "KSL", "SUS", "VIB", "WS", "KSR", "DC", "DM", "EGS", "REV", "Fine", "FMS2", "AMS2"},
{"ALG", "FB", "FMS", "AMS", "A", "D", "SR", "R", "S", "TL", "KS", "ML", "DT", "DT2", "SSG", "AM", "AMD", "FMD", "EGT", "EGT", "KSL", "SUS", "VIB", "WS", "KSR", "DC", "DM", "EGS", "REV", "Fine", "FMS2", "AMS2"},
@ -153,6 +161,10 @@ const char* oplDrumNames[4]={
"Snare", "Tom", "Top", "HiHat"
};
const char* esfmNoiseModeNames[4]={
"Noise disabled", "Snare (square + noise)", "HiHat (ringmod from OP3, + noise)", "Top (ringmod from OP3)"
};
const bool opIsOutput[8][4]={
{false,false,false,true},
{false,false,false,true},
@ -206,8 +218,21 @@ enum FMParams {
FM_AMS2=31
};
enum ESFMParams {
ESFM_NOISE=0,
ESFM_DELAY=1,
ESFM_OUTLVL=2,
ESFM_MODIN=3,
ESFM_LEFT=4,
ESFM_RIGHT=5,
ESFM_CT=6,
ESFM_DT=7
};
#define FM_NAME(x) fmParamNames[settings.fmNames][x]
#define FM_SHORT_NAME(x) fmParamShortNames[settings.fmNames][x]
#define ESFM_NAME(x) (esfmParamNames[x])
#define ESFM_SHORT_NAME(x) (esfmParamShortNames[x])
const char* macroTypeLabels[4]={
ICON_FA_BAR_CHART "##IMacroType",
@ -2951,7 +2976,7 @@ void FurnaceGUI::drawInsEdit() {
if (ImGui::BeginTabBar("insEditTab")) {
std::vector<FurnaceGUIMacroDesc> macroList;
if (ins->type==DIV_INS_FM || ins->type==DIV_INS_OPL || ins->type==DIV_INS_OPLL || ins->type==DIV_INS_OPZ || ins->type==DIV_INS_OPL_DRUMS || ins->type==DIV_INS_OPM) {
if (ins->type==DIV_INS_FM || ins->type==DIV_INS_OPL || ins->type==DIV_INS_OPLL || ins->type==DIV_INS_OPZ || ins->type==DIV_INS_OPL_DRUMS || ins->type==DIV_INS_OPM || ins->type==DIV_INS_ESFM) {
char label[32];
int opCount=4;
if (ins->type==DIV_INS_OPLL) opCount=2;
@ -2985,171 +3010,205 @@ void FurnaceGUI::drawInsEdit() {
}
}
if (ImGui::BeginTable("fmDetails",3,ImGuiTableFlags_SizingStretchSame)) {
ImGui::TableSetupColumn("c0",ImGuiTableColumnFlags_WidthStretch,0.0);
ImGui::TableSetupColumn("c1",ImGuiTableColumnFlags_WidthStretch,0.0);
ImGui::TableSetupColumn("c2",ImGuiTableColumnFlags_WidthStretch,0.0);
if (ins->type!=DIV_INS_ESFM) {
if (ImGui::BeginTable("fmDetails",3,ImGuiTableFlags_SizingStretchSame)) {
ImGui::TableSetupColumn("c0",ImGuiTableColumnFlags_WidthStretch,0.0);
ImGui::TableSetupColumn("c1",ImGuiTableColumnFlags_WidthStretch,0.0);
ImGui::TableSetupColumn("c2",ImGuiTableColumnFlags_WidthStretch,0.0);
ImGui::TableNextRow();
switch (ins->type) {
case DIV_INS_FM:
case DIV_INS_OPM:
ImGui::TableNextColumn();
P(CWSliderScalar(FM_NAME(FM_FB),ImGuiDataType_U8,&ins->fm.fb,&_ZERO,&_SEVEN)); rightClickable
P(CWSliderScalar(FM_NAME(FM_FMS),ImGuiDataType_U8,&ins->fm.fms,&_ZERO,&_SEVEN)); rightClickable
ImGui::TableNextColumn();
P(CWSliderScalar(FM_NAME(FM_ALG),ImGuiDataType_U8,&ins->fm.alg,&_ZERO,&_SEVEN)); rightClickable
P(CWSliderScalar(FM_NAME(FM_AMS),ImGuiDataType_U8,&ins->fm.ams,&_ZERO,&_THREE)); rightClickable
ImGui::TableNextColumn();
if (fmPreviewOn) {
drawFMPreview(ImVec2(ImGui::GetContentRegionAvail().x,48.0*dpiScale));
if (!fmPreviewPaused) {
renderFMPreview(ins,1);
WAKE_UP;
}
} else {
drawAlgorithm(ins->fm.alg,FM_ALGS_4OP,ImVec2(ImGui::GetContentRegionAvail().x,48.0*dpiScale));
}
kvsConfig(ins);
break;
case DIV_INS_OPZ:
ImGui::TableNextColumn();
P(CWSliderScalar(FM_NAME(FM_FB),ImGuiDataType_U8,&ins->fm.fb,&_ZERO,&_SEVEN)); rightClickable
P(CWSliderScalar(FM_NAME(FM_FMS),ImGuiDataType_U8,&ins->fm.fms,&_ZERO,&_SEVEN)); rightClickable
P(CWSliderScalar(FM_NAME(FM_FMS2),ImGuiDataType_U8,&ins->fm.fms2,&_ZERO,&_SEVEN)); rightClickable
ImGui::TableNextColumn();
P(CWSliderScalar(FM_NAME(FM_ALG),ImGuiDataType_U8,&ins->fm.alg,&_ZERO,&_SEVEN)); rightClickable
P(CWSliderScalar(FM_NAME(FM_AMS),ImGuiDataType_U8,&ins->fm.ams,&_ZERO,&_THREE)); rightClickable
P(CWSliderScalar(FM_NAME(FM_AMS2),ImGuiDataType_U8,&ins->fm.ams2,&_ZERO,&_THREE)); rightClickable
ImGui::TableNextColumn();
if (fmPreviewOn) {
drawFMPreview(ImVec2(ImGui::GetContentRegionAvail().x,48.0*dpiScale));
if (!fmPreviewPaused) {
renderFMPreview(ins,1);
WAKE_UP;
}
} else {
drawAlgorithm(ins->fm.alg,FM_ALGS_4OP,ImVec2(ImGui::GetContentRegionAvail().x,48.0*dpiScale));
}
kvsConfig(ins);
if (ImGui::Button("Request from TX81Z")) {
doAction(GUI_ACTION_TX81Z_REQUEST);
}
/*
ImGui::SameLine();
if (ImGui::Button("Send to TX81Z")) {
showError("Coming soon!");
}
*/
break;
case DIV_INS_OPL:
case DIV_INS_OPL_DRUMS: {
bool fourOp=(ins->fm.ops==4 || ins->type==DIV_INS_OPL_DRUMS);
bool drums=ins->fm.opllPreset==16;
int algMax=fourOp?3:1;
ImGui::TableNextColumn();
ins->fm.alg&=algMax;
P(CWSliderScalar(FM_NAME(FM_FB),ImGuiDataType_U8,&ins->fm.fb,&_ZERO,&_SEVEN)); rightClickable
if (ins->type==DIV_INS_OPL) {
ImGui::BeginDisabled(ins->fm.opllPreset==16);
if (ImGui::Checkbox("4-op",&fourOp)) { PARAMETER
ins->fm.ops=fourOp?4:2;
}
ImGui::EndDisabled();
}
ImGui::TableNextColumn();
P(CWSliderScalar(FM_NAME(FM_ALG),ImGuiDataType_U8,&ins->fm.alg,&_ZERO,&algMax)); rightClickable
if (ins->type==DIV_INS_OPL) {
if (ImGui::Checkbox("Drums",&drums)) { PARAMETER
ins->fm.opllPreset=drums?16:0;
}
}
ImGui::TableNextColumn();
if (fmPreviewOn) {
drawFMPreview(ImVec2(ImGui::GetContentRegionAvail().x,48.0*dpiScale));
if (!fmPreviewPaused) {
renderFMPreview(ins,1);
WAKE_UP;
}
} else {
drawAlgorithm(ins->fm.alg&algMax,fourOp?FM_ALGS_4OP_OPL:FM_ALGS_2OP_OPL,ImVec2(ImGui::GetContentRegionAvail().x,48.0*dpiScale));
}
kvsConfig(ins);
break;
}
case DIV_INS_OPLL: {
bool dc=fmOrigin.fms;
bool dm=fmOrigin.ams;
bool sus=ins->fm.alg;
ImGui::TableNextColumn();
ImGui::BeginDisabled(ins->fm.opllPreset!=0);
P(CWSliderScalar(FM_NAME(FM_FB),ImGuiDataType_U8,&fmOrigin.fb,&_ZERO,&_SEVEN)); rightClickable
if (ImGui::Checkbox(FM_NAME(FM_DC),&dc)) { PARAMETER
fmOrigin.fms=dc;
}
ImGui::EndDisabled();
ImGui::TableNextColumn();
if (ImGui::Checkbox(FM_NAME(FM_SUS),&sus)) { PARAMETER
ins->fm.alg=sus;
}
ImGui::BeginDisabled(ins->fm.opllPreset!=0);
if (ImGui::Checkbox(FM_NAME(FM_DM),&dm)) { PARAMETER
fmOrigin.ams=dm;
}
ImGui::EndDisabled();
ImGui::TableNextColumn();
if (fmPreviewOn) {
drawFMPreview(ImVec2(ImGui::GetContentRegionAvail().x,24.0*dpiScale));
if (!fmPreviewPaused) {
renderFMPreview(ins,1);
WAKE_UP;
}
} else {
drawAlgorithm(0,FM_ALGS_2OP_OPL,ImVec2(ImGui::GetContentRegionAvail().x,24.0*dpiScale));
}
kvsConfig(ins,false);
ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
if (ImGui::BeginCombo("##LLPreset",opllInsNames[presentWhich][ins->fm.opllPreset])) {
if (isPresentCount>1) {
if (ImGui::BeginTable("LLPresetList",isPresentCount)) {
ImGui::TableNextRow(ImGuiTableRowFlags_Headers);
for (int i=0; i<4; i++) {
if (!isPresent[i]) continue;
ImGui::TableNextColumn();
ImGui::Text("%s name",opllVariants[i]);
}
for (int i=0; i<17; i++) {
ImGui::TableNextRow();
for (int j=0; j<4; j++) {
if (!isPresent[j]) continue;
ImGui::TableNextColumn();
ImGui::PushID(j*17+i);
if (ImGui::Selectable(opllInsNames[j][i])) {
ins->fm.opllPreset=i;
}
ImGui::PopID();
}
}
ImGui::EndTable();
ImGui::TableNextRow();
switch (ins->type) {
case DIV_INS_FM:
case DIV_INS_OPM:
ImGui::TableNextColumn();
P(CWSliderScalar(FM_NAME(FM_FB),ImGuiDataType_U8,&ins->fm.fb,&_ZERO,&_SEVEN)); rightClickable
P(CWSliderScalar(FM_NAME(FM_FMS),ImGuiDataType_U8,&ins->fm.fms,&_ZERO,&_SEVEN)); rightClickable
ImGui::TableNextColumn();
P(CWSliderScalar(FM_NAME(FM_ALG),ImGuiDataType_U8,&ins->fm.alg,&_ZERO,&_SEVEN)); rightClickable
P(CWSliderScalar(FM_NAME(FM_AMS),ImGuiDataType_U8,&ins->fm.ams,&_ZERO,&_THREE)); rightClickable
ImGui::TableNextColumn();
if (fmPreviewOn) {
drawFMPreview(ImVec2(ImGui::GetContentRegionAvail().x,48.0*dpiScale));
if (!fmPreviewPaused) {
renderFMPreview(ins,1);
WAKE_UP;
}
} else {
for (int i=0; i<17; i++) {
if (ImGui::Selectable(opllInsNames[presentWhich][i])) {
ins->fm.opllPreset=i;
}
drawAlgorithm(ins->fm.alg,FM_ALGS_4OP,ImVec2(ImGui::GetContentRegionAvail().x,48.0*dpiScale));
}
kvsConfig(ins);
break;
case DIV_INS_OPZ:
ImGui::TableNextColumn();
P(CWSliderScalar(FM_NAME(FM_FB),ImGuiDataType_U8,&ins->fm.fb,&_ZERO,&_SEVEN)); rightClickable
P(CWSliderScalar(FM_NAME(FM_FMS),ImGuiDataType_U8,&ins->fm.fms,&_ZERO,&_SEVEN)); rightClickable
P(CWSliderScalar(FM_NAME(FM_FMS2),ImGuiDataType_U8,&ins->fm.fms2,&_ZERO,&_SEVEN)); rightClickable
ImGui::TableNextColumn();
P(CWSliderScalar(FM_NAME(FM_ALG),ImGuiDataType_U8,&ins->fm.alg,&_ZERO,&_SEVEN)); rightClickable
P(CWSliderScalar(FM_NAME(FM_AMS),ImGuiDataType_U8,&ins->fm.ams,&_ZERO,&_THREE)); rightClickable
P(CWSliderScalar(FM_NAME(FM_AMS2),ImGuiDataType_U8,&ins->fm.ams2,&_ZERO,&_THREE)); rightClickable
ImGui::TableNextColumn();
if (fmPreviewOn) {
drawFMPreview(ImVec2(ImGui::GetContentRegionAvail().x,48.0*dpiScale));
if (!fmPreviewPaused) {
renderFMPreview(ins,1);
WAKE_UP;
}
} else {
drawAlgorithm(ins->fm.alg,FM_ALGS_4OP,ImVec2(ImGui::GetContentRegionAvail().x,48.0*dpiScale));
}
kvsConfig(ins);
if (ImGui::Button("Request from TX81Z")) {
doAction(GUI_ACTION_TX81Z_REQUEST);
}
/*
ImGui::SameLine();
if (ImGui::Button("Send to TX81Z")) {
showError("Coming soon!");
}
*/
break;
case DIV_INS_OPL:
case DIV_INS_OPL_DRUMS: {
bool fourOp=(ins->fm.ops==4 || ins->type==DIV_INS_OPL_DRUMS);
bool drums=ins->fm.opllPreset==16;
int algMax=fourOp?3:1;
ImGui::TableNextColumn();
ins->fm.alg&=algMax;
P(CWSliderScalar(FM_NAME(FM_FB),ImGuiDataType_U8,&ins->fm.fb,&_ZERO,&_SEVEN)); rightClickable
if (ins->type==DIV_INS_OPL) {
ImGui::BeginDisabled(ins->fm.opllPreset==16);
if (ImGui::Checkbox("4-op",&fourOp)) { PARAMETER
ins->fm.ops=fourOp?4:2;
}
ImGui::EndDisabled();
}
ImGui::TableNextColumn();
P(CWSliderScalar(FM_NAME(FM_ALG),ImGuiDataType_U8,&ins->fm.alg,&_ZERO,&algMax)); rightClickable
if (ins->type==DIV_INS_OPL) {
if (ImGui::Checkbox("Drums",&drums)) { PARAMETER
ins->fm.opllPreset=drums?16:0;
}
}
ImGui::EndCombo();
ImGui::TableNextColumn();
if (fmPreviewOn) {
drawFMPreview(ImVec2(ImGui::GetContentRegionAvail().x,48.0*dpiScale));
if (!fmPreviewPaused) {
renderFMPreview(ins,1);
WAKE_UP;
}
} else {
drawAlgorithm(ins->fm.alg&algMax,fourOp?FM_ALGS_4OP_OPL:FM_ALGS_2OP_OPL,ImVec2(ImGui::GetContentRegionAvail().x,48.0*dpiScale));
}
kvsConfig(ins);
break;
}
break;
case DIV_INS_OPLL: {
bool dc=fmOrigin.fms;
bool dm=fmOrigin.ams;
bool sus=ins->fm.alg;
ImGui::TableNextColumn();
ImGui::BeginDisabled(ins->fm.opllPreset!=0);
P(CWSliderScalar(FM_NAME(FM_FB),ImGuiDataType_U8,&fmOrigin.fb,&_ZERO,&_SEVEN)); rightClickable
if (ImGui::Checkbox(FM_NAME(FM_DC),&dc)) { PARAMETER
fmOrigin.fms=dc;
}
ImGui::EndDisabled();
ImGui::TableNextColumn();
if (ImGui::Checkbox(FM_NAME(FM_SUS),&sus)) { PARAMETER
ins->fm.alg=sus;
}
ImGui::BeginDisabled(ins->fm.opllPreset!=0);
if (ImGui::Checkbox(FM_NAME(FM_DM),&dm)) { PARAMETER
fmOrigin.ams=dm;
}
ImGui::EndDisabled();
ImGui::TableNextColumn();
if (fmPreviewOn) {
drawFMPreview(ImVec2(ImGui::GetContentRegionAvail().x,24.0*dpiScale));
if (!fmPreviewPaused) {
renderFMPreview(ins,1);
WAKE_UP;
}
} else {
drawAlgorithm(0,FM_ALGS_2OP_OPL,ImVec2(ImGui::GetContentRegionAvail().x,24.0*dpiScale));
}
kvsConfig(ins,false);
ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
if (ImGui::BeginCombo("##LLPreset",opllInsNames[presentWhich][ins->fm.opllPreset])) {
if (isPresentCount>1) {
if (ImGui::BeginTable("LLPresetList",isPresentCount)) {
ImGui::TableNextRow(ImGuiTableRowFlags_Headers);
for (int i=0; i<4; i++) {
if (!isPresent[i]) continue;
ImGui::TableNextColumn();
ImGui::Text("%s name",opllVariants[i]);
}
for (int i=0; i<17; i++) {
ImGui::TableNextRow();
for (int j=0; j<4; j++) {
if (!isPresent[j]) continue;
ImGui::TableNextColumn();
ImGui::PushID(j*17+i);
if (ImGui::Selectable(opllInsNames[j][i])) {
ins->fm.opllPreset=i;
}
ImGui::PopID();
}
}
ImGui::EndTable();
}
} else {
for (int i=0; i<17; i++) {
if (ImGui::Selectable(opllInsNames[presentWhich][i])) {
ins->fm.opllPreset=i;
}
}
}
ImGui::EndCombo();
}
break;
}
case DIV_INS_ESFM: {
// unreachable
break;
}
default:
break;
}
default:
break;
ImGui::EndTable();
}
} else {
// ESFM
if (ImGui::BeginTable("fmDetails",3,ImGuiTableFlags_SizingStretchProp)) {
ImGui::TableSetupColumn("c0",ImGuiTableColumnFlags_WidthStretch,0.50f);
ImGui::TableSetupColumn("c1",ImGuiTableColumnFlags_WidthStretch,0.15f);
ImGui::TableSetupColumn("c2",ImGuiTableColumnFlags_WidthStretch,0.35f);
ImGui::TableNextRow();
{
ImGui::TableNextColumn();
P(CWSliderScalar(ESFM_NAME(ESFM_NOISE),ImGuiDataType_U8,&ins->esfm.noise,&_ZERO,&_THREE)); rightClickable
ImGui::TextUnformatted(esfmNoiseModeNames[ins->esfm.noise&3]);
ImGui::TableNextColumn();
ImGui::TableNextColumn();
if (fmPreviewOn) {
drawFMPreview(ImVec2(ImGui::GetContentRegionAvail().x,48.0*dpiScale));
if (!fmPreviewPaused) {
renderFMPreview(ins,1);
WAKE_UP;
}
} else {
//drawAlgorithm(ins->fm.alg,FM_ALGS_4OP,ImVec2(ImGui::GetContentRegionAvail().x,48.0*dpiScale));
ImGui::TextUnformatted("TODO: implement fancy ESFM\nauto algorithm drawing");
}
kvsConfig(ins);
}
ImGui::EndTable();
}
ImGui::EndTable();
}
if (((ins->type==DIV_INS_OPLL || ins->type==DIV_INS_OPL) && ins->fm.opllPreset==16) || ins->type==DIV_INS_OPL_DRUMS) {
@ -3236,9 +3295,15 @@ void FurnaceGUI::drawInsEdit() {
if (ins->type==DIV_INS_OPL ||ins->type==DIV_INS_OPL_DRUMS) numCols=13;
if (ins->type==DIV_INS_OPLL) numCols=12;
if (ins->type==DIV_INS_OPZ) numCols=19;
if (ins->type==DIV_INS_ESFM) numCols=19;
if (ImGui::BeginTable("FMOperators",numCols,ImGuiTableFlags_SizingStretchProp|ImGuiTableFlags_BordersH|ImGuiTableFlags_BordersOuterV)) {
// configure columns
ImGui::TableSetupColumn("c0",ImGuiTableColumnFlags_WidthFixed); // op name
if (ins->type==DIV_INS_ESFM) {
ImGui::TableSetupColumn("c0e0",ImGuiTableColumnFlags_WidthStretch,0.05f); // outLvl
ImGui::TableSetupColumn("c0e1",ImGuiTableColumnFlags_WidthFixed); // -separator-
ImGui::TableSetupColumn("c0e2",ImGuiTableColumnFlags_WidthStretch,0.05f); // delay
}
ImGui::TableSetupColumn("c1",ImGuiTableColumnFlags_WidthStretch,0.05f); // ar
ImGui::TableSetupColumn("c2",ImGuiTableColumnFlags_WidthStretch,0.05f); // dr
ImGui::TableSetupColumn("c3",ImGuiTableColumnFlags_WidthStretch,0.05f); // sl
@ -3253,13 +3318,20 @@ void FurnaceGUI::drawInsEdit() {
ImGui::TableSetupColumn("c8z0",ImGuiTableColumnFlags_WidthStretch,0.05f); // egs
ImGui::TableSetupColumn("c8z1",ImGuiTableColumnFlags_WidthStretch,0.05f); // rev
}
if (ins->type==DIV_INS_ESFM) {
ImGui::TableSetupColumn("c8e0",ImGuiTableColumnFlags_WidthStretch,0.05f); // outLvl
}
ImGui::TableSetupColumn("c9",ImGuiTableColumnFlags_WidthStretch,0.05f); // mult
if (ins->type==DIV_INS_OPZ) {
ImGui::TableSetupColumn("c9z",ImGuiTableColumnFlags_WidthStretch,0.05f); // fine
}
if (ins->type==DIV_INS_FM || ins->type==DIV_INS_OPZ || ins->type==DIV_INS_OPM) {
if (ins->type==DIV_INS_ESFM) {
ImGui::TableSetupColumn("c9e",ImGuiTableColumnFlags_WidthStretch,0.05f); // ct
}
if (ins->type==DIV_INS_FM || ins->type==DIV_INS_OPZ || ins->type==DIV_INS_OPM || ins->type==DIV_INS_ESFM) {
ImGui::TableSetupColumn("c10",ImGuiTableColumnFlags_WidthStretch,0.05f); // dt
}
if (ins->type==DIV_INS_OPZ || ins->type==DIV_INS_OPM) {
@ -3276,7 +3348,17 @@ void FurnaceGUI::drawInsEdit() {
// header
ImGui::TableNextRow(ImGuiTableRowFlags_Headers);
ImGui::TableNextColumn();
if (ins->type==DIV_INS_ESFM) {
ImGui::TableNextColumn();
CENTER_TEXT(ESFM_SHORT_NAME(ESFM_MODIN));
ImGui::TextUnformatted(ESFM_SHORT_NAME(ESFM_MODIN));
TOOLTIP_TEXT(ESFM_NAME(ESFM_MODIN));
ImGui::TableNextColumn();
ImGui::TableNextColumn();
CENTER_TEXT(ESFM_SHORT_NAME(ESFM_DELAY));
ImGui::TextUnformatted(ESFM_SHORT_NAME(ESFM_DELAY));
TOOLTIP_TEXT(ESFM_NAME(ESFM_DELAY));
}
ImGui::TableNextColumn();
CENTER_TEXT(FM_SHORT_NAME(FM_AR));
ImGui::TextUnformatted(FM_SHORT_NAME(FM_AR));
@ -3332,6 +3414,12 @@ void FurnaceGUI::drawInsEdit() {
ImGui::TextUnformatted(FM_SHORT_NAME(FM_REV));
TOOLTIP_TEXT(FM_NAME(FM_REV));
}
if (ins->type==DIV_INS_ESFM) {
ImGui::TableNextColumn();
CENTER_TEXT(ESFM_SHORT_NAME(ESFM_OUTLVL));
ImGui::TextUnformatted(ESFM_SHORT_NAME(ESFM_OUTLVL));
TOOLTIP_TEXT(ESFM_NAME(ESFM_OUTLVL));
}
ImGui::TableNextColumn();
CENTER_TEXT(FM_SHORT_NAME(FM_MULT));
ImGui::TextUnformatted(FM_SHORT_NAME(FM_MULT));
@ -3342,6 +3430,12 @@ void FurnaceGUI::drawInsEdit() {
ImGui::TextUnformatted(FM_SHORT_NAME(FM_FINE));
TOOLTIP_TEXT(FM_NAME(FM_FINE));
}
if (ins->type==DIV_INS_ESFM) {
ImGui::TableNextColumn();
CENTER_TEXT(ESFM_SHORT_NAME(ESFM_CT));
ImGui::TextUnformatted(ESFM_SHORT_NAME(ESFM_CT));
TOOLTIP_TEXT(ESFM_NAME(ESFM_CT));
}
ImGui::TableNextColumn();
if (ins->type==DIV_INS_FM || ins->type==DIV_INS_OPZ || ins->type==DIV_INS_OPM) {
CENTER_TEXT(FM_SHORT_NAME(FM_DT));
@ -3349,6 +3443,12 @@ void FurnaceGUI::drawInsEdit() {
TOOLTIP_TEXT(FM_NAME(FM_DT));
ImGui::TableNextColumn();
}
if (ins->type==DIV_INS_ESFM) {
CENTER_TEXT(ESFM_SHORT_NAME(ESFM_DT));
ImGui::TextUnformatted(ESFM_SHORT_NAME(ESFM_DT));
TOOLTIP_TEXT(ESFM_NAME(ESFM_DT));
ImGui::TableNextColumn();
}
if (ins->type==DIV_INS_OPZ || ins->type==DIV_INS_OPM) {
CENTER_TEXT(FM_SHORT_NAME(FM_DT2));
ImGui::TextUnformatted(FM_SHORT_NAME(FM_DT2));
@ -3364,7 +3464,7 @@ void FurnaceGUI::drawInsEdit() {
ImGui::TextUnformatted("Other");
}
ImGui::TableNextColumn();
if (ins->type==DIV_INS_OPL || ins->type==DIV_INS_OPL_DRUMS || ins->type==DIV_INS_OPZ) {
if (ins->type==DIV_INS_OPL || ins->type==DIV_INS_OPL_DRUMS || ins->type==DIV_INS_OPZ || ins->type==DIV_INS_ESFM) {
ImGui::TableNextColumn();
CENTER_TEXT(FM_NAME(FM_WS));
ImGui::TextUnformatted(FM_NAME(FM_WS));
@ -3381,6 +3481,8 @@ void FurnaceGUI::drawInsEdit() {
for (int i=0; i<opCount; i++) {
DivInstrumentFM::Operator& op=fmOrigin.op[(opCount==4 && ins->type!=DIV_INS_OPL_DRUMS)?opOrder[i]:i];
DivInstrumentESFM::Operator& opE=ins->esfm.op[i];
ImGui::TableNextRow();
ImGui::TableNextColumn();
@ -3389,6 +3491,17 @@ void FurnaceGUI::drawInsEdit() {
bool mod=true;
if (ins->type==DIV_INS_OPL_DRUMS) {
mod=false;
} else if (ins->type==DIV_INS_ESFM) {
// this is the same as the KVS heuristic in platform/esfm.h
if (opE.outLvl==7) mod=false;
else if (opE.outLvl>0) {
if (i==3) mod=false;
else {
DivInstrumentESFM::Operator& opENext=ins->esfm.op[i+1];
if (opENext.modIn==0) mod=false;
else if ((opE.outLvl-opENext.modIn) >= 2) mod=false;
}
}
} else if (opCount==4) {
if (ins->type==DIV_INS_OPL) {
if (opIsOutputOPL[fmOrigin.alg&3][i]) mod=false;
@ -3440,7 +3553,7 @@ void FurnaceGUI::drawInsEdit() {
} else {
ImGui::TextUnformatted(opNameLabel.c_str());
}
// drag point
OP_DRAG_POINT;
@ -3452,7 +3565,7 @@ void FurnaceGUI::drawInsEdit() {
maxTl=63;
}
}
if (ins->type==DIV_INS_OPL || ins->type==DIV_INS_OPL_DRUMS) {
if (ins->type==DIV_INS_OPL || ins->type==DIV_INS_OPL_DRUMS || ins->type==DIV_INS_ESFM) {
maxTl=63;
}
int maxArDr=(ins->type==DIV_INS_FM || ins->type==DIV_INS_OPZ || ins->type==DIV_INS_OPM)?31:15;
@ -3461,6 +3574,18 @@ void FurnaceGUI::drawInsEdit() {
bool vibOn=op.vib;
bool susOn=op.sus;
unsigned char ssgEnv=op.ssgEnv&7;
if (ins->type==DIV_INS_ESFM) {
ImGui::TableNextColumn();
CENTER_VSLIDER;
P(CWVSliderScalar("##MODIN",ImVec2(20.0f*dpiScale,sliderHeight),ImGuiDataType_U8,&opE.modIn,&_ZERO,&_SEVEN)); rightClickable
ImGui::TableNextColumn();
ImGui::Dummy(ImVec2(4.0f*dpiScale,2.0f*dpiScale));
ImGui::TableNextColumn();
opE.delay&=7;
CENTER_VSLIDER;
P(CWVSliderScalar("##DELAY",ImVec2(20.0f*dpiScale,sliderHeight),ImGuiDataType_U8,&opE.delay,&_ZERO,&_SEVEN)); rightClickable
}
ImGui::TableNextColumn();
op.ar&=maxArDr;
@ -3528,6 +3653,12 @@ void FurnaceGUI::drawInsEdit() {
P(CWVSliderScalar("##REV",ImVec2(20.0f*dpiScale,sliderHeight),ImGuiDataType_U8,&op.dam,&_ZERO,&_SEVEN)); rightClickable
}
if (ins->type==DIV_INS_ESFM) {
ImGui::TableNextColumn();
CENTER_VSLIDER;
P(CWVSliderScalar("##OUTLVL",ImVec2(20.0f*dpiScale,sliderHeight),ImGuiDataType_U8,&opE.outLvl,&_ZERO,&_SEVEN)); rightClickable
}
ImGui::TableNextColumn();
CENTER_VSLIDER;
P(CWVSliderScalar("##MULT",ImVec2(20.0f*dpiScale,sliderHeight),ImGuiDataType_U8,&op.mult,&_ZERO,&_FIFTEEN)); rightClickable
@ -3538,6 +3669,12 @@ void FurnaceGUI::drawInsEdit() {
P(CWVSliderScalar("##FINE",ImVec2(20.0f*dpiScale,sliderHeight),ImGuiDataType_U8,&op.dvb,&_ZERO,&_FIFTEEN)); rightClickable
}
if (ins->type==DIV_INS_ESFM) {
ImGui::TableNextColumn();
CENTER_VSLIDER;
P(CWVSliderScalar("##CT",ImVec2(20.0f*dpiScale,sliderHeight),ImGuiDataType_S8,&opE.ct,&_MINUS_TWELVE,&_TWELVE)); rightClickable
}
if (ins->type==DIV_INS_FM || ins->type==DIV_INS_OPZ || ins->type==DIV_INS_OPM) {
int detune=detuneMap[settings.unsignedDetune?1:0][op.dt&7];
ImGui::TableNextColumn();
@ -3608,6 +3745,42 @@ void FurnaceGUI::drawInsEdit() {
op.ssgEnv=(op.ssgEnv&8)|(ssgEnv&7);
}
}
} else if (ins->type==DIV_INS_ESFM) {
ImGui::TableNextColumn();
CENTER_VSLIDER;
P(CWVSliderScalar("##DT",ImVec2(20.0f*dpiScale,sliderHeight),ImGuiDataType_S8,&opE.dt,&_MINUS_ONE_HUNDRED_TWENTY_EIGHT,&_ONE_HUNDRED_TWENTY_SEVEN)); rightClickable
ImGui::TableNextColumn();
bool amOn=op.am;
bool leftOn=opE.left;
bool rightOn=opE.right;
if (ImGui::BeginTable("panCheckboxes",2,ImGuiTableFlags_SizingStretchSame)) {
ImGui::TableSetupColumn("c0",ImGuiTableColumnFlags_WidthStretch,0.0);
ImGui::TableSetupColumn("c1",ImGuiTableColumnFlags_WidthStretch,0.0);
ImGui::TableNextRow();
ImGui::TableNextColumn();
if (ImGui::Checkbox(ESFM_SHORT_NAME(ESFM_LEFT),&leftOn)) { PARAMETER
opE.left=leftOn;
}
ImGui::TableNextColumn();
if (ImGui::Checkbox(ESFM_SHORT_NAME(ESFM_RIGHT),&rightOn)) { PARAMETER
opE.right=rightOn;
}
ImGui::EndTable();
}
if (ImGui::Checkbox(FM_NAME(FM_AM),&amOn)) { PARAMETER
op.am=amOn;
}
if (ImGui::Checkbox(FM_NAME(FM_VIB),&vibOn)) { PARAMETER
op.vib=vibOn;
}
if (ImGui::Checkbox(FM_NAME(FM_KSR),&ksrOn)) { PARAMETER
op.ksr=ksrOn;
}
if (ImGui::Checkbox(FM_NAME(FM_SUS),&susOn)) { PARAMETER
op.sus=susOn;
}
} else if (ins->type!=DIV_INS_OPM) {
ImGui::TableNextColumn();
bool amOn=op.am;
@ -3632,7 +3805,7 @@ void FurnaceGUI::drawInsEdit() {
}
}
if (ins->type==DIV_INS_OPL || ins->type==DIV_INS_OPL_DRUMS || ins->type==DIV_INS_OPZ) {
if (ins->type==DIV_INS_OPL || ins->type==DIV_INS_OPL_DRUMS || ins->type==DIV_INS_OPZ || ins->type==DIV_INS_ESFM) {
ImGui::TableNextColumn();
ImGui::Dummy(ImVec2(4.0f*dpiScale,2.0f*dpiScale));
ImGui::TableNextColumn();

View file

@ -24,6 +24,7 @@ const int _ONE=1;
const int _THREE=3;
const int _SEVEN=7;
const int _TEN=10;
const int _TWELVE=12;
const int _FIFTEEN=15;
const int _SIXTEEN=16;
const int _THIRTY_ONE=31;
@ -35,5 +36,6 @@ const int _FIVE_HUNDRED_ELEVEN=511;
const int _TWO_THOUSAND_FORTY_SEVEN=2047;
const int _FOUR_THOUSAND_NINETY_FIVE=4095;
const int _SIXTY_FIVE_THOUSAND_FIVE_HUNDRED_THIRTY_FIVE=65535;
const int _MINUS_TWELVE=-12;
const int _MINUS_ONE_HUNDRED_TWENTY_SEVEN=-127;
const int _MINUS_ONE_HUNDRED_TWENTY_EIGHT=-128;

View file

@ -26,6 +26,7 @@ extern const int _ONE;
extern const int _THREE;
extern const int _SEVEN;
extern const int _TEN;
extern const int _TWELVE;
extern const int _FIFTEEN;
extern const int _SIXTEEN;
extern const int _THIRTY_ONE;
@ -37,5 +38,6 @@ extern const int _FIVE_HUNDRED_ELEVEN;
extern const int _TWO_THOUSAND_FORTY_SEVEN;
extern const int _FOUR_THOUSAND_NINETY_FIVE;
extern const int _SIXTY_FIVE_THOUSAND_FIVE_HUNDRED_THIRTY_FIVE;
extern const int _MINUS_TWELVE;
extern const int _MINUS_ONE_HUNDRED_TWENTY_SEVEN;
extern const int _MINUS_ONE_HUNDRED_TWENTY_EIGHT;