PageRenderTime 31ms CodeModel.GetById 12ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/gme/gb_cpu_io.h

http://game-music-emu.googlecode.com/
C++ Header | 72 lines | 61 code | 9 blank | 2 comment | 21 complexity | 6c8fad2ed5fe8be2474761ae8a843e37 MD5 | raw file
 1
 2#include "Gbs_Emu.h"
 3
 4#include "blargg_source.h"
 5
 6int Gbs_Emu::cpu_read( gb_addr_t addr )
 7{
 8	int result = *cpu::get_code( addr );
 9	if ( unsigned (addr - Gb_Apu::start_addr) < Gb_Apu::register_count )
10		result = apu.read_register( clock(), addr );
11#ifndef NDEBUG
12	else if ( unsigned (addr - 0x8000) < 0x2000 || unsigned (addr - 0xE000) < 0x1F00 )
13		debug_printf( "Read from unmapped memory $%.4x\n", (unsigned) addr );
14	else if ( unsigned (addr - 0xFF01) < 0xFF80 - 0xFF01 )
15		debug_printf( "Unhandled I/O read 0x%4X\n", (unsigned) addr );
16#endif
17	return result;
18}
19
20void Gbs_Emu::cpu_write( gb_addr_t addr, int data )
21{
22	unsigned offset = addr - ram_addr;
23	if ( offset <= 0xFFFF - ram_addr )
24	{
25		ram [offset] = data;
26		if ( (addr ^ 0xE000) <= 0x1F80 - 1 )
27		{
28			if ( unsigned (addr - Gb_Apu::start_addr) < Gb_Apu::register_count )
29			{
30				GME_APU_HOOK( this, addr - Gb_Apu::start_addr, data );
31				apu.write_register( clock(), addr, data );
32			}
33			else if ( (addr ^ 0xFF06) < 2 )
34				update_timer();
35			else if ( addr == joypad_addr )
36				ram [offset] = 0; // keep joypad return value 0
37			else
38				ram [offset] = 0xFF;
39
40			//if ( addr == 0xFFFF )
41			//  debug_printf( "Wrote interrupt mask\n" );
42		}
43	}
44	else if ( (addr ^ 0x2000) <= 0x2000 - 1 )
45	{
46		set_bank( data );
47	}
48#ifndef NDEBUG
49	else if ( unsigned (addr - 0x8000) < 0x2000 || unsigned (addr - 0xE000) < 0x1F00 )
50	{
51		debug_printf( "Wrote to unmapped memory $%.4x\n", (unsigned) addr );
52	}
53#endif
54}
55
56#define CPU_READ_FAST( cpu, addr, time, out ) \
57	CPU_READ_FAST_( STATIC_CAST(Gbs_Emu*,cpu), addr, time, out )
58
59#define CPU_READ_FAST_( emu, addr, time, out ) \
60{\
61	out = READ_PROG( addr );\
62	if ( unsigned (addr - Gb_Apu::start_addr) < Gb_Apu::register_count )\
63		out = emu->apu.read_register( emu->cpu_time - time * clocks_per_instr, addr );\
64	else\
65		check( out == emu->cpu_read( addr ) );\
66}
67
68#define CPU_READ( cpu, addr, time ) \
69	STATIC_CAST(Gbs_Emu*,cpu)->cpu_read( addr )
70
71#define CPU_WRITE( cpu, addr, data, time ) \
72	STATIC_CAST(Gbs_Emu*,cpu)->cpu_write( addr, data )