HW/HSP: Ensure an IHSPDevice object always exists to remove null checks as a micro optimization.

This commit is contained in:
Jordan Woyak
2026-03-30 18:12:11 -05:00
parent 207fe1f5de
commit 187fcfa7dc
2 changed files with 16 additions and 22 deletions

View File

@@ -16,54 +16,49 @@ HSPManager::~HSPManager() = default;
void HSPManager::Init()
{
AddDevice(Config::Get(Config::MAIN_HSP_DEVICE));
SetDevice(Config::Get(Config::MAIN_HSP_DEVICE));
}
void HSPManager::Shutdown()
{
RemoveDevice();
m_device.reset();
}
u64 HSPManager::Read(u32 address)
{
DEBUG_LOG_FMT(HSP, "HSP read from 0x{:08x}", address);
if (m_device)
return m_device->Read(address);
return 0;
return m_device->Read(address);
}
void HSPManager::Write(u32 address, u64 value)
{
DEBUG_LOG_FMT(HSP, "HSP write to 0x{:08x}: 0x{:016x}", address, value);
if (m_device)
m_device->Write(address, value);
m_device->Write(address, value);
}
void HSPManager::DoState(PointerWrap& p)
{
HSPDeviceType type = m_device->GetDeviceType();
p.Do(type);
const HSPDeviceType current_type = m_device->GetDeviceType();
auto state_type = current_type;
p.Do(state_type);
// If the type doesn't match, switch to the right device type
if (type != m_device->GetDeviceType())
AddDevice(type);
if (state_type != current_type)
SetDevice(state_type);
m_device->DoState(p);
}
void HSPManager::AddDevice(std::unique_ptr<IHSPDevice> device)
void HSPManager::SetDevice(std::unique_ptr<IHSPDevice> device)
{
// Set the new one
m_device = std::move(device);
}
void HSPManager::AddDevice(const HSPDeviceType device)
void HSPManager::SetDevice(const HSPDeviceType device)
{
AddDevice(HSPDevice_Create(device));
SetDevice(HSPDevice_Create(device));
}
void HSPManager::RemoveDevice()
{
m_device.reset();
}
} // namespace HSP

View File

@@ -32,9 +32,8 @@ public:
void DoState(PointerWrap& p);
void RemoveDevice();
void AddDevice(std::unique_ptr<IHSPDevice> device);
void AddDevice(HSPDeviceType device);
void SetDevice(std::unique_ptr<IHSPDevice> device);
void SetDevice(HSPDeviceType device);
private:
std::unique_ptr<IHSPDevice> m_device;