SI: Expose Commands constants and switch to enum class

This commit is contained in:
Bonta 2021-07-04 13:02:50 +02:00
parent 8ee21acf34
commit fdcee30a3d
10 changed files with 69 additions and 131 deletions

View File

@ -41,6 +41,40 @@ enum TSIDevices : u32
SI_AM_BASEBOARD = 0x10110800 // gets ORd with dipswitch state SI_AM_BASEBOARD = 0x10110800 // gets ORd with dipswitch state
}; };
// Commands
enum class EBufferCommands : u8
{
CMD_STATUS = 0x00,
CMD_READ_GBA = 0x14,
CMD_WRITE_GBA = 0x15,
CMD_DIRECT = 0x40,
CMD_ORIGIN = 0x41,
CMD_RECALIBRATE = 0x42,
CMD_DIRECT_KB = 0x54,
CMD_RESET = 0xFF
};
enum class EDirectCommands : u8
{
CMD_FORCE = 0x30,
CMD_WRITE = 0x40,
CMD_POLL = 0x54
};
union UCommand
{
u32 hex = 0;
struct
{
u32 parameter1 : 8;
u32 parameter2 : 8;
u32 command : 8;
u32 : 8;
};
UCommand() = default;
UCommand(u32 value) : hex{value} {}
};
// For configuration use, since some devices can have the same SI Device ID // For configuration use, since some devices can have the same SI Device ID
enum SIDevices : int enum SIDevices : int
{ {

View File

@ -19,8 +19,8 @@ CSIDevice_DanceMat::CSIDevice_DanceMat(SIDevices device, int device_number)
int CSIDevice_DanceMat::RunBuffer(u8* buffer, int request_length) int CSIDevice_DanceMat::RunBuffer(u8* buffer, int request_length)
{ {
// Read the command // Read the command
EBufferCommands command = static_cast<EBufferCommands>(buffer[0]); const auto command = static_cast<EBufferCommands>(buffer[0]);
if (command == CMD_RESET) if (command == EBufferCommands::CMD_STATUS)
{ {
ISIDevice::RunBuffer(buffer, request_length); ISIDevice::RunBuffer(buffer, request_length);

View File

@ -34,14 +34,6 @@ int s_num_connected;
Common::Flag s_server_running; Common::Flag s_server_running;
} // namespace } // namespace
enum EJoybusCmds
{
CMD_RESET = 0xff,
CMD_STATUS = 0x00,
CMD_READ = 0x14,
CMD_WRITE = 0x15
};
constexpr auto GC_BITS_PER_SECOND = 200000; constexpr auto GC_BITS_PER_SECOND = 200000;
constexpr auto GBA_BITS_PER_SECOND = 250000; constexpr auto GBA_BITS_PER_SECOND = 250000;
constexpr auto GC_STOP_BIT_NS = 6500; constexpr auto GC_STOP_BIT_NS = 6500;
@ -50,7 +42,7 @@ constexpr auto SEND_MAX_SIZE = 5, RECV_MAX_SIZE = 5;
// --- GameBoy Advance "Link Cable" --- // --- GameBoy Advance "Link Cable" ---
static int GetTransferTime(u8 cmd) static int GetTransferTime(EBufferCommands cmd)
{ {
u64 gc_bytes_transferred = 1; u64 gc_bytes_transferred = 1;
u64 gba_bytes_transferred = 1; u64 gba_bytes_transferred = 1;
@ -58,18 +50,18 @@ static int GetTransferTime(u8 cmd)
switch (cmd) switch (cmd)
{ {
case CMD_RESET: case EBufferCommands::CMD_RESET:
case CMD_STATUS: case EBufferCommands::CMD_STATUS:
{ {
gba_bytes_transferred = 3; gba_bytes_transferred = 3;
break; break;
} }
case CMD_READ: case EBufferCommands::CMD_READ_GBA:
{ {
gba_bytes_transferred = 5; gba_bytes_transferred = 5;
break; break;
} }
case CMD_WRITE: case EBufferCommands::CMD_WRITE_GBA:
{ {
gc_bytes_transferred = 5; gc_bytes_transferred = 5;
break; break;
@ -249,10 +241,10 @@ void GBASockServer::Send(const u8* si_buffer)
for (size_t i = 0; i < send_data.size(); i++) for (size_t i = 0; i < send_data.size(); i++)
send_data[i] = si_buffer[i]; send_data[i] = si_buffer[i];
u8 cmd = send_data[0]; const auto cmd = static_cast<EBufferCommands>(send_data[0]);
sf::Socket::Status status; sf::Socket::Status status;
if (cmd == CMD_WRITE) if (cmd == EBufferCommands::CMD_WRITE_GBA)
status = m_client->send(send_data.data(), send_data.size()); status = m_client->send(send_data.data(), send_data.size());
else else
status = m_client->send(send_data.data(), 1); status = m_client->send(send_data.data(), 1);
@ -334,7 +326,7 @@ int CSIDevice_GBA::RunBuffer(u8* buffer, int request_length)
return -1; return -1;
} }
m_last_cmd = buffer[0]; m_last_cmd = static_cast<EBufferCommands>(buffer[0]);
m_timestamp_sent = CoreTiming::GetTicks(); m_timestamp_sent = CoreTiming::GetTicks();
m_next_action = NextAction::WaitTransferTime; m_next_action = NextAction::WaitTransferTime;
return 0; return 0;
@ -355,11 +347,11 @@ int CSIDevice_GBA::RunBuffer(u8* buffer, int request_length)
u8 bytes = 1; u8 bytes = 1;
switch (m_last_cmd) switch (m_last_cmd)
{ {
case CMD_RESET: case EBufferCommands::CMD_RESET:
case CMD_STATUS: case EBufferCommands::CMD_STATUS:
bytes = 3; bytes = 3;
break; break;
case CMD_READ: case EBufferCommands::CMD_READ_GBA:
bytes = 5; bytes = 5;
break; break;
default: default:
@ -372,8 +364,9 @@ int CSIDevice_GBA::RunBuffer(u8* buffer, int request_length)
return -1; return -1;
#ifdef _DEBUG #ifdef _DEBUG
const Common::Log::LOG_LEVELS log_level = const Common::Log::LOG_LEVELS log_level =
(m_last_cmd == CMD_STATUS || m_last_cmd == CMD_RESET) ? Common::Log::LERROR : (m_last_cmd == EBufferCommands::CMD_STATUS || m_last_cmd == EBufferCommands::CMD_RESET) ?
Common::Log::LWARNING; Common::Log::LERROR :
Common::Log::LWARNING;
GENERIC_LOG_FMT(Common::Log::SERIALINTERFACE, log_level, GENERIC_LOG_FMT(Common::Log::SERIALINTERFACE, log_level,
"{} [< {:02x}{:02x}{:02x}{:02x}{:02x}] ({})", "{} [< {:02x}{:02x}{:02x}{:02x}{:02x}] ({})",
m_device_number, buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], m_device_number, buffer[0], buffer[1], buffer[2], buffer[3], buffer[4],

View File

@ -60,7 +60,7 @@ private:
GBASockServer m_sock_server; GBASockServer m_sock_server;
NextAction m_next_action = NextAction::SendCommand; NextAction m_next_action = NextAction::SendCommand;
u8 m_last_cmd; EBufferCommands m_last_cmd;
u64 m_timestamp_sent = 0; u64 m_timestamp_sent = 0;
}; };
} // namespace SerialInterface } // namespace SerialInterface

View File

@ -45,20 +45,20 @@ int CSIDevice_GCController::RunBuffer(u8* buffer, int request_length)
return -1; return -1;
// Read the command // Read the command
EBufferCommands command = static_cast<EBufferCommands>(buffer[0]); const auto command = static_cast<EBufferCommands>(buffer[0]);
// Handle it // Handle it
switch (command) switch (command)
{ {
case CMD_RESET: case EBufferCommands::CMD_STATUS:
case CMD_ID: case EBufferCommands::CMD_RESET:
{ {
u32 id = Common::swap32(SI_GC_CONTROLLER); u32 id = Common::swap32(SI_GC_CONTROLLER);
std::memcpy(buffer, &id, sizeof(id)); std::memcpy(buffer, &id, sizeof(id));
return sizeof(id); return sizeof(id);
} }
case CMD_DIRECT: case EBufferCommands::CMD_DIRECT:
{ {
INFO_LOG_FMT(SERIALINTERFACE, "PAD - Direct (Request length: {})", request_length); INFO_LOG_FMT(SERIALINTERFACE, "PAD - Direct (Request length: {})", request_length);
u32 high, low; u32 high, low;
@ -71,7 +71,7 @@ int CSIDevice_GCController::RunBuffer(u8* buffer, int request_length)
return sizeof(high) + sizeof(low); return sizeof(high) + sizeof(low);
} }
case CMD_ORIGIN: case EBufferCommands::CMD_ORIGIN:
{ {
INFO_LOG_FMT(SERIALINTERFACE, "PAD - Get Origin"); INFO_LOG_FMT(SERIALINTERFACE, "PAD - Get Origin");
@ -84,7 +84,7 @@ int CSIDevice_GCController::RunBuffer(u8* buffer, int request_length)
} }
// Recalibrate (FiRES: i am not 100 percent sure about this) // Recalibrate (FiRES: i am not 100 percent sure about this)
case CMD_RECALIBRATE: case EBufferCommands::CMD_RECALIBRATE:
{ {
INFO_LOG_FMT(SERIALINTERFACE, "PAD - Recalibrate"); INFO_LOG_FMT(SERIALINTERFACE, "PAD - Recalibrate");
@ -290,13 +290,7 @@ void CSIDevice_GCController::SendCommand(u32 command, u8 poll)
{ {
UCommand controller_command(command); UCommand controller_command(command);
switch (controller_command.command) if (static_cast<EDirectCommands>(controller_command.command) == EDirectCommands::CMD_WRITE)
{
// Costis sent it in some demos :)
case 0x00:
break;
case CMD_WRITE:
{ {
const u32 type = controller_command.parameter1; // 0 = stop, 1 = rumble, 2 = stop hard const u32 type = controller_command.parameter1; // 0 = stop, 1 = rumble, 2 = stop hard
@ -317,15 +311,12 @@ void CSIDevice_GCController::SendCommand(u32 command, u8 poll)
INFO_LOG_FMT(SERIALINTERFACE, "PAD {} set to mode {}", m_device_number, m_mode); INFO_LOG_FMT(SERIALINTERFACE, "PAD {} set to mode {}", m_device_number, m_mode);
} }
} }
break; else if (controller_command.command != 0x00)
default:
{ {
// Costis sent 0x00 in some demos :)
ERROR_LOG_FMT(SERIALINTERFACE, "Unknown direct command ({:#x})", command); ERROR_LOG_FMT(SERIALINTERFACE, "Unknown direct command ({:#x})", command);
PanicAlertFmt("SI: Unknown direct command"); PanicAlertFmt("SI: Unknown direct command");
} }
break;
}
} }
// Savestate support // Savestate support

View File

@ -12,16 +12,6 @@ namespace SerialInterface
class CSIDevice_GCController : public ISIDevice class CSIDevice_GCController : public ISIDevice
{ {
protected: protected:
// Commands
enum EBufferCommands
{
CMD_RESET = 0x00,
CMD_DIRECT = 0x40,
CMD_ORIGIN = 0x41,
CMD_RECALIBRATE = 0x42,
CMD_ID = 0xff,
};
struct SOrigin struct SOrigin
{ {
u16 button; u16 button;
@ -35,25 +25,6 @@ protected:
u8 unk_5; u8 unk_5;
}; };
enum EDirectCommands
{
CMD_WRITE = 0x40
};
union UCommand
{
u32 hex = 0;
struct
{
u32 parameter1 : 8;
u32 parameter2 : 8;
u32 command : 8;
u32 : 8;
};
UCommand() = default;
UCommand(u32 value) : hex{value} {}
};
enum EButtonCombo enum EButtonCombo
{ {
COMBO_NONE = 0, COMBO_NONE = 0,

View File

@ -25,13 +25,13 @@ int CSIDevice_GCSteeringWheel::RunBuffer(u8* buffer, int request_length)
ISIDevice::RunBuffer(buffer, request_length); ISIDevice::RunBuffer(buffer, request_length);
// Read the command // Read the command
EBufferCommands command = static_cast<EBufferCommands>(buffer[0]); const auto command = static_cast<EBufferCommands>(buffer[0]);
// Handle it // Handle it
switch (command) switch (command)
{ {
case CMD_RESET: case EBufferCommands::CMD_STATUS:
case CMD_ID: case EBufferCommands::CMD_RESET:
{ {
u32 id = Common::swap32(SI_GC_STEERING); u32 id = Common::swap32(SI_GC_STEERING);
std::memcpy(buffer, &id, sizeof(id)); std::memcpy(buffer, &id, sizeof(id));
@ -101,7 +101,7 @@ void CSIDevice_GCSteeringWheel::SendCommand(u32 command, u8 poll)
{ {
UCommand wheel_command(command); UCommand wheel_command(command);
if (wheel_command.command == CMD_FORCE) if (static_cast<EDirectCommands>(wheel_command.command) == EDirectCommands::CMD_FORCE)
{ {
// get the correct pad number that should rumble locally when using netplay // get the correct pad number that should rumble locally when using netplay
const int pad_num = NetPlay_InGamePadToLocalPad(m_device_number); const int pad_num = NetPlay_InGamePadToLocalPad(m_device_number);

View File

@ -17,21 +17,6 @@ public:
void SendCommand(u32 command, u8 poll) override; void SendCommand(u32 command, u8 poll) override;
private: private:
// Commands
enum EBufferCommands
{
CMD_RESET = 0x00,
CMD_ORIGIN = 0x41,
CMD_RECALIBRATE = 0x42,
CMD_ID = 0xff,
};
enum EDirectCommands
{
CMD_FORCE = 0x30,
CMD_WRITE = 0x40
};
enum class ForceCommandType : u8 enum class ForceCommandType : u8
{ {
MotorOn = 0x03, MotorOn = 0x03,

View File

@ -31,15 +31,15 @@ int CSIDevice_Keyboard::RunBuffer(u8* buffer, int request_length)
// Handle it // Handle it
switch (command) switch (command)
{ {
case CMD_RESET: case EBufferCommands::CMD_STATUS:
case CMD_ID: case EBufferCommands::CMD_RESET:
{ {
u32 id = Common::swap32(SI_GC_KEYBOARD); u32 id = Common::swap32(SI_GC_KEYBOARD);
std::memcpy(buffer, &id, sizeof(id)); std::memcpy(buffer, &id, sizeof(id));
return sizeof(id); return sizeof(id);
} }
case CMD_DIRECT: case EBufferCommands::CMD_DIRECT_KB:
{ {
INFO_LOG_FMT(SERIALINTERFACE, "Keyboard - Direct (Request Length: {})", request_length); INFO_LOG_FMT(SERIALINTERFACE, "Keyboard - Direct (Request Length: {})", request_length);
u32 high, low; u32 high, low;
@ -84,23 +84,15 @@ void CSIDevice_Keyboard::SendCommand(u32 command, u8 poll)
{ {
UCommand keyboard_command(command); UCommand keyboard_command(command);
switch (keyboard_command.command) if (static_cast<EDirectCommands>(keyboard_command.command) == EDirectCommands::CMD_POLL)
{
case 0x00:
break;
case CMD_POLL:
{ {
m_counter++; m_counter++;
m_counter &= 15; m_counter &= 15;
} }
break; else if (keyboard_command.command != 0x00)
default:
{ {
ERROR_LOG_FMT(SERIALINTERFACE, "Unknown direct command ({:#x})", command); ERROR_LOG_FMT(SERIALINTERFACE, "Unknown direct command ({:#x})", command);
} }
break;
}
} }
void CSIDevice_Keyboard::DoState(PointerWrap& p) void CSIDevice_Keyboard::DoState(PointerWrap& p)

View File

@ -32,34 +32,6 @@ public:
void DoState(PointerWrap& p) override; void DoState(PointerWrap& p) override;
protected: protected:
// Commands
enum EBufferCommands
{
CMD_RESET = 0x00,
CMD_DIRECT = 0x54,
CMD_ID = 0xff,
};
enum EDirectCommands
{
CMD_WRITE = 0x40,
CMD_POLL = 0x54
};
union UCommand
{
u32 hex = 0;
struct
{
u32 parameter1 : 8;
u32 parameter2 : 8;
u32 command : 8;
u32 : 8;
};
UCommand() = default;
UCommand(u32 value) : hex{value} {}
};
// PADAnalogMode // PADAnalogMode
u8 m_mode = 0; u8 m_mode = 0;