diff options
Diffstat (limited to 'apps/codecs/libgme/gbs_emu.h')
-rw-r--r-- | apps/codecs/libgme/gbs_emu.h | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/apps/codecs/libgme/gbs_emu.h b/apps/codecs/libgme/gbs_emu.h new file mode 100644 index 0000000000..c107264f9f --- /dev/null +++ b/apps/codecs/libgme/gbs_emu.h | |||
@@ -0,0 +1,204 @@ | |||
1 | // Nintendo Game Boy GBS music file emulator | ||
2 | |||
3 | // Game_Music_Emu 0.5.2 | ||
4 | #ifndef GBS_EMU_H | ||
5 | #define GBS_EMU_H | ||
6 | |||
7 | #include "rom_data.h" | ||
8 | #include "multi_buffer.h" | ||
9 | #include "gb_apu.h" | ||
10 | #include "gb_cpu.h" | ||
11 | #include "m3u_playlist.h" | ||
12 | |||
13 | /* typedef uint8_t byte; */ | ||
14 | typedef short sample_t; | ||
15 | |||
16 | enum { joypad_addr = 0xFF00 }; | ||
17 | enum { ram_addr = 0xA000 }; | ||
18 | enum { hi_page = 0xFF00 - ram_addr }; | ||
19 | enum { io_base = 0xFF00 }; | ||
20 | enum { buf_size = 2048 }; | ||
21 | |||
22 | // Selects which sound hardware to use. AGB hardware is cleaner than the | ||
23 | // others. Doesn't take effect until next start_track(). | ||
24 | enum sound_t { | ||
25 | sound_dmg = mode_dmg, // Game Boy monochrome | ||
26 | sound_cgb = mode_cgb, // Game Boy Color | ||
27 | sound_agb = mode_agb, // Game Boy Advance | ||
28 | sound_gbs // Use DMG/CGB based on GBS (default) | ||
29 | }; | ||
30 | |||
31 | // GBS file header | ||
32 | enum { header_size = 112 }; | ||
33 | struct header_t | ||
34 | { | ||
35 | char tag [3]; | ||
36 | byte vers; | ||
37 | byte track_count; | ||
38 | byte first_track; | ||
39 | byte load_addr [2]; | ||
40 | byte init_addr [2]; | ||
41 | byte play_addr [2]; | ||
42 | byte stack_ptr [2]; | ||
43 | byte timer_modulo; | ||
44 | byte timer_mode; | ||
45 | char game [32]; | ||
46 | char author [32]; | ||
47 | char copyright [32]; | ||
48 | }; | ||
49 | |||
50 | struct Gbs_Emu { | ||
51 | enum sound_t sound_hardware; | ||
52 | |||
53 | int tempo; | ||
54 | |||
55 | // timer | ||
56 | blip_time_t cpu_time; | ||
57 | blip_time_t end_time; | ||
58 | blip_time_t play_period; | ||
59 | blip_time_t next_play; | ||
60 | |||
61 | // Sound | ||
62 | long clock_rate_; | ||
63 | long sample_rate_; | ||
64 | unsigned buf_changed_count; | ||
65 | int voice_count_; | ||
66 | double gain_; | ||
67 | double tempo_; | ||
68 | |||
69 | // track-specific | ||
70 | byte track_count; | ||
71 | volatile bool track_ended; | ||
72 | int current_track_; | ||
73 | blargg_long out_time; // number of samples played since start of track | ||
74 | blargg_long emu_time; // number of samples emulator has generated since start of track | ||
75 | bool emu_track_ended_; // emulator has reached end of track | ||
76 | |||
77 | // fading | ||
78 | blargg_long fade_start; | ||
79 | int fade_step; | ||
80 | |||
81 | // silence detection | ||
82 | // Disable automatic end-of-track detection and skipping of silence at beginning | ||
83 | bool ignore_silence; | ||
84 | |||
85 | int max_initial_silence; | ||
86 | int mute_mask_; | ||
87 | int silence_lookahead; // speed to run emulator when looking ahead for silence | ||
88 | long silence_time; // number of samples where most recent silence began | ||
89 | long silence_count; // number of samples of silence to play before using buf | ||
90 | long buf_remain; // number of samples left in silence buffer | ||
91 | |||
92 | // Larger items at the end | ||
93 | // Header for currently loaded file | ||
94 | struct header_t header; | ||
95 | |||
96 | // M3u Playlist | ||
97 | struct M3u_Playlist m3u; | ||
98 | |||
99 | struct Gb_Apu apu; | ||
100 | struct Gb_Cpu cpu; | ||
101 | struct Stereo_Buffer stereo_buf; | ||
102 | |||
103 | sample_t buf [buf_size]; | ||
104 | |||
105 | // rom & ram | ||
106 | struct Rom_Data rom; | ||
107 | byte ram [0x4000 + 0x2000 + cpu_padding]; | ||
108 | }; | ||
109 | |||
110 | |||
111 | // Basic functionality | ||
112 | // Initializes Gbs_Emu structure | ||
113 | void Gbs_init( struct Gbs_Emu* this ); | ||
114 | |||
115 | // Stops (clear) Gbs_Emu structure | ||
116 | void Gbs_stop( struct Gbs_Emu* this ); | ||
117 | |||
118 | // Loads a file from memory | ||
119 | blargg_err_t Gbs_load( struct Gbs_Emu* this, void* data, long size ); | ||
120 | |||
121 | // Set output sample rate. Must be called only once before loading file. | ||
122 | blargg_err_t Gbs_set_sample_rate( struct Gbs_Emu* this, long sample_rate ); | ||
123 | |||
124 | // Start a track, where 0 is the first track. Also clears warning string. | ||
125 | blargg_err_t Gbs_start_track( struct Gbs_Emu* this, int ); | ||
126 | |||
127 | // Generate 'count' samples info 'buf'. Output is in stereo. Any emulation | ||
128 | // errors set warning string, and major errors also end track. | ||
129 | blargg_err_t Gbs_play( struct Gbs_Emu* this, long count, sample_t* buf ) ICODE_ATTR; | ||
130 | |||
131 | // Track status/control | ||
132 | // Number of milliseconds (1000 msec = 1 second) played since beginning of track | ||
133 | long Track_tell( struct Gbs_Emu* this ); | ||
134 | |||
135 | // Seek to new time in track. Seeking backwards or far forward can take a while. | ||
136 | blargg_err_t Track_seek( struct Gbs_Emu* this, long msec ); | ||
137 | |||
138 | // Skip n samples | ||
139 | blargg_err_t Track_skip( struct Gbs_Emu* this, long n ); | ||
140 | |||
141 | // Set start time and length of track fade out. Once fade ends track_ended() returns | ||
142 | // true. Fade time can be changed while track is playing. | ||
143 | void Track_set_fade( struct Gbs_Emu* this, long start_msec, long length_msec ); | ||
144 | |||
145 | // Get track length in milliseconds | ||
146 | static inline long Track_get_length( struct Gbs_Emu* this, int n ) | ||
147 | { | ||
148 | long length = 120 * 1000; /* 2 minutes */ | ||
149 | if ( (this->m3u.size > 0) && (n < this->m3u.size) ) { | ||
150 | struct entry_t* entry = &this->m3u.entries [n]; | ||
151 | length = entry->length; | ||
152 | } | ||
153 | |||
154 | return length; | ||
155 | } | ||
156 | |||
157 | |||
158 | // Sound customization | ||
159 | // Adjust song tempo, where 1.0 = normal, 0.5 = half speed, 2.0 = double speed. | ||
160 | // Track length as returned by track_info() assumes a tempo of 1.0. | ||
161 | void Sound_set_tempo( struct Gbs_Emu* this, double ); | ||
162 | |||
163 | // Mute/unmute voice i, where voice 0 is first voice | ||
164 | void Sound_mute_voice( struct Gbs_Emu* this, int index, bool mute ); | ||
165 | |||
166 | // Set muting state of all voices at once using a bit mask, where -1 mutes them all, | ||
167 | // 0 unmutes them all, 0x01 mutes just the first voice, etc. | ||
168 | void Sound_mute_voices( struct Gbs_Emu* this, int mask ); | ||
169 | |||
170 | // Change overall output amplitude, where 1.0 results in minimal clamping. | ||
171 | // Must be called before set_sample_rate(). | ||
172 | static inline void Sound_set_gain( struct Gbs_Emu* this, double g ) | ||
173 | { | ||
174 | assert( !this->sample_rate_ ); // you must set gain before setting sample rate | ||
175 | this->gain_ = g; | ||
176 | } | ||
177 | |||
178 | |||
179 | // Emulation (You shouldn't touch these) | ||
180 | |||
181 | blargg_err_t Run_clocks( struct Gbs_Emu* this, blip_time_t duration ) ICODE_ATTR; | ||
182 | void Set_bank( struct Gbs_Emu* this, int ) ICODE_ATTR; | ||
183 | void Update_timer( struct Gbs_Emu* this ) ICODE_ATTR; | ||
184 | |||
185 | // Runs CPU until time becomes >= 0 | ||
186 | void Run_cpu( struct Gbs_Emu* this ) ICODE_ATTR; | ||
187 | |||
188 | // Reads/writes memory and I/O | ||
189 | int Read_mem( struct Gbs_Emu* this, addr_t addr ) ICODE_ATTR; | ||
190 | void Write_mem( struct Gbs_Emu* this, addr_t addr, int data ) ICODE_ATTR; | ||
191 | |||
192 | // Current time | ||
193 | static inline blip_time_t Time( struct Gbs_Emu* this ) | ||
194 | { | ||
195 | return Cpu_time( &this->cpu ) + this->end_time; | ||
196 | } | ||
197 | |||
198 | void Jsr_then_stop( struct Gbs_Emu* this, byte const [] ) ICODE_ATTR; | ||
199 | void Write_io_inline( struct Gbs_Emu* this, int offset, int data, int base ) ICODE_ATTR; | ||
200 | void Write_io_( struct Gbs_Emu* this, int offset, int data ) ICODE_ATTR; | ||
201 | int Read_io( struct Gbs_Emu* this, int offset ) ICODE_ATTR; | ||
202 | void Write_io( struct Gbs_Emu* this, int offset, int data ) ICODE_ATTR; | ||
203 | |||
204 | #endif | ||