PageRenderTime 34ms CodeModel.GetById 13ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/gme/nes_cpu_io.h

http://game-music-emu.googlecode.com/
C++ Header | 83 lines | 66 code | 17 blank | 0 comment | 15 complexity | 66e97e756901102dd0a686a20215d0d3 MD5 | raw file
 1
 2#include "Nsf_Emu.h"
 3
 4#if !NSF_EMU_APU_ONLY
 5	#include "Nes_Namco_Apu.h"
 6#endif
 7
 8#include "blargg_source.h"
 9
10int Nsf_Emu::cpu_read( nes_addr_t addr )
11{
12	int result;
13	
14	result = cpu::low_mem [addr & 0x7FF];
15	if ( !(addr & 0xE000) )
16		goto exit;
17	
18	result = *cpu::get_code( addr );
19	if ( addr > 0x7FFF )
20		goto exit;
21	
22	result = sram [addr & (sizeof sram - 1)];
23	if ( addr > 0x5FFF )
24		goto exit;
25	
26	if ( addr == Nes_Apu::status_addr )
27		return apu.read_status( cpu::time() );
28	
29	#if !NSF_EMU_APU_ONLY
30		if ( addr == Nes_Namco_Apu::data_reg_addr && namco )
31			return namco->read_data();
32	#endif
33	
34	result = addr >> 8; // simulate open bus
35	
36	if ( addr != 0x2002 )
37		debug_printf( "Read unmapped $%.4X\n", (unsigned) addr );
38	
39exit:
40	return result;
41}
42
43void Nsf_Emu::cpu_write( nes_addr_t addr, int data )
44{
45	{
46		nes_addr_t offset = addr ^ sram_addr;
47		if ( offset < sizeof sram )
48		{
49			sram [offset] = data;
50			return;
51		}
52	}
53	{
54		int temp = addr & 0x7FF;
55		if ( !(addr & 0xE000) )
56		{
57			cpu::low_mem [temp] = data;
58			return;
59		}
60	}
61	
62	if ( unsigned (addr - Nes_Apu::start_addr) <= Nes_Apu::end_addr - Nes_Apu::start_addr )
63	{
64		GME_APU_HOOK( this, addr - Nes_Apu::start_addr, data );
65		apu.write_register( cpu::time(), addr, data );
66		return;
67	}
68	
69	unsigned bank = addr - bank_select_addr;
70	if ( bank < bank_count )
71	{
72		blargg_long offset = rom.mask_addr( data * (blargg_long) bank_size );
73		if ( offset >= rom.size() )
74			set_warning( "Invalid bank" );
75		cpu::map_code( (bank + 8) * bank_size, bank_size, rom.at_addr( offset ) );
76		return;
77	}
78	
79	cpu_write_misc( addr, data );
80}
81
82#define CPU_READ( cpu, addr, time )         STATIC_CAST(Nsf_Emu&,*cpu).cpu_read( addr )
83#define CPU_WRITE( cpu, addr, data, time )  STATIC_CAST(Nsf_Emu&,*cpu).cpu_write( addr, data )