3rd part of FS#12176. Gain setting migrated to fixed point for libgme.
[kugel-rb.git] / apps / codecs / libgme / nsf_emu.h
blob05b5e5a920ced32add561f98198c127b86c34b68
1 // Nintendo NES/Famicom NSF music file emulator
3 // Game_Music_Emu 0.5.5
4 #ifndef NSF_EMU_H
5 #define NSF_EMU_H
7 #include "rom_data.h"
8 #include "multi_buffer.h"
9 #include "nes_apu.h"
10 #include "nes_cpu.h"
11 #include "nsfe_info.h"
12 #include "m3u_playlist.h"
14 #ifndef NSF_EMU_APU_ONLY
15 #include "nes_namco_apu.h"
16 #include "nes_vrc6_apu.h"
17 #include "nes_fme7_apu.h"
18 #include "nes_fds_apu.h"
19 #include "nes_mmc5_apu.h"
20 #include "nes_vrc7_apu.h"
21 #endif
23 typedef short sample_t;
25 // Sound chip flags
26 enum {
27 vrc6_flag = 1 << 0,
28 vrc7_flag = 1 << 1,
29 fds_flag = 1 << 2,
30 mmc5_flag = 1 << 3,
31 namco_flag = 1 << 4,
32 fme7_flag = 1 << 5
35 enum { fds_banks = 2 };
36 enum { bank_count = fds_banks + 8 };
38 enum { rom_begin = 0x8000 };
39 enum { bank_select_addr = 0x5FF8 };
40 enum { mem_size = 0x10000 };
42 // cpu sits here when waiting for next call to play routine
43 enum { idle_addr = 0x5FF6 };
44 enum { banks_addr = idle_addr };
45 enum { badop_addr = bank_select_addr };
47 enum { low_ram_size = 0x800 };
48 enum { sram_size = 0x2000 };
49 enum { fdsram_size = 0x6000 };
50 enum { fdsram_offset = 0x2000 + page_size + 8 };
51 enum { sram_addr = 0x6000 };
52 enum { unmapped_size= page_size + 8 };
54 enum { buf_size = 2048 };
56 // NSF file header
57 enum { header_size = 0x80 };
58 struct header_t
60 char tag [5];
61 byte vers;
62 byte track_count;
63 byte first_track;
64 byte load_addr [2];
65 byte init_addr [2];
66 byte play_addr [2];
67 char game [32];
68 char author [32];
69 char copyright [32];
70 byte ntsc_speed [2];
71 byte banks [8];
72 byte pal_speed [2];
73 byte speed_flags;
74 byte chip_flags;
75 byte unused [4];
78 struct Nsf_Emu {
79 // Play routine timing
80 nes_time_t next_play;
81 nes_time_t play_period;
82 int play_extra;
83 int play_delay;
84 struct registers_t saved_state; // of interrupted init routine
86 int track_count;
88 // general
89 int max_initial_silence;
90 int voice_count;
91 int mute_mask_;
92 int tempo;
93 int gain;
95 long sample_rate;
97 // track-specific
98 int current_track;
99 blargg_long out_time; // number of samples played since start of track
100 blargg_long emu_time; // number of samples emulator has generated since start of track
101 bool emu_track_ended_; // emulator has reached end of track
102 volatile bool track_ended;
104 // fading
105 blargg_long fade_start;
106 int fade_step;
108 // silence detection
109 int silence_lookahead; // speed to run emulator when looking ahead for silence
110 bool ignore_silence;
111 long silence_time; // number of samples where most recent silence began
112 long silence_count; // number of samples of silence to play before using buf
113 long buf_remain; // number of samples left in silence buffer
115 double clock_rate_;
116 long clock_rate__;
117 unsigned buf_changed_count;
119 // M3u Playlist
120 struct M3u_Playlist m3u;
122 // Larger items at the end
123 #ifndef NSF_EMU_APU_ONLY
124 byte mmc5_mul [2];
126 struct Nes_Fds_Apu fds;
127 struct Nes_Mmc5_Apu mmc5;
128 struct Nes_Namco_Apu namco;
129 struct Nes_Vrc6_Apu vrc6;
130 struct Nes_Fme7_Apu fme7;
131 struct Nes_Vrc7_Apu vrc7;
132 #endif
134 struct Nes_Cpu cpu;
135 struct Nes_Apu apu;
137 // Header for currently loaded file
138 struct header_t header;
140 struct Stereo_Buffer stereo_buf;
141 struct Rom_Data rom;
143 // Extended nsf info
144 struct Nsfe_Info info;
146 sample_t buf [buf_size];
147 byte high_ram[fdsram_size + fdsram_offset];
148 byte low_ram [low_ram_size];
151 // Basic functionality (see Gme_File.h for file loading/track info functions)
153 void Nsf_init( struct Nsf_Emu* this );
154 blargg_err_t Nsf_load( struct Nsf_Emu* this, void* data, long size );
155 blargg_err_t Nsf_post_load( struct Nsf_Emu* this );
157 // Set output sample rate. Must be called only once before loading file.
158 blargg_err_t Nsf_set_sample_rate( struct Nsf_Emu* this, long sample_rate );
160 // Start a track, where 0 is the first track. Also clears warning string.
161 blargg_err_t Nsf_start_track( struct Nsf_Emu* this , int );
163 // Generate 'count' samples info 'buf'. Output is in stereo. Any emulation
164 // errors set warning string, and major errors also end track.
165 blargg_err_t Nsf_play( struct Nsf_Emu* this, long count, sample_t* buf );
167 void Nsf_clear_playlist( struct Nsf_Emu* this );
168 void Nsf_disable_playlist( struct Nsf_Emu* this, bool b ); // use clear_playlist()
170 // Track status/control
172 // Number of milliseconds (1000 msec = 1 second) played since beginning of track
173 long Track_tell( struct Nsf_Emu* this );
175 // Seek to new time in track. Seeking backwards or far forward can take a while.
176 blargg_err_t Track_seek( struct Nsf_Emu* this, long msec );
178 // Skip n samples
179 blargg_err_t Track_skip( struct Nsf_Emu* this, long n );
181 // Set start time and length of track fade out. Once fade ends track_ended() returns
182 // true. Fade time can be changed while track is playing.
183 void Track_set_fade( struct Nsf_Emu* this, long start_msec, long length_msec );
185 // Get track length in milliseconds
186 long Track_length( struct Nsf_Emu* this, int n );
188 // Sound customization
190 // Adjust song tempo, where 1.0 = normal, 0.5 = half speed, 2.0 = double speed.
191 // Track length as returned by track_info() assumes a tempo of 1.0.
192 void Sound_set_tempo( struct Nsf_Emu* this, int t );
194 // Mute/unmute voice i, where voice 0 is first voice
195 void Sound_mute_voice( struct Nsf_Emu* this, int index, bool mute );
197 // Set muting state of all voices at once using a bit mask, where -1 mutes them all,
198 // 0 unmutes them all, 0x01 mutes just the first voice, etc.
199 void Sound_mute_voices( struct Nsf_Emu* this, int mask );
201 // Change overall output amplitude, where 1.0 results in minimal clamping.
202 // Must be called before set_sample_rate().
203 static inline void Sound_set_gain( struct Nsf_Emu* this, int g )
205 assert( !this->sample_rate ); // you must set gain before setting sample rate
206 this->gain = g;
209 // Emulation (You shouldn't touch these)
211 blargg_err_t run_clocks( struct Nsf_Emu* this, blip_time_t* duration, int );
213 void map_memory( struct Nsf_Emu* this ) ICODE_ATTR;
214 void write_bank( struct Nsf_Emu* this, int index, int data ) ICODE_ATTR;
215 int cpu_read( struct Nsf_Emu* this, addr_t ) ICODE_ATTR;
216 void cpu_write( struct Nsf_Emu* this, addr_t, int ) ICODE_ATTR;
217 void push_byte( struct Nsf_Emu* this, int ) ICODE_ATTR;
218 addr_t get_addr( byte const [] ) ICODE_ATTR;
219 bool run_cpu_until( struct Nsf_Emu* this, nes_time_t end ) ICODE_ATTR;
221 // Sets clocks between calls to play routine to p + 1/2 clock
222 static inline void set_play_period( struct Nsf_Emu* this, int p ) { this->play_period = p; }
224 // Time play routine will next be called
225 static inline nes_time_t play_time( struct Nsf_Emu* this ) { return this->next_play; }
227 // Emulates to at least time t. Might emulate a few clocks extra.
228 void run_until( struct Nsf_Emu* this, nes_time_t t ) ICODE_ATTR;
230 // Runs cpu to at least time t and returns false, or returns true
231 // if it encounters illegal instruction (halt).
232 bool run_cpu_until( struct Nsf_Emu* this, nes_time_t t ) ICODE_ATTR;
234 // cpu calls through to these to access memory (except instructions)
235 int read_mem( struct Nsf_Emu* this, addr_t ) ICODE_ATTR;
236 void write_mem( struct Nsf_Emu* this, addr_t, int ) ICODE_ATTR;
238 // Address of play routine
239 static inline addr_t play_addr( struct Nsf_Emu* this ) { return get_addr( this->header.play_addr ); }
241 // Same as run_until, except emulation stops for any event (routine returned,
242 // play routine called, illegal instruction).
243 void run_once( struct Nsf_Emu* this, nes_time_t ) ICODE_ATTR;
245 // Reads byte as cpu would when executing code. Only works for RAM/ROM,
246 // NOT I/O like sound chips.
247 int read_code( struct Nsf_Emu* this, addr_t addr ) ICODE_ATTR;
249 static inline byte* fdsram( struct Nsf_Emu* this ) { return &this->high_ram [fdsram_offset]; }
250 static inline byte* sram( struct Nsf_Emu* this ) { return this->high_ram; }
251 static inline byte* unmapped_code( struct Nsf_Emu* this ) { return &this->high_ram [sram_size]; }
253 #ifndef NSF_EMU_APU_ONLY
254 static inline int fds_enabled( struct Nsf_Emu* this ) { return this->header.chip_flags & fds_flag; }
255 static inline int vrc6_enabled( struct Nsf_Emu* this ) { return this->header.chip_flags & vrc6_flag; }
256 static inline int vrc7_enabled( struct Nsf_Emu* this ) { return this->header.chip_flags & vrc7_flag; }
257 static inline int mmc5_enabled( struct Nsf_Emu* this ) { return this->header.chip_flags & mmc5_flag; }
258 static inline int namco_enabled( struct Nsf_Emu* this ) { return this->header.chip_flags & namco_flag; }
259 static inline int fme7_enabled( struct Nsf_Emu* this ) { return this->header.chip_flags & fme7_flag; }
260 #endif
262 #endif