Skip to content

Commit

Permalink
Partial refactor/split
Browse files Browse the repository at this point in the history
  • Loading branch information
Pokechu22 committed Nov 13, 2022
1 parent a7dd6b6 commit c403c42
Show file tree
Hide file tree
Showing 20 changed files with 678 additions and 398 deletions.
12 changes: 7 additions & 5 deletions Source/Core/Core/HW/Wiimote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ HIDWiimote* GetHIDWiimoteSource(unsigned int index)
switch (GetSource(index))
{
case WiimoteSource::Emulated:
hid_source = static_cast<WiimoteEmu::Wiimote*>(::Wiimote::GetConfig()->GetController(index));
hid_source =
static_cast<WiimoteEmu::WiimoteBase*>(::Wiimote::GetConfig()->GetController(index));
break;

case WiimoteSource::Real:
Expand Down Expand Up @@ -160,7 +161,7 @@ ControllerEmu::ControlGroup* GetShinkansenGroup(int number, WiimoteEmu::Shinkans

ControllerEmu::ControlGroup* GetBalanceBoardGroup(int number, WiimoteEmu::BalanceBoardGroup group)
{
return static_cast<WiimoteEmu::Wiimote*>(s_config.GetController(number))
return static_cast<WiimoteEmu::BalanceBoard*>(s_config.GetController(number))
->GetBalanceBoardGroup(group);
}

Expand All @@ -183,8 +184,9 @@ void Initialize(InitializeMode init_mode)
{
if (s_config.ControllersNeedToBeCreated())
{
for (unsigned int i = WIIMOTE_CHAN_0; i < MAX_BBMOTES; ++i)
for (unsigned int i = WIIMOTE_CHAN_0; i < MAX_WIIMOTES; ++i)
s_config.CreateController<WiimoteEmu::Wiimote>(i);
s_config.CreateController<WiimoteEmu::BalanceBoard>(WIIMOTE_BALANCE_BOARD);
}

s_config.RegisterHotplugCallback();
Expand All @@ -205,7 +207,7 @@ void Initialize(InitializeMode init_mode)
void ResetAllWiimotes()
{
for (int i = WIIMOTE_CHAN_0; i < MAX_BBMOTES; ++i)
static_cast<WiimoteEmu::Wiimote*>(s_config.GetController(i))->Reset();
static_cast<WiimoteEmu::WiimoteBase*>(s_config.GetController(i))->Reset();
}

void LoadConfig()
Expand Down Expand Up @@ -235,7 +237,7 @@ void DoState(PointerWrap& p)
if (WiimoteSource(state_wiimote_source) == WiimoteSource::Emulated)
{
// Sync complete state of emulated wiimotes.
static_cast<WiimoteEmu::Wiimote*>(s_config.GetController(i))->DoState(p);
static_cast<WiimoteEmu::WiimoteBase*>(s_config.GetController(i))->DoState(p);
}

if (p.IsReadMode())
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/Wiimote.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ enum class ShinkansenGroup;
enum class BalanceBoardGroup;
} // namespace WiimoteEmu

enum
enum : u8
{
WIIMOTE_CHAN_0 = 0,
WIIMOTE_CHAN_1,
Expand Down
129 changes: 76 additions & 53 deletions Source/Core/Core/HW/WiimoteEmu/EmuSubroutines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace WiimoteEmu
{
using namespace WiimoteCommon;

void Wiimote::HandleReportMode(const OutputReportMode& dr)
void WiimoteBase::HandleReportMode(const OutputReportMode& dr)
{
if (!DataReportBuilder::IsValidMode(dr.mode))
{
Expand All @@ -46,7 +46,8 @@ void Wiimote::HandleReportMode(const OutputReportMode& dr)

// Tests that we have enough bytes for the report before we run the handler.
template <typename T, typename H>
void Wiimote::InvokeHandler(H&& handler, const WiimoteCommon::OutputReportGeneric& rpt, u32 size)
void WiimoteBase::InvokeHandler(H&& handler, const WiimoteCommon::OutputReportGeneric& rpt,
u32 size)
{
if (size < sizeof(T))
{
Expand All @@ -58,17 +59,17 @@ void Wiimote::InvokeHandler(H&& handler, const WiimoteCommon::OutputReportGeneri
(this->*handler)(Common::BitCastPtr<T>(&rpt.data[0]));
}

void Wiimote::EventLinked()
void WiimoteBase::EventLinked()
{
Reset();
}

void Wiimote::EventUnlinked()
void WiimoteBase::EventUnlinked()
{
Reset();
}

void Wiimote::InterruptDataOutput(const u8* data, u32 size)
void WiimoteBase::InterruptDataOutput(const u8* data, u32 size)
{
if (size == 0)
{
Expand All @@ -87,50 +88,50 @@ void Wiimote::InterruptDataOutput(const u8* data, u32 size)

// WiiBrew:
// In every single Output Report, bit 0 (0x01) of the first byte controls the Rumble feature.
InvokeHandler<OutputReportRumble>(&Wiimote::HandleReportRumble, rpt, rpt_size);
InvokeHandler<OutputReportRumble>(&WiimoteBase::HandleReportRumble, rpt, rpt_size);

switch (rpt.rpt_id)
{
case OutputReportID::Rumble:
// This is handled above.
break;
case OutputReportID::LED:
InvokeHandler<OutputReportLeds>(&Wiimote::HandleReportLeds, rpt, rpt_size);
InvokeHandler<OutputReportLeds>(&WiimoteBase::HandleReportLeds, rpt, rpt_size);
break;
case OutputReportID::ReportMode:
InvokeHandler<OutputReportMode>(&Wiimote::HandleReportMode, rpt, rpt_size);
InvokeHandler<OutputReportMode>(&WiimoteBase::HandleReportMode, rpt, rpt_size);
break;
case OutputReportID::IRLogicEnable:
InvokeHandler<OutputReportEnableFeature>(&Wiimote::HandleIRLogicEnable, rpt, rpt_size);
InvokeHandler<OutputReportEnableFeature>(&WiimoteBase::HandleIRLogicEnable, rpt, rpt_size);
break;
case OutputReportID::SpeakerEnable:
InvokeHandler<OutputReportEnableFeature>(&Wiimote::HandleSpeakerEnable, rpt, rpt_size);
InvokeHandler<OutputReportEnableFeature>(&WiimoteBase::HandleSpeakerEnable, rpt, rpt_size);
break;
case OutputReportID::RequestStatus:
InvokeHandler<OutputReportRequestStatus>(&Wiimote::HandleRequestStatus, rpt, rpt_size);
InvokeHandler<OutputReportRequestStatus>(&WiimoteBase::HandleRequestStatus, rpt, rpt_size);
break;
case OutputReportID::WriteData:
InvokeHandler<OutputReportWriteData>(&Wiimote::HandleWriteData, rpt, rpt_size);
InvokeHandler<OutputReportWriteData>(&WiimoteBase::HandleWriteData, rpt, rpt_size);
break;
case OutputReportID::ReadData:
InvokeHandler<OutputReportReadData>(&Wiimote::HandleReadData, rpt, rpt_size);
InvokeHandler<OutputReportReadData>(&WiimoteBase::HandleReadData, rpt, rpt_size);
break;
case OutputReportID::SpeakerData:
InvokeHandler<OutputReportSpeakerData>(&Wiimote::HandleSpeakerData, rpt, rpt_size);
InvokeHandler<OutputReportSpeakerData>(&WiimoteBase::HandleSpeakerData, rpt, rpt_size);
break;
case OutputReportID::SpeakerMute:
InvokeHandler<OutputReportEnableFeature>(&Wiimote::HandleSpeakerMute, rpt, rpt_size);
InvokeHandler<OutputReportEnableFeature>(&WiimoteBase::HandleSpeakerMute, rpt, rpt_size);
break;
case OutputReportID::IRLogicEnable2:
InvokeHandler<OutputReportEnableFeature>(&Wiimote::HandleIRLogicEnable2, rpt, rpt_size);
InvokeHandler<OutputReportEnableFeature>(&WiimoteBase::HandleIRLogicEnable2, rpt, rpt_size);
break;
default:
PanicAlertFmt("HidOutputReport: Unknown report ID {:#04x}", static_cast<u8>(rpt.rpt_id));
break;
}
}

void Wiimote::SendAck(OutputReportID rpt_id, ErrorCode error_code)
void WiimoteBase::SendAck(OutputReportID rpt_id, ErrorCode error_code)
{
TypedInputData<InputReportAck> rpt(InputReportID::Ack);
auto& ack = rpt.payload;
Expand All @@ -145,24 +146,6 @@ void Wiimote::SendAck(OutputReportID rpt_id, ErrorCode error_code)
void Wiimote::HandleExtensionSwap(ExtensionNumber desired_extension_number,
bool desired_motion_plus)
{
if (WIIMOTE_BALANCE_BOARD == m_index)
{
// Prevent M+ or anything else silly from being attached to a balance board.
if (m_is_motion_plus_attached)
{
m_is_motion_plus_attached = false;
m_motion_plus.GetExtPort().AttachExtension(GetNoneExtension());
}
// Also force the BB "extension".
if (m_active_extension != ExtensionNumber::BALANCE_BOARD)
{
m_active_extension = ExtensionNumber::BALANCE_BOARD;
m_extension_port.AttachExtension(GetActiveExtension());
GetActiveExtension()->Reset();
}
return;
}

// FYI: AttachExtension also connects devices to the i2c bus

if (m_is_motion_plus_attached && !desired_motion_plus)
Expand Down Expand Up @@ -241,7 +224,7 @@ void Wiimote::HandleExtensionSwap(ExtensionNumber desired_extension_number,
}
}

void Wiimote::HandleRequestStatus(const OutputReportRequestStatus&)
void WiimoteBase::HandleRequestStatus(const OutputReportRequestStatus&)
{
// FYI: buttons are updated in Update() for determinism

Expand All @@ -263,7 +246,7 @@ void Wiimote::HandleRequestStatus(const OutputReportRequestStatus&)
InterruptDataInputCallback(rpt.GetData(), rpt.GetSize());
}

void Wiimote::HandleWriteData(const OutputReportWriteData& wd)
void WiimoteBase::HandleWriteData(const OutputReportWriteData& wd)
{
if (m_read_request.size)
{
Expand Down Expand Up @@ -342,7 +325,7 @@ void Wiimote::HandleReportRumble(const WiimoteCommon::OutputReportRumble& rpt)
// FYI: A real wiimote never seems to ACK a rumble report:
}

void Wiimote::HandleReportLeds(const WiimoteCommon::OutputReportLeds& rpt)
void WiimoteBase::HandleReportLeds(const WiimoteCommon::OutputReportLeds& rpt)
{
m_status.leds = rpt.leds;

Expand Down Expand Up @@ -412,7 +395,7 @@ void Wiimote::HandleSpeakerData(const WiimoteCommon::OutputReportSpeakerData& rp
// More investigation is needed.
}

void Wiimote::HandleReadData(const OutputReportReadData& rd)
void WiimoteBase::HandleReadData(const OutputReportReadData& rd)
{
if (m_read_request.size)
{
Expand Down Expand Up @@ -443,7 +426,7 @@ void Wiimote::HandleReadData(const OutputReportReadData& rd)
// FYI: No "ACK" is sent under normal situations.
}

bool Wiimote::ProcessReadDataRequest()
bool WiimoteBase::ProcessReadDataRequest()
{
// Limit the amt to 16 bytes
// AyuanX: the MTU is 640B though... what a waste!
Expand Down Expand Up @@ -557,29 +540,26 @@ bool Wiimote::ProcessReadDataRequest()
return true;
}

void Wiimote::DoState(PointerWrap& p)
void WiimoteBase::DoState(PointerWrap& p)
{
// No need to sync. Index will not change.
// p.Do(m_index);

// No need to sync. This is not wiimote state.
// p.Do(m_sensor_bar_on_top);

p.Do(m_reporting_mode);
p.Do(m_reporting_continuous);

p.Do(m_speaker_mute);

p.Do(m_status);
p.Do(m_eeprom);
p.Do(m_read_request);

// Sub-devices:
m_speaker_logic.DoState(p);
m_camera_logic.DoState(p);
p.DoMarker("WiimoteBase");
}

if (p.IsReadMode())
m_camera_logic.SetEnabled(m_status.ir);
void Wiimote::DoState(PointerWrap& p)
{
WiimoteBase::DoState(p);

p.Do(m_speaker_mute);

p.Do(m_is_motion_plus_attached);
p.Do(m_active_extension);
Expand All @@ -595,6 +575,13 @@ void Wiimote::DoState(PointerWrap& p)
if (m_active_extension != ExtensionNumber::NONE)
GetActiveExtension()->DoState(p);

// Sub-devices:
m_speaker_logic.DoState(p);
m_camera_logic.DoState(p);

if (p.IsReadMode())
m_camera_logic.SetEnabled(m_status.ir);

// Dynamics
p.Do(m_swing_state);
p.Do(m_tilt_state);
Expand All @@ -607,9 +594,45 @@ void Wiimote::DoState(PointerWrap& p)
p.DoMarker("Wiimote");
}

ExtensionNumber Wiimote::GetActiveExtensionNumber() const
void BalanceBoard::DoState(PointerWrap& p)
{
WiimoteBase::DoState(p);
m_ext.DoState(p);
p.DoMarker("BalanceBoard");
}

// TODO: Are these implemented correctly? How does the balance board actually handle missing
// hardware?
// Seems like using Nack causes things not to work, so Success probably is correct.
void BalanceBoard::HandleReportRumble(const WiimoteCommon::OutputReportRumble& rpt)
{
// rpt.ack does not exist ("A real wiimote never seems to ACK a rumble report")
}
void BalanceBoard::HandleIRLogicEnable(const WiimoteCommon::OutputReportEnableFeature& rpt)
{
// Not called?
if (rpt.ack)
SendAck(OutputReportID::IRLogicEnable, ErrorCode::Success);
}
void BalanceBoard::HandleIRLogicEnable2(const WiimoteCommon::OutputReportEnableFeature& rpt)
{
if (rpt.ack)
SendAck(OutputReportID::IRLogicEnable2, ErrorCode::Success);
}
void BalanceBoard::HandleSpeakerMute(const WiimoteCommon::OutputReportEnableFeature& rpt)
{
if (rpt.ack)
SendAck(OutputReportID::SpeakerMute, ErrorCode::Success);
}
void BalanceBoard::HandleSpeakerEnable(const WiimoteCommon::OutputReportEnableFeature& rpt)
{
if (rpt.ack)
SendAck(OutputReportID::SpeakerEnable, ErrorCode::Success);
}
void BalanceBoard::HandleSpeakerData(const WiimoteCommon::OutputReportSpeakerData& rpt)
{
return m_active_extension;
// rpt.ack does not exist
// ("Speaker data reports normally do not ACK but I have seen them ACK with error codes")
}

} // namespace WiimoteEmu
Loading

0 comments on commit c403c42

Please sign in to comment.