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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
|
// Game_Music_Emu 0.6-pre. http://www.slack.net/~ant/
#include "ay_emu.h"
#include "blargg_endian.h"
/* Copyright (C) 2006-2009 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"
const char* const gme_wrong_file_type = "Wrong file type for this emulator";
// TODO: probably don't need detailed errors as to why file is corrupt
int const spectrum_clock = 3546900; // 128K Spectrum
int const spectrum_period = 70908;
//int const spectrum_clock = 3500000; // 48K Spectrum
//int const spectrum_period = 69888;
int const cpc_clock = 2000000;
static void clear_track_vars( struct Ay_Emu *this )
{
this->current_track = -1;
track_stop( &this->track_filter );
}
void Ay_init( struct Ay_Emu *this )
{
this->sample_rate = 0;
this->mute_mask_ = 0;
this->tempo = (int)FP_ONE_TEMPO;
this->gain = (int)FP_ONE_GAIN;
this->track_count = 0;
// defaults
this->tfilter = *track_get_setup( &this->track_filter );
this->tfilter.max_initial = 2;
this->tfilter.lookahead = 6;
this->track_filter.silence_ignored_ = false;
this->beeper_output = NULL;
disable_beeper( this );
Ay_apu_init( &this->apu );
Z80_init( &this->cpu );
// clears fields
this->voice_count = 0;
this->voice_types = 0;
clear_track_vars( this );
}
// Track info
// Given pointer to 2-byte offset of data, returns pointer to data, or NULL if
// offset is 0 or there is less than min_size bytes of data available.
static byte const* get_data( struct file_t const* file, byte const ptr [], int min_size )
{
int offset = (int16_t) get_be16( ptr );
int pos = ptr - (byte const*) file->header;
int size = file->end - (byte const*) file->header;
assert( (unsigned) pos <= (unsigned) size - 2 );
int limit = size - min_size;
if ( limit < 0 || !offset || (unsigned) (pos + offset) > (unsigned) limit )
return NULL;
return ptr + offset;
}
static blargg_err_t parse_header( byte const in [], int size, struct file_t* out )
{
if ( size < header_size )
return gme_wrong_file_type;
out->header = (struct header_t const*) in;
out->end = in + size;
struct header_t const* h = (struct header_t const*) in;
if ( memcmp( h->tag, "ZXAYEMUL", 8 ) )
return gme_wrong_file_type;
out->tracks = get_data( out, h->track_info, (h->max_track + 1) * 4 );
if ( !out->tracks )
return "missing track data";
return 0;
}
// Setup
static void change_clock_rate( struct Ay_Emu *this, int rate )
{
this->clock_rate_ = rate;
Buffer_clock_rate( &this->stereo_buf, rate );
}
blargg_err_t Ay_load_mem( struct Ay_Emu *this, byte const in [], long size )
{
// Unload
this->voice_count = 0;
this->track_count = 0;
this->m3u.size = 0;
clear_track_vars( this );
assert( offsetof (struct header_t,track_info [2]) == header_size );
RETURN_ERR( parse_header( in, size, &this->file ) );
/* if ( file.header->vers > 2 )
warning( "Unknown file version" ); */
this->voice_count = ay_osc_count + 1; // +1 for beeper
static int const types [ay_osc_count + 1] = {
wave_type+0, wave_type+1, wave_type+2, mixed_type+1
};
this->voice_types = types;
Ay_apu_volume( &this->apu, this->gain);
// Setup buffer
change_clock_rate( this, spectrum_clock );
RETURN_ERR( Buffer_set_channel_count( &this->stereo_buf, this->voice_count, this->voice_types ) );
this->buf_changed_count = Buffer_channels_changed_count( &this->stereo_buf );
Sound_set_tempo( this, this->tempo );
Sound_mute_voices( this, this->mute_mask_ );
this->track_count = this->file.header->max_track + 1;
return 0;
}
static void set_beeper_output( struct Ay_Emu *this, struct Blip_Buffer* b )
{
this->beeper_output = b;
if ( b && !this->cpc_mode )
this->beeper_mask = 0x10;
else
disable_beeper( this );
}
static void set_voice( struct Ay_Emu *this, int i, struct Blip_Buffer* center )
{
if ( i >= ay_osc_count )
set_beeper_output( this, center );
else
Ay_apu_set_output( &this->apu, i, center );
}
static blargg_err_t run_clocks( struct Ay_Emu *this, blip_time_t* duration, int msec )
{
#if defined(ROCKBOX)
(void) msec;
#endif
cpu_time_t *end = duration;
struct Z80_Cpu* cpu = &this->cpu;
Z80_set_time( cpu, 0 );
// Since detection of CPC mode will halve clock rate during the frame
// and thus generate up to twice as much sound, we must generate half
// as much until mode is known.
if ( !(this->spectrum_mode | this->cpc_mode) )
*end /= 2;
while ( Z80_time( cpu ) < *end )
{
run_cpu( this, min( *end, this->next_play ) );
if ( Z80_time( cpu ) >= this->next_play )
{
// next frame
this->next_play += this->play_period;
if ( cpu->r.iff1 )
{
// interrupt enabled
if ( this->mem.ram [cpu->r.pc] == 0x76 )
cpu->r.pc++; // advance past HALT instruction
cpu->r.iff1 = 0;
cpu->r.iff2 = 0;
this->mem.ram [--cpu->r.sp] = (byte) (cpu->r.pc >> 8);
this->mem.ram [--cpu->r.sp] = (byte) (cpu->r.pc);
// fixed interrupt
cpu->r.pc = 0x38;
Z80_adjust_time( cpu, 12 );
if ( cpu->r.im == 2 )
{
// vectored interrupt
addr_t addr = cpu->r.i * 0x100 + 0xFF;
cpu->r.pc = this->mem.ram [(addr + 1) & 0xFFFF] * 0x100 + this->mem.ram [addr];
Z80_adjust_time( cpu, 6 );
}
}
}
}
// End time frame
*end = Z80_time( cpu );
this->next_play -= *end;
check( this->next_play >= 0 );
Z80_adjust_time( cpu, -*end );
Ay_apu_end_frame( &this->apu, *end );
return 0;
}
// Emulation
void cpu_out_( struct Ay_Emu *this, cpu_time_t time, addr_t addr, int data )
{
// Spectrum
if ( !this->cpc_mode )
{
switch ( addr & 0xFEFF )
{
case 0xFEFD:
this->spectrum_mode = true;
Ay_apu_write_addr( &this->apu, data );
return;
case 0xBEFD:
this->spectrum_mode = true;
Ay_apu_write_data( &this->apu, time, data );
return;
}
}
// CPC
if ( !this->spectrum_mode )
{
switch ( addr >> 8 )
{
case 0xF6:
switch ( data & 0xC0 )
{
case 0xC0:
Ay_apu_write_addr( &this->apu, this->cpc_latch );
goto enable_cpc;
case 0x80:
Ay_apu_write_data( &this->apu, time, this->cpc_latch );
goto enable_cpc;
}
break;
case 0xF4:
this->cpc_latch = data;
goto enable_cpc;
}
}
/* dprintf( "Unmapped OUT: $%04X <- $%02X\n", addr, data ); */
return;
enable_cpc:
if ( !this->cpc_mode )
{
this->cpc_mode = true;
disable_beeper( this );
change_clock_rate( this, cpc_clock );
Sound_set_tempo( this, this->tempo );
}
}
blargg_err_t Ay_set_sample_rate( struct Ay_Emu *this, int rate )
{
require( !this->sample_rate ); // sample rate can't be changed once set
Buffer_init( &this->stereo_buf );
RETURN_ERR( Buffer_set_sample_rate( &this->stereo_buf, rate, 1000 / 20 ) );
// Set buffer bass
Buffer_bass_freq( &this->stereo_buf, 160 );
this->sample_rate = rate;
RETURN_ERR( track_init( &this->track_filter, this ) );
this->tfilter.max_silence = 6 * stereo * this->sample_rate;
return 0;
}
void Sound_mute_voice( struct Ay_Emu *this, int index, bool mute )
{
require( (unsigned) index < (unsigned) this->voice_count );
int bit = 1 << index;
int mask = this->mute_mask_ | bit;
if ( !mute )
mask ^= bit;
Sound_mute_voices( this, mask );
}
void Sound_mute_voices( struct Ay_Emu *this, int mask )
{
require( this->sample_rate ); // sample rate must be set first
this->mute_mask_ = mask;
int i;
for ( i = this->voice_count; i--; )
{
if ( mask & (1 << i) )
{
set_voice( this, i, 0 );
}
else
{
struct channel_t ch = Buffer_channel( &this->stereo_buf, i );
assert( (ch.center && ch.left && ch.right) ||
(!ch.center && !ch.left && !ch.right) ); // all or nothing
set_voice( this, i, ch.center );
}
}
}
void Sound_set_tempo( struct Ay_Emu *this, int t )
{
require( this->sample_rate ); // sample rate must be set first
int const min = (int)(FP_ONE_TEMPO*0.02);
int const max = (int)(FP_ONE_TEMPO*4.00);
if ( t < min ) t = min;
if ( t > max ) t = max;
this->tempo = t;
int p = spectrum_period;
if ( this->clock_rate_ != spectrum_clock )
p = this->clock_rate_ / 50;
this->play_period = (blip_time_t) ((p * FP_ONE_TEMPO) / t);
}
blargg_err_t Ay_start_track( struct Ay_Emu *this, int track )
{
clear_track_vars( this );
// Remap track if playlist available
if ( this->m3u.size > 0 ) {
struct entry_t* e = &this->m3u.entries[track];
track = e->track;
}
this->current_track = track;
Buffer_clear( &this->stereo_buf );
byte* const mem = this->mem.ram;
memset( mem + 0x0000, 0xC9, 0x100 ); // fill RST vectors with RET
memset( mem + 0x0100, 0xFF, 0x4000 - 0x100 );
memset( mem + ram_addr, 0x00, mem_size - ram_addr );
// locate data blocks
byte const* const data = get_data( &this->file, this->file.tracks + track * 4 + 2, 14 );
if ( !data )
return "file data missing";
byte const* const more_data = get_data( &this->file, data + 10, 6 );
if ( !more_data )
return "file data missing";
byte const* blocks = get_data( &this->file, data + 12, 8 );
if ( !blocks )
return "file data missing";
// initial addresses
unsigned addr = get_be16( blocks );
if ( !addr )
return "file data missing";
unsigned init = get_be16( more_data + 2 );
if ( !init )
init = addr;
// copy blocks into memory
do
{
blocks += 2;
unsigned len = get_be16( blocks ); blocks += 2;
if ( addr + len > mem_size )
{
/* warning( "Bad data block size" ); */
len = mem_size - addr;
}
check( len );
byte const* in = get_data( &this->file, blocks, 0 ); blocks += 2;
if ( len > (unsigned) (this->file.end - in) )
{
/* warning( "File data missing" ); */
len = this->file.end - in;
}
memcpy( mem + addr, in, len );
if ( this->file.end - blocks < 8 )
{
/* warning( "File data missing" ); */
break;
}
}
while ( (addr = get_be16( blocks )) != 0 );
// copy and configure driver
static byte const passive [] = {
0xF3, // DI
0xCD, 0, 0, // CALL init
0xED, 0x5E, // LOOP: IM 2
0xFB, // EI
0x76, // HALT
0x18, 0xFA // JR LOOP
};
static byte const active [] = {
0xF3, // DI
0xCD, 0, 0, // CALL init
0xED, 0x56, // LOOP: IM 1
0xFB, // EI
0x76, // HALT
0xCD, 0, 0, // CALL play
0x18, 0xF7 // JR LOOP
};
memcpy( mem, passive, sizeof passive );
int const play_addr = get_be16( more_data + 4 );
if ( play_addr )
{
memcpy( mem, active, sizeof active );
mem [ 9] = play_addr;
mem [10] = play_addr >> 8;
}
mem [2] = init;
mem [3] = init >> 8;
mem [0x38] = 0xFB; // Put EI at interrupt vector (followed by RET)
// start at spectrum speed
change_clock_rate( this, spectrum_clock );
Sound_set_tempo( this, this->tempo );
struct registers_t r;
memset( &r, 0, sizeof(struct registers_t) );
r.sp = get_be16( more_data );
r.b.a = r.b.b = r.b.d = r.b.h = data [8];
r.b.flags = r.b.c = r.b.e = r.b.l = data [9];
r.alt.w = r.w;
r.ix = r.iy = r.w.hl;
memset( this->mem.padding1, 0xFF, sizeof this->mem.padding1 );
int const mirrored = 0x80; // this much is mirrored after end of memory
memset( this->mem.ram + mem_size + mirrored, 0xFF, sizeof this->mem.ram - mem_size - mirrored );
memcpy( this->mem.ram + mem_size, this->mem.ram, mirrored ); // some code wraps around (ugh)
Z80_reset( &this->cpu, this->mem.padding1, this->mem.padding1 );
Z80_map_mem( &this->cpu, 0, mem_size, this->mem.ram, this->mem.ram );
this->cpu.r = r;
this->beeper_delta = (int) ((ay_amp_range*4)/5);
this->last_beeper = 0;
this->next_play = this->play_period;
this->spectrum_mode = false;
this->cpc_mode = false;
this->cpc_latch = 0;
set_beeper_output( this, this->beeper_output );
Ay_apu_reset( &this->apu );
// a few tunes rely on channels having tone enabled at the beginning
Ay_apu_write_addr( &this->apu, 7 );
Ay_apu_write_data( &this->apu, 0, 0x38 );
// convert filter times to samples
struct setup_t s = this->tfilter;
s.max_initial *= this->sample_rate * stereo;
#ifdef GME_DISABLE_SILENCE_LOOKAHEAD
s.lookahead = 1;
#endif
track_setup( &this->track_filter, &s );
return track_start( &this->track_filter );
}
// Tell/Seek
static int msec_to_samples( int msec, int sample_rate )
{
int sec = msec / 1000;
msec -= sec * 1000;
return (sec * sample_rate + msec * sample_rate / 1000) * stereo;
}
int Track_tell( struct Ay_Emu *this )
{
int rate = this->sample_rate * stereo;
int sec = track_sample_count( &this->track_filter ) / rate;
return sec * 1000 + (track_sample_count( &this->track_filter ) - sec * rate) * 1000 / rate;
}
blargg_err_t Track_seek( struct Ay_Emu *this, int msec )
{
int time = msec_to_samples( msec, this->sample_rate );
if ( time < track_sample_count( &this->track_filter ) )
RETURN_ERR( Ay_start_track( this, this->current_track ) );
return Track_skip( this, time - track_sample_count( &this->track_filter ) );
}
blargg_err_t skip_( void *emu, int count )
{
struct Ay_Emu* this = (struct Ay_Emu*) emu;
// for long skip, mute sound
const int threshold = 32768;
if ( count > threshold )
{
int saved_mute = this->mute_mask_;
Sound_mute_voices( this, ~0 );
int n = count - threshold/2;
n &= ~(2048-1); // round to multiple of 2048
count -= n;
RETURN_ERR( skippy_( &this->track_filter, n ) );
Sound_mute_voices( this, saved_mute );
}
return skippy_( &this->track_filter, count );
}
blargg_err_t Track_skip( struct Ay_Emu *this, int count )
{
require( this->current_track >= 0 ); // start_track() must have been called already
return track_skip( &this->track_filter, count );
}
int Track_get_length( struct Ay_Emu* this, int n )
{
int length = 0;
byte const* track_info = get_data( &this->file, this->file.tracks + n * 4 + 2, 6 );
if ( track_info )
length = get_be16( track_info + 4 ) * (1000 / 50); // frames to msec
if ( (this->m3u.size > 0) && (n < this->m3u.size) ) {
struct entry_t* entry = &this->m3u.entries [n];
length = entry->length;
}
if ( length <= 0 )
length = 120 * 1000; /* 2 minutes */
return length;
}
void Track_set_fade( struct Ay_Emu *this, int start_msec, int length_msec )
{
track_set_fade( &this->track_filter, msec_to_samples( start_msec, this->sample_rate ),
length_msec * this->sample_rate / (1000 / stereo) );
}
blargg_err_t Ay_play( struct Ay_Emu *this, int out_count, sample_t* out )
{
require( this->current_track >= 0 );
require( out_count % stereo == 0 );
return track_play( &this->track_filter, out_count, out );
}
blargg_err_t play_( void *emu, int count, sample_t* out )
{
struct Ay_Emu* this = (struct Ay_Emu*) emu;
int remain = count;
while ( remain )
{
Buffer_disable_immediate_removal( &this->stereo_buf );
remain -= Buffer_read_samples( &this->stereo_buf, &out [count - remain], remain );
if ( remain )
{
if ( this->buf_changed_count != Buffer_channels_changed_count( &this->stereo_buf ) )
{
this->buf_changed_count = Buffer_channels_changed_count( &this->stereo_buf );
// Remute voices
Sound_mute_voices( this, this->mute_mask_ );
}
int msec = Buffer_length( &this->stereo_buf );
blip_time_t clocks_emulated = msec * this->clock_rate_ / 1000 - 100;
RETURN_ERR( run_clocks( this, &clocks_emulated, msec ) );
assert( clocks_emulated );
Buffer_end_frame( &this->stereo_buf, clocks_emulated );
}
}
return 0;
}
|