Merge pull request #1559 from Kagamiin/feature/esfm

Add ESFM system
This commit is contained in:
tildearrow 2024-01-16 16:51:33 -05:00 committed by GitHub
commit 0acd62f4d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 5594 additions and 90 deletions

View file

@ -492,6 +492,8 @@ extern/opl/opl3.c
extern/YM3812-LLE/fmopl2.c
extern/YMF262-LLE/fmopl3.c
extern/YMF276-LLE/fmopn2.c
extern/ESFMu/esfm.c
extern/ESFMu/esfm_registers.c
src/pch.cpp
@ -700,6 +702,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

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!

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).

1224
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);
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;
flag key_on_gate;
uint2 eg_state;
flag eg_delay_run;
uint16 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

@ -243,6 +243,11 @@ enum DivDispatchCmds {
DIV_CMD_C64_AD, // (value)
DIV_CMD_C64_SR, // (value)
DIV_CMD_ESFM_OP_PANNING, // (op, value)
DIV_CMD_ESFM_OUTLVL, // (op, value)
DIV_CMD_ESFM_MODIN, // (op, value)
DIV_CMD_ESFM_ENV_DELAY, // (op, value)
DIV_ALWAYS_SET_VOLUME, // () -> alwaysSetVol
DIV_CMD_MAX

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"
@ -644,6 +645,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;
}
@ -2409,6 +2412,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

@ -6445,7 +6445,7 @@ SafeWriter* DivEngine::saveText(bool separatePatterns) {
w->writeText(fmt::sprintf("- type: %d\n",(int)ins->type));
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) {
w->writeText("- FM parameters:\n");
w->writeText(fmt::sprintf(" - ALG: %d\n",ins->fm.alg));
w->writeText(fmt::sprintf(" - FB: %d\n",ins->fm.fb));
@ -6489,6 +6489,25 @@ SafeWriter* DivEngine::saveText(bool separatePatterns) {
}
}
if (ins->type==DIV_INS_ESFM) {
w->writeText("- ESFM parameters:\n");
w->writeText(fmt::sprintf(" - noise mode: %d\n",ins->esfm.noise));
for (int j=0; j<ins->fm.ops; j++) {
DivInstrumentESFM::Operator& opE=ins->esfm.op[j];
w->writeText(fmt::sprintf(" - operator %d:\n",j));
w->writeText(fmt::sprintf(" - DL: %d\n",opE.delay));
w->writeText(fmt::sprintf(" - OL: %d\n",opE.outLvl));
w->writeText(fmt::sprintf(" - MI: %d\n",opE.modIn));
w->writeText(fmt::sprintf(" - output left: %s\n",trueFalse[opE.left?1:0]));
w->writeText(fmt::sprintf(" - output right: %s\n",trueFalse[opE.right?1:0]));
w->writeText(fmt::sprintf(" - CT: %d\n",opE.ct));
w->writeText(fmt::sprintf(" - DT: %d\n",opE.dt));
w->writeText(fmt::sprintf(" - fixed frequency: %s\n",trueFalse[opE.fixed?1:0]));
}
}
if (ins->type==DIV_INS_GB) {
w->writeText("- Game Boy parameters:\n");
w->writeText(fmt::sprintf(" - volume: %d\n",ins->gb.envVol));

View file

@ -230,6 +230,29 @@ 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(fixed) &&
_C(ct) &&
_C(dt)
);
}
#undef _C
#define FEATURE_BEGIN(x) \
@ -743,6 +766,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.fixed&1)<<3|(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;
@ -786,6 +825,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;
@ -999,7 +1039,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:
@ -1047,6 +1090,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
@ -1189,6 +1235,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);
@ -2598,6 +2647,32 @@ 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.fixed=(next>>3)&1;
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];
bool volIsCutoff=false;
@ -2666,6 +2741,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,7 @@ enum DivInstrumentType: unsigned short {
DIV_INS_TED=52,
DIV_INS_C140=53,
DIV_INS_C219=54,
DIV_INS_ESFM=55,
DIV_INS_MAX,
DIV_INS_NULL
};
@ -771,6 +772,56 @@ struct DivInstrumentSNES {
d2(0) {}
};
// ESFM operator structure:
// - DELAY, OUT, MOD, L, R, NOISE
// - Virtual: CT, DT, FIXED
// - 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, fixed;
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(1),
right(1),
fixed(0),
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;
}
};
struct DivInstrument {
String name;
DivInstrumentType type;
@ -787,6 +838,7 @@ struct DivInstrument {
DivInstrumentSoundUnit su;
DivInstrumentES5506 es5506;
DivInstrumentSNES snes;
DivInstrumentESFM esfm;
/**
* these are internal functions.
@ -811,6 +863,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);
@ -831,6 +884,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);

1073
src/engine/platform/esfm.cpp Normal file

File diff suppressed because it is too large Load diff

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

@ -0,0 +1,210 @@
/**
* 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 0x400 bytes, the nearest power of 2.
#define ESFM_REG_POOL_SIZE 0x400
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;
struct {
int baseNoteOverride;
bool fixedArp;
int arpOff;
int pitch2;
bool hasOpArp;
bool hasOpPitch;
} opsState[4];
void handleArpFmOp(int offset=0, int o=0) {
DivMacroInt::IntOp& m=this->std.op[o];
if (m.ssg.had) {
opsState[o].hasOpArp=true;
if (m.ssg.val<0) {
if (!(m.ssg.val&0x40000000)) {
opsState[o].baseNoteOverride=(m.ssg.val|0x40000000)+offset;
opsState[o].fixedArp=true;
} else {
opsState[o].arpOff=m.ssg.val;
opsState[o].fixedArp=false;
}
} else {
if (m.ssg.val&0x40000000) {
opsState[o].baseNoteOverride=(m.ssg.val&(~0x40000000))+offset;
opsState[o].fixedArp=true;
} else {
opsState[o].arpOff=m.ssg.val;
opsState[o].fixedArp=false;
}
}
freqChanged=true;
}
else
{
opsState[o].hasOpArp=false;
}
}
void handlePitchFmOp(int o)
{
DivMacroInt::IntOp& m=this->std.op[o];
if (m.dt.had) {
opsState[o].hasOpPitch=true;
if (m.dt.mode) {
opsState[o].pitch2+=m.dt.val;
CLAMP_VAR(opsState[o].pitch2,-131071,131071);
} else {
opsState[o].pitch2=m.dt.val;
}
this->freqChanged=true;
}
else
{
opsState[o].hasOpPitch=false;
}
}
Channel():
SharedChannel<int>(0),
freqL{0, 0, 0, 0},
freqH{0, 0, 0, 0},
hardReset(false),
globalPan(3),
macroVolMul(64) {
memset(opsState, 0, sizeof(opsState));
}
};
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);
int mapVelocity(int ch, float vel);
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

@ -242,7 +242,13 @@ const char* cmdName[]={
"C64_AD",
"C64_SR",
"ALWAYS_SET_VOLUME"
"ESFM_OP_PANNING",
"ESFM_OUTLVL",
"ESFM_MODIN",
"ESFM_ENV_DELAY",
"ALWAYS_SET_VOLUME",
};
static_assert((sizeof(cmdName)/sizeof(void*))==DIV_CMD_MAX,"update cmdName!");

View file

@ -131,7 +131,8 @@ enum DivSystem {
DIV_SYSTEM_K053260,
DIV_SYSTEM_TED,
DIV_SYSTEM_C140,
DIV_SYSTEM_C219
DIV_SYSTEM_C219,
DIV_SYSTEM_ESFM
};
enum DivEffectType: unsigned short {
@ -397,7 +398,7 @@ struct DivSong {
std::vector<DivEffectStorage> effects;
DivInstrument nullIns, nullInsOPLL, nullInsOPL, nullInsOPLDrums, nullInsQSound;
DivInstrument nullIns, nullInsOPLL, nullInsOPL, nullInsOPLDrums, nullInsQSound, nullInsESFM;
DivWavetable nullWave;
DivSample nullSample;
@ -607,6 +608,49 @@ struct DivSong {
nullInsOPLDrums.fm.op[3].mult=2;
nullInsQSound.std.panLMacro.mode=true;
// ESFM default instrument - port of OPN default instrument
nullInsESFM.esfm.noise=0;
nullInsESFM.esfm.op[0].outLvl=0;
nullInsESFM.esfm.op[0].modIn=4;
nullInsESFM.esfm.op[0].dt=2;
nullInsESFM.fm.op[0].tl=42;
nullInsESFM.fm.op[0].ar=15;
nullInsESFM.fm.op[0].dr=3;
nullInsESFM.fm.op[0].sl=15;
nullInsESFM.fm.op[0].rr=3;
nullInsESFM.fm.op[0].mult=5;
nullInsESFM.esfm.op[1].outLvl=0;
nullInsESFM.esfm.op[1].modIn=7;
nullInsESFM.esfm.op[1].dt=-3;
nullInsESFM.fm.op[1].tl=18;
nullInsESFM.fm.op[1].ar=15;
nullInsESFM.fm.op[1].dr=3;
nullInsESFM.fm.op[1].sl=15;
nullInsESFM.fm.op[1].rr=4;
nullInsESFM.fm.op[1].mult=1;
nullInsESFM.esfm.op[2].outLvl=0;
nullInsESFM.esfm.op[2].modIn=7;
nullInsESFM.esfm.op[2].dt=2;
nullInsESFM.fm.op[2].tl=48;
nullInsESFM.fm.op[2].ar=15;
nullInsESFM.fm.op[2].dr=2;
nullInsESFM.fm.op[2].sl=11;
nullInsESFM.fm.op[2].rr=1;
nullInsESFM.fm.op[2].mult=1;
nullInsESFM.fm.op[2].sus=1;
nullInsESFM.esfm.op[3].outLvl=7;
nullInsESFM.esfm.op[3].modIn=7;
nullInsESFM.esfm.op[3].dt=-3;
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

@ -615,6 +615,56 @@ void DivEngine::registerSystems() {
{0x20, {DIV_CMD_SAMPLE_FREQ, "20xx: Set PCM frequency"}}
};
EffectHandlerMap fmESFMPostEffectHandlerMap={
{0x10, {DIV_CMD_FM_AM_DEPTH, "10xy: Set AM depth (x: operator from 1 to 4 (0 for all ops); y: depth (0: 1dB, 1: 4.8dB))", effectOpVal<4>, effectValAnd<1>}},
{0x12, {DIV_CMD_FM_TL, "12xx: Set level of operator 1 (0 highest, 3F lowest)", constVal<0>, effectVal}},
{0x13, {DIV_CMD_FM_TL, "13xx: Set level of operator 2 (0 highest, 3F lowest)", constVal<1>, effectVal}},
{0x14, {DIV_CMD_FM_TL, "14xx: Set level of operator 3 (0 highest, 3F lowest)", constVal<2>, effectVal}},
{0x15, {DIV_CMD_FM_TL, "15xx: Set level of operator 4 (0 highest, 3F lowest)", constVal<3>, effectVal}},
{0x16, {DIV_CMD_FM_MULT, "16xy: Set operator multiplier (x: operator from 1 to 4; y: multiplier)", effectOpValNoZero<4>, effectValAnd<15>}},
{0x17, {DIV_CMD_FM_PM_DEPTH, "17xy: Set vibrato depth (x: operator from 1 to 4 (0 for all ops); y: depth (0: normal, 1: double))", effectOpVal<4>, effectValAnd<1>}},
{0x19, {DIV_CMD_FM_AR, "19xx: Set attack of all operators (0 to F)", constVal<-1>, effectValAnd<15>}},
{0x1a, {DIV_CMD_FM_AR, "1Axx: Set attack of operator 1 (0 to F)", constVal<0>, effectValAnd<15>}},
{0x1b, {DIV_CMD_FM_AR, "1Bxx: Set attack of operator 2 (0 to F)", constVal<1>, effectValAnd<15>}},
{0x1c, {DIV_CMD_FM_AR, "1Cxx: Set attack of operator 3 (0 to F)", constVal<2>, effectValAnd<15>}},
{0x1d, {DIV_CMD_FM_AR, "1Dxx: Set attack of operator 4 (0 to F)", constVal<3>, effectValAnd<15>}},
{0x20, {DIV_CMD_ESFM_OP_PANNING, "20xy: Set panning of operator 1 (x: left; y: right)", constVal<0>, effectVal}},
{0x21, {DIV_CMD_ESFM_OP_PANNING, "21xy: Set panning of operator 2 (x: left; y: right)", constVal<1>, effectVal}},
{0x22, {DIV_CMD_ESFM_OP_PANNING, "22xy: Set panning of operator 3 (x: left; y: right)", constVal<2>, effectVal}},
{0x23, {DIV_CMD_ESFM_OP_PANNING, "23xy: Set panning of operator 4 (x: left; y: right)", constVal<3>, effectVal}},
{0x24, {DIV_CMD_ESFM_OUTLVL, "24xy: Set output level register (x: operator from 1 to 4 (0 for all ops); y: level from 0 to 7)", effectOpVal<4>, effectValAnd<7>}},
{0x25, {DIV_CMD_ESFM_MODIN, "25xy: Set modulation input level (x: operator from 1 to 4 (0 for all ops); y: level from 0 to 7)", effectOpVal<4>, effectValAnd<7>}},
{0x26, {DIV_CMD_ESFM_ENV_DELAY, "26xy: Set envelope delay (x: operator from 1 to 4 (0 for all ops); y: delay from 0 to 7)", effectOpVal<4>, effectValAnd<7>}},
{0x27, {DIV_CMD_STD_NOISE_MODE, "27xx: Set noise mode for operator 4 (x: mode from 0 to 3)", effectValAnd<3>}},
{0x2a, {DIV_CMD_FM_WS, "2Axy: Set waveform (x: operator from 1 to 4 (0 for all ops); y: waveform from 0 to 7)", effectOpVal<4>, effectValAnd<7>}},
{0x2f, {DIV_CMD_FM_FIXFREQ, "2Fxy: Set fixed frequency block (x: operator from 1 to 4; y: octave from 0 to 7)", effectOpValNoZero<4>, effectValAnd<7>}},
{0x40, {DIV_CMD_FM_DT, "40xx: Set detune of operator 1 (80: center)", constVal<0>, effectVal}},
{0x41, {DIV_CMD_FM_DT, "41xx: Set detune of operator 2 (80: center)", constVal<1>, effectVal}},
{0x42, {DIV_CMD_FM_DT, "42xx: Set detune of operator 3 (80: center)", constVal<2>, effectVal}},
{0x43, {DIV_CMD_FM_DT, "43xx: Set detune of operator 4 (80: center)", constVal<3>, effectVal}},
{0x50, {DIV_CMD_FM_AM, "50xy: Set AM (x: operator from 1 to 4 (0 for all ops); y: AM)", effectOpVal<4>, effectValAnd<1>}},
{0x51, {DIV_CMD_FM_SL, "51xy: Set sustain level (x: operator from 1 to 4 (0 for all ops); y: sustain)", effectOpVal<4>, effectValAnd<15>}},
{0x52, {DIV_CMD_FM_RR, "52xy: Set release (x: operator from 1 to 4 (0 for all ops); y: release)", effectOpVal<4>, effectValAnd<15>}},
{0x53, {DIV_CMD_FM_VIB, "53xy: Set vibrato (x: operator from 1 to 4 (0 for all ops); y: enabled)", effectOpVal<4>, effectValAnd<1>}},
{0x54, {DIV_CMD_FM_RS, "54xy: Set envelope scale (x: operator from 1 to 4 (0 for all ops); y: scale from 0 to 3)", effectOpVal<4>, effectValAnd<3>}},
{0x55, {DIV_CMD_FM_SUS, "55xy: Set envelope sustain (x: operator from 1 to 4 (0 for all ops); y: enabled)", effectOpVal<4>, effectValAnd<1>}},
{0x56, {DIV_CMD_FM_DR, "56xx: Set decay of all operators (0 to F)", constVal<-1>, effectValAnd<15>}},
{0x57, {DIV_CMD_FM_DR, "57xx: Set decay of operator 1 (0 to F)", constVal<0>, effectValAnd<15>}},
{0x58, {DIV_CMD_FM_DR, "58xx: Set decay of operator 2 (0 to F)", constVal<1>, effectValAnd<15>}},
{0x59, {DIV_CMD_FM_DR, "59xx: Set decay of operator 3 (0 to F)", constVal<2>, effectValAnd<15>}},
{0x5a, {DIV_CMD_FM_DR, "5Axx: Set decay of operator 4 (0 to F)", constVal<3>, effectValAnd<15>}},
{0x5b, {DIV_CMD_FM_KSR, "5Bxy: Set whether key will scale envelope (x: operator from 1 to 4 (0 for all ops); y: enabled)", effectOpVal<4>, effectValAnd<1>}}
};
const EffectHandler fmESFMFixFreqFNumHandler[4]={
{DIV_CMD_FM_FIXFREQ, "3xyy: Set fixed frequency F-num of operator 1 (x: high 2 bits from 0 to 3; y: low 8 bits of F-num)", constVal<4>, effectValLong<10>},
{DIV_CMD_FM_FIXFREQ, "3xyy: Set fixed frequency F-num of operator 2 (x: high 2 bits from 4 to 7; y: low 8 bits of F-num)", constVal<5>, effectValLong<10>},
{DIV_CMD_FM_FIXFREQ, "3xyy: Set fixed frequency F-num of operator 3 (x: high 2 bits from 8 to B; y: low 8 bits of F-num)", constVal<6>, effectValLong<10>},
{DIV_CMD_FM_FIXFREQ, "3xyy: Set fixed frequency F-num of operator 4 (x: high 2 bits from C to F; y: low 8 bits of F-num)", constVal<7>, effectValLong<10>},
};
for (int i=0; i<16; i++) {
fmESFMPostEffectHandlerMap.emplace(0x30+i,fmESFMFixFreqFNumHandler[i/4]);
}
// SysDefs
// this chip uses YMZ ADPCM, but the emulator uses ADPCM-B because I got it wrong back then.
@ -1921,6 +1971,20 @@ void DivEngine::registerSystems() {
}
);
sysDefs[DIV_SYSTEM_ESFM]=new DivSysDef(
"ESS ES1xxx series (ESFM)", NULL, 0xd1, 0, 18, true, false, 0, false, 0, 0, 0,
"a unique FM synth featured in PC sound cards.\nbased on the OPL3 design, but with lots of its features extended.",
{"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},
{},
{
{0x2e, {DIV_CMD_FM_HARD_RESET, "2Exx: Toggle hard envelope reset on new notes"}},
},
fmESFMPostEffectHandlerMap
);
sysDefs[DIV_SYSTEM_DUMMY]=new DivSysDef(
"Dummy System", NULL, 0xfd, 0, 8, false, true, 0, false, 0, 0, 0,
"this is a system designed for testing purposes.",

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,76 @@ 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;
unsigned char freqL, freqH;
if (opE.fixed) {
freqL=opE.dt;
freqH=opE.ct&0x1f;
} else {
// perform detune calculation
int offset=(opE.ct<<7)+opE.dt;
double fbase=(mult0?2048.0:1024.0)*pow(2.0,(float)offset/(128.0*12.0));
int bf=round(fbase);
int block=0;
while (bf>0x3ff) {
bf>>=1;
block++;
}
freqL=bf&0xff;
freqH=((block&7)<<2)|((bf>>8)&3);
}
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));
ESFM_WRITE(baseAddr+4,freqL);
ESFM_WRITE(baseAddr+5,(opE.delay<<5)|freqH);
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 +425,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

@ -290,6 +290,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,
@ -1244,8 +1245,10 @@ struct FurnaceGUIMacroDesc {
bool isBitfield, blockMode, bit30;
String (*hoverFunc)(int,float,void*);
void* hoverFuncUser;
bool isArp;
bool isPitch;
FurnaceGUIMacroDesc(const char* name, DivInstrumentMacro* m, int macroMin, int macroMax, float macroHeight, ImVec4 col=ImVec4(1.0f,1.0f,1.0f,1.0f), bool block=false, const char* mName=NULL, String (*hf)(int,float,void*)=NULL, bool bitfield=false, const char** bfVal=NULL, unsigned int bitOff=0, bool bit30Special=false, void* hfu=NULL):
FurnaceGUIMacroDesc(const char* name, DivInstrumentMacro* m, int macroMin, int macroMax, float macroHeight, ImVec4 col=ImVec4(1.0f,1.0f,1.0f,1.0f), bool block=false, const char* mName=NULL, String (*hf)(int,float,void*)=NULL, bool bitfield=false, const char** bfVal=NULL, unsigned int bitOff=0, bool bit30Special=false, void* hfu=NULL, bool isArp=false, bool isPitch=false):
macro(m),
height(macroHeight),
displayName(name),
@ -1257,7 +1260,9 @@ struct FurnaceGUIMacroDesc {
blockMode(block),
bit30(bit30Special),
hoverFunc(hf),
hoverFuncUser(hfu) {
hoverFuncUser(hfu),
isArp(isArp),
isPitch(isPitch) {
// MSVC -> hell
this->min=macroMin;
this->max=macroMax;
@ -1487,6 +1492,7 @@ class FurnaceGUI {
void* fmPreviewOPLL;
void* fmPreviewOPZ;
void* fmPreviewOPZInterface;
void* fmPreviewESFM;
String* editString;
SDL_Event userEvent;
@ -2368,6 +2374,7 @@ class FurnaceGUI {
void drawSSGEnv(unsigned char type, const ImVec2& size);
void drawWaveform(unsigned char type, bool opz, const ImVec2& size);
void drawAlgorithm(unsigned char alg, FurnaceGUIFMAlgs algType, const ImVec2& size);
void drawESFMAlgorithm(DivInstrumentESFM& esfm, const ImVec2& size);
void drawFMEnv(unsigned char tl, unsigned char ar, unsigned char dr, unsigned char d2r, unsigned char rr, unsigned char sl, unsigned char sus, unsigned char egt, unsigned char algOrGlobalSus, float maxTl, float maxArDr, float maxRr, const ImVec2& size, unsigned short instType);
void drawGBEnv(unsigned char vol, unsigned char len, unsigned char sLen, bool dir, const ImVec2& size);
bool drawSysConf(int chan, int sysPos, DivSystem type, DivConfig& flags, bool modifyOnChange, bool fromMenu=false);
@ -2379,6 +2386,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,7 @@ 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},
{"FM (ESFM)",ICON_FA_AREA_CHART,ICON_FUR_INS_ESFM},
{NULL,ICON_FA_QUESTION,ICON_FA_QUESTION}
};
@ -984,6 +985,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)),
@ -1199,6 +1201,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!
};
@ -1234,6 +1237,7 @@ const int chipsFM[]={
DIV_SYSTEM_OPL3,
DIV_SYSTEM_OPL3_DRUMS,
DIV_SYSTEM_OPZ,
DIV_SYSTEM_ESFM,
0 // don't remove this last one!
};

File diff suppressed because it is too large Load diff

View file

@ -28,6 +28,7 @@ const int _EIGHT=8;
const int _TEN=10;
const int _FIFTEEN=15;
const int _SIXTEEN=16;
const int _TWENTY_FOUR=24;
const int _THIRTY_ONE=31;
const int _SIXTY_FOUR=64;
const int _ONE_HUNDRED=100;
@ -37,5 +38,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_TWENTY_FOUR=-24;
const int _MINUS_ONE_HUNDRED_TWENTY_SEVEN=-127;
const int _MINUS_ONE_HUNDRED_TWENTY_EIGHT=-128;

View file

@ -30,6 +30,7 @@ extern const int _EIGHT;
extern const int _TEN;
extern const int _FIFTEEN;
extern const int _SIXTEEN;
extern const int _TWENTY_FOUR;
extern const int _THIRTY_ONE;
extern const int _SIXTY_FOUR;
extern const int _ONE_HUNDRED;
@ -39,5 +40,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_TWENTY_FOUR;
extern const int _MINUS_ONE_HUNDRED_TWENTY_SEVEN;
extern const int _MINUS_ONE_HUNDRED_TWENTY_EIGHT;

View file

@ -1070,6 +1070,17 @@ void FurnaceGUI::initSystemPresets() {
CH(DIV_SYSTEM_PCSPKR, 1.0f, 0, "")
}
);
ENTRY(
"PC + ESS AudioDrive ES1488 (native ESFM mode)", {
CH(DIV_SYSTEM_ESFM, 1.0f, 0, ""),
CH(DIV_SYSTEM_PCM_DAC, 1.0f, 0,
"rate=44100\n"
"outDepth=15\n"
"stereo=true\n"
),
CH(DIV_SYSTEM_PCSPKR, 1.0f, 0, "")
}
);
ENTRY(
"PC + PC-FXGA", {
CH(DIV_SYSTEM_PCE, 1.0f, 0, ""), // HuC6230 (WSG from HuC6280 but with built in 2 OKI ADPCM playback engines)
@ -2364,6 +2375,11 @@ void FurnaceGUI::initSystemPresets() {
)
}
);
ENTRY(
"ESS ES1xxx series (ESFM)", {
CH(DIV_SYSTEM_ESFM, 1.0f, 0, "")
}
);
if (settings.hiddenSystems) {
ENTRY(
"Yamaha YMU759 (MA-2)", {

View file

@ -2279,6 +2279,7 @@ bool FurnaceGUI::drawSysConf(int chan, int sysPos, DivSystem type, DivConfig& fl
case DIV_SYSTEM_C219:
break;
case DIV_SYSTEM_YMU759:
case DIV_SYSTEM_ESFM:
supportsCustomRate=false;
ImGui::Text("nothing to configure");
break;

View file

@ -277,6 +277,8 @@ const char* FurnaceGUI::getSystemPartNumber(DivSystem sys, DivConfig& flags) {
case DIV_SYSTEM_C219:
return "C219";
break;
case DIV_SYSTEM_ESFM:
return "ES1xxx";
default:
return FurnaceGUI::getSystemName(sys);
break;