diff options
author | Andree Buschmann <AndreeBuschmann@t-online.de> | 2011-08-07 20:01:04 +0000 |
---|---|---|
committer | Andree Buschmann <AndreeBuschmann@t-online.de> | 2011-08-07 20:01:04 +0000 |
commit | acb0917556fc33681c1df5a530cf754193e67705 (patch) | |
tree | 052a47097009a210e4aed9c207bd6aa4828cc000 /apps/codecs/libgme/nsf_emu.h | |
parent | 93c6f1329a5691a8be158cefe15641bd1daf9ef8 (diff) | |
download | rockbox-acb0917556fc33681c1df5a530cf754193e67705.tar.gz rockbox-acb0917556fc33681c1df5a530cf754193e67705.zip |
Submit initial patch from FS#12176. Adds support for several new game music formats (AY, GBS, HES, KSS, SGC, VGM and VGZ) and replaces the current NSF and NSFE with a new implementation based on a port of the Game Music Emu library 'GME'. This first submit does not cover the full functionality provided by the author's original patch: Coleco-SGV is not supported, some GME-specific m3u-support has been removed and IRAM is not used yet. Further changes are very likely to follow this submit. Thanks to Mauricio Garrido.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30264 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/codecs/libgme/nsf_emu.h')
-rw-r--r-- | apps/codecs/libgme/nsf_emu.h | 262 |
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 | |||
23 | typedef short sample_t; | ||
24 | |||
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 | ||
33 | }; | ||
34 | |||
35 | enum { fds_banks = 2 }; | ||
36 | enum { bank_count = fds_banks + 8 }; | ||
37 | |||
38 | enum { rom_begin = 0x8000 }; | ||
39 | enum { bank_select_addr = 0x5FF8 }; | ||
40 | enum { mem_size = 0x10000 }; | ||
41 | |||
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 }; | ||
46 | |||
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 }; | ||
53 | |||
54 | enum { buf_size = 2048 }; | ||
55 | |||
56 | // NSF file header | ||
57 | enum { header_size = 0x80 }; | ||
58 | struct 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 | |||
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 | ||
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 | |||
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 ); | ||
156 | |||
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 ); | ||
159 | |||
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 ); | ||
162 | |||
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 ); | ||
166 | |||
167 | void Nsf_clear_playlist( struct Nsf_Emu* this ); | ||
168 | void 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 | ||
173 | long Track_tell( struct Nsf_Emu* this ); | ||
174 | |||
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 ); | ||
177 | |||
178 | // Skip n samples | ||
179 | blargg_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. | ||
183 | void Track_set_fade( struct Nsf_Emu* this, long start_msec, long length_msec ); | ||
184 | |||
185 | // Get track length in milliseconds | ||
186 | long 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. | ||
192 | void Sound_set_tempo( struct Nsf_Emu* this, double t ); | ||
193 | |||
194 | // Mute/unmute voice i, where voice 0 is first voice | ||
195 | void 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. | ||
199 | void 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(). | ||
203 | static 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 | |||
211 | blargg_err_t run_clocks( struct Nsf_Emu* this, blip_time_t* duration, int ); | ||
212 | |||
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; | ||
220 | |||
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; } | ||
223 | |||
224 | // Time play routine will next be called | ||
225 | static 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. | ||
228 | void 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). | ||
232 | bool 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) | ||
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; | ||
237 | |||
238 | // Address of play routine | ||
239 | static 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). | ||
243 | void 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. | ||
247 | int read_code( struct Nsf_Emu* this, addr_t addr ) ICODE_ATTR; | ||
248 | |||
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]; } | ||
252 | |||
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 | ||
261 | |||
262 | #endif | ||