summaryrefslogtreecommitdiff
path: root/apps/codecs/libgme/sgc_emu.h
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libgme/sgc_emu.h')
-rw-r--r--apps/codecs/libgme/sgc_emu.h199
1 files changed, 199 insertions, 0 deletions
diff --git a/apps/codecs/libgme/sgc_emu.h b/apps/codecs/libgme/sgc_emu.h
new file mode 100644
index 0000000000..957e7438ef
--- /dev/null
+++ b/apps/codecs/libgme/sgc_emu.h
@@ -0,0 +1,199 @@
1// Sega/Game Gear/Coleco SGC music file emulator
2
3// Game_Music_Emu 0.6-pre
4#ifndef SGC_EMU_H
5#define SGC_EMU_H
6
7#include "blargg_common.h"
8#include "multi_buffer.h"
9
10#include "rom_data.h"
11#include "z80_cpu.h"
12#include "sms_fm_apu.h"
13#include "sms_apu.h"
14#include "m3u_playlist.h"
15
16typedef short sample_t;
17typedef struct Z80_Cpu Sgc_Cpu;
18
19enum { buf_size = 2048 };
20
21// SGC file header
22enum { header_size = 0xA0 };
23struct header_t
24{
25 char tag [4]; // "SGC\x1A"
26 byte vers; // 0x01
27 byte rate; // 0=NTSC 1=PAL
28 byte reserved1 [2];
29 byte load_addr [2];
30 byte init_addr [2];
31 byte play_addr [2];
32 byte stack_ptr [2];
33 byte reserved2 [2];
34 byte rst_addrs [7*2];
35 byte mapping [4]; // Used by Sega only
36 byte first_song; // Song to start playing first
37 byte song_count;
38 byte first_effect;
39 byte last_effect;
40 byte system; // 0=Master System 1=Game Gear 2=Colecovision
41 byte reserved3 [23];
42 char game [32]; // strings can be 32 chars, NOT terminated
43 char author [32];
44 char copyright [32];
45};
46
47// True if header has valid file signature
48static inline bool valid_tag( struct header_t* h )
49{
50 return 0 == memcmp( h->tag, "SGC\x1A", 4 );
51}
52
53static inline int effect_count( struct header_t* h ) { return h->last_effect ? h->last_effect - h->first_effect + 1 : 0; }
54
55
56struct Sgc_Emu {
57 bool fm_accessed;
58
59 cpu_time_t play_period;
60 cpu_time_t next_play;
61 void const* bank2; // ROM selected for bank 2, in case RAM is currently hiding it
62 addr_t vectors_addr; // RST vectors start here
63 addr_t idle_addr; // return address for init/play routines
64 void* coleco_bios;
65
66 // general
67 int voice_count;
68 int mute_mask_;
69 double tempo;
70 double gain;
71
72 long sample_rate;
73
74 // track-specific
75 volatile bool track_ended;
76 int current_track;
77 int track_count;
78 blargg_long out_time; // number of samples played since start of track
79 blargg_long emu_time; // number of samples emulator has generated since start of track
80 bool emu_track_ended_; // emulator has reached end of track
81
82 // fading
83 blargg_long fade_start;
84 int fade_step;
85
86 // silence detection
87 bool ignore_silence;
88 int max_initial_silence;
89 int silence_lookahead; // speed to run emulator when looking ahead for silence
90 long silence_time; // number of samples where most recent silence began
91 long silence_count; // number of samples of silence to play before using buf
92 long buf_remain; // number of samples left in silence buffer
93
94 long clock_rate_;
95 unsigned buf_changed_count;
96
97 // M3u Playlist
98 struct M3u_Playlist m3u;
99
100 sample_t buf [buf_size];
101 struct Stereo_Buffer stereo_buf;
102
103 struct Sms_Apu apu;
104 struct Sms_Fm_Apu fm_apu;
105
106 Sgc_Cpu cpu;
107
108 // large items
109 struct header_t header;
110 struct Rom_Data rom;
111 byte vectors [page_size + page_padding];
112 byte ram [0x2000 + page_padding];
113 byte ram2 [0x4000 + page_padding];
114 byte unmapped_write [0x4000];
115};
116
117// Basic functionality (see Gme_File.h for file loading/track info functions)
118
119void Sgc_init( struct Sgc_Emu* this );
120
121blargg_err_t Sgc_load_mem( struct Sgc_Emu* this, const void* data, long size );
122
123static inline int clock_rate( struct Sgc_Emu* this ) { return this->header.rate ? 3546893 : 3579545; }
124
125// 0x2000 bytes
126static inline void set_coleco_bios( struct Sgc_Emu* this, void* p ) { this->coleco_bios = p; }
127
128// Set output sample rate. Must be called only once before loading file.
129blargg_err_t Sgc_set_sample_rate( struct Sgc_Emu* this, long sample_rate );
130
131// Start a track, where 0 is the first track. Also clears warning string.
132blargg_err_t Sgc_start_track( struct Sgc_Emu* this, int track );
133
134// Generate 'count' samples info 'buf'. Output is in stereo. Any emulation
135// errors set warning string, and major errors also end track.
136blargg_err_t Sgc_play( struct Sgc_Emu* this, long count, sample_t* buf );
137
138// Track status/control
139
140// Number of milliseconds (1000 msec = 1 second) played since beginning of track
141long Track_tell( struct Sgc_Emu* this );
142
143// Seek to new time in track. Seeking backwards or far forward can take a while.
144blargg_err_t Track_seek( struct Sgc_Emu* this, long msec );
145
146// Skip n samples
147blargg_err_t Track_skip( struct Sgc_Emu* this, long n );
148
149// Set start time and length of track fade out. Once fade ends track_ended() returns
150// true. Fade time can be changed while track is playing.
151void Track_set_fade( struct Sgc_Emu* this, long start_msec, long length_msec );
152
153// Get track length in milliseconds
154static inline long Track_get_length( struct Sgc_Emu* this, int n )
155{
156 long length = 120 * 1000; /* 2 minutes */
157 if ( (this->m3u.size > 0) && (n < this->m3u.size) ) {
158 struct entry_t* entry = &this->m3u.entries [n];
159 length = entry->length;
160 }
161
162 return length;
163}
164
165// Sound customization
166
167// Adjust song tempo, where 1.0 = normal, 0.5 = half speed, 2.0 = double speed.
168// Track length as returned by track_info() assumes a tempo of 1.0.
169void Sound_set_tempo( struct Sgc_Emu* this, double t );
170
171// Mute/unmute voice i, where voice 0 is first voice
172void Sound_mute_voice( struct Sgc_Emu* this, int index, bool mute );
173
174// Set muting state of all voices at once using a bit mask, where -1 mutes them all,
175// 0 unmutes them all, 0x01 mutes just the first voice, etc.
176void Sound_mute_voices( struct Sgc_Emu* this, int mask );
177
178// Change overall output amplitude, where 1.0 results in minimal clamping.
179// Must be called before set_sample_rate().
180static inline void Sound_set_gain( struct Sgc_Emu* this, double g )
181{
182 assert( !this->sample_rate ); // you must set gain before setting sample rate
183 this->gain = g;
184}
185
186// True if Master System or Game Gear
187static inline bool sega_mapping( struct Sgc_Emu* this )
188{
189 return this->header.system <= 1;
190}
191
192// Emulation (You shouldn't touch these)
193
194bool run_cpu( struct Sgc_Emu* this, cpu_time_t end_time ) ICODE_ATTR;
195void cpu_out( struct Sgc_Emu* this, cpu_time_t time, addr_t addr, int data ) ICODE_ATTR;
196void cpu_write( struct Sgc_Emu* this, addr_t addr, int data ) ICODE_ATTR;
197void jsr( struct Sgc_Emu* this, byte addr [2] ) ICODE_ATTR;
198
199#endif