summaryrefslogtreecommitdiff
path: root/plugins/gme/game-music-emu-0.6pre/gme/Gbs_Cpu.cpp
blob: b683eee251e7d5f5436281dfb262bd902f5efa9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Game_Music_Emu 0.6-pre. http://www.slack.net/~ant/

#include "Gbs_Core.h"

#include "blargg_endian.h"

//#include "gb_cpu_log.h"

/* Copyright (C) 2003-2009 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. This
module is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details. You should have received a copy of the GNU Lesser General Public
License along with this module; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */

#include "blargg_source.h"

#ifndef LOG_MEM
	#define LOG_MEM( addr, str, data ) data
#endif

int Gbs_Core::read_mem( addr_t addr )
{
	int result = *cpu.get_code( addr );
	if ( (unsigned) (addr - apu_.io_addr) < apu_.io_size )
		result = apu_.read_register( time(), addr );
	
#ifndef NDEBUG
	else if ( unsigned (addr - 0x8000) < 0x2000 || unsigned (addr - 0xE000) < 0x1F00 )
		dprintf( "Unmapped read $%04X\n", (unsigned) addr );
	else if ( unsigned (addr - 0xFF01) < 0xFF80 - 0xFF01 && addr != 0xFF70 && addr != 0xFF05 )
		dprintf( "Unmapped read $%04X\n", (unsigned) addr );
#endif

	return LOG_MEM( addr, ">", result );
}

inline void Gbs_Core::write_io_inline( int offset, int data, int base )
{
	if ( (unsigned) (offset - (apu_.io_addr - base)) < apu_.io_size )
		apu_.write_register( time(), offset + base, data & 0xFF );
	else if ( (unsigned) (offset - (0xFF06 - base)) < 2 )
		update_timer();
	else if ( offset == io_base - base )
		ram [base - ram_addr + offset] = 0; // keep joypad return value 0
	else
		ram [base - ram_addr + offset] = 0xFF;
	
	//if ( offset == 0xFFFF - base )
	//  dprintf( "Wrote interrupt mask\n" );
}

void Gbs_Core::write_mem( addr_t addr, int data )
{
	(void) LOG_MEM( addr, "<", data );
	
	int offset = addr - ram_addr;
	if ( (unsigned) offset < 0x10000 - ram_addr )
	{
		ram [offset] = data;
		
		offset -= 0xE000 - ram_addr;
		if ( (unsigned) offset < 0x1F80 )
			write_io_inline( offset, data, 0xE000 );
	}
	else if ( (unsigned) (offset - (0x2000 - ram_addr)) < 0x2000 )
	{
		set_bank( data & 0xFF );
	}
#ifndef NDEBUG
	else if ( unsigned (addr - 0x8000) < 0x2000 || unsigned (addr - 0xE000) < 0x1F00 )
	{
		dprintf( "Unmapped write $%04X\n", (unsigned) addr );
	}
#endif
}

void Gbs_Core::write_io_( int offset, int data )
{
	write_io_inline( offset, data, io_base );
}

inline void Gbs_Core::write_io( int offset, int data )
{
	(void) LOG_MEM( offset + io_base, "<", data );
	
	ram [io_base - ram_addr + offset] = data;
	if ( (unsigned) offset < 0x80 )
		write_io_( offset, data );
}

int Gbs_Core::read_io( int offset )
{
	int const io_base = 0xFF00;
	int result = ram [io_base - ram_addr + offset];
	
	if ( (unsigned) (offset - (apu_.io_addr - io_base)) < apu_.io_size )
	{
		result = apu_.read_register( time(), offset + io_base );
		(void) LOG_MEM( offset + io_base, ">", result );
	}
	else
	{
		check( result == read_mem( offset + io_base ) );
	}
	return result;
}

#define READ_FAST( addr, out ) \
{\
	out = READ_CODE( addr );\
	if ( (unsigned) (addr - apu_.io_addr) < apu_.io_size )\
		out = LOG_MEM( addr, ">", apu_.read_register( TIME() + end_time, addr ) );\
	else\
		check( out == read_mem( addr ) );\
}

#define READ_MEM(  addr       ) read_mem( addr )
#define WRITE_MEM( addr, data ) write_mem( addr, data )

#define WRITE_IO( addr, data )  write_io( addr, data )
#define READ_IO( addr, out )    out = read_io( addr )

#define CPU cpu

#define CPU_BEGIN \
void Gbs_Core::run_cpu()\
{
	#include "Gb_Cpu_run.h"
}