LMMS
Loading...
Searching...
No Matches
Nes_Apu.h
Go to the documentation of this file.
1// NES 2A03 APU sound chip emulator
2
3// Nes_Snd_Emu 0.1.8
4#ifndef NES_APU_H
5#define NES_APU_H
6
7#include "blargg_common.h"
8
9typedef blargg_long nes_time_t; // CPU clock cycle count
10typedef unsigned nes_addr_t; // 16-bit memory address
11
12#include "Nes_Oscs.h"
13
14struct apu_state_t;
15class Nes_Buffer;
16
17class Nes_Apu {
18public:
19 // Set buffer to generate all sound into, or disable sound if NULL
20 void output( Blip_Buffer* );
21
22 // Set memory reader callback used by DMC oscillator to fetch samples.
23 // When callback is invoked, 'user_data' is passed unchanged as the
24 // first parameter.
25 void dmc_reader( int (*callback)( void* user_data, nes_addr_t ), void* user_data = NULL );
26
27 // All time values are the number of CPU clock cycles relative to the
28 // beginning of the current time frame. Before resetting the CPU clock
29 // count, call end_frame( last_cpu_time ).
30
31 // Write to register (0x4000-0x4017, except 0x4014 and 0x4016)
32 enum { start_addr = 0x4000 };
33 enum { end_addr = 0x4017 };
35
36 // Read from status register at 0x4015
37 enum { status_addr = 0x4015 };
39
40 // Run all oscillators up to specified time, end current time frame, then
41 // start a new time frame at time 0. Time frames have no effect on emulation
42 // and each can be whatever length is convenient.
43 void end_frame( nes_time_t );
44
45// Additional optional features (can be ignored without any problem)
46
47 // Reset internal frame counter, registers, and all oscillators.
48 // Use PAL timing if pal_timing is true, otherwise use NTSC timing.
49 // Set the DMC oscillator's initial DAC value to initial_dmc_dac without
50 // any audible click.
51 void reset( bool pal_mode = false, int initial_dmc_dac = 0 );
52
53 // Adjust frame period
54 void set_tempo( double );
55
56 // Save/load exact emulation state
57 void save_state( apu_state_t* out ) const;
58 void load_state( apu_state_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 = INT_MAX / 2 + 1 };
82 enum { irq_waiting = 0 };
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
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
97public:
98 Nes_Apu();
99 BLARGG_DISABLE_NOTHROW
100private:
101 friend class Nes_Nonlinearizer;
102 void enable_nonlinear( double volume );
103 static double nonlinear_tnd_gain() { return 0.75; }
104private:
105 friend struct Nes_Dmc;
106
107 // noncopyable
108 Nes_Apu( const Nes_Apu& );
110
117
118 double tempo_;
119 nes_time_t last_time; // has been run until this time in current frame
124 int frame_delay; // cycles until frame counter runs next
125 int frame; // current frame (0-3)
129 void (*irq_notifier_)( void* user_data );
130 void* irq_data;
131 Nes_Square::Synth square_synth; // shared by squares
132
133 void irq_changed();
135 void run_until_( nes_time_t );
136
137 // TODO: remove
138 friend class Nes_Core;
139};
140
141inline void Nes_Apu::osc_output( int osc, Blip_Buffer* buf )
142{
143 assert( (unsigned) osc < osc_count );
144 oscs [osc]->output = buf;
145}
146
148{
149 return earliest_irq_;
150}
151
152inline void Nes_Apu::dmc_reader( int (*func)( void*, nes_addr_t ), void* user_data )
153{
154 dmc.prg_reader_data = user_data;
155 dmc.prg_reader = func;
156}
157
158inline void Nes_Apu::irq_notifier( void (*func)( void* user_data ), void* user_data )
159{
160 irq_notifier_ = func;
161 irq_data = user_data;
162}
163
164inline int Nes_Apu::count_dmc_reads( nes_time_t time, nes_time_t* last_read ) const
165{
166 return dmc.count_reads( time, last_read );
167}
168
170{
171 if ( length_counter == 0 )
172 return Nes_Apu::no_irq; // not reading
173
174 return apu->last_dmc_time + delay + long (bits_remain - 1) * period;
175}
176
177inline nes_time_t Nes_Apu::next_dmc_read_time() const { return dmc.next_read_time(); }
178
179#endif
#define NULL
Definition CarlaBridgeFormat.cpp:30
blargg_long nes_time_t
Definition Nes_Apu.h:9
unsigned nes_addr_t
Definition Nes_Apu.h:10
assert(0)
Definition Blip_Buffer.h:23
void enable_nonlinear(double volume)
Definition Nes_Apu.cpp:48
friend class Nes_Core
Definition Nes_Apu.h:138
Nes_Apu(const Nes_Apu &)
Nes_Apu & operator=(const Nes_Apu &)
@ status_addr
Definition Nes_Apu.h:37
nes_time_t next_irq
Definition Nes_Apu.h:122
friend struct Nes_Dmc
Definition Nes_Apu.h:105
nes_time_t earliest_irq_
Definition Nes_Apu.h:121
Nes_Square::Synth square_synth
Definition Nes_Apu.h:131
void set_tempo(double)
Definition Nes_Apu.cpp:80
Nes_Dmc dmc
Definition Nes_Apu.h:116
@ osc_count
Definition Nes_Apu.h:70
void save_state(apu_state_t *out) const
int frame
Definition Nes_Apu.h:125
static double nonlinear_tnd_gain()
Definition Nes_Apu.h:103
void osc_output(int index, Blip_Buffer *buffer)
Definition Nes_Apu.h:141
void end_frame(nes_time_t)
Definition Nes_Apu.cpp:238
void run_until_(nes_time_t)
Definition Nes_Apu.cpp:148
int count_dmc_reads(nes_time_t t, nes_time_t *last_read=NULL) const
Definition Nes_Apu.h:164
nes_time_t last_dmc_time
Definition Nes_Apu.h:120
void treble_eq(const blip_eq_t &)
Definition Nes_Apu.cpp:40
bool irq_flag
Definition Nes_Apu.h:128
int frame_delay
Definition Nes_Apu.h:124
nes_time_t earliest_irq(nes_time_t) const
Definition Nes_Apu.h:147
nes_time_t next_dmc_read_time() const
Definition Nes_Apu.h:177
@ no_irq
Definition Nes_Apu.h:81
int osc_enables
Definition Nes_Apu.h:126
Nes_Apu()
Definition Nes_Apu.cpp:20
int frame_mode
Definition Nes_Apu.h:127
Nes_Osc * oscs[osc_count]
Definition Nes_Apu.h:111
Nes_Square square2
Definition Nes_Apu.h:113
void irq_changed()
Definition Nes_Apu.cpp:118
void * irq_data
Definition Nes_Apu.h:130
void(* irq_notifier_)(void *user_data)
Definition Nes_Apu.h:129
@ irq_waiting
Definition Nes_Apu.h:82
Nes_Triangle triangle
Definition Nes_Apu.h:115
void run_until(nes_time_t)
Definition Nes_Apu.cpp:137
@ start_addr
Definition Nes_Apu.h:32
int read_status(nes_time_t)
Definition Nes_Apu.cpp:369
void write_register(nes_time_t, nes_addr_t, int data)
Definition Nes_Apu.cpp:283
Nes_Square square1
Definition Nes_Apu.h:112
void dmc_reader(int(*callback)(void *user_data, nes_addr_t), void *user_data=NULL)
Definition Nes_Apu.h:152
void load_state(apu_state_t const &)
void irq_notifier(void(*callback)(void *user_data), void *user_data=NULL)
Definition Nes_Apu.h:158
void volume(double)
Definition Nes_Apu.cpp:65
void state_restored()
@ end_addr
Definition Nes_Apu.h:33
void output(Blip_Buffer *)
Definition Nes_Apu.cpp:74
Nes_Noise noise
Definition Nes_Apu.h:114
friend class Nes_Nonlinearizer
Definition Nes_Apu.h:101
double tempo_
Definition Nes_Apu.h:118
int frame_period
Definition Nes_Apu.h:123
void reset(bool pal_mode=false, int initial_dmc_dac=0)
Definition Nes_Apu.cpp:88
nes_time_t last_time
Definition Nes_Apu.h:119
Definition Blip_Buffer.h:239
struct huft * t
Definition inflate.c:943
JSAMPIMAGE data
Definition jpeglib.h:945
float out
Definition lilv_test.c:1461
Nes_Apu * apu
Definition Nes_Oscs.h:132
int bits_remain
Definition Nes_Oscs.h:114
nes_time_t next_read_time() const
Definition Nes_Apu.h:169
int period
Definition Nes_Oscs.h:111
Definition Nes_Oscs.h:96
Definition Nes_Oscs.h:13
int delay
Definition Nes_Oscs.h:18
int length_counter
Definition Nes_Oscs.h:17
Definition Nes_Oscs.h:52
Blip_Synth< blip_good_quality, 1 > Synth
Definition Nes_Oscs.h:59
Definition Nes_Oscs.h:76
RECT const char void(* callback)(const char *droppath))) SWELL_API_DEFINE(BOOL
Definition swell-functions.h:1004
#define void
Definition unzip.h:396