-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCookieboySoundUnit1.cpp
182 lines (151 loc) · 3.54 KB
/
CookieboySoundUnit1.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "CookieboySoundUnit1.h"
#include "CookieboySound.h"
Cookieboy::SoundUnit1::SoundUnit1(const bool &_CGB, Sound &soundController):
CGB(_CGB),
SoundController(soundController),
LengthCounter(0x3F, StatusBit),
Sweep(NR13, NR14, StatusBit, Duty)
{
Reset();
}
Cookieboy::SoundUnit1::~SoundUnit1()
{
}
void Cookieboy::SoundUnit1::TimerStep(DWORD clockDelta)
{
Duty.Step(clockDelta);
}
void Cookieboy::SoundUnit1::FrameSequencerStep(BYTE sequencerStep)
{
Sweep.Step(sequencerStep);
Envelope.Step(sequencerStep);
LengthCounter.Step(sequencerStep);
}
short Cookieboy::SoundUnit1::GetWaveLeftOutput()
{
short leftSwitch = (SoundController.GetNR51() >> 4) & 0x1;
short masterVolume = (SoundController.GetNR50() >> 4) & 0x7;
short envelopeValue = Envelope.GetEnvelopeValue();
return Duty.GetOutput() * envelopeValue * (masterVolume + 1) * leftSwitch * StatusBit;
}
short Cookieboy::SoundUnit1::GetWaveRightOutput()
{
short rightSwitch = SoundController.GetNR51() & 0x1;
short masterVolume = SoundController.GetNR50() & 0x7;
short envelopeValue = Envelope.GetEnvelopeValue();
return Duty.GetOutput() * envelopeValue * (masterVolume + 1) * rightSwitch * StatusBit;
}
void Cookieboy::SoundUnit1::Reset()
{
NR10Changed(0, true);
NR11Changed(0, true);
NR12Changed(0, true);
NR13Changed(0, true);
NR14Changed(0, true);
Duty.Reset();
Sweep.Reset();
Envelope.Reset();
LengthCounter.Reset();
StatusBit = 0;
WaveOutput = 0;
}
void Cookieboy::SoundUnit1::EmulateBIOS()
{
Reset();
//On DMG after BIOS executes NR52 containts 0xF1 - status bit for sound 1 is set
StatusBit = 1;
NR10Changed(0x80, true);
NR11Changed(0xBF, true);
NR12Changed(0xF3, true);
NR14Changed(0xBF, true);
}
//Sweep function control
void Cookieboy::SoundUnit1::NR10Changed(BYTE value, bool override)
{
if (!SoundController.GetAllSoundEnabled() && !override)
{
return;
}
Sweep.NRX0Changed(value);
}
//Wave pattern duty, Sound length
void Cookieboy::SoundUnit1::NR11Changed(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();
}
NR11 = value;
Duty.NRX1Changed(value);
LengthCounter.NRX1Changed(value);
}
//Envelope function control
void Cookieboy::SoundUnit1::NR12Changed(BYTE value, bool override)
{
if (!SoundController.GetAllSoundEnabled() && !override)
{
return;
}
Envelope.NRX2Changed(value);
if (Envelope.DisablesSound())
{
StatusBit = 0;
}
}
//Low frequency bits
void Cookieboy::SoundUnit1::NR13Changed(BYTE value, bool override)
{
if (!SoundController.GetAllSoundEnabled() && !override)
{
return;
}
NR13 = value;
Duty.NRX3Changed(value);
}
//initial, counter/consecutive mode, High frequency bits
void Cookieboy::SoundUnit1::NR14Changed(BYTE value, bool override)
{
if (!SoundController.GetAllSoundEnabled() && !override)
{
return;
}
NR14 = 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);
Sweep.NRX4Changed(value);
Envelope.NRX4Changed(value);
LengthCounter.NRX4Changed(value);
}
void Cookieboy::SoundUnit1::NR52Changed(BYTE value)
{
if (!(value >> 7))
{
StatusBit = 0;
NR10Changed(0, true);
if (CGB)
{
NR11Changed(0, true);
}
else
{
NR11Changed(NR11 & LengthCounter.GetLengthMask(), true);
}
NR12Changed(0, true);
NR13Changed(0, true);
NR14Changed(0, true);
}
}