diff options
author | Sean Bartell <wingedtachikoma@gmail.com> | 2011-06-25 21:32:25 -0400 |
---|---|---|
committer | Nils Wallménius <nils@rockbox.org> | 2012-04-25 22:13:20 +0200 |
commit | f40bfc9267b13b54e6379dfe7539447662879d24 (patch) | |
tree | 9b20069d5e62809ff434061ad730096836f916f2 /lib/rbcodec/codecs/libgme/nsf_emu.h | |
parent | a0009907de7a0107d49040d8a180f140e2eff299 (diff) | |
download | rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.tar.gz rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.zip |
Add codecs to librbcodec.
Change-Id: Id7f4717d51ed02d67cb9f9cb3c0ada4a81843f97
Reviewed-on: http://gerrit.rockbox.org/137
Reviewed-by: Nils Wallménius <nils@rockbox.org>
Tested-by: Nils Wallménius <nils@rockbox.org>
Diffstat (limited to 'lib/rbcodec/codecs/libgme/nsf_emu.h')
-rw-r--r-- | lib/rbcodec/codecs/libgme/nsf_emu.h | 261 |
1 files changed, 261 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libgme/nsf_emu.h b/lib/rbcodec/codecs/libgme/nsf_emu.h new file mode 100644 index 0000000000..00bdad4a4e --- /dev/null +++ b/lib/rbcodec/codecs/libgme/nsf_emu.h | |||
@@ -0,0 +1,261 @@ | |||
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 | #include "track_filter.h" | ||
14 | |||
15 | #ifndef NSF_EMU_APU_ONLY | ||
16 | #include "nes_namco_apu.h" | ||
17 | #include "nes_vrc6_apu.h" | ||
18 | #include "nes_fme7_apu.h" | ||
19 | #include "nes_fds_apu.h" | ||
20 | #include "nes_mmc5_apu.h" | ||
21 | #ifndef NSF_EMU_NO_VRC7 | ||
22 | #include "nes_vrc7_apu.h" | ||
23 | #endif | ||
24 | #endif | ||
25 | |||
26 | // Sound chip flags | ||
27 | enum { | ||
28 | vrc6_flag = 1 << 0, | ||
29 | vrc7_flag = 1 << 1, | ||
30 | fds_flag = 1 << 2, | ||
31 | mmc5_flag = 1 << 3, | ||
32 | namco_flag = 1 << 4, | ||
33 | fme7_flag = 1 << 5 | ||
34 | }; | ||
35 | |||
36 | enum { fds_banks = 2 }; | ||
37 | enum { bank_count = fds_banks + 8 }; | ||
38 | |||
39 | enum { rom_begin = 0x8000 }; | ||
40 | enum { bank_select_addr = 0x5FF8 }; | ||
41 | enum { mem_size = 0x10000 }; | ||
42 | |||
43 | // cpu sits here when waiting for next call to play routine | ||
44 | enum { idle_addr = 0x5FF6 }; | ||
45 | enum { banks_addr = idle_addr }; | ||
46 | enum { badop_addr = bank_select_addr }; | ||
47 | |||
48 | enum { low_ram_size = 0x800 }; | ||
49 | enum { sram_size = 0x2000 }; | ||
50 | enum { fdsram_size = 0x6000 }; | ||
51 | enum { fdsram_offset = 0x2000 + page_size + 8 }; | ||
52 | enum { sram_addr = 0x6000 }; | ||
53 | enum { unmapped_size= page_size + 8 }; | ||
54 | enum { max_voices = 32 }; | ||
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 | // general | ||
87 | int voice_count; | ||
88 | int voice_types [32]; | ||
89 | int mute_mask_; | ||
90 | int tempo; | ||
91 | int gain; | ||
92 | |||
93 | int sample_rate; | ||
94 | |||
95 | // track-specific | ||
96 | int track_count; | ||
97 | int current_track; | ||
98 | |||
99 | int clock_rate__; | ||
100 | unsigned buf_changed_count; | ||
101 | |||
102 | // M3u Playlist | ||
103 | struct M3u_Playlist m3u; | ||
104 | |||
105 | // Larger items at the end | ||
106 | #ifndef NSF_EMU_APU_ONLY | ||
107 | byte mmc5_mul [2]; | ||
108 | |||
109 | struct Nes_Fds_Apu fds; | ||
110 | struct Nes_Mmc5_Apu mmc5; | ||
111 | struct Nes_Namco_Apu namco; | ||
112 | struct Nes_Vrc6_Apu vrc6; | ||
113 | struct Nes_Fme7_Apu fme7; | ||
114 | #ifndef NSF_EMU_NO_VRC7 | ||
115 | struct Nes_Vrc7_Apu vrc7; | ||
116 | #endif | ||
117 | #endif | ||
118 | |||
119 | struct Nes_Cpu cpu; | ||
120 | struct Nes_Apu apu; | ||
121 | |||
122 | // Header for currently loaded file | ||
123 | struct header_t header; | ||
124 | |||
125 | struct setup_t tfilter; | ||
126 | struct Track_Filter track_filter; | ||
127 | |||
128 | struct Multi_Buffer stereo_buf; | ||
129 | struct Rom_Data rom; | ||
130 | |||
131 | // Extended nsf info | ||
132 | struct Nsfe_Info info; | ||
133 | |||
134 | byte high_ram[fdsram_size + fdsram_offset]; | ||
135 | byte low_ram [low_ram_size]; | ||
136 | }; | ||
137 | |||
138 | // Basic functionality (see Gme_File.h for file loading/track info functions) | ||
139 | |||
140 | void Nsf_init( struct Nsf_Emu* this ); | ||
141 | blargg_err_t Nsf_load_mem( struct Nsf_Emu* this, void* data, long size ); | ||
142 | blargg_err_t Nsf_post_load( struct Nsf_Emu* this ); | ||
143 | |||
144 | // Set output sample rate. Must be called only once before loading file. | ||
145 | blargg_err_t Nsf_set_sample_rate( struct Nsf_Emu* this, int sample_rate ); | ||
146 | |||
147 | // Start a track, where 0 is the first track. Also clears warning string. | ||
148 | blargg_err_t Nsf_start_track( struct Nsf_Emu* this , int ); | ||
149 | |||
150 | // Generate 'count' samples info 'buf'. Output is in stereo. Any emulation | ||
151 | // errors set warning string, and major errors also end track. | ||
152 | blargg_err_t Nsf_play( struct Nsf_Emu* this, int count, sample_t* buf ); | ||
153 | |||
154 | void Nsf_clear_playlist( struct Nsf_Emu* this ); | ||
155 | void Nsf_disable_playlist( struct Nsf_Emu* this, bool b ); // use clear_playlist() | ||
156 | |||
157 | // Track status/control | ||
158 | |||
159 | // Number of milliseconds (1000 msec = 1 second) played since beginning of track | ||
160 | int Track_tell( struct Nsf_Emu* this ); | ||
161 | |||
162 | // Seek to new time in track. Seeking backwards or far forward can take a while. | ||
163 | blargg_err_t Track_seek( struct Nsf_Emu* this, int msec ); | ||
164 | |||
165 | // Skip n samples | ||
166 | blargg_err_t Track_skip( struct Nsf_Emu* this, int n ); | ||
167 | |||
168 | // Set start time and length of track fade out. Once fade ends track_ended() returns | ||
169 | // true. Fade time can be changed while track is playing. | ||
170 | void Track_set_fade( struct Nsf_Emu* this, int start_msec, int length_msec ); | ||
171 | |||
172 | // True if a track has reached its end | ||
173 | static inline bool Track_ended( struct Nsf_Emu* this ) | ||
174 | { | ||
175 | return track_ended( &this->track_filter ); | ||
176 | } | ||
177 | |||
178 | // Disables automatic end-of-track detection and skipping of silence at beginning | ||
179 | static inline void Track_ignore_silence( struct Nsf_Emu* this, bool disable ) | ||
180 | { | ||
181 | this->track_filter.silence_ignored_ = disable; | ||
182 | } | ||
183 | |||
184 | // Get track length in milliseconds | ||
185 | int Track_length( struct Nsf_Emu* this, int n ); | ||
186 | |||
187 | // Sound customization | ||
188 | |||
189 | // Adjust song tempo, where 1.0 = normal, 0.5 = half speed, 2.0 = double speed. | ||
190 | // Track length as returned by track_info() assumes a tempo of 1.0. | ||
191 | void Sound_set_tempo( struct Nsf_Emu* this, int t ); | ||
192 | |||
193 | // Mute/unmute voice i, where voice 0 is first voice | ||
194 | void Sound_mute_voice( struct Nsf_Emu* this, int index, bool mute ); | ||
195 | |||
196 | // Set muting state of all voices at once using a bit mask, where -1 mutes them all, | ||
197 | // 0 unmutes them all, 0x01 mutes just the first voice, etc. | ||
198 | void Sound_mute_voices( struct Nsf_Emu* this, int mask ); | ||
199 | |||
200 | // Change overall output amplitude, where 1.0 results in minimal clamping. | ||
201 | // Must be called before set_sample_rate(). | ||
202 | static inline void Sound_set_gain( struct Nsf_Emu* this, int g ) | ||
203 | { | ||
204 | assert( !this->sample_rate ); // you must set gain before setting sample rate | ||
205 | this->gain = g; | ||
206 | } | ||
207 | |||
208 | // Emulation (You shouldn't touch these) | ||
209 | |||
210 | blargg_err_t run_clocks( struct Nsf_Emu* this, blip_time_t* duration, int ); | ||
211 | |||
212 | void write_bank( struct Nsf_Emu* this, int index, int data ); | ||
213 | int cpu_read( struct Nsf_Emu* this, addr_t ); | ||
214 | void cpu_write( struct Nsf_Emu* this, addr_t, int ); | ||
215 | addr_t get_addr( byte const [] ); | ||
216 | bool run_cpu_until( struct Nsf_Emu* this, nes_time_t end ); | ||
217 | |||
218 | // Sets clocks between calls to play routine to p + 1/2 clock | ||
219 | static inline void set_play_period( struct Nsf_Emu* this, int p ) { this->play_period = p; } | ||
220 | |||
221 | // Time play routine will next be called | ||
222 | static inline nes_time_t play_time( struct Nsf_Emu* this ) { return this->next_play; } | ||
223 | |||
224 | // Emulates to at least time t. Might emulate a few clocks extra. | ||
225 | void run_until( struct Nsf_Emu* this, nes_time_t t ); | ||
226 | |||
227 | // Runs cpu to at least time t and returns false, or returns true | ||
228 | // if it encounters illegal instruction (halt). | ||
229 | bool run_cpu_until( struct Nsf_Emu* this, nes_time_t t ); | ||
230 | |||
231 | // cpu calls through to these to access memory (except instructions) | ||
232 | int read_mem( struct Nsf_Emu* this, addr_t ); | ||
233 | void write_mem( struct Nsf_Emu* this, addr_t, int ); | ||
234 | |||
235 | // Address of play routine | ||
236 | static inline addr_t play_addr( struct Nsf_Emu* this ) { return get_addr( this->header.play_addr ); } | ||
237 | |||
238 | // Same as run_until, except emulation stops for any event (routine returned, | ||
239 | // play routine called, illegal instruction). | ||
240 | void run_once( struct Nsf_Emu* this, nes_time_t ); | ||
241 | |||
242 | // Reads byte as cpu would when executing code. Only works for RAM/ROM, | ||
243 | // NOT I/O like sound chips. | ||
244 | int read_code( struct Nsf_Emu* this, addr_t addr ); | ||
245 | |||
246 | static inline byte* fdsram( struct Nsf_Emu* this ) { return &this->high_ram [fdsram_offset]; } | ||
247 | static inline byte* sram( struct Nsf_Emu* this ) { return this->high_ram; } | ||
248 | static inline byte* unmapped_code( struct Nsf_Emu* this ) { return &this->high_ram [sram_size]; } | ||
249 | |||
250 | #ifndef NSF_EMU_APU_ONLY | ||
251 | static inline int fds_enabled( struct Nsf_Emu* this ) { return this->header.chip_flags & fds_flag; } | ||
252 | static inline int vrc6_enabled( struct Nsf_Emu* this ) { return this->header.chip_flags & vrc6_flag; } | ||
253 | #ifndef NSF_EMU_NO_VRC7 | ||
254 | static inline int vrc7_enabled( struct Nsf_Emu* this ) { return this->header.chip_flags & vrc7_flag; } | ||
255 | #endif | ||
256 | static inline int mmc5_enabled( struct Nsf_Emu* this ) { return this->header.chip_flags & mmc5_flag; } | ||
257 | static inline int namco_enabled( struct Nsf_Emu* this ) { return this->header.chip_flags & namco_flag; } | ||
258 | static inline int fme7_enabled( struct Nsf_Emu* this ) { return this->header.chip_flags & fme7_flag; } | ||
259 | #endif | ||
260 | |||
261 | #endif | ||