summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libgme/kss_emu.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libgme/kss_emu.h')
-rw-r--r--lib/rbcodec/codecs/libgme/kss_emu.h223
1 files changed, 223 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libgme/kss_emu.h b/lib/rbcodec/codecs/libgme/kss_emu.h
new file mode 100644
index 0000000000..382e4b807b
--- /dev/null
+++ b/lib/rbcodec/codecs/libgme/kss_emu.h
@@ -0,0 +1,223 @@
1// MSX computer KSS music file emulator
2
3// Game_Music_Emu 0.5.5
4#ifndef KSS_EMU_H
5#define KSS_EMU_H
6
7#include "gme.h"
8#include "blargg_common.h"
9
10#include "rom_data.h"
11#include "multi_buffer.h"
12
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"
20
21typedef int kss_time_t;
22typedef int kss_addr_t;
23typedef struct Z80_Cpu Kss_Cpu;
24
25// Sound chip flags
26enum {
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
33};
34
35enum { idle_addr = 0xFFFF };
36enum { scc_enabled_true = 0xC000 };
37enum { mem_size = 0x10000 };
38
39// KSS file header
40enum { header_size = 0x20 };
41enum { header_base_size = 0x10 };
42enum { header_ext_size = header_size - header_base_size };
43
44struct header_t
45{
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;
55
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;
65};
66
67struct sms_t {
68 struct Sms_Apu psg;
69 struct Opl_Apu fm;
70};
71
72struct msx_t {
73 struct Ay_Apu psg;
74 struct Scc_Apu scc;
75 struct Opl_Apu music;
76 struct Opl_Apu audio;
77};
78
79struct Kss_Emu {
80 struct header_t header;
81
82 int chip_flags;
83 bool scc_accessed;
84 bool gain_updated;
85
86 unsigned scc_enabled; // 0 or 0xC000
87 int bank_count;
88
89 blip_time_t play_period;
90 blip_time_t next_play;
91 int ay_latch;
92
93 // general
94 int voice_count;
95 int const* voice_types;
96 int mute_mask_;
97 int tempo;
98 int gain;
99
100 int sample_rate;
101
102 // track-specific
103 int track_count;
104 int current_track;
105
106 int clock_rate_;
107 unsigned buf_changed_count;
108
109 // M3u Playlist
110 struct M3u_Playlist m3u;
111
112 struct setup_t tfilter;
113 struct Track_Filter track_filter;
114
115 struct sms_t sms;
116 struct msx_t msx;
117
118 Kss_Cpu cpu;
119 struct Multi_Buffer stereo_buf; // NULL if using custom buffer
120 struct Rom_Data rom;
121
122 byte unmapped_read [0x100];
123 byte unmapped_write [page_size];
124 byte ram [mem_size + cpu_padding];
125};
126
127// Basic functionality (see Gme_File.h for file loading/track info functions)
128
129void Kss_init( struct Kss_Emu* this );
130blargg_err_t Kss_load_mem( struct Kss_Emu* this, const void* data, long size );
131blargg_err_t end_frame( struct Kss_Emu* this, kss_time_t );
132
133// Set output sample rate. Must be called only once before loading file.
134blargg_err_t Kss_set_sample_rate( struct Kss_Emu* this, int sample_rate );
135
136// Start a track, where 0 is the first track. Also clears warning string.
137blargg_err_t Kss_start_track( struct Kss_Emu* this, int track );
138
139// Generate 'count' samples info 'buf'. Output is in stereo. Any emulation
140// errors set warning string, and major errors also end track.
141blargg_err_t Kss_play( struct Kss_Emu* this, int count, sample_t* buf );
142
143// Track status/control
144
145// Number of milliseconds (1000 msec = 1 second) played since beginning of track
146int Track_tell( struct Kss_Emu* this );
147
148// Seek to new time in track. Seeking backwards or far forward can take a while.
149blargg_err_t Track_seek( struct Kss_Emu* this, int msec );
150
151// Skip n samples
152blargg_err_t Track_skip( struct Kss_Emu* this, int n );
153
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.
156void Track_set_fade( struct Kss_Emu* this, int start_msec, int length_msec );
157
158// True if a track has reached its end
159static inline bool Track_ended( struct Kss_Emu* this )
160{
161 return track_ended( &this->track_filter );
162}
163
164// Disables automatic end-of-track detection and skipping of silence at beginning
165static inline void Track_ignore_silence( struct Kss_Emu* this, bool disable )
166{
167 this->track_filter.silence_ignored_ = disable;
168}
169
170// Get track length in milliseconds
171static inline int Track_get_length( struct Kss_Emu* this, int n )
172{
173 int length = 0;
174
175 if ( (this->m3u.size > 0) && (n < this->m3u.size) ) {
176 struct entry_t* entry = &this->m3u.entries [n];
177 length = entry->length;
178 }
179
180 if ( length <= 0 )
181 length = 120 * 1000; /* 2 minutes */
182
183 return length;
184}
185
186// Sound customization
187
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.
190void Sound_set_tempo( struct Kss_Emu* this, int t );
191
192// Mute/unmute voice i, where voice 0 is first voice
193void Sound_mute_voice( struct Kss_Emu* this, int index, bool mute );
194
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.
197void Sound_mute_voices( struct Kss_Emu* this, int mask );
198
199// Change overall output amplitude, where 1.0 results in minimal clamping.
200// Must be called before set_sample_rate().
201static inline void Sound_set_gain( struct Kss_Emu* this, int g )
202{
203 assert( !this->sample_rate ); // you must set gain before setting sample rate
204 this->gain = g;
205}
206
207// Emulation (You shouldn't touch these
208void cpu_write( struct Kss_Emu* this, kss_addr_t, int );
209int cpu_in( struct Kss_Emu* this, kss_time_t, kss_addr_t );
210void cpu_out( struct Kss_Emu* this, kss_time_t, kss_addr_t, int );
211
212void cpu_write_( struct Kss_Emu* this, kss_addr_t addr, int data );
213bool run_cpu( struct Kss_Emu* this, kss_time_t end );
214void jsr( struct Kss_Emu* this, byte const addr [] );
215
216static inline int sms_psg_enabled( struct Kss_Emu* this ) { return this->chip_flags & sms_psg_flag; }
217static inline int sms_fm_enabled( struct Kss_Emu* this ) { return this->chip_flags & sms_fm_flag; }
218static inline int msx_psg_enabled( struct Kss_Emu* this ) { return this->chip_flags & msx_psg_flag; }
219static inline int msx_scc_enabled( struct Kss_Emu* this ) { return this->chip_flags & msx_scc_flag; }
220static inline int msx_music_enabled( struct Kss_Emu* this ) { return this->chip_flags & msx_music_flag;}
221static inline int msx_audio_enabled( struct Kss_Emu* this ) { return this->chip_flags & msx_audio_flag;}
222
223#endif