Files
MesenCE/Core/Gameboy/GbControlManager.cpp
NovaSquirrelandSour 325c78fcbd GB: Link cable support with two local consoles (#77)
* GB: Link cable support with two local consoles

* GB: Fix crash when color blending is enabled

* GB: Fix formatting

* GB: Link UI fixes, control fixes, audio fixes

Copied from Sour's branch. Also a separate change to use separate savefiles.

* GB: Try to avoid crashes from buffer overflows

* GB: Separate savefiles with linked multiplayer

* GB: Add support for faster link speed

* GB: Move enum to correct file

* GB: Refactor, move serial transfer to function

* -Fixed secondary console sometimes running far ahead of the main console when frame counters did not match (e.g because one console had their LCD disabled longer, etc.) This would cause buffer overflows in the audio buffer.
-Fixed screen tearing when both consoles are not perfectly in sync (because of the LCD being disabled for a different amount of time, etc.)
-Fixed main console not calling PlayQueuedAudio properly mid-frame
-Reuse VS Dualsystem code to merge audio/video when possible/simple and moved that to AvMergeUtilities

Some visual glitches can appear on the secondary screen (for GB) while rewinding, but this can't really be fixed.

* Fixed warning (unused local variable)

* Prevent loading single-console save states when in dual-console modes

* Disable debugger for VS DualSystem's subconsole. Fixes issues when debugging VS DualSystem games

* Code cleanup

* Rework solution to disable debugger on second console to improve performance
Added InternalRunFrame for nes & gb to avoid checking for the presence of the second console after each cpu instruction

* Don't enable the linked GB option by default

* Removed unused function

* Netplay: Force game to reload after updating emulation settings when connecting to the server (or when the host changes game)
This ensures that e.g the GB link cable setting is applied and that the game loads correctly, regardless of the client's link cable settings (otherwise the game could load without the link mode enabled, etc.)

---------

Co-authored-by: Sour <[email protected]>
2026-05-15 02:35:27 -07:00

177 lines
5.1 KiB
C++

#include "pch.h"
#include "Gameboy/Gameboy.h"
#include "Gameboy/GbMemoryManager.h"
#include "Gameboy/GbControlManager.h"
#include "Gameboy/Input/GbController.h"
#include "SNES/Coprocessors/SGB/SuperGameboy.h"
#include "Shared/Emulator.h"
#include "Shared/EmuSettings.h"
#include "Shared/KeyManager.h"
#include "Shared/SystemActionManager.h"
#include <functional>
GbControlManager::GbControlManager(Emulator* emu, Gameboy* console) : BaseControlManager(emu, CpuType::Gameboy)
{
_emu = emu;
_console = console;
if(!console->IsPrimaryConsole()) {
RegisterInputProvider(this);
}
}
GbControlManager::~GbControlManager()
{
UnregisterInputProvider(this);
}
GbControlManagerState GbControlManager::GetState()
{
return _state;
}
shared_ptr<BaseControlDevice> GbControlManager::CreateControllerDevice(ControllerType type, uint8_t port)
{
if(_console->IsSgb()) {
return nullptr;
}
shared_ptr<BaseControlDevice> device;
GameboyConfig& cfg = _emu->GetSettings()->GetGameboyConfig();
switch(type) {
default:
case ControllerType::None: break;
case ControllerType::GameboyController: device.reset(new GbController(_emu, port, port == 0 ? cfg.Controller.Keys : cfg.LinkedController.Keys)); break;
}
return device;
}
void GbControlManager::UpdateControlDevices()
{
GameboyConfig cfg = _emu->GetSettings()->GetGameboyConfig();
if(_emu->GetSettings()->IsEqual(_prevConfig, cfg) && _controlDevices.size() > 0) {
//Do nothing if configuration is unchanged
return;
}
auto lock = _deviceLock.AcquireSafe();
ClearDevices();
shared_ptr<BaseControlDevice> device(CreateControllerDevice(ControllerType::GameboyController, 0));
if(device) {
RegisterControlDevice(device);
}
if(_console->IsPrimaryConsole() && _console->GetLinkedConsole()) {
shared_ptr<BaseControlDevice> linkedDevice = CreateControllerDevice(ControllerType::GameboyController, 1);
if(linkedDevice) {
RegisterControlDevice(linkedDevice);
linkedDevice->Disconnect();
}
}
}
uint8_t GbControlManager::ReadInputPort()
{
//Bit 7 - Not used
//Bit 6 - Not used
//Bit 5 - P15 Select Button Keys (0=Select)
//Bit 4 - P14 Select Direction Keys (0=Select)
//Bit 3 - P13 Input Down or Start (0=Pressed) (Read Only)
//Bit 2 - P12 Input Up or Select (0=Pressed) (Read Only)
//Bit 1 - P11 Input Left or Button B (0=Pressed) (Read Only)
//Bit 0 - P10 Input Right or Button A (0=Pressed) (Read Only)
uint8_t result = 0x0F;
uint8_t inputSelect = _state.InputSelect;
SuperGameboy* sgb = _console->GetSgb();
if(sgb) {
if((inputSelect & 0x30) == 0x30) {
result = sgb->GetInputIndex();
} else {
if(!(inputSelect & 0x20)) {
result &= sgb->GetInput() >> 4;
}
if(!(inputSelect & 0x10)) {
result &= sgb->GetInput() & 0x0F;
}
}
} else {
for(shared_ptr<BaseControlDevice>& controller : _controlDevices) {
if(controller->GetPort() == 0 && controller->GetControllerType() == ControllerType::GameboyController) {
if(!(inputSelect & 0x20)) {
result &= ~(controller->IsPressed(GbController::A) ? 0x01 : 0);
result &= ~(controller->IsPressed(GbController::B) ? 0x02 : 0);
result &= ~(controller->IsPressed(GbController::Select) ? 0x04 : 0);
result &= ~(controller->IsPressed(GbController::Start) ? 0x08 : 0);
}
if(!(inputSelect & 0x10)) {
result &= ~(controller->IsPressed(GbController::Right) ? 0x01 : 0);
result &= ~(controller->IsPressed(GbController::Left) ? 0x02 : 0);
result &= ~(controller->IsPressed(GbController::Up) ? 0x04 : 0);
result &= ~(controller->IsPressed(GbController::Down) ? 0x08 : 0);
}
}
}
}
return result | (inputSelect & 0x30) | 0xC0;
}
void GbControlManager::WriteInputPort(uint8_t value)
{
//Changing the select bits can trigger the joypad IRQ (Fixes Double Dragon 3 input issues)
ProcessInputChange([&]() { _state.InputSelect = value & 0x30; });
SuperGameboy* sgb = _console->GetSgb();
if(sgb) {
sgb->ProcessInputPortWrite(_state.InputSelect);
}
}
void GbControlManager::ProcessInputChange(std::function<void()> inputUpdateCallback)
{
uint8_t prevInput = ReadInputPort();
inputUpdateCallback();
uint8_t newInput = ReadInputPort();
if(prevInput != newInput) {
_console->GetMemoryManager()->RequestIrq(GbIrqSource::Joypad);
}
}
void GbControlManager::UpdateInputState()
{
ProcessInputChange([this]() { BaseControlManager::UpdateInputState(); });
}
bool GbControlManager::SetInput(BaseControlDevice* device)
{
//Copy port P2 (port 1) from the main console to the subconsole.
//This allows input to be recorded properly for rewind, movies, etc.
uint8_t port = device->GetPort();
GbControlManager* mainControlManager = (GbControlManager*)_console->GetLinkedConsole()->GetControlManager();
if(mainControlManager && port == 0) {
shared_ptr<BaseControlDevice> controlDevice = mainControlManager->GetControlDevice(1);
if(controlDevice) {
ControlDeviceState state = controlDevice->GetRawState();
device->SetRawState(state);
}
}
return true;
}
void GbControlManager::Serialize(Serializer& s)
{
BaseControlManager::Serialize(s);
SV(_state.InputSelect);
for(uint8_t i = 0; i < _controlDevices.size(); i++) {
SVI(_controlDevices[i]);
}
}