diff snes_spc/SNES_SPC_misc.cpp @ 0:e38dacceb958

initial import
author Robert McIntyre <rlm@mit.edu>
date Fri, 21 Oct 2011 05:53:11 -0700
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/snes_spc/SNES_SPC_misc.cpp	Fri Oct 21 05:53:11 2011 -0700
     1.3 @@ -0,0 +1,380 @@
     1.4 +// SPC emulation support: init, sample buffering, reset, SPC loading
     1.5 +
     1.6 +// snes_spc 0.9.0. http://www.slack.net/~ant/
     1.7 +
     1.8 +#include "SNES_SPC.h"
     1.9 +
    1.10 +#include <string.h>
    1.11 +
    1.12 +/* Copyright (C) 2004-2007 Shay Green. This module is free software; you
    1.13 +can redistribute it and/or modify it under the terms of the GNU Lesser
    1.14 +General Public License as published by the Free Software Foundation; either
    1.15 +version 2.1 of the License, or (at your option) any later version. This
    1.16 +module is distributed in the hope that it will be useful, but WITHOUT ANY
    1.17 +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.18 +FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
    1.19 +details. You should have received a copy of the GNU Lesser General Public
    1.20 +License along with this module; if not, write to the Free Software Foundation,
    1.21 +Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
    1.22 +
    1.23 +#include "blargg_source.h"
    1.24 +
    1.25 +#define RAM         (m.ram.ram)
    1.26 +#define REGS        (m.smp_regs [0])
    1.27 +#define REGS_IN     (m.smp_regs [1])
    1.28 +
    1.29 +// (n ? n : 256)
    1.30 +#define IF_0_THEN_256( n ) ((uint8_t) ((n) - 1) + 1)
    1.31 +
    1.32 +
    1.33 +//// Init
    1.34 +
    1.35 +blargg_err_t SNES_SPC::init()
    1.36 +{
    1.37 +	memset( &m, 0, sizeof m );
    1.38 +	dsp.init( RAM );
    1.39 +	
    1.40 +	m.tempo = tempo_unit;
    1.41 +	
    1.42 +	// Most SPC music doesn't need ROM, and almost all the rest only rely
    1.43 +	// on these two bytes
    1.44 +	m.rom [0x3E] = 0xFF;
    1.45 +	m.rom [0x3F] = 0xC0;
    1.46 +	
    1.47 +	static unsigned char const cycle_table [128] =
    1.48 +	{//   01   23   45   67   89   AB   CD   EF
    1.49 +	    0x28,0x47,0x34,0x36,0x26,0x54,0x54,0x68, // 0
    1.50 +	    0x48,0x47,0x45,0x56,0x55,0x65,0x22,0x46, // 1
    1.51 +	    0x28,0x47,0x34,0x36,0x26,0x54,0x54,0x74, // 2
    1.52 +	    0x48,0x47,0x45,0x56,0x55,0x65,0x22,0x38, // 3
    1.53 +	    0x28,0x47,0x34,0x36,0x26,0x44,0x54,0x66, // 4
    1.54 +	    0x48,0x47,0x45,0x56,0x55,0x45,0x22,0x43, // 5
    1.55 +	    0x28,0x47,0x34,0x36,0x26,0x44,0x54,0x75, // 6
    1.56 +	    0x48,0x47,0x45,0x56,0x55,0x55,0x22,0x36, // 7
    1.57 +	    0x28,0x47,0x34,0x36,0x26,0x54,0x52,0x45, // 8
    1.58 +	    0x48,0x47,0x45,0x56,0x55,0x55,0x22,0xC5, // 9
    1.59 +	    0x38,0x47,0x34,0x36,0x26,0x44,0x52,0x44, // A
    1.60 +	    0x48,0x47,0x45,0x56,0x55,0x55,0x22,0x34, // B
    1.61 +	    0x38,0x47,0x45,0x47,0x25,0x64,0x52,0x49, // C
    1.62 +	    0x48,0x47,0x56,0x67,0x45,0x55,0x22,0x83, // D
    1.63 +	    0x28,0x47,0x34,0x36,0x24,0x53,0x43,0x40, // E
    1.64 +	    0x48,0x47,0x45,0x56,0x34,0x54,0x22,0x60, // F
    1.65 +	};
    1.66 +	
    1.67 +	// unpack cycle table
    1.68 +	for ( int i = 0; i < 128; i++ )
    1.69 +	{
    1.70 +		int n = cycle_table [i];
    1.71 +		m.cycle_table [i * 2 + 0] = n >> 4;
    1.72 +		m.cycle_table [i * 2 + 1] = n & 0x0F;
    1.73 +	}
    1.74 +	
    1.75 +	#if SPC_LESS_ACCURATE
    1.76 +		memcpy( reg_times, reg_times_, sizeof reg_times );
    1.77 +	#endif
    1.78 +	
    1.79 +	reset();
    1.80 +	return 0;
    1.81 +}
    1.82 +
    1.83 +void SNES_SPC::init_rom( uint8_t const in [rom_size] )
    1.84 +{
    1.85 +	memcpy( m.rom, in, sizeof m.rom );
    1.86 +}
    1.87 +
    1.88 +void SNES_SPC::set_tempo( int t )
    1.89 +{
    1.90 +	m.tempo = t;
    1.91 +	int const timer2_shift = 4; // 64 kHz
    1.92 +	int const other_shift  = 3; //  8 kHz
    1.93 +	
    1.94 +	#if SPC_DISABLE_TEMPO
    1.95 +		m.timers [2].prescaler = timer2_shift;
    1.96 +		m.timers [1].prescaler = timer2_shift + other_shift;
    1.97 +		m.timers [0].prescaler = timer2_shift + other_shift;
    1.98 +	#else
    1.99 +		if ( !t )
   1.100 +			t = 1;
   1.101 +		int const timer2_rate  = 1 << timer2_shift;
   1.102 +		int rate = (timer2_rate * tempo_unit + (t >> 1)) / t;
   1.103 +		if ( rate < timer2_rate / 4 )
   1.104 +			rate = timer2_rate / 4; // max 4x tempo
   1.105 +		m.timers [2].prescaler = rate;
   1.106 +		m.timers [1].prescaler = rate << other_shift;
   1.107 +		m.timers [0].prescaler = rate << other_shift;
   1.108 +	#endif
   1.109 +}
   1.110 +
   1.111 +// Timer registers have been loaded. Applies these to the timers. Does not
   1.112 +// reset timer prescalers or dividers.
   1.113 +void SNES_SPC::timers_loaded()
   1.114 +{
   1.115 +	int i;
   1.116 +	for ( i = 0; i < timer_count; i++ )
   1.117 +	{
   1.118 +		Timer* t = &m.timers [i];
   1.119 +		t->period  = IF_0_THEN_256( REGS [r_t0target + i] );
   1.120 +		t->enabled = REGS [r_control] >> i & 1;
   1.121 +		t->counter = REGS_IN [r_t0out + i] & 0x0F;
   1.122 +	}
   1.123 +	
   1.124 +	set_tempo( m.tempo );
   1.125 +}
   1.126 +
   1.127 +// Loads registers from unified 16-byte format
   1.128 +void SNES_SPC::load_regs( uint8_t const in [reg_count] )
   1.129 +{
   1.130 +	memcpy( REGS, in, reg_count );
   1.131 +	memcpy( REGS_IN, REGS, reg_count );
   1.132 +	
   1.133 +	// These always read back as 0
   1.134 +	REGS_IN [r_test    ] = 0;
   1.135 +	REGS_IN [r_control ] = 0;
   1.136 +	REGS_IN [r_t0target] = 0;
   1.137 +	REGS_IN [r_t1target] = 0;
   1.138 +	REGS_IN [r_t2target] = 0;
   1.139 +}
   1.140 +
   1.141 +// RAM was just loaded from SPC, with $F0-$FF containing SMP registers
   1.142 +// and timer counts. Copies these to proper registers.
   1.143 +void SNES_SPC::ram_loaded()
   1.144 +{
   1.145 +	m.rom_enabled = 0;
   1.146 +	load_regs( &RAM [0xF0] );
   1.147 +	
   1.148 +	// Put STOP instruction around memory to catch PC underflow/overflow
   1.149 +	memset( m.ram.padding1, cpu_pad_fill, sizeof m.ram.padding1 );
   1.150 +	memset( m.ram.padding2, cpu_pad_fill, sizeof m.ram.padding2 );
   1.151 +}
   1.152 +
   1.153 +// Registers were just loaded. Applies these new values.
   1.154 +void SNES_SPC::regs_loaded()
   1.155 +{
   1.156 +	enable_rom( REGS [r_control] & 0x80 );
   1.157 +	timers_loaded();
   1.158 +}
   1.159 +
   1.160 +void SNES_SPC::reset_time_regs()
   1.161 +{
   1.162 +	m.cpu_error     = 0;
   1.163 +	m.echo_accessed = 0;
   1.164 +	m.spc_time      = 0;
   1.165 +	m.dsp_time      = 0;
   1.166 +	#if SPC_LESS_ACCURATE
   1.167 +		m.dsp_time = clocks_per_sample + 1;
   1.168 +	#endif
   1.169 +	
   1.170 +	for ( int i = 0; i < timer_count; i++ )
   1.171 +	{
   1.172 +		Timer* t = &m.timers [i];
   1.173 +		t->next_time = 1;
   1.174 +		t->divider   = 0;
   1.175 +	}
   1.176 +	
   1.177 +	regs_loaded();
   1.178 +	
   1.179 +	m.extra_clocks = 0;
   1.180 +	reset_buf();
   1.181 +}
   1.182 +
   1.183 +void SNES_SPC::reset_common( int timer_counter_init )
   1.184 +{
   1.185 +	int i;
   1.186 +	for ( i = 0; i < timer_count; i++ )
   1.187 +		REGS_IN [r_t0out + i] = timer_counter_init;
   1.188 +	
   1.189 +	// Run IPL ROM
   1.190 +	memset( &m.cpu_regs, 0, sizeof m.cpu_regs );
   1.191 +	m.cpu_regs.pc = rom_addr;
   1.192 +	
   1.193 +	REGS [r_test   ] = 0x0A;
   1.194 +	REGS [r_control] = 0xB0; // ROM enabled, clear ports
   1.195 +	for ( i = 0; i < port_count; i++ )
   1.196 +		REGS_IN [r_cpuio0 + i] = 0;
   1.197 +	
   1.198 +	reset_time_regs();
   1.199 +}
   1.200 +
   1.201 +void SNES_SPC::soft_reset()
   1.202 +{
   1.203 +	reset_common( 0 );
   1.204 +	dsp.soft_reset();
   1.205 +}
   1.206 +
   1.207 +void SNES_SPC::reset()
   1.208 +{
   1.209 +	memset( RAM, 0xFF, 0x10000 );
   1.210 +	ram_loaded();
   1.211 +	reset_common( 0x0F );
   1.212 +	dsp.reset();
   1.213 +}
   1.214 +
   1.215 +char const SNES_SPC::signature [signature_size + 1] =
   1.216 +		"SNES-SPC700 Sound File Data v0.30\x1A\x1A";
   1.217 +
   1.218 +blargg_err_t SNES_SPC::load_spc( void const* data, long size )
   1.219 +{
   1.220 +	spc_file_t const* const spc = (spc_file_t const*) data;
   1.221 +	
   1.222 +	// be sure compiler didn't insert any padding into fle_t
   1.223 +	assert( sizeof (spc_file_t) == spc_min_file_size + 0x80 );
   1.224 +	
   1.225 +	// Check signature and file size
   1.226 +	if ( size < signature_size || memcmp( spc, signature, 27 ) )
   1.227 +		return "Not an SPC file";
   1.228 +	
   1.229 +	if ( size < spc_min_file_size )
   1.230 +		return "Corrupt SPC file";
   1.231 +	
   1.232 +	// CPU registers
   1.233 +	m.cpu_regs.pc  = spc->pch * 0x100 + spc->pcl;
   1.234 +	m.cpu_regs.a   = spc->a;
   1.235 +	m.cpu_regs.x   = spc->x;
   1.236 +	m.cpu_regs.y   = spc->y;
   1.237 +	m.cpu_regs.psw = spc->psw;
   1.238 +	m.cpu_regs.sp  = spc->sp;
   1.239 +	
   1.240 +	// RAM and registers
   1.241 +	memcpy( RAM, spc->ram, 0x10000 );
   1.242 +	ram_loaded();
   1.243 +	
   1.244 +	// DSP registers
   1.245 +	dsp.load( spc->dsp );
   1.246 +	
   1.247 +	reset_time_regs();
   1.248 +	
   1.249 +	return 0;
   1.250 +}
   1.251 +
   1.252 +void SNES_SPC::clear_echo()
   1.253 +{
   1.254 +	if ( !(dsp.read( SPC_DSP::r_flg ) & 0x20) )
   1.255 +	{
   1.256 +		int addr = 0x100 * dsp.read( SPC_DSP::r_esa );
   1.257 +		int end  = addr + 0x800 * (dsp.read( SPC_DSP::r_edl ) & 0x0F);
   1.258 +		if ( end > 0x10000 )
   1.259 +			end = 0x10000;
   1.260 +		memset( &RAM [addr], 0xFF, end - addr );
   1.261 +	}
   1.262 +}
   1.263 +
   1.264 +
   1.265 +//// Sample output
   1.266 +
   1.267 +void SNES_SPC::reset_buf()
   1.268 +{
   1.269 +	// Start with half extra buffer of silence
   1.270 +	sample_t* out = m.extra_buf;
   1.271 +	while ( out < &m.extra_buf [extra_size / 2] )
   1.272 +		*out++ = 0;
   1.273 +	
   1.274 +	m.extra_pos = out;
   1.275 +	m.buf_begin = 0;
   1.276 +	
   1.277 +	dsp.set_output( 0, 0 );
   1.278 +}
   1.279 +
   1.280 +void SNES_SPC::set_output( sample_t* out, int size )
   1.281 +{
   1.282 +	require( (size & 1) == 0 ); // size must be even
   1.283 +	
   1.284 +	m.extra_clocks &= clocks_per_sample - 1;
   1.285 +	if ( out )
   1.286 +	{
   1.287 +		sample_t const* out_end = out + size;
   1.288 +		m.buf_begin = out;
   1.289 +		m.buf_end   = out_end;
   1.290 +		
   1.291 +		// Copy extra to output
   1.292 +		sample_t const* in = m.extra_buf;
   1.293 +		while ( in < m.extra_pos && out < out_end )
   1.294 +			*out++ = *in++;
   1.295 +		
   1.296 +		// Handle output being full already
   1.297 +		if ( out >= out_end )
   1.298 +		{
   1.299 +			// Have DSP write to remaining extra space
   1.300 +			out     = dsp.extra();
   1.301 +			out_end = &dsp.extra() [extra_size];
   1.302 +			
   1.303 +			// Copy any remaining extra samples as if DSP wrote them
   1.304 +			while ( in < m.extra_pos )
   1.305 +				*out++ = *in++;
   1.306 +			assert( out <= out_end );
   1.307 +		}
   1.308 +		
   1.309 +		dsp.set_output( out, out_end - out );
   1.310 +	}
   1.311 +	else
   1.312 +	{
   1.313 +		reset_buf();
   1.314 +	}
   1.315 +}
   1.316 +
   1.317 +void SNES_SPC::save_extra()
   1.318 +{
   1.319 +	// Get end pointers
   1.320 +	sample_t const* main_end = m.buf_end;     // end of data written to buf
   1.321 +	sample_t const* dsp_end  = dsp.out_pos(); // end of data written to dsp.extra()
   1.322 +	if ( m.buf_begin <= dsp_end && dsp_end <= main_end )
   1.323 +	{
   1.324 +		main_end = dsp_end;
   1.325 +		dsp_end  = dsp.extra(); // nothing in DSP's extra
   1.326 +	}
   1.327 +	
   1.328 +	// Copy any extra samples at these ends into extra_buf
   1.329 +	sample_t* out = m.extra_buf;
   1.330 +	sample_t const* in;
   1.331 +	for ( in = m.buf_begin + sample_count(); in < main_end; in++ )
   1.332 +		*out++ = *in;
   1.333 +	for ( in = dsp.extra(); in < dsp_end ; in++ )
   1.334 +		*out++ = *in;
   1.335 +	
   1.336 +	m.extra_pos = out;
   1.337 +	assert( out <= &m.extra_buf [extra_size] );
   1.338 +}
   1.339 +
   1.340 +blargg_err_t SNES_SPC::play( int count, sample_t* out )
   1.341 +{
   1.342 +	require( (count & 1) == 0 ); // must be even
   1.343 +	if ( count )
   1.344 +	{
   1.345 +		set_output( out, count );
   1.346 +		end_frame( count * (clocks_per_sample / 2) );
   1.347 +	}
   1.348 +	
   1.349 +	const char* err = m.cpu_error;
   1.350 +	m.cpu_error = 0;
   1.351 +	return err;
   1.352 +}
   1.353 +
   1.354 +blargg_err_t SNES_SPC::skip( int count )
   1.355 +{
   1.356 +	#if SPC_LESS_ACCURATE
   1.357 +	if ( count > 2 * sample_rate * 2 )
   1.358 +	{
   1.359 +		set_output( 0, 0 );
   1.360 +		
   1.361 +		// Skip a multiple of 4 samples
   1.362 +		time_t end = count;
   1.363 +		count = (count & 3) + 1 * sample_rate * 2;
   1.364 +		end = (end - count) * (clocks_per_sample / 2);
   1.365 +		
   1.366 +		m.skipped_kon  = 0;
   1.367 +		m.skipped_koff = 0;
   1.368 +		
   1.369 +		// Preserve DSP and timer synchronization
   1.370 +		// TODO: verify that this really preserves it
   1.371 +		int old_dsp_time = m.dsp_time + m.spc_time;
   1.372 +		m.dsp_time = end - m.spc_time + skipping_time;
   1.373 +		end_frame( end );
   1.374 +		m.dsp_time = m.dsp_time - skipping_time + old_dsp_time;
   1.375 +		
   1.376 +		dsp.write( SPC_DSP::r_koff, m.skipped_koff & ~m.skipped_kon );
   1.377 +		dsp.write( SPC_DSP::r_kon , m.skipped_kon );
   1.378 +		clear_echo();
   1.379 +	}
   1.380 +	#endif
   1.381 +	
   1.382 +	return play( count, 0 );
   1.383 +}