Controller: Add emulation of Namco GunCon

This commit is contained in:
Connor McLaughlin 2020-04-26 01:18:42 +10:00
parent 2026ef99e8
commit e6bd6587fd
11 changed files with 320 additions and 7 deletions

View File

@ -52,6 +52,8 @@ add_library(core
mdec.h mdec.h
memory_card.cpp memory_card.cpp
memory_card.h memory_card.h
namco_guncon.cpp
namco_guncon.h
pad.cpp pad.cpp
pad.h pad.h
psf_loader.cpp psf_loader.cpp

View File

@ -1,6 +1,7 @@
#include "controller.h" #include "controller.h"
#include "analog_controller.h" #include "analog_controller.h"
#include "common/state_wrapper.h" #include "common/state_wrapper.h"
#include "namco_guncon.h"
#include "digital_controller.h" #include "digital_controller.h"
Controller::Controller() = default; Controller::Controller() = default;
@ -36,7 +37,7 @@ float Controller::GetVibrationMotorStrength(u32 motor)
return 0.0f; return 0.0f;
} }
std::unique_ptr<Controller> Controller::Create(ControllerType type) std::unique_ptr<Controller> Controller::Create(System* system, ControllerType type)
{ {
switch (type) switch (type)
{ {
@ -46,6 +47,9 @@ std::unique_ptr<Controller> Controller::Create(ControllerType type)
case ControllerType::AnalogController: case ControllerType::AnalogController:
return AnalogController::Create(); return AnalogController::Create();
case ControllerType::NamcoGunCon:
return NamcoGunCon::Create(system);
case ControllerType::None: case ControllerType::None:
default: default:
return {}; return {};
@ -72,6 +76,9 @@ Controller::AxisList Controller::GetAxisNames(ControllerType type)
case ControllerType::AnalogController: case ControllerType::AnalogController:
return AnalogController::StaticGetAxisNames(); return AnalogController::StaticGetAxisNames();
case ControllerType::NamcoGunCon:
return NamcoGunCon::StaticGetAxisNames();
case ControllerType::None: case ControllerType::None:
default: default:
return {}; return {};
@ -88,6 +95,9 @@ Controller::ButtonList Controller::GetButtonNames(ControllerType type)
case ControllerType::AnalogController: case ControllerType::AnalogController:
return AnalogController::StaticGetButtonNames(); return AnalogController::StaticGetButtonNames();
case ControllerType::NamcoGunCon:
return NamcoGunCon::StaticGetButtonNames();
case ControllerType::None: case ControllerType::None:
default: default:
return {}; return {};
@ -104,6 +114,9 @@ u32 Controller::GetVibrationMotorCount(ControllerType type)
case ControllerType::AnalogController: case ControllerType::AnalogController:
return AnalogController::StaticGetVibrationMotorCount(); return AnalogController::StaticGetVibrationMotorCount();
case ControllerType::NamcoGunCon:
return NamcoGunCon::StaticGetVibrationMotorCount();
case ControllerType::None: case ControllerType::None:
default: default:
return 0; return 0;
@ -120,6 +133,9 @@ std::optional<s32> Controller::GetAxisCodeByName(ControllerType type, std::strin
case ControllerType::AnalogController: case ControllerType::AnalogController:
return AnalogController::StaticGetAxisCodeByName(axis_name); return AnalogController::StaticGetAxisCodeByName(axis_name);
case ControllerType::NamcoGunCon:
return NamcoGunCon::StaticGetAxisCodeByName(axis_name);
case ControllerType::None: case ControllerType::None:
default: default:
return std::nullopt; return std::nullopt;
@ -136,6 +152,9 @@ std::optional<s32> Controller::GetButtonCodeByName(ControllerType type, std::str
case ControllerType::AnalogController: case ControllerType::AnalogController:
return AnalogController::StaticGetButtonCodeByName(button_name); return AnalogController::StaticGetButtonCodeByName(button_name);
case ControllerType::NamcoGunCon:
return NamcoGunCon::StaticGetButtonCodeByName(button_name);
case ControllerType::None: case ControllerType::None:
default: default:
return std::nullopt; return std::nullopt;

View File

@ -7,6 +7,7 @@
#include <vector> #include <vector>
class StateWrapper; class StateWrapper;
class System;
class Controller class Controller
{ {
@ -48,7 +49,7 @@ public:
virtual float GetVibrationMotorStrength(u32 motor); virtual float GetVibrationMotorStrength(u32 motor);
/// Creates a new controller of the specified type. /// Creates a new controller of the specified type.
static std::unique_ptr<Controller> Create(ControllerType type); static std::unique_ptr<Controller> Create(System* system, ControllerType type);
/// Gets the integer code for an axis in the specified controller type. /// Gets the integer code for an axis in the specified controller type.
static std::optional<s32> GetAxisCodeByName(ControllerType type, std::string_view axis_name); static std::optional<s32> GetAxisCodeByName(ControllerType type, std::string_view axis_name);

View File

@ -75,6 +75,7 @@
<ClCompile Include="interrupt_controller.cpp" /> <ClCompile Include="interrupt_controller.cpp" />
<ClCompile Include="mdec.cpp" /> <ClCompile Include="mdec.cpp" />
<ClCompile Include="memory_card.cpp" /> <ClCompile Include="memory_card.cpp" />
<ClCompile Include="namco_guncon.cpp" />
<ClCompile Include="pad.cpp" /> <ClCompile Include="pad.cpp" />
<ClCompile Include="controller.cpp" /> <ClCompile Include="controller.cpp" />
<ClCompile Include="psf_loader.cpp" /> <ClCompile Include="psf_loader.cpp" />
@ -115,6 +116,7 @@
<ClInclude Include="interrupt_controller.h" /> <ClInclude Include="interrupt_controller.h" />
<ClInclude Include="mdec.h" /> <ClInclude Include="mdec.h" />
<ClInclude Include="memory_card.h" /> <ClInclude Include="memory_card.h" />
<ClInclude Include="namco_guncon.h" />
<ClInclude Include="pad.h" /> <ClInclude Include="pad.h" />
<ClInclude Include="controller.h" /> <ClInclude Include="controller.h" />
<ClInclude Include="psf_loader.h" /> <ClInclude Include="psf_loader.h" />

View File

@ -41,6 +41,7 @@
<ClCompile Include="timing_event.cpp" /> <ClCompile Include="timing_event.cpp" />
<ClCompile Include="cdrom_async_reader.cpp" /> <ClCompile Include="cdrom_async_reader.cpp" />
<ClCompile Include="psf_loader.cpp" /> <ClCompile Include="psf_loader.cpp" />
<ClCompile Include="namco_guncon.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="types.h" /> <ClInclude Include="types.h" />
@ -83,6 +84,7 @@
<ClInclude Include="timing_event.h" /> <ClInclude Include="timing_event.h" />
<ClInclude Include="cdrom_async_reader.h" /> <ClInclude Include="cdrom_async_reader.h" />
<ClInclude Include="psf_loader.h" /> <ClInclude Include="psf_loader.h" />
<ClInclude Include="namco_guncon.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="cpu_core.inl" /> <None Include="cpu_core.inl" />

219
src/core/namco_guncon.cpp Normal file
View File

@ -0,0 +1,219 @@
#include "namco_guncon.h"
#include "common/assert.h"
#include "common/log.h"
#include "common/state_wrapper.h"
#include "gpu.h"
#include "imgui.h"
#include "system.h"
#include <array>
Log_SetChannel(NamcoGunCon);
NamcoGunCon::NamcoGunCon(System* system) : m_system(system) {}
NamcoGunCon::~NamcoGunCon() = default;
ControllerType NamcoGunCon::GetType() const
{
return ControllerType::NamcoGunCon;
}
std::optional<s32> NamcoGunCon::GetAxisCodeByName(std::string_view axis_name) const
{
return StaticGetAxisCodeByName(axis_name);
}
std::optional<s32> NamcoGunCon::GetButtonCodeByName(std::string_view button_name) const
{
return StaticGetButtonCodeByName(button_name);
}
void NamcoGunCon::Reset()
{
m_transfer_state = TransferState::Idle;
}
bool NamcoGunCon::DoState(StateWrapper& sw)
{
if (!Controller::DoState(sw))
return false;
sw.Do(&m_button_state);
sw.Do(&m_position_x);
sw.Do(&m_position_y);
sw.Do(&m_transfer_state);
return true;
}
void NamcoGunCon::SetAxisState(s32 axis_code, float value) {}
void NamcoGunCon::SetButtonState(Button button, bool pressed)
{
static constexpr std::array<u8, static_cast<size_t>(Button::Count)> indices = {{13, 3, 14}};
if (pressed)
m_button_state &= ~(u16(1) << indices[static_cast<u8>(button)]);
else
m_button_state |= u16(1) << indices[static_cast<u8>(button)];
}
void NamcoGunCon::SetButtonState(s32 button_code, bool pressed)
{
if (button_code < 0 || button_code >= static_cast<s32>(Button::Count))
return;
SetButtonState(static_cast<Button>(button_code), pressed);
}
void NamcoGunCon::ResetTransferState()
{
m_transfer_state = TransferState::Idle;
}
bool NamcoGunCon::Transfer(const u8 data_in, u8* data_out)
{
static constexpr u16 ID = 0x5A63;
switch (m_transfer_state)
{
case TransferState::Idle:
{
// ack when sent 0x01, send ID for 0x42
if (data_in == 0x42)
{
*data_out = Truncate8(ID);
m_transfer_state = TransferState::IDMSB;
return true;
}
else
{
*data_out = 0xFF;
return (data_in == 0x01);
}
}
case TransferState::IDMSB:
{
*data_out = Truncate8(ID >> 8);
m_transfer_state = TransferState::ButtonsLSB;
return true;
}
case TransferState::ButtonsLSB:
{
*data_out = Truncate8(m_button_state);
m_transfer_state = TransferState::ButtonsMSB;
return true;
}
case TransferState::ButtonsMSB:
{
*data_out = Truncate8(m_button_state >> 8);
m_transfer_state = TransferState::XLSB;
return true;
}
case TransferState::XLSB:
{
UpdatePosition();
*data_out = Truncate8(m_position_x);
m_transfer_state = TransferState::XMSB;
return true;
}
case TransferState::XMSB:
{
*data_out = Truncate8(m_position_x >> 8);
m_transfer_state = TransferState::YLSB;
return true;
}
case TransferState::YLSB:
{
*data_out = Truncate8(m_position_y);
m_transfer_state = TransferState::YMSB;
return true;
}
case TransferState::YMSB:
{
*data_out = Truncate8(m_position_y >> 8);
m_transfer_state = TransferState::Idle;
return false;
}
default:
{
UnreachableCode();
return false;
}
}
}
void NamcoGunCon::UpdatePosition()
{
// get screen coordinates. TODO better way
const s32 mouse_x = static_cast<s32>(ImGui::GetIO().MousePos.x);
const s32 mouse_y = static_cast<s32>(ImGui::GetIO().MousePos.y);
// are we within the active display area?
u32 tick, line;
if (mouse_x < 0 || mouse_y < 0 ||
!m_system->GetGPU()->ConvertScreenCoordinatesToBeamTicksAndLines(mouse_x, mouse_y, &tick, &line))
{
Log_DebugPrintf("Lightgun out of range for window coordinates %d,%d", mouse_x, mouse_y);
m_position_x = 0x01;
m_position_y = 0x0A;
return;
}
// 8MHz units for X = 44100*768*11/7 = 53222400 / 8000000 = 6.6528
m_position_x = static_cast<u16>(static_cast<float>(tick) * (1.0f / 6.6528f));
m_position_y = static_cast<u16>(line);
Log_DebugPrintf("Lightgun window coordinates %d,%d -> tick %u line %u 8mhz ticks %u", tick, line, m_position_x);
}
std::unique_ptr<NamcoGunCon> NamcoGunCon::Create(System* system)
{
return std::make_unique<NamcoGunCon>(system);
}
std::optional<s32> NamcoGunCon::StaticGetAxisCodeByName(std::string_view button_name)
{
return std::nullopt;
}
std::optional<s32> NamcoGunCon::StaticGetButtonCodeByName(std::string_view button_name)
{
#define BUTTON(name) \
if (button_name == #name) \
{ \
return static_cast<s32>(ZeroExtend32(static_cast<u8>(Button::name))); \
}
BUTTON(Trigger);
BUTTON(A);
BUTTON(B);
return std::nullopt;
#undef BUTTON
}
Controller::AxisList NamcoGunCon::StaticGetAxisNames()
{
return {};
}
Controller::ButtonList NamcoGunCon::StaticGetButtonNames()
{
#define B(n) \
{ \
#n, static_cast < s32>(Button::n) \
}
return {B(Trigger), B(A), B(B)};
#undef B
}
u32 NamcoGunCon::StaticGetVibrationMotorCount()
{
return 0;
}

66
src/core/namco_guncon.h Normal file
View File

@ -0,0 +1,66 @@
#pragma once
#include "controller.h"
#include <memory>
#include <optional>
#include <string_view>
class NamcoGunCon final : public Controller
{
public:
enum class Button : u8
{
Trigger = 0,
A = 1,
B = 2,
Count
};
NamcoGunCon(System* system);
~NamcoGunCon() override;
static std::unique_ptr<NamcoGunCon> Create(System* system);
static std::optional<s32> StaticGetAxisCodeByName(std::string_view button_name);
static std::optional<s32> StaticGetButtonCodeByName(std::string_view button_name);
static AxisList StaticGetAxisNames();
static ButtonList StaticGetButtonNames();
static u32 StaticGetVibrationMotorCount();
ControllerType GetType() const override;
std::optional<s32> GetAxisCodeByName(std::string_view axis_name) const override;
std::optional<s32> GetButtonCodeByName(std::string_view button_name) const override;
void Reset() override;
bool DoState(StateWrapper& sw) override;
void SetAxisState(s32 axis_code, float value) override;
void SetButtonState(s32 button_code, bool pressed) override;
void ResetTransferState() override;
bool Transfer(const u8 data_in, u8* data_out) override;
void SetButtonState(Button button, bool pressed);
private:
void UpdatePosition();
enum class TransferState : u8
{
Idle,
IDMSB,
ButtonsLSB,
ButtonsMSB,
XLSB,
XMSB,
YLSB,
YMSB
};
System* m_system;
// buttons are active low
u16 m_button_state = UINT16_C(0xFFFF);
u16 m_position_x = 0;
u16 m_position_y = 0;
TransferState m_transfer_state = TransferState::Idle;
};

View File

@ -50,7 +50,7 @@ bool Pad::DoState(StateWrapper& sw)
m_controllers[i].reset(); m_controllers[i].reset();
if (state_controller_type != ControllerType::None) if (state_controller_type != ControllerType::None)
m_controllers[i] = Controller::Create(state_controller_type); m_controllers[i] = Controller::Create(m_system, state_controller_type);
} }
if (m_controllers[i]) if (m_controllers[i])

View File

@ -349,9 +349,10 @@ const char* Settings::GetAudioBackendDisplayName(AudioBackend backend)
return s_audio_backend_display_names[static_cast<int>(backend)]; return s_audio_backend_display_names[static_cast<int>(backend)];
} }
static std::array<const char*, 3> s_controller_type_names = {{"None", "DigitalController", "AnalogController"}}; static std::array<const char*, 4> s_controller_type_names = {
static std::array<const char*, 3> s_controller_display_names = { {"None", "DigitalController", "AnalogController", "NamcoGunCon"}};
{"None", "Digital Controller", "Analog Controller (DualShock)"}}; static std::array<const char*, 4> s_controller_display_names = {
{"None", "Digital Controller", "Analog Controller (DualShock)", "Namco GunCon"}};
std::optional<ControllerType> Settings::ParseControllerTypeName(const char* str) std::optional<ControllerType> Settings::ParseControllerTypeName(const char* str)
{ {

View File

@ -785,7 +785,7 @@ void System::UpdateControllers()
const ControllerType type = settings.controller_types[i]; const ControllerType type = settings.controller_types[i];
if (type != ControllerType::None) if (type != ControllerType::None)
{ {
std::unique_ptr<Controller> controller = Controller::Create(type); std::unique_ptr<Controller> controller = Controller::Create(this, type);
if (controller) if (controller)
m_pad->SetController(i, std::move(controller)); m_pad->SetController(i, std::move(controller));
} }

View File

@ -87,6 +87,7 @@ enum class ControllerType
None, None,
DigitalController, DigitalController,
AnalogController, AnalogController,
NamcoGunCon,
Count Count
}; };