-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesfm.h
289 lines (249 loc) · 7.32 KB
/
esfm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* 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.
* - akumanatt
* For helping out with code optimization.
* - 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.
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;
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;
flag eg_delay_transitioned_10;
flag eg_delay_transitioned_10_gate;
flag eg_delay_transitioned_01;
flag eg_delay_transitioned_01_gate;
uint16 eg_delay_counter;
uint16 eg_delay_counter_compare;
} 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];
uint16 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;
double timer_accumulator[2];
uint8 timer_reload[2];
uint8 timer_counter[2];
flag timer_enable[2];
flag timer_mask[2];
flag timer_overflow[2];
flag irq_bit;
// -- Test bits (NOT IMPLEMENTED) --
// Halts the envelope generators from advancing. Written on bit 0, read back from bit 5.
flag test_bit_w0_r5_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_1_distort;
// Seems to do nothing.
flag test_bit_2;
// Seems to do nothing.
flag test_bit_3;
// Appears to attenuate the output by about 3 dB.
flag test_bit_4_attenuate;
// Written on bit 5, read back from bit 0. Seems to do nothing.
flag test_bit_w5_r0;
// Resets all phase generators and holds them in the reset state while this bit is set.
flag test_bit_6_phase_stop_reset;
// Seems to do nothing.
flag test_bit_7;
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