1 //
2 // Copyright (C) 2007 by sinamas <sinamas at users.sourceforge.net>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License version 2 as
6 // published by the Free Software Foundation.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License version 2 for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // version 2 along with this program; if not, write to the
15 // Free Software Foundation, Inc.,
16 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18
19 #include "envelope_unit.h"
20 #include <algorithm>
21
22 namespace gambatte {
23
24 EnvelopeUnit::VolOnOffEvent EnvelopeUnit::nullEvent_;
25
EnvelopeUnit(VolOnOffEvent & volOnOffEvent)26 EnvelopeUnit::EnvelopeUnit(VolOnOffEvent &volOnOffEvent)
27 : volOnOffEvent_(volOnOffEvent)
28 , nr2_(0)
29 , volume_(0)
30 {
31 }
32
reset()33 void EnvelopeUnit::reset() {
34 counter_ = counter_disabled;
35 }
36
saveState(SaveState::SPU::Env & estate) const37 void EnvelopeUnit::saveState(SaveState::SPU::Env &estate) const {
38 estate.counter = counter_;
39 estate.volume = volume_;
40 }
41
loadState(SaveState::SPU::Env const & estate,unsigned nr2,unsigned long cc)42 void EnvelopeUnit::loadState(SaveState::SPU::Env const &estate, unsigned nr2, unsigned long cc) {
43 counter_ = std::max(estate.counter, cc);
44 volume_ = estate.volume;
45 nr2_ = nr2;
46 }
47
event()48 void EnvelopeUnit::event() {
49 unsigned long const period = nr2_ & 7;
50
51 if (period) {
52 unsigned newVol = volume_;
53 if (nr2_ & 8)
54 ++newVol;
55 else
56 --newVol;
57
58 if (newVol < 0x10U) {
59 volume_ = newVol;
60 if (volume_ < 2)
61 volOnOffEvent_(counter_);
62
63 counter_ += period << 15;
64 } else
65 counter_ = counter_disabled;
66 } else
67 counter_ += 8ul << 15;
68 }
69
nr2Change(unsigned const newNr2)70 bool EnvelopeUnit::nr2Change(unsigned const newNr2) {
71 if (!(nr2_ & 7) && counter_ != counter_disabled)
72 ++volume_;
73 else if (!(nr2_ & 8))
74 volume_ += 2;
75
76 if ((nr2_ ^ newNr2) & 8)
77 volume_ = 0x10 - volume_;
78
79 volume_ &= 0xF;
80 nr2_ = newNr2;
81 return !(newNr2 & 0xF8);
82 }
83
nr4Init(unsigned long const cc)84 bool EnvelopeUnit::nr4Init(unsigned long const cc) {
85 unsigned long period = (nr2_ & 7) ? nr2_ & 7 : 8;
86
87 if (((cc + 2) & 0x7000) == 0x0000)
88 ++period;
89
90 counter_ = cc - ((cc - 0x1000) & 0x7FFF) + period * 0x8000;
91
92 volume_ = nr2_ >> 4;
93 return !(nr2_ & 0xF8);
94 }
95
96 }
97