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 fixed [TODO].
  • Loading branch information
booto committed Aug 16, 2018
1 parent 60f12e1 commit 7c6813d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 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
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

0 comments on commit 7c6813d

Please sign in to comment.