1
2// Konami VRC6 sound chip emulator
3
4// Nes_Snd_Emu 0.1.7. Copyright (C) 2003-2005 Shay Green. GNU LGPL license.
5
6#ifndef NES_VRC6_H
7#define NES_VRC6_H
8
9#include "Nes_Apu.h"
10
11struct vrc6_snapshot_t;
12
13class Nes_Vrc6 {
14public:
15 Nes_Vrc6();
16 ~Nes_Vrc6();
17
18 // See Nes_Apu.h for reference
19 void reset();
20 void volume( double );
21 void treble_eq( blip_eq_t const& );
22 void output( Blip_Buffer* );
23 enum { osc_count = 3 };
24 void osc_output( int index, Blip_Buffer* );
25 void end_frame( cpu_time_t );
26 void save_snapshot( vrc6_snapshot_t* ) const;
27 void load_snapshot( vrc6_snapshot_t const& );
28
29 // Oscillator 0 write-only registers are at $9000-$9002
30 // Oscillator 1 write-only registers are at $A000-$A002
31 // Oscillator 2 write-only registers are at $B000-$B002
32 enum { reg_count = 3 };
33 enum { base_addr = 0x9000 };
34 enum { addr_step = 0x1000 };
35 void write_osc( cpu_time_t, int osc, int reg, int data );
36
37private:
38 // noncopyable
39 Nes_Vrc6( const Nes_Vrc6& );
40 Nes_Vrc6& operator = ( const Nes_Vrc6& );
41
42 struct Vrc6_Osc
43 {
44 BOOST::uint8_t regs [3];
45 Blip_Buffer* output;
46 int delay;
47 int last_amp;
48 int phase;
49 int amp; // only used by saw
50
51 int period() const
52 {
53 return (regs [2] & 0x0f) * 0x100L + regs [1] + 1;
54 }
55 };
56
57 Vrc6_Osc oscs [osc_count];
58 cpu_time_t last_time;
59
60 Blip_Synth<blip_med_quality,31> saw_synth;
61 Blip_Synth<blip_good_quality,15> square_synth;
62
63 void run_until( cpu_time_t );
64 void run_square( Vrc6_Osc& osc, cpu_time_t );
65 void run_saw( cpu_time_t );
66};
67
68struct vrc6_snapshot_t
69{
70 BOOST::uint8_t regs [3] [3];
71 BOOST::uint8_t saw_amp;
72 BOOST::uint16_t delays [3];
73 BOOST::uint8_t phases [3];
74 BOOST::uint8_t unused;
75};
76BOOST_STATIC_ASSERT( sizeof (vrc6_snapshot_t) == 20 );
77
78inline void Nes_Vrc6::osc_output( int i, Blip_Buffer* buf )
79{
80 assert( (unsigned) i < osc_count );
81 oscs [i].output = buf;
82}
83
84#endif
85
86