1 // Private oscillators used by Nes_Apu
2 
3 // Nes_Snd_Emu 0.1.8
4 #ifndef NES_OSCS_H
5 #define NES_OSCS_H
6 
7 #include "blargg_common.h"
8 #include "Blip_Buffer.h"
9 
10 class Nes_Apu;
11 
12 struct Nes_Osc
13 {
14 	unsigned char regs [4];
15 	bool reg_written [4];
16 	Blip_Buffer* output;
17 	int length_counter;// length counter (0 if unused by oscillator)
18 	int delay;      // delay until next (potential) transition
19 	int last_amp;   // last amplitude oscillator was outputting
20 
21 	void clock_length( int halt_mask );
periodNes_Osc22 	int period() const {
23 		return (regs [3] & 7) * 0x100 + (regs [2] & 0xFF);
24 	}
resetNes_Osc25 	void reset() {
26 		delay = 0;
27 		last_amp = 0;
28 	}
update_ampNes_Osc29 	int update_amp( int amp ) {
30 		int delta = amp - last_amp;
31 		last_amp = amp;
32 		return delta;
33 	}
34 };
35 
36 struct Nes_Envelope : Nes_Osc
37 {
38 	int envelope;
39 	int env_delay;
40 
41 	void clock_envelope();
42 	int volume() const;
resetNes_Envelope43 	void reset() {
44 		envelope = 0;
45 		env_delay = 0;
46 		Nes_Osc::reset();
47 	}
48 };
49 
50 // Nes_Square
51 struct Nes_Square : Nes_Envelope
52 {
53 	enum { negate_flag = 0x08 };
54 	enum { shift_mask = 0x07 };
55 	enum { phase_range = 8 };
56 	int phase;
57 	int sweep_delay;
58 
59 	typedef Blip_Synth<blip_good_quality,1> Synth;
60 	Synth const& synth; // shared between squares
61 
Nes_SquareNes_Square62 	Nes_Square( Synth const* s ) : synth( *s ) { }
63 
64 	void clock_sweep( int adjust );
65 	void run( nes_time_t, nes_time_t );
resetNes_Square66 	void reset() {
67 		sweep_delay = 0;
68 		Nes_Envelope::reset();
69 	}
70 	nes_time_t maintain_phase( nes_time_t time, nes_time_t end_time,
71 			nes_time_t timer_period );
72 };
73 
74 // Nes_Triangle
75 struct Nes_Triangle : Nes_Osc
76 {
77 	enum { phase_range = 16 };
78 	int phase;
79 	int linear_counter;
80 	Blip_Synth<blip_med_quality,1> synth;
81 
82 	int calc_amp() const;
83 	void run( nes_time_t, nes_time_t );
84 	void clock_linear_counter();
resetNes_Triangle85 	void reset() {
86 		linear_counter = 0;
87 		phase = 1;
88 		Nes_Osc::reset();
89 	}
90 	nes_time_t maintain_phase( nes_time_t time, nes_time_t end_time,
91 			nes_time_t timer_period );
92 };
93 
94 // Nes_Noise
95 struct Nes_Noise : Nes_Envelope
96 {
97 	int noise;
98 	Blip_Synth<blip_med_quality,1> synth;
99 
100 	void run( nes_time_t, nes_time_t );
resetNes_Noise101 	void reset() {
102 		noise = 1 << 14;
103 		Nes_Envelope::reset();
104 	}
105 };
106 
107 // Nes_Dmc
108 struct Nes_Dmc : Nes_Osc
109 {
110 	int address;    // address of next byte to read
111 	int period;
112 	//int length_counter; // bytes remaining to play (already defined in Nes_Osc)
113 	int buf;
114 	int bits_remain;
115 	int bits;
116 	bool buf_full;
117 	bool silence;
118 
119 	enum { loop_flag = 0x40 };
120 
121 	int dac;
122 
123 	nes_time_t next_irq;
124 	bool irq_enabled;
125 	bool irq_flag;
126 	bool pal_mode;
127 	bool nonlinear;
128 
129 	int (*prg_reader)( void*, nes_addr_t ); // needs to be initialized to prg read function
130 	void* prg_reader_data;
131 
132 	Nes_Apu* apu;
133 
134 	Blip_Synth<blip_med_quality,1> synth;
135 
136 	void start();
137 	void write_register( int, int );
138 	void run( nes_time_t, nes_time_t );
139 	void recalc_irq();
140 	void fill_buffer();
141 	void reload_sample();
142 	void reset();
143 	int count_reads( nes_time_t, nes_time_t* ) const;
144 	nes_time_t next_read_time() const;
145 };
146 
147 #endif
148