Fix makefile conditions
[maemo-rb.git] / apps / codecs / libgme / kss_emu.h
blob382e4b807b198ff46d3ce8fd5591265d56b7f6bf
1 // MSX computer KSS music file emulator
3 // Game_Music_Emu 0.5.5
4 #ifndef KSS_EMU_H
5 #define KSS_EMU_H
7 #include "gme.h"
8 #include "blargg_common.h"
10 #include "rom_data.h"
11 #include "multi_buffer.h"
13 #include "kss_scc_apu.h"
14 #include "z80_cpu.h"
15 #include "sms_apu.h"
16 #include "ay_apu.h"
17 #include "opl_apu.h"
18 #include "m3u_playlist.h"
19 #include "track_filter.h"
21 typedef int kss_time_t;
22 typedef int kss_addr_t;
23 typedef struct Z80_Cpu Kss_Cpu;
25 // Sound chip flags
26 enum {
27 sms_psg_flag = 1 << 0,
28 sms_fm_flag = 1 << 1,
29 msx_psg_flag = 1 << 2,
30 msx_scc_flag = 1 << 3,
31 msx_music_flag = 1 << 4,
32 msx_audio_flag = 1 << 5
35 enum { idle_addr = 0xFFFF };
36 enum { scc_enabled_true = 0xC000 };
37 enum { mem_size = 0x10000 };
39 // KSS file header
40 enum { header_size = 0x20 };
41 enum { header_base_size = 0x10 };
42 enum { header_ext_size = header_size - header_base_size };
44 struct header_t
46 byte tag [4];
47 byte load_addr [2];
48 byte load_size [2];
49 byte init_addr [2];
50 byte play_addr [2];
51 byte first_bank;
52 byte bank_mode;
53 byte extra_header;
54 byte device_flags;
56 // KSSX extended data, if extra_header==0x10
57 byte data_size [4];
58 byte unused [4];
59 byte first_track [2];
60 byte last_track [2]; // if no extended data, we set this to 0xFF
61 byte psg_vol;
62 byte scc_vol;
63 byte msx_music_vol;
64 byte msx_audio_vol;
67 struct sms_t {
68 struct Sms_Apu psg;
69 struct Opl_Apu fm;
72 struct msx_t {
73 struct Ay_Apu psg;
74 struct Scc_Apu scc;
75 struct Opl_Apu music;
76 struct Opl_Apu audio;
79 struct Kss_Emu {
80 struct header_t header;
82 int chip_flags;
83 bool scc_accessed;
84 bool gain_updated;
86 unsigned scc_enabled; // 0 or 0xC000
87 int bank_count;
89 blip_time_t play_period;
90 blip_time_t next_play;
91 int ay_latch;
93 // general
94 int voice_count;
95 int const* voice_types;
96 int mute_mask_;
97 int tempo;
98 int gain;
100 int sample_rate;
102 // track-specific
103 int track_count;
104 int current_track;
106 int clock_rate_;
107 unsigned buf_changed_count;
109 // M3u Playlist
110 struct M3u_Playlist m3u;
112 struct setup_t tfilter;
113 struct Track_Filter track_filter;
115 struct sms_t sms;
116 struct msx_t msx;
118 Kss_Cpu cpu;
119 struct Multi_Buffer stereo_buf; // NULL if using custom buffer
120 struct Rom_Data rom;
122 byte unmapped_read [0x100];
123 byte unmapped_write [page_size];
124 byte ram [mem_size + cpu_padding];
127 // Basic functionality (see Gme_File.h for file loading/track info functions)
129 void Kss_init( struct Kss_Emu* this );
130 blargg_err_t Kss_load_mem( struct Kss_Emu* this, const void* data, long size );
131 blargg_err_t end_frame( struct Kss_Emu* this, kss_time_t );
133 // Set output sample rate. Must be called only once before loading file.
134 blargg_err_t Kss_set_sample_rate( struct Kss_Emu* this, int sample_rate );
136 // Start a track, where 0 is the first track. Also clears warning string.
137 blargg_err_t Kss_start_track( struct Kss_Emu* this, int track );
139 // Generate 'count' samples info 'buf'. Output is in stereo. Any emulation
140 // errors set warning string, and major errors also end track.
141 blargg_err_t Kss_play( struct Kss_Emu* this, int count, sample_t* buf );
143 // Track status/control
145 // Number of milliseconds (1000 msec = 1 second) played since beginning of track
146 int Track_tell( struct Kss_Emu* this );
148 // Seek to new time in track. Seeking backwards or far forward can take a while.
149 blargg_err_t Track_seek( struct Kss_Emu* this, int msec );
151 // Skip n samples
152 blargg_err_t Track_skip( struct Kss_Emu* this, int n );
154 // Set start time and length of track fade out. Once fade ends track_ended() returns
155 // true. Fade time can be changed while track is playing.
156 void Track_set_fade( struct Kss_Emu* this, int start_msec, int length_msec );
158 // True if a track has reached its end
159 static inline bool Track_ended( struct Kss_Emu* this )
161 return track_ended( &this->track_filter );
164 // Disables automatic end-of-track detection and skipping of silence at beginning
165 static inline void Track_ignore_silence( struct Kss_Emu* this, bool disable )
167 this->track_filter.silence_ignored_ = disable;
170 // Get track length in milliseconds
171 static inline int Track_get_length( struct Kss_Emu* this, int n )
173 int length = 0;
175 if ( (this->m3u.size > 0) && (n < this->m3u.size) ) {
176 struct entry_t* entry = &this->m3u.entries [n];
177 length = entry->length;
180 if ( length <= 0 )
181 length = 120 * 1000; /* 2 minutes */
183 return length;
186 // Sound customization
188 // Adjust song tempo, where 1.0 = normal, 0.5 = half speed, 2.0 = double speed.
189 // Track length as returned by track_info() assumes a tempo of 1.0.
190 void Sound_set_tempo( struct Kss_Emu* this, int t );
192 // Mute/unmute voice i, where voice 0 is first voice
193 void Sound_mute_voice( struct Kss_Emu* this, int index, bool mute );
195 // Set muting state of all voices at once using a bit mask, where -1 mutes them all,
196 // 0 unmutes them all, 0x01 mutes just the first voice, etc.
197 void Sound_mute_voices( struct Kss_Emu* this, int mask );
199 // Change overall output amplitude, where 1.0 results in minimal clamping.
200 // Must be called before set_sample_rate().
201 static inline void Sound_set_gain( struct Kss_Emu* this, int g )
203 assert( !this->sample_rate ); // you must set gain before setting sample rate
204 this->gain = g;
207 // Emulation (You shouldn't touch these
208 void cpu_write( struct Kss_Emu* this, kss_addr_t, int );
209 int cpu_in( struct Kss_Emu* this, kss_time_t, kss_addr_t );
210 void cpu_out( struct Kss_Emu* this, kss_time_t, kss_addr_t, int );
212 void cpu_write_( struct Kss_Emu* this, kss_addr_t addr, int data );
213 bool run_cpu( struct Kss_Emu* this, kss_time_t end );
214 void jsr( struct Kss_Emu* this, byte const addr [] );
216 static inline int sms_psg_enabled( struct Kss_Emu* this ) { return this->chip_flags & sms_psg_flag; }
217 static inline int sms_fm_enabled( struct Kss_Emu* this ) { return this->chip_flags & sms_fm_flag; }
218 static inline int msx_psg_enabled( struct Kss_Emu* this ) { return this->chip_flags & msx_psg_flag; }
219 static inline int msx_scc_enabled( struct Kss_Emu* this ) { return this->chip_flags & msx_scc_flag; }
220 static inline int msx_music_enabled( struct Kss_Emu* this ) { return this->chip_flags & msx_music_flag;}
221 static inline int msx_audio_enabled( struct Kss_Emu* this ) { return this->chip_flags & msx_audio_flag;}
223 #endif