summaryrefslogtreecommitdiff
path: root/plugins/gme/game-music-emu-0.6pre/gme/Sms_Apu.h
blob: 1da4da3077a711a8b5ccae30cbc656e9fc2ff355 (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
// Sega Master System SN76489 PSG sound chip emulator

// Sms_Snd_Emu 0.1.2
#ifndef SMS_APU_H
#define SMS_APU_H

#include "blargg_common.h"
#include "Blip_Buffer.h"

struct sms_apu_state_t;

class Sms_Apu {
public:
// Basics

	// Sets buffer(s) to generate sound into, or 0 to mute. If only center is not 0,
	// output is mono.
	void set_output( Blip_Buffer* center, Blip_Buffer* left = NULL, Blip_Buffer* right = NULL );

	// Emulates to time t, then writes data to Game Gear left/right assignment byte
	void write_ggstereo( blip_time_t t, int data );
	
	// Emulates to time t, then writes data
	void write_data( blip_time_t t, int data );
	
	// Emulates to time t, then subtracts t from the current time.
	// OK if previous write call had time slightly after t.
	void end_frame( blip_time_t t );

// More features

	// Resets sound chip and sets noise feedback bits and width
	void reset( unsigned noise_feedback = 0, int noise_width = 0 );
	
	// Same as set_output(), but for a particular channel
	// 0: Square 1, 1: Square 2, 2: Square 3, 3: Noise
	enum { osc_count = 4 }; // 0 <= chan < osc_count
	void set_output( int chan, Blip_Buffer* center, Blip_Buffer* left = NULL, Blip_Buffer* right = NULL );
	
	// Sets overall volume, where 1.0 is normal
	void volume( double );
	
	// Sets treble equalization
	void treble_eq( blip_eq_t const& );
	
	// Saves full emulation state to state_out. Data format is portable and
	// includes some extra space to avoid expansion in case more state needs
	// to be stored in the future.
	void save_state( sms_apu_state_t* state_out );
	
	// Loads state. You should call reset() BEFORE this.
	blargg_err_t load_state( sms_apu_state_t const& in );

private:
	// noncopyable
	Sms_Apu( const Sms_Apu& );
	Sms_Apu& operator = ( const Sms_Apu& );

// Implementation
public:
	Sms_Apu();
	~Sms_Apu() { }
	BLARGG_DISABLE_NOTHROW
	
	// Use set_output() instead
	BLARGG_DEPRECATED( void output    (        Blip_Buffer* c                                 ) { set_output( c, c, c    ); } )
	BLARGG_DEPRECATED( void output    (        Blip_Buffer* c, Blip_Buffer* l, Blip_Buffer* r ) { set_output( c, l, r    ); } )
	BLARGG_DEPRECATED( void osc_output( int i, Blip_Buffer* c                                 ) { set_output( i, c, c, c ); } )
	BLARGG_DEPRECATED( void osc_output( int i, Blip_Buffer* c, Blip_Buffer* l, Blip_Buffer* r ) { set_output( i, c, l, r ); } )

private:
	struct Osc
	{
		Blip_Buffer* outputs [4]; // NULL, right, left, center
		Blip_Buffer* output;
		int          last_amp;
		
		int         volume;
		int         period;
		int         delay;
		unsigned    phase;
	};
	
	Osc     oscs [osc_count];
	int     ggstereo;
	int     latch;
	
	blip_time_t last_time;
	int         min_tone_period;
	unsigned    noise_feedback;
	unsigned    looped_feedback;
	Blip_Synth_Fast fast_synth;
	Blip_Synth_Norm norm_synth;
	
	int calc_output( int i ) const;
	void run_until( blip_time_t );
	const char* save_load( sms_apu_state_t*, bool save );
	friend class Sms_Apu_Tester;
};

struct sms_apu_state_t
{
	// If SMS_APU_CUSTOM_STATE is 1, values are stored as normal integers,
	// so your code can then save and load them however it likes. Otherwise,
	// they are 4-byte arrays in little-endian format, making entire
	// structure suitable for direct storage on disk.
	
#if SMS_APU_CUSTOM_STATE
	typedef int val_t;
#else
	typedef unsigned char val_t [4];
#endif
	
	enum { format0 = 0x50414D53 };
	
	val_t format;
	val_t version;
	val_t latch;
	val_t ggstereo;
	val_t periods [4];
	val_t volumes [4];
	val_t delays  [4];
	val_t phases  [4];
	
	val_t unused  [12]; // for future expansion
};

#endif