summaryrefslogtreecommitdiff
path: root/gme/Game_Music_Emu-0.5.2/gme/Nsfe_Emu.cpp
blob: 0a785e60985fe023ee197a00f486497586c8bebb (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
// Game_Music_Emu 0.5.2. http://www.slack.net/~ant/

#include "Nsfe_Emu.h"

#include "blargg_endian.h"
#include <string.h>
#include <ctype.h>

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

Nsfe_Info::Nsfe_Info() { playlist_disabled = false; }

Nsfe_Info::~Nsfe_Info() { }

inline void Nsfe_Info::unload()
{
	track_name_data.clear();
	track_names.clear();
	playlist.clear();
	track_times.clear();
}

// TODO: if no playlist, treat as if there is a playlist that is just 1,2,3,4,5... ?
void Nsfe_Info::disable_playlist( bool b )
{
	playlist_disabled = b;
	info.track_count = playlist.size();
	if ( !info.track_count || playlist_disabled )
		info.track_count = actual_track_count_;
}

int Nsfe_Info::remap_track( int track ) const
{
	if ( !playlist_disabled && (unsigned) track < playlist.size() )
		track = playlist [track];
	return track;
}

// Read multiple strings and separate into individual strings
static blargg_err_t read_strs( Data_Reader& in, long size, blargg_vector<char>& chars,
		blargg_vector<const char*>& strs )
{
	RETURN_ERR( chars.resize( size + 1 ) );
	chars [size] = 0; // in case last string doesn't have terminator
	RETURN_ERR( in.read( &chars [0], size ) );
	
	RETURN_ERR( strs.resize( 128 ) );
	int count = 0;
	for ( int i = 0; i < size; i++ )
	{
		if ( (int) strs.size() <= count )
			RETURN_ERR( strs.resize( count * 2 ) );
		strs [count++] = &chars [i];
		while ( i < size && chars [i] )
			i++;
	}
	
	return strs.resize( count );
}

// Copy in to out, where out has out_max characters allocated. Truncate to
// out_max - 1 characters.
static void copy_str( const char* in, char* out, int out_max )
{
	out [out_max - 1] = 0;
	strncpy( out, in, out_max - 1 );
}

struct nsfe_info_t
{
	byte load_addr [2];
	byte init_addr [2];
	byte play_addr [2];
	byte speed_flags;
	byte chip_flags;
	byte track_count;
	byte first_track;
	byte unused [6];
};

blargg_err_t Nsfe_Info::load( Data_Reader& in, Nsf_Emu* nsf_emu )
{
	int const nsfe_info_size = 16;
	assert( offsetof (nsfe_info_t,unused [6]) == nsfe_info_size );
	
	// check header
	byte signature [4];
	blargg_err_t err = in.read( signature, sizeof signature );
	if ( err )
		return (err == in.eof_error ? gme_wrong_file_type : err);
	if ( memcmp( signature, "NSFE", 4 ) )
		return gme_wrong_file_type;
	
	// free previous info
	track_name_data.clear();
	track_names.clear();
	playlist.clear();
	track_times.clear();
	
	// default nsf header
	static const Nsf_Emu::header_t base_header =
	{
		{'N','E','S','M','\x1A'},// tag
		1,                  // version
		1, 1,               // track count, first track
		{0,0},{0,0},{0,0},  // addresses
		"","","",           // strings
		{0x1A, 0x41},       // NTSC rate
		{0,0,0,0,0,0,0,0},  // banks
		{0x20, 0x4E},       // PAL rate
		0, 0,               // flags
		{0,0,0,0}           // unused
	};
	Nsf_Emu::header_t& header = info;
	header = base_header;
	
	// parse tags
	int phase = 0;
	while ( phase != 3 )
	{
		// read size and tag
		byte block_header [2] [4];
		RETURN_ERR( in.read( block_header, sizeof block_header ) );
		blargg_long size = get_le32( block_header [0] );
		blargg_long tag  = get_le32( block_header [1] );
		
		//dprintf( "tag: %c%c%c%c\n", char(tag), char(tag>>8), char(tag>>16), char(tag>>24) );
		
		switch ( tag )
		{
			case BLARGG_4CHAR('O','F','N','I'): {
				check( phase == 0 );
				if ( size < 8 )
					return "Corrupt file";
				
				nsfe_info_t finfo;
				finfo.track_count = 1;
				finfo.first_track = 0;
				
				RETURN_ERR( in.read( &finfo, min( size, (blargg_long) nsfe_info_size ) ) );
				if ( size > nsfe_info_size )
					RETURN_ERR( in.skip( size - nsfe_info_size ) );
				phase = 1;
				info.speed_flags = finfo.speed_flags;
				info.chip_flags  = finfo.chip_flags;
				info.track_count = finfo.track_count;
				this->actual_track_count_ = finfo.track_count;
				info.first_track = finfo.first_track;
				memcpy( info.load_addr, finfo.load_addr, 2 * 3 );
				break;
			}
			
			case BLARGG_4CHAR('K','N','A','B'):
				if ( size > (int) sizeof info.banks )
					return "Corrupt file";
				RETURN_ERR( in.read( info.banks, size ) );
				break;
			
			case BLARGG_4CHAR('h','t','u','a'): {
				blargg_vector<char> chars;
				blargg_vector<const char*> strs;
				RETURN_ERR( read_strs( in, size, chars, strs ) );
				int n = strs.size();
				
				if ( n > 3 )
					copy_str( strs [3], info.dumper, sizeof info.dumper );
				
				if ( n > 2 )
					copy_str( strs [2], info.copyright, sizeof info.copyright );
				
				if ( n > 1 )
					copy_str( strs [1], info.author, sizeof info.author );
				
				if ( n > 0 )
					copy_str( strs [0], info.game, sizeof info.game );
				
				break;
			}
			
			case BLARGG_4CHAR('e','m','i','t'):
				RETURN_ERR( track_times.resize( size / 4 ) );
				RETURN_ERR( in.read( track_times.begin(), track_times.size() * 4 ) );
				break;
			
			case BLARGG_4CHAR('l','b','l','t'):
				RETURN_ERR( read_strs( in, size, track_name_data, track_names ) );
				break;
			
			case BLARGG_4CHAR('t','s','l','p'):
				RETURN_ERR( playlist.resize( size ) );
				RETURN_ERR( in.read( &playlist [0], size ) );
				break;
			
			case BLARGG_4CHAR('A','T','A','D'): {
				check( phase == 1 );
				phase = 2;
				if ( !nsf_emu )
				{
					RETURN_ERR( in.skip( size ) );
				}
				else
				{
					Subset_Reader sub( &in, size ); // limit emu to nsf data
					Remaining_Reader rem( &header, Nsf_Emu::header_size, &sub );
					RETURN_ERR( nsf_emu->load( rem ) );
					check( rem.remain() == 0 );
				}
				break;
			}
			
			case BLARGG_4CHAR('D','N','E','N'):
				check( phase == 2 );
				phase = 3;
				break;
			
			default:
				// tags that can be skipped start with a lowercase character
				check( islower( (tag >> 24) & 0xFF ) );
				RETURN_ERR( in.skip( size ) );
				break;
		}
	}
	
	return 0;
}

blargg_err_t Nsfe_Info::track_info_( track_info_t* out, int track ) const
{
	int remapped = remap_track( track );
	if ( (unsigned) remapped < track_times.size() )
	{
		long length = (BOOST::int32_t) get_le32( track_times [remapped] );
		if ( length > 0 )
			out->length = length;
	}
	if ( (unsigned) remapped < track_names.size() )
		Gme_File::copy_field_( out->song, track_names [remapped] );
	
	GME_COPY_FIELD( info, out, game );
	GME_COPY_FIELD( info, out, author );
	GME_COPY_FIELD( info, out, copyright );
	GME_COPY_FIELD( info, out, dumper );
	return 0;
}

Nsfe_Emu::Nsfe_Emu()
{
	loading = false;
	set_type( gme_nsfe_type );
}

Nsfe_Emu::~Nsfe_Emu() { }

void Nsfe_Emu::unload()
{
	if ( !loading )
		info.unload(); // TODO: extremely hacky!
	Nsf_Emu::unload();
}

blargg_err_t Nsfe_Emu::track_info_( track_info_t* out, int track ) const
{
	return info.track_info_( out, track );
}

struct Nsfe_File : Gme_Info_
{
	Nsfe_Info info;
	
	Nsfe_File() { set_type( gme_nsfe_type ); }
	
	blargg_err_t load_( Data_Reader& in )
	{
		RETURN_ERR( info.load( in, 0 ) );
		info.disable_playlist( false );
		set_track_count( info.info.track_count );
		return 0;
	}
	
	blargg_err_t track_info_( track_info_t* out, int track ) const
	{
		return info.track_info_( out, track );
	}
};

static Music_Emu* new_nsfe_emu () { return BLARGG_NEW Nsfe_Emu ; }
static Music_Emu* new_nsfe_file() { return BLARGG_NEW Nsfe_File; }

gme_type_t_ const gme_nsfe_type [1] = { "Nintendo NES", 0, &new_nsfe_emu, &new_nsfe_file, "NSFE", 1 };

blargg_err_t Nsfe_Emu::load_( Data_Reader& in )
{
	if ( loading )
		return Nsf_Emu::load_( in );
	
	// TODO: this hacky recursion-avoidance could have subtle problems
	loading = true;
	blargg_err_t err = info.load( in, this );
	loading = false;
	disable_playlist( false );
	return err;
}

void Nsfe_Emu::disable_playlist( bool b )
{
	info.disable_playlist( b );
	set_track_count( info.info.track_count );
}

void Nsfe_Emu::clear_playlist_()
{
	disable_playlist();
	Nsf_Emu::clear_playlist_();
}

blargg_err_t Nsfe_Emu::start_track_( int track )
{
	return Nsf_Emu::start_track_( info.remap_track( track ) );
}