Merge pull request #233 from delroth/dsphle-cleanups

DSPHLE Zelda cleanups
This commit is contained in:
Pierre Bourdon
2014-03-30 20:12:20 +02:00
5 changed files with 233 additions and 425 deletions
File diff suppressed because it is too large Load Diff
+33 -33
View File
@@ -140,8 +140,8 @@ public:
u32 Read32()
{
u32 res = *(u32*)&m_Buffer[m_readOffset];
m_readOffset += 4;
u32 res = *(u32*)&m_buffer[m_read_offset];
m_read_offset += 4;
return res;
}
@@ -198,52 +198,52 @@ private:
}
// These are the only dynamically allocated things allowed in the ucode.
s32* m_VoiceBuffer;
s16* m_ResampleBuffer;
s32* m_LeftBuffer;
s32* m_RightBuffer;
s32* m_voice_buffer;
s16* m_resample_buffer;
s32* m_left_buffer;
s32* m_right_buffer;
// If you add variables, remember to keep DoState() and the constructor up to date.
s16 m_AFCCoefTable[32];
s16 m_MiscTable[0x280];
s16 m_afc_coef_table[32];
s16 m_misc_table[0x280];
bool m_bSyncInProgress;
u32 m_MaxVoice;
u32 m_SyncFlags[16];
bool m_sync_in_progress;
u32 m_max_voice;
u32 m_sync_flags[16];
// Used by SMS version
u32 m_NumSyncMail;
u32 m_num_sync_mail;
u32 m_NumVoices;
u32 m_num_voices;
bool m_bSyncCmdPending;
u32 m_CurVoice;
u32 m_CurBuffer;
u32 m_NumBuffers;
bool m_sync_cmd_pending;
u32 m_current_voice;
u32 m_current_buffer;
u32 m_num_buffers;
// Those are set by command 0x1 (DsetupTable)
u32 m_VoicePBsAddr;
u32 m_UnkTableAddr;
u32 m_AFCCoefTableAddr;
u32 m_ReverbPBsAddr;
u32 m_voice_pbs_addr;
u32 m_unk_table_addr;
u32 m_afc_coef_table_addr;
u32 m_reverb_pbs_addr;
u32 m_RightBuffersAddr;
u32 m_LeftBuffersAddr;
u32 m_right_buffers_addr;
u32 m_left_buffers_addr;
//u32 m_unkAddr;
u32 m_pos;
// Only in SMG ucode
// Set by command 0xE (DsetDMABaseAddr)
u32 m_DMABaseAddr;
u32 m_dma_base_addr;
// List, buffer management =====================
u32 m_numSteps;
bool m_bListInProgress;
u32 m_num_steps;
bool m_list_in_progress;
u32 m_step;
u8 m_Buffer[1024];
u8 m_buffer[1024];
u32 m_readOffset;
u32 m_read_offset;
enum EMailState
{
@@ -253,12 +253,12 @@ private:
ReadingSystemMsg
};
EMailState m_MailState;
u16 m_PBMask[0x10];
EMailState m_mail_state;
u16 m_pb_mask[0x10];
u32 m_NumPBs;
u32 m_PBAddress; // The main param block array
u32 m_PBAddress2; // 4 smaller param blocks
u32 m_num_pbs;
u32 m_pb_address; // The main param block array
u32 m_pb_address2; // 4 smaller param blocks
void ExecuteList();
@@ -1,180 +0,0 @@
#if 0
void ZeldaUCode::UpdatePB(ZPB& _rPB, int *templbuffer, int *temprbuffer, u32 _Size)
{
u16* pTest = (u16*)&_rPB;
// Checks at 0293
if (pTest[0x00] == 0)
return;
if (pTest[0x01] != 0)
return;
if (pTest[0x06] != 0x00)
{
// probably pTest[0x06] == 0 -> AFC (and variants)
// See 02a4
}
else
{
switch (_rPB.type) // or Bytes per Sample
{
case 0x05:
case 0x09:
{
// initialize "decoder" if the sample is played the first time
if (pTest[0x04] != 0)
{
// This is 0717_ReadOutPBStuff
// increment 4fb
// zelda:
// perhaps init or "has played before"
pTest[0x32] = 0x00;
pTest[0x66] = 0x00; // history1
pTest[0x67] = 0x00; // history2
// samplerate? length? num of samples? i dunno...
// Likely length...
pTest[0x3a] = pTest[0x8a];
pTest[0x3b] = pTest[0x8b];
// Copy ARAM addr from r to rw area.
pTest[0x38] = pTest[0x8c];
pTest[0x39] = pTest[0x8d];
}
if (pTest[0x01] != 0) // 0747 early out... i dunno if this can happen because we filter it above
return;
u32 ARAMAddr = (pTest[0x38] << 16) | pTest[0x39];
u32 NumberOfSamples = (pTest[0x3a] << 16) | pTest[0x3b];
// round upwards how many samples we need to copy, 0759
NumberOfSamples = (NumberOfSamples + 0xf) >> 4; // i think the lower 4 are the fraction
u32 frac = NumberOfSamples & 0xF;
u8 inBuffer[9];
short outbuf[16];
u32 sampleCount = 0;
// It must be something like this:
// The PB contains a small sample buffer of 0x4D decoded samples.
// If it's empty or "used", decode to it.
// Then, resample from this buffer to the output as you go. When it needs
// wrapping, decode more.
#define USE_RESAMPLE
#if !defined(USE_RESAMPLE)
for (int s = 0; s < _Size/16; s++)
{
for (int i = 0; i < 9; i++)
{
inBuffer[i] = DSP::ReadARAM(ARAMAddr);
ARAMAddr++;
}
AFCdecodebuffer((char*)inBuffer, outbuf, (short*)&pTest[0x66], (short*)&pTest[0x67]);
for (int i = 0; i < 16; i++)
{
templbuffer[sampleCount] += outbuf[i];
temprbuffer[sampleCount] += outbuf[i];
sampleCount++;
}
NumberOfSamples--;
if (NumberOfSamples<=0)
break;
}
#else
while (NumberOfSamples > 0)
{
for (int i = 0; i < 9; i++)
{
inBuffer[i] = DSP::ReadARAM(ARAMAddr);
ARAMAddr++;
}
AFCdecodebuffer(m_AFCCoefTable, (char*)inBuffer, outbuf, (short*)&pTest[0x66], (short*)&pTest[0x67], 9);
CResampler Sampler(outbuf, 16, 48000);
while (Sampler.m_queueSize > 0)
{
int sample = Sampler.sample_queue.front();
Sampler.sample_queue.pop();
Sampler.m_queueSize -= 1;
templbuffer[sampleCount] += sample;
temprbuffer[sampleCount] += sample;
sampleCount++;
if (sampleCount > _Size)
break;
}
if (sampleCount > _Size)
break;
NumberOfSamples--;
}
#endif
if (NumberOfSamples == 0)
{
pTest[0x01] = 1; // we are done ??
}
// write back
NumberOfSamples = (NumberOfSamples << 4); // missing fraction
pTest[0x38] = ARAMAddr >> 16;
pTest[0x39] = ARAMAddr & 0xFFFF;
pTest[0x3a] = NumberOfSamples >> 16;
pTest[0x3b] = NumberOfSamples & 0xFFFF;
#if 0
NumberOfSamples = (NumberOfSamples + 0xf) >> 4;
static u8 Buffer[500000];
for (int i =0; i<NumberOfSamples*9; i++)
{
Buffer[i] = DSP::ReadARAM(ARAMAddr+i);
}
// yes, the dumps are really zelda sound ;)
DumpAFC(Buffer, NumberOfSamples*9, 0x3d00);
DumpPB(_rPB);
// exit(1);
#endif
// i think pTest[0x3a] and pTest[0x3b] got an update after you have decoded some samples...
// just decrement them with the number of samples you have played
// and incrrease the ARAM Offset in pTest[0x38], pTest[0x39]
// end of block (Zelda 03b2)
if (pTest[0x06] == 0)
{
// 02a4
//
pTest[0x04] = 0;
}
}
break;
default:
ERROR_LOG(DSPHLE, "Zelda Ucode: Unknown PB type %i", _rPB.type);
break;
}
}
}
#endif
@@ -158,7 +158,7 @@ void ZeldaUCode::RenderSynth_WaveTable(ZeldaVoicePB &PB, s32* _Buffer, int _Size
for (int i = 0; i < 0x50; i++)
{
_Buffer[i] = m_MiscTable[address];
_Buffer[i] = m_misc_table[address];
ACC0 += PB.RatioInt << 5;
address = AddValueToReg(address, ((ACC0 >> 16) & 0xffff));
@@ -277,7 +277,7 @@ void ZeldaUCode::RenderVoice_AFC(ZeldaVoicePB &PB, s16 *_Buffer, int _Size)
u32 ram_mask = 1024 * 1024 * 16 - 1;
if (IsDMAVersion())
{
source = Memory::GetPointer(m_DMABaseAddr);
source = Memory::GetPointer(m_dma_base_addr);
ram_mask = 1024 * 1024 * 64 - 1;
}
else
@@ -326,7 +326,7 @@ restart:
u32 prev_addr = PB.CurAddr;
// Prefill the decode buffer.
AFCdecodebuffer(m_AFCCoefTable, (char*)(source + (PB.CurAddr & ram_mask)), outbuf, (short*)&PB.YN2, (short*)&PB.YN1, PB.Format);
AFCdecodebuffer(m_afc_coef_table, (char*)(source + (PB.CurAddr & ram_mask)), outbuf, (short*)&PB.YN2, (short*)&PB.YN1, PB.Format);
PB.CurAddr += PB.Format; // 9 or 5
u32 SamplePosition = PB.Length - PB.RemLength;
@@ -350,7 +350,7 @@ restart:
prev_yn2 = PB.YN2;
prev_addr = PB.CurAddr;
AFCdecodebuffer(m_AFCCoefTable, (char*)(source + (PB.CurAddr & ram_mask)), outbuf, (short*)&PB.YN2, (short*)&PB.YN1, PB.Format);
AFCdecodebuffer(m_afc_coef_table, (char*)(source + (PB.CurAddr & ram_mask)), outbuf, (short*)&PB.YN2, (short*)&PB.YN1, PB.Format);
PB.CurAddr += PB.Format; // 9 or 5
}
}
@@ -477,7 +477,7 @@ void ZeldaUCode::RenderAddVoice(ZeldaVoicePB &PB, s32* _LeftBuffer, s32* _RightB
{
s32 sample = (s32)(s16)PB.FixedSample;
for (int i = 0; i < _Size; i++)
m_VoiceBuffer[i] = sample;
m_voice_buffer[i] = sample;
goto ContinueWithBlock; // Yes, a goto. Yes, it's evil, but it makes the flow look much more like the DSP code.
}
@@ -498,18 +498,18 @@ void ZeldaUCode::RenderAddVoice(ZeldaVoicePB &PB, s32* _LeftBuffer, s32* _RightB
{
case 0x0005: // AFC with extra low bitrate (32:5 compression).
case 0x0009: // AFC with normal bitrate (32:9 compression).
RenderVoice_AFC(PB, m_ResampleBuffer + 4, _Size);
Resample(PB, _Size, m_ResampleBuffer + 4, m_VoiceBuffer, true);
RenderVoice_AFC(PB, m_resample_buffer + 4, _Size);
Resample(PB, _Size, m_resample_buffer + 4, m_voice_buffer, true);
break;
case 0x0008: // PCM8 - normal PCM 8-bit audio. Used in Mario Kart DD + very little in Zelda WW.
RenderVoice_PCM8(PB, m_ResampleBuffer + 4, _Size);
Resample(PB, _Size, m_ResampleBuffer + 4, m_VoiceBuffer, true);
RenderVoice_PCM8(PB, m_resample_buffer + 4, _Size);
Resample(PB, _Size, m_resample_buffer + 4, m_voice_buffer, true);
break;
case 0x0010: // PCM16 - normal PCM 16-bit audio.
RenderVoice_PCM16(PB, m_ResampleBuffer + 4, _Size);
Resample(PB, _Size, m_ResampleBuffer + 4, m_VoiceBuffer, true);
RenderVoice_PCM16(PB, m_resample_buffer + 4, _Size);
Resample(PB, _Size, m_resample_buffer + 4, m_voice_buffer, true);
break;
case 0x0020:
@@ -517,15 +517,15 @@ void ZeldaUCode::RenderAddVoice(ZeldaVoicePB &PB, s32* _LeftBuffer, s32* _RightB
// to the output buffer. However, (if we ever see this sound type), we'll
// have to resample anyway since we're running at a different sample rate.
RenderVoice_Raw(PB, m_ResampleBuffer + 4, _Size);
Resample(PB, _Size, m_ResampleBuffer + 4, m_VoiceBuffer, true);
RenderVoice_Raw(PB, m_resample_buffer + 4, _Size);
Resample(PB, _Size, m_resample_buffer + 4, m_voice_buffer, true);
break;
case 0x0021:
// Raw sound from RAM. Important for Zelda WW. Cutscenes use the music
// to let the game know they ended
RenderVoice_Raw(PB, m_ResampleBuffer + 4, _Size);
Resample(PB, _Size, m_ResampleBuffer + 4, m_VoiceBuffer, true);
RenderVoice_Raw(PB, m_resample_buffer + 4, _Size);
Resample(PB, _Size, m_resample_buffer + 4, m_voice_buffer, true);
break;
default:
@@ -540,16 +540,16 @@ void ZeldaUCode::RenderAddVoice(ZeldaVoicePB &PB, s32* _LeftBuffer, s32* _RightB
// Synthesized sounds
case 0x0003: WARN_LOG(DSPHLE, "PB Format 0x03 used!");
case 0x0000: // Example: Magic meter filling up in ZWW
RenderSynth_RectWave(PB, m_VoiceBuffer, _Size);
RenderSynth_RectWave(PB, m_voice_buffer, _Size);
break;
case 0x0001: // Example: "Denied" sound when trying to pull out a sword indoors in ZWW
RenderSynth_SawWave(PB, m_VoiceBuffer, _Size);
RenderSynth_SawWave(PB, m_voice_buffer, _Size);
break;
case 0x0006:
WARN_LOG(DSPHLE, "Synthesizing 0x0006 (constant sound)");
RenderSynth_Constant(PB, m_VoiceBuffer, _Size);
RenderSynth_Constant(PB, m_voice_buffer, _Size);
break;
// These are more "synth" formats - square wave, saw wave etc.
@@ -561,12 +561,12 @@ void ZeldaUCode::RenderAddVoice(ZeldaVoicePB &PB, s32* _LeftBuffer, s32* _RightB
case 0x0007: // Example: "success" SFX in Pikmin 1, Pikmin 2 in a cave, not sure what sound it is.
case 0x000b: // Example: SFX in area selection menu in Pikmin
case 0x000c: // Example: beam of death/yellow force-field in Temple of the Gods, ZWW
RenderSynth_WaveTable(PB, m_VoiceBuffer, _Size);
RenderSynth_WaveTable(PB, m_voice_buffer, _Size);
break;
default:
// TODO: Implement general decoder here
memset(m_VoiceBuffer, 0, _Size * sizeof(s32));
memset(m_voice_buffer, 0, _Size * sizeof(s32));
ERROR_LOG(DSPHLE, "Unknown MixAddVoice format in zelda %04x", PB.Format);
break;
}
@@ -602,10 +602,10 @@ ContinueWithBlock:
short AX0H = PB.raw[0x28] & 0x7F;
short AX1L = AX0L ^ 0x7F;
short AX1H = AX0H ^ 0x7F;
AX0L = m_MiscTable[0x200 + AX0L];
AX0H = m_MiscTable[0x200 + AX0H];
AX1L = m_MiscTable[0x200 + AX1L];
AX1H = m_MiscTable[0x200 + AX1H];
AX0L = m_misc_table[0x200 + AX0L];
AX0H = m_misc_table[0x200 + AX0H];
AX1L = m_misc_table[0x200 + AX1L];
AX1H = m_misc_table[0x200 + AX1H];
short b00[20];
b00[0] = AX1L * AX1H >> 16;
@@ -649,7 +649,7 @@ ContinueWithBlock:
int ramp = value << 16;
for (int i = 0; i < _Size; i++)
{
int unmixed_audio = m_VoiceBuffer[i];
int unmixed_audio = m_voice_buffer[i];
switch (count) {
case 0: _LeftBuffer[i] += (u64)unmixed_audio * ramp >> 29; break;
case 1: _RightBuffer[i] += (u64)unmixed_audio * ramp >> 29; break;
@@ -703,7 +703,7 @@ ContinueWithBlock:
// 0ca9_RampedMultiplyAddBuffer
for (int i = 0; i < _Size; i++)
{
int value = m_VoiceBuffer[i];
int value = m_voice_buffer[i];
// TODO - add to buffer specified by dest_buffer_address
switch (count)
@@ -740,40 +740,40 @@ void ZeldaUCode::MixAudio()
const int BufferSamples = 5 * 16;
// Final mix buffers
memset(m_LeftBuffer, 0, BufferSamples * sizeof(s32));
memset(m_RightBuffer, 0, BufferSamples * sizeof(s32));
memset(m_left_buffer, 0, BufferSamples * sizeof(s32));
memset(m_right_buffer, 0, BufferSamples * sizeof(s32));
// For each PB...
for (u32 i = 0; i < m_NumVoices; i++)
for (u32 i = 0; i < m_num_voices; i++)
{
if (!IsLightVersion())
{
u32 flags = m_SyncFlags[(i >> 4) & 0xF];
u32 flags = m_sync_flags[(i >> 4) & 0xF];
if (!(flags & 1 << (15 - (i & 0xF))))
continue;
}
ZeldaVoicePB pb;
ReadVoicePB(m_VoicePBsAddr + (i * 0x180), pb);
ReadVoicePB(m_voice_pbs_addr + (i * 0x180), pb);
if (pb.Status == 0)
continue;
if (pb.KeyOff != 0)
continue;
RenderAddVoice(pb, m_LeftBuffer, m_RightBuffer, BufferSamples);
WritebackVoicePB(m_VoicePBsAddr + (i * 0x180), pb);
RenderAddVoice(pb, m_left_buffer, m_right_buffer, BufferSamples);
WritebackVoicePB(m_voice_pbs_addr + (i * 0x180), pb);
}
// Post processing, final conversion.
s16* left_buffer = (s16*)HLEMemory_Get_Pointer(m_LeftBuffersAddr);
s16* right_buffer = (s16*)HLEMemory_Get_Pointer(m_RightBuffersAddr);
left_buffer += m_CurBuffer * BufferSamples;
right_buffer += m_CurBuffer * BufferSamples;
s16* left_buffer = (s16*)HLEMemory_Get_Pointer(m_left_buffers_addr);
s16* right_buffer = (s16*)HLEMemory_Get_Pointer(m_right_buffers_addr);
left_buffer += m_current_buffer * BufferSamples;
right_buffer += m_current_buffer * BufferSamples;
for (int i = 0; i < BufferSamples; i++)
{
s32 left = m_LeftBuffer[i];
s32 right = m_RightBuffer[i];
s32 left = m_left_buffer[i];
s32 right = m_right_buffer[i];
MathUtil::Clamp(&left, -32768, 32767);
left_buffer[i] = Common::swap16((short)left);