summaryrefslogtreecommitdiff
path: root/apps/codecs/libgme/nsf_emu.h
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libgme/nsf_emu.h')
-rw-r--r--apps/codecs/libgme/nsf_emu.h262
1 files changed, 262 insertions, 0 deletions
diff --git a/apps/codecs/libgme/nsf_emu.h b/apps/codecs/libgme/nsf_emu.h
new file mode 100644
index 0000000000..421425e339
--- /dev/null
+++ b/apps/codecs/libgme/nsf_emu.h
@@ -0,0 +1,262 @@
1// Nintendo NES/Famicom NSF music file emulator
2
3// Game_Music_Emu 0.5.5
4#ifndef NSF_EMU_H
5#define NSF_EMU_H
6
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"
13
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
22
23typedef short sample_t;
24
25// Sound chip flags
26enum {
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
33};
34
35enum { fds_banks = 2 };
36enum { bank_count = fds_banks + 8 };
37
38enum { rom_begin = 0x8000 };
39enum { bank_select_addr = 0x5FF8 };
40enum { mem_size = 0x10000 };
41
42// cpu sits here when waiting for next call to play routine
43enum { idle_addr = 0x5FF6 };
44enum { banks_addr = idle_addr };
45enum { badop_addr = bank_select_addr };
46
47enum { low_ram_size = 0x800 };
48enum { sram_size = 0x2000 };
49enum { fdsram_size = 0x6000 };
50enum { fdsram_offset = 0x2000 + page_size + 8 };
51enum { sram_addr = 0x6000 };
52enum { unmapped_size= page_size + 8 };
53
54enum { buf_size = 2048 };
55
56// NSF file header
57enum { header_size = 0x80 };
58struct header_t
59{
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];
76};
77
78struct 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
85
86 int track_count;
87
88 // general
89 int max_initial_silence;
90 int voice_count;
91 int mute_mask_;
92 double tempo;
93 double gain;
94
95 long sample_rate;
96
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;
103
104 // fading
105 blargg_long fade_start;
106 int fade_step;
107
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
114
115 double clock_rate_;
116 long clock_rate__;
117 unsigned buf_changed_count;
118
119 // M3u Playlist
120 struct M3u_Playlist m3u;
121
122 // Larger items at the end
123 #ifndef NSF_EMU_APU_ONLY
124 byte mmc5_mul [2];
125
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
133
134 struct Nes_Cpu cpu;
135 struct Nes_Apu apu;
136
137 // Header for currently loaded file
138 struct header_t header;
139
140 struct Stereo_Buffer stereo_buf;
141 struct Rom_Data rom;
142
143 // Extended nsf info
144 struct Nsfe_Info info;
145
146 sample_t buf [buf_size];
147 byte high_ram[fdsram_size + fdsram_offset];
148 byte low_ram [low_ram_size];
149};
150
151// Basic functionality (see Gme_File.h for file loading/track info functions)
152
153void Nsf_init( struct Nsf_Emu* this );
154blargg_err_t Nsf_load( struct Nsf_Emu* this, void* data, long size );
155blargg_err_t Nsf_post_load( struct Nsf_Emu* this );
156
157// Set output sample rate. Must be called only once before loading file.
158blargg_err_t Nsf_set_sample_rate( struct Nsf_Emu* this, long sample_rate );
159
160// Start a track, where 0 is the first track. Also clears warning string.
161blargg_err_t Nsf_start_track( struct Nsf_Emu* this , int );
162
163// Generate 'count' samples info 'buf'. Output is in stereo. Any emulation
164// errors set warning string, and major errors also end track.
165blargg_err_t Nsf_play( struct Nsf_Emu* this, long count, sample_t* buf );
166
167void Nsf_clear_playlist( struct Nsf_Emu* this );
168void Nsf_disable_playlist( struct Nsf_Emu* this, bool b ); // use clear_playlist()
169
170// Track status/control
171
172// Number of milliseconds (1000 msec = 1 second) played since beginning of track
173long Track_tell( struct Nsf_Emu* this );
174
175// Seek to new time in track. Seeking backwards or far forward can take a while.
176blargg_err_t Track_seek( struct Nsf_Emu* this, long msec );
177
178// Skip n samples
179blargg_err_t Track_skip( struct Nsf_Emu* this, long n );
180
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.
183void Track_set_fade( struct Nsf_Emu* this, long start_msec, long length_msec );
184
185// Get track length in milliseconds
186long Track_length( struct Nsf_Emu* this, int n );
187
188// Sound customization
189
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.
192void Sound_set_tempo( struct Nsf_Emu* this, double t );
193
194// Mute/unmute voice i, where voice 0 is first voice
195void Sound_mute_voice( struct Nsf_Emu* this, int index, bool mute );
196
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.
199void Sound_mute_voices( struct Nsf_Emu* this, int mask );
200
201// Change overall output amplitude, where 1.0 results in minimal clamping.
202// Must be called before set_sample_rate().
203static inline void Sound_set_gain( struct Nsf_Emu* this, double g )
204{
205 assert( !this->sample_rate ); // you must set gain before setting sample rate
206 this->gain = g;
207}
208
209// Emulation (You shouldn't touch these)
210
211blargg_err_t run_clocks( struct Nsf_Emu* this, blip_time_t* duration, int );
212
213void map_memory( struct Nsf_Emu* this ) ICODE_ATTR;
214void write_bank( struct Nsf_Emu* this, int index, int data ) ICODE_ATTR;
215int cpu_read( struct Nsf_Emu* this, addr_t ) ICODE_ATTR;
216void cpu_write( struct Nsf_Emu* this, addr_t, int ) ICODE_ATTR;
217void push_byte( struct Nsf_Emu* this, int ) ICODE_ATTR;
218addr_t get_addr( byte const [] ) ICODE_ATTR;
219bool run_cpu_until( struct Nsf_Emu* this, nes_time_t end ) ICODE_ATTR;
220
221// Sets clocks between calls to play routine to p + 1/2 clock
222static inline void set_play_period( struct Nsf_Emu* this, int p ) { this->play_period = p; }
223
224// Time play routine will next be called
225static inline nes_time_t play_time( struct Nsf_Emu* this ) { return this->next_play; }
226
227// Emulates to at least time t. Might emulate a few clocks extra.
228void run_until( struct Nsf_Emu* this, nes_time_t t ) ICODE_ATTR;
229
230// Runs cpu to at least time t and returns false, or returns true
231// if it encounters illegal instruction (halt).
232bool run_cpu_until( struct Nsf_Emu* this, nes_time_t t ) ICODE_ATTR;
233
234// cpu calls through to these to access memory (except instructions)
235int read_mem( struct Nsf_Emu* this, addr_t ) ICODE_ATTR;
236void write_mem( struct Nsf_Emu* this, addr_t, int ) ICODE_ATTR;
237
238// Address of play routine
239static inline addr_t play_addr( struct Nsf_Emu* this ) { return get_addr( this->header.play_addr ); }
240
241// Same as run_until, except emulation stops for any event (routine returned,
242// play routine called, illegal instruction).
243void run_once( struct Nsf_Emu* this, nes_time_t ) ICODE_ATTR;
244
245// Reads byte as cpu would when executing code. Only works for RAM/ROM,
246// NOT I/O like sound chips.
247int read_code( struct Nsf_Emu* this, addr_t addr ) ICODE_ATTR;
248
249static inline byte* fdsram( struct Nsf_Emu* this ) { return &this->high_ram [fdsram_offset]; }
250static inline byte* sram( struct Nsf_Emu* this ) { return this->high_ram; }
251static inline byte* unmapped_code( struct Nsf_Emu* this ) { return &this->high_ram [sram_size]; }
252
253#ifndef NSF_EMU_APU_ONLY
254static inline int fds_enabled( struct Nsf_Emu* this ) { return this->header.chip_flags & fds_flag; }
255static inline int vrc6_enabled( struct Nsf_Emu* this ) { return this->header.chip_flags & vrc6_flag; }
256static inline int vrc7_enabled( struct Nsf_Emu* this ) { return this->header.chip_flags & vrc7_flag; }
257static inline int mmc5_enabled( struct Nsf_Emu* this ) { return this->header.chip_flags & mmc5_flag; }
258static inline int namco_enabled( struct Nsf_Emu* this ) { return this->header.chip_flags & namco_flag; }
259static inline int fme7_enabled( struct Nsf_Emu* this ) { return this->header.chip_flags & fme7_flag; }
260#endif
261
262#endif