Skip to content

Commit

Permalink
It builds!
Browse files Browse the repository at this point in the history
  • Loading branch information
Pokechu22 committed Jan 14, 2024
1 parent a175779 commit b761f72
Show file tree
Hide file tree
Showing 16 changed files with 390 additions and 300 deletions.
6 changes: 2 additions & 4 deletions Source/Core/Common/I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ void I2CBus::DoState(PointerWrap& p)

bool I2CSlaveAutoIncrementing::StartWrite(u8 slave_addr)
{
if (slave_addr == m_slave_addr)
if (DeviceEnabled() && slave_addr == m_slave_addr)
{
INFO_LOG_FMT(WII_IPC, "I2C Device {:02x} write started, previously active: {}", m_slave_addr,
m_active);
Expand All @@ -428,7 +428,7 @@ bool I2CSlaveAutoIncrementing::StartWrite(u8 slave_addr)

bool I2CSlaveAutoIncrementing::StartRead(u8 slave_addr)
{
if (slave_addr == m_slave_addr)
if (DeviceEnabled() && slave_addr == m_slave_addr)
{
INFO_LOG_FMT(WII_IPC, "I2C Device {:02x} read started, previously active: {}", m_slave_addr,
m_active);
Expand Down Expand Up @@ -508,8 +508,6 @@ void I2CSlaveAutoIncrementing::DoState(PointerWrap& p)
}
p.Do(m_active);
p.Do(m_device_address);

DoDeviceState(p);
}

}; // namespace Common
Expand Down
18 changes: 14 additions & 4 deletions Source/Core/Common/I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class I2CSlave
}
};

class I2CSlaveAutoIncrementing : public I2CSlave
class I2CSlaveAutoIncrementing : public virtual I2CSlave
{
public:
I2CSlaveAutoIncrementing(u8 slave_addr) : m_slave_addr(slave_addr) {}
Expand All @@ -59,14 +59,14 @@ class I2CSlaveAutoIncrementing : public I2CSlave
std::optional<u8> ReadByte() override;
bool WriteByte(u8 value) override;

void DoState(PointerWrap& p);
virtual void DoState(PointerWrap& p);

protected:
// i.e. should the device respond on the bus
virtual bool DeviceEnabled() { return true; }
virtual u8 ReadByte(u8 addr) = 0;
virtual void WriteByte(u8 addr, u8 value) = 0;

virtual void DoDeviceState(PointerWrap& p) = 0;

private:
const u8 m_slave_addr;
bool m_active = false;
Expand Down Expand Up @@ -104,6 +104,16 @@ class I2CBusSimple : public I2CBusBase
int BusRead(u8 slave_addr, u8 addr, int count, u8* data_out);
};

class I2CBusForwarding : public I2CBusSimple
{
public:
using I2CBusBase::ReadByte;
using I2CBusBase::StartRead;
using I2CBusBase::StartWrite;
using I2CBusBase::Stop;
using I2CBusBase::WriteByte;
};

// An I²C bus implementation accessed via bit-banging.
// A few assumptions and limitations exist:
// - All devices support both writes and reads.
Expand Down
7 changes: 6 additions & 1 deletion Source/Core/Core/HW/WII_IPC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ class AVEDevice : public Common::I2CSlaveAutoIncrementing
ave_ever_logged = {};
}

void DoState(PointerWrap& p) override
{
I2CSlaveAutoIncrementing::DoState(p);
p.Do(m_registers);
}

AVEState m_registers{};

protected:
Expand Down Expand Up @@ -203,7 +209,6 @@ class AVEDevice : public Common::I2CSlaveAutoIncrementing
value);
}
}
void DoDeviceState(PointerWrap& p) override { p.Do(m_registers); }

private:
std::bitset<sizeof(AVEState)> ave_ever_logged{}; // logging only, not saved
Expand Down
25 changes: 10 additions & 15 deletions Source/Core/Core/HW/WiimoteEmu/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,26 @@ void CameraLogic::Reset()

void CameraLogic::DoState(PointerWrap& p)
{
I2CSlaveAutoIncrementing::DoState(p);

p.Do(m_reg_data);

// FYI: m_is_enabled is handled elsewhere.
}

int CameraLogic::BusRead(u8 slave_addr, u8 addr, int count, u8* data_out)
bool CameraLogic::DeviceEnabled()
{
if (I2C_ADDR != slave_addr)
return 0;

if (!m_is_enabled)
return 0;

return RawRead(&m_reg_data, addr, count, data_out);
return m_is_enabled;
}

int CameraLogic::BusWrite(u8 slave_addr, u8 addr, int count, const u8* data_in)
u8 CameraLogic::ReadByte(u8 addr)
{
if (I2C_ADDR != slave_addr)
return 0;

if (!m_is_enabled)
return 0;
return RawRead(&m_reg_data, addr);
}

return RawWrite(&m_reg_data, addr, count, data_in);
void CameraLogic::WriteByte(u8 addr, u8 value)
{
RawWrite(&m_reg_data, addr, value);
}

std::array<CameraPoint, CameraLogic::NUM_POINTS>
Expand Down
13 changes: 8 additions & 5 deletions Source/Core/Core/HW/WiimoteEmu/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

#include "Common/ChunkFile.h"
#include "Common/CommonTypes.h"
#include "Common/I2C.h"
#include "Core/HW/WiimoteEmu/Dynamics.h"
#include "Core/HW/WiimoteEmu/I2CBus.h"
#include "InputCommon/ControllerEmu/ControlGroup/Cursor.h"

namespace Common
Expand Down Expand Up @@ -102,9 +102,11 @@ struct IRFull : IRExtended
};
static_assert(sizeof(IRFull) == 9, "Wrong size");

class CameraLogic : public I2CSlave
class CameraLogic : public Common::I2CSlaveAutoIncrementing
{
public:
CameraLogic() : I2CSlaveAutoIncrementing(I2C_ADDR) {}

// OEM sensor bar distance between LED clusters in meters.
static constexpr float SENSOR_BAR_LED_SEPARATION = 0.2f;

Expand Down Expand Up @@ -133,7 +135,7 @@ class CameraLogic : public I2CSlave
static constexpr int MAX_POINT_SIZE = 15;

void Reset();
void DoState(PointerWrap& p);
void DoState(PointerWrap& p) override;
static std::array<CameraPoint, NUM_POINTS> GetCameraPoints(const Common::Matrix44& transform,
Common::Vec2 field_of_view);
void Update(const std::array<CameraPoint, NUM_POINTS>& camera_points);
Expand Down Expand Up @@ -177,8 +179,9 @@ class CameraLogic : public I2CSlave
static const u8 REPORT_DATA_OFFSET = offsetof(Register, camera_data);

private:
int BusRead(u8 slave_addr, u8 addr, int count, u8* data_out) override;
int BusWrite(u8 slave_addr, u8 addr, int count, const u8* data_in) override;
bool DeviceEnabled() override;
u8 ReadByte(u8 addr) override;
void WriteByte(u8 addr, u8 value) override;

Register m_reg_data{};

Expand Down
45 changes: 27 additions & 18 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,34 +71,46 @@ void None::DoState(PointerWrap& p)
// Nothing needed.
}

int None::BusRead(u8 slave_addr, u8 addr, int count, u8* data_out)
bool None::StartWrite(u8 slave_addr)
{
return 0;
return false;
}

bool None::StartRead(u8 slave_addr)
{
return false;
}

void None::Stop()
{
// Nothing needed.
}

int None::BusWrite(u8 slave_addr, u8 addr, int count, const u8* data_in)
std::optional<u8> None::ReadByte()
{
return 0;
return std::nullopt;
}

bool None::WriteByte(u8 value)
{
return false;
}

bool EncryptedExtension::ReadDeviceDetectPin() const
{
return true;
}

int EncryptedExtension::BusRead(u8 slave_addr, u8 addr, int count, u8* data_out)
u8 EncryptedExtension::ReadByte(u8 addr)
{
if (I2C_ADDR != slave_addr)
return 0;

if (0x00 == addr)
{
// This is where real hardware would update controller data
// We do it in Update() for TAS determinism
// TAS code fails to sync data reads and such..
}

auto const result = RawRead(&m_reg, addr, count, data_out);
u8 result = RawRead(&m_reg, addr);

// Encrypt data read from extension register.
if (ENCRYPTION_ENABLED == m_reg.encryption)
Expand All @@ -109,30 +121,25 @@ int EncryptedExtension::BusRead(u8 slave_addr, u8 addr, int count, u8* data_out)
m_is_key_dirty = false;
}

ext_key.Encrypt(data_out, addr, count);
ext_key.Encrypt(&result, addr, 1);
}

return result;
}

int EncryptedExtension::BusWrite(u8 slave_addr, u8 addr, int count, const u8* data_in)
void EncryptedExtension::WriteByte(u8 addr, u8 value)
{
if (I2C_ADDR != slave_addr)
return 0;

auto const result = RawWrite(&m_reg, addr, count, data_in);
RawWrite(&m_reg, addr, value);

constexpr u8 ENCRYPTION_KEY_DATA_BEGIN = offsetof(Register, encryption_key_data);
constexpr u8 ENCRYPTION_KEY_DATA_END = ENCRYPTION_KEY_DATA_BEGIN + 0x10;

if (addr + count > ENCRYPTION_KEY_DATA_BEGIN && addr < ENCRYPTION_KEY_DATA_END)
if (addr >= ENCRYPTION_KEY_DATA_BEGIN && addr < ENCRYPTION_KEY_DATA_END)
{
// FYI: Real extensions seem to require the key data written in specifically sized chunks.
// We just run the key generation on all writes to the key area.
m_is_key_dirty = true;
}

return result;
}

void EncryptedExtension::Reset()
Expand All @@ -147,6 +154,8 @@ void EncryptedExtension::Reset()

void EncryptedExtension::DoState(PointerWrap& p)
{
I2CSlaveAutoIncrementing::DoState(p);

p.Do(m_reg);

if (p.IsReadMode())
Expand Down
36 changes: 28 additions & 8 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@

#include "Common/ChunkFile.h"
#include "Common/CommonTypes.h"
#include "Common/I2C.h"
#include "Core/HW/WiimoteEmu/Encryption.h"
#include "Core/HW/WiimoteEmu/I2CBus.h"
#include "InputCommon/ControllerEmu/ControllerEmu.h"

#ifdef _MSC_VER
#pragma warning(disable : 4250) // C4250 inherits via dominance - intended behavior here
#endif

namespace WiimoteEmu
{
struct DesiredExtensionState;

class Extension : public ControllerEmu::EmulatedController, public I2CSlave
class Extension : public ControllerEmu::EmulatedController, public virtual Common::I2CSlave
{
public:
explicit Extension(const char* name);
Expand Down Expand Up @@ -56,23 +60,39 @@ class None : public Extension
void Reset() override;
void DoState(PointerWrap& p) override;

int BusRead(u8 slave_addr, u8 addr, int count, u8* data_out) override;
int BusWrite(u8 slave_addr, u8 addr, int count, const u8* data_in) override;
bool StartWrite(u8 slave_addr) override;
bool StartRead(u8 slave_addr) override;
void Stop() override;
std::optional<u8> ReadByte() override;
bool WriteByte(u8 value) override;
};

// This class provides the encryption and initialization behavior of most extensions.
class EncryptedExtension : public Extension
class EncryptedExtension : public Extension, public Common::I2CSlaveAutoIncrementing
{
public:
static constexpr u8 I2C_ADDR = 0x52;
static constexpr int CONTROLLER_DATA_BYTES = 21;

using Extension::Extension;
explicit EncryptedExtension(const char* name)
: Extension(name), I2CSlaveAutoIncrementing(I2C_ADDR)
{
}
EncryptedExtension(const char* config_name, const char* display_name)
: Extension(config_name, display_name), I2CSlaveAutoIncrementing(I2C_ADDR)
{
}

// TODO: This is public for TAS reasons.
// TODO: TAS handles encryption poorly.
EncryptionKey ext_key;

using I2CSlaveAutoIncrementing::ReadByte;
using I2CSlaveAutoIncrementing::StartRead;
using I2CSlaveAutoIncrementing::StartWrite;
using I2CSlaveAutoIncrementing::Stop;
using I2CSlaveAutoIncrementing::WriteByte;

static constexpr int CALIBRATION_CHECKSUM_BYTES = 2;

#pragma pack(push, 1)
Expand Down Expand Up @@ -117,8 +137,8 @@ class EncryptedExtension : public Extension

bool ReadDeviceDetectPin() const override;

int BusRead(u8 slave_addr, u8 addr, int count, u8* data_out) override;
int BusWrite(u8 slave_addr, u8 addr, int count, const u8* data_in) override;
u8 ReadByte(u8 addr) override;
void WriteByte(u8 addr, u8 value) override;
};

class Extension1stParty : public EncryptedExtension
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/WiimoteEmu/ExtensionPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace WiimoteEmu
{
ExtensionPort::ExtensionPort(I2CBus* i2c_bus) : m_i2c_bus(*i2c_bus)
ExtensionPort::ExtensionPort(Common::I2CBusBase* i2c_bus) : m_i2c_bus(*i2c_bus)
{
}

Expand Down
6 changes: 3 additions & 3 deletions Source/Core/Core/HW/WiimoteEmu/ExtensionPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#pragma once

#include "Common/ChunkFile.h"
#include "Common/I2C.h"
#include "Core/HW/WiimoteEmu/Extension/Extension.h"
#include "Core/HW/WiimoteEmu/I2CBus.h"

namespace WiimoteEmu
{
Expand Down Expand Up @@ -35,14 +35,14 @@ class ExtensionPort
static constexpr u8 REPORT_I2C_SLAVE = 0x52;
static constexpr u8 REPORT_I2C_ADDR = 0x00;

explicit ExtensionPort(I2CBus* i2c_bus);
explicit ExtensionPort(Common::I2CBusBase* i2c_bus);

bool IsDeviceConnected() const;
void AttachExtension(Extension* dev);

private:
Extension* m_extension = nullptr;
I2CBus& m_i2c_bus;
Common::I2CBusBase& m_i2c_bus;
};

} // namespace WiimoteEmu
Loading

0 comments on commit b761f72

Please sign in to comment.