From e9ab33b8713ae19a6d3ba5db0068750884b1f359 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Sat, 6 Jan 2024 07:04:36 +0100 Subject: [PATCH] Core/DSPHLE: Remove global System access in UCodes. --- Source/Core/Core/HW/DSPHLE/UCodes/AESnd.cpp | 70 +++++++------- Source/Core/Core/HW/DSPHLE/UCodes/ASnd.cpp | 96 +++++++++++--------- Source/Core/Core/HW/DSPHLE/UCodes/AX.cpp | 46 ++++++---- Source/Core/Core/HW/DSPHLE/UCodes/AX.h | 3 +- Source/Core/Core/HW/DSPHLE/UCodes/AXVoice.h | 10 +- Source/Core/Core/HW/DSPHLE/UCodes/AXWii.cpp | 37 +++++--- Source/Core/Core/HW/DSPHLE/UCodes/CARD.cpp | 2 +- Source/Core/Core/HW/DSPHLE/UCodes/GBA.cpp | 20 ++-- Source/Core/Core/HW/DSPHLE/UCodes/GBA.h | 7 +- Source/Core/Core/HW/DSPHLE/UCodes/ROM.cpp | 10 +- Source/Core/Core/HW/DSPHLE/UCodes/UCodes.cpp | 60 ++++-------- Source/Core/Core/HW/DSPHLE/UCodes/UCodes.h | 26 +++--- Source/Core/Core/HW/DSPHLE/UCodes/Zelda.cpp | 43 ++++++--- Source/Core/Core/HW/DSPHLE/UCodes/Zelda.h | 14 +++ 14 files changed, 245 insertions(+), 199 deletions(-) diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/AESnd.cpp b/Source/Core/Core/HW/DSPHLE/UCodes/AESnd.cpp index b4dbe7844b..8a71b8a355 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/AESnd.cpp +++ b/Source/Core/Core/HW/DSPHLE/UCodes/AESnd.cpp @@ -95,7 +95,7 @@ void AESndUCode::Update() // This is dubious in general, since we set the interrupt parameter on m_mail_handler.PushMail if (m_mail_handler.HasPending()) { - Core::System::GetInstance().GetDSP().GenerateDSPInterruptFromDSPEmu(DSP::INT_DSP); + m_dsphle->GetSystem().GetDSP().GenerateDSPInterruptFromDSPEmu(DSP::INT_DSP); } } @@ -154,14 +154,18 @@ void AESndUCode::HandleMail(u32 mail) // No mail is sent in response break; case MAIL_SEND_SAMPLES: + { DEBUG_LOG_FMT(DSPHLE, "AESndUCode - MAIL_SEND_SAMPLES"); // send_samples + auto& memory = m_dsphle->GetSystem().GetMemory(); for (u32 i = 0; i < NUM_OUTPUT_SAMPLES * 2; i++) { - HLEMemory_Write_U16(m_parameter_block.out_buf + i * sizeof(u16), m_output_buffer[i]); + HLEMemory_Write_U16(memory, m_parameter_block.out_buf + i * sizeof(u16), + m_output_buffer[i]); } m_mail_handler.PushMail(DSP_SYNC, true); break; + } case MAIL_TERMINATE: INFO_LOG_FMT(DSPHLE, "AESndUCode - MAIL_TERMINATE: {:08x}", mail); if (m_crc != HASH_2022_PAD && m_crc != HASH_2023) @@ -197,40 +201,42 @@ void AESndUCode::HandleMail(u32 mail) void AESndUCode::DMAInParameterBlock() { - m_parameter_block.out_buf = HLEMemory_Read_U32(m_parameter_block_addr + 0); - m_parameter_block.buf_start = HLEMemory_Read_U32(m_parameter_block_addr + 4); - m_parameter_block.buf_end = HLEMemory_Read_U32(m_parameter_block_addr + 8); - m_parameter_block.buf_curr = HLEMemory_Read_U32(m_parameter_block_addr + 12); - m_parameter_block.yn1 = HLEMemory_Read_U16(m_parameter_block_addr + 16); - m_parameter_block.yn2 = HLEMemory_Read_U16(m_parameter_block_addr + 18); - m_parameter_block.pds = HLEMemory_Read_U16(m_parameter_block_addr + 20); - m_parameter_block.freq = HLEMemory_Read_U32(m_parameter_block_addr + 22); - m_parameter_block.counter = HLEMemory_Read_U16(m_parameter_block_addr + 26); - m_parameter_block.left = HLEMemory_Read_U16(m_parameter_block_addr + 28); - m_parameter_block.right = HLEMemory_Read_U16(m_parameter_block_addr + 30); - m_parameter_block.volume_l = HLEMemory_Read_U16(m_parameter_block_addr + 32); - m_parameter_block.volume_r = HLEMemory_Read_U16(m_parameter_block_addr + 34); - m_parameter_block.delay = HLEMemory_Read_U32(m_parameter_block_addr + 36); - m_parameter_block.flags = HLEMemory_Read_U32(m_parameter_block_addr + 40); + auto& memory = m_dsphle->GetSystem().GetMemory(); + m_parameter_block.out_buf = HLEMemory_Read_U32(memory, m_parameter_block_addr + 0); + m_parameter_block.buf_start = HLEMemory_Read_U32(memory, m_parameter_block_addr + 4); + m_parameter_block.buf_end = HLEMemory_Read_U32(memory, m_parameter_block_addr + 8); + m_parameter_block.buf_curr = HLEMemory_Read_U32(memory, m_parameter_block_addr + 12); + m_parameter_block.yn1 = HLEMemory_Read_U16(memory, m_parameter_block_addr + 16); + m_parameter_block.yn2 = HLEMemory_Read_U16(memory, m_parameter_block_addr + 18); + m_parameter_block.pds = HLEMemory_Read_U16(memory, m_parameter_block_addr + 20); + m_parameter_block.freq = HLEMemory_Read_U32(memory, m_parameter_block_addr + 22); + m_parameter_block.counter = HLEMemory_Read_U16(memory, m_parameter_block_addr + 26); + m_parameter_block.left = HLEMemory_Read_U16(memory, m_parameter_block_addr + 28); + m_parameter_block.right = HLEMemory_Read_U16(memory, m_parameter_block_addr + 30); + m_parameter_block.volume_l = HLEMemory_Read_U16(memory, m_parameter_block_addr + 32); + m_parameter_block.volume_r = HLEMemory_Read_U16(memory, m_parameter_block_addr + 34); + m_parameter_block.delay = HLEMemory_Read_U32(memory, m_parameter_block_addr + 36); + m_parameter_block.flags = HLEMemory_Read_U32(memory, m_parameter_block_addr + 40); } void AESndUCode::DMAOutParameterBlock() { - HLEMemory_Write_U32(m_parameter_block_addr + 0, m_parameter_block.out_buf); - HLEMemory_Write_U32(m_parameter_block_addr + 4, m_parameter_block.buf_start); - HLEMemory_Write_U32(m_parameter_block_addr + 8, m_parameter_block.buf_end); - HLEMemory_Write_U32(m_parameter_block_addr + 12, m_parameter_block.buf_curr); - HLEMemory_Write_U16(m_parameter_block_addr + 16, m_parameter_block.yn1); - HLEMemory_Write_U16(m_parameter_block_addr + 18, m_parameter_block.yn2); - HLEMemory_Write_U16(m_parameter_block_addr + 20, m_parameter_block.pds); - HLEMemory_Write_U32(m_parameter_block_addr + 22, m_parameter_block.freq); - HLEMemory_Write_U16(m_parameter_block_addr + 26, m_parameter_block.counter); - HLEMemory_Write_U16(m_parameter_block_addr + 28, m_parameter_block.left); - HLEMemory_Write_U16(m_parameter_block_addr + 30, m_parameter_block.right); - HLEMemory_Write_U16(m_parameter_block_addr + 32, m_parameter_block.volume_l); - HLEMemory_Write_U16(m_parameter_block_addr + 34, m_parameter_block.volume_r); - HLEMemory_Write_U32(m_parameter_block_addr + 36, m_parameter_block.delay); - HLEMemory_Write_U32(m_parameter_block_addr + 40, m_parameter_block.flags); + auto& memory = m_dsphle->GetSystem().GetMemory(); + HLEMemory_Write_U32(memory, m_parameter_block_addr + 0, m_parameter_block.out_buf); + HLEMemory_Write_U32(memory, m_parameter_block_addr + 4, m_parameter_block.buf_start); + HLEMemory_Write_U32(memory, m_parameter_block_addr + 8, m_parameter_block.buf_end); + HLEMemory_Write_U32(memory, m_parameter_block_addr + 12, m_parameter_block.buf_curr); + HLEMemory_Write_U16(memory, m_parameter_block_addr + 16, m_parameter_block.yn1); + HLEMemory_Write_U16(memory, m_parameter_block_addr + 18, m_parameter_block.yn2); + HLEMemory_Write_U16(memory, m_parameter_block_addr + 20, m_parameter_block.pds); + HLEMemory_Write_U32(memory, m_parameter_block_addr + 22, m_parameter_block.freq); + HLEMemory_Write_U16(memory, m_parameter_block_addr + 26, m_parameter_block.counter); + HLEMemory_Write_U16(memory, m_parameter_block_addr + 28, m_parameter_block.left); + HLEMemory_Write_U16(memory, m_parameter_block_addr + 30, m_parameter_block.right); + HLEMemory_Write_U16(memory, m_parameter_block_addr + 32, m_parameter_block.volume_l); + HLEMemory_Write_U16(memory, m_parameter_block_addr + 34, m_parameter_block.volume_r); + HLEMemory_Write_U32(memory, m_parameter_block_addr + 36, m_parameter_block.delay); + HLEMemory_Write_U32(memory, m_parameter_block_addr + 40, m_parameter_block.flags); } void AESndAccelerator::OnEndException() diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/ASnd.cpp b/Source/Core/Core/HW/DSPHLE/UCodes/ASnd.cpp index 2fcc5510b3..bb255dfb5b 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/ASnd.cpp +++ b/Source/Core/Core/HW/DSPHLE/UCodes/ASnd.cpp @@ -80,7 +80,7 @@ void ASndUCode::Update() // This is dubious in general, since we set the interrupt parameter on m_mail_handler.PushMail if (m_mail_handler.HasPending()) { - Core::System::GetInstance().GetDSP().GenerateDSPInterruptFromDSPEmu(DSP::INT_DSP); + m_dsphle->GetSystem().GetDSP().GenerateDSPInterruptFromDSPEmu(DSP::INT_DSP); } } @@ -128,17 +128,20 @@ void ASndUCode::HandleMail(u32 mail) // Mail is handled by DoMixing() break; case MAIL_INPUT_SAMPLES_2: + { WARN_LOG_FMT(DSPHLE, "ASndUCode - MAIL_INPUT_SAMPLES_2: {:08x} - not normally used", mail); // input_samples2 DMAInVoiceData(); // first do_dma call // second do_dma call + auto& memory = m_dsphle->GetSystem().GetMemory(); for (u32 i = 0; i < NUM_OUTPUT_SAMPLES * 2; i++) { - m_output_buffer[i] = HLEMemory_Read_U16(m_current_voice.out_buf + i * sizeof(u16)); + m_output_buffer[i] = HLEMemory_Read_U16(memory, m_current_voice.out_buf + i * sizeof(u16)); } DoMixing(DSP_SYNC); // Mail is handled by DoMixing() break; + } case MAIL_SET_VOICE_DATA_BUFFER: DEBUG_LOG_FMT(DSPHLE, "ASndUCode - MAIL_SET_VOICE_DATA_BUFFER: {:08x}", mail); m_next_mail_is_voice_addr = true; @@ -153,13 +156,16 @@ void ASndUCode::HandleMail(u32 mail) // Mail is handled by DoMixing() break; case MAIN_SEND_SAMPLES: + { DEBUG_LOG_FMT(DSPHLE, "ASndUCode - MAIN_SEND_SAMPLES: {:08x}", mail); + auto& memory = m_dsphle->GetSystem().GetMemory(); for (u32 i = 0; i < NUM_OUTPUT_SAMPLES * 2; i++) { - HLEMemory_Write_U16(m_current_voice.out_buf + i * sizeof(u16), m_output_buffer[i]); + HLEMemory_Write_U16(memory, m_current_voice.out_buf + i * sizeof(u16), m_output_buffer[i]); } m_mail_handler.PushMail(DSP_SYNC, true); break; + } case MAIL_ROM_DUMP_WORD: WARN_LOG_FMT(DSPHLE, "ASndUCode - MAIL_ROM_DUMP_WORD: {:08x} - not normally used", mail); // Reads instruction at 0x8000 | (mail >> 16), and sends it back in DMBL. DMBH is 0. @@ -196,51 +202,53 @@ void ASndUCode::HandleMail(u32 mail) void ASndUCode::DMAInVoiceData() { - m_current_voice.out_buf = HLEMemory_Read_U32(m_voice_addr); - m_current_voice.delay_samples = HLEMemory_Read_U32(m_voice_addr + 4); - u32 new_flags = HLEMemory_Read_U32(m_voice_addr + 8); + auto& memory = m_dsphle->GetSystem().GetMemory(); + m_current_voice.out_buf = HLEMemory_Read_U32(memory, m_voice_addr); + m_current_voice.delay_samples = HLEMemory_Read_U32(memory, m_voice_addr + 4); + u32 new_flags = HLEMemory_Read_U32(memory, m_voice_addr + 8); if (m_current_voice.flags != new_flags) DEBUG_LOG_FMT(DSPHLE, "ASndUCode - flags: {:08x}", new_flags); m_current_voice.flags = new_flags; - m_current_voice.start_addr = HLEMemory_Read_U32(m_voice_addr + 12); - m_current_voice.end_addr = HLEMemory_Read_U32(m_voice_addr + 16); - m_current_voice.freq = HLEMemory_Read_U32(m_voice_addr + 20); - m_current_voice.left = HLEMemory_Read_U16(m_voice_addr + 24); - m_current_voice.right = HLEMemory_Read_U16(m_voice_addr + 26); - m_current_voice.counter = HLEMemory_Read_U32(m_voice_addr + 28); - m_current_voice.volume_l = HLEMemory_Read_U16(m_voice_addr + 32); - m_current_voice.volume_r = HLEMemory_Read_U16(m_voice_addr + 34); - m_current_voice.start_addr2 = HLEMemory_Read_U32(m_voice_addr + 36); - m_current_voice.end_addr2 = HLEMemory_Read_U32(m_voice_addr + 40); - m_current_voice.volume2_l = HLEMemory_Read_U16(m_voice_addr + 44); - m_current_voice.volume2_r = HLEMemory_Read_U16(m_voice_addr + 46); - m_current_voice.backup_addr = HLEMemory_Read_U32(m_voice_addr + 48); - m_current_voice.tick_counter = HLEMemory_Read_U32(m_voice_addr + 52); - m_current_voice.cb = HLEMemory_Read_U32(m_voice_addr + 56); - m_current_voice._pad = HLEMemory_Read_U32(m_voice_addr + 60); + m_current_voice.start_addr = HLEMemory_Read_U32(memory, m_voice_addr + 12); + m_current_voice.end_addr = HLEMemory_Read_U32(memory, m_voice_addr + 16); + m_current_voice.freq = HLEMemory_Read_U32(memory, m_voice_addr + 20); + m_current_voice.left = HLEMemory_Read_U16(memory, m_voice_addr + 24); + m_current_voice.right = HLEMemory_Read_U16(memory, m_voice_addr + 26); + m_current_voice.counter = HLEMemory_Read_U32(memory, m_voice_addr + 28); + m_current_voice.volume_l = HLEMemory_Read_U16(memory, m_voice_addr + 32); + m_current_voice.volume_r = HLEMemory_Read_U16(memory, m_voice_addr + 34); + m_current_voice.start_addr2 = HLEMemory_Read_U32(memory, m_voice_addr + 36); + m_current_voice.end_addr2 = HLEMemory_Read_U32(memory, m_voice_addr + 40); + m_current_voice.volume2_l = HLEMemory_Read_U16(memory, m_voice_addr + 44); + m_current_voice.volume2_r = HLEMemory_Read_U16(memory, m_voice_addr + 46); + m_current_voice.backup_addr = HLEMemory_Read_U32(memory, m_voice_addr + 48); + m_current_voice.tick_counter = HLEMemory_Read_U32(memory, m_voice_addr + 52); + m_current_voice.cb = HLEMemory_Read_U32(memory, m_voice_addr + 56); + m_current_voice._pad = HLEMemory_Read_U32(memory, m_voice_addr + 60); } void ASndUCode::DMAOutVoiceData() { - HLEMemory_Write_U32(m_voice_addr, m_current_voice.out_buf); - HLEMemory_Write_U32(m_voice_addr + 4, m_current_voice.delay_samples); - HLEMemory_Write_U32(m_voice_addr + 8, m_current_voice.flags); - HLEMemory_Write_U32(m_voice_addr + 12, m_current_voice.start_addr); - HLEMemory_Write_U32(m_voice_addr + 16, m_current_voice.end_addr); - HLEMemory_Write_U32(m_voice_addr + 20, m_current_voice.freq); - HLEMemory_Write_U16(m_voice_addr + 24, m_current_voice.left); - HLEMemory_Write_U16(m_voice_addr + 26, m_current_voice.right); - HLEMemory_Write_U32(m_voice_addr + 28, m_current_voice.counter); - HLEMemory_Write_U16(m_voice_addr + 32, m_current_voice.volume_l); - HLEMemory_Write_U16(m_voice_addr + 34, m_current_voice.volume_r); - HLEMemory_Write_U32(m_voice_addr + 36, m_current_voice.start_addr2); - HLEMemory_Write_U32(m_voice_addr + 40, m_current_voice.end_addr2); - HLEMemory_Write_U16(m_voice_addr + 44, m_current_voice.volume2_l); - HLEMemory_Write_U16(m_voice_addr + 46, m_current_voice.volume2_r); - HLEMemory_Write_U32(m_voice_addr + 48, m_current_voice.backup_addr); - HLEMemory_Write_U32(m_voice_addr + 52, m_current_voice.tick_counter); - HLEMemory_Write_U32(m_voice_addr + 56, m_current_voice.cb); - HLEMemory_Write_U32(m_voice_addr + 60, m_current_voice._pad); + auto& memory = m_dsphle->GetSystem().GetMemory(); + HLEMemory_Write_U32(memory, m_voice_addr, m_current_voice.out_buf); + HLEMemory_Write_U32(memory, m_voice_addr + 4, m_current_voice.delay_samples); + HLEMemory_Write_U32(memory, m_voice_addr + 8, m_current_voice.flags); + HLEMemory_Write_U32(memory, m_voice_addr + 12, m_current_voice.start_addr); + HLEMemory_Write_U32(memory, m_voice_addr + 16, m_current_voice.end_addr); + HLEMemory_Write_U32(memory, m_voice_addr + 20, m_current_voice.freq); + HLEMemory_Write_U16(memory, m_voice_addr + 24, m_current_voice.left); + HLEMemory_Write_U16(memory, m_voice_addr + 26, m_current_voice.right); + HLEMemory_Write_U32(memory, m_voice_addr + 28, m_current_voice.counter); + HLEMemory_Write_U16(memory, m_voice_addr + 32, m_current_voice.volume_l); + HLEMemory_Write_U16(memory, m_voice_addr + 34, m_current_voice.volume_r); + HLEMemory_Write_U32(memory, m_voice_addr + 36, m_current_voice.start_addr2); + HLEMemory_Write_U32(memory, m_voice_addr + 40, m_current_voice.end_addr2); + HLEMemory_Write_U16(memory, m_voice_addr + 44, m_current_voice.volume2_l); + HLEMemory_Write_U16(memory, m_voice_addr + 46, m_current_voice.volume2_r); + HLEMemory_Write_U32(memory, m_voice_addr + 48, m_current_voice.backup_addr); + HLEMemory_Write_U32(memory, m_voice_addr + 52, m_current_voice.tick_counter); + HLEMemory_Write_U32(memory, m_voice_addr + 56, m_current_voice.cb); + HLEMemory_Write_U32(memory, m_voice_addr + 60, m_current_voice._pad); } void ASndUCode::DoMixing(u32 return_mail) @@ -449,9 +457,10 @@ void ASndUCode::DMAInSampleData() // The only difference is that this one forces the address to be aligned, while when // jump_load_smp_dma is used, the address is expected to already be aligned. const u32 addr = m_current_voice.start_addr & ~INPUT_SAMPLE_BUFFER_BYTE_MASK; + auto& memory = m_dsphle->GetSystem().GetMemory(); for (u16 i = 0; i < INPUT_SAMPLE_BUFFER_SIZE_WORDS; i++) { - m_input_sample_buffer[i] = HLEMemory_Read_U16(addr + i * sizeof(u16)); + m_input_sample_buffer[i] = HLEMemory_Read_U16(memory, addr + i * sizeof(u16)); } } @@ -461,9 +470,10 @@ void ASndUCode::DMAInSampleDataAssumeAligned() // This is technically not a function, but instead is directly jumped to and then jumps to $ar3 // (which is set to an address from sample_selector). We can just treat it as a function though. const u32 addr = m_current_voice.start_addr; + auto& memory = m_dsphle->GetSystem().GetMemory(); for (u16 i = 0; i < INPUT_SAMPLE_BUFFER_SIZE_WORDS; i++) { - m_input_sample_buffer[i] = HLEMemory_Read_U16(addr + i * sizeof(u16)); + m_input_sample_buffer[i] = HLEMemory_Read_U16(memory, addr + i * sizeof(u16)); } } diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/AX.cpp b/Source/Core/Core/HW/DSPHLE/UCodes/AX.cpp index 43c0bad0ec..d921e2d1ba 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/AX.cpp +++ b/Source/Core/Core/HW/DSPHLE/UCodes/AX.cpp @@ -390,9 +390,10 @@ void AXUCode::DownloadAndMixWithVolume(u32 addr, u16 vol_main, u16 vol_auxa, u16 int** buffers[3] = {buffers_main, buffers_auxa, buffers_auxb}; u16 volumes[3] = {vol_main, vol_auxa, vol_auxb}; + auto& memory = m_dsphle->GetSystem().GetMemory(); for (u32 i = 0; i < 3; ++i) { - int* ptr = (int*)HLEMemory_Get_Pointer(addr); + int* ptr = (int*)HLEMemory_Get_Pointer(memory, addr); u16 volume = volumes[i]; for (u32 j = 0; j < 3; ++j) { @@ -415,16 +416,17 @@ void AXUCode::ProcessPBList(u32 pb_addr) AXPB pb; + auto& memory = m_dsphle->GetSystem().GetMemory(); while (pb_addr) { AXBuffers buffers = {{m_samples_main_left, m_samples_main_right, m_samples_main_surround, m_samples_auxA_left, m_samples_auxA_right, m_samples_auxA_surround, m_samples_auxB_left, m_samples_auxB_right, m_samples_auxB_surround}}; - ReadPB(pb_addr, pb, m_crc); + ReadPB(memory, pb_addr, pb, m_crc); u32 updates_addr = HILO_TO_32(pb.updates.data); - u16* updates = (u16*)HLEMemory_Get_Pointer(updates_addr); + u16* updates = (u16*)HLEMemory_Get_Pointer(memory, updates_addr); for (int curr_ms = 0; curr_ms < 5; ++curr_ms) { @@ -439,7 +441,7 @@ void AXUCode::ProcessPBList(u32 pb_addr) ptr += spms; } - WritePB(pb_addr, pb, m_crc); + WritePB(memory, pb_addr, pb, m_crc); pb_addr = HILO_TO_32(pb.next_pb); } } @@ -464,9 +466,10 @@ void AXUCode::MixAUXSamples(int aux_id, u32 write_addr, u32 read_addr) } // First, we need to send the contents of our AUX buffers to the CPU. + auto& memory = m_dsphle->GetSystem().GetMemory(); if (write_addr) { - int* ptr = (int*)HLEMemory_Get_Pointer(write_addr); + int* ptr = (int*)HLEMemory_Get_Pointer(memory, write_addr); for (auto& buffer : buffers) for (u32 j = 0; j < 5 * 32; ++j) *ptr++ = Common::swap32(buffer[j]); @@ -474,7 +477,7 @@ void AXUCode::MixAUXSamples(int aux_id, u32 write_addr, u32 read_addr) // Then, we read the new temp from the CPU and add to our current // temp. - int* ptr = (int*)HLEMemory_Get_Pointer(read_addr); + int* ptr = (int*)HLEMemory_Get_Pointer(memory, read_addr); for (auto& sample : m_samples_main_left) sample += (int)Common::swap32(*ptr++); for (auto& sample : m_samples_main_right) @@ -493,12 +496,13 @@ void AXUCode::UploadLRS(u32 dst_addr) buffers[1][i] = Common::swap32(m_samples_main_right[i]); buffers[2][i] = Common::swap32(m_samples_main_surround[i]); } - memcpy(HLEMemory_Get_Pointer(dst_addr), buffers, sizeof(buffers)); + memcpy(HLEMemory_Get_Pointer(m_dsphle->GetSystem().GetMemory(), dst_addr), buffers, + sizeof(buffers)); } void AXUCode::SetMainLR(u32 src_addr) { - int* ptr = (int*)HLEMemory_Get_Pointer(src_addr); + int* ptr = (int*)HLEMemory_Get_Pointer(m_dsphle->GetSystem().GetMemory(), src_addr); for (u32 i = 0; i < 5 * 32; ++i) { int samp = (int)Common::swap32(*ptr++); @@ -545,7 +549,8 @@ void AXUCode::RunCompressor(u16 threshold, u16 release_frames, u32 table_addr, u } // apply the selected ramp - u16* ramp = (u16*)HLEMemory_Get_Pointer(table_addr + table_offset); + auto& memory = m_dsphle->GetSystem().GetMemory(); + u16* ramp = (u16*)HLEMemory_Get_Pointer(memory, table_addr + table_offset); for (u32 i = 0; i < 32 * millis; ++i) { u16 coef = Common::swap16(*ramp++); @@ -560,7 +565,8 @@ void AXUCode::OutputSamples(u32 lr_addr, u32 surround_addr) for (u32 i = 0; i < 5 * 32; ++i) surround_buffer[i] = Common::swap32(m_samples_main_surround[i]); - memcpy(HLEMemory_Get_Pointer(surround_addr), surround_buffer, sizeof(surround_buffer)); + auto& memory = m_dsphle->GetSystem().GetMemory(); + memcpy(HLEMemory_Get_Pointer(memory, surround_addr), surround_buffer, sizeof(surround_buffer)); // 32 samples per ms, 5 ms, 2 channels short buffer[5 * 32 * 2]; @@ -575,20 +581,21 @@ void AXUCode::OutputSamples(u32 lr_addr, u32 surround_addr) buffer[2 * i + 1] = Common::swap16(left); } - memcpy(HLEMemory_Get_Pointer(lr_addr), buffer, sizeof(buffer)); + memcpy(HLEMemory_Get_Pointer(memory, lr_addr), buffer, sizeof(buffer)); } void AXUCode::MixAUXBLR(u32 ul_addr, u32 dl_addr) { // Upload AUXB L/R - int* ptr = (int*)HLEMemory_Get_Pointer(ul_addr); + auto& memory = m_dsphle->GetSystem().GetMemory(); + int* ptr = (int*)HLEMemory_Get_Pointer(memory, ul_addr); for (auto& sample : m_samples_auxB_left) *ptr++ = Common::swap32(sample); for (auto& sample : m_samples_auxB_right) *ptr++ = Common::swap32(sample); // Mix AUXB L/R to MAIN L/R, and replace AUXB L/R - ptr = (int*)HLEMemory_Get_Pointer(dl_addr); + ptr = (int*)HLEMemory_Get_Pointer(memory, dl_addr); for (u32 i = 0; i < 5 * 32; ++i) { int samp = Common::swap32(*ptr++); @@ -605,7 +612,8 @@ void AXUCode::MixAUXBLR(u32 ul_addr, u32 dl_addr) void AXUCode::SetOppositeLR(u32 src_addr) { - int* ptr = (int*)HLEMemory_Get_Pointer(src_addr); + auto& memory = m_dsphle->GetSystem().GetMemory(); + int* ptr = (int*)HLEMemory_Get_Pointer(memory, src_addr); for (u32 i = 0; i < 5 * 32; ++i) { int inp = Common::swap32(*ptr++); @@ -626,7 +634,8 @@ void AXUCode::SendAUXAndMix(u32 auxa_lrs_up, u32 auxb_s_up, u32 main_l_dl, u32 m }; // Upload AUXA LRS - int* ptr = (int*)HLEMemory_Get_Pointer(auxa_lrs_up); + auto& memory = m_dsphle->GetSystem().GetMemory(); + int* ptr = (int*)HLEMemory_Get_Pointer(memory, auxa_lrs_up); for (const auto& up_buffer : up_buffers) { for (u32 j = 0; j < 32 * 5; ++j) @@ -634,7 +643,7 @@ void AXUCode::SendAUXAndMix(u32 auxa_lrs_up, u32 auxb_s_up, u32 main_l_dl, u32 m } // Upload AUXB S - ptr = (int*)HLEMemory_Get_Pointer(auxb_s_up); + ptr = (int*)HLEMemory_Get_Pointer(memory, auxb_s_up); for (auto& sample : m_samples_auxB_surround) *ptr++ = Common::swap32(sample); @@ -655,7 +664,7 @@ void AXUCode::SendAUXAndMix(u32 auxa_lrs_up, u32 auxb_s_up, u32 main_l_dl, u32 m // Download and mix for (size_t i = 0; i < dl_buffers.size(); ++i) { - const int* dl_src = (int*)HLEMemory_Get_Pointer(dl_addrs[i]); + const int* dl_src = (int*)HLEMemory_Get_Pointer(memory, dl_addrs[i]); for (size_t j = 0; j < 32 * 5; ++j) dl_buffers[i][j] += (int)Common::swap32(*dl_src++); } @@ -743,8 +752,9 @@ void AXUCode::CopyCmdList(u32 addr, u16 size) return; } + auto& memory = m_dsphle->GetSystem().GetMemory(); for (u32 i = 0; i < size; ++i, addr += 2) - m_cmdlist[i] = HLEMemory_Read_U16(addr); + m_cmdlist[i] = HLEMemory_Read_U16(memory, addr); } void AXUCode::Update() diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/AX.h b/Source/Core/Core/HW/DSPHLE/UCodes/AX.h index 1aabf2cc63..76951d5f68 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/AX.h +++ b/Source/Core/Core/HW/DSPHLE/UCodes/AX.h @@ -18,6 +18,7 @@ #include "Common/BitUtils.h" #include "Common/CommonTypes.h" #include "Common/Swap.h" +#include "Core/HW/DSPHLE/DSPHLE.h" #include "Core/HW/DSPHLE/UCodes/UCodes.h" #include "Core/HW/Memmap.h" #include "Core/System.h" @@ -154,7 +155,7 @@ protected: template void InitMixingBuffers(u32 init_addr, const std::array& buffers) { - auto& system = Core::System::GetInstance(); + auto& system = m_dsphle->GetSystem(); auto& memory = system.GetMemory(); std::array init_array; memory.CopyFromEmuSwapped(init_array.data(), init_addr, sizeof(init_array)); diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/AXVoice.h b/Source/Core/Core/HW/DSPHLE/UCodes/AXVoice.h index 77dbae95d5..2e055608a2 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/AXVoice.h +++ b/Source/Core/Core/HW/DSPHLE/UCodes/AXVoice.h @@ -100,11 +100,8 @@ bool HasLpf(u32 crc) } // Read a PB from MRAM/ARAM -void ReadPB(u32 addr, PB_TYPE& pb, u32 crc) +void ReadPB(Memory::MemoryManager& memory, u32 addr, PB_TYPE& pb, u32 crc) { - auto& system = Core::System::GetInstance(); - auto& memory = system.GetMemory(); - if (HasLpf(crc)) { u16* dst = (u16*)&pb; @@ -127,11 +124,8 @@ void ReadPB(u32 addr, PB_TYPE& pb, u32 crc) } // Write a PB back to MRAM/ARAM -void WritePB(u32 addr, const PB_TYPE& pb, u32 crc) +void WritePB(Memory::MemoryManager& memory, u32 addr, const PB_TYPE& pb, u32 crc) { - auto& system = Core::System::GetInstance(); - auto& memory = system.GetMemory(); - if (HasLpf(crc)) { const u16* src = (const u16*)&pb; diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/AXWii.cpp b/Source/Core/Core/HW/DSPHLE/UCodes/AXWii.cpp index cde059a716..094327c3c5 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/AXWii.cpp +++ b/Source/Core/Core/HW/DSPHLE/UCodes/AXWii.cpp @@ -271,7 +271,8 @@ void AXWiiUCode::SetupProcessing(u32 init_addr) void AXWiiUCode::AddToLR(u32 val_addr, bool neg) { - int* ptr = (int*)HLEMemory_Get_Pointer(val_addr); + auto& memory = m_dsphle->GetSystem().GetMemory(); + int* ptr = (int*)HLEMemory_Get_Pointer(memory, val_addr); for (int i = 0; i < 32 * 3; ++i) { int val = (int)Common::swap32(*ptr++); @@ -285,7 +286,8 @@ void AXWiiUCode::AddToLR(u32 val_addr, bool neg) void AXWiiUCode::AddSubToLR(u32 val_addr) { - int* ptr = (int*)HLEMemory_Get_Pointer(val_addr); + auto& memory = m_dsphle->GetSystem().GetMemory(); + int* ptr = (int*)HLEMemory_Get_Pointer(memory, val_addr); for (int i = 0; i < 32 * 3; ++i) { int val = (int)Common::swap32(*ptr++); @@ -371,7 +373,8 @@ bool AXWiiUCode::ExtractUpdatesFields(AXPBWii& pb, u16* num_updates, u16* update u16 addr_hi = pb_mem[44]; u16 addr_lo = pb_mem[45]; u32 addr = HILO_TO_32(addr); - u16* ptr = (u16*)HLEMemory_Get_Pointer(addr); + auto& memory = m_dsphle->GetSystem().GetMemory(); + u16* ptr = (u16*)HLEMemory_Get_Pointer(memory, addr); *updates_addr = addr; @@ -423,6 +426,7 @@ void AXWiiUCode::ProcessPBList(u32 pb_addr) AXPBWii pb; + auto& memory = m_dsphle->GetSystem().GetMemory(); while (pb_addr) { AXBuffers buffers = {{m_samples_main_left, m_samples_main_right, m_samples_main_surround, @@ -433,7 +437,7 @@ void AXWiiUCode::ProcessPBList(u32 pb_addr) m_samples_aux1, m_samples_wm2, m_samples_aux2, m_samples_wm3, m_samples_aux3}}; - ReadPB(pb_addr, pb, m_crc); + ReadPB(memory, pb_addr, pb, m_crc); u16 num_updates[3]; u16 updates[1024]; @@ -460,7 +464,7 @@ void AXWiiUCode::ProcessPBList(u32 pb_addr) m_coeffs_checksum ? m_coeffs.data() : nullptr); } - WritePB(pb_addr, pb, m_crc); + WritePB(memory, pb_addr, pb, m_crc); pb_addr = HILO_TO_32(pb.next_pb); } } @@ -506,9 +510,10 @@ void AXWiiUCode::MixAUXSamples(int aux_id, u32 write_addr, u32 read_addr, u16 vo } // Send the content of AUX buffers to the CPU + auto& memory = m_dsphle->GetSystem().GetMemory(); if (write_addr) { - int* ptr = (int*)HLEMemory_Get_Pointer(write_addr); + int* ptr = (int*)HLEMemory_Get_Pointer(memory, write_addr); for (const auto& buffer : buffers) { for (u32 j = 0; j < 3 * 32; ++j) @@ -517,7 +522,7 @@ void AXWiiUCode::MixAUXSamples(int aux_id, u32 write_addr, u32 read_addr, u16 vo } // Then read the buffers from the CPU and add to our main buffers. - const int* ptr = (int*)HLEMemory_Get_Pointer(read_addr); + const int* ptr = (int*)HLEMemory_Get_Pointer(memory, read_addr); for (auto& main_buffer : main_buffers) { for (u32 j = 0; j < 3 * 32; ++j) @@ -536,7 +541,8 @@ void AXWiiUCode::UploadAUXMixLRSC(int aux_id, u32* addresses, u16 volume) int* aux_surround = aux_id ? m_samples_auxB_surround : m_samples_auxA_surround; int* auxc_buffer = aux_id ? m_samples_auxC_surround : m_samples_auxC_right; - int* upload_ptr = (int*)HLEMemory_Get_Pointer(addresses[0]); + auto& memory = m_dsphle->GetSystem().GetMemory(); + int* upload_ptr = (int*)HLEMemory_Get_Pointer(memory, addresses[0]); for (u32 i = 0; i < 96; ++i) *upload_ptr++ = Common::swap32(aux_left[i]); for (u32 i = 0; i < 96; ++i) @@ -544,7 +550,7 @@ void AXWiiUCode::UploadAUXMixLRSC(int aux_id, u32* addresses, u16 volume) for (u32 i = 0; i < 96; ++i) *upload_ptr++ = Common::swap32(aux_surround[i]); - upload_ptr = (int*)HLEMemory_Get_Pointer(addresses[1]); + upload_ptr = (int*)HLEMemory_Get_Pointer(memory, addresses[1]); for (u32 i = 0; i < 96; ++i) *upload_ptr++ = Common::swap32(auxc_buffer[i]); @@ -556,7 +562,7 @@ void AXWiiUCode::UploadAUXMixLRSC(int aux_id, u32* addresses, u16 volume) m_samples_auxC_left}; for (u32 mix_i = 0; mix_i < 4; ++mix_i) { - int* dl_ptr = (int*)HLEMemory_Get_Pointer(addresses[2 + mix_i]); + int* dl_ptr = (int*)HLEMemory_Get_Pointer(memory, addresses[2 + mix_i]); for (u32 i = 0; i < 96; ++i) aux_left[i] = Common::swap32(dl_ptr[i]); @@ -579,14 +585,16 @@ void AXWiiUCode::OutputSamples(u32 lr_addr, u32 surround_addr, u16 volume, bool for (size_t i = 0; i < upload_buffer.size(); ++i) upload_buffer[i] = Common::swap32(m_samples_main_surround[i]); - memcpy(HLEMemory_Get_Pointer(surround_addr), upload_buffer.data(), sizeof(upload_buffer)); + auto& memory = m_dsphle->GetSystem().GetMemory(); + memcpy(HLEMemory_Get_Pointer(memory, surround_addr), upload_buffer.data(), sizeof(upload_buffer)); if (upload_auxc) { surround_addr += sizeof(upload_buffer); for (size_t i = 0; i < upload_buffer.size(); ++i) upload_buffer[i] = Common::swap32(m_samples_auxC_left[i]); - memcpy(HLEMemory_Get_Pointer(surround_addr), upload_buffer.data(), sizeof(upload_buffer)); + memcpy(HLEMemory_Get_Pointer(memory, surround_addr), upload_buffer.data(), + sizeof(upload_buffer)); } // Clamp internal buffers to 16 bits. @@ -610,7 +618,7 @@ void AXWiiUCode::OutputSamples(u32 lr_addr, u32 surround_addr, u16 volume, bool buffer[2 * i + 1] = Common::swap16(m_samples_main_left[i]); } - memcpy(HLEMemory_Get_Pointer(lr_addr), buffer.data(), sizeof(buffer)); + memcpy(HLEMemory_Get_Pointer(memory, lr_addr), buffer.data(), sizeof(buffer)); m_mail_handler.PushMail(DSP_SYNC, true); } @@ -618,10 +626,11 @@ void AXWiiUCode::OutputWMSamples(u32* addresses) { int* buffers[] = {m_samples_wm0, m_samples_wm1, m_samples_wm2, m_samples_wm3}; + auto& memory = m_dsphle->GetSystem().GetMemory(); for (u32 i = 0; i < 4; ++i) { int* in = buffers[i]; - u16* out = (u16*)HLEMemory_Get_Pointer(addresses[i]); + u16* out = (u16*)HLEMemory_Get_Pointer(memory, addresses[i]); for (u32 j = 0; j < 3 * 6; ++j) { int sample = std::clamp(in[j], -32767, 32767); diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/CARD.cpp b/Source/Core/Core/HW/DSPHLE/UCodes/CARD.cpp index 883467a86c..db62da5695 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/CARD.cpp +++ b/Source/Core/Core/HW/DSPHLE/UCodes/CARD.cpp @@ -28,7 +28,7 @@ void CARDUCode::Update() // check if we have something to send if (m_mail_handler.HasPending()) { - Core::System::GetInstance().GetDSP().GenerateDSPInterruptFromDSPEmu(DSP::INT_DSP); + m_dsphle->GetSystem().GetDSP().GenerateDSPInterruptFromDSPEmu(DSP::INT_DSP); } } diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/GBA.cpp b/Source/Core/Core/HW/DSPHLE/UCodes/GBA.cpp index 1eb256e1b8..9ddd6801ca 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/GBA.cpp +++ b/Source/Core/Core/HW/DSPHLE/UCodes/GBA.cpp @@ -15,26 +15,26 @@ namespace DSP::HLE { -void ProcessGBACrypto(u32 address) +void ProcessGBACrypto(Memory::MemoryManager& memory, u32 address) { // Nonce challenge (first read from GBA, hence already little-endian) - const u32 challenge = HLEMemory_Read_U32LE(address); + const u32 challenge = HLEMemory_Read_U32LE(memory, address); // Palette of pulsing logo on GBA during transmission [0,6] - const u32 logo_palette = HLEMemory_Read_U32(address + 4); + const u32 logo_palette = HLEMemory_Read_U32(memory, address + 4); // Speed and direction of palette interpolation [-4,4] - const u32 logo_speed_32 = HLEMemory_Read_U32(address + 8); + const u32 logo_speed_32 = HLEMemory_Read_U32(memory, address + 8); // Length of JoyBoot program to upload - const u32 length = HLEMemory_Read_U32(address + 12); + const u32 length = HLEMemory_Read_U32(memory, address + 12); // Address to return results to game - const u32 dest_addr = HLEMemory_Read_U32(address + 16); + const u32 dest_addr = HLEMemory_Read_U32(memory, address + 16); // Unwrap key from challenge using 'sedo' magic number (to encrypt JoyBoot program) const u32 key = challenge ^ 0x6f646573; - HLEMemory_Write_U32(dest_addr, key); + HLEMemory_Write_U32(memory, dest_addr, key); // Pack palette parameters u16 palette_speed_coded; @@ -62,7 +62,7 @@ void ProcessGBACrypto(u32 address) // Wrap with 'Kawa' or 'sedo' (Kawasedo is the author of the BIOS cipher) t3 ^= ((t3 & 0x200) != 0 ? 0x6f646573 : 0x6177614b); - HLEMemory_Write_U32(dest_addr + 4, t3); + HLEMemory_Write_U32(memory, dest_addr + 4, t3); // Done! DEBUG_LOG_FMT(DSPHLE, @@ -85,7 +85,7 @@ void GBAUCode::Update() // check if we have something to send if (m_mail_handler.HasPending()) { - Core::System::GetInstance().GetDSP().GenerateDSPInterruptFromDSPEmu(DSP::INT_DSP); + m_dsphle->GetSystem().GetDSP().GenerateDSPInterruptFromDSPEmu(DSP::INT_DSP); } } @@ -119,7 +119,7 @@ void GBAUCode::HandleMail(u32 mail) { const u32 address = mail & 0x0fff'ffff; - ProcessGBACrypto(address); + ProcessGBACrypto(m_dsphle->GetSystem().GetMemory(), address); m_mail_handler.PushMail(DSP_DONE); m_mail_state = MailState::WaitingForNextTask; diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/GBA.h b/Source/Core/Core/HW/DSPHLE/UCodes/GBA.h index a2cf96869a..06c4663bb1 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/GBA.h +++ b/Source/Core/Core/HW/DSPHLE/UCodes/GBA.h @@ -6,6 +6,11 @@ #include "Common/CommonTypes.h" #include "Core/HW/DSPHLE/UCodes/UCodes.h" +namespace Memory +{ +class MemoryManager; +} + namespace DSP::HLE { class DSPHLE; @@ -13,7 +18,7 @@ class DSPHLE; // Computes two 32 bit integers to be returned to the game, based on the // provided crypto parameters at the provided MRAM address. The integers are // written back to RAM at the dest address provided in the crypto parameters. -void ProcessGBACrypto(u32 address); +void ProcessGBACrypto(Memory::MemoryManager& memory, u32 address); class GBAUCode final : public UCodeInterface { diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/ROM.cpp b/Source/Core/Core/HW/DSPHLE/UCodes/ROM.cpp index cd5e72a009..761742bac4 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/ROM.cpp +++ b/Source/Core/Core/HW/DSPHLE/UCodes/ROM.cpp @@ -19,6 +19,7 @@ #include "Core/HW/DSPHLE/DSPHLE.h" #include "Core/HW/DSPHLE/MailHandler.h" #include "Core/HW/DSPHLE/UCodes/UCodes.h" +#include "Core/System.h" namespace DSP::HLE { @@ -94,13 +95,14 @@ void ROMUCode::HandleMail(u32 mail) void ROMUCode::BootUCode() { - const u32 ector_crc = - Common::HashEctor(static_cast(HLEMemory_Get_Pointer(m_current_ucode.m_ram_address)), - m_current_ucode.m_length); + auto& memory = m_dsphle->GetSystem().GetMemory(); + const u32 ector_crc = Common::HashEctor( + static_cast(HLEMemory_Get_Pointer(memory, m_current_ucode.m_ram_address)), + m_current_ucode.m_length); if (Config::Get(Config::MAIN_DUMP_UCODE)) { - DSP::DumpDSPCode(static_cast(HLEMemory_Get_Pointer(m_current_ucode.m_ram_address)), + DSP::DumpDSPCode(static_cast(HLEMemory_Get_Pointer(memory, m_current_ucode.m_ram_address)), m_current_ucode.m_length, ector_crc); } diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/UCodes.cpp b/Source/Core/Core/HW/DSPHLE/UCodes/UCodes.cpp index 1c70944fa9..206624313e 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/UCodes.cpp +++ b/Source/Core/Core/HW/DSPHLE/UCodes/UCodes.cpp @@ -39,33 +39,24 @@ constexpr bool ExramRead(u32 address) return (address & 0x10000000) != 0; } -u8 HLEMemory_Read_U8(u32 address) +u8 HLEMemory_Read_U8(Memory::MemoryManager& memory, u32 address) { - auto& system = Core::System::GetInstance(); - auto& memory = system.GetMemory(); - if (ExramRead(address)) return memory.GetEXRAM()[address & memory.GetExRamMask()]; return memory.GetRAM()[address & memory.GetRamMask()]; } -void HLEMemory_Write_U8(u32 address, u8 value) +void HLEMemory_Write_U8(Memory::MemoryManager& memory, u32 address, u8 value) { - auto& system = Core::System::GetInstance(); - auto& memory = system.GetMemory(); - if (ExramRead(address)) memory.GetEXRAM()[address & memory.GetExRamMask()] = value; else memory.GetRAM()[address & memory.GetRamMask()] = value; } -u16 HLEMemory_Read_U16LE(u32 address) +u16 HLEMemory_Read_U16LE(Memory::MemoryManager& memory, u32 address) { - auto& system = Core::System::GetInstance(); - auto& memory = system.GetMemory(); - u16 value; if (ExramRead(address)) @@ -76,32 +67,26 @@ u16 HLEMemory_Read_U16LE(u32 address) return value; } -u16 HLEMemory_Read_U16(u32 address) +u16 HLEMemory_Read_U16(Memory::MemoryManager& memory, u32 address) { - return Common::swap16(HLEMemory_Read_U16LE(address)); + return Common::swap16(HLEMemory_Read_U16LE(memory, address)); } -void HLEMemory_Write_U16LE(u32 address, u16 value) +void HLEMemory_Write_U16LE(Memory::MemoryManager& memory, u32 address, u16 value) { - auto& system = Core::System::GetInstance(); - auto& memory = system.GetMemory(); - if (ExramRead(address)) std::memcpy(&memory.GetEXRAM()[address & memory.GetExRamMask()], &value, sizeof(u16)); else std::memcpy(&memory.GetRAM()[address & memory.GetRamMask()], &value, sizeof(u16)); } -void HLEMemory_Write_U16(u32 address, u16 value) +void HLEMemory_Write_U16(Memory::MemoryManager& memory, u32 address, u16 value) { - HLEMemory_Write_U16LE(address, Common::swap16(value)); + HLEMemory_Write_U16LE(memory, address, Common::swap16(value)); } -u32 HLEMemory_Read_U32LE(u32 address) +u32 HLEMemory_Read_U32LE(Memory::MemoryManager& memory, u32 address) { - auto& system = Core::System::GetInstance(); - auto& memory = system.GetMemory(); - u32 value; if (ExramRead(address)) @@ -112,32 +97,26 @@ u32 HLEMemory_Read_U32LE(u32 address) return value; } -u32 HLEMemory_Read_U32(u32 address) +u32 HLEMemory_Read_U32(Memory::MemoryManager& memory, u32 address) { - return Common::swap32(HLEMemory_Read_U32LE(address)); + return Common::swap32(HLEMemory_Read_U32LE(memory, address)); } -void HLEMemory_Write_U32LE(u32 address, u32 value) +void HLEMemory_Write_U32LE(Memory::MemoryManager& memory, u32 address, u32 value) { - auto& system = Core::System::GetInstance(); - auto& memory = system.GetMemory(); - if (ExramRead(address)) std::memcpy(&memory.GetEXRAM()[address & memory.GetExRamMask()], &value, sizeof(u32)); else std::memcpy(&memory.GetRAM()[address & memory.GetRamMask()], &value, sizeof(u32)); } -void HLEMemory_Write_U32(u32 address, u32 value) +void HLEMemory_Write_U32(Memory::MemoryManager& memory, u32 address, u32 value) { - HLEMemory_Write_U32LE(address, Common::swap32(value)); + HLEMemory_Write_U32LE(memory, address, Common::swap32(value)); } -void* HLEMemory_Get_Pointer(u32 address) +void* HLEMemory_Get_Pointer(Memory::MemoryManager& memory, u32 address) { - auto& system = Core::System::GetInstance(); - auto& memory = system.GetMemory(); - if (ExramRead(address)) return &memory.GetEXRAM()[address & memory.GetExRamMask()]; @@ -204,14 +183,13 @@ void UCodeInterface::PrepareBootUCode(u32 mail) m_needs_resume_mail = true; m_upload_setup_in_progress = false; - const u32 ector_crc = - Common::HashEctor(static_cast(HLEMemory_Get_Pointer(m_next_ucode.iram_mram_addr)), - m_next_ucode.iram_size); + auto& memory = m_dsphle->GetSystem().GetMemory(); + const u32 ector_crc = Common::HashEctor( + static_cast(HLEMemory_Get_Pointer(memory, m_next_ucode.iram_mram_addr)), + m_next_ucode.iram_size); if (Config::Get(Config::MAIN_DUMP_UCODE)) { - auto& system = Core::System::GetInstance(); - auto& memory = system.GetMemory(); DSP::DumpDSPCode(memory.GetPointer(m_next_ucode.iram_mram_addr), m_next_ucode.iram_size, ector_crc); } diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/UCodes.h b/Source/Core/Core/HW/DSPHLE/UCodes/UCodes.h index 029713ca60..67841cf811 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/UCodes.h +++ b/Source/Core/Core/HW/DSPHLE/UCodes/UCodes.h @@ -8,6 +8,10 @@ #include "Common/CommonTypes.h" +namespace Memory +{ +class MemoryManager; +} class PointerWrap; namespace DSP::HLE @@ -19,22 +23,22 @@ class DSPHLE; #define UCODE_INIT_AUDIO_SYSTEM 0x00000001 #define UCODE_NULL 0xFFFFFFFF -u8 HLEMemory_Read_U8(u32 address); -void HLEMemory_Write_U8(u32 address, u8 value); +u8 HLEMemory_Read_U8(Memory::MemoryManager& memory, u32 address); +void HLEMemory_Write_U8(Memory::MemoryManager& memory, u32 address, u8 value); -u16 HLEMemory_Read_U16LE(u32 address); -u16 HLEMemory_Read_U16(u32 address); +u16 HLEMemory_Read_U16LE(Memory::MemoryManager& memory, u32 address); +u16 HLEMemory_Read_U16(Memory::MemoryManager& memory, u32 address); -void HLEMemory_Write_U16LE(u32 address, u16 value); -void HLEMemory_Write_U16(u32 address, u16 value); +void HLEMemory_Write_U16LE(Memory::MemoryManager& memory, u32 address, u16 value); +void HLEMemory_Write_U16(Memory::MemoryManager& memory, u32 address, u16 value); -u32 HLEMemory_Read_U32LE(u32 address); -u32 HLEMemory_Read_U32(u32 address); +u32 HLEMemory_Read_U32LE(Memory::MemoryManager& memory, u32 address); +u32 HLEMemory_Read_U32(Memory::MemoryManager& memory, u32 address); -void HLEMemory_Write_U32LE(u32 address, u32 value); -void HLEMemory_Write_U32(u32 address, u32 value); +void HLEMemory_Write_U32LE(Memory::MemoryManager& memory, u32 address, u32 value); +void HLEMemory_Write_U32(Memory::MemoryManager& memory, u32 address, u32 value); -void* HLEMemory_Get_Pointer(u32 address); +void* HLEMemory_Get_Pointer(Memory::MemoryManager& memory, u32 address); class UCodeInterface { diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/Zelda.cpp b/Source/Core/Core/HW/DSPHLE/UCodes/Zelda.cpp index 80b7ac72b9..cae6f74b19 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/Zelda.cpp +++ b/Source/Core/Core/HW/DSPHLE/UCodes/Zelda.cpp @@ -118,7 +118,8 @@ static const std::map UCODE_FLAGS = { // * The Legend of Zelda: Twilight Princess / Wii (type ????, CRC ????) }; -ZeldaUCode::ZeldaUCode(DSPHLE* dsphle, u32 crc) : UCodeInterface(dsphle, crc) +ZeldaUCode::ZeldaUCode(DSPHLE* dsphle, u32 crc) + : UCodeInterface(dsphle, crc), m_renderer(dsphle->GetSystem()) { auto it = UCODE_FLAGS.find(crc); if (it == UCODE_FLAGS.end()) @@ -369,7 +370,7 @@ void ZeldaUCode::HandleMailLight(u32 mail) m_sync_max_voice_id = 0xFFFFFFFF; m_sync_voice_skip_flags.fill(0xFFFF); RenderAudio(); - Core::System::GetInstance().GetDSP().GenerateDSPInterruptFromDSPEmu(DSP::INT_DSP); + m_dsphle->GetSystem().GetDSP().GenerateDSPInterruptFromDSPEmu(DSP::INT_DSP); break; case MailState::HALTED: @@ -470,7 +471,8 @@ void ZeldaUCode::RunPendingCommands() m_renderer.SetVPBBaseAddress(Read32()); - u16* data_ptr = (u16*)HLEMemory_Get_Pointer(Read32()); + auto& memory = m_dsphle->GetSystem().GetMemory(); + u16* data_ptr = (u16*)HLEMemory_Get_Pointer(memory, Read32()); std::array resampling_coeffs; for (size_t i = 0; i < 0x100; ++i) @@ -492,7 +494,7 @@ void ZeldaUCode::RunPendingCommands() m_renderer.SetSineTable(std::move(sine_table)); } - u16* afc_coeffs_ptr = (u16*)HLEMemory_Get_Pointer(Read32()); + u16* afc_coeffs_ptr = (u16*)HLEMemory_Get_Pointer(memory, Read32()); std::array afc_coeffs; for (size_t i = 0; i < 0x20; ++i) afc_coeffs[i] = Common::swap16(afc_coeffs_ptr[i]); @@ -542,7 +544,7 @@ void ZeldaUCode::RunPendingCommands() case 0x0C: if (m_flags & SUPPORTS_GBA_CRYPTO) { - ProcessGBACrypto(Read32()); + ProcessGBACrypto(m_dsphle->GetSystem().GetMemory(), Read32()); } else if (m_flags & WEIRD_CMD_0C) { @@ -965,6 +967,12 @@ struct ReverbPB }; #pragma pack(pop) +ZeldaAudioRenderer::ZeldaAudioRenderer(Core::System& system) : m_system(system) +{ +} + +ZeldaAudioRenderer::~ZeldaAudioRenderer() = default; + void ZeldaAudioRenderer::PrepareFrame() { if (m_prepared) @@ -1047,7 +1055,8 @@ void ZeldaAudioRenderer::ApplyReverb(bool post_rendering) &m_buf_front_right_reverb_last8, }; - u16* rpb_base_ptr = (u16*)HLEMemory_Get_Pointer(m_reverb_pb_base_addr); + auto& memory = m_system.GetMemory(); + u16* rpb_base_ptr = (u16*)HLEMemory_Get_Pointer(memory, m_reverb_pb_base_addr); for (u16 rpb_idx = 0; rpb_idx < 4; ++rpb_idx) { ReverbPB rpb; @@ -1061,7 +1070,7 @@ void ZeldaAudioRenderer::ApplyReverb(bool post_rendering) u16 mram_buffer_idx = m_reverb_pb_frames_count[rpb_idx]; u32 mram_addr = rpb.GetCircularBufferBase() + mram_buffer_idx * 0x50 * sizeof(s16); - s16* mram_ptr = (s16*)HLEMemory_Get_Pointer(mram_addr); + s16* mram_ptr = (s16*)HLEMemory_Get_Pointer(memory, mram_addr); if (!post_rendering) { @@ -1316,8 +1325,9 @@ void ZeldaAudioRenderer::FinalizeFrame() ApplyVolumeInPlace_4_12(&m_buf_front_left, m_output_volume); ApplyVolumeInPlace_4_12(&m_buf_front_right, m_output_volume); - u16* ram_left_buffer = (u16*)HLEMemory_Get_Pointer(m_output_lbuf_addr); - u16* ram_right_buffer = (u16*)HLEMemory_Get_Pointer(m_output_rbuf_addr); + auto& memory = m_system.GetMemory(); + u16* ram_left_buffer = (u16*)HLEMemory_Get_Pointer(memory, m_output_lbuf_addr); + u16* ram_right_buffer = (u16*)HLEMemory_Get_Pointer(memory, m_output_rbuf_addr); for (size_t i = 0; i < m_buf_front_left.size(); ++i) { ram_left_buffer[i] = Common::swap16(m_buf_front_left[i]); @@ -1335,8 +1345,9 @@ void ZeldaAudioRenderer::FinalizeFrame() void ZeldaAudioRenderer::FetchVPB(u16 voice_id, VPB* vpb) { + auto& memory = m_system.GetMemory(); u16* vpb_words = (u16*)vpb; - u16* ram_vpbs = (u16*)HLEMemory_Get_Pointer(m_vpb_base_addr); + u16* ram_vpbs = (u16*)HLEMemory_Get_Pointer(memory, m_vpb_base_addr); // A few versions of the UCode have VPB of size 0x80 (vs. the standard // 0xC0). The whole 0x40-0x80 part is gone. Handle that by moving things @@ -1353,8 +1364,9 @@ void ZeldaAudioRenderer::FetchVPB(u16 voice_id, VPB* vpb) void ZeldaAudioRenderer::StoreVPB(u16 voice_id, VPB* vpb) { + auto& memory = m_system.GetMemory(); u16* vpb_words = (u16*)vpb; - u16* ram_vpbs = (u16*)HLEMemory_Get_Pointer(m_vpb_base_addr); + u16* ram_vpbs = (u16*)HLEMemory_Get_Pointer(memory, m_vpb_base_addr); size_t vpb_size = (m_flags & TINY_VPB) ? 0x80 : 0xC0; size_t base_idx = voice_id * vpb_size; @@ -1538,9 +1550,9 @@ void ZeldaAudioRenderer::Resample(VPB* vpb, const s16* src, MixingBuffer* dst) void* ZeldaAudioRenderer::GetARAMPtr(u32 offset) const { if (SConfig::GetInstance().bWii) - return HLEMemory_Get_Pointer(m_aram_base_addr + offset); + return HLEMemory_Get_Pointer(m_system.GetMemory(), m_aram_base_addr + offset); else - return reinterpret_cast(Core::System::GetInstance().GetDSP().GetARAMPtr()) + offset; + return reinterpret_cast(m_system.GetDSP().GetARAMPtr()) + offset; } template @@ -1773,8 +1785,9 @@ void ZeldaAudioRenderer::DecodeAFC(VPB* vpb, s16* dst, size_t block_count) void ZeldaAudioRenderer::DownloadRawSamplesFromMRAM(s16* dst, VPB* vpb, u16 requested_samples_count) { + auto& memory = m_system.GetMemory(); u32 addr = vpb->GetBaseAddress() + vpb->current_position_h * sizeof(u16); - s16* src_ptr = (s16*)HLEMemory_Get_Pointer(addr); + s16* src_ptr = (s16*)HLEMemory_Get_Pointer(memory, addr); if (requested_samples_count > vpb->GetRemainingLength()) { @@ -1803,7 +1816,7 @@ void ZeldaAudioRenderer::DownloadRawSamplesFromMRAM(s16* dst, VPB* vpb, u16 requ for (u16 i = 0; i < vpb->samples_before_loop; ++i) *dst++ = Common::swap16(*src_ptr++); vpb->SetBaseAddress(vpb->GetLoopAddress()); - src_ptr = (s16*)HLEMemory_Get_Pointer(vpb->GetLoopAddress()); + src_ptr = (s16*)HLEMemory_Get_Pointer(memory, vpb->GetLoopAddress()); for (u16 i = vpb->samples_before_loop; i < requested_samples_count; ++i) *dst++ = Common::swap16(*src_ptr++); vpb->current_position_h = requested_samples_count - vpb->samples_before_loop; diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/Zelda.h b/Source/Core/Core/HW/DSPHLE/UCodes/Zelda.h index a0142dc957..509c983a72 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/Zelda.h +++ b/Source/Core/Core/HW/DSPHLE/UCodes/Zelda.h @@ -9,6 +9,11 @@ #include "Common/CommonTypes.h" #include "Core/HW/DSPHLE/UCodes/UCodes.h" +namespace Core +{ +class System; +} + namespace DSP::HLE { class DSPHLE; @@ -16,6 +21,13 @@ class DSPHLE; class ZeldaAudioRenderer { public: + explicit ZeldaAudioRenderer(Core::System& system); + ZeldaAudioRenderer(const ZeldaAudioRenderer&) = delete; + ZeldaAudioRenderer(ZeldaAudioRenderer&&) = delete; + ZeldaAudioRenderer& operator=(const ZeldaAudioRenderer&) = delete; + ZeldaAudioRenderer& operator=(ZeldaAudioRenderer&&) = delete; + ~ZeldaAudioRenderer(); + void PrepareFrame(); void AddVoice(u16 voice_id); void FinalizeFrame(); @@ -183,6 +195,8 @@ private: std::array m_buf_front_left_reverb_last8{}; std::array m_buf_front_right_reverb_last8{}; u32 m_reverb_pb_base_addr = 0; + + Core::System& m_system; }; class ZeldaUCode final : public UCodeInterface