cog/Frameworks/GME/gme/Kss_Scc_Apu.h

112 lines
2.3 KiB
C
Raw Normal View History

2007-10-11 23:11:58 +00:00
// Konami SCC sound chip emulator
2013-09-28 03:24:23 +00:00
// $package
2007-10-11 23:11:58 +00:00
#ifndef KSS_SCC_APU_H
#define KSS_SCC_APU_H
#include "blargg_common.h"
#include "Blip_Buffer.h"
class Scc_Apu {
public:
2013-09-28 03:24:23 +00:00
// Basics
// Sets buffer to generate sound into, or 0 to mute.
void set_output( Blip_Buffer* );
// Emulates to time t, then writes data to reg
enum { reg_count = 0xB0 }; // 0 <= reg < reg_count
void write( blip_time_t t, int reg, int data );
// Emulates to time t, then subtracts t from the current time.
// OK if previous write call had time slightly after t.
void end_frame( blip_time_t t );
// More features
// Resets sound chip
2007-10-11 23:11:58 +00:00
void reset();
2013-09-28 03:24:23 +00:00
// Same as set_output(), but for a particular channel
2007-10-11 23:11:58 +00:00
enum { osc_count = 5 };
2013-09-28 03:24:23 +00:00
void set_output( int chan, Blip_Buffer* );
// Set overall volume, where 1.0 is normal
2007-10-11 23:11:58 +00:00
void volume( double );
2013-09-28 03:24:23 +00:00
// Set treble equalization
void treble_eq( blip_eq_t const& eq ) { synth.treble_eq( eq ); }
private:
// noncopyable
Scc_Apu( const Scc_Apu& );
Scc_Apu& operator = ( const Scc_Apu& );
// Implementation
2007-10-11 23:11:58 +00:00
public:
Scc_Apu();
2013-09-28 03:24:23 +00:00
BLARGG_DISABLE_NOTHROW
2007-10-11 23:11:58 +00:00
private:
enum { amp_range = 0x8000 };
struct osc_t
{
int delay;
int phase;
int last_amp;
Blip_Buffer* output;
};
osc_t oscs [osc_count];
blip_time_t last_time;
unsigned char regs [reg_count];
2013-09-28 03:24:23 +00:00
Blip_Synth_Fast synth;
2007-10-11 23:11:58 +00:00
void run_until( blip_time_t );
};
2013-09-28 03:24:23 +00:00
inline void Scc_Apu::set_output( int index, Blip_Buffer* b )
2007-10-11 23:11:58 +00:00
{
assert( (unsigned) index < osc_count );
oscs [index].output = b;
}
inline void Scc_Apu::write( blip_time_t time, int addr, int data )
{
2013-09-28 03:24:23 +00:00
//assert( (unsigned) addr < reg_count );
assert( ( addr >= 0x9800 && addr <= 0x988F ) || ( addr >= 0xB800 && addr <= 0xB8AF ) );
2007-10-11 23:11:58 +00:00
run_until( time );
2013-09-28 03:24:23 +00:00
addr -= 0x9800;
if ( ( unsigned ) addr < 0x90 )
{
if ( ( unsigned ) addr < 0x60 )
regs [addr] = data;
else if ( ( unsigned ) addr < 0x80 )
{
regs [addr] = regs[addr + 0x20] = data;
}
else if ( ( unsigned ) addr < 0x90 )
{
regs [addr + 0x20] = data;
}
}
else
{
addr -= 0xB800 - 0x9800;
if ( ( unsigned ) addr < 0xB0 )
regs [addr] = data;
}
2007-10-11 23:11:58 +00:00
}
inline void Scc_Apu::end_frame( blip_time_t end_time )
{
if ( end_time > last_time )
run_until( end_time );
2013-09-28 03:24:23 +00:00
2007-10-11 23:11:58 +00:00
last_time -= end_time;
assert( last_time >= 0 );
}
#endif