comparison src/console/Nes_Vrc6_Apu.cxx @ 316:fb513e10174e trunk

[svn] - merge libconsole-blargg into mainline libconsole: + obsoletes plugins-ugly:sapplug
author nenolod
date Thu, 30 Nov 2006 19:54:33 -0800
parents 3da1b8942b8b
children 986f098da058
comparison
equal deleted inserted replaced
315:2294f3a6f136 316:fb513e10174e
1 1 // Nes_Snd_Emu 0.1.8. http://www.slack.net/~ant/
2 // Nes_Snd_Emu 0.1.7. http://www.slack.net/~ant/
3 2
4 #include "Nes_Vrc6_Apu.h" 3 #include "Nes_Vrc6_Apu.h"
5 4
6 /* Copyright (C) 2003-2006 Shay Green. This module is free software; you 5 /* Copyright (C) 2003-2006 Shay Green. This module is free software; you
7 can redistribute it and/or modify it under the terms of the GNU Lesser 6 can redistribute it and/or modify it under the terms of the GNU Lesser
8 General Public License as published by the Free Software Foundation; either 7 General Public License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version. This 8 version 2.1 of the License, or (at your option) any later version. This
10 module is distributed in the hope that it will be useful, but WITHOUT ANY 9 module is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for 11 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 more details. You should have received a copy of the GNU Lesser General 12 details. You should have received a copy of the GNU Lesser General Public
14 Public License along with this module; if not, write to the Free Software 13 License along with this module; if not, write to the Free Software Foundation,
15 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ 14 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
16 15
17 #include BLARGG_SOURCE_BEGIN 16 #include "blargg_source.h"
18 17
19 Nes_Vrc6_Apu::Nes_Vrc6_Apu() 18 Nes_Vrc6_Apu::Nes_Vrc6_Apu()
20 { 19 {
21 output( NULL ); 20 output( NULL );
22 volume( 1.0 ); 21 volume( 1.0 );
23 reset(); 22 reset();
24 }
25
26 Nes_Vrc6_Apu::~Nes_Vrc6_Apu()
27 {
28 } 23 }
29 24
30 void Nes_Vrc6_Apu::reset() 25 void Nes_Vrc6_Apu::reset()
31 { 26 {
32 last_time = 0; 27 last_time = 0;
46 { 41 {
47 for ( int i = 0; i < osc_count; i++ ) 42 for ( int i = 0; i < osc_count; i++ )
48 osc_output( i, buf ); 43 osc_output( i, buf );
49 } 44 }
50 45
51 void Nes_Vrc6_Apu::run_until( nes_time_t time ) 46 void Nes_Vrc6_Apu::run_until( blip_time_t time )
52 { 47 {
53 require( time >= last_time ); 48 require( time >= last_time );
54 run_square( oscs [0], time ); 49 run_square( oscs [0], time );
55 run_square( oscs [1], time ); 50 run_square( oscs [1], time );
56 run_saw( time ); 51 run_saw( time );
57 last_time = time; 52 last_time = time;
58 } 53 }
59 54
60 void Nes_Vrc6_Apu::write_osc( nes_time_t time, int osc_index, int reg, int data ) 55 void Nes_Vrc6_Apu::write_osc( blip_time_t time, int osc_index, int reg, int data )
61 { 56 {
62 require( (unsigned) osc_index < osc_count ); 57 require( (unsigned) osc_index < osc_count );
63 require( (unsigned) reg < reg_count ); 58 require( (unsigned) reg < reg_count );
64 59
65 run_until( time ); 60 run_until( time );
66 oscs [osc_index].regs [reg] = data; 61 oscs [osc_index].regs [reg] = data;
67 } 62 }
68 63
69 void Nes_Vrc6_Apu::end_frame( nes_time_t time ) 64 void Nes_Vrc6_Apu::end_frame( blip_time_t time )
70 { 65 {
71 if ( time > last_time ) 66 if ( time > last_time )
72 run_until( time ); 67 run_until( time );
73 68
74 assert( last_time >= time ); 69 assert( last_time >= time );
75 last_time -= time; 70 last_time -= time;
76 } 71 }
77 72
78 void Nes_Vrc6_Apu::save_snapshot( vrc6_snapshot_t* out ) const 73 void Nes_Vrc6_Apu::save_state( vrc6_apu_state_t* out ) const
79 { 74 {
80 out->saw_amp = oscs [2].amp; 75 out->saw_amp = oscs [2].amp;
81 for ( int i = 0; i < osc_count; i++ ) 76 for ( int i = 0; i < osc_count; i++ )
82 { 77 {
83 Vrc6_Osc const& osc = oscs [i]; 78 Vrc6_Osc const& osc = oscs [i];
87 out->delays [i] = osc.delay; 82 out->delays [i] = osc.delay;
88 out->phases [i] = osc.phase; 83 out->phases [i] = osc.phase;
89 } 84 }
90 } 85 }
91 86
92 void Nes_Vrc6_Apu::load_snapshot( vrc6_snapshot_t const& in ) 87 void Nes_Vrc6_Apu::load_state( vrc6_apu_state_t const& in )
93 { 88 {
94 reset(); 89 reset();
95 oscs [2].amp = in.saw_amp; 90 oscs [2].amp = in.saw_amp;
96 for ( int i = 0; i < osc_count; i++ ) 91 for ( int i = 0; i < osc_count; i++ )
97 { 92 {
104 } 99 }
105 if ( !oscs [2].phase ) 100 if ( !oscs [2].phase )
106 oscs [2].phase = 1; 101 oscs [2].phase = 1;
107 } 102 }
108 103
109 #include BLARGG_ENABLE_OPTIMIZER 104 void Nes_Vrc6_Apu::run_square( Vrc6_Osc& osc, blip_time_t end_time )
110
111 void Nes_Vrc6_Apu::run_square( Vrc6_Osc& osc, nes_time_t end_time )
112 { 105 {
113 Blip_Buffer* output = osc.output; 106 Blip_Buffer* output = osc.output;
114 if ( !output ) 107 if ( !output )
115 return; 108 return;
109 output->set_modified();
116 110
117 int volume = osc.regs [0] & 15; 111 int volume = osc.regs [0] & 15;
118 if ( !(osc.regs [2] & 0x80) ) 112 if ( !(osc.regs [2] & 0x80) )
119 volume = 0; 113 volume = 0;
120 114
121 int gate = osc.regs [0] & 0x80; 115 int gate = osc.regs [0] & 0x80;
122 int duty = ((osc.regs [0] >> 4) & 7) + 1; 116 int duty = ((osc.regs [0] >> 4) & 7) + 1;
123 int delta = ((gate || osc.phase < duty) ? volume : 0) - osc.last_amp; 117 int delta = ((gate || osc.phase < duty) ? volume : 0) - osc.last_amp;
124 nes_time_t time = last_time; 118 blip_time_t time = last_time;
125 if ( delta ) 119 if ( delta )
126 { 120 {
127 osc.last_amp += delta; 121 osc.last_amp += delta;
128 square_synth.offset( time, delta, output ); 122 square_synth.offset( time, delta, output );
129 } 123 }
159 } 153 }
160 osc.delay = time - end_time; 154 osc.delay = time - end_time;
161 } 155 }
162 } 156 }
163 157
164 void Nes_Vrc6_Apu::run_saw( nes_time_t end_time ) 158 void Nes_Vrc6_Apu::run_saw( blip_time_t end_time )
165 { 159 {
166 Vrc6_Osc& osc = oscs [2]; 160 Vrc6_Osc& osc = oscs [2];
167 Blip_Buffer* output = osc.output; 161 Blip_Buffer* output = osc.output;
168 if ( !output ) 162 if ( !output )
169 return; 163 return;
164 output->set_modified();
170 165
171 int amp = osc.amp; 166 int amp = osc.amp;
172 int amp_step = osc.regs [0] & 0x3F; 167 int amp_step = osc.regs [0] & 0x3F;
173 nes_time_t time = last_time; 168 blip_time_t time = last_time;
174 int last_amp = osc.last_amp; 169 int last_amp = osc.last_amp;
175 if ( !(osc.regs [2] & 0x80) || !(amp_step | amp) ) 170 if ( !(osc.regs [2] & 0x80) || !(amp_step | amp) )
176 { 171 {
177 osc.delay = 0; 172 osc.delay = 0;
178 int delta = (amp >> 3) - last_amp; 173 int delta = (amp >> 3) - last_amp;