Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
#include "CommonFramework/Tools/GlobalThreadPools.h"
#include "CommonFramework/Windows/LabChannel_Tests.h"
#include "CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.h"
#include "Controllers/MgbaLink/MgbaLink_Keys_Tests.h"
#include "Controllers/MgbaLink/MgbaLink_Tests.h"
#include "NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_KeyMap_Tests.h"
#include "CommonFramework/Windows/LabSnapshot_Tests.h"
#include "CommonFramework/Windows/LabStateJson_Tests.h"
#include "CommonTools/VisualDetectors/BlackBorderDetector.h"
Expand Down Expand Up @@ -74,6 +76,8 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){
add_tests_AudioFileLoader(ret);
add_tests_LabChannel(ret);
MgbaLink::add_tests(ret);
MgbaLink::add_key_tests(ret);
NintendoSwitch::add_tests_MgbaLinkKeyMap(ret);
add_tests_VideoSource_MgbaLink(ret);
add_tests_LabSnapshot(ret);
add_tests_LabStateJson(ret);
Expand Down
89 changes: 89 additions & 0 deletions SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Keys.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* MgbaLink Keys
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <algorithm>
#include <chrono>
#include <cmath>
#include <format>
#include "MgbaLink_Keys.h"

namespace PokemonAutomation{
namespace MgbaLink{


namespace{

// "duration" in (fractional) GBA frames.
double to_frames(WallDuration duration){
double microseconds = (double)std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
return microseconds * FrameClock::GBA_FRAMES_PER_SECOND / 1000000.0;
}

uint64_t offset_frame(const FrameClock& clock, double frames){
double frame = (double)clock.anchor_frame() + frames;
return frame <= 0 ? 0 : (uint64_t)frame;
}

} // namespace



uint64_t frames_for_duration(WallDuration duration){
if (duration <= WallDuration::zero()){
return 0;
}
return std::max<uint64_t>(1, (uint64_t)std::llround(to_frames(duration)));
}

uint64_t frame_at(const FrameClock& clock, WallClock time){
return offset_frame(clock, std::round(to_frames(time - clock.anchor_time())));
}

uint64_t grant_frame(const FrameClock& clock, WallClock now, uint64_t lead_frames){
return offset_frame(clock, std::floor(to_frames(now - clock.anchor_time()))) + lead_frames;
}



std::optional<KeysCommand> KeySchedule::push(WallClock now, uint16_t mask, WallDuration duration){
if (duration <= WallDuration::zero()){
return std::nullopt;
}
uint64_t start_frame = m_end_frame;
WallClock start_time = m_end_time;
uint64_t earliest = frame_at(m_clock, now) + LEAD_FRAMES;
if (start_frame < earliest){
start_frame = earliest;
start_time = m_clock.timestamp(start_frame);
}

m_end_time = start_time + duration;
m_end_frame = std::max(start_frame + 1, frame_at(m_clock, m_end_time));

if (m_held_mask == mask){
return std::nullopt;
}
m_held_mask = mask;
return KeysCommand{start_frame, mask};
}



void InputLog::record(const KeysCommand& sent, uint64_t frames_late){
m_entries.emplace_back(Entry{sent.frame + frames_late, sent.mask});
}

std::string InputLog::to_text() const{
std::string text;
for (const Entry& entry : m_entries){
text += std::format("{} {}\n", entry.frame, entry.mask);
}
return text;
}


} // namespace MgbaLink
} // namespace PokemonAutomation
133 changes: 133 additions & 0 deletions SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Keys.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/* MgbaLink Keys
*
* From: https://github.com/PokemonAutomation/
*
* The input side of MgbaLink v1 (lab/mgba/PROTOCOL.md on the fork's agents
* branch): GBA key masks, the conversion of app time to emulated frames,
* the frames that "KEYS" and "GRANT" name, and the input log. Pure logic;
* the controller that sends the commands holds the connection.
*
* Programs keep milliseconds; this is the boundary that turns them into
* frames (design: docs/design/Lab-mGBA-Console.md, section 2).
*
*/

#ifndef PokemonAutomation_Controllers_MgbaLink_Keys_H
#define PokemonAutomation_Controllers_MgbaLink_Keys_H

#include <stdint.h>
#include <optional>
#include <string>
#include <vector>
#include "Common/Cpp/Time.h"
#include "MgbaLink_Protocol.h"

namespace PokemonAutomation{
namespace MgbaLink{


// GBA KEYINPUT bits, as mGBA's C.GBA_KEY.
enum GbaKey : uint16_t{
GBA_KEY_NONE = 0,
GBA_KEY_A = 1 << 0,
GBA_KEY_B = 1 << 1,
GBA_KEY_SELECT = 1 << 2,
GBA_KEY_START = 1 << 3,
GBA_KEY_RIGHT = 1 << 4,
GBA_KEY_LEFT = 1 << 5,
GBA_KEY_UP = 1 << 6,
GBA_KEY_DOWN = 1 << 7,
GBA_KEY_R = 1 << 8,
GBA_KEY_L = 1 << 9,
};


// How many frames a state held for "duration" lasts:
// max(1, round(duration * 59.7275 / 1000)), so 17 ms is 1 frame and 80 ms
// is 5. A zero duration holds no frame.
uint64_t frames_for_duration(WallDuration duration);

// The frame app time "time" lands on: F0 + round((time - t0) * 59.7275 / 1000),
// never below frame 0.
uint64_t frame_at(const FrameClock& clock, WallClock time);

// The frame to "GRANT" at "now" in paced mode:
// F0 + floor((now - t0) * 59.7275 / 1000) + lead_frames.
uint64_t grant_frame(const FrameClock& clock, WallClock now, uint64_t lead_frames);


// One "KEYS <frame> <mask>" command.
struct KeysCommand{
uint64_t frame = 0;
uint16_t mask = 0;

friend bool operator==(const KeysCommand&, const KeysCommand&) = default;
};


// Turns the controller's stream of states, each held for a duration, into
// "KEYS" commands on frames. States follow each other without gaps: a state
// starts on the frame the previous one ended, unless that frame has already
// passed (the controller was idle), in which case it starts LEAD_FRAMES
// after "now". The end of each state is placed from app time, so rounding
// does not add up over many states, but every state lasts at least one
// frame. Not thread-safe.
class KeySchedule{
public:
// The pacer grants this far ahead, so commands for the next frames
// still reach the bridge before it runs them.
static constexpr uint64_t LEAD_FRAMES = 2;

explicit KeySchedule(FrameClock clock)
: m_clock(clock)
, m_end_time(clock.anchor_time())
, m_end_frame(clock.anchor_frame())
{}

// Appends "mask" held for "duration". Returns the command to send, or
// nullopt if the keys do not change (or "duration" is zero, which holds
// nothing and is dropped).
std::optional<KeysCommand> push(WallClock now, uint16_t mask, WallDuration duration);

// The frame the last state ends on: the next state starts there.
uint64_t end_frame() const{
return m_end_frame;
}

private:
FrameClock m_clock;
WallClock m_end_time;
uint64_t m_end_frame;
std::optional<uint16_t> m_held_mask;
};


// What landed on which frame: one entry per "KEYS" sent, at the frame the
// bridge applied it (the frame named, plus how late it was). With the start
// state this replays the run in mGBA without the app. Not thread-safe.
class InputLog{
public:
struct Entry{
uint64_t frame = 0;
uint16_t mask = 0;

friend bool operator==(const Entry&, const Entry&) = default;
};

void record(const KeysCommand& sent, uint64_t frames_late);

const std::vector<Entry>& entries() const{
return m_entries;
}

// One "<frame> <mask>" line per entry, in decimal.
std::string to_text() const;

private:
std::vector<Entry> m_entries;
};


} // namespace MgbaLink
} // namespace PokemonAutomation
#endif
Loading
Loading