Skip to content

Commit

Permalink
SI: Add 16bit accessors for SI IO buffer
Browse files Browse the repository at this point in the history
Dolphin has traditionally treated the SI IO buffer (128 bytes) as a set of
32 little endian u32s. This works out fine if you only ever read/write
using aligned 32bit accesses. Different sized accesses or misaligned reads
will mess it up. Byte swapping reads/writes will fix this up, but all the
SI devices that use the SI IO buffer need to be adjusted.
  • Loading branch information
booto committed Sep 12, 2018
1 parent 0fbe1a2 commit 3825e2e
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 30 deletions.
30 changes: 27 additions & 3 deletions Source/Core/Core/HW/SI/SI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "Common/ChunkFile.h"
#include "Common/CommonTypes.h"
#include "Common/Swap.h"
#include "Core/ConfigManager.h"
#include "Core/CoreTiming.h"
#include "Core/HW/MMIO.h"
Expand Down Expand Up @@ -50,6 +51,7 @@ enum
SI_COM_CSR = 0x34,
SI_STATUS_REG = 0x38,
SI_EXI_CLOCK_COUNT = 0x3C,
SI_IO_BUFFER = 0x80,
};

// SI Channel Output
Expand Down Expand Up @@ -414,12 +416,34 @@ void Shutdown()
void RegisterMMIO(MMIO::Mapping* mmio, u32 base)
{
// Register SI buffer direct accesses.
const u32 io_buffer_base = base | SI_IO_BUFFER;
for (size_t i = 0; i < s_si_buffer.size(); i += sizeof(u32))
{
const u32 address = base | static_cast<u32>(s_si_buffer.size() + i);
const u32 address = base | static_cast<u32>(io_buffer_base + i);

mmio->Register(address, MMIO::DirectRead<u32>((u32*)&s_si_buffer[i]),
MMIO::DirectWrite<u32>((u32*)&s_si_buffer[i]));
mmio->Register(address, MMIO::ComplexRead<u32>([i](u32) {
u32 val;
std::memcpy(&val, &s_si_buffer[i], sizeof(val));
return Common::swap32(val);
}),
MMIO::ComplexWrite<u32>([i](u32, u32 val) {
val = Common::swap32(val);
std::memcpy(&s_si_buffer[i], &val, sizeof(val));
}));
}
for (size_t i = 0; i < s_si_buffer.size(); i += sizeof(u16))
{
const u32 address = base | static_cast<u32>(io_buffer_base + i);

mmio->Register(address, MMIO::ComplexRead<u16>([i](u32) {
u16 val;
std::memcpy(&val, &s_si_buffer[i], sizeof(val));
return Common::swap16(val);
}),
MMIO::ComplexWrite<u16>([i](u32, u16 val) {
val = Common::swap16(val);
std::memcpy(&s_si_buffer[i], &val, sizeof(val));
}));
}

// In and out for the 4 SI channels.
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/SI/SI_Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int ISIDevice::RunBuffer(u8* buffer, int length)

while (num < length)
{
temp += StringFromFormat("0x%02x ", buffer[num ^ 3]);
temp += StringFromFormat("0x%02x ", buffer[num]);
num++;

if ((num % 8) == 0)
Expand Down
5 changes: 3 additions & 2 deletions Source/Core/Core/HW/SI/SI_DeviceDanceMat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cstring>

#include "Common/CommonTypes.h"
#include "Common/Swap.h"
#include "InputCommon/GCPadStatus.h"

namespace SerialInterface
Expand All @@ -19,13 +20,13 @@ CSIDevice_DanceMat::CSIDevice_DanceMat(SIDevices device, int device_number)
int CSIDevice_DanceMat::RunBuffer(u8* buffer, int length)
{
// Read the command
EBufferCommands command = static_cast<EBufferCommands>(buffer[3]);
EBufferCommands command = static_cast<EBufferCommands>(buffer[0]);

if (command == CMD_RESET)
{
ISIDevice::RunBuffer(buffer, length);

constexpr u32 id = SI_DANCEMAT;
u32 id = Common::swap32(SI_DANCEMAT);
std::memcpy(buffer, &id, sizeof(id));
}
else
Expand Down
21 changes: 11 additions & 10 deletions Source/Core/Core/HW/SI/SI_DeviceGBA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "Common/CommonTypes.h"
#include "Common/Flag.h"
#include "Common/Logging/Log.h"
#include "Common/Swap.h"
#include "Common/Thread.h"
#include "Core/CoreTiming.h"
#include "Core/HW/SI/SI_Device.h"
Expand All @@ -32,7 +33,7 @@ std::mutex s_cs_gba;
std::mutex s_cs_gba_clk;
int s_num_connected;
Common::Flag s_server_running;
}
} // namespace

enum EJoybusCmds
{
Expand Down Expand Up @@ -235,7 +236,7 @@ void GBASockServer::Send(const u8* si_buffer)

std::array<u8, SEND_MAX_SIZE> send_data;
for (size_t i = 0; i < send_data.size(); i++)
send_data[i] = si_buffer[i ^ 3];
send_data[i] = si_buffer[i];

u8 cmd = send_data[0];
if (cmd != CMD_STATUS)
Expand Down Expand Up @@ -281,7 +282,7 @@ int GBASockServer::Receive(u8* si_buffer)
}

for (size_t i = 0; i < recv_data.size(); i++)
si_buffer[i ^ 3] = recv_data[i];
si_buffer[i] = recv_data[i];
return static_cast<int>(std::min(num_received, recv_data.size()));
}

Expand All @@ -299,23 +300,23 @@ int CSIDevice_GBA::RunBuffer(u8* buffer, int length)
if (m_sock_server.Connect())
{
#ifdef _DEBUG
NOTICE_LOG(SERIALINTERFACE, "%01d cmd %02x [> %02x%02x%02x%02x]", m_device_number, buffer[3],
buffer[2], buffer[1], buffer[0], buffer[7]);
NOTICE_LOG(SERIALINTERFACE, "%01d cmd %02x [> %02x%02x%02x%02x]", m_device_number, buffer[0],
buffer[1], buffer[2], buffer[3], buffer[4]);
#endif
m_sock_server.Send(buffer);
}
else
{
constexpr u32 reply = SI_ERROR_NO_RESPONSE;
u32 reply = Common::swap32(SI_ERROR_NO_RESPONSE);
std::memcpy(buffer, &reply, sizeof(reply));
return sizeof(reply);
}
m_last_cmd = buffer[3];
m_last_cmd = buffer[0];
m_timestamp_sent = CoreTiming::GetTicks();
m_next_action = NextAction::WaitTransferTime;
}

// [[fallthrough]]
// [[fallthrough]]b
case NextAction::WaitTransferTime:
{
int elapsed_time = static_cast<int>(CoreTiming::GetTicks() - m_timestamp_sent);
Expand All @@ -332,7 +333,7 @@ int CSIDevice_GBA::RunBuffer(u8* buffer, int length)
m_next_action = NextAction::SendCommand;
if (num_data_received == 0)
{
constexpr u32 reply = SI_ERROR_NO_RESPONSE;
u32 reply = Common::swap32(SI_ERROR_NO_RESPONSE);
std::memcpy(buffer, &reply, sizeof(reply));
return sizeof(reply);
}
Expand All @@ -342,7 +343,7 @@ int CSIDevice_GBA::RunBuffer(u8* buffer, int length)
LogTypes::LWARNING;
GENERIC_LOG(LogTypes::SERIALINTERFACE, log_level,
"%01d [< %02x%02x%02x%02x%02x] (%i)", m_device_number,
buffer[3], buffer[2], buffer[1], buffer[0], buffer[7], num_data_received);
buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], num_data_received);
#endif
return num_data_received;
}
Expand Down
15 changes: 8 additions & 7 deletions Source/Core/Core/HW/SI/SI_DeviceGCController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Common/CommonTypes.h"
#include "Common/Logging/Log.h"
#include "Common/MsgHandler.h"
#include "Common/Swap.h"
#include "Core/CoreTiming.h"
#include "Core/HW/GCPad.h"
#include "Core/HW/ProcessorInterface.h"
Expand Down Expand Up @@ -50,21 +51,21 @@ int CSIDevice_GCController::RunBuffer(u8* buffer, int length)
GCPadStatus pad_status = GetPadStatus();
if (!pad_status.isConnected)
{
constexpr u32 reply = SI_ERROR_NO_RESPONSE;
u32 reply = Common::swap32(SI_ERROR_NO_RESPONSE);
std::memcpy(buffer, &reply, sizeof(reply));
return 4;
}

// Read the command
EBufferCommands command = static_cast<EBufferCommands>(buffer[3]);
EBufferCommands command = static_cast<EBufferCommands>(buffer[0]);

// Handle it
switch (command)
{
case CMD_RESET:
case CMD_ID:
{
constexpr u32 id = SI_GC_CONTROLLER;
u32 id = Common::swap32(SI_GC_CONTROLLER);
std::memcpy(buffer, &id, sizeof(id));
break;
}
Expand All @@ -76,8 +77,8 @@ int CSIDevice_GCController::RunBuffer(u8* buffer, int length)
GetData(high, low);
for (int i = 0; i < (length - 1) / 2; i++)
{
buffer[i + 0] = (high >> (i * 8)) & 0xff;
buffer[i + 4] = (low >> (i * 8)) & 0xff;
buffer[i + 0] = (high >> (24 - (i * 8))) & 0xff;
buffer[i + 4] = (low >> (24 - (i * 8))) & 0xff;
}
}
break;
Expand All @@ -92,7 +93,7 @@ int CSIDevice_GCController::RunBuffer(u8* buffer, int length)
u8* calibration = reinterpret_cast<u8*>(&m_origin);
for (int i = 0; i < (int)sizeof(SOrigin); i++)
{
buffer[i ^ 3] = *calibration++;
buffer[i] = *calibration++;
}
}
break;
Expand All @@ -108,7 +109,7 @@ int CSIDevice_GCController::RunBuffer(u8* buffer, int length)
u8* calibration = reinterpret_cast<u8*>(&m_origin);
for (int i = 0; i < (int)sizeof(SOrigin); i++)
{
buffer[i ^ 3] = *calibration++;
buffer[i] = *calibration++;
}
}
break;
Expand Down
5 changes: 3 additions & 2 deletions Source/Core/Core/HW/SI/SI_DeviceGCSteeringWheel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "Common/CommonTypes.h"
#include "Common/Logging/Log.h"
#include "Common/Swap.h"
#include "Core/HW/GCPad.h"

namespace SerialInterface
Expand All @@ -23,15 +24,15 @@ int CSIDevice_GCSteeringWheel::RunBuffer(u8* buffer, int length)
ISIDevice::RunBuffer(buffer, length);

// Read the command
EBufferCommands command = static_cast<EBufferCommands>(buffer[3]);
EBufferCommands command = static_cast<EBufferCommands>(buffer[0]);

// Handle it
switch (command)
{
case CMD_RESET:
case CMD_ID:
{
constexpr u32 id = SI_GC_STEERING;
u32 id = Common::swap32(SI_GC_STEERING);
std::memcpy(buffer, &id, sizeof(id));
break;
}
Expand Down
9 changes: 5 additions & 4 deletions Source/Core/Core/HW/SI/SI_DeviceKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Common/ChunkFile.h"
#include "Common/CommonTypes.h"
#include "Common/Logging/Log.h"
#include "Common/Swap.h"
#include "Core/HW/GCKeyboard.h"
#include "InputCommon/KeyboardStatus.h"

Expand All @@ -26,15 +27,15 @@ int CSIDevice_Keyboard::RunBuffer(u8* buffer, int length)
ISIDevice::RunBuffer(buffer, length);

// Read the command
EBufferCommands command = static_cast<EBufferCommands>(buffer[3]);
EBufferCommands command = static_cast<EBufferCommands>(buffer[0]);

// Handle it
switch (command)
{
case CMD_RESET:
case CMD_ID:
{
constexpr u32 id = SI_GC_KEYBOARD;
u32 id = Common::swap32(SI_GC_KEYBOARD);
std::memcpy(buffer, &id, sizeof(id));
break;
}
Expand All @@ -46,8 +47,8 @@ int CSIDevice_Keyboard::RunBuffer(u8* buffer, int length)
GetData(high, low);
for (int i = 0; i < (length - 1) / 2; i++)
{
buffer[i + 0] = (high >> (i * 8)) & 0xff;
buffer[i + 4] = (low >> (i * 8)) & 0xff;
buffer[i + 0] = (high >> (24 - (i * 8))) & 0xff;
buffer[i + 4] = (low >> (24 - (i * 8))) & 0xff;
}
}
break;
Expand Down
3 changes: 2 additions & 1 deletion Source/Core/Core/HW/SI/SI_DeviceNull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Refer to the license.txt file included.

#include "Core/HW/SI/SI_DeviceNull.h"
#include "Common/Swap.h"

#include <cstring>

Expand All @@ -15,7 +16,7 @@ CSIDevice_Null::CSIDevice_Null(SIDevices device, int device_number)

int CSIDevice_Null::RunBuffer(u8* buffer, int length)
{
constexpr u32 reply = SI_ERROR_NO_RESPONSE;
u32 reply = Common::swap32(SI_ERROR_NO_RESPONSE);
std::memcpy(buffer, &reply, sizeof(reply));
return 4;
}
Expand Down

0 comments on commit 3825e2e

Please sign in to comment.