-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
AudioInterface.cpp
328 lines (277 loc) · 8.45 KB
/
AudioInterface.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Copyright 2003 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
/*
Here is a nice ascii overview of audio flow affected by this file:
(RAM)---->[AI FIFO]---->[SRC]---->[Mixer]---->[DAC]---->(Speakers)
^
|
[L/R Volume]
\
(DVD)---->[Drive I/F]---->[SRC]---->[Counter]
Notes:
Output at "48KHz" is actually 48043Hz.
Sample counter counts streaming stereo samples after upsampling.
[DAC] causes [AI I/F] to read from RAM at rate selected by AIDFR.
Each [SRC] will upsample a 32KHz source, or pass through the 48KHz
source. The [Mixer]/[DAC] only operate at 48KHz.
AIS == disc streaming == DTK(Disk Track Player) == streaming audio, etc.
Supposedly, the retail hardware only supports 48KHz streaming from
[Drive I/F]. However it's more likely that the hardware supports
32KHz streaming, and the upsampling is transparent to the user.
TODO check if anything tries to stream at 32KHz.
The [Drive I/F] actually supports simultaneous requests for audio and
normal data. For this reason, we can't really get rid of the crit section.
IMPORTANT:
This file mainly deals with the [Drive I/F], however [AIDFR] controls
the rate at which the audio data is DMA'd from RAM into the [AI FIFO]
(and the speed at which the FIFO is read by its SRC). Everything else
relating to AID happens in DSP.cpp. It's kinda just bad hardware design.
TODO maybe the files should be merged?
*/
#include "AudioCommon/AudioCommon.h"
#include "Common/ChunkFile.h"
#include "Common/CommonTypes.h"
#include "Common/MathUtil.h"
#include "Core/CoreTiming.h"
#include "Core/HW/AudioInterface.h"
#include "Core/HW/CPU.h"
#include "Core/HW/MMIO.h"
#include "Core/HW/ProcessorInterface.h"
#include "Core/HW/SystemTimers.h"
#include "Core/PowerPC/PowerPC.h"
namespace AudioInterface
{
// Internal hardware addresses
enum
{
AI_CONTROL_REGISTER = 0x6C00,
AI_VOLUME_REGISTER = 0x6C04,
AI_SAMPLE_COUNTER = 0x6C08,
AI_INTERRUPT_TIMING = 0x6C0C,
};
enum
{
AIS_32KHz = 0,
AIS_48KHz = 1,
AID_32KHz = 1,
AID_48KHz = 0
};
// AI Control Register
union AICR
{
AICR() { hex = 0;}
AICR(u32 _hex) { hex = _hex;}
struct
{
u32 PSTAT : 1; // sample counter/playback enable
u32 AISFR : 1; // AIS Frequency (0=32khz 1=48khz)
u32 AIINTMSK : 1; // 0=interrupt masked 1=interrupt enabled
u32 AIINT : 1; // audio interrupt status
u32 AIINTVLD : 1; // This bit controls whether AIINT is affected by the Interrupt Timing register
// matching the sample counter. Once set, AIINT will hold its last value
u32 SCRESET : 1; // write to reset counter
u32 AIDFR : 1; // AID Frequency (0=48khz 1=32khz)
u32 :25;
};
u32 hex;
};
// AI Volume Register
union AIVR
{
AIVR() { hex = 0;}
struct
{
u32 left : 8;
u32 right : 8;
u32 :16;
};
u32 hex;
};
// STATE_TO_SAVE
// Registers
static AICR m_Control;
static AIVR m_Volume;
static u32 m_SampleCounter = 0;
static u32 m_InterruptTiming = 0;
static u64 g_LastCPUTime = 0;
static u64 g_CPUCyclesPerSample = 0xFFFFFFFFFFFULL;
static unsigned int g_AISSampleRate = 48000;
static unsigned int g_AIDSampleRate = 32000;
void DoState(PointerWrap &p)
{
p.DoPOD(m_Control);
p.DoPOD(m_Volume);
p.Do(m_SampleCounter);
p.Do(m_InterruptTiming);
p.Do(g_LastCPUTime);
p.Do(g_AISSampleRate);
p.Do(g_AIDSampleRate);
p.Do(g_CPUCyclesPerSample);
}
static void GenerateAudioInterrupt();
static void UpdateInterrupts();
static void IncreaseSampleCount(const u32 _uAmount);
static int GetAIPeriod();
static int et_AI;
void Init()
{
m_Control.hex = 0;
m_Control.AISFR = AIS_48KHz;
m_Volume.hex = 0;
m_SampleCounter = 0;
m_InterruptTiming = 0;
g_LastCPUTime = 0;
g_CPUCyclesPerSample = 0xFFFFFFFFFFFULL;
g_AISSampleRate = 48000;
g_AIDSampleRate = 32000;
et_AI = CoreTiming::RegisterEvent("AICallback", Update);
}
void Shutdown()
{
}
void RegisterMMIO(MMIO::Mapping* mmio, u32 base)
{
mmio->Register(base | AI_CONTROL_REGISTER,
MMIO::DirectRead<u32>(&m_Control.hex),
MMIO::ComplexWrite<u32>([](u32, u32 val) {
AICR tmpAICtrl(val);
if (m_Control.AIINTMSK != tmpAICtrl.AIINTMSK)
{
DEBUG_LOG(AUDIO_INTERFACE, "Change AIINTMSK to %d", tmpAICtrl.AIINTMSK);
m_Control.AIINTMSK = tmpAICtrl.AIINTMSK;
}
if (m_Control.AIINTVLD != tmpAICtrl.AIINTVLD)
{
DEBUG_LOG(AUDIO_INTERFACE, "Change AIINTVLD to %d", tmpAICtrl.AIINTVLD);
m_Control.AIINTVLD = tmpAICtrl.AIINTVLD;
}
// Set frequency of streaming audio
if (tmpAICtrl.AISFR != m_Control.AISFR)
{
// AISFR rates below are intentionally inverted wrt yagcd
DEBUG_LOG(AUDIO_INTERFACE, "Change AISFR to %s", tmpAICtrl.AISFR ? "48khz":"32khz");
m_Control.AISFR = tmpAICtrl.AISFR;
g_AISSampleRate = tmpAICtrl.AISFR ? 48000 : 32000;
g_sound_stream->GetMixer()->SetStreamInputSampleRate(g_AISSampleRate);
g_CPUCyclesPerSample = SystemTimers::GetTicksPerSecond() / g_AISSampleRate;
}
// Set frequency of DMA
if (tmpAICtrl.AIDFR != m_Control.AIDFR)
{
DEBUG_LOG(AUDIO_INTERFACE, "Change AIDFR to %s", tmpAICtrl.AIDFR ? "32khz":"48khz");
m_Control.AIDFR = tmpAICtrl.AIDFR;
g_AIDSampleRate = tmpAICtrl.AIDFR ? 32000 : 48000;
g_sound_stream->GetMixer()->SetDMAInputSampleRate(g_AIDSampleRate);
}
// Streaming counter
if (tmpAICtrl.PSTAT != m_Control.PSTAT)
{
DEBUG_LOG(AUDIO_INTERFACE, "%s streaming audio", tmpAICtrl.PSTAT ? "start":"stop");
m_Control.PSTAT = tmpAICtrl.PSTAT;
g_LastCPUTime = CoreTiming::GetTicks();
CoreTiming::RemoveEvent(et_AI);
CoreTiming::ScheduleEvent(GetAIPeriod(), et_AI);
}
// AI Interrupt
if (tmpAICtrl.AIINT)
{
DEBUG_LOG(AUDIO_INTERFACE, "Clear AIS Interrupt");
m_Control.AIINT = 0;
}
// Sample Count Reset
if (tmpAICtrl.SCRESET)
{
DEBUG_LOG(AUDIO_INTERFACE, "Reset AIS sample counter");
m_SampleCounter = 0;
g_LastCPUTime = CoreTiming::GetTicks();
}
UpdateInterrupts();
})
);
mmio->Register(base | AI_VOLUME_REGISTER,
MMIO::DirectRead<u32>(&m_Volume.hex),
MMIO::ComplexWrite<u32>([](u32, u32 val) {
m_Volume.hex = val;
g_sound_stream->GetMixer()->SetStreamingVolume(m_Volume.left, m_Volume.right);
})
);
mmio->Register(base | AI_SAMPLE_COUNTER,
MMIO::ComplexRead<u32>([](u32) {
return m_SampleCounter + static_cast<u32>((CoreTiming::GetTicks() - g_LastCPUTime) / g_CPUCyclesPerSample);
}),
MMIO::ComplexWrite<u32>([](u32, u32 val) {
m_SampleCounter = val;
g_LastCPUTime = CoreTiming::GetTicks();
CoreTiming::RemoveEvent(et_AI);
CoreTiming::ScheduleEvent(GetAIPeriod(), et_AI);
})
);
mmio->Register(base | AI_INTERRUPT_TIMING,
MMIO::DirectRead<u32>(&m_InterruptTiming),
MMIO::ComplexWrite<u32>([](u32, u32 val) {
DEBUG_LOG(AUDIO_INTERFACE, "AI_INTERRUPT_TIMING=%08x@%08x", val, PowerPC::ppcState.pc);
m_InterruptTiming = val;
CoreTiming::RemoveEvent(et_AI);
CoreTiming::ScheduleEvent(GetAIPeriod(), et_AI);
})
);
}
static void UpdateInterrupts()
{
ProcessorInterface::SetInterrupt(
ProcessorInterface::INT_CAUSE_AI, m_Control.AIINT & m_Control.AIINTMSK);
}
static void GenerateAudioInterrupt()
{
m_Control.AIINT = 1;
UpdateInterrupts();
}
void GenerateAISInterrupt()
{
GenerateAudioInterrupt();
}
static void IncreaseSampleCount(const u32 _iAmount)
{
if (m_Control.PSTAT)
{
u32 old_SampleCounter = m_SampleCounter + 1;
m_SampleCounter += _iAmount;
if ((m_InterruptTiming - old_SampleCounter) <= (m_SampleCounter - old_SampleCounter))
{
DEBUG_LOG(AUDIO_INTERFACE, "GenerateAudioInterrupt %08x:%08x @ %08x m_Control.AIINTVLD=%d", m_SampleCounter, m_InterruptTiming, PowerPC::ppcState.pc, m_Control.AIINTVLD);
GenerateAudioInterrupt();
}
}
}
bool IsPlaying()
{
return (m_Control.PSTAT == 1);
}
unsigned int GetAIDSampleRate()
{
return g_AIDSampleRate;
}
void Update(u64 userdata, int cyclesLate)
{
if (m_Control.PSTAT)
{
const u64 Diff = CoreTiming::GetTicks() - g_LastCPUTime;
if (Diff > g_CPUCyclesPerSample)
{
const u32 Samples = static_cast<u32>(Diff / g_CPUCyclesPerSample);
g_LastCPUTime += Samples * g_CPUCyclesPerSample;
IncreaseSampleCount(Samples);
}
CoreTiming::ScheduleEvent(GetAIPeriod() - cyclesLate, et_AI);
}
}
int GetAIPeriod()
{
u64 period = g_CPUCyclesPerSample * (m_InterruptTiming-m_SampleCounter);
u64 s_period = g_CPUCyclesPerSample * g_AISSampleRate;
if (period == 0)
return static_cast<int>(s_period);
return static_cast<int>(std::min(period, s_period));
}
} // end of namespace AudioInterface