1 #ifndef SOUND_H
2 #define SOUND_H
3 
4 #include "sound_blargg.h"
5 
6 #define SGCNT0_H 0x82
7 #define FIFOA_L 0xa0
8 #define FIFOA_H 0xa2
9 #define FIFOB_L 0xa4
10 #define FIFOB_H 0xa6
11 
12 #define BLIP_BUFFER_ACCURACY 16
13 #define BLIP_PHASE_BITS 8
14 #define BLIP_WIDEST_IMPULSE_ 16
15 #define BLIP_BUFFER_EXTRA_ 18
16 #define BLIP_RES 256
17 #define BLIP_RES_MIN_ONE 255
18 #define BLIP_SAMPLE_BITS 30
19 #define BLIP_READER_DEFAULT_BASS 9
20 #define BLIP_DEFAULT_LENGTH 250		/* 1/4th of a second */
21 
22 #define BUFS_SIZE 3
23 #define STEREO 2
24 
25 #define	CLK_MUL	GB_APU_OVERCLOCK
26 #define CLK_MUL_MUL_2 8
27 #define CLK_MUL_MUL_4 16
28 #define CLK_MUL_MUL_6 24
29 #define CLK_MUL_MUL_8 32
30 #define CLK_MUL_MUL_15 60
31 #define CLK_MUL_MUL_32 128
32 #define DAC_BIAS 7
33 
34 #define PERIOD_MASK 0x70
35 #define SHIFT_MASK 0x07
36 
37 #define PERIOD2_MASK 0x1FFFF
38 
39 #define BANK40_MASK 0x40
40 #define BANK_SIZE 32
41 #define BANK_SIZE_MIN_ONE 31
42 #define BANK_SIZE_DIV_TWO 16
43 
44 /* 11-bit frequency in NRx3 and NRx4*/
45 #define GB_OSC_FREQUENCY() (((regs[4] & 7) << 8) + regs[3])
46 
47 #define	WAVE_TYPE	0x100
48 #define NOISE_TYPE	0x200
49 #define MIXED_TYPE	WAVE_TYPE | NOISE_TYPE
50 #define TYPE_INDEX_MASK	0xFF
51 
52 struct blip_buffer_state_t
53 {
54         uint32_t offset_;
55         int32_t reader_accum_;
56         int32_t buf [BLIP_BUFFER_EXTRA_];
57 };
58 
59 
60 /* Begins reading from buffer. Name should be unique to the current block.*/
61 #define BLIP_READER_BEGIN( name, blip_buffer ) \
62         const int32_t * name##_reader_buf = (blip_buffer).buffer_;\
63         int32_t name##_reader_accum = (blip_buffer).reader_accum_
64 
65 /* Advances to next sample*/
66 #define BLIP_READER_NEXT( name, bass ) \
67         (void) (name##_reader_accum += *name##_reader_buf++ - (name##_reader_accum >> (bass)))
68 
69 /* Ends reading samples from buffer. The number of samples read must now be removed
70    using Blip_Buffer::remove_samples(). */
71 #define BLIP_READER_END( name, blip_buffer ) \
72         (void) ((blip_buffer).reader_accum_ = name##_reader_accum)
73 
74 #define BLIP_READER_ADJ_( name, offset ) (name##_reader_buf += offset)
75 
76 #define BLIP_READER_NEXT_IDX_( name, idx ) {\
77         name##_reader_accum -= name##_reader_accum >> BLIP_READER_DEFAULT_BASS;\
78         name##_reader_accum += name##_reader_buf [(idx)];\
79 }
80 
81 #define BLIP_READER_NEXT_RAW_IDX_( name, idx ) {\
82         name##_reader_accum -= name##_reader_accum >> BLIP_READER_DEFAULT_BASS; \
83         name##_reader_accum += *(int32_t const*) ((char const*) name##_reader_buf + (idx)); \
84 }
85 
86 #if defined (_M_IX86) || defined (_M_IA64) || defined (__i486__) || \
87                 defined (__x86_64__) || defined (__ia64__) || defined (__i386__)
88         #define BLIP_CLAMP_( in ) in < -0x8000 || 0x7FFF < in
89 #else
90         #define BLIP_CLAMP_( in ) (int16_t) in != in
91 #endif
92 
93 /* Clamp sample to int16_t range */
94 #define BLIP_CLAMP( sample, out ) { if ( BLIP_CLAMP_( (sample) ) ) (out) = ((sample) >> 24) ^ 0x7FFF; }
95 #define GB_ENV_DAC_ENABLED() (regs[2] & 0xF8)	/* Non-zero if DAC is enabled*/
96 #define GB_NOISE_PERIOD2_INDEX()	(regs[3] >> 4)
97 #define GB_NOISE_PERIOD2(base)		(base << GB_NOISE_PERIOD2_INDEX())
98 #define GB_NOISE_LFSR_MASK()		((regs[3] & 0x08) ? ~0x4040 : ~0x4000)
99 #define GB_WAVE_DAC_ENABLED() (regs[0] & 0x80)	/* Non-zero if DAC is enabled*/
100 
101 #define reload_sweep_timer() \
102         sweep_delay = (regs [0] & PERIOD_MASK) >> 4; \
103         if ( !sweep_delay ) \
104                 sweep_delay = 8;
105 
106 
107 
108 void soundSetVolume( float );
109 
110 // Manages muting bitmask. The bits control the following channels:
111 // 0x001 Pulse 1
112 // 0x002 Pulse 2
113 // 0x004 Wave
114 // 0x008 Noise
115 // 0x100 PCM 1
116 // 0x200 PCM 2
117 void soundPause (void);
118 void soundResume (void);
119 void soundSetSampleRate(long sampleRate);
120 void soundReset (void);
121 void soundEvent_u8( int gb_addr, uint32_t addr, uint8_t  data );
122 void soundEvent_u8_parallel(int gb_addr[], uint32_t address[], uint8_t data[]);
123 void soundEvent_u16( uint32_t addr, uint16_t data );
124 void soundTimerOverflow( int which );
125 void process_sound_tick_fn (void);
126 void soundSaveGameMem(uint8_t *& data);
127 void soundReadGameMem(const uint8_t *& data, int version);
128 
129 extern int SOUND_CLOCK_TICKS;   // Number of 16.8 MHz clocks between calls to soundTick()
130 extern int soundTicks;          // Number of 16.8 MHz clocks until soundTick() will be called
131 
132 #endif // SOUND_H
133