HW/SerialInterface: Pass System to ISIDevice.

This commit is contained in:
Admiral H. Curtiss 2023-03-12 14:19:38 +01:00
parent 5d07a45241
commit d371b17f37
No known key found for this signature in database
GPG Key ID: F051B4C4044F33FB
19 changed files with 72 additions and 67 deletions

View File

@ -367,7 +367,7 @@ void DoState(PointerWrap& p)
if (type != device->GetDeviceType()) if (type != device->GetDeviceType())
{ {
AddDevice(SIDevice_Create(type, i)); AddDevice(SIDevice_Create(Core::System::GetInstance(), type, i));
} }
device->DoState(p); device->DoState(p);
@ -672,7 +672,7 @@ void AddDevice(std::unique_ptr<ISIDevice> device)
void AddDevice(const SIDevices device, int device_number) void AddDevice(const SIDevices device, int device_number)
{ {
AddDevice(SIDevice_Create(device, device_number)); AddDevice(SIDevice_Create(Core::System::GetInstance(), device, device_number));
} }
void ChangeDevice(SIDevices device, int channel) void ChangeDevice(SIDevices device, int channel)

View File

@ -55,8 +55,8 @@ std::istream& operator>>(std::istream& stream, SIDevices& device)
return stream; return stream;
} }
ISIDevice::ISIDevice(SIDevices device_type, int device_number) ISIDevice::ISIDevice(Core::System& system, SIDevices device_type, int device_number)
: m_device_number(device_number), m_device_type(device_type) : m_system(system), m_device_number(device_number), m_device_type(device_type)
{ {
} }
@ -169,43 +169,44 @@ bool SIDevice_IsGCController(SIDevices type)
} }
// F A C T O R Y // F A C T O R Y
std::unique_ptr<ISIDevice> SIDevice_Create(const SIDevices device, const int port_number) std::unique_ptr<ISIDevice> SIDevice_Create(Core::System& system, const SIDevices device,
const int port_number)
{ {
switch (device) switch (device)
{ {
case SIDEVICE_GC_CONTROLLER: case SIDEVICE_GC_CONTROLLER:
return std::make_unique<CSIDevice_GCController>(device, port_number); return std::make_unique<CSIDevice_GCController>(system, device, port_number);
case SIDEVICE_WIIU_ADAPTER: case SIDEVICE_WIIU_ADAPTER:
return std::make_unique<CSIDevice_GCAdapter>(device, port_number); return std::make_unique<CSIDevice_GCAdapter>(system, device, port_number);
case SIDEVICE_DANCEMAT: case SIDEVICE_DANCEMAT:
return std::make_unique<CSIDevice_DanceMat>(device, port_number); return std::make_unique<CSIDevice_DanceMat>(system, device, port_number);
case SIDEVICE_GC_STEERING: case SIDEVICE_GC_STEERING:
return std::make_unique<CSIDevice_GCSteeringWheel>(device, port_number); return std::make_unique<CSIDevice_GCSteeringWheel>(system, device, port_number);
case SIDEVICE_GC_TARUKONGA: case SIDEVICE_GC_TARUKONGA:
return std::make_unique<CSIDevice_TaruKonga>(device, port_number); return std::make_unique<CSIDevice_TaruKonga>(system, device, port_number);
case SIDEVICE_GC_GBA: case SIDEVICE_GC_GBA:
return std::make_unique<CSIDevice_GBA>(device, port_number); return std::make_unique<CSIDevice_GBA>(system, device, port_number);
case SIDEVICE_GC_GBA_EMULATED: case SIDEVICE_GC_GBA_EMULATED:
#ifdef HAS_LIBMGBA #ifdef HAS_LIBMGBA
return std::make_unique<CSIDevice_GBAEmu>(device, port_number); return std::make_unique<CSIDevice_GBAEmu>(system, device, port_number);
#else #else
PanicAlertFmtT("Error: This build does not support emulated GBA controllers"); PanicAlertFmtT("Error: This build does not support emulated GBA controllers");
return std::make_unique<CSIDevice_Null>(device, port_number); return std::make_unique<CSIDevice_Null>(system, device, port_number);
#endif #endif
case SIDEVICE_GC_KEYBOARD: case SIDEVICE_GC_KEYBOARD:
return std::make_unique<CSIDevice_Keyboard>(device, port_number); return std::make_unique<CSIDevice_Keyboard>(system, device, port_number);
case SIDEVICE_AM_BASEBOARD: case SIDEVICE_AM_BASEBOARD:
case SIDEVICE_NONE: case SIDEVICE_NONE:
default: default:
return std::make_unique<CSIDevice_Null>(device, port_number); return std::make_unique<CSIDevice_Null>(system, device, port_number);
} }
} }
} // namespace SerialInterface } // namespace SerialInterface

View File

@ -8,6 +8,10 @@
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
class PointerWrap; class PointerWrap;
namespace Core
{
class System;
}
namespace SerialInterface namespace SerialInterface
{ {
@ -105,7 +109,7 @@ std::istream& operator>>(std::istream& stream, SIDevices& device);
class ISIDevice class ISIDevice
{ {
public: public:
ISIDevice(SIDevices device_type, int device_number); ISIDevice(Core::System& system, SIDevices device_type, int device_number);
virtual ~ISIDevice(); virtual ~ISIDevice();
int GetDeviceNumber() const; int GetDeviceNumber() const;
@ -128,6 +132,8 @@ public:
virtual void OnEvent(u64 userdata, s64 cycles_late); virtual void OnEvent(u64 userdata, s64 cycles_late);
protected: protected:
Core::System& m_system;
int m_device_number; int m_device_number;
SIDevices m_device_type; SIDevices m_device_type;
}; };
@ -135,5 +141,5 @@ protected:
int SIDevice_GetGBATransferTime(EBufferCommands cmd); int SIDevice_GetGBATransferTime(EBufferCommands cmd);
bool SIDevice_IsGCController(SIDevices type); bool SIDevice_IsGCController(SIDevices type);
std::unique_ptr<ISIDevice> SIDevice_Create(SIDevices device, int port_number); std::unique_ptr<ISIDevice> SIDevice_Create(Core::System& system, SIDevices device, int port_number);
} // namespace SerialInterface } // namespace SerialInterface

View File

@ -11,8 +11,8 @@
namespace SerialInterface namespace SerialInterface
{ {
CSIDevice_DanceMat::CSIDevice_DanceMat(SIDevices device, int device_number) CSIDevice_DanceMat::CSIDevice_DanceMat(Core::System& system, SIDevices device, int device_number)
: CSIDevice_GCController(device, device_number) : CSIDevice_GCController(system, device, device_number)
{ {
} }

View File

@ -10,10 +10,10 @@ struct GCPadStatus;
namespace SerialInterface namespace SerialInterface
{ {
class CSIDevice_DanceMat : public CSIDevice_GCController class CSIDevice_DanceMat final : public CSIDevice_GCController
{ {
public: public:
CSIDevice_DanceMat(SIDevices device, int device_number); CSIDevice_DanceMat(Core::System& system, SIDevices device, int device_number);
int RunBuffer(u8* buffer, int request_length) override; int RunBuffer(u8* buffer, int request_length) override;
u32 MapPadStatus(const GCPadStatus& pad_status) override; u32 MapPadStatus(const GCPadStatus& pad_status) override;

View File

@ -140,13 +140,12 @@ void GBASockServer::Disconnect()
m_booted = false; m_booted = false;
} }
void GBASockServer::ClockSync() void GBASockServer::ClockSync(Core::System& system)
{ {
if (!m_clock_sync) if (!m_clock_sync)
if (!(m_clock_sync = GetNextClock())) if (!(m_clock_sync = GetNextClock()))
return; return;
auto& system = Core::System::GetInstance();
auto& core_timing = system.GetCoreTiming(); auto& core_timing = system.GetCoreTiming();
u32 time_slice = 0; u32 time_slice = 0;
@ -263,7 +262,8 @@ void GBASockServer::Flush()
} }
} }
CSIDevice_GBA::CSIDevice_GBA(SIDevices device, int device_number) : ISIDevice(device, device_number) CSIDevice_GBA::CSIDevice_GBA(Core::System& system, SIDevices device, int device_number)
: ISIDevice(system, device, device_number)
{ {
} }
@ -273,7 +273,7 @@ int CSIDevice_GBA::RunBuffer(u8* buffer, int request_length)
{ {
case NextAction::SendCommand: case NextAction::SendCommand:
{ {
m_sock_server.ClockSync(); m_sock_server.ClockSync(m_system);
if (m_sock_server.Connect()) if (m_sock_server.Connect())
{ {
#ifdef _DEBUG #ifdef _DEBUG
@ -289,15 +289,14 @@ int CSIDevice_GBA::RunBuffer(u8* buffer, int request_length)
} }
m_last_cmd = static_cast<EBufferCommands>(buffer[0]); m_last_cmd = static_cast<EBufferCommands>(buffer[0]);
m_timestamp_sent = Core::System::GetInstance().GetCoreTiming().GetTicks(); m_timestamp_sent = m_system.GetCoreTiming().GetTicks();
m_next_action = NextAction::WaitTransferTime; m_next_action = NextAction::WaitTransferTime;
return 0; return 0;
} }
case NextAction::WaitTransferTime: case NextAction::WaitTransferTime:
{ {
int elapsed_time = int elapsed_time = static_cast<int>(m_system.GetCoreTiming().GetTicks() - m_timestamp_sent);
static_cast<int>(Core::System::GetInstance().GetCoreTiming().GetTicks() - m_timestamp_sent);
// Tell SI to ask again after TransferInterval() cycles // Tell SI to ask again after TransferInterval() cycles
if (SIDevice_GetGBATransferTime(m_last_cmd) > elapsed_time) if (SIDevice_GetGBATransferTime(m_last_cmd) > elapsed_time)
return 0; return 0;

View File

@ -25,7 +25,7 @@ public:
bool Connect(); bool Connect();
bool IsConnected(); bool IsConnected();
void ClockSync(); void ClockSync(Core::System& system);
void Send(const u8* si_buffer); void Send(const u8* si_buffer);
int Receive(u8* si_buffer, u8 bytes); int Receive(u8* si_buffer, u8 bytes);
void Flush(); void Flush();
@ -40,10 +40,10 @@ private:
bool m_booted = false; bool m_booted = false;
}; };
class CSIDevice_GBA : public ISIDevice class CSIDevice_GBA final : public ISIDevice
{ {
public: public:
CSIDevice_GBA(SIDevices device, int device_number); CSIDevice_GBA(Core::System& system, SIDevices device, int device_number);
int RunBuffer(u8* buffer, int request_length) override; int RunBuffer(u8* buffer, int request_length) override;
int TransferInterval() override; int TransferInterval() override;

View File

@ -27,11 +27,11 @@ static s64 GetSyncInterval()
return SystemTimers::GetTicksPerSecond() / 1000; return SystemTimers::GetTicksPerSecond() / 1000;
} }
CSIDevice_GBAEmu::CSIDevice_GBAEmu(SIDevices device, int device_number) CSIDevice_GBAEmu::CSIDevice_GBAEmu(Core::System& system, SIDevices device, int device_number)
: ISIDevice(device, device_number) : ISIDevice(system, device, device_number)
{ {
m_core = std::make_shared<HW::GBA::Core>(m_device_number); m_core = std::make_shared<HW::GBA::Core>(m_device_number);
m_core->Start(Core::System::GetInstance().GetCoreTiming().GetTicks()); m_core->Start(system.GetCoreTiming().GetTicks());
m_gbahost = Host_CreateGBAHost(m_core); m_gbahost = Host_CreateGBAHost(m_core);
m_core->SetHost(m_gbahost); m_core->SetHost(m_gbahost);
ScheduleEvent(m_device_number, GetSyncInterval()); ScheduleEvent(m_device_number, GetSyncInterval());
@ -56,7 +56,7 @@ int CSIDevice_GBAEmu::RunBuffer(u8* buffer, int request_length)
buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]); buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]);
#endif #endif
m_last_cmd = static_cast<EBufferCommands>(buffer[0]); m_last_cmd = static_cast<EBufferCommands>(buffer[0]);
m_timestamp_sent = Core::System::GetInstance().GetCoreTiming().GetTicks(); m_timestamp_sent = m_system.GetCoreTiming().GetTicks();
m_core->SendJoybusCommand(m_timestamp_sent, TransferInterval(), buffer, m_keys); m_core->SendJoybusCommand(m_timestamp_sent, TransferInterval(), buffer, m_keys);
RemoveEvent(m_device_number); RemoveEvent(m_device_number);
@ -75,8 +75,7 @@ int CSIDevice_GBAEmu::RunBuffer(u8* buffer, int request_length)
case NextAction::WaitTransferTime: case NextAction::WaitTransferTime:
{ {
int elapsed_time = int elapsed_time = static_cast<int>(m_system.GetCoreTiming().GetTicks() - m_timestamp_sent);
static_cast<int>(Core::System::GetInstance().GetCoreTiming().GetTicks() - m_timestamp_sent);
// Tell SI to ask again after TransferInterval() cycles // Tell SI to ask again after TransferInterval() cycles
if (TransferInterval() > elapsed_time) if (TransferInterval() > elapsed_time)
return 0; return 0;
@ -164,8 +163,7 @@ void CSIDevice_GBAEmu::DoState(PointerWrap& p)
void CSIDevice_GBAEmu::OnEvent(u64 userdata, s64 cycles_late) void CSIDevice_GBAEmu::OnEvent(u64 userdata, s64 cycles_late)
{ {
m_core->SendJoybusCommand(Core::System::GetInstance().GetCoreTiming().GetTicks() + userdata, 0, m_core->SendJoybusCommand(m_system.GetCoreTiming().GetTicks() + userdata, 0, nullptr, m_keys);
nullptr, m_keys);
ScheduleEvent(m_device_number, userdata + GetSyncInterval()); ScheduleEvent(m_device_number, userdata + GetSyncInterval());
} }
} // namespace SerialInterface } // namespace SerialInterface

View File

@ -17,10 +17,10 @@ class GBAHostInterface;
namespace SerialInterface namespace SerialInterface
{ {
class CSIDevice_GBAEmu : public ISIDevice class CSIDevice_GBAEmu final : public ISIDevice
{ {
public: public:
CSIDevice_GBAEmu(SIDevices device, int device_number); CSIDevice_GBAEmu(Core::System& system, SIDevices device, int device_number);
~CSIDevice_GBAEmu(); ~CSIDevice_GBAEmu();
int RunBuffer(u8* buffer, int request_length) override; int RunBuffer(u8* buffer, int request_length) override;

View File

@ -15,8 +15,8 @@
namespace SerialInterface namespace SerialInterface
{ {
CSIDevice_GCAdapter::CSIDevice_GCAdapter(SIDevices device, int device_number) CSIDevice_GCAdapter::CSIDevice_GCAdapter(Core::System& system, SIDevices device, int device_number)
: CSIDevice_GCController(device, device_number) : CSIDevice_GCController(system, device, device_number)
{ {
// Make sure PAD_GET_ORIGIN gets set due to a newly connected device. // Make sure PAD_GET_ORIGIN gets set due to a newly connected device.
GCAdapter::ResetDeviceType(m_device_number); GCAdapter::ResetDeviceType(m_device_number);

View File

@ -9,10 +9,10 @@
namespace SerialInterface namespace SerialInterface
{ {
class CSIDevice_GCAdapter : public CSIDevice_GCController class CSIDevice_GCAdapter final : public CSIDevice_GCController
{ {
public: public:
CSIDevice_GCAdapter(SIDevices device, int device_number); CSIDevice_GCAdapter(Core::System& system, SIDevices device, int device_number);
GCPadStatus GetPadStatus() override; GCPadStatus GetPadStatus() override;
int RunBuffer(u8* buffer, int request_length) override; int RunBuffer(u8* buffer, int request_length) override;

View File

@ -24,8 +24,9 @@
namespace SerialInterface namespace SerialInterface
{ {
// --- standard GameCube controller --- // --- standard GameCube controller ---
CSIDevice_GCController::CSIDevice_GCController(SIDevices device, int device_number) CSIDevice_GCController::CSIDevice_GCController(Core::System& system, SIDevices device,
: ISIDevice(device, device_number) int device_number)
: ISIDevice(system, device, device_number)
{ {
// Here we set origin to perfectly centered values. // Here we set origin to perfectly centered values.
// This purposely differs from real hardware which sets origin to current input state. // This purposely differs from real hardware which sets origin to current input state.
@ -264,19 +265,18 @@ CSIDevice_GCController::HandleButtonCombos(const GCPadStatus& pad_status)
{ {
m_last_button_combo = temp_combo; m_last_button_combo = temp_combo;
if (m_last_button_combo != COMBO_NONE) if (m_last_button_combo != COMBO_NONE)
m_timer_button_combo_start = Core::System::GetInstance().GetCoreTiming().GetTicks(); m_timer_button_combo_start = m_system.GetCoreTiming().GetTicks();
} }
if (m_last_button_combo != COMBO_NONE) if (m_last_button_combo != COMBO_NONE)
{ {
auto& system = Core::System::GetInstance(); const u64 current_time = m_system.GetCoreTiming().GetTicks();
const u64 current_time = system.GetCoreTiming().GetTicks();
if (u32(current_time - m_timer_button_combo_start) > SystemTimers::GetTicksPerSecond() * 3) if (u32(current_time - m_timer_button_combo_start) > SystemTimers::GetTicksPerSecond() * 3)
{ {
if (m_last_button_combo == COMBO_RESET) if (m_last_button_combo == COMBO_RESET)
{ {
INFO_LOG_FMT(SERIALINTERFACE, "PAD - COMBO_RESET"); INFO_LOG_FMT(SERIALINTERFACE, "PAD - COMBO_RESET");
system.GetProcessorInterface().ResetButton_Tap(); m_system.GetProcessorInterface().ResetButton_Tap();
} }
else if (m_last_button_combo == COMBO_ORIGIN) else if (m_last_button_combo == COMBO_ORIGIN)
{ {
@ -355,8 +355,8 @@ void CSIDevice_GCController::RefreshConfig()
} }
} }
CSIDevice_TaruKonga::CSIDevice_TaruKonga(SIDevices device, int device_number) CSIDevice_TaruKonga::CSIDevice_TaruKonga(Core::System& system, SIDevices device, int device_number)
: CSIDevice_GCController(device, device_number) : CSIDevice_GCController(system, device, device_number)
{ {
} }

View File

@ -53,7 +53,7 @@ protected:
public: public:
// Constructor // Constructor
CSIDevice_GCController(SIDevices device, int device_number); CSIDevice_GCController(Core::System& system, SIDevices device, int device_number);
~CSIDevice_GCController() override; ~CSIDevice_GCController() override;
// Run the SI Buffer // Run the SI Buffer
@ -92,10 +92,10 @@ private:
}; };
// "TaruKonga", the DK Bongo controller // "TaruKonga", the DK Bongo controller
class CSIDevice_TaruKonga : public CSIDevice_GCController class CSIDevice_TaruKonga final : public CSIDevice_GCController
{ {
public: public:
CSIDevice_TaruKonga(SIDevices device, int device_number); CSIDevice_TaruKonga(Core::System& system, SIDevices device, int device_number);
bool GetData(u32& hi, u32& low) override; bool GetData(u32& hi, u32& low) override;

View File

@ -14,8 +14,9 @@
namespace SerialInterface namespace SerialInterface
{ {
CSIDevice_GCSteeringWheel::CSIDevice_GCSteeringWheel(SIDevices device, int device_number) CSIDevice_GCSteeringWheel::CSIDevice_GCSteeringWheel(Core::System& system, SIDevices device,
: CSIDevice_GCController(device, device_number) int device_number)
: CSIDevice_GCController(system, device, device_number)
{ {
} }

View File

@ -7,10 +7,10 @@
namespace SerialInterface namespace SerialInterface
{ {
class CSIDevice_GCSteeringWheel : public CSIDevice_GCController class CSIDevice_GCSteeringWheel final : public CSIDevice_GCController
{ {
public: public:
CSIDevice_GCSteeringWheel(SIDevices device, int device_number); CSIDevice_GCSteeringWheel(Core::System& system, SIDevices device, int device_number);
int RunBuffer(u8* buffer, int request_length) override; int RunBuffer(u8* buffer, int request_length) override;
bool GetData(u32& hi, u32& low) override; bool GetData(u32& hi, u32& low) override;

View File

@ -16,8 +16,8 @@
namespace SerialInterface namespace SerialInterface
{ {
// --- GameCube keyboard --- // --- GameCube keyboard ---
CSIDevice_Keyboard::CSIDevice_Keyboard(SIDevices device, int device_number) CSIDevice_Keyboard::CSIDevice_Keyboard(Core::System& system, SIDevices device, int device_number)
: ISIDevice(device, device_number) : ISIDevice(system, device, device_number)
{ {
} }

View File

@ -12,11 +12,11 @@ struct KeyboardStatus;
namespace SerialInterface namespace SerialInterface
{ {
class CSIDevice_Keyboard : public ISIDevice class CSIDevice_Keyboard final : public ISIDevice
{ {
public: public:
// Constructor // Constructor
CSIDevice_Keyboard(SIDevices device, int device_number); CSIDevice_Keyboard(Core::System& system, SIDevices device, int device_number);
// Run the SI Buffer // Run the SI Buffer
int RunBuffer(u8* buffer, int request_length) override; int RunBuffer(u8* buffer, int request_length) override;

View File

@ -5,8 +5,8 @@
namespace SerialInterface namespace SerialInterface
{ {
CSIDevice_Null::CSIDevice_Null(SIDevices device, int device_number) CSIDevice_Null::CSIDevice_Null(Core::System& system, SIDevices device, int device_number)
: ISIDevice{device, device_number} : ISIDevice{system, device, device_number}
{ {
} }

View File

@ -12,7 +12,7 @@ namespace SerialInterface
class CSIDevice_Null final : public ISIDevice class CSIDevice_Null final : public ISIDevice
{ {
public: public:
CSIDevice_Null(SIDevices device, int device_number); CSIDevice_Null(Core::System& system, SIDevices device, int device_number);
int RunBuffer(u8* buffer, int request_length) override; int RunBuffer(u8* buffer, int request_length) override;
bool GetData(u32& hi, u32& low) override; bool GetData(u32& hi, u32& low) override;