2019-09-20 06:47:41 +00:00
|
|
|
#pragma once
|
|
|
|
#include "common/bitfield.h"
|
|
|
|
#include "common/fifo_queue.h"
|
|
|
|
#include "types.h"
|
|
|
|
#include <array>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
class StateWrapper;
|
|
|
|
|
2020-01-24 04:53:40 +00:00
|
|
|
class TimingEvent;
|
2019-12-08 14:51:52 +00:00
|
|
|
class Controller;
|
2019-10-27 06:45:23 +00:00
|
|
|
class MemoryCard;
|
2019-09-20 06:47:41 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
class Pad final
|
2019-09-20 06:47:41 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Pad();
|
|
|
|
~Pad();
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
void Initialize();
|
|
|
|
void Shutdown();
|
2019-09-20 06:47:41 +00:00
|
|
|
void Reset();
|
|
|
|
bool DoState(StateWrapper& sw);
|
|
|
|
|
2019-12-14 13:20:24 +00:00
|
|
|
Controller* GetController(u32 slot) const { return m_controllers[slot].get(); }
|
|
|
|
void SetController(u32 slot, std::unique_ptr<Controller> dev);
|
2019-09-29 15:07:38 +00:00
|
|
|
|
2019-10-27 06:45:23 +00:00
|
|
|
MemoryCard* GetMemoryCard(u32 slot) { return m_memory_cards[slot].get(); }
|
2019-12-14 13:20:24 +00:00
|
|
|
void SetMemoryCard(u32 slot, std::unique_ptr<MemoryCard> dev);
|
2019-09-20 06:47:41 +00:00
|
|
|
|
|
|
|
u32 ReadRegister(u32 offset);
|
|
|
|
void WriteRegister(u32 offset, u32 value);
|
|
|
|
|
2021-01-05 14:00:56 +00:00
|
|
|
ALWAYS_INLINE bool IsTransmitting() const { return m_state != State::Idle; }
|
|
|
|
|
2019-09-20 06:47:41 +00:00
|
|
|
private:
|
|
|
|
static constexpr u32 NUM_SLOTS = 2;
|
2019-09-22 15:25:58 +00:00
|
|
|
|
|
|
|
enum class State : u32
|
|
|
|
{
|
|
|
|
Idle,
|
2019-12-05 13:03:54 +00:00
|
|
|
Transmitting,
|
|
|
|
WaitingForACK
|
2019-09-22 15:25:58 +00:00
|
|
|
};
|
2019-09-20 06:47:41 +00:00
|
|
|
|
2019-09-29 15:07:38 +00:00
|
|
|
enum class ActiveDevice : u8
|
|
|
|
{
|
|
|
|
None,
|
|
|
|
Controller,
|
|
|
|
MemoryCard
|
|
|
|
};
|
|
|
|
|
2019-09-20 06:47:41 +00:00
|
|
|
union JOY_CTRL
|
|
|
|
{
|
2019-09-20 09:21:45 +00:00
|
|
|
u16 bits;
|
2019-09-20 06:47:41 +00:00
|
|
|
|
|
|
|
BitField<u16, bool, 0, 1> TXEN;
|
|
|
|
BitField<u16, bool, 1, 1> SELECT;
|
|
|
|
BitField<u16, bool, 2, 1> RXEN;
|
|
|
|
BitField<u16, bool, 4, 1> ACK;
|
|
|
|
BitField<u16, bool, 6, 1> RESET;
|
|
|
|
BitField<u16, u8, 8, 2> RXIMODE;
|
|
|
|
BitField<u16, bool, 10, 1> TXINTEN;
|
|
|
|
BitField<u16, bool, 11, 1> RXINTEN;
|
|
|
|
BitField<u16, bool, 12, 1> ACKINTEN;
|
|
|
|
BitField<u16, u8, 13, 1> SLOT;
|
|
|
|
};
|
|
|
|
|
|
|
|
union JOY_STAT
|
|
|
|
{
|
|
|
|
u32 bits;
|
|
|
|
|
|
|
|
BitField<u32, bool, 0, 1> TXRDY;
|
|
|
|
BitField<u32, bool, 1, 1> RXFIFONEMPTY;
|
|
|
|
BitField<u32, bool, 2, 1> TXDONE;
|
|
|
|
BitField<u32, bool, 7, 1> ACKINPUT;
|
|
|
|
BitField<u32, bool, 9, 1> INTR;
|
|
|
|
BitField<u32, u32, 11, 21> TMR;
|
|
|
|
};
|
|
|
|
|
|
|
|
union JOY_MODE
|
|
|
|
{
|
|
|
|
u16 bits;
|
|
|
|
|
|
|
|
BitField<u16, u8, 0, 2> reload_factor;
|
|
|
|
BitField<u16, u8, 2, 2> character_length;
|
|
|
|
BitField<u16, bool, 4, 1> parity_enable;
|
|
|
|
BitField<u16, u8, 5, 1> parity_type;
|
|
|
|
BitField<u16, u8, 8, 1> clk_polarity;
|
|
|
|
};
|
|
|
|
|
2021-01-05 14:00:56 +00:00
|
|
|
ALWAYS_INLINE bool CanTransfer() const { return m_transmit_buffer_full && m_JOY_CTRL.SELECT && m_JOY_CTRL.TXEN; }
|
2019-09-22 15:25:58 +00:00
|
|
|
|
2021-01-05 14:00:56 +00:00
|
|
|
ALWAYS_INLINE TickCount GetTransferTicks() const { return static_cast<TickCount>(ZeroExtend32(m_JOY_BAUD) * 8); }
|
2020-07-01 14:43:26 +00:00
|
|
|
|
|
|
|
// From @JaCzekanski
|
|
|
|
// ACK lasts ~96 ticks or approximately 2.84us at master clock (not implemented).
|
|
|
|
// ACK delay is between 6.8us-13.7us, or ~338 ticks at master clock for approximately 9.98us.
|
|
|
|
// Memory card responds faster, approximately 5us or ~170 ticks.
|
|
|
|
static constexpr TickCount GetACKTicks(bool memory_card) { return memory_card ? 170 : 450; }
|
2019-10-26 15:52:18 +00:00
|
|
|
|
2019-09-20 06:47:41 +00:00
|
|
|
void SoftReset();
|
|
|
|
void UpdateJoyStat();
|
2020-01-24 04:53:40 +00:00
|
|
|
void TransferEvent(TickCount ticks_late);
|
2019-09-22 15:25:58 +00:00
|
|
|
void BeginTransfer();
|
2020-01-24 04:53:40 +00:00
|
|
|
void DoTransfer(TickCount ticks_late);
|
2019-12-05 13:03:54 +00:00
|
|
|
void DoACK();
|
2019-09-22 15:25:58 +00:00
|
|
|
void EndTransfer();
|
2019-09-29 15:07:38 +00:00
|
|
|
void ResetDeviceTransferState();
|
2019-09-20 06:47:41 +00:00
|
|
|
|
2019-12-14 13:20:24 +00:00
|
|
|
std::array<std::unique_ptr<Controller>, NUM_SLOTS> m_controllers;
|
|
|
|
std::array<std::unique_ptr<MemoryCard>, NUM_SLOTS> m_memory_cards;
|
2019-11-11 15:13:08 +00:00
|
|
|
|
2020-01-24 04:53:40 +00:00
|
|
|
std::unique_ptr<TimingEvent> m_transfer_event;
|
2019-09-22 15:25:58 +00:00
|
|
|
State m_state = State::Idle;
|
|
|
|
|
2019-09-20 06:47:41 +00:00
|
|
|
JOY_CTRL m_JOY_CTRL = {};
|
|
|
|
JOY_STAT m_JOY_STAT = {};
|
|
|
|
JOY_MODE m_JOY_MODE = {};
|
2019-10-18 13:10:41 +00:00
|
|
|
u16 m_JOY_BAUD = 0;
|
2019-09-20 06:47:41 +00:00
|
|
|
|
2019-09-29 15:07:38 +00:00
|
|
|
ActiveDevice m_active_device = ActiveDevice::None;
|
2019-11-11 15:13:08 +00:00
|
|
|
u8 m_receive_buffer = 0;
|
|
|
|
u8 m_transmit_buffer = 0;
|
|
|
|
u8 m_transmit_value = 0;
|
|
|
|
bool m_receive_buffer_full = false;
|
|
|
|
bool m_transmit_buffer_full = false;
|
2019-09-20 06:47:41 +00:00
|
|
|
};
|
2020-07-31 07:09:18 +00:00
|
|
|
|
|
|
|
extern Pad g_pad;
|