summaryrefslogtreecommitdiff
path: root/plugins/gme/game-music-emu-0.6pre/gme/Sap_Cpu.cpp
blob: 2f1e5dcc2f2c3e41cf505eadfc861dbc8c5be42b (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
// Game_Music_Emu 0.6-pre. http://www.slack.net/~ant/

#include "Sap_Core.h"

#include "blargg_endian.h"

//#define CPU_LOG_MAX 100000
//#include "nes_cpu_log.h"

/* Copyright (C) 2003-2008 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"

// functions defined in same file as CPU emulator to help compiler's optimizer

int Sap_Core::read_d40b()
{
	//dprintf( "D40B read\n" );
	check( cpu.time() >= frame_start );
	return ((unsigned) (cpu.time() - frame_start) / scanline_period % lines_per_frame) / 2;
}

void Sap_Core::write_D2xx( int d2xx, int data )
{
	addr_t const base = 0xD200;
	
	if ( d2xx < apu_.io_size )
	{
		apu_.write_data( time(), d2xx + base, data );
		return;
	}
	
	if ( (unsigned) (d2xx - 0x10) < apu2_.io_size && info.stereo )
	{
		apu2_.write_data( time(), d2xx + (base - 0x10), data );
		return;
	}
	
	if ( d2xx == 0xD40A - base )
	{
		dprintf( "D40A write\n" );
		time_t t = cpu.time();
		time_t into_line = (t - frame_start) % scanline_period;
		cpu.set_end_time( t - into_line + scanline_period );
		return;
	}
	
	if ( (d2xx & ~0x0010) != 0x0F || data != 0x03 )
		dprintf( "Unmapped write $%04X <- $%02X\n", d2xx + base, data );
}

inline int Sap_Core::read_mem( addr_t addr )
{
	int result = mem.ram [addr];
	if ( addr == 0xD40B )
		result = read_d40b(); 
	else if ( (addr & 0xF900) == 0xD000 )
		dprintf( "Unmapped read $%04X\n", addr );
	return result;
}


#define READ_LOW(  addr       ) (ram [addr])
#define WRITE_LOW( addr, data ) (ram [addr] = data)

#define READ_MEM(  addr       ) read_mem( addr )
#define WRITE_MEM( addr, data ) \
{\
	ram [addr] = data;\
	int d2xx = addr - 0xD200;\
	if ( (unsigned) d2xx < 0x100 )\
		write_D2xx( d2xx, data );\
}

#define CPU         cpu
#define FLAT_MEM    ram

#define CPU_BEGIN \
bool Sap_Core::run_cpu( time_t end )\
{\
	CPU.set_end_time( end );\
	byte* const ram = this->mem.ram; /* cache */
	
	#include "Nes_Cpu_run.h"
	
	return cpu.time_past_end() < 0;
}