| 1 |
#ifndef _NES_DMC_H_ |
| 2 |
#define _NES_DMC_H_ |
| 3 |
#include <functional> |
| 4 |
|
| 5 |
namespace xgm |
| 6 |
{ |
| 7 |
class NES_APU; // forward declaration |
| 8 |
|
| 9 |
/** Bottom Half of APU **/ |
| 10 |
class NES_DMC |
| 11 |
{ |
| 12 |
public: |
| 13 |
enum |
| 14 |
{ |
| 15 |
OPT_ENABLE_4011=0, |
| 16 |
OPT_ENABLE_PNOISE, |
| 17 |
OPT_UNMUTE_ON_RESET, |
| 18 |
OPT_DPCM_ANTI_CLICK, |
| 19 |
OPT_NONLINEAR_MIXER, |
| 20 |
OPT_RANDOMIZE_NOISE, |
| 21 |
OPT_TRI_MUTE, |
| 22 |
OPT_RANDOMIZE_TRI, |
| 23 |
OPT_DPCM_REVERSE, |
| 24 |
OPT_END |
| 25 |
}; |
| 26 |
protected: |
| 27 |
const int GETA_BITS; |
| 28 |
static const unsigned int freq_table[2][16]; |
| 29 |
static const unsigned int wavlen_table[2][16]; |
| 30 |
unsigned int tnd_table[2][16][16][128]; |
| 31 |
|
| 32 |
int option[OPT_END]; |
| 33 |
int mask; |
| 34 |
int sm[2][3]; |
| 35 |
unsigned int reg[0x10]; |
| 36 |
unsigned int len_reg; |
| 37 |
unsigned int adr_reg; |
| 38 |
std::function<void(unsigned short, unsigned int&)> memory; |
| 39 |
unsigned int daddress; |
| 40 |
unsigned int dlength; |
| 41 |
unsigned int data; |
| 42 |
bool empty; |
| 43 |
short damp; |
| 44 |
int dac_lsb; |
| 45 |
bool dmc_pop; |
| 46 |
int dmc_pop_offset; |
| 47 |
int dmc_pop_follow; |
| 48 |
double clock; |
| 49 |
unsigned int rate; |
| 50 |
int pal; |
| 51 |
int mode; |
| 52 |
bool irq; |
| 53 |
|
| 54 |
int counter[3]; // frequency dividers |
| 55 |
int tphase; // triangle phase |
| 56 |
unsigned int nfreq; // noise frequency |
| 57 |
unsigned int dfreq; // DPCM frequency |
| 58 |
|
| 59 |
unsigned int tri_freq; |
| 60 |
int linear_counter; |
| 61 |
int linear_counter_reload; |
| 62 |
bool linear_counter_halt; |
| 63 |
bool linear_counter_control; |
| 64 |
|
| 65 |
int noise_volume; |
| 66 |
unsigned int noise, noise_tap; |
| 67 |
|
| 68 |
// noise envelope |
| 69 |
bool envelope_loop; |
| 70 |
bool envelope_disable; |
| 71 |
bool envelope_write; |
| 72 |
int envelope_div_period; |
| 73 |
int envelope_div; |
| 74 |
int envelope_counter; |
| 75 |
|
| 76 |
bool enable[2]; // tri/noise enable |
| 77 |
int length_counter[2]; // 0=tri, 1=noise |
| 78 |
|
| 79 |
// frame sequencer |
| 80 |
NES_APU* apu; // apu is clocked by DMC's frame sequencer |
| 81 |
int frame_sequence_count; // current cycle count |
| 82 |
int frame_sequence_length; // CPU cycles per FrameSequence |
| 83 |
int frame_sequence_step; // current step of frame sequence |
| 84 |
int frame_sequence_steps; // 4/5 steps per frame |
| 85 |
bool frame_irq; |
| 86 |
bool frame_irq_enable; |
| 87 |
|
| 88 |
inline unsigned int calc_tri (unsigned int clocks); |
| 89 |
inline unsigned int calc_dmc (unsigned int clocks); |
| 90 |
inline unsigned int calc_noise (unsigned int clocks); |
| 91 |
|
| 92 |
public: |
| 93 |
unsigned int out[3]; |
| 94 |
NES_DMC (); |
| 95 |
~NES_DMC (); |
| 96 |
|
| 97 |
void InitializeTNDTable(double wt, double wn, double wd); |
| 98 |
void SetPal (bool is_pal); |
| 99 |
void SetAPU (NES_APU* apu_); |
| 100 |
void SetMemory (std::function<void(unsigned short, unsigned int&)> r); |
| 101 |
void FrameSequence(int s); |
| 102 |
int GetDamp(){ return (damp<<1)|dac_lsb ; } |
| 103 |
void TickFrameSequence (unsigned int clocks); |
| 104 |
|
| 105 |
void Reset (); |
| 106 |
void Tick (unsigned int clocks); |
| 107 |
unsigned int Render (int b[2]); |
| 108 |
bool Write (unsigned int adr, unsigned int val, unsigned int id=0); |
| 109 |
bool Read (unsigned int adr, unsigned int & val, unsigned int id=0); |
| 110 |
void SetRate (double rate); |
| 111 |
void SetClock (double rate); |
| 112 |
void SetOption (int, int); |
| 113 |
void SetMask(int m){ mask = m; } |
| 114 |
void SetStereoMix (int trk, short mixl, short mixr); |
| 115 |
}; |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
#endif |