From 199db12565c2a32c7bb14a0b20df50147abe5d56 Mon Sep 17 00:00:00 2001 From: Tyler Wilding Date: Fri, 18 Nov 2022 21:17:47 -0500 Subject: [PATCH] input-rec: integrate with new SIO code --- pcsx2/Recording/InputRecording.cpp | 40 ++++++++++++++++++++++++++---- pcsx2/Recording/InputRecording.h | 4 +-- pcsx2/Sio.cpp | 17 +++++++------ pcsx2/Sio.h | 2 +- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/pcsx2/Recording/InputRecording.cpp b/pcsx2/Recording/InputRecording.cpp index 32ed73e195..26b4d4b7d2 100644 --- a/pcsx2/Recording/InputRecording.cpp +++ b/pcsx2/Recording/InputRecording.cpp @@ -606,13 +606,43 @@ void InputRecording::stop() // TODO: Refactor this void InputRecording::ControllerInterrupt(u8 port, size_t fifoSize, u8 dataIn, u8 dataOut) { - // TODO - Multi-Tap Support + m_pad_data_available = data == s_READ_DATA_AND_VIBRATE_QUERY_FIRST_BYTE; +} + +void InputRecording::querySecondByte(const u8 data) +{ + m_pad_data_available &= data == s_READ_DATA_AND_VIBRATE_QUERY_SECOND_BYTE; +} + +void InputRecording::controllerInterrupt(u8 port, size_t fifoSize, u8& dataIn, u8& dataOut) +{ + // TODO - Multi-Tap Support (Qt doesn't support it yet anyway!) + // Keep these safe-guard checks in here, they are input recording only concerns if (fifoSize == 1) - fInterruptFrame = dataIn == READ_DATA_AND_VIBRATE_FIRST_BYTE; + { + queryFirstByte(dataOut); + return; + } else if (fifoSize == 2) { - if (dataOut != READ_DATA_AND_VIBRATE_SECOND_BYTE) - fInterruptFrame = false; + querySecondByte(dataOut); + return; + } + + if (!m_pad_data_available) + { + // bad data / first and second byte checks failed + return; + } + + if (m_controls.isReplaying()) + { + if (!m_file.readKeyBuffer(dataOut, m_frame_counter, port, fifoSize)) + { + InputRec::consoleLog(fmt::format("Failed to read input data at frame {}", m_frame_counter)); + } + // Update controller data state for future VirtualPad / logging usage. + //pads[port].padData->UpdateControllerData(bufIndex, bufVal); } // If there is data to read (previous two bytes looked correct) @@ -622,7 +652,7 @@ void InputRecording::ControllerInterrupt(u8 port, size_t fifoSize, u8 dataIn, u8 const u16 bufIndex = fifoSize - 3; if (state == InputRecordingMode::Replaying) { - if (!m_file.writeKeyBuffer(m_frame_counter, port, bufIndex, bufVal)) + if (!m_file.writeKeyBuffer(m_frame_counter, port, fifoSize, dataOut)) { InputRec::consoleLog(fmt::format("Failed to write input data at frame {}", m_frame_counter)); } diff --git a/pcsx2/Recording/InputRecording.h b/pcsx2/Recording/InputRecording.h index aa269a62c3..b988582167 100644 --- a/pcsx2/Recording/InputRecording.h +++ b/pcsx2/Recording/InputRecording.h @@ -168,7 +168,7 @@ public: void queryFirstByte(const u8 data); void querySecondByte(const u8 bufVal); - void controllerInterrupt(const u8 port, const u16 bufIndex, u8& bufVal); + void controllerInterrupt(const u8 port, const size_t fifoSize, u8& dataIn, u8& dataOut); void incFrameCounter(); u64 getFrameCounter() const; bool isActive() const; @@ -188,7 +188,7 @@ public: private: // - https://github.com/PCSX2/pcsx2/blob/7db9627ff6986c2d3faeecc58525a0e32da2f29f/pcsx2/PAD/Windows/PAD.cpp#L1141 - static constexpr u8 s_READ_DATA_AND_VIBRATE_QUERY_FIRST_BYTE = 0x42; + static constexpr u8 s_READ_DATA_AND_VIBRATE_QUERY_FIRST_BYTE = 0x79; // - https://github.com/PCSX2/pcsx2/blob/7db9627ff6986c2d3faeecc58525a0e32da2f29f/pcsx2/PAD/Windows/PAD.cpp#L1142 static constexpr u8 s_READ_DATA_AND_VIBRATE_QUERY_SECOND_BYTE = 0x5A; diff --git a/pcsx2/Sio.cpp b/pcsx2/Sio.cpp index f9e732d7be..40a230078f 100644 --- a/pcsx2/Sio.cpp +++ b/pcsx2/Sio.cpp @@ -421,14 +421,14 @@ u8 Sio0::Memcard(u8 value) // SIO2 // ============================================================================ -void Sio2::UpdateInputRecording(u8 dataIn, u8 dataOut) +void Sio2::UpdateInputRecording(u8& dataIn, u8& dataOut) { - if (EmuConfig.EnableRecordingTools) + if (EmuConfig.EnableRecordingTools && g_InputRecording.isActive()) { // Ignore multitapped slots, only allow physical ports if (slot == 0) { - g_InputRecording.ControllerInterrupt(port, fifoOut.size(), dataIn, dataOut); + g_InputRecording.controllerInterrupt(port, fifoOut.size(), dataIn, dataOut); } } } @@ -540,9 +540,10 @@ void Sio2::SetRecv1(u32 value) void Sio2::Pad() { // Send PAD our current port, and get back whatever it says the first response byte should be. - const u8 firstResponseByte = PADstartPoll(port, slot); + u8 commandByte = 0x01; + u8 firstResponseByte = PADstartPoll(port, slot); + UpdateInputRecording(commandByte, firstResponseByte); fifoOut.push_back(firstResponseByte); - UpdateInputRecording(0x01, firstResponseByte); // Some games will refuse to read ALL pads, if RECV1 is not set to the CONNECTED value when ANY pad is polled, // REGARDLESS of whether that pad is truly connected or not. SetRecv1(Recv1::CONNECTED); @@ -550,11 +551,11 @@ void Sio2::Pad() // Then for every byte in fifoIn, pass to PAD and see what it kicks back to us. while (!fifoIn.empty()) { - const u8 commandByte = fifoIn.front(); + u8 commandByte = fifoIn.front(); fifoIn.pop_front(); - const u8 responseByte = PADpoll(commandByte); - fifoOut.push_back(responseByte); + u8 responseByte = PADpoll(commandByte); UpdateInputRecording(commandByte, responseByte); + fifoOut.push_back(responseByte); } } diff --git a/pcsx2/Sio.h b/pcsx2/Sio.h index 3ede33433d..0445db7350 100644 --- a/pcsx2/Sio.h +++ b/pcsx2/Sio.h @@ -162,7 +162,7 @@ public: class Sio2 { private: - void UpdateInputRecording(u8 dataIn, u8 dataOut); + void UpdateInputRecording(u8& dataIn, u8& dataOut); public: std::array send3; // 0x1f808200 - 0x1f80823f