-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCookieboySoundUnit4.cpp
145 lines (119 loc) · 2.73 KB
/
CookieboySoundUnit4.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
#include "CookieboySoundUnit4.h"
#include "CookieboySound.h"
Cookieboy::SoundUnit4::SoundUnit4(const bool &_CGB, Sound &soundController):
CGB(_CGB),
SoundController(soundController),
LengthCounter(0x3F, StatusBit, 0xFF)
{
Reset();
}
Cookieboy::SoundUnit4::~SoundUnit4()
{
}
void Cookieboy::SoundUnit4::TimerStep(DWORD clockDelta)
{
lfsr.Step(clockDelta);
}
void Cookieboy::SoundUnit4::FrameSequencerStep(BYTE sequencerStep)
{
Envelope.Step(sequencerStep);
LengthCounter.Step(sequencerStep);
}
short Cookieboy::SoundUnit4::GetWaveLeftOutput()
{
short leftSwitch = (SoundController.GetNR51() >> 7) & 0x1;
short masterVolume = (SoundController.GetNR50() >> 4) & 0x7;
short envelopeValue = Envelope.GetEnvelopeValue();
return lfsr.GetOutput() * envelopeValue * (masterVolume + 1) * leftSwitch * StatusBit;
}
short Cookieboy::SoundUnit4::GetWaveRightOutput()
{
short rightSwitch = (SoundController.GetNR51() >> 3) & 0x1;
short masterVolume = SoundController.GetNR50() & 0x7;
short envelopeValue = Envelope.GetEnvelopeValue();
return lfsr.GetOutput() * envelopeValue * (masterVolume + 1) * rightSwitch * StatusBit;
}
void Cookieboy::SoundUnit4::Reset()
{
NR41Changed(0, true);
NR42Changed(0, true);
NR43Changed(0, true);
NR44Changed(0, true);
lfsr.Reset();
Envelope.Reset();
LengthCounter.Reset();
StatusBit = 0;
}
void Cookieboy::SoundUnit4::EmulateBIOS()
{
Reset();
NR41Changed(0xFF, true);
}
//Sound length
void Cookieboy::SoundUnit4::NR41Changed(BYTE value, bool override)
{
if (CGB && !SoundController.GetAllSoundEnabled() && !override)
{
return;
}
//While all sound off only length can be written
LengthCounter.NRX1Changed(value);
}
//Envelope function control
void Cookieboy::SoundUnit4::NR42Changed(BYTE value, bool override)
{
if (!SoundController.GetAllSoundEnabled() && !override)
{
return;
}
Envelope.NRX2Changed(value);
if (Envelope.DisablesSound())
{
StatusBit = 0;
}
}
//LFSR control
void Cookieboy::SoundUnit4::NR43Changed(BYTE value, bool override)
{
if (!SoundController.GetAllSoundEnabled() && !override)
{
return;
}
lfsr.NR43Changed(value);
}
//initial, counter/consecutive mode
void Cookieboy::SoundUnit4::NR44Changed(BYTE value, bool override)
{
if (!SoundController.GetAllSoundEnabled() && !override)
{
return;
}
NR44 = value;
//If channel initial set
if (value >> 7)
{
StatusBit = 1;
}
//In some cases envelope unit disables sound unit
if (Envelope.DisablesSound())
{
StatusBit = 0;
}
lfsr.NR44Changed(value);
Envelope.NRX4Changed(value);
LengthCounter.NRX4Changed(value);
}
void Cookieboy::SoundUnit4::NR52Changed(BYTE value)
{
if (!(value >> 7))
{
StatusBit = 0;
if (CGB)
{
NR41Changed(0, true);
}
NR42Changed(0, true);
NR43Changed(0, true);
NR44Changed(0, true);
}
}