-
Notifications
You must be signed in to change notification settings - Fork 6
/
CookieboySoundUnit2.cpp
155 lines (128 loc) · 2.97 KB
/
CookieboySoundUnit2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "CookieboySoundUnit2.h"
#include "CookieboySound.h"
Cookieboy::SoundUnit2::SoundUnit2(const bool &_CGB, Sound &soundController):
CGB(_CGB),
SoundController(soundController),
LengthCounter(0x3F, StatusBit)
{
Reset();
}
Cookieboy::SoundUnit2::~SoundUnit2()
{
}
void Cookieboy::SoundUnit2::TimerStep(DWORD clockDelta)
{
Duty.Step(clockDelta);
}
void Cookieboy::SoundUnit2::FrameSequencerStep(BYTE sequencerStep)
{
Envelope.Step(sequencerStep);
LengthCounter.Step(sequencerStep);
}
short Cookieboy::SoundUnit2::GetWaveLeftOutput()
{
short leftSwitch = (SoundController.GetNR51() >> 5) & 0x1;
short masterVolume = (SoundController.GetNR50() >> 4) & 0x7;
short envelopeValue = Envelope.GetEnvelopeValue();
return Duty.GetOutput() * envelopeValue * (masterVolume + 1) * leftSwitch * StatusBit;
}
short Cookieboy::SoundUnit2::GetWaveRightOutput()
{
short rightSwitch = (SoundController.GetNR51() >> 1) & 0x1;
short masterVolume = SoundController.GetNR50() & 0x7;
short envelopeValue = Envelope.GetEnvelopeValue();
return Duty.GetOutput() * envelopeValue * (masterVolume + 1) * rightSwitch * StatusBit;
}
void Cookieboy::SoundUnit2::Reset()
{
NR21Changed(0, true);
NR22Changed(0, true);
NR23Changed(0, true);
NR24Changed(0, true);
StatusBit = 0;
}
void Cookieboy::SoundUnit2::EmulateBIOS()
{
Reset();
NR21Changed(0x80, true);
NR22Changed(0xF3, true);
}
//Wave pattern duty, Sound length
void Cookieboy::SoundUnit2::NR21Changed(BYTE value, bool override)
{
if (CGB && !SoundController.GetAllSoundEnabled() && !override)
{
return;
}
if (!SoundController.GetAllSoundEnabled() && !override)
{
//While all sound off only length can be written
value &= LengthCounter.GetLengthMask();
}
NR21 = value;
Duty.NRX1Changed(value);
LengthCounter.NRX1Changed(value);
}
//Envelope function control
void Cookieboy::SoundUnit2::NR22Changed(BYTE value, bool override)
{
if (!SoundController.GetAllSoundEnabled() && !override)
{
return;
}
Envelope.NRX2Changed(value);
if (Envelope.DisablesSound())
{
StatusBit = 0;
}
}
//Low frequency bits
void Cookieboy::SoundUnit2::NR23Changed(BYTE value, bool override)
{
if (!SoundController.GetAllSoundEnabled() && !override)
{
return;
}
NR23 = value;
Duty.NRX3Changed(value);
}
//initial, counter/consecutive mode, High frequency bits
void Cookieboy::SoundUnit2::NR24Changed(BYTE value, bool override)
{
if (!SoundController.GetAllSoundEnabled() && !override)
{
return;
}
NR24 = value;
//If channel initial set
if (value >> 7)
{
StatusBit = 1;
}
//In some cases envelope unit disables sound unit
if (Envelope.DisablesSound())
{
StatusBit = 0;
}
Duty.NRX4Changed(value);
Envelope.NRX4Changed(value);
LengthCounter.NRX4Changed(value);
}
void Cookieboy::SoundUnit2::NR52Changed(BYTE value)
{
if (!(value >> 7))
{
StatusBit = 0;
if (CGB)
{
NR21Changed(0, true);
}
else
{
NR21Changed(NR21 & LengthCounter.GetLengthMask(), true);
}
NR22Changed(0, true);
NR23Changed(0, true);
NR24Changed(0, true);
}
}