1 // NES 2A03 APU sound chip emulator
2
3 // Nes_Snd_Emu $vers
4 #ifndef NES_APU_H
5 #define NES_APU_H
6
7 #include "blargg_common.h"
8 #include "Nes_Oscs.h"
9
10 struct apu_state_t;
11 class Nes_Buffer;
12
13 class Nes_Apu {
14 public:
15 // Basics
16
17 typedef int nes_time_t; // NES CPU clock cycle count
18
19 // Sets memory reader callback used by DMC oscillator to fetch samples.
20 // When callback is invoked, 'user_data' is passed unchanged as the
21 // first parameter.
22 //void dmc_reader( int (*callback)( void* user_data, int addr ), void* user_data = NULL );
23
24 // Sets buffer to generate sound into, or 0 to mute output (reduces
25 // emulation accuracy).
26 void set_output( Blip_Buffer* );
27
28 // All time values are the number of CPU clock cycles relative to the
29 // beginning of the current time frame. Before resetting the CPU clock
30 // count, call end_frame( last_cpu_time ).
31
32 // Writes to register (0x4000-0x4013, and 0x4015 and 0x4017)
33 enum { io_addr = 0x4000 };
34 enum { io_size = 0x18 };
35 void write_register( nes_time_t, int addr, int data );
36
37 // Reads from status register (0x4015)
38 enum { status_addr = 0x4015 };
39 int read_status( nes_time_t );
40
41 // Runs all oscillators up to specified time, ends current time frame, then
42 // starts a new time frame at time 0. Time frames have no effect on emulation
43 // and each can be whatever length is convenient.
44 void end_frame( nes_time_t );
45
46 // Optional
47
48 // Resets internal frame counter, registers, and all oscillators.
49 // Uses PAL timing if pal_timing is true, otherwise use NTSC timing.
50 // Sets the DMC oscillator's initial DAC value to initial_dmc_dac without
51 // any audible click.
52 void reset( bool pal_mode = false, int initial_dmc_dac = 0 );
53
54 // Same as set_output(), but for a particular channel
55 // 0: Square 1, 1: Square 2, 2: Triangle, 3: Noise, 4: DMC
56 enum { osc_count = 5 };
57 void set_output( int chan, Blip_Buffer* buf );
58
59 // Adjusts frame period
60 void set_tempo( double );
61
62 // Saves/loads exact emulation state
63 void save_state( apu_state_t* out ) const;
64 void load_state( apu_state_t const& );
65
66 // Sets overall volume (default is 1.0)
67 void volume( double );
68
69 // Sets treble equalization (see notes.txt)
70 void treble_eq( const blip_eq_t& );
71
72 // Sets IRQ time callback that is invoked when the time of earliest IRQ
73 // may have changed, or NULL to disable. When callback is invoked,
74 // 'user_data' is passed unchanged as the first parameter.
75 //void irq_notifier( void (*callback)( void* user_data ), void* user_data = NULL );
76
77 // Gets time that APU-generated IRQ will occur if no further register reads
78 // or writes occur. If IRQ is already pending, returns irq_waiting. If no
79 // IRQ will occur, returns no_irq.
80 enum { no_irq = INT_MAX/2 + 1 };
81 enum { irq_waiting = 0 };
82 nes_time_t earliest_irq( nes_time_t ) const;
83
84 // Counts number of DMC reads that would occur if 'run_until( t )' were executed.
85 // If last_read is not NULL, set *last_read to the earliest time that
86 // 'count_dmc_reads( time )' would result in the same result.
87 int count_dmc_reads( nes_time_t t, nes_time_t* last_read = NULL ) const;
88
89 // Time when next DMC memory read will occur
90 nes_time_t next_dmc_read_time() const;
91
92 // Runs DMC until specified time, so that any DMC memory reads can be
93 // accounted for (i.e. inserting CPU wait states).
94 void run_until( nes_time_t );
95
96
97 // Implementation
98 public:
99 Nes_Apu();
100 BLARGG_DISABLE_NOTHROW
101 // Use set_output() in place of these
102 BLARGG_DEPRECATED( void output ( Blip_Buffer* c ); )
103 BLARGG_DEPRECATED( void osc_output( int i, Blip_Buffer* c ); )
104
105 BLARGG_DEPRECATED_TEXT( enum { start_addr = 0x4000 }; )
106 BLARGG_DEPRECATED_TEXT( enum { end_addr = 0x4017 }; )
107
108 blargg_callback<int (*)( void* user_data, int addr )> dmc_reader;
109 blargg_callback<void (*)( void* user_data )> irq_notifier;
110
111 void enable_nonlinear_( double sq, double tnd );
tnd_total_()112 static float tnd_total_() { return 196.015f; }
113
114 void enable_w4011_( bool enable = true ) { enable_w4011 = enable; }
115
116 private:
117 friend struct Nes_Dmc;
118
119 // noncopyable
120 Nes_Apu( const Nes_Apu& );
121 Nes_Apu& operator = ( const Nes_Apu& );
122
123 Nes_Osc* oscs [osc_count];
124 Nes_Square square1;
125 Nes_Square square2;
126 Nes_Noise noise;
127 Nes_Triangle triangle;
128 Nes_Dmc dmc;
129
130 double tempo_;
131 nes_time_t last_time; // has been run until this time in current frame
132 nes_time_t last_dmc_time;
133 nes_time_t earliest_irq_;
134 nes_time_t next_irq;
135 int frame_period;
136 int frame_delay; // cycles until frame counter runs next
137 int frame; // current frame (0-3)
138 int osc_enables;
139 int frame_mode;
140 bool irq_flag;
141 bool enable_w4011;
142 Nes_Square::Synth square_synth; // shared by squares
143
144 void irq_changed();
145 void state_restored();
146 void run_until_( nes_time_t );
147
148 // TODO: remove
149 friend class Nes_Core;
150 };
151
set_output(int osc,Blip_Buffer * buf)152 inline void Nes_Apu::set_output( int osc, Blip_Buffer* buf )
153 {
154 assert( (unsigned) osc < osc_count );
155 oscs [osc]->output = buf;
156 }
157
earliest_irq(nes_time_t)158 inline Nes_Apu::nes_time_t Nes_Apu::earliest_irq( nes_time_t ) const
159 {
160 return earliest_irq_;
161 }
162
count_dmc_reads(nes_time_t time,nes_time_t * last_read)163 inline int Nes_Apu::count_dmc_reads( nes_time_t time, nes_time_t* last_read ) const
164 {
165 return dmc.count_reads( time, last_read );
166 }
167
next_read_time()168 inline Nes_Apu::nes_time_t Nes_Dmc::next_read_time() const
169 {
170 if ( length_counter == 0 )
171 return Nes_Apu::no_irq; // not reading
172
173 return apu->last_dmc_time + delay + (bits_remain - 1) * period;
174 }
175
next_dmc_read_time()176 inline Nes_Apu::nes_time_t Nes_Apu::next_dmc_read_time() const { return dmc.next_read_time(); }
177
178 BLARGG_DEPRECATED( typedef int nes_time_t; ) // use your own typedef
179 BLARGG_DEPRECATED( typedef unsigned nes_addr_t; ) // use your own typedef
180
181 BLARGG_DEPRECATED_TEXT( inline void Nes_Apu::output ( Blip_Buffer* c ) { set_output( c ); } )
182 BLARGG_DEPRECATED_TEXT( inline void Nes_Apu::osc_output( int i, Blip_Buffer* c ) { set_output( i, c ); } )
183
184 #endif
185