From 5f9648417cd3c1debaa9cf1f27fc61d05a253818 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Tue, 30 Aug 2022 12:42:45 -0700 Subject: [PATCH] Create Common/I2C Currently this is a copy of the Wii Remote I2CBus, and is unused. That will change in later commits. --- Source/Core/Common/CMakeLists.txt | 2 + Source/Core/Common/I2C.cpp | 53 ++++++++++++++++++++++ Source/Core/Common/I2C.h | 75 +++++++++++++++++++++++++++++++ Source/Core/DolphinLib.props | 2 + 4 files changed, 132 insertions(+) create mode 100644 Source/Core/Common/I2C.cpp create mode 100644 Source/Core/Common/I2C.h diff --git a/Source/Core/Common/CMakeLists.txt b/Source/Core/Common/CMakeLists.txt index 81431e564c..048fffe2d1 100644 --- a/Source/Core/Common/CMakeLists.txt +++ b/Source/Core/Common/CMakeLists.txt @@ -79,6 +79,8 @@ add_library(common HostDisassembler.h HttpRequest.cpp HttpRequest.h + I2C.cpp + I2C.h Image.cpp Image.h IniFile.cpp diff --git a/Source/Core/Common/I2C.cpp b/Source/Core/Common/I2C.cpp new file mode 100644 index 0000000000..d44f98dffb --- /dev/null +++ b/Source/Core/Common/I2C.cpp @@ -0,0 +1,53 @@ +// Copyright 2022 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "Common/I2C.h" + +#include + +namespace Common +{ +void I2CBus::AddSlave(I2CSlave* slave) +{ + m_slaves.emplace_back(slave); +} + +void I2CBus::RemoveSlave(I2CSlave* slave) +{ + m_slaves.erase(std::remove(m_slaves.begin(), m_slaves.end(), slave), m_slaves.end()); +} + +void I2CBus::Reset() +{ + m_slaves.clear(); +} + +int I2CBus::BusRead(u8 slave_addr, u8 addr, int count, u8* data_out) +{ + for (auto& slave : m_slaves) + { + auto const bytes_read = slave->BusRead(slave_addr, addr, count, data_out); + + // A slave responded, we are done. + if (bytes_read) + return bytes_read; + } + + return 0; +} + +int I2CBus::BusWrite(u8 slave_addr, u8 addr, int count, const u8* data_in) +{ + for (auto& slave : m_slaves) + { + auto const bytes_written = slave->BusWrite(slave_addr, addr, count, data_in); + + // A slave responded, we are done. + if (bytes_written) + return bytes_written; + } + + return 0; +} + +}; // namespace Common diff --git a/Source/Core/Common/I2C.h b/Source/Core/Common/I2C.h new file mode 100644 index 0000000000..1ea1c8198d --- /dev/null +++ b/Source/Core/Common/I2C.h @@ -0,0 +1,75 @@ +// Copyright 2022 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include + +#include "Common/CommonTypes.h" + +namespace Common +{ +class I2CBus; + +class I2CSlave +{ + friend I2CBus; + +protected: + virtual ~I2CSlave() = default; + + virtual int BusRead(u8 slave_addr, u8 addr, int count, u8* data_out) = 0; + virtual int BusWrite(u8 slave_addr, u8 addr, int count, const u8* data_in) = 0; + + template + static int RawRead(T* reg_data, u8 addr, int count, u8* data_out) + { + static_assert(std::is_standard_layout_v && std::is_trivially_copyable_v); + static_assert(0x100 == sizeof(T)); + + // TODO: addr wraps around after 0xff + + u8* src = reinterpret_cast(reg_data) + addr; + count = std::min(count, int(reinterpret_cast(reg_data + 1) - src)); + + std::copy_n(src, count, data_out); + + return count; + } + + template + static int RawWrite(T* reg_data, u8 addr, int count, const u8* data_in) + { + static_assert(std::is_standard_layout_v && std::is_trivially_copyable_v); + static_assert(0x100 == sizeof(T)); + + // TODO: addr wraps around after 0xff + + u8* dst = reinterpret_cast(reg_data) + addr; + count = std::min(count, int(reinterpret_cast(reg_data + 1) - dst)); + + std::copy_n(data_in, count, dst); + + return count; + } +}; + +class I2CBus +{ +public: + void AddSlave(I2CSlave* slave); + void RemoveSlave(I2CSlave* slave); + + void Reset(); + + // TODO: change int to u16 or something + int BusRead(u8 slave_addr, u8 addr, int count, u8* data_out); + int BusWrite(u8 slave_addr, u8 addr, int count, const u8* data_in); + +private: + // Pointers are unowned: + std::vector m_slaves; +}; + +} // namespace Common diff --git a/Source/Core/DolphinLib.props b/Source/Core/DolphinLib.props index a61f0d822a..b6967694ee 100644 --- a/Source/Core/DolphinLib.props +++ b/Source/Core/DolphinLib.props @@ -120,6 +120,7 @@ + @@ -812,6 +813,7 @@ +