Path: blob/main/misc/emulator/xnes/snes9x/apu/SNES_SPC_misc.cpp
28798 views
// SPC emulation support: init, sample buffering, reset, SPC loading12// snes_spc 0.9.0. http://www.slack.net/~ant/34#include "SNES_SPC.h"56#include <string.h>78/* Copyright (C) 2004-2007 Shay Green. This module is free software; you9can redistribute it and/or modify it under the terms of the GNU Lesser10General Public License as published by the Free Software Foundation; either11version 2.1 of the License, or (at your option) any later version. This12module is distributed in the hope that it will be useful, but WITHOUT ANY13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS14FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more15details. You should have received a copy of the GNU Lesser General Public16License along with this module; if not, write to the Free Software Foundation,17Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */1819#include "blargg_source.h"2021#define RAM (m.ram.ram)22#define REGS (m.smp_regs [0])23#define REGS_IN (m.smp_regs [1])2425// (n ? n : 256)26#define IF_0_THEN_256( n ) ((uint8_t) ((n) - 1) + 1)272829//// Init3031blargg_err_t SNES_SPC::init()32{33memset( &m, 0, sizeof m );34dsp.init( RAM );3536m.tempo = tempo_unit;3738// Most SPC music doesn't need ROM, and almost all the rest only rely39// on these two bytes40m.rom [0x3E] = 0xFF;41m.rom [0x3F] = 0xC0;4243static unsigned char const cycle_table [128] =44{// 01 23 45 67 89 AB CD EF450x28,0x47,0x34,0x36,0x26,0x54,0x54,0x68, // 0460x48,0x47,0x45,0x56,0x55,0x65,0x22,0x46, // 1470x28,0x47,0x34,0x36,0x26,0x54,0x54,0x74, // 2480x48,0x47,0x45,0x56,0x55,0x65,0x22,0x38, // 3490x28,0x47,0x34,0x36,0x26,0x44,0x54,0x66, // 4500x48,0x47,0x45,0x56,0x55,0x45,0x22,0x43, // 5510x28,0x47,0x34,0x36,0x26,0x44,0x54,0x75, // 6520x48,0x47,0x45,0x56,0x55,0x55,0x22,0x36, // 7530x28,0x47,0x34,0x36,0x26,0x54,0x52,0x45, // 8540x48,0x47,0x45,0x56,0x55,0x55,0x22,0xC5, // 9550x38,0x47,0x34,0x36,0x26,0x44,0x52,0x44, // A560x48,0x47,0x45,0x56,0x55,0x55,0x22,0x34, // B570x38,0x47,0x45,0x47,0x25,0x64,0x52,0x49, // C580x48,0x47,0x56,0x67,0x45,0x55,0x22,0x83, // D590x28,0x47,0x34,0x36,0x24,0x53,0x43,0x40, // E600x48,0x47,0x45,0x56,0x34,0x54,0x22,0x60, // F61};6263// unpack cycle table64for ( int i = 0; i < 128; i++ )65{66int n = cycle_table [i];67m.cycle_table [i * 2 + 0] = n >> 4;68m.cycle_table [i * 2 + 1] = n & 0x0F;69}7071allow_time_overflow = false;7273dsp.rom = m.rom;74dsp.hi_ram = m.hi_ram;7576#ifdef DEBUGGER77apu_trace = NULL;78debug_trace = false;79#endif8081#if SPC_LESS_ACCURATE82memcpy( reg_times, reg_times_, sizeof reg_times );83#endif8485reset();86return 0;87}8889void SNES_SPC::init_rom( uint8_t const in [rom_size] )90{91memcpy( m.rom, in, sizeof m.rom );92}9394void SNES_SPC::set_tempo( int t )95{96m.tempo = t;97int const timer2_shift = 4; // 64 kHz98int const other_shift = 3; // 8 kHz99100#if SPC_DISABLE_TEMPO101m.timers [2].prescaler = timer2_shift;102m.timers [1].prescaler = timer2_shift + other_shift;103m.timers [0].prescaler = timer2_shift + other_shift;104#else105if ( !t )106t = 1;107int const timer2_rate = 1 << timer2_shift;108int rate = (timer2_rate * tempo_unit + (t >> 1)) / t;109if ( rate < timer2_rate / 4 )110rate = timer2_rate / 4; // max 4x tempo111m.timers [2].prescaler = rate;112m.timers [1].prescaler = rate << other_shift;113m.timers [0].prescaler = rate << other_shift;114#endif115}116117// Timer registers have been loaded. Applies these to the timers. Does not118// reset timer prescalers or dividers.119void SNES_SPC::timers_loaded()120{121int i;122for ( i = 0; i < timer_count; i++ )123{124Timer* t = &m.timers [i];125t->period = IF_0_THEN_256( REGS [r_t0target + i] );126t->enabled = REGS [r_control] >> i & 1;127t->counter = REGS_IN [r_t0out + i] & 0x0F;128}129130set_tempo( m.tempo );131}132133// Loads registers from unified 16-byte format134void SNES_SPC::load_regs( uint8_t const in [reg_count] )135{136memcpy( REGS, in, reg_count );137memcpy( REGS_IN, REGS, reg_count );138139// These always read back as 0140REGS_IN [r_test ] = 0;141REGS_IN [r_control ] = 0;142REGS_IN [r_t0target] = 0;143REGS_IN [r_t1target] = 0;144REGS_IN [r_t2target] = 0;145}146147// RAM was just loaded from SPC, with $F0-$FF containing SMP registers148// and timer counts. Copies these to proper registers.149void SNES_SPC::ram_loaded()150{151m.rom_enabled = dsp.rom_enabled = 0;152load_regs( &RAM [0xF0] );153154// Put STOP instruction around memory to catch PC underflow/overflow155memset( m.ram.padding1, cpu_pad_fill, sizeof m.ram.padding1 );156memset( m.ram.padding2, cpu_pad_fill, sizeof m.ram.padding2 );157}158159// Registers were just loaded. Applies these new values.160void SNES_SPC::regs_loaded()161{162enable_rom( REGS [r_control] & 0x80 );163timers_loaded();164}165166void SNES_SPC::reset_time_regs()167{168m.cpu_error = 0;169m.echo_accessed = 0;170m.spc_time = 0;171m.dsp_time = 0;172#if SPC_LESS_ACCURATE173m.dsp_time = clocks_per_sample + 1;174#endif175176for ( int i = 0; i < timer_count; i++ )177{178Timer* t = &m.timers [i];179t->next_time = 1;180t->divider = 0;181}182183regs_loaded();184185m.extra_clocks = 0;186reset_buf();187}188189void SNES_SPC::reset_common( int timer_counter_init )190{191int i;192for ( i = 0; i < timer_count; i++ )193REGS_IN [r_t0out + i] = timer_counter_init;194195// Run IPL ROM196memset( &m.cpu_regs, 0, sizeof m.cpu_regs );197m.cpu_regs.pc = rom_addr;198199REGS [r_test ] = 0x0A;200REGS [r_control] = 0xB0; // ROM enabled, clear ports201for ( i = 0; i < port_count; i++ )202REGS_IN [r_cpuio0 + i] = 0;203204reset_time_regs();205}206207void SNES_SPC::soft_reset()208{209reset_common( 0 );210dsp.soft_reset();211}212213void SNES_SPC::reset()214{215m.cpu_regs.pc = 0xFFC0;216m.cpu_regs.a = 0x00;217m.cpu_regs.x = 0x00;218m.cpu_regs.y = 0x00;219m.cpu_regs.psw = 0x02;220m.cpu_regs.sp = 0xEF;221memset( RAM, 0x00, 0x10000 );222ram_loaded();223reset_common( 0x0F );224dsp.reset();225}226227char const SNES_SPC::signature [signature_size + 1] =228"SNES-SPC700 Sound File Data v0.30\x1A\x1A";229230blargg_err_t SNES_SPC::load_spc( void const* data, long size )231{232spc_file_t const* const spc = (spc_file_t const*) data;233234// be sure compiler didn't insert any padding into fle_t235assert( sizeof (spc_file_t) == spc_min_file_size + 0x80 );236237// Check signature and file size238if ( size < signature_size || memcmp( spc, signature, 27 ) )239return "Not an SPC file";240241if ( size < spc_min_file_size )242return "Corrupt SPC file";243244// CPU registers245m.cpu_regs.pc = spc->pch * 0x100 + spc->pcl;246m.cpu_regs.a = spc->a;247m.cpu_regs.x = spc->x;248m.cpu_regs.y = spc->y;249m.cpu_regs.psw = spc->psw;250m.cpu_regs.sp = spc->sp;251252// RAM and registers253memcpy( RAM, spc->ram, 0x10000 );254ram_loaded();255256// DSP registers257dsp.load( spc->dsp );258259reset_time_regs();260261return 0;262}263264void SNES_SPC::clear_echo()265{266if ( !(dsp.read( SPC_DSP::r_flg ) & 0x20) )267{268int addr = 0x100 * dsp.read( SPC_DSP::r_esa );269int end = addr + 0x800 * (dsp.read( SPC_DSP::r_edl ) & 0x0F);270if ( end > 0x10000 )271end = 0x10000;272memset( &RAM [addr], 0xFF, end - addr );273}274}275276277//// Sample output278279void SNES_SPC::reset_buf()280{281// Start with half extra buffer of silence282sample_t* out = m.extra_buf;283while ( out < &m.extra_buf [extra_size / 2] )284*out++ = 0;285286m.extra_pos = out;287m.buf_begin = 0;288289dsp.set_output( 0, 0 );290}291292void SNES_SPC::set_output( sample_t* out, int size )293{294require( (size & 1) == 0 ); // size must be even295296m.extra_clocks &= clocks_per_sample - 1;297if ( out )298{299sample_t const* out_end = out + size;300m.buf_begin = out;301m.buf_end = out_end;302303// Copy extra to output304sample_t const* in = m.extra_buf;305while ( in < m.extra_pos && out < out_end )306*out++ = *in++;307308// Handle output being full already309if ( out >= out_end )310{311// Have DSP write to remaining extra space312out = dsp.extra();313out_end = &dsp.extra() [extra_size];314315// Copy any remaining extra samples as if DSP wrote them316while ( in < m.extra_pos )317*out++ = *in++;318assert( out <= out_end );319}320321dsp.set_output( out, out_end - out );322}323else324{325reset_buf();326}327}328329void SNES_SPC::save_extra()330{331// Get end pointers332sample_t const* main_end = m.buf_end; // end of data written to buf333sample_t const* dsp_end = dsp.out_pos(); // end of data written to dsp.extra()334if ( m.buf_begin <= dsp_end && dsp_end <= main_end )335{336main_end = dsp_end;337dsp_end = dsp.extra(); // nothing in DSP's extra338}339340// Copy any extra samples at these ends into extra_buf341sample_t* out = m.extra_buf;342sample_t const* in;343for ( in = m.buf_begin + sample_count(); in < main_end; in++ )344*out++ = *in;345for ( in = dsp.extra(); in < dsp_end ; in++ )346*out++ = *in;347348m.extra_pos = out;349assert( out <= &m.extra_buf [extra_size] );350}351352blargg_err_t SNES_SPC::play( int count, sample_t* out )353{354require( (count & 1) == 0 ); // must be even355if ( count )356{357set_output( out, count );358end_frame( count * (clocks_per_sample / 2) );359}360361const char* err = m.cpu_error;362m.cpu_error = 0;363return err;364}365366blargg_err_t SNES_SPC::skip( int count )367{368#if SPC_LESS_ACCURATE369if ( count > 2 * sample_rate * 2 )370{371set_output( 0, 0 );372373// Skip a multiple of 4 samples374time_t end = count;375count = (count & 3) + 1 * sample_rate * 2;376end = (end - count) * (clocks_per_sample / 2);377378m.skipped_kon = 0;379m.skipped_koff = 0;380381// Preserve DSP and timer synchronization382// TODO: verify that this really preserves it383int old_dsp_time = m.dsp_time + m.spc_time;384m.dsp_time = end - m.spc_time + skipping_time;385end_frame( end );386m.dsp_time = m.dsp_time - skipping_time + old_dsp_time;387388dsp.write( SPC_DSP::r_koff, m.skipped_koff & ~m.skipped_kon );389dsp.write( SPC_DSP::r_kon , m.skipped_kon );390clear_echo();391}392#endif393394return play( count, 0 );395}396397//// Snes9x Accessor398399void SNES_SPC::dsp_set_spc_snapshot_callback( void (*callback) (void) )400{401dsp.set_spc_snapshot_callback( callback );402}403404void SNES_SPC::dsp_dump_spc_snapshot( void )405{406dsp.dump_spc_snapshot();407}408409void SNES_SPC::dsp_set_stereo_switch( int value )410{411dsp.set_stereo_switch( value );412}413414SNES_SPC::uint8_t SNES_SPC::dsp_reg_value( int ch, int addr )415{416return dsp.reg_value( ch, addr );417}418419int SNES_SPC::dsp_envx_value( int ch )420{421return dsp.envx_value( ch );422}423424//// Snes9x debugger425426#ifdef DEBUGGER427428void SNES_SPC::debug_toggle_trace( void )429{430debug_trace = !debug_trace;431432if (debug_trace)433{434printf("APU tracing enabled.\n");435ENSURE_TRACE_OPEN(apu_trace, "apu_trace.log", "wb")436}437else438{439printf("APU tracing disabled.\n");440fclose(apu_trace);441apu_trace = NULL;442}443}444445bool SNES_SPC::debug_is_enabled( void ) { return debug_trace; }446447void SNES_SPC::debug_do_trace( int a, int x, int y, uint8_t const *pc, uint8_t *sp, int psw, int c, int nz, int dp )448{449char msg[512];450451ENSURE_TRACE_OPEN(apu_trace, "apu_trace.log", "a")452453debug_op_print(msg, a, x, y, pc, sp, psw, c, nz, dp);454fprintf(apu_trace, "%s ", msg);455debug_io_print(msg);456fprintf(apu_trace, "%s ", msg);457S9xPrintHVPosition(msg);458fprintf(apu_trace, "%s\n", msg);459}460461void SNES_SPC::debug_op_print( char *buffer, int a, int x, int y, uint8_t const *pc, uint8_t *sp, int psw, int c, int nz, int dp )462{463static char mnemonics[256][20] =464{465"NOP",466"TCALL 0",467"SET1 $%02X.0",468"BBS $%02X.0,$%04X",469"OR A,$%02X",470"OR A,!$%04X",471"OR A,(X)",472"OR A,[$%02X+X]",473"OR A,#$%02X",474"OR $%02X,$%02X",475"OR1 C,$%04X.%d",476"ASL $%02X",477"MOV !$%04X,Y",478"PUSH PSW",479"TSET1 !$%04X",480"BRK",481"BPL $%04X",482"TCALL 1",483"CLR1 $%02X.0",484"BBC $%02X.0,$%04X",485"OR A,$%02X+X",486"OR A,!$%04X+X",487"OR A,!$%04X+Y",488"OR A,[$%02X]+Y",489"OR $%02X,#$%02X",490"OR (X),(Y)",491"DECW $%02X",492"ASL $%02X+X",493"ASL A",494"DEC X",495"CMP X,!$%04X",496"JMP [!$%04X+X]",497"CLRP",498"TCALL 2",499"SET1 $%02X.1",500"BBS $%02X.1,$%04X",501"AND A,$%02X",502"AND A,!$%04X",503"AND A,(X)",504"AND A,[$%02X+X]",505"AND A,#$%02X",506"AND $%02X,$%02X",507"OR1 C,/$%04X.%d",508"ROL $%02X",509"ROL !$%04X",510"PUSH A",511"CBNE $%02X,$%04X",512"BRA $%04X",513"BMI $%04X",514"TCALL 3",515"CLR1 $%02X.1",516"BBC $%02X.1,$%04X",517"AND A,$%02X+X",518"AND A,!$%04X+X",519"AND A,!$%04X+Y",520"AND A,[$%02X]+Y",521"AND $%02X,#$%02X",522"AND (X),(Y)",523"INCW $%02X",524"ROL $%02X+X",525"ROL A",526"INC X",527"CMP X,$%02X",528"CALL !$%04X",529"SETP",530"TCALL 4",531"SET1 $%02X.2",532"BBS $%02X.2,$%04X",533"EOR A,$%02X",534"EOR A,!$%04X",535"EOR A,(X)",536"EOR A,[$%02X+X]",537"EOR A,#$%02X",538"EOR $%02X,$%02X",539"AND1 C,$%04X.%d",540"LSR $%02X",541"LSR !$%04X",542"PUSH X",543"TCLR1 !$%04X",544"PCALL $%02X",545"BVC $%04X",546"TCALL 5",547"CLR1 $%02X.2",548"BBC $%02X.2,$%04X",549"EOR A,$%02X+X",550"EOR A,!$%04X+X",551"EOR A,!$%04X+Y",552"EOR A,[$%02X]+Y",553"EOR $%02X,#$%02X",554"EOR (X),(Y)",555"CMPW YA,$%02X",556"LSR $%02X+X",557"LSR A",558"MOV X,A",559"CMP Y,!$%04X",560"JMP !$%04X",561"CLRC",562"TCALL 6",563"SET1 $%02X.3",564"BBS $%02X.3,$%04X",565"CMP A,$%02X",566"CMP A,!$%04X",567"CMP A,(X)",568"CMP A,[$%02X+X]",569"CMP A,#$%02X",570"CMP $%02X,$%02X",571"AND1 C,/$%04X.%d",572"ROR $%02X",573"ROR !$%04X",574"PUSH Y",575"DBNZ $%02X,$%04X",576"RET",577"BVS $%04X",578"TCALL 7",579"CLR1 $%02X.3",580"BBC $%02X.3,$%04X",581"CMP A,$%02X+X",582"CMP A,!$%04X+X",583"CMP A,!$%04X+Y",584"CMP A,[$%02X]+Y",585"CMP $%02X,#$%02X",586"CMP (X),(Y)",587"ADDW YA,$%02X",588"ROR $%02X+X",589"ROR A",590"MOV A,X",591"CMP Y,$%02X",592"RET1",593"SETC",594"TCALL 8",595"SET1 $%02X.4",596"BBS $%02X.4,$%04X",597"ADC A,$%02X",598"ADC A,!$%04X",599"ADC A,(X)",600"ADC A,[$%02X+X]",601"ADC A,#$%02X",602"ADC $%02X,$%02X",603"EOR1 C,$%04X.%d",604"DEC $%02X",605"DEC !$%04X",606"MOV Y,#$%02X",607"POP PSW",608"MOV $%02X,#$%02X",609"BCC $%04X",610"TCALL 9",611"CLR1 $%02X.4",612"BBC $%02X.4,$%04X",613"ADC A,$%02X+X",614"ADC A,!$%04X+X",615"ADC A,!$%04X+Y",616"ADC A,[$%02X]+Y",617"ADC $%02X,#$%02X",618"ADC (X),(Y)",619"SUBW YA,$%02X",620"DEC $%02X+X",621"DEC A",622"MOV X,SP",623"DIV YA,X",624"XCN A",625"EI",626"TCALL 10",627"SET1 $%02X.5",628"BBS $%02X.5,$%04X",629"SBC A,$%02X",630"SBC A,!$%04X",631"SBC A,(X)",632"SBC A,[$%02X+X]",633"SBC A,#$%02X",634"SBC $%02X,$%02X",635"MOV1 C,$%04X.%d",636"INC $%02X",637"INC !$%04X",638"CMP Y,#$%02X",639"POP A",640"MOV (X)+,A",641"BCS $%04X",642"TCALL 11",643"CLR1 $%02X.5",644"BBC $%02X.5,$%04X",645"SBC A,$%02X+X",646"SBC A,!$%04X+X",647"SBC A,!$%04X+Y",648"SBC A,[$%02X]+Y",649"SBC $%02X,#$%02X",650"SBC (X),(Y)",651"MOVW YA,$%02X",652"INC $%02X+X",653"INC A",654"MOV SP,X",655"DAS A",656"MOV A,(X)+",657"DI",658"TCALL 12",659"SET1 $%02X.6",660"BBS $%02X.6,$%04X",661"MOV $%02X,A",662"MOV !$%04X,A",663"MOV (X),A",664"MOV [$%02X+X],A",665"CMP X,#$%02X",666"MOV !$%04X,X",667"MOV1 $%04X.%d,C",668"MOV $%02X,Y",669"ASL !$%04X",670"MOV X,#$%02X",671"POP X",672"MUL YA",673"BNE $%04X",674"TCALL 13",675"CLR1 $%02X.6",676"BBC $%02X.6,$%04X",677"MOV $%02X+X,A",678"MOV !$%04X+X,A",679"MOV !$%04X+Y,A",680"MOV [$%02X]+Y,A",681"MOV $%02X,X",682"MOV $%02X+Y,X",683"MOVW $%02X,YA",684"MOV $%02X+X,Y",685"DEC Y",686"MOV A,Y",687"CBNE $%02X+X,$%04X",688"DAA A",689"CLRV",690"TCALL 14",691"SET1 $%02X.7",692"BBS $%02X.7,$%04X",693"MOV A,$%02X",694"MOV A,!$%04X",695"MOV A,(X)",696"MOV A,[$%02X+X]",697"MOV A,#$%02X",698"MOV X,!$%04X",699"NOT1 $%04X.%d",700"MOV Y,$%02X",701"MOV Y,!$%04X",702"NOTC",703"POP Y",704"SLEEP",705"BEQ $%04X",706"TCALL 15",707"CLR1 $%02X.7",708"BBC $%02X.7,$%04X",709"MOV A,$%02X+X",710"MOV A,!$%04X+X",711"MOV A,!$%04X+Y",712"MOV A,[$%02X]+Y",713"MOV X,$%02X",714"MOV X,$%02X+Y",715"MOV $%02X,$%02X",716"MOV Y,$%02X+X",717"INC Y",718"MOV Y,A",719"DBNZ Y,$%04X",720"STOP"721};722723static int modes[256] =724{7252, 2, 0, 5, 0, 1, 2, 0, 0, 3, 6, 0, 1, 2, 1, 2,7267, 2, 0, 5, 0, 1, 1, 0, 4, 2, 0, 0, 2, 2, 1, 1,7272, 2, 0, 5, 0, 1, 2, 0, 0, 3, 6, 0, 1, 2, 5, 7,7287, 2, 0, 5, 0, 1, 1, 0, 4, 2, 0, 0, 2, 2, 0, 1,7292, 2, 0, 5, 0, 1, 2, 0, 0, 3, 6, 0, 1, 2, 1, 0,7307, 2, 0, 5, 0, 1, 1, 0, 4, 2, 0, 0, 2, 2, 1, 1,7312, 2, 0, 5, 0, 1, 2, 0, 0, 3, 6, 0, 1, 2, 5, 2,7327, 2, 0, 5, 0, 1, 1, 0, 4, 2, 0, 0, 2, 2, 0, 2,7332, 2, 0, 5, 0, 1, 2, 0, 0, 3, 6, 0, 1, 0, 2, 4,7347, 2, 0, 5, 0, 1, 1, 0, 4, 2, 0, 0, 2, 2, 2, 2,7352, 2, 0, 5, 0, 1, 2, 0, 0, 3, 6, 0, 1, 0, 2, 2,7367, 2, 0, 5, 0, 1, 1, 0, 4, 2, 0, 0, 2, 2, 2, 2,7372, 2, 0, 5, 0, 1, 2, 0, 0, 1, 6, 0, 1, 0, 2, 2,7387, 2, 0, 5, 0, 1, 1, 0, 0, 0, 0, 0, 2, 2, 5, 2,7392, 2, 0, 5, 0, 1, 2, 0, 0, 1, 6, 0, 1, 2, 2, 2,7407, 2, 0, 5, 0, 1, 1, 0, 0, 0, 3, 0, 2, 2, 7, 2741};742743static int modesToBytes[] =744{7452, 3, 1, 3, 3, 3, 3, 2746};747748int const n80 = 0x80; // nz749int const p20 = 0x20; // dp750int const z02 = 0x02; // nz751int const c01 = 0x01; // c752753#define GET_PC() (pc - ram)754#define GET_SP() (sp - 0x101 - ram)755#define GET_PSW( out )\756{\757out = psw & ~(n80 | p20 | z02 | c01);\758out |= c >> 8 & c01;\759out |= dp >> 3 & p20;\760out |= ((nz >> 4) | nz) & n80;\761if ( !(uint8_t) nz ) out |= z02;\762}763764uint8_t const *ram = RAM;765766int addr;767int tsp, tpsw;768uint8_t d0, d1, d2;769770addr = GET_PC();771tsp = GET_SP();772GET_PSW(tpsw);773774d0 = *pc;775d1 = (addr < 0xffff) ? *(pc + 1) : 0;776d2 = (addr < 0xfffe) ? *(pc + 2) : 0;777778int mode = modes[d0];779int bytes = modesToBytes[mode];780char mnem[100];781782switch (bytes)783{784case 1:785sprintf(buffer, "%04X %02X ", addr, d0);786break;787788case 2:789sprintf(buffer, "%04X %02X %02X ", addr, d0, d1);790break;791792case 3:793sprintf(buffer, "%04X %02X %02X %02X ", addr, d0, d1, d2);794break;795}796797switch (mode)798{799case 0:800sprintf(mnem, mnemonics[d0], d1);801break;802803case 1:804sprintf(mnem, mnemonics[d0], d1 + (d2 << 8));805break;806807case 2:808strcpy (mnem, mnemonics[d0]);809break;810811case 3:812sprintf(mnem, mnemonics[d0], d2, d1);813break;814815case 4:816sprintf(mnem, mnemonics[d0], d2, d1);817break;818819case 5:820sprintf(mnem, mnemonics[d0], d1, addr + 3 + (int8_t) d2);821break;822823case 6:824sprintf(mnem, mnemonics[d0], (d1 + (d2 << 8)) & 0x1fff, d2 >> 5);825break;826827case 7:828sprintf(mnem, mnemonics[d0], addr + 2 + (int8_t) d1);829break;830}831832sprintf(buffer, "%s %-20s A:%02X X:%02X Y:%02X S:%02X P:%c%c%c%c%c%c%c%c ROM:%d",833buffer, mnem, a, x, y, tsp,834(tpsw & 0x80) ? 'N' : 'n',835(tpsw & 0x40) ? 'V' : 'v',836(tpsw & 0x20) ? 'P' : 'p',837(tpsw & 0x10) ? 'B' : 'b',838(tpsw & 0x08) ? 'H' : 'h',839(tpsw & 0x04) ? 'I' : 'i',840(tpsw & 0x02) ? 'Z' : 'z',841(tpsw & 0x01) ? 'C' : 'c',842m.rom_enabled ? 1 : 0);843}844845void SNES_SPC::debug_io_print( char *buffer )846{847sprintf(buffer, "i/o %02X/%02X %02X/%02X %02X/%02X %02X/%02X",848m.smp_regs[1][r_cpuio0], m.smp_regs[0][r_cpuio0],849m.smp_regs[1][r_cpuio1], m.smp_regs[0][r_cpuio1],850m.smp_regs[1][r_cpuio2], m.smp_regs[0][r_cpuio2],851m.smp_regs[1][r_cpuio3], m.smp_regs[0][r_cpuio3]);852}853854#endif855856857