1 /* Copyright (C) 2003-2007 Shay Green. This module is free software; you
2 can redistribute it and/or modify it under the terms of the GNU Lesser
3 General Public License as published by the Free Software Foundation; either
4 version 2.1 of the License, or (at your option) any later version. This
5 module is distributed in the hope that it will be useful, but WITHOUT ANY
6 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
7 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
8 details. You should have received a copy of the GNU Lesser General Public
9 License along with this module; if not, write to the Free Software Foundation,
10 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
11 
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <limits.h>
16 #include <math.h>
17 
18 #include "sound.h"
19 
20 #include "gba.h"
21 #include "globals.h"
22 
23 #include "memory.h"
24 #include "port.h"
25 #include "system.h"
26 
27 #define NR10 0x60
28 #define NR11 0x62
29 #define NR12 0x63
30 #define NR13 0x64
31 #define NR14 0x65
32 #define NR21 0x68
33 #define NR22 0x69
34 #define NR23 0x6c
35 #define NR24 0x6d
36 #define NR30 0x70
37 #define NR31 0x72
38 #define NR32 0x73
39 #define NR33 0x74
40 #define NR34 0x75
41 #define NR41 0x78
42 #define NR42 0x79
43 #define NR43 0x7c
44 #define NR44 0x7d
45 #define NR50 0x80
46 #define NR51 0x81
47 #define NR52 0x84
48 
49 /* 1/100th of a second */
50 #define SOUND_CLOCK_TICKS_ 167772
51 #define SOUNDVOLUME 0.5f
52 #define SOUNDVOLUME_ -1
53 
54 /*============================================================
55 	CLASS DECLS
56 ============================================================ */
57 
58 class Blip_Buffer
59 {
60 	public:
61 		const char * set_sample_rate( long samples_per_sec, int msec_length = 1000 / 4 );
62 		void clear( void);
63 		void save_state( blip_buffer_state_t* out );
64 		void load_state( blip_buffer_state_t const& in );
65 		uint32_t clock_rate_factor( long clock_rate ) const;
66 		long clock_rate_;
67 		int length_;		/* Length of buffer in milliseconds*/
68 		long sample_rate_;	/* Current output sample rate*/
69 		uint32_t factor_;
70 		uint32_t offset_;
71 		int32_t * buffer_;
72 		int32_t buffer_size_;
73 		int32_t reader_accum_;
74 		Blip_Buffer();
75 		~Blip_Buffer();
76 	private:
77 		Blip_Buffer( const Blip_Buffer& );
78 		Blip_Buffer& operator = ( const Blip_Buffer& );
79 };
80 
81 class Blip_Synth
82 {
83 	public:
84 	Blip_Buffer* buf;
85 	int delta_factor;
86 
87 	Blip_Synth();
88 
volume(double v)89 	void volume( double v ) { delta_factor = int ((v * 1.0) * (1L << BLIP_SAMPLE_BITS) + 0.5); }
90 	void offset( int32_t, int delta, Blip_Buffer* ) const;
91 	void offset_resampled( uint32_t, int delta, Blip_Buffer* ) const;
offset_inline(int32_t t,int delta,Blip_Buffer * buf) const92 	void offset_inline( int32_t t, int delta, Blip_Buffer* buf ) const {
93 		offset_resampled( t * buf->factor_ + buf->offset_, delta, buf );
94 	}
95 };
96 
97 class Gb_Osc
98 {
99 	public:
100 	Blip_Buffer* outputs [4];	/* NULL, right, left, center*/
101 	Blip_Buffer* output;		/* where to output sound*/
102 	uint8_t * regs;			/* osc's 5 registers*/
103 	int mode;			/* mode_dmg, mode_cgb, mode_agb*/
104 	int dac_off_amp;		/* amplitude when DAC is off*/
105 	int last_amp;			/* current amplitude in Blip_Buffer*/
106 	Blip_Synth const* good_synth;
107 	Blip_Synth  const* med_synth;
108 
109 	int delay;			/* clocks until frequency timer expires*/
110 	int length_ctr;			/* length counter*/
111 	unsigned phase;			/* waveform phase (or equivalent)*/
112 	bool enabled;			/* internal enabled flag*/
113 
114 	void clock_length();
115 	void reset();
116 	protected:
117 	void update_amp( int32_t, int new_amp );
118 	int write_trig( int frame_phase, int max_len, int old_data );
119 };
120 
121 class Gb_Env : public Gb_Osc
122 {
123 	public:
124 	int  env_delay;
125 	int  volume;
126 	bool env_enabled;
127 
128 	void clock_envelope();
129 	bool write_register( int frame_phase, int reg, int old_data, int data );
130 
reset()131 	void reset()
132 	{
133 		env_delay = 0;
134 		volume    = 0;
135 		Gb_Osc::reset();
136 	}
137 	private:
138 	void zombie_volume( int old, int data );
139 	int reload_env_timer();
140 };
141 
142 class Gb_Square : public Gb_Env
143 {
144 	public:
145 	bool write_register( int frame_phase, int reg, int old_data, int data );
146 	void run( int32_t, int32_t );
147 
reset()148 	void reset()
149 	{
150 		Gb_Env::reset();
151 		delay = 0x40000000; /* TODO: something less hacky (never clocked until first trigger)*/
152 	}
153 	private:
154 	/* Frequency timer period*/
period() const155 	int period() const { return (2048 - GB_OSC_FREQUENCY()) * (CLK_MUL_MUL_4); }
156 };
157 
158 class Gb_Sweep_Square : public Gb_Square
159 {
160 	public:
161 	int  sweep_freq;
162 	int  sweep_delay;
163 	bool sweep_enabled;
164 	bool sweep_neg;
165 
166 	void clock_sweep();
167 	void write_register( int frame_phase, int reg, int old_data, int data );
168 
reset()169 	void reset()
170 	{
171 		sweep_freq    = 0;
172 		sweep_delay   = 0;
173 		sweep_enabled = false;
174 		sweep_neg     = false;
175 		Gb_Square::reset();
176 	}
177 	private:
178 	void calc_sweep( bool update );
179 };
180 
181 class Gb_Noise : public Gb_Env
182 {
183 	public:
184 	int divider; /* noise has more complex frequency divider setup*/
185 
186 	void run( int32_t, int32_t );
187 	void write_register( int frame_phase, int reg, int old_data, int data );
188 
reset()189 	void reset()
190 	{
191 		divider = 0;
192 		Gb_Env::reset();
193 		delay = CLK_MUL_MUL_4; /* TODO: remove?*/
194 	}
195 };
196 
197 class Gb_Wave : public Gb_Osc
198 {
199 	public:
200 	int sample_buf;		/* last wave RAM byte read (hardware has this as well)*/
201 	int agb_mask;		/* 0xFF if AGB features enabled, 0 otherwise*/
202 	uint8_t* wave_ram;	/* 32 bytes (64 nybbles), stored in APU*/
203 
204 	void write_register( int frame_phase, int reg, int old_data, int data );
205 	void run( int32_t, int32_t );
206 
207 	/* Reads/writes wave RAM*/
208 	int read( unsigned addr ) const;
209 	void write( unsigned addr, int data );
210 
reset()211 	void reset()
212 	{
213 		sample_buf = 0;
214 		Gb_Osc::reset();
215 	}
216 
217 	private:
218 	friend class Gb_Apu;
219 
220 	/* Frequency timer period*/
period() const221 	int period() const { return (2048 - GB_OSC_FREQUENCY()) * (CLK_MUL_MUL_2); }
222 
223 	void corrupt_wave();
224 
225 	/* Wave index that would be accessed, or -1 if no access would occur*/
226 	int access( unsigned addr ) const;
227 };
228 
229 /*============================================================
230 	INLINE CLASS FUNCS
231 ============================================================ */
232 
offset_resampled(uint32_t time,int delta,Blip_Buffer * blip_buf) const233 INLINE void Blip_Synth::offset_resampled( uint32_t time, int delta, Blip_Buffer* blip_buf ) const
234 {
235 	int32_t left, right, phase;
236 	int32_t *buf;
237 
238 	delta *= delta_factor;
239 	buf = blip_buf->buffer_ + (time >> BLIP_BUFFER_ACCURACY);
240 	phase = (int) (time >> (BLIP_BUFFER_ACCURACY - BLIP_PHASE_BITS) & BLIP_RES_MIN_ONE);
241 
242 	left = buf [0] + delta;
243 
244 	right = (delta >> BLIP_PHASE_BITS) * phase;
245 
246 	left  -= right;
247 	right += buf [1];
248 
249 	buf [0] = left;
250 	buf [1] = right;
251 }
252 
offset(int32_t t,int delta,Blip_Buffer * buf) const253 INLINE void Blip_Synth::offset( int32_t t, int delta, Blip_Buffer* buf ) const
254 {
255         offset_resampled( t * buf->factor_ + buf->offset_, delta, buf );
256 }
257 
read(unsigned addr) const258 INLINE int Gb_Wave::read( unsigned addr ) const
259 {
260 	int index;
261 
262 	if(enabled)
263 		index = access( addr );
264 	else
265 		index = addr & 0x0F;
266 
267 	unsigned char const * wave_bank = &wave_ram[(~regs[0] & BANK40_MASK) >> 2 & agb_mask];
268 
269 	return (index < 0 ? 0xFF : wave_bank[index]);
270 }
271 
write(unsigned addr,int data)272 INLINE void Gb_Wave::write( unsigned addr, int data )
273 {
274 	int index;
275 
276 	if(enabled)
277 		index = access( addr );
278 	else
279 		index = addr & 0x0F;
280 
281 	unsigned char * wave_bank = &wave_ram[(~regs[0] & BANK40_MASK) >> 2 & agb_mask];
282 
283 	if ( index >= 0 )
284 		wave_bank[index] = data;;
285 }
286 
287 static int16_t   soundFinalWave [1600];
288 long  soundSampleRate    = 22050;
289 int   SOUND_CLOCK_TICKS  = SOUND_CLOCK_TICKS_;
290 int   soundTicks         = SOUND_CLOCK_TICKS_;
291 
292 static int soundEnableFlag   = 0x3ff; /* emulator channels enabled*/
293 static float const apu_vols [4] = { -0.25f, -0.5f, -1.0f, -0.25f };
294 
295 static const int table [0x40] =
296 {
297 		0xFF10,     0,0xFF11,0xFF12,0xFF13,0xFF14,     0,     0,
298 		0xFF16,0xFF17,     0,     0,0xFF18,0xFF19,     0,     0,
299 		0xFF1A,     0,0xFF1B,0xFF1C,0xFF1D,0xFF1E,     0,     0,
300 		0xFF20,0xFF21,     0,     0,0xFF22,0xFF23,     0,     0,
301 		0xFF24,0xFF25,     0,     0,0xFF26,     0,     0,     0,
302 		     0,     0,     0,     0,     0,     0,     0,     0,
303 		0xFF30,0xFF31,0xFF32,0xFF33,0xFF34,0xFF35,0xFF36,0xFF37,
304 		0xFF38,0xFF39,0xFF3A,0xFF3B,0xFF3C,0xFF3D,0xFF3E,0xFF3F,
305 };
306 
307 typedef struct
308 {
309 	int last_amp;
310 	int last_time;
311 	int shift;
312 	Blip_Buffer* output;
313 } gba_pcm_t;
314 
315 typedef struct
316 {
317 	bool enabled;
318 	uint8_t   fifo [32];
319 	int  count;
320 	int  dac;
321 	int  readIndex;
322 	int  writeIndex;
323 	int     which;
324 	int  timer;
325 	gba_pcm_t pcm;
326 } gba_pcm_fifo_t;
327 
328 static gba_pcm_fifo_t   pcm [2];
329 
330 
331 static Blip_Synth pcm_synth; // 32 kHz, 16 kHz, 8 kHz
332 
333 static Blip_Buffer bufs_buffer [BUFS_SIZE];
334 static int mixer_samples_read;
335 
gba_pcm_init(void)336 static void gba_pcm_init (void)
337 {
338 	pcm[0].pcm.output    = 0;
339 	pcm[0].pcm.last_time = 0;
340 	pcm[0].pcm.last_amp  = 0;
341 	pcm[0].pcm.shift     = 0;
342 
343 	pcm[1].pcm.output    = 0;
344 	pcm[1].pcm.last_time = 0;
345 	pcm[1].pcm.last_amp  = 0;
346 	pcm[1].pcm.shift     = 0;
347 }
348 
gba_pcm_apply_control(int pcm_idx,int idx)349 static void gba_pcm_apply_control( int pcm_idx, int idx )
350 {
351 	int ch = 0;
352 	pcm[pcm_idx].pcm.shift = ~ioMem [SGCNT0_H] >> (2 + idx) & 1;
353 
354 	if ( (ioMem [NR52] & 0x80) )
355 		ch = ioMem [SGCNT0_H+1] >> (idx << 2) & 3;
356 
357 	Blip_Buffer* out = 0;
358 	switch ( ch )
359 	{
360 		case 1:
361 			out = &bufs_buffer[1];
362 			break;
363 		case 2:
364 			out = &bufs_buffer[0];
365 			break;
366 		case 3:
367 			out = &bufs_buffer[2];
368 			break;
369 	}
370 
371 	if ( pcm[pcm_idx].pcm.output != out )
372 	{
373 		if ( pcm[pcm_idx].pcm.output )
374 			pcm_synth.offset( SOUND_CLOCK_TICKS - soundTicks, -pcm[pcm_idx].pcm.last_amp, pcm[pcm_idx].pcm.output );
375 		pcm[pcm_idx].pcm.last_amp = 0;
376 		pcm[pcm_idx].pcm.output = out;
377 	}
378 }
379 
380 /*============================================================
381 	GB APU
382 ============================================================ */
383 
384 /* 0: Square 1, 1: Square 2, 2: Wave, 3: Noise */
385 #define OSC_COUNT 4
386 
387 /* Resets hardware to initial power on state BEFORE boot ROM runs. Mode selects*/
388 /* sound hardware. Additional AGB wave features are enabled separately.*/
389 #define MODE_AGB	2
390 
391 #define START_ADDR	0xFF10
392 #define END_ADDR	0xFF3F
393 
394 /* Reads and writes must be within the START_ADDR to END_ADDR range, inclusive.*/
395 /* Addresses outside this range are not mapped to the sound hardware.*/
396 #define REGISTER_COUNT	48
397 #define REGS_SIZE 64
398 
399 /* Clock rate that sound hardware runs at.
400  * formula: 4194304 * 4
401  * */
402 #define CLOCK_RATE 16777216
403 
404 struct gb_apu_t
405 {
406 	bool		reduce_clicks_;
407 	uint8_t		regs[REGS_SIZE]; // last values written to registers
408 	int32_t		last_time;	// time sound emulator has been run to
409 	int32_t		frame_time;	// time of next frame sequencer action
410 	int32_t		frame_period;       // clocks between each frame sequencer step
411 	int32_t         frame_phase;    // phase of next frame sequencer step
412 	double		volume_;
413 	Gb_Osc*		oscs [OSC_COUNT];
414 	Gb_Sweep_Square square1;
415 	Gb_Square       square2;
416 	Gb_Wave         wave;
417 	Gb_Noise        noise;
418 	Blip_Synth	good_synth;
419 	Blip_Synth	med_synth;
420 } gb_apu;
421 
422 // Format of save state. Should be stable across versions of the library,
423 // with earlier versions properly opening later save states. Includes some
424 // room for expansion so the state size shouldn't increase.
425 struct gb_apu_state_t
426 {
427 	// Values stored as plain int so your code can read/write them easily.
428 	// Structure can NOT be written to disk, since format is not portable.
429 	typedef int val_t;
430 
431 	enum { format0 = 0x50414247 };
432 
433 	val_t format;   // format of all following data
434 	val_t version;  // later versions just add fields to end
435 
436 	unsigned char regs [0x40];
437 	val_t frame_time;
438 	val_t frame_phase;
439 
440 	val_t sweep_freq;
441 	val_t sweep_delay;
442 	val_t sweep_enabled;
443 	val_t sweep_neg;
444 	val_t noise_divider;
445 	val_t wave_buf;
446 
447 	val_t delay      [4];
448 	val_t length_ctr [4];
449 	val_t phase      [4];
450 	val_t enabled    [4];
451 
452 	val_t env_delay   [3];
453 	val_t env_volume  [3];
454 	val_t env_enabled [3];
455 
456 	val_t unused  [13]; // for future expansion
457 };
458 
459 #define VOL_REG 0xFF24
460 #define STEREO_REG 0xFF25
461 #define STATUS_REG 0xFF26
462 #define WAVE_RAM 0xFF30
463 #define POWER_MASK 0x80
464 
465 #define OSC_COUNT 4
466 
gb_apu_reduce_clicks(bool reduce)467 static void gb_apu_reduce_clicks( bool reduce )
468 {
469 	gb_apu.reduce_clicks_ = reduce;
470 
471 	/* Click reduction makes DAC off generate same output as volume 0*/
472 	int dac_off_amp = 0;
473 
474 	gb_apu.oscs[0]->dac_off_amp = dac_off_amp;
475 	gb_apu.oscs[1]->dac_off_amp = dac_off_amp;
476 	gb_apu.oscs[2]->dac_off_amp = dac_off_amp;
477 	gb_apu.oscs[3]->dac_off_amp = dac_off_amp;
478 
479 	/* AGB always eliminates clicks on wave channel using same method*/
480 	gb_apu.wave.dac_off_amp = -DAC_BIAS;
481 }
482 
gb_apu_synth_volume(int iv)483 static void gb_apu_synth_volume( int iv )
484 {
485 	double v = gb_apu.volume_ * 0.60 / OSC_COUNT / 15 /*steps*/ / 8 /*master vol range*/ * iv;
486 	gb_apu.good_synth.volume( v );
487 	gb_apu.med_synth .volume( v );
488 }
489 
gb_apu_apply_volume(void)490 static void gb_apu_apply_volume (void)
491 {
492 	int data, left, right, vol_tmp;
493 	data  = gb_apu.regs [VOL_REG - START_ADDR];
494 	left  = data >> 4 & 7;
495 	right = data & 7;
496 	vol_tmp = left < right ? right : left;
497 	gb_apu_synth_volume( vol_tmp + 1 );
498 }
499 
gb_apu_silence_osc(Gb_Osc & o)500 static void gb_apu_silence_osc( Gb_Osc& o )
501 {
502 	int delta;
503 
504 	delta = -o.last_amp;
505 	if ( delta )
506 	{
507 		o.last_amp = 0;
508 		if ( o.output )
509 		{
510 			gb_apu.med_synth.offset( gb_apu.last_time, delta, o.output );
511 		}
512 	}
513 }
514 
gb_apu_run_until_(int32_t end_time)515 static void gb_apu_run_until_( int32_t end_time )
516 {
517 	int32_t time;
518 
519 	do{
520 		/* run oscillators*/
521 		time = end_time;
522 		if ( time > gb_apu.frame_time )
523 			time = gb_apu.frame_time;
524 
525 		gb_apu.square1.run( gb_apu.last_time, time );
526 		gb_apu.square2.run( gb_apu.last_time, time );
527 		gb_apu.wave   .run( gb_apu.last_time, time );
528 		gb_apu.noise  .run( gb_apu.last_time, time );
529 		gb_apu.last_time = time;
530 
531 		if ( time == end_time )
532 			break;
533 
534 		/* run frame sequencer*/
535 		gb_apu.frame_time += gb_apu.frame_period * CLK_MUL;
536 		switch ( gb_apu.frame_phase++ )
537 		{
538 			case 2:
539 			case 6:
540 				/* 128 Hz*/
541 				gb_apu.square1.clock_sweep();
542 			case 0:
543 			case 4:
544 				/* 256 Hz*/
545 				gb_apu.square1.clock_length();
546 				gb_apu.square2.clock_length();
547 				gb_apu.wave   .clock_length();
548 				gb_apu.noise  .clock_length();
549 				break;
550 
551 			case 7:
552 				/* 64 Hz*/
553 				gb_apu.frame_phase = 0;
554 				gb_apu.square1.clock_envelope();
555 				gb_apu.square2.clock_envelope();
556 				gb_apu.noise  .clock_envelope();
557 		}
558 	}while(1);
559 }
560 
write_register(int frame_phase,int reg,int old_data,int data)561 INLINE void Gb_Sweep_Square::write_register( int frame_phase, int reg, int old_data, int data )
562 {
563         if ( reg == 0 && sweep_enabled && sweep_neg && !(data & 0x08) )
564                 enabled = false; // sweep negate disabled after used
565 
566         if ( Gb_Square::write_register( frame_phase, reg, old_data, data ) )
567         {
568                 sweep_freq = GB_OSC_FREQUENCY();
569                 sweep_neg = false;
570                 reload_sweep_timer();
571                 sweep_enabled = (regs [0] & (PERIOD_MASK | SHIFT_MASK)) != 0;
572                 if ( regs [0] & SHIFT_MASK )
573                         calc_sweep( false );
574         }
575 }
576 
write_register(int frame_phase,int reg,int old_data,int data)577 INLINE void Gb_Wave::write_register( int frame_phase, int reg, int old_data, int data )
578 {
579         switch ( reg )
580 	{
581 		case 0:
582 			if ( !GB_WAVE_DAC_ENABLED() )
583 				enabled = false;
584 			break;
585 
586 		case 1:
587 			length_ctr = 256 - data;
588 			break;
589 
590 		case 4:
591 			if ( write_trig( frame_phase, 256, old_data ) )
592 			{
593 				if ( !GB_WAVE_DAC_ENABLED() )
594 					enabled = false;
595 				phase = 0;
596 				delay    = period() + CLK_MUL_MUL_6;
597 			}
598 	}
599 }
600 
write_register(int frame_phase,int reg,int old_data,int data)601 INLINE void Gb_Noise::write_register( int frame_phase, int reg, int old_data, int data )
602 {
603         if ( Gb_Env::write_register( frame_phase, reg, old_data, data ) )
604         {
605                 phase = 0x7FFF;
606                 delay += CLK_MUL_MUL_8;
607         }
608 }
609 
gb_apu_write_osc(int index,int reg,int old_data,int data)610 static void gb_apu_write_osc( int index, int reg, int old_data, int data )
611 {
612         reg -= index * 5;
613         switch ( index )
614 	{
615 		case 0:
616 			gb_apu.square1.write_register( gb_apu.frame_phase, reg, old_data, data );
617 			break;
618 		case 1:
619 			gb_apu.square2.write_register( gb_apu.frame_phase, reg, old_data, data );
620 			break;
621 		case 2:
622 			gb_apu.wave.write_register( gb_apu.frame_phase, reg, old_data, data );
623 			break;
624 		case 3:
625 			gb_apu.noise.write_register( gb_apu.frame_phase, reg, old_data, data );
626 			break;
627 	}
628 }
629 
gb_apu_calc_output(int osc)630 static INLINE int gb_apu_calc_output( int osc )
631 {
632 	int bits = gb_apu.regs [STEREO_REG - START_ADDR] >> osc;
633 	return (bits >> 3 & 2) | (bits & 1);
634 }
635 
gb_apu_write_register(int32_t time,unsigned addr,int data)636 static void gb_apu_write_register( int32_t time, unsigned addr, int data )
637 {
638 	int reg = addr - START_ADDR;
639 	if ( (unsigned) reg >= REGISTER_COUNT )
640 		return;
641 
642 	if ( addr < STATUS_REG && !(gb_apu.regs [STATUS_REG - START_ADDR] & POWER_MASK) )
643 		return;	/* Power is off*/
644 
645 	if ( time > gb_apu.last_time )
646 		gb_apu_run_until_( time );
647 
648 	if ( addr >= WAVE_RAM )
649 	{
650 		gb_apu.wave.write( addr, data );
651 	}
652 	else
653 	{
654 		int old_data = gb_apu.regs [reg];
655 		gb_apu.regs [reg] = data;
656 
657 		if ( addr < VOL_REG )
658 			gb_apu_write_osc( reg / 5, reg, old_data, data );	/* Oscillator*/
659 		else if ( addr == VOL_REG && data != old_data )
660 		{
661 			/* Master volume*/
662 			for ( int i = OSC_COUNT; --i >= 0; )
663 				gb_apu_silence_osc( *gb_apu.oscs [i] );
664 
665 			gb_apu_apply_volume();
666 		}
667 		else if ( addr == STEREO_REG )
668 		{
669 			/* Stereo panning*/
670 			for ( int i = OSC_COUNT; --i >= 0; )
671 			{
672 				Gb_Osc& o = *gb_apu.oscs [i];
673 				Blip_Buffer* out = o.outputs [gb_apu_calc_output( i )];
674 				if ( o.output != out )
675 				{
676 					gb_apu_silence_osc( o );
677 					o.output = out;
678 				}
679 			} }
680 		else if ( addr == STATUS_REG && (data ^ old_data) & POWER_MASK )
681 		{
682 			/* Power control*/
683 			gb_apu.frame_phase = 0;
684 			for ( int i = OSC_COUNT; --i >= 0; )
685 				gb_apu_silence_osc( *gb_apu.oscs [i] );
686 
687 			for ( int i = 0; i < 32; i++ )
688 				gb_apu.regs [i] = 0;
689 
690 			gb_apu.square1.reset();
691 			gb_apu.square2.reset();
692 			gb_apu.wave   .reset();
693 			gb_apu.noise  .reset();
694 
695 			gb_apu_apply_volume();
696 
697 			gb_apu.square1.length_ctr = 64;
698 			gb_apu.square2.length_ctr = 64;
699 			gb_apu.wave   .length_ctr = 256;
700 			gb_apu.noise  .length_ctr = 64;
701 
702 			gb_apu.regs [STATUS_REG - START_ADDR] = data;
703 		}
704 	}
705 }
706 
gb_apu_reset(uint32_t mode,bool agb_wave)707 static void gb_apu_reset( uint32_t mode, bool agb_wave )
708 {
709 	/* Hardware mode*/
710 	mode = MODE_AGB; /* using AGB wave features implies AGB hardware*/
711 	gb_apu.wave.agb_mask = 0xFF;
712 	gb_apu.oscs [0]->mode = mode;
713 	gb_apu.oscs [1]->mode = mode;
714 	gb_apu.oscs [2]->mode = mode;
715 	gb_apu.oscs [3]->mode = mode;
716 	gb_apu_reduce_clicks( gb_apu.reduce_clicks_ );
717 
718 	/* Reset state*/
719 	gb_apu.frame_time  = 0;
720 	gb_apu.last_time   = 0;
721 	gb_apu.frame_phase = 0;
722 
723 	for ( int i = 0; i < 32; i++ )
724 		gb_apu.regs [i] = 0;
725 
726 	gb_apu.square1.reset();
727 	gb_apu.square2.reset();
728 	gb_apu.wave   .reset();
729 	gb_apu.noise  .reset();
730 
731 	gb_apu_apply_volume();
732 
733 	gb_apu.square1.length_ctr = 64;
734 	gb_apu.square2.length_ctr = 64;
735 	gb_apu.wave   .length_ctr = 256;
736 	gb_apu.noise  .length_ctr = 64;
737 
738 	/* Load initial wave RAM*/
739 	static unsigned char const initial_wave [2] [16] = {
740 		{0x84,0x40,0x43,0xAA,0x2D,0x78,0x92,0x3C,0x60,0x59,0x59,0xB0,0x34,0xB8,0x2E,0xDA},
741 		{0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF},
742 	};
743 	for ( int b = 2; --b >= 0; )
744 	{
745 		/* Init both banks (does nothing if not in AGB mode)*/
746 		gb_apu_write_register( 0, 0xFF1A, b * 0x40 );
747 		for ( unsigned i = 0; i < sizeof initial_wave [0]; i++ )
748 			gb_apu_write_register( 0, i + WAVE_RAM, initial_wave [1] [i] );
749 	}
750 }
751 
gb_apu_new(void)752 static void gb_apu_new(void)
753 {
754 	int i;
755 
756 	gb_apu.wave.wave_ram = &gb_apu.regs [WAVE_RAM - START_ADDR];
757 
758 	gb_apu.oscs [0] = &gb_apu.square1;
759 	gb_apu.oscs [1] = &gb_apu.square2;
760 	gb_apu.oscs [2] = &gb_apu.wave;
761 	gb_apu.oscs [3] = &gb_apu.noise;
762 
763 	for ( i = OSC_COUNT; --i >= 0; )
764 	{
765 		Gb_Osc& o = *gb_apu.oscs [i];
766 		o.regs        = &gb_apu.regs [i * 5];
767 		o.output      = 0;
768 		o.outputs [0] = 0;
769 		o.outputs [1] = 0;
770 		o.outputs [2] = 0;
771 		o.outputs [3] = 0;
772 		o.good_synth  = &gb_apu.good_synth;
773 		o.med_synth   = &gb_apu.med_synth;
774 	}
775 
776 	gb_apu.reduce_clicks_ = false;
777 	gb_apu.frame_period = 4194304 / 512; /* 512 Hz*/
778 
779 	gb_apu.volume_ = 1.0;
780 	gb_apu_reset(MODE_AGB, false);
781 }
782 
783 
784 
gb_apu_set_output(Blip_Buffer * center,Blip_Buffer * left,Blip_Buffer * right,int osc)785 static void gb_apu_set_output( Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right, int osc )
786 {
787 	int i;
788 
789 	i = osc;
790 	do
791 	{
792 		Gb_Osc& o = *gb_apu.oscs [i];
793 		o.outputs [1] = right;
794 		o.outputs [2] = left;
795 		o.outputs [3] = center;
796 		o.output = o.outputs [gb_apu_calc_output( i )];
797 		++i;
798 	}
799 	while ( i < osc );
800 }
801 
gb_apu_volume(double v)802 static void gb_apu_volume( double v )
803 {
804 	if ( gb_apu.volume_ != v )
805 	{
806 		gb_apu.volume_ = v;
807 		gb_apu_apply_volume();
808 	}
809 }
810 
gb_apu_apply_stereo(void)811 static void gb_apu_apply_stereo (void)
812 {
813 	int i;
814 
815 	for ( i = OSC_COUNT; --i >= 0; )
816 	{
817 		Gb_Osc& o = *gb_apu.oscs [i];
818 		Blip_Buffer* out = o.outputs [gb_apu_calc_output( i )];
819 		if ( o.output != out )
820 		{
821 			gb_apu_silence_osc( o );
822 			o.output = out;
823 		}
824 	}
825 }
826 
827 #define REFLECT( x, y ) (save ?       (io->y) = (x) :         (x) = (io->y)          )
828 
gb_apu_save_load(gb_apu_state_t * io,bool save)829 static INLINE const char* gb_apu_save_load( gb_apu_state_t* io, bool save )
830 {
831 	int format, version;
832 
833 	format = io->format0;
834 
835 	REFLECT( format, format );
836 	if ( format != io->format0 )
837 		return "Unsupported sound save state format";
838 
839 	version = 0;
840 	REFLECT( version, version );
841 
842 	/* Registers and wave RAM*/
843 	if ( save )
844 		memcpy( io->regs, gb_apu.regs, sizeof io->regs );
845 	else
846 		memcpy( gb_apu.regs, io->regs, sizeof(gb_apu.regs) );
847 
848 	/* Frame sequencer*/
849 	REFLECT( gb_apu.frame_time,  frame_time  );
850 	REFLECT( gb_apu.frame_phase, frame_phase );
851 
852 	REFLECT( gb_apu.square1.sweep_freq,    sweep_freq );
853 	REFLECT( gb_apu.square1.sweep_delay,   sweep_delay );
854 	REFLECT( gb_apu.square1.sweep_enabled, sweep_enabled );
855 	REFLECT( gb_apu.square1.sweep_neg,     sweep_neg );
856 
857 	REFLECT( gb_apu.noise.divider,         noise_divider );
858 	REFLECT( gb_apu.wave.sample_buf,       wave_buf );
859 
860 	return 0;
861 }
862 
863 /* second function to avoid inline limits of some compilers*/
gb_apu_save_load2(gb_apu_state_t * io,bool save)864 static INLINE void gb_apu_save_load2( gb_apu_state_t* io, bool save )
865 {
866 	int i;
867 	for ( i = OSC_COUNT; --i >= 0; )
868 	{
869 		Gb_Osc& osc = *gb_apu.oscs [i];
870 		REFLECT( osc.delay,      delay      [i] );
871 		REFLECT( osc.length_ctr, length_ctr [i] );
872 		REFLECT( osc.phase,      phase      [i] );
873 		REFLECT( osc.enabled,    enabled    [i] );
874 
875 		if ( i != 2 )
876 		{
877 			int j = 2 < i ? 2 : i;
878 			Gb_Env& env = STATIC_CAST(Gb_Env&,osc);
879 			REFLECT( env.env_delay,   env_delay   [j] );
880 			REFLECT( env.volume,      env_volume  [j] );
881 			REFLECT( env.env_enabled, env_enabled [j] );
882 		}
883 	}
884 }
885 
gb_apu_save_state(gb_apu_state_t * out)886 static void gb_apu_save_state( gb_apu_state_t* out )
887 {
888 	(void) gb_apu_save_load( out, true );
889 	gb_apu_save_load2( out, true );
890 }
891 
gb_apu_load_state(gb_apu_state_t const & in)892 static const char * gb_apu_load_state( gb_apu_state_t const& in )
893 {
894 	RETURN_ERR( gb_apu_save_load( CONST_CAST(gb_apu_state_t*,&in), false));
895 	gb_apu_save_load2( CONST_CAST(gb_apu_state_t*,&in), false );
896 
897 	gb_apu_apply_stereo();
898 	gb_apu_synth_volume( 0 );          /* suppress output for the moment*/
899 	gb_apu_run_until_( gb_apu.last_time );    /* get last_amp updated*/
900 	gb_apu_apply_volume();             /* now use correct volume*/
901 
902 	return 0;
903 }
904 
905 /*============================================================
906 	GB OSCS
907 ============================================================ */
908 
909 #define TRIGGER_MASK 0x80
910 #define LENGTH_ENABLED 0x40
911 
912 #define VOLUME_SHIFT_PLUS_FOUR	6
913 #define SIZE20_MASK 0x20
914 
reset()915 void Gb_Osc::reset()
916 {
917         output   = 0;
918         last_amp = 0;
919         delay    = 0;
920         phase    = 0;
921         enabled  = false;
922 }
923 
update_amp(int32_t time,int new_amp)924 INLINE void Gb_Osc::update_amp( int32_t time, int new_amp )
925 {
926 	int delta = new_amp - last_amp;
927         if ( delta )
928         {
929                 last_amp = new_amp;
930                 med_synth->offset( time, delta, output );
931         }
932 }
933 
clock_length()934 void Gb_Osc::clock_length()
935 {
936         if ( (regs [4] & LENGTH_ENABLED) && length_ctr )
937         {
938                 if ( --length_ctr <= 0 )
939                         enabled = false;
940         }
941 }
942 
reload_env_timer()943 INLINE int Gb_Env::reload_env_timer()
944 {
945         int raw = regs [2] & 7;
946         env_delay = (raw ? raw : 8);
947         return raw;
948 }
949 
clock_envelope()950 void Gb_Env::clock_envelope()
951 {
952         if ( env_enabled && --env_delay <= 0 && reload_env_timer() )
953         {
954                 int v = volume + (regs [2] & 0x08 ? +1 : -1);
955                 if ( 0 <= v && v <= 15 )
956                         volume = v;
957                 else
958                         env_enabled = false;
959         }
960 }
961 
962 #define reload_sweep_timer() \
963         sweep_delay = (regs [0] & PERIOD_MASK) >> 4; \
964         if ( !sweep_delay ) \
965                 sweep_delay = 8;
966 
calc_sweep(bool update)967 void Gb_Sweep_Square::calc_sweep( bool update )
968 {
969 	int shift, delta, freq;
970 
971         shift = regs [0] & SHIFT_MASK;
972         delta = sweep_freq >> shift;
973         sweep_neg = (regs [0] & 0x08) != 0;
974         freq = sweep_freq + (sweep_neg ? -delta : delta);
975 
976         if ( freq > 0x7FF )
977                 enabled = false;
978         else if ( shift && update )
979         {
980                 sweep_freq = freq;
981 
982                 regs [3] = freq & 0xFF;
983                 regs [4] = (regs [4] & ~0x07) | (freq >> 8 & 0x07);
984         }
985 }
986 
clock_sweep()987 void Gb_Sweep_Square::clock_sweep()
988 {
989         if ( --sweep_delay <= 0 )
990         {
991                 reload_sweep_timer();
992                 if ( sweep_enabled && (regs [0] & PERIOD_MASK) )
993                 {
994                         calc_sweep( true  );
995                         calc_sweep( false );
996                 }
997         }
998 }
999 
access(unsigned addr) const1000 int Gb_Wave::access( unsigned addr ) const
1001 {
1002 	return addr & 0x0F;
1003 }
1004 
1005 // write_register
1006 
write_trig(int frame_phase,int max_len,int old_data)1007 int Gb_Osc::write_trig( int frame_phase, int max_len, int old_data )
1008 {
1009         int data = regs [4];
1010 
1011         if ( (frame_phase & 1) && !(old_data & LENGTH_ENABLED) && length_ctr )
1012         {
1013                 if ( (data & LENGTH_ENABLED))
1014                         length_ctr--;
1015         }
1016 
1017         if ( data & TRIGGER_MASK )
1018         {
1019                 enabled = true;
1020                 if ( !length_ctr )
1021                 {
1022                         length_ctr = max_len;
1023                         if ( (frame_phase & 1) && (data & LENGTH_ENABLED) )
1024                                 length_ctr--;
1025                 }
1026         }
1027 
1028         if ( !length_ctr )
1029                 enabled = false;
1030 
1031         return data & TRIGGER_MASK;
1032 }
1033 
zombie_volume(int old,int data)1034 INLINE void Gb_Env::zombie_volume( int old, int data )
1035 {
1036 	int v = volume;
1037 
1038 	// CGB-05 behavior, very close to AGB behavior as well
1039 	if ( (old ^ data) & 8 )
1040 	{
1041 		if ( !(old & 8) )
1042 		{
1043 			v++;
1044 			if ( old & 7 )
1045 				v++;
1046 		}
1047 
1048 		v = 16 - v;
1049 	}
1050 	else if ( (old & 0x0F) == 8 )
1051 		v++;
1052 	volume = v & 0x0F;
1053 }
1054 
write_register(int frame_phase,int reg,int old,int data)1055 bool Gb_Env::write_register( int frame_phase, int reg, int old, int data )
1056 {
1057         int const max_len = 64;
1058 
1059         switch ( reg )
1060 	{
1061 		case 1:
1062 			length_ctr = max_len - (data & (max_len - 1));
1063 			break;
1064 
1065 		case 2:
1066 			if ( !GB_ENV_DAC_ENABLED() )
1067 				enabled = false;
1068 
1069 			zombie_volume( old, data );
1070 
1071 			if ( (data & 7) && env_delay == 8 )
1072 			{
1073 				env_delay = 1;
1074 				clock_envelope(); // TODO: really happens at next length clock
1075 			}
1076 			break;
1077 
1078 		case 4:
1079 			if ( write_trig( frame_phase, max_len, old ) )
1080 			{
1081 				volume = regs [2] >> 4;
1082 				reload_env_timer();
1083 				env_enabled = true;
1084 				if ( frame_phase == 7 )
1085 					env_delay++;
1086 				if ( !GB_ENV_DAC_ENABLED() )
1087 					enabled = false;
1088 				return true;
1089 			}
1090 	}
1091         return false;
1092 }
1093 
write_register(int frame_phase,int reg,int old_data,int data)1094 bool Gb_Square::write_register( int frame_phase, int reg, int old_data, int data )
1095 {
1096         bool result = Gb_Env::write_register( frame_phase, reg, old_data, data );
1097         if ( result )
1098                 delay = (delay & (CLK_MUL_MUL_4 - 1)) + period();
1099         return result;
1100 }
1101 
1102 
corrupt_wave()1103 void Gb_Wave::corrupt_wave()
1104 {
1105         int pos = ((phase + 1) & BANK_SIZE_MIN_ONE) >> 1;
1106         if ( pos < 4 )
1107                 wave_ram [0] = wave_ram [pos];
1108         else
1109                 for ( int i = 4; --i >= 0; )
1110                         wave_ram [i] = wave_ram [(pos & ~3) + i];
1111 }
1112 
1113 /* Synthesis*/
1114 
run(int32_t time,int32_t end_time)1115 void Gb_Square::run( int32_t time, int32_t end_time )
1116 {
1117         /* Calc duty and phase*/
1118         static unsigned char const duty_offsets [4] = { 1, 1, 3, 7 };
1119         static unsigned char const duties       [4] = { 1, 2, 4, 6 };
1120         int const duty_code = regs [1] >> 6;
1121         int32_t duty_offset = duty_offsets [duty_code];
1122         int32_t duty = duties [duty_code];
1123 	/* AGB uses inverted duty*/
1124 	duty_offset -= duty;
1125 	duty = 8 - duty;
1126         int ph = (phase + duty_offset) & 7;
1127 
1128         /* Determine what will be generated*/
1129         int vol = 0;
1130         Blip_Buffer* const out = output;
1131         if ( out )
1132         {
1133                 int amp = dac_off_amp;
1134                 if ( GB_ENV_DAC_ENABLED() )
1135                 {
1136                         if ( enabled )
1137                                 vol = volume;
1138 
1139 			amp = -(vol >> 1);
1140 
1141                         /* Play inaudible frequencies as constant amplitude*/
1142                         if ( GB_OSC_FREQUENCY() >= 0x7FA && delay < CLK_MUL_MUL_32 )
1143                         {
1144                                 amp += (vol * duty) >> 3;
1145                                 vol = 0;
1146                         }
1147 
1148                         if ( ph < duty )
1149                         {
1150                                 amp += vol;
1151                                 vol = -vol;
1152                         }
1153                 }
1154                 update_amp( time, amp );
1155         }
1156 
1157         /* Generate wave*/
1158         time += delay;
1159         if ( time < end_time )
1160         {
1161                 int const per = period();
1162                 if ( !vol )
1163                 {
1164                         /* Maintain phase when not playing*/
1165                         int count = (end_time - time + per - 1) / per;
1166                         ph += count; /* will be masked below*/
1167                         time += (int32_t) count * per;
1168                 }
1169                 else
1170                 {
1171                         /* Output amplitude transitions*/
1172                         int delta = vol;
1173                         do
1174                         {
1175                                 ph = (ph + 1) & 7;
1176                                 if ( ph == 0 || ph == duty )
1177                                 {
1178                                         good_synth->offset_inline( time, delta, out );
1179                                         delta = -delta;
1180                                 }
1181                                 time += per;
1182                         }
1183                         while ( time < end_time );
1184 
1185                         if ( delta != vol )
1186                                 last_amp -= delta;
1187                 }
1188                 phase = (ph - duty_offset) & 7;
1189         }
1190         delay = time - end_time;
1191 }
1192 
1193 /* Quickly runs LFSR for a large number of clocks. For use when noise is generating*/
1194 /* no sound.*/
run_lfsr(unsigned s,unsigned mask,int count)1195 static unsigned run_lfsr( unsigned s, unsigned mask, int count )
1196 {
1197 	/* optimization used in several places:*/
1198 	/* ((s & (1 << b)) << n) ^ ((s & (1 << b)) << (n + 1)) = (s & (1 << b)) * (3 << n)*/
1199 
1200 	if ( mask == 0x4000 )
1201 	{
1202 		if ( count >= 32767 )
1203 			count %= 32767;
1204 
1205 		/* Convert from Fibonacci to Galois configuration,*/
1206 		/* shifted left 1 bit*/
1207 		s ^= (s & 1) * 0x8000;
1208 
1209 		/* Each iteration is equivalent to clocking LFSR 255 times*/
1210 		while ( (count -= 255) > 0 )
1211 			s ^= ((s & 0xE) << 12) ^ ((s & 0xE) << 11) ^ (s >> 3);
1212 		count += 255;
1213 
1214 		/* Each iteration is equivalent to clocking LFSR 15 times*/
1215 		/* (interesting similarity to single clocking below)*/
1216 		while ( (count -= 15) > 0 )
1217 			s ^= ((s & 2) * (3 << 13)) ^ (s >> 1);
1218 		count += 15;
1219 
1220 		/* Remaining singles*/
1221 		do{
1222 			--count;
1223 			s = ((s & 2) * (3 << 13)) ^ (s >> 1);
1224 		}while(count >= 0);
1225 
1226 		/* Convert back to Fibonacci configuration*/
1227 		s &= 0x7FFF;
1228 	}
1229 	else if ( count < 8)
1230 	{
1231 		/* won't fully replace upper 8 bits, so have to do the unoptimized way*/
1232 		do{
1233 			--count;
1234 			s = (s >> 1 | mask) ^ (mask & -((s - 1) & 2));
1235 		}while(count >= 0);
1236 	}
1237 	else
1238 	{
1239 		if ( count > 127 )
1240 		{
1241 			count %= 127;
1242 			if ( !count )
1243 				count = 127; /* must run at least once*/
1244 		}
1245 
1246 		/* Need to keep one extra bit of history*/
1247 		s = s << 1 & 0xFF;
1248 
1249 		/* Convert from Fibonacci to Galois configuration,*/
1250 		/* shifted left 2 bits*/
1251 		s ^= (s & 2) << 7;
1252 
1253 		/* Each iteration is equivalent to clocking LFSR 7 times*/
1254 		/* (interesting similarity to single clocking below)*/
1255 		while ( (count -= 7) > 0 )
1256 			s ^= ((s & 4) * (3 << 5)) ^ (s >> 1);
1257 		count += 7;
1258 
1259 		/* Remaining singles*/
1260 		while ( --count >= 0 )
1261 			s = ((s & 4) * (3 << 5)) ^ (s >> 1);
1262 
1263 		/* Convert back to Fibonacci configuration and*/
1264 		/* repeat last 8 bits above significant 7*/
1265 		s = (s << 7 & 0x7F80) | (s >> 1 & 0x7F);
1266 	}
1267 
1268 	return s;
1269 }
1270 
run(int32_t time,int32_t end_time)1271 void Gb_Noise::run( int32_t time, int32_t end_time )
1272 {
1273         /* Determine what will be generated*/
1274         int vol = 0;
1275         Blip_Buffer* const out = output;
1276         if ( out )
1277         {
1278                 int amp = dac_off_amp;
1279                 if ( GB_ENV_DAC_ENABLED() )
1280                 {
1281                         if ( enabled )
1282                                 vol = volume;
1283 
1284 			amp = -(vol >> 1);
1285 
1286                         if ( !(phase & 1) )
1287                         {
1288                                 amp += vol;
1289                                 vol = -vol;
1290                         }
1291                 }
1292 
1293                 /* AGB negates final output*/
1294 		vol = -vol;
1295 		amp    = -amp;
1296 
1297                 update_amp( time, amp );
1298         }
1299 
1300         /* Run timer and calculate time of next LFSR clock*/
1301         static unsigned char const period1s [8] = { 1, 2, 4, 6, 8, 10, 12, 14 };
1302         int const period1 = period1s [regs [3] & 7] * CLK_MUL;
1303         {
1304                 int extra = (end_time - time) - delay;
1305                 int const per2 = GB_NOISE_PERIOD2(8);
1306                 time += delay + ((divider ^ (per2 >> 1)) & (per2 - 1)) * period1;
1307 
1308                 int count = (extra < 0 ? 0 : (extra + period1 - 1) / period1);
1309                 divider = (divider - count) & PERIOD2_MASK;
1310                 delay = count * period1 - extra;
1311         }
1312 
1313         /* Generate wave*/
1314         if ( time < end_time )
1315         {
1316                 unsigned const mask = GB_NOISE_LFSR_MASK();
1317                 unsigned bits = phase;
1318 
1319                 int per = GB_NOISE_PERIOD2( period1 * 8 );
1320                 if ( GB_NOISE_PERIOD2_INDEX() >= 0xE )
1321                 {
1322                         time = end_time;
1323                 }
1324                 else if ( !vol )
1325                 {
1326                         /* Maintain phase when not playing*/
1327                         int count = (end_time - time + per - 1) / per;
1328                         time += (int32_t) count * per;
1329                         bits = run_lfsr( bits, ~mask, count );
1330                 }
1331                 else
1332                 {
1333                         /* Output amplitude transitions*/
1334                         int delta = -vol;
1335                         do
1336                         {
1337                                 unsigned changed = bits + 1;
1338                                 bits = bits >> 1 & mask;
1339                                 if ( changed & 2 )
1340                                 {
1341                                         bits |= ~mask;
1342                                         delta = -delta;
1343                                         med_synth->offset_inline( time, delta, out );
1344                                 }
1345                                 time += per;
1346                         }
1347                         while ( time < end_time );
1348 
1349                         if ( delta == vol )
1350                                 last_amp += delta;
1351                 }
1352                 phase = bits;
1353         }
1354 }
1355 
run(int32_t time,int32_t end_time)1356 void Gb_Wave::run( int32_t time, int32_t end_time )
1357 {
1358         /* Calc volume*/
1359         static unsigned char const volumes [8] = { 0, 4, 2, 1, 3, 3, 3, 3 };
1360         int const volume_idx = regs [2] >> 5 & (agb_mask | 3); /* 2 bits on DMG/CGB, 3 on AGB*/
1361         int const volume_mul = volumes [volume_idx];
1362 
1363         /* Determine what will be generated*/
1364         int playing = false;
1365         Blip_Buffer* const out = output;
1366         if ( out )
1367         {
1368                 int amp = dac_off_amp;
1369                 if ( GB_WAVE_DAC_ENABLED() )
1370                 {
1371                         /* Play inaudible frequencies as constant amplitude*/
1372                         amp = 128; /* really depends on average of all samples in wave*/
1373 
1374                         /* if delay is larger, constant amplitude won't start yet*/
1375                         if ( GB_OSC_FREQUENCY() <= 0x7FB || delay > CLK_MUL_MUL_15 )
1376                         {
1377                                 if ( volume_mul )
1378                                         playing = (int) enabled;
1379 
1380                                 amp = (sample_buf << (phase << 2 & 4) & 0xF0) * playing;
1381                         }
1382 
1383                         amp = ((amp * volume_mul) >> VOLUME_SHIFT_PLUS_FOUR) - DAC_BIAS;
1384                 }
1385                 update_amp( time, amp );
1386         }
1387 
1388         /* Generate wave*/
1389         time += delay;
1390         if ( time < end_time )
1391         {
1392                 unsigned char const* wave = wave_ram;
1393 
1394                 /* wave size and bank*/
1395                 int const flags = regs [0] & agb_mask;
1396                 int const wave_mask = (flags & SIZE20_MASK) | 0x1F;
1397                 int swap_banks = 0;
1398                 if ( flags & BANK40_MASK)
1399                 {
1400                         swap_banks = flags & SIZE20_MASK;
1401                         wave += BANK_SIZE_DIV_TWO - (swap_banks >> 1);
1402                 }
1403 
1404                 int ph = phase ^ swap_banks;
1405                 ph = (ph + 1) & wave_mask; /* pre-advance*/
1406 
1407                 int const per = period();
1408                 if ( !playing )
1409                 {
1410                         /* Maintain phase when not playing*/
1411                         int count = (end_time - time + per - 1) / per;
1412                         ph += count; /* will be masked below*/
1413                         time += (int32_t) count * per;
1414                 }
1415                 else
1416                 {
1417                         /* Output amplitude transitions*/
1418                         int lamp = last_amp + DAC_BIAS;
1419                         do
1420                         {
1421                                 /* Extract nybble*/
1422                                 int nybble = wave [ph >> 1] << (ph << 2 & 4) & 0xF0;
1423                                 ph = (ph + 1) & wave_mask;
1424 
1425                                 /* Scale by volume*/
1426                                 int amp = (nybble * volume_mul) >> VOLUME_SHIFT_PLUS_FOUR;
1427 
1428                                 int delta = amp - lamp;
1429                                 if ( delta )
1430                                 {
1431                                         lamp = amp;
1432                                         med_synth->offset_inline( time, delta, out );
1433                                 }
1434                                 time += per;
1435                         }
1436                         while ( time < end_time );
1437                         last_amp = lamp - DAC_BIAS;
1438                 }
1439                 ph = (ph - 1) & wave_mask; /* undo pre-advance and mask position*/
1440 
1441                 /* Keep track of last byte read*/
1442                 if ( enabled )
1443                         sample_buf = wave [ph >> 1];
1444 
1445                 phase = ph ^ swap_banks; /* undo swapped banks*/
1446         }
1447         delay = time - end_time;
1448 }
1449 
1450 /*============================================================
1451 	BLIP BUFFER
1452 ============================================================ */
1453 
1454 /* Blip_Buffer 0.4.1. http://www.slack.net/~ant */
1455 
1456 #define FIXED_SHIFT 12
1457 #define SAL_FIXED_SHIFT 4096
1458 #define TO_FIXED( f )   int ((f) * SAL_FIXED_SHIFT)
1459 #define FROM_FIXED( f ) ((f) >> FIXED_SHIFT)
1460 
Blip_Buffer()1461 Blip_Buffer::Blip_Buffer()
1462 {
1463    factor_       = INT_MAX;
1464    buffer_       = 0;
1465    buffer_size_  = 0;
1466    sample_rate_  = 0;
1467    clock_rate_   = 0;
1468    length_       = 0;
1469 
1470    clear();
1471 }
1472 
~Blip_Buffer()1473 Blip_Buffer::~Blip_Buffer()
1474 {
1475    if (buffer_)
1476       free(buffer_);
1477 }
1478 
clear(void)1479 void Blip_Buffer::clear( void)
1480 {
1481    offset_       = 0;
1482    reader_accum_ = 0;
1483    if (buffer_)
1484       memset( buffer_, 0, (buffer_size_ + BLIP_BUFFER_EXTRA_) * sizeof (int32_t) );
1485 }
1486 
set_sample_rate(long new_rate,int msec)1487 const char * Blip_Buffer::set_sample_rate( long new_rate, int msec )
1488 {
1489    /* start with maximum length that resampled time can represent*/
1490    long new_size = (ULONG_MAX >> BLIP_BUFFER_ACCURACY) - BLIP_BUFFER_EXTRA_ - 64;
1491    if ( msec != 0)
1492    {
1493       long s = (new_rate * (msec + 1) + 999) / 1000;
1494       if ( s < new_size )
1495          new_size = s;
1496    }
1497 
1498    if ( buffer_size_ != new_size )
1499    {
1500       void* p = realloc( buffer_, (new_size + BLIP_BUFFER_EXTRA_) * sizeof *buffer_ );
1501       if ( !p )
1502          return "Out of memory";
1503       buffer_ = (int32_t *) p;
1504    }
1505 
1506    buffer_size_ = new_size;
1507 
1508    /* update things based on the sample rate*/
1509    sample_rate_ = new_rate;
1510    length_ = new_size * 1000 / new_rate - 1;
1511 
1512    /* update these since they depend on sample rate*/
1513    if ( clock_rate_ )
1514       factor_ = clock_rate_factor( clock_rate_);
1515 
1516    clear();
1517 
1518    return 0;
1519 }
1520 
1521 /* Sets number of source time units per second */
1522 
clock_rate_factor(long rate) const1523 uint32_t Blip_Buffer::clock_rate_factor( long rate ) const
1524 {
1525    double ratio = (double) sample_rate_ / rate;
1526    int32_t factor = (int32_t) floor( ratio * (1L << BLIP_BUFFER_ACCURACY) + 0.5 );
1527    return (uint32_t) factor;
1528 }
1529 
1530 
1531 /*============================================================
1532 	BLIP SYNTH
1533 ============================================================ */
1534 
Blip_Synth()1535 Blip_Synth::Blip_Synth()
1536 {
1537         buf          = 0;
1538         delta_factor = 0;
1539 }
1540 
save_state(blip_buffer_state_t * out)1541 void Blip_Buffer::save_state( blip_buffer_state_t* out )
1542 {
1543         out->offset_       = offset_;
1544         out->reader_accum_ = reader_accum_;
1545         memcpy( out->buf, &buffer_ [offset_ >> BLIP_BUFFER_ACCURACY], sizeof out->buf );
1546 }
1547 
load_state(blip_buffer_state_t const & in)1548 void Blip_Buffer::load_state( blip_buffer_state_t const& in )
1549 {
1550         clear();
1551 
1552         offset_       = in.offset_;
1553         reader_accum_ = in.reader_accum_;
1554         memcpy( buffer_, in.buf, sizeof(in.buf));
1555 }
1556 
1557 /*============================================================
1558 	STEREO BUFFER
1559 ============================================================ */
1560 
1561 /* Uses three buffers (one for center) and outputs stereo sample pairs. */
1562 
1563 #define STEREO_BUFFER_SAMPLES_AVAILABLE() ((long)(bufs_buffer[0].offset_ -  mixer_samples_read) << 1)
1564 #define stereo_buffer_samples_avail() ((((bufs_buffer [0].offset_ >> BLIP_BUFFER_ACCURACY) - mixer_samples_read) << 1))
1565 
1566 
stereo_buffer_set_sample_rate(long rate,int msec)1567 static const char * stereo_buffer_set_sample_rate( long rate, int msec )
1568 {
1569         mixer_samples_read = 0;
1570         for ( int i = BUFS_SIZE; --i >= 0; )
1571                 RETURN_ERR( bufs_buffer [i].set_sample_rate( rate, msec ) );
1572         return 0;
1573 }
1574 
stereo_buffer_clock_rate(long rate)1575 static void stereo_buffer_clock_rate( long rate )
1576 {
1577 	bufs_buffer[2].factor_ = bufs_buffer [2].clock_rate_factor( rate );
1578 	bufs_buffer[1].factor_ = bufs_buffer [1].clock_rate_factor( rate );
1579 	bufs_buffer[0].factor_ = bufs_buffer [0].clock_rate_factor( rate );
1580 }
1581 
stereo_buffer_clear(void)1582 static void stereo_buffer_clear (void)
1583 {
1584         mixer_samples_read = 0;
1585 	bufs_buffer [2].clear();
1586 	bufs_buffer [1].clear();
1587 	bufs_buffer [0].clear();
1588 }
1589 
1590 /* mixers use a single index value to improve performance on register-challenged processors
1591  * offset goes from negative to zero*/
1592 
stereo_buffer_mixer_read_pairs(int16_t * out,int count)1593 static INLINE void stereo_buffer_mixer_read_pairs( int16_t* out, int count )
1594 {
1595 	/* TODO: if caller never marks buffers as modified, uses mono*/
1596 	/* except that buffer isn't cleared, so caller can encounter*/
1597 	/* subtle problems and not realize the cause.*/
1598 	mixer_samples_read += count;
1599 	int16_t* outtemp = out + count * STEREO;
1600 
1601 	/* do left + center and right + center separately to reduce register load*/
1602 	Blip_Buffer* buf = &bufs_buffer [2];
1603 	{
1604 		--buf;
1605 		--outtemp;
1606 
1607 		BLIP_READER_BEGIN( side,   *buf );
1608 		BLIP_READER_BEGIN( center, bufs_buffer[2] );
1609 
1610 		BLIP_READER_ADJ_( side,   mixer_samples_read );
1611 		BLIP_READER_ADJ_( center, mixer_samples_read );
1612 
1613 		int offset = -count;
1614 		do
1615 		{
1616 			int s = (center_reader_accum + side_reader_accum) >> 14;
1617 			BLIP_READER_NEXT_IDX_( side,   offset );
1618 			BLIP_READER_NEXT_IDX_( center, offset );
1619 			BLIP_CLAMP( s, s );
1620 
1621 			++offset; /* before write since out is decremented to slightly before end*/
1622 			outtemp [offset * STEREO] = (int16_t) s;
1623 		}while ( offset );
1624 
1625 		BLIP_READER_END( side,   *buf );
1626 	}
1627 	{
1628 		--buf;
1629 		--outtemp;
1630 
1631 		BLIP_READER_BEGIN( side,   *buf );
1632 		BLIP_READER_BEGIN( center, bufs_buffer[2] );
1633 
1634 		BLIP_READER_ADJ_( side,   mixer_samples_read );
1635 		BLIP_READER_ADJ_( center, mixer_samples_read );
1636 
1637 		int offset = -count;
1638 		do
1639 		{
1640 			int s = (center_reader_accum + side_reader_accum) >> 14;
1641 			BLIP_READER_NEXT_IDX_( side,   offset );
1642 			BLIP_READER_NEXT_IDX_( center, offset );
1643 			BLIP_CLAMP( s, s );
1644 
1645 			++offset; /* before write since out is decremented to slightly before end*/
1646 			outtemp [offset * STEREO] = (int16_t) s;
1647 		}while ( offset );
1648 
1649 		BLIP_READER_END( side,   *buf );
1650 
1651 		/* only end center once*/
1652 		BLIP_READER_END( center, bufs_buffer[2] );
1653 	}
1654 }
1655 
blip_buffer_remove_all_samples(long count)1656 static void blip_buffer_remove_all_samples( long count )
1657 {
1658 	uint32_t new_offset = (uint32_t)count << BLIP_BUFFER_ACCURACY;
1659 	/* BLIP BUFFER #1 */
1660 	bufs_buffer[0].offset_ -= new_offset;
1661 	bufs_buffer[1].offset_ -= new_offset;
1662 	bufs_buffer[2].offset_ -= new_offset;
1663 
1664 	/* copy remaining samples to beginning and clear old samples*/
1665 	long remain = (bufs_buffer[0].offset_ >> BLIP_BUFFER_ACCURACY) + BLIP_BUFFER_EXTRA_;
1666 	memmove( bufs_buffer[0].buffer_, bufs_buffer[0].buffer_ + count, remain * sizeof *bufs_buffer[0].buffer_ );
1667 	memset( bufs_buffer[0].buffer_ + remain, 0, count * sizeof(*bufs_buffer[0].buffer_));
1668 
1669 	remain = (bufs_buffer[1].offset_ >> BLIP_BUFFER_ACCURACY) + BLIP_BUFFER_EXTRA_;
1670 	memmove( bufs_buffer[1].buffer_, bufs_buffer[1].buffer_ + count, remain * sizeof *bufs_buffer[1].buffer_ );
1671 	memset( bufs_buffer[1].buffer_ + remain, 0, count * sizeof(*bufs_buffer[1].buffer_));
1672 
1673 	remain = (bufs_buffer[2].offset_ >> BLIP_BUFFER_ACCURACY) + BLIP_BUFFER_EXTRA_;
1674 	memmove( bufs_buffer[2].buffer_, bufs_buffer[2].buffer_ + count, remain * sizeof *bufs_buffer[2].buffer_ );
1675 	memset( bufs_buffer[2].buffer_ + remain, 0, count * sizeof(*bufs_buffer[2].buffer_));
1676 }
1677 
stereo_buffer_read_samples(int16_t * out,long out_size)1678 static long stereo_buffer_read_samples( int16_t * out, long out_size )
1679 {
1680 	int pair_count;
1681 
1682         out_size = (STEREO_BUFFER_SAMPLES_AVAILABLE() < out_size) ? STEREO_BUFFER_SAMPLES_AVAILABLE() : out_size;
1683 
1684         pair_count = int (out_size >> 1);
1685         if ( pair_count )
1686 	{
1687 		stereo_buffer_mixer_read_pairs( out, pair_count );
1688 		blip_buffer_remove_all_samples( mixer_samples_read );
1689 		mixer_samples_read = 0;
1690 	}
1691         return out_size;
1692 }
1693 
gba_to_gb_sound_parallel(int * addr,int * addr2)1694 static void gba_to_gb_sound_parallel( int * addr, int * addr2 )
1695 {
1696 	uint32_t addr1_table = *addr - 0x60;
1697 	uint32_t addr2_table = *addr2 - 0x60;
1698 	*addr = table [addr1_table];
1699 	*addr2 = table [addr2_table];
1700 }
1701 
pcm_fifo_write_control(int data,int data2)1702 static void pcm_fifo_write_control( int data, int data2)
1703 {
1704 	pcm[0].enabled = (data & 0x0300) ? true : false;
1705 	pcm[0].timer   = (data & 0x0400) ? 1 : 0;
1706 
1707 	if ( data & 0x0800 )
1708 	{
1709 		// Reset
1710 		pcm[0].writeIndex = 0;
1711 		pcm[0].readIndex  = 0;
1712 		pcm[0].count      = 0;
1713 		pcm[0].dac        = 0;
1714 		memset(pcm[0].fifo, 0, sizeof(pcm[0].fifo));
1715 	}
1716 
1717 	gba_pcm_apply_control( 0, pcm[0].which );
1718 
1719 	if(pcm[0].pcm.output)
1720 	{
1721 		int time = SOUND_CLOCK_TICKS -  soundTicks;
1722 
1723 		pcm[0].dac = (int8_t)pcm[0].dac >> pcm[0].pcm.shift;
1724 		int delta = pcm[0].dac - pcm[0].pcm.last_amp;
1725 		if ( delta )
1726 		{
1727 			pcm[0].pcm.last_amp = pcm[0].dac;
1728 			pcm_synth.offset( time, delta, pcm[0].pcm.output );
1729 		}
1730 		pcm[0].pcm.last_time = time;
1731 	}
1732 
1733 	pcm[1].enabled = (data2 & 0x0300) ? true : false;
1734 	pcm[1].timer   = (data2 & 0x0400) ? 1 : 0;
1735 
1736 	if ( data2 & 0x0800 )
1737 	{
1738 		// Reset
1739 		pcm[1].writeIndex = 0;
1740 		pcm[1].readIndex  = 0;
1741 		pcm[1].count      = 0;
1742 		pcm[1].dac        = 0;
1743 		memset( pcm[1].fifo, 0, sizeof(pcm[1].fifo));
1744 	}
1745 
1746 	gba_pcm_apply_control( 1, pcm[1].which );
1747 
1748 	if(pcm[1].pcm.output)
1749 	{
1750 		int time = SOUND_CLOCK_TICKS -  soundTicks;
1751 
1752 		pcm[1].dac = (int8_t)pcm[1].dac >> pcm[1].pcm.shift;
1753 		int delta = pcm[1].dac - pcm[1].pcm.last_amp;
1754 		if ( delta )
1755 		{
1756 			pcm[1].pcm.last_amp = pcm[1].dac;
1757 			pcm_synth.offset( time, delta, pcm[1].pcm.output );
1758 		}
1759 		pcm[1].pcm.last_time = time;
1760 	}
1761 }
1762 
soundEvent_u16_parallel(uint32_t address[])1763 static void soundEvent_u16_parallel(uint32_t address[])
1764 {
1765 	for(int i = 0; i < 8; i++)
1766 	{
1767 		switch ( address[i] )
1768 		{
1769 			case SGCNT0_H:
1770 				//Begin of Write SGCNT0_H
1771 				WRITE16LE( &ioMem [SGCNT0_H], 0 & 0x770F );
1772 				pcm_fifo_write_control(0, 0);
1773 
1774 				gb_apu_volume( apu_vols [ioMem [SGCNT0_H] & 3] );
1775 				//End of SGCNT0_H
1776 				break;
1777 
1778 			case FIFOA_L:
1779 			case FIFOA_H:
1780 				pcm[0].fifo [pcm[0].writeIndex  ] = 0;
1781 				pcm[0].fifo [pcm[0].writeIndex+1] = 0;
1782 				pcm[0].count += 2;
1783 				pcm[0].writeIndex = (pcm[0].writeIndex + 2) & 31;
1784 				WRITE16LE( &ioMem[address[i]], 0 );
1785 				break;
1786 
1787 			case FIFOB_L:
1788 			case FIFOB_H:
1789 				pcm[1].fifo [pcm[1].writeIndex  ] = 0;
1790 				pcm[1].fifo [pcm[1].writeIndex+1] = 0;
1791 				pcm[1].count += 2;
1792 				pcm[1].writeIndex = (pcm[1].writeIndex + 2) & 31;
1793 				WRITE16LE( &ioMem[address[i]], 0 );
1794 				break;
1795 
1796 			case 0x88:
1797 				WRITE16LE( &ioMem[address[i]], 0 );
1798 				break;
1799 
1800 			default:
1801 				{
1802 					int gb_addr[2]	= {static_cast<int>(address[i] & ~1), static_cast<int>(address[i] | 1)};
1803 					uint32_t address_array[2] = {address[i] & ~ 1, address[i] | 1};
1804 					uint8_t data_array[2] = {0};
1805 					gba_to_gb_sound_parallel(&gb_addr[0], &gb_addr[1]);
1806 					soundEvent_u8_parallel(gb_addr, address_array, data_array);
1807 					break;
1808 				}
1809 		}
1810 	}
1811 }
1812 
gba_pcm_fifo_timer_overflowed(unsigned pcm_idx)1813 static void gba_pcm_fifo_timer_overflowed( unsigned pcm_idx )
1814 {
1815 	if ( pcm[pcm_idx].count <= 16 )
1816 	{
1817 		// Need to fill FIFO
1818 		CPUCheckDMA( 3, pcm[pcm_idx].which ? 4 : 2 );
1819 
1820 		if ( pcm[pcm_idx].count <= 16 )
1821 		{
1822 			// Not filled by DMA, so fill with 16 bytes of silence
1823 			int reg = pcm[pcm_idx].which ? FIFOB_L : FIFOA_L;
1824 
1825 			uint32_t address_array[8] = {static_cast<uint32_t>(reg), static_cast<uint32_t>(reg+2), static_cast<uint32_t>(reg), static_cast<uint32_t>(reg+2), static_cast<uint32_t>(reg), static_cast<uint32_t>(reg+2), static_cast<uint32_t>(reg), static_cast<uint32_t>(reg+2)};
1826 			soundEvent_u16_parallel(address_array);
1827 		}
1828 	}
1829 
1830 	// Read next sample from FIFO
1831 	pcm[pcm_idx].count--;
1832 	pcm[pcm_idx].dac = pcm[pcm_idx].fifo [pcm[pcm_idx].readIndex];
1833 	pcm[pcm_idx].readIndex = (pcm[pcm_idx].readIndex + 1) & 31;
1834 
1835 	if(pcm[pcm_idx].pcm.output)
1836 	{
1837 		int time = SOUND_CLOCK_TICKS -  soundTicks;
1838 
1839 		pcm[pcm_idx].dac = (int8_t)pcm[pcm_idx].dac >> pcm[pcm_idx].pcm.shift;
1840 		int delta = pcm[pcm_idx].dac - pcm[pcm_idx].pcm.last_amp;
1841 		if ( delta )
1842 		{
1843 			pcm[pcm_idx].pcm.last_amp = pcm[pcm_idx].dac;
1844 			pcm_synth.offset( time, delta, pcm[pcm_idx].pcm.output );
1845 		}
1846 		pcm[pcm_idx].pcm.last_time = time;
1847 	}
1848 }
1849 
soundEvent_u8_parallel(int gb_addr[],uint32_t address[],uint8_t data[])1850 void soundEvent_u8_parallel(int gb_addr[], uint32_t address[], uint8_t data[])
1851 {
1852 	for(uint32_t i = 0; i < 2; i++)
1853 	{
1854 		ioMem[address[i]] = data[i];
1855 		gb_apu_write_register( SOUND_CLOCK_TICKS -  soundTicks, gb_addr[i], data[i] );
1856 
1857 		if ( address[i] == NR52 )
1858 		{
1859 			gba_pcm_apply_control(0, 0 );
1860 			gba_pcm_apply_control(1, 1 );
1861 		}
1862 		// TODO: what about byte writes to SGCNT0_H etc.?
1863 	}
1864 }
1865 
soundEvent_u8(int gb_addr,uint32_t address,uint8_t data)1866 void soundEvent_u8(int gb_addr, uint32_t address, uint8_t data)
1867 {
1868 	ioMem[address] = data;
1869 	gb_apu_write_register( SOUND_CLOCK_TICKS -  soundTicks, gb_addr, data );
1870 
1871 	if ( address == NR52 )
1872 	{
1873 		gba_pcm_apply_control(0, 0 );
1874 		gba_pcm_apply_control(1, 1 );
1875 	}
1876 	// TODO: what about byte writes to SGCNT0_H etc.?
1877 }
1878 
1879 
soundEvent_u16(uint32_t address,uint16_t data)1880 void soundEvent_u16(uint32_t address, uint16_t data)
1881 {
1882 	switch ( address )
1883 	{
1884 		case SGCNT0_H:
1885 			//Begin of Write SGCNT0_H
1886 			WRITE16LE( &ioMem [SGCNT0_H], data & 0x770F );
1887 			pcm_fifo_write_control( data, data >> 4);
1888 
1889 			gb_apu_volume( apu_vols [ioMem [SGCNT0_H] & 3] );
1890 			//End of SGCNT0_H
1891 			break;
1892 
1893 		case FIFOA_L:
1894 		case FIFOA_H:
1895 			pcm[0].fifo [pcm[0].writeIndex  ] = data & 0xFF;
1896 			pcm[0].fifo [pcm[0].writeIndex+1] = data >> 8;
1897 			pcm[0].count += 2;
1898 			pcm[0].writeIndex = (pcm[0].writeIndex + 2) & 31;
1899 			WRITE16LE( &ioMem[address], data );
1900 			break;
1901 
1902 		case FIFOB_L:
1903 		case FIFOB_H:
1904 			pcm[1].fifo [pcm[1].writeIndex  ] = data & 0xFF;
1905 			pcm[1].fifo [pcm[1].writeIndex+1] = data >> 8;
1906 			pcm[1].count += 2;
1907 			pcm[1].writeIndex = (pcm[1].writeIndex + 2) & 31;
1908 			WRITE16LE( &ioMem[address], data );
1909 			break;
1910 
1911 		case 0x88:
1912 			data &= 0xC3FF;
1913 			WRITE16LE( &ioMem[address], data );
1914 			break;
1915 
1916 		default:
1917 			{
1918 				int gb_addr[2]	= {static_cast<int>(address & ~1), (int)(address | 1)};
1919 				uint32_t address_array[2] = {address & ~ 1, static_cast<uint32_t>(address | 1)};
1920 				uint8_t data_array[2] = {(uint8_t)data, (uint8_t)(data >> 8)};
1921 				gba_to_gb_sound_parallel(&gb_addr[0], &gb_addr[1]);
1922 				soundEvent_u8_parallel(gb_addr, address_array, data_array);
1923 				break;
1924 			}
1925 	}
1926 }
1927 
soundTimerOverflow(int timer)1928 void soundTimerOverflow(int timer)
1929 {
1930 	if ( timer == pcm[0].timer && pcm[0].enabled )
1931 		gba_pcm_fifo_timer_overflowed(0);
1932 	if ( timer == pcm[1].timer && pcm[1].enabled )
1933 		gba_pcm_fifo_timer_overflowed(1);
1934 }
1935 
process_sound_tick_fn(void)1936 void process_sound_tick_fn (void)
1937 {
1938 	// Run sound hardware to present
1939 	pcm[0].pcm.last_time -= SOUND_CLOCK_TICKS;
1940 	if ( pcm[0].pcm.last_time < -2048 )
1941 		pcm[0].pcm.last_time = -2048;
1942 
1943 	pcm[1].pcm.last_time -= SOUND_CLOCK_TICKS;
1944 	if ( pcm[1].pcm.last_time < -2048 )
1945 		pcm[1].pcm.last_time = -2048;
1946 
1947 	/* Emulates sound hardware up to a specified time, ends current time
1948 	frame, then starts a new frame at time 0 */
1949 
1950 	if(SOUND_CLOCK_TICKS > gb_apu.last_time)
1951 		gb_apu_run_until_( SOUND_CLOCK_TICKS );
1952 
1953 	gb_apu.frame_time -= SOUND_CLOCK_TICKS;
1954 	gb_apu.last_time -= SOUND_CLOCK_TICKS;
1955 
1956 	bufs_buffer[2].offset_ += SOUND_CLOCK_TICKS * bufs_buffer[2].factor_;
1957 	bufs_buffer[1].offset_ += SOUND_CLOCK_TICKS * bufs_buffer[1].factor_;
1958 	bufs_buffer[0].offset_ += SOUND_CLOCK_TICKS * bufs_buffer[0].factor_;
1959 
1960 
1961 	// dump all the samples available
1962 	// VBA will only ever store 1 frame worth of samples
1963 	int numSamples = stereo_buffer_read_samples( (int16_t*) soundFinalWave, stereo_buffer_samples_avail());
1964 	systemOnWriteDataToSoundBuffer(soundFinalWave, numSamples);
1965 }
1966 
apply_muting(void)1967 static void apply_muting (void)
1968 {
1969 	// PCM
1970 	gba_pcm_apply_control(1, 0 );
1971 	gba_pcm_apply_control(1, 1 );
1972 
1973 	// APU
1974 	gb_apu_set_output( &bufs_buffer[2], &bufs_buffer[0], &bufs_buffer[1], 0 );
1975 	gb_apu_set_output( &bufs_buffer[2], &bufs_buffer[0], &bufs_buffer[1], 1 );
1976 	gb_apu_set_output( &bufs_buffer[2], &bufs_buffer[0], &bufs_buffer[1], 2 );
1977 	gb_apu_set_output( &bufs_buffer[2], &bufs_buffer[0], &bufs_buffer[1], 3 );
1978 }
1979 
1980 
remake_stereo_buffer(void)1981 static void remake_stereo_buffer (void)
1982 {
1983 	if ( !ioMem )
1984 		return;
1985 
1986 	// Clears pointers kept to old stereo_buffer
1987 	gba_pcm_init();
1988 
1989 	// Stereo_Buffer
1990 
1991         mixer_samples_read = 0;
1992 	stereo_buffer_set_sample_rate( soundSampleRate, BLIP_DEFAULT_LENGTH );
1993 	stereo_buffer_clock_rate( CLOCK_RATE );
1994 
1995 	// PCM
1996 	pcm [0].which = 0;
1997 	pcm [1].which = 1;
1998 
1999 	// APU
2000 	gb_apu_new();
2001 	gb_apu_reset( MODE_AGB, true );
2002 
2003 	stereo_buffer_clear();
2004 
2005 	soundTicks = SOUND_CLOCK_TICKS;
2006 
2007 	apply_muting();
2008 
2009 	gb_apu_volume(apu_vols [ioMem [SGCNT0_H] & 3] );
2010 
2011 	pcm_synth.volume( 0.66 / 256 * SOUNDVOLUME_ );
2012 }
2013 
soundReset(void)2014 void soundReset (void)
2015 {
2016 	remake_stereo_buffer();
2017 	//Begin of Reset APU
2018 	gb_apu_reset( MODE_AGB, true );
2019 
2020 	stereo_buffer_clear();
2021 
2022 	soundTicks = SOUND_CLOCK_TICKS;
2023 	//End of Reset APU
2024 
2025 	SOUND_CLOCK_TICKS = SOUND_CLOCK_TICKS_;
2026 	soundTicks        = SOUND_CLOCK_TICKS_;
2027 
2028 	// Sound Event (NR52)
2029 	int gb_addr = table[NR52 - 0x60];
2030 	if ( gb_addr )
2031 	{
2032 		ioMem[NR52] = 0x80;
2033 		gb_apu_write_register( SOUND_CLOCK_TICKS -  soundTicks, gb_addr, 0x80 );
2034 
2035 		gba_pcm_apply_control(0, 0 );
2036 		gba_pcm_apply_control(1, 1 );
2037 	}
2038 
2039 	// TODO: what about byte writes to SGCNT0_H etc.?
2040 	// End of Sound Event (NR52)
2041 }
2042 
soundSetSampleRate(long sampleRate)2043 void soundSetSampleRate(long sampleRate)
2044 {
2045 	if ( soundSampleRate != sampleRate )
2046 	{
2047 		soundSampleRate      = sampleRate;
2048 		remake_stereo_buffer();
2049 	}
2050 }
2051 
2052 static int dummy_state [16];
2053 
2054 #define SKIP( type, name ) { dummy_state, sizeof (type) }
2055 
2056 #define LOAD( type, name ) { &name, sizeof (type) }
2057 
2058 static struct {
2059 	gb_apu_state_t apu;
2060 
2061 	// old state
2062 	int soundDSBValue;
2063 	uint8_t soundDSAValue;
2064 } state;
2065 
2066 // New state format
2067 static variable_desc gba_state [] =
2068 {
2069 	// PCM
2070 	LOAD( int, pcm [0].readIndex ),
2071 	LOAD( int, pcm [0].count ),
2072 	LOAD( int, pcm [0].writeIndex ),
2073 	LOAD(uint8_t[32],pcm[0].fifo ),
2074 	LOAD( int, pcm [0].dac ),
2075 
2076 	SKIP( int [4], room_for_expansion ),
2077 
2078 	LOAD( int, pcm [1].readIndex ),
2079 	LOAD( int, pcm [1].count ),
2080 	LOAD( int, pcm [1].writeIndex ),
2081 	LOAD(uint8_t[32],pcm[1].fifo ),
2082 	LOAD( int, pcm [1].dac ),
2083 
2084 	SKIP( int [4], room_for_expansion ),
2085 
2086 	// APU
2087 	LOAD( uint8_t [0x40], state.apu.regs ),      // last values written to registers and wave RAM (both banks)
2088 	LOAD( int, state.apu.frame_time ),      // clocks until next frame sequencer action
2089 	LOAD( int, state.apu.frame_phase ),     // next step frame sequencer will run
2090 
2091 	LOAD( int, state.apu.sweep_freq ),      // sweep's internal frequency register
2092 	LOAD( int, state.apu.sweep_delay ),     // clocks until next sweep action
2093 	LOAD( int, state.apu.sweep_enabled ),
2094 	LOAD( int, state.apu.sweep_neg ),       // obscure internal flag
2095 	LOAD( int, state.apu.noise_divider ),
2096 	LOAD( int, state.apu.wave_buf ),        // last read byte of wave RAM
2097 
2098 	LOAD( int [4], state.apu.delay ),       // clocks until next channel action
2099 	LOAD( int [4], state.apu.length_ctr ),
2100 	LOAD( int [4], state.apu.phase ),       // square/wave phase, noise LFSR
2101 	LOAD( int [4], state.apu.enabled ),     // internal enabled flag
2102 
2103 	LOAD( int [3], state.apu.env_delay ),   // clocks until next envelope action
2104 	LOAD( int [3], state.apu.env_volume ),
2105 	LOAD( int [3], state.apu.env_enabled ),
2106 
2107 	SKIP( int [13], room_for_expansion ),
2108 
2109 	// Emulator
2110 	LOAD( int, soundEnableFlag ),
2111 	LOAD( int, soundTicks ),
2112 
2113 	SKIP( int [14], room_for_expansion ),
2114 
2115 	{ NULL, 0 }
2116 };
2117 
soundSaveGameMem(uint8_t * & data)2118 void soundSaveGameMem(uint8_t *& data)
2119 {
2120 	gb_apu_save_state(&state.apu);
2121 	memset(dummy_state, 0, sizeof dummy_state);
2122 	utilWriteDataMem(data, gba_state);
2123 }
2124 
soundReadGameMem(const uint8_t * & in_data,int)2125 void soundReadGameMem(const uint8_t *& in_data, int)
2126 {
2127 	// Prepare APU and default state
2128 
2129 	//Begin of Reset APU
2130 	gb_apu_reset( MODE_AGB, true );
2131 
2132 	stereo_buffer_clear();
2133 
2134 	soundTicks = SOUND_CLOCK_TICKS;
2135 	//End of Reset APU
2136 
2137 	gb_apu_save_state( &state.apu );
2138 
2139 	utilReadDataMem( in_data, gba_state );
2140 
2141 	gb_apu_load_state( state.apu );
2142 	//Begin of Write SGCNT0_H
2143 	int data = (READ16LE( &ioMem [SGCNT0_H] ) & 0x770F);
2144 	WRITE16LE( &ioMem [SGCNT0_H], data & 0x770F );
2145 	pcm_fifo_write_control( data, data >> 4 );
2146 
2147 	gb_apu_volume(apu_vols [ioMem [SGCNT0_H] & 3] );
2148 	//End of SGCNT0_H
2149 }
2150 
2151