comparison Plugins/Input/console/Nes_Apu.h @ 493:c04dff121e1d trunk

[svn] hostile merge, phase 2: reimport based on new plugin code
author nenolod
date Tue, 24 Jan 2006 20:19:01 -0800
parents
children
comparison
equal deleted inserted replaced
492:ccb68bad47b2 493:c04dff121e1d
1
2 // NES 2A03 APU sound chip emulator
3
4 // Nes_Snd_Emu 0.1.7
5
6 #ifndef NES_APU_H
7 #define NES_APU_H
8
9 typedef long nes_time_t; // CPU clock cycle count
10 typedef unsigned nes_addr_t; // 16-bit memory address
11
12 #include "Nes_Oscs.h"
13
14 struct apu_snapshot_t;
15 class Nonlinear_Buffer;
16
17 class Nes_Apu {
18 public:
19 Nes_Apu();
20 ~Nes_Apu();
21
22 // Set buffer to generate all sound into, or disable sound if NULL
23 void output( Blip_Buffer* );
24
25 // Set memory reader callback used by DMC oscillator to fetch samples.
26 // When callback is invoked, 'user_data' is passed unchanged as the
27 // first parameter.
28 void dmc_reader( int (*callback)( void* user_data, nes_addr_t ), void* user_data = NULL );
29
30 // All time values are the number of CPU clock cycles relative to the
31 // beginning of the current time frame. Before resetting the CPU clock
32 // count, call end_frame( last_cpu_time ).
33
34 // Write to register (0x4000-0x4017, except 0x4014 and 0x4016)
35 enum { start_addr = 0x4000 };
36 enum { end_addr = 0x4017 };
37 void write_register( nes_time_t, nes_addr_t, int data );
38
39 // Read from status register at 0x4015
40 enum { status_addr = 0x4015 };
41 int read_status( nes_time_t );
42
43 // Run all oscillators up to specified time, end current time frame, then
44 // start a new time frame at time 0. Time frames have no effect on emulation
45 // and each can be whatever length is convenient.
46 void end_frame( nes_time_t );
47
48 // Additional optional features (can be ignored without any problem)
49
50 // Reset internal frame counter, registers, and all oscillators.
51 // Use PAL timing if pal_timing is true, otherwise use NTSC timing.
52 // Set the DMC oscillator's initial DAC value to initial_dmc_dac without
53 // any audible click.
54 void reset( bool pal_timing = false, int initial_dmc_dac = 0 );
55
56 // Save/load snapshot of exact emulation state
57 void save_snapshot( apu_snapshot_t* out ) const;
58 void load_snapshot( apu_snapshot_t const& );
59
60 // Set overall volume (default is 1.0)
61 void volume( double );
62
63 // Set treble equalization (see notes.txt)
64 void treble_eq( const blip_eq_t& );
65
66 // Set sound output of specific oscillator to buffer. If buffer is NULL,
67 // the specified oscillator is muted and emulation accuracy is reduced.
68 // The oscillators are indexed as follows: 0) Square 1, 1) Square 2,
69 // 2) Triangle, 3) Noise, 4) DMC.
70 enum { osc_count = 5 };
71 void osc_output( int index, Blip_Buffer* buffer );
72
73 // Set IRQ time callback that is invoked when the time of earliest IRQ
74 // may have changed, or NULL to disable. When callback is invoked,
75 // 'user_data' is passed unchanged as the first parameter.
76 void irq_notifier( void (*callback)( void* user_data ), void* user_data = NULL );
77
78 // Get time that APU-generated IRQ will occur if no further register reads
79 // or writes occur. If IRQ is already pending, returns irq_waiting. If no
80 // IRQ will occur, returns no_irq.
81 enum { no_irq = LONG_MAX / 2 + 1 };
82 enum { irq_waiting = 0 };
83 nes_time_t earliest_irq( nes_time_t ) const;
84
85 // Count number of DMC reads that would occur if 'run_until( t )' were executed.
86 // If last_read is not NULL, set *last_read to the earliest time that
87 // 'count_dmc_reads( time )' would result in the same result.
88 int count_dmc_reads( nes_time_t t, nes_time_t* last_read = NULL ) const;
89
90 // Time when next DMC memory read will occur
91 nes_time_t next_dmc_read_time() const;
92
93 // Run DMC until specified time, so that any DMC memory reads can be
94 // accounted for (i.e. inserting CPU wait states).
95 void run_until( nes_time_t );
96
97 // End of public interface.
98 private:
99 friend class Nes_Nonlinearizer;
100 void enable_nonlinear( double volume );
101 static double nonlinear_tnd_gain() { return 0.75; }
102 private:
103 friend struct Nes_Dmc;
104
105 // noncopyable
106 Nes_Apu( const Nes_Apu& );
107 Nes_Apu& operator = ( const Nes_Apu& );
108
109 Nes_Osc* oscs [osc_count];
110 Nes_Square square1;
111 Nes_Square square2;
112 Nes_Noise noise;
113 Nes_Triangle triangle;
114 Nes_Dmc dmc;
115
116 nes_time_t last_time; // has been run until this time in current frame
117 nes_time_t last_dmc_time;
118 nes_time_t earliest_irq_;
119 nes_time_t next_irq;
120 int frame_period;
121 int frame_delay; // cycles until frame counter runs next
122 int frame; // current frame (0-3)
123 int osc_enables;
124 int frame_mode;
125 bool irq_flag;
126 void (*irq_notifier_)( void* user_data );
127 void* irq_data;
128 Nes_Square::Synth square_synth; // shared by squares
129
130 void irq_changed();
131 void state_restored();
132 void run_until_( nes_time_t );
133 };
134
135 inline void Nes_Apu::osc_output( int osc, Blip_Buffer* buf )
136 {
137 assert( (unsigned) osc < osc_count );
138 oscs [osc]->output = buf;
139 }
140
141 inline nes_time_t Nes_Apu::earliest_irq( nes_time_t ) const
142 {
143 return earliest_irq_;
144 }
145
146 inline void Nes_Apu::dmc_reader( int (*func)( void*, nes_addr_t ), void* user_data )
147 {
148 dmc.rom_reader_data = user_data;
149 dmc.rom_reader = func;
150 }
151
152 inline void Nes_Apu::irq_notifier( void (*func)( void* user_data ), void* user_data )
153 {
154 irq_notifier_ = func;
155 irq_data = user_data;
156 }
157
158 inline int Nes_Apu::count_dmc_reads( nes_time_t time, nes_time_t* last_read ) const
159 {
160 return dmc.count_reads( time, last_read );
161 }
162
163 inline nes_time_t Nes_Dmc::next_read_time() const
164 {
165 if ( length_counter == 0 )
166 return Nes_Apu::no_irq; // not reading
167
168 return apu->last_dmc_time + delay + long (bits_remain - 1) * period;
169 }
170
171 inline nes_time_t Nes_Apu::next_dmc_read_time() const { return dmc.next_read_time(); }
172
173 #endif
174