summaryrefslogtreecommitdiff
path: root/plugins/gme/game-music-emu-0.6pre/gme/Nsf_Core.cpp
blob: f08222500f6d9856220649ae3efaa1c8d79d584a (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Game_Music_Emu 0.6-pre. http://www.slack.net/~ant/

#include "Nsf_Core.h"

#include "blargg_endian.h"

#if !NSF_EMU_APU_ONLY
	#include "Nes_Namco_Apu.h"
	#include "Nes_Vrc6_Apu.h"
	#include "Nes_Fme7_Apu.h"
	#include "Nes_Fds_Apu.h"
	#include "Nes_Mmc5_Apu.h"
	#include "Nes_Vrc7_Apu.h"
#endif

/* 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"

Nsf_Core::Nsf_Core()
{
	fds   = NULL;
	fme7  = NULL;
	mmc5  = NULL;
	namco = NULL;
	vrc6  = NULL;
	vrc7  = NULL;
}

Nsf_Core::~Nsf_Core()
{
	unload();
}

void Nsf_Core::unload()
{
#if !NSF_EMU_APU_ONLY
	delete fds;
	fds = NULL;
	
	delete fme7;
	fme7 = NULL;
	
	delete namco;
	namco = NULL;
	
	delete mmc5;
	mmc5 = NULL;
	
	delete vrc6;
	vrc6 = NULL;
	
	delete vrc7;
	vrc7 = NULL;
#endif

	Nsf_Impl::unload();
}

void Nsf_Core::set_tempo( double t )
{
	set_play_period( (int) (header().play_period() / t) );
	nes_apu()->set_tempo( t );
#if !NSF_EMU_APU_ONLY
	if ( fds )
		fds->set_tempo( t );
#endif
}

blargg_err_t Nsf_Core::post_load()
{
	int chip_flags = header().chip_flags;
	
	#if !NSF_EMU_APU_ONLY
		if ( chip_flags & header_t::fds_mask )
			CHECK_ALLOC( fds = BLARGG_NEW Nes_Fds_Apu );
		
		if ( chip_flags & header_t::fme7_mask )
			CHECK_ALLOC( fme7 = BLARGG_NEW Nes_Fme7_Apu );
		
		if ( chip_flags & header_t::mmc5_mask )
			CHECK_ALLOC( mmc5 = BLARGG_NEW Nes_Mmc5_Apu );
		
		if ( chip_flags & header_t::namco_mask )
			CHECK_ALLOC( namco = BLARGG_NEW Nes_Namco_Apu );
		
		if ( chip_flags & header_t::vrc6_mask )
			CHECK_ALLOC( vrc6 = BLARGG_NEW Nes_Vrc6_Apu );
		
		if ( chip_flags & header_t::vrc7_mask )
		{
			#if NSF_EMU_NO_VRC7
				chip_flags = ~chips_mask; // give warning rather than error
			#else
					CHECK_ALLOC( vrc7 = BLARGG_NEW Nes_Vrc7_Apu );
					RETURN_ERR( vrc7->init() );
			#endif
		}
	#endif

	set_tempo( 1.0 );
	
	if ( chip_flags & ~chips_mask )
		set_warning( "Uses unsupported audio expansion hardware" );

	return Nsf_Impl::post_load();
}

int Nsf_Core::cpu_read( addr_t addr )
{
	#if !NSF_EMU_APU_ONLY
	{
		if ( addr == Nes_Namco_Apu::data_reg_addr && namco )
			return namco->read_data();
		
		if ( (unsigned) (addr - Nes_Fds_Apu::io_addr) < Nes_Fds_Apu::io_size && fds )
			return fds->read( time(), addr );
		
		int i = addr - 0x5C00;
		if ( (unsigned) i < mmc5->exram_size && mmc5 )
			return mmc5->exram [i];
		
		int m = addr - 0x5205;
		if ( (unsigned) m < 2 && mmc5 )
			return (mmc5_mul [0] * mmc5_mul [1]) >> (m * 8) & 0xFF;
	}
	#endif
	
	return Nsf_Impl::cpu_read( addr );
}

int Nsf_Core::unmapped_read( addr_t addr )
{
	switch ( addr )
	{
	case 0x2002:
	case 0x4016:
	case 0x4017:
		return addr >> 8;
	}

	return Nsf_Impl::unmapped_read( addr );
}

void Nsf_Core::cpu_write( addr_t addr, int data )
{
	#if !NSF_EMU_APU_ONLY
	{
		if ( (unsigned) (addr - fds->io_addr) < fds->io_size && fds )
		{
			fds->write( time(), addr, data );
			return;
		}
		
		if ( namco )
		{
			if ( addr == namco->addr_reg_addr )
			{
				namco->write_addr( data );
				return;
			}
			
			if ( addr == namco->data_reg_addr )
			{
				namco->write_data( time(), data );
				return;
			}
		}
		
		if ( vrc6 )
		{
			int reg = addr & (vrc6->addr_step - 1);
			int osc = (unsigned) (addr - vrc6->base_addr) / vrc6->addr_step;
			if ( (unsigned) osc < vrc6->osc_count && (unsigned) reg < vrc6->reg_count )
			{
				vrc6->write_osc( time(), osc, reg, data );
				return;
			}
		}
		
		if ( addr >= fme7->latch_addr && fme7 )
		{
			switch ( addr & fme7->addr_mask )
			{
			case Nes_Fme7_Apu::latch_addr:
				fme7->write_latch( data );
				return;
			
			case Nes_Fme7_Apu::data_addr:
				fme7->write_data( time(), data );
				return;
			}
		}
		
		if ( mmc5 )
		{
			if ( (unsigned) (addr - mmc5->regs_addr) < mmc5->regs_size )
			{
				mmc5->write_register( time(), addr, data );
				return;
			}
			
			int m = addr - 0x5205;
			if ( (unsigned) m < 2 )
			{
				mmc5_mul [m] = data;
				return;
			}
			
			int i = addr - 0x5C00;
			if ( (unsigned) i < mmc5->exram_size )
			{
				mmc5->exram [i] = data;
				return;
			}
		}
		
		if ( vrc7 )
		{
			if ( addr == 0x9010 )
			{
				vrc7->write_reg( data );
				return;
			}
			
			if ( (unsigned) (addr - 0x9028) <= 0x08 )
			{
				vrc7->write_data( time(), data );
				return;
			}
		}
	}
	#endif
	
	return Nsf_Impl::cpu_write( addr, data );
}

void Nsf_Core::unmapped_write( addr_t addr, int data )
{
	switch ( addr )
	{
	case 0x8000: // some write to $8000 and $8001 repeatedly
	case 0x8001:
	case 0x4800: // probably namco sound mistakenly turned on in MCK
	case 0xF800:
	case 0xFFF8: // memory mapper?
		return;
	}
	
	if ( mmc5 && addr == 0x5115 ) return;
	
	// FDS memory
	if ( fds && (unsigned) (addr - 0x8000) < 0x6000 ) return;

	Nsf_Impl::unmapped_write( addr, data );
}

blargg_err_t Nsf_Core::start_track( int track )
{
	#if !NSF_EMU_APU_ONLY
		if ( mmc5 )
		{
			mmc5_mul [0] = 0;
			mmc5_mul [1] = 0;
			memset( mmc5->exram, 0, mmc5->exram_size );
		}
	#endif
	
	#if !NSF_EMU_APU_ONLY
		if ( fds   ) fds  ->reset();
		if ( fme7  ) fme7 ->reset();
		if ( mmc5  ) mmc5 ->reset();
		if ( namco ) namco->reset();
		if ( vrc6  ) vrc6 ->reset();
		if ( vrc7  ) vrc7 ->reset();
	#endif
	
	return Nsf_Impl::start_track( track );
}

void Nsf_Core::end_frame( time_t end )
{
	Nsf_Impl::end_frame( end );
	
	#if !NSF_EMU_APU_ONLY
		if ( fds   ) fds  ->end_frame( end );
		if ( fme7  ) fme7 ->end_frame( end );
		if ( mmc5  ) mmc5 ->end_frame( end );
		if ( namco ) namco->end_frame( end );
		if ( vrc6  ) vrc6 ->end_frame( end );
		if ( vrc7  ) vrc7 ->end_frame( end );
	#endif
}