summaryrefslogtreecommitdiff
path: root/plugins/gme/game-music-emu-0.6pre/gme/Hes_Core.cpp
blob: 2640ad1f581e17200ae075b6c23cdd531e599cb2 (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Game_Music_Emu 0.6-pre. http://www.slack.net/~ant/

#include "Hes_Core.h"

#include "blargg_endian.h"

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

int const timer_mask  = 0x04;
int const vdp_mask    = 0x02;
int const i_flag_mask = 0x04;
int const unmapped    = 0xFF;

int const period_60hz = 262 * 455; // scanlines * clocks per scanline

Hes_Core::Hes_Core() : rom( Hes_Cpu::page_size )
{
	timer.raw_load = 0;
}

Hes_Core::~Hes_Core() { }

void Hes_Core::unload()
{
	rom.clear();
	Gme_Loader::unload();
}

bool Hes_Core::header_t::valid_tag() const
{
	return 0 == memcmp( tag, "HESM", 4 );
}

blargg_err_t Hes_Core::load_( Data_Reader& in )
{
	assert( offsetof (header_t,unused [4]) == header_t::size );
	RETURN_ERR( rom.load( in, header_t::size, &header_, unmapped ) );
	
	if ( !header_.valid_tag() )
		return blargg_err_file_type;
	
	if ( header_.vers != 0 )
		set_warning( "Unknown file version" );
	
	if ( memcmp( header_.data_tag, "DATA", 4 ) )
		set_warning( "Data header missing" );
	
	if ( memcmp( header_.unused, "\0\0\0\0", 4 ) )
		set_warning( "Unknown header data" );
	
	// File spec supports multiple blocks, but I haven't found any, and
	// many files have bad sizes in the only block, so it's simpler to
	// just try to load the damn data as best as possible.
	
	int addr = get_le32( header_.addr );
	int size = get_le32( header_.data_size );
	int const rom_max = 0x100000;
	if ( (unsigned) addr >= (unsigned) rom_max )
	{
		set_warning( "Invalid address" );
		addr &= rom_max - 1;
	}
	if ( (unsigned) (addr + size) > (unsigned) rom_max )
		set_warning( "Invalid size" );
	
	if ( size != rom.file_size() )
	{
		if ( size <= rom.file_size() - 4 && !memcmp( rom.begin() + size, "DATA", 4 ) )
			set_warning( "Multiple DATA not supported" );
		else if ( size < rom.file_size() )
			set_warning( "Extra file data" );
		else
			set_warning( "Missing file data" );
	}
	
	rom.set_addr( addr );
	
	return blargg_ok;
}

void Hes_Core::recalc_timer_load()
{
	timer.load = timer.raw_load * timer_base + 1;
}

void Hes_Core::set_tempo( double t )
{
	play_period = (time_t) (period_60hz / t);
	timer_base = (int) (1024 / t);
	recalc_timer_load();
}

blargg_err_t Hes_Core::start_track( int track )
{
	memset( ram, 0, sizeof ram ); // some HES music relies on zero fill
	memset( sgx, 0, sizeof sgx );
	
	apu_.reset();
	adpcm_.reset();
	cpu.reset();
	
	for ( int i = 0; i < (int) sizeof header_.banks; i++ )
		set_mmr( i, header_.banks [i] );
	set_mmr( cpu.page_count, 0xFF ); // unmapped beyond end of address space
	
	irq.disables  = timer_mask | vdp_mask;
	irq.timer     = cpu.future_time;
	irq.vdp       = cpu.future_time;
	
	timer.enabled   = false;
	timer.raw_load  = 0x80;
	timer.count     = timer.load;
	timer.fired     = false;
	timer.last_time = 0;
	
	vdp.latch     = 0;
	vdp.control   = 0;
	vdp.next_vbl  = 0;
	
	ram [0x1FF] = (idle_addr - 1) >> 8;
	ram [0x1FE] = (idle_addr - 1) & 0xFF;
	cpu.r.sp = 0xFD;
	cpu.r.pc = get_le16( header_.init_addr );
	cpu.r.a  = track;
	
	recalc_timer_load();
	
	return blargg_ok;
}

// Hardware

void Hes_Core::run_until( time_t present )
{
	while ( vdp.next_vbl < present )
		vdp.next_vbl += play_period;
	
	time_t elapsed = present - timer.last_time;
	if ( elapsed > 0 )
	{
		if ( timer.enabled )
		{
			timer.count -= elapsed;
			if ( timer.count <= 0 )
				timer.count += timer.load;
		}
		timer.last_time = present;
	}
}

void Hes_Core::write_vdp( int addr, int data )
{
	switch ( addr )
	{
	case 0:
		vdp.latch = data & 0x1F;
		break;
	
	case 2:
		if ( vdp.latch == 5 )
		{
			if ( data & 0x04 )
				set_warning( "Scanline interrupt unsupported" );
			run_until( cpu.time() );
			vdp.control = data;
			irq_changed();
		}
		else
		{
			dprintf( "VDP not supported: $%02X <- $%02X\n", vdp.latch, data );
		}
		break;
	
	case 3:
		dprintf( "VDP MSB not supported: $%02X <- $%02X\n", vdp.latch, data );
		break;
	}
}

void Hes_Core::write_mem_( addr_t addr, int data )
{
	time_t time = cpu.time();
	if ( (unsigned) (addr - apu_.io_addr) < apu_.io_size )
	{
		// Avoid going way past end when a long block xfer is writing to I/O space.
		// Not a problem for other registers below because they don't write to
		// Blip_Buffer.
		time_t t = min( time, cpu.end_time() + 8 );
		apu_.write_data( t, addr, data );
		return;
	}
	if ( (unsigned) (addr - adpcm_.io_addr) < adpcm_.io_size )
	{
		time_t t = min( time, cpu.end_time() + 6 );
		adpcm_.write_data( t, addr, data );
		return;
	}
	
	switch ( addr )
	{
	case 0x0000:
	case 0x0002:
	case 0x0003:
		write_vdp( addr, data );
		return;
	
	case 0x0C00: {
		run_until( time );
		timer.raw_load = (data & 0x7F) + 1;
		recalc_timer_load();
		timer.count = timer.load;
		break;
	}
	
	case 0x0C01:
		data &= 1;
		if ( timer.enabled == data )
			return;
		run_until( time );
		timer.enabled = data;
		if ( data )
			timer.count = timer.load;
		break;
	
	case 0x1402:
		run_until( time );
		irq.disables = data;
		if ( (data & 0xF8) && (data & 0xF8) != 0xF8 ) // flag questionable values
			dprintf( "Int mask: $%02X\n", data );
		break;
	
	case 0x1403:
		run_until( time );
		if ( timer.enabled )
			timer.count = timer.load;
		timer.fired = false;
		break;
	
#ifndef NDEBUG
	case 0x1000: // I/O port
	case 0x0402: // palette
	case 0x0403:
	case 0x0404:
	case 0x0405:
		return;
		
	default:
		dprintf( "unmapped write $%04X <- $%02X\n", addr, data );
		return;
#endif
	}
	
	irq_changed();
}

int Hes_Core::read_mem_( addr_t addr )
{
	time_t time = cpu.time();
	addr &= cpu.page_size - 1;
	switch ( addr )
	{
	case 0x0000:
		if ( irq.vdp > time )
			return 0;
		irq.vdp = cpu.future_time;
		run_until( time );
		irq_changed();
		return 0x20;
		
	case 0x0002:
	case 0x0003:
		dprintf( "VDP read not supported: %d\n", addr );
		return 0;
	
	case 0x0C01:
		//return timer.enabled; // TODO: remove?
	case 0x0C00:
		run_until( time );
		dprintf( "Timer count read\n" );
		return (unsigned) (timer.count - 1) / timer_base;
	
	case 0x1402:
		return irq.disables;
	
	case 0x1403:
		{
			int status = 0;
			if ( irq.timer <= time ) status |= timer_mask;
			if ( irq.vdp   <= time ) status |= vdp_mask;
			return status;
		}

	case 0x180A:
	case 0x180B:
	case 0x180C:
	case 0x180D:
		return adpcm_.read_data( time, addr );
		
	#ifndef NDEBUG
		case 0x1000: // I/O port
		//case 0x180C: // CD-ROM
		//case 0x180D:
			break;
		
		default:
			dprintf( "unmapped read  $%04X\n", addr );
	#endif
	}
	
	return unmapped;
}

void Hes_Core::irq_changed()
{
	time_t present = cpu.time();
	
	if ( irq.timer > present )
	{
		irq.timer = cpu.future_time;
		if ( timer.enabled && !timer.fired )
			irq.timer = present + timer.count;
	}
	
	if ( irq.vdp > present )
	{
		irq.vdp = cpu.future_time;
		if ( vdp.control & 0x08 )
			irq.vdp = vdp.next_vbl;
	}
	
	time_t time = cpu.future_time;
	if ( !(irq.disables & timer_mask) ) time = irq.timer;
	if ( !(irq.disables &   vdp_mask) ) time = min( time, irq.vdp );
	
	cpu.set_irq_time( time );
}

int Hes_Core::cpu_done()
{
	check( cpu.time() >= cpu.end_time() ||
			(!(cpu.r.flags & i_flag_mask) && cpu.time() >= cpu.irq_time()) );
	
	if ( !(cpu.r.flags & i_flag_mask) )
	{
		time_t present = cpu.time();
		
		if ( irq.timer <= present && !(irq.disables & timer_mask) )
		{
			timer.fired = true;
			irq.timer = cpu.future_time;
			irq_changed(); // overkill, but not worth writing custom code
			return 0x0A;
		}
		
		if ( irq.vdp <= present && !(irq.disables & vdp_mask) )
		{
			// work around for bugs with music not acknowledging VDP
			//run_until( present );
			//irq.vdp = cpu.future_time;
			//irq_changed();
			return 0x08;
		}
	}
	return -1;
}

static void adjust_time( Hes_Core::time_t& time, Hes_Core::time_t delta )
{
	if ( time < Hes_Cpu::future_time )
	{
		time -= delta;
		if ( time < 0 )
			time = 0;
	}
}

blargg_err_t Hes_Core::end_frame( time_t duration )
{
	if ( run_cpu( duration ) )
		set_warning( "Emulation error (illegal instruction)" );
	
	check( cpu.time() >= duration );
	//check( time() - duration < 20 ); // Txx instruction could cause going way over
	
	run_until( duration );
	
	// end time frame
	timer.last_time -= duration;
	vdp.next_vbl    -= duration;
	cpu.end_frame( duration );
	::adjust_time( irq.timer, duration );
	::adjust_time( irq.vdp,   duration );
	apu_.end_frame( duration );
	adpcm_.end_frame( duration );
	
	return blargg_ok;
}