1
2// Nes_Snd_Emu 0.1.7. http://www.slack.net/~ant/libs/
3
4#include "apu_snapshot.h"
5#include "Nes_Apu.h"
6
7/* Copyright (C) 2003-2005 Shay Green. This module is free software; you
8can redistribute it and/or modify it under the terms of the GNU Lesser
9General Public License as published by the Free Software Foundation; either
10version 2.1 of the License, or (at your option) any later version. This
11module is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14more details. You should have received a copy of the GNU Lesser General
15Public License along with this module; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18#include BLARGG_SOURCE_BEGIN
19
20template<int mode>
21struct apu_reflection
22{
23 #define REFLECT( apu, state ) (mode ? void (apu = state) : void (state = apu))
24
25 static void reflect_env( apu_snapshot_t::env_t& state, Nes_Envelope& osc )
26 {
27 REFLECT( state [0], osc.env_delay );
28 REFLECT( state [1], osc.envelope );
29 REFLECT( state [2], osc.reg_written [3] );
30 }
31
32 static void reflect_square( apu_snapshot_t::square_t& state, Nes_Square& osc )
33 {
34 reflect_env( state.env, osc );
35 REFLECT( state.delay, osc.delay );
36 REFLECT( state.length, osc.length_counter );
37 REFLECT( state.phase, osc.phase );
38 REFLECT( state.swp_delay, osc.sweep_delay );
39 REFLECT( state.swp_reset, osc.reg_written [1] );
40 }
41
42 static void reflect_triangle( apu_snapshot_t::triangle_t& state, Nes_Triangle& osc )
43 {
44 REFLECT( state.delay, osc.delay );
45 REFLECT( state.length, osc.length_counter );
46 REFLECT( state.linear_counter, osc.linear_counter );
47 REFLECT( state.linear_mode, osc.reg_written [3] );
48 }
49
50 static void reflect_noise( apu_snapshot_t::noise_t& state, Nes_Noise& osc )
51 {
52 reflect_env( state.env, osc );
53 REFLECT( state.delay, osc.delay );
54 REFLECT( state.length, osc.length_counter );
55 REFLECT( state.shift_reg, osc.noise );
56 }
57
58 static void reflect_dmc( apu_snapshot_t::dmc_t& state, Nes_Dmc& osc )
59 {
60 REFLECT( state.delay, osc.delay );
61 REFLECT( state.remain, osc.length_counter );
62 REFLECT( state.buf, osc.buf );
63 REFLECT( state.bits_remain, osc.bits_remain );
64 REFLECT( state.bits, osc.bits );
65 REFLECT( state.buf_empty, osc.buf_empty );
66 REFLECT( state.silence, osc.silence );
67 REFLECT( state.irq_flag, osc.irq_flag );
68 if ( mode )
69 state.addr = osc.address | 0x8000;
70 else
71 osc.address = state.addr & 0x7fff;
72 }
73};
74
75void Nes_Apu::save_snapshot( apu_snapshot_t* state ) const
76{
77 for ( int i = 0; i < osc_count * 4; i++ )
78 state->w40xx [i] = oscs [i >> 2]->regs [i & 3];
79 state->w40xx [0x11] = dmc.dac;
80
81 state->w4015 = osc_enables;
82 state->w4017 = frame_mode;
83 state->delay = frame_delay;
84 state->step = frame;
85 state->irq_flag = irq_flag;
86
87 typedef apu_reflection<1> refl;
88 Nes_Apu& apu = *(Nes_Apu*) this; // const_cast
89 refl::reflect_square ( state->square1, apu.square1 );
90 refl::reflect_square ( state->square2, apu.square2 );
91 refl::reflect_triangle( state->triangle, apu.triangle );
92 refl::reflect_noise ( state->noise, apu.noise );
93 refl::reflect_dmc ( state->dmc, apu.dmc );
94}
95
96void Nes_Apu::load_snapshot( apu_snapshot_t const& state )
97{
98 reset();
99
100 write_register( 0, 0x4017, state.w4017 );
101 write_register( 0, 0x4015, state.w4015 );
102
103 for ( int i = 0; i < osc_count * 4; i++ )
104 {
105 int n = state.w40xx [i];
106 oscs [i >> 2]->regs [i & 3] = n;
107 write_register( 0, 0x4000 + i, n );
108 }
109
110 frame_delay = state.delay;
111 frame = state.step;
112 irq_flag = state.irq_flag;
113
114 typedef apu_reflection<0> refl;
115 apu_snapshot_t& st = (apu_snapshot_t&) state; // const_cast
116 refl::reflect_square ( st.square1, square1 );
117 refl::reflect_square ( st.square2, square2 );
118 refl::reflect_triangle( st.triangle, triangle );
119 refl::reflect_noise ( st.noise, noise );
120 refl::reflect_dmc ( st.dmc, dmc );
121 dmc.recalc_irq();
122 dmc.last_amp = dmc.dac;
123}
124
125