summaryrefslogtreecommitdiffstats
path: root/lib/rbcodec/codecs/libgme/nsfe_info.c
blob: 337b1e580ab826df57d96550a0eb26f5a27c4acd (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
// Game_Music_Emu 0.5.5. http://www.slack.net/~ant/

#include "nsf_emu.h"

#include "blargg_endian.h"
#include <string.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"

void Info_init( struct Nsfe_Info* this )
{ 
	this->playlist_disabled = false; 
}

void Info_unload( struct Nsfe_Info* this )
{
	memset(this->playlist, 0, 256);
	memset(this->track_times, 0, 256 * sizeof(int32_t));
	
	this->playlist_size = 0;
	this->track_times_size = 0;
}

// TODO: if no playlist, treat as if there is a playlist that is just 1,2,3,4,5... ?
void Info_disable_playlist( struct Nsfe_Info* this, bool b )
{
	this->playlist_disabled = b;
	this->track_count = this->playlist_size;
	if ( !this->track_count || this->playlist_disabled )
		this->track_count = this->actual_track_count_;
}

int Info_remap_track( struct Nsfe_Info* this, int track )
{
	if ( !this->playlist_disabled && (unsigned) track < (unsigned) this->playlist_size )
		track = this->playlist [track];
	return track;
}

const char eof_error [] = "Unexpected end of file";

// Read n bytes from memory buffer
static blargg_err_t in_read( void* dst, long bytes, void* data, long* offset, long size )
{
	if ((*offset + bytes) > size) return eof_error;
	
	memcpy(dst, (char*) data + *offset, bytes);
	*offset += bytes;
	return 0;
}

static blargg_err_t in_skip( long bytes, long *offset, long size )
{
	if ((*offset + bytes) > size) return eof_error;
	
	*offset += bytes;
	return 0;
}

// Skip n bytes from memory buffer

// Read multiple strings and separate into individual strings
static int read_strs( void* data, long bytes, long* offset, long size,
		const char* strs [4] )
{
	char* chars = (char*) data + *offset;
	chars [bytes - 1] = 0; // in case last string doesn't have terminator
	
	if ( in_skip( bytes, offset, size) )
		return -1;

	int count = 0, i;
	for ( i = 0; i < bytes; i++ )
	{
		strs [count] = &chars [i];
		while ( i < bytes && chars [i] )
			i++;
		
		count++;
		if (count >= 4) 
			break;
	}
	
	return count;
}

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 Info_load( struct Nsfe_Info* this, void* data, long size, struct Nsf_Emu* nsf_emu )
{
	long offset = 0;
	int const nsfe_info_size = 16;
	assert( offsetof (struct nsfe_info_t,unused [6]) == nsfe_info_size );
	
	// check header
	byte signature [4];
	blargg_err_t err = in_read( signature, sizeof signature, data, &offset, size );
	if ( err )
		return (err == eof_error ? gme_wrong_file_type : err);
	if ( memcmp( signature, "NSFE", 4 ) ) {
	}
	
	// free previous info
	/* TODO: clear track_names */
	memset(this->playlist, 0, 256);
	memset(this->track_times, 0, 256 * sizeof(int32_t));
	
	this->playlist_size = 0;
	this->track_times_size = 0;
	
	// default nsf header
	static const struct 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
	};
	
	memcpy( &nsf_emu->header, &base_header, sizeof 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, data, &offset, size ) );
		
		int chunk_size = get_le32( block_header [0] );
		int tag  = get_le32( block_header [1] );
		
		switch ( tag )
		{
			case BLARGG_4CHAR('O','F','N','I'): {
				check( phase == 0 );
				if ( chunk_size < 8 )
					return "Corrupt file";
				
				struct nsfe_info_t finfo;
				finfo.track_count = 1;
				finfo.first_track = 0;
				
				RETURN_ERR( in_read( &finfo, min( chunk_size, nsfe_info_size ), 
					(char*) data, &offset, size ) );
				
				if ( chunk_size > nsfe_info_size )
					RETURN_ERR( in_skip( chunk_size - nsfe_info_size, &offset, size ) );
					
				phase = 1;
				nsf_emu->header.speed_flags = finfo.speed_flags;
				nsf_emu->header.chip_flags  = finfo.chip_flags;
				nsf_emu->header.track_count = finfo.track_count;
				this->actual_track_count_ = finfo.track_count;
				nsf_emu->header.first_track = finfo.first_track;
				memcpy( nsf_emu->header.load_addr, finfo.load_addr, 2 * 3 );
				break;
			}
			
			case BLARGG_4CHAR('K','N','A','B'):
				if ( chunk_size > (int) sizeof nsf_emu->header.banks )
					return "Corrupt file";
				RETURN_ERR( in_read( nsf_emu->header.banks, chunk_size, data, &offset, size ) );
				break;
			
			case BLARGG_4CHAR('h','t','u','a'): {
				const char* strs [4];
				int n = read_strs( data, chunk_size, &offset, size, strs );
				if ( n < 0 ) 
					return eof_error;
				break;
			}
			
			case BLARGG_4CHAR('e','m','i','t'):
				this->track_times_size = chunk_size / 4;
				RETURN_ERR( in_read( this->track_times, this->track_times_size * 4, data, &offset, size ) );
				break;
			
			case BLARGG_4CHAR('l','b','l','t'):
				RETURN_ERR( in_skip( chunk_size, &offset, size ) );
				break;
			
			case BLARGG_4CHAR('t','s','l','p'):
				this->playlist_size = chunk_size;
				RETURN_ERR( in_read( &this->playlist [0], chunk_size, data, &offset, size ) );
				break;
			
			case BLARGG_4CHAR('A','T','A','D'): {
				check( phase == 1 );
				phase = 2;
				if ( !nsf_emu )
				{
					RETURN_ERR( in_skip( chunk_size, &offset, size ) );
				}
				else
				{
					// Avoid unexpected end of file
					if ( (offset + chunk_size) > size )
						return eof_error;
						
					RETURN_ERR( Rom_load( &nsf_emu->rom, (char*) data + offset, chunk_size, 0, 0, 0 ) );
					RETURN_ERR( Nsf_post_load( nsf_emu ) );
					offset += chunk_size;
				}
				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( chunk_size, &offset, size ) );
				break;
		}
	}
	
	return 0;
}

int Track_length( struct Nsf_Emu* this, int n )
{
	int length = 0; 
	if ( (this->m3u.size > 0) && (n < this->m3u.size) ) {
		struct entry_t* entry = &this->m3u.entries [n];
		length = entry->length;
	} 
	else if ( (this->info.playlist_size > 0) && (n < this->info.playlist_size) ) {
		int remapped = Info_remap_track( &this->info, n );
		if ( (unsigned) remapped < (unsigned) this->info.track_times_size )
			length = (int32_t) get_le32( &this->info.track_times [remapped] );
	}
	else if( (unsigned) n < (unsigned) this->info.track_times_size )
		length = (int32_t) get_le32( &this->info.track_times [n] );
	
	/* Length will be 2,30 minutes for one track songs,
		and 1,45 minutes for multitrack songs */
	if ( length <= 0 )
		length = (this->track_count > 1 ? 105 : 150) * 1000;
	
	return length;
}