From e1b838355e94702fa0724de06f903864dfb0488e Mon Sep 17 00:00:00 2001 From: "Adrian.Conlon" Date: Fri, 15 Sep 2017 00:53:56 +0100 Subject: [PATCH] Add P1 (i.e. keyboard) support. Interestingly, gets Tetris *much* further. Signed-off-by: Adrian.Conlon --- LR35902/inc/GameBoyBus.h | 33 +++++++++++++++++++++++++++++++++ LR35902/src/GameBoyBus.cpp | 25 ++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/LR35902/inc/GameBoyBus.h b/LR35902/inc/GameBoyBus.h index 530598c..c1ecb83 100644 --- a/LR35902/inc/GameBoyBus.h +++ b/LR35902/inc/GameBoyBus.h @@ -217,6 +217,30 @@ namespace EightBit { void loadBootRom(const std::string& path); void loadGameRom(const std::string& path); + void pressUp() { m_upPressed = true; } + void releaseUp() { m_upPressed = false; } + + void pressDown() { m_downPressed = true; } + void releaseDown() { m_downPressed = false; } + + void pressLeft() { m_leftPressed = true; } + void releaseLeft() { m_leftPressed = false; } + + void pressRight() { m_rightPressed = true; } + void releaseRight() { m_rightPressed = false; } + + void pressA() { m_aPressed = true; } + void releaseA() { m_aPressed = false; } + + void pressB() { m_bPressed = true; } + void releaseB() { m_bPressed = false; } + + void pressSelect() { m_selectPressed = true; } + void releaseSelect() { m_selectPressed = false; } + + void pressStart() { m_startPressed = true; } + void releaseStart() { m_startPressed = false; } + protected: virtual uint8_t& reference(uint16_t address, bool& rom) { @@ -280,6 +304,15 @@ namespace EightBit { std::array m_soundChannelEnabled; bool m_soundEnabled; + bool m_upPressed; + bool m_downPressed; + bool m_leftPressed; + bool m_rightPressed; + bool m_aPressed; + bool m_bPressed; + bool m_selectPressed; + bool m_startPressed; + void checkTimer(int cycles); void validateCartridgeType(); diff --git a/LR35902/src/GameBoyBus.cpp b/LR35902/src/GameBoyBus.cpp index c3b6453..5a16e26 100644 --- a/LR35902/src/GameBoyBus.cpp +++ b/LR35902/src/GameBoyBus.cpp @@ -22,7 +22,16 @@ EightBit::GameBoy::Bus::Bus() m_ramBank(0), m_timerCounter(0), m_timerRate(0), - m_dmaTransferActive(false) { + m_dmaTransferActive(false), + m_upPressed(false), + m_downPressed(false), + m_leftPressed(false), + m_rightPressed(false), + m_aPressed(false), + m_bPressed(false), + m_selectPressed(false), + m_startPressed(false) +{ ReadingByte.connect(std::bind(&GameBoy::Bus::Bus_ReadingByte, this, std::placeholders::_1)); ReadByte.connect(std::bind(&GameBoy::Bus::Bus_ReadByte, this, std::placeholders::_1)); WritingByte.connect(std::bind(&GameBoy::Bus::Bus_WritingByte, this, std::placeholders::_1)); @@ -67,8 +76,18 @@ void EightBit::GameBoy::Bus::Bus_ReadingByte(const uint16_t address) { switch (ioRegister) { // Port/Mode Registers - case P1: - mask(Processor::Mask5); + case P1: { + auto direction = m_rightPressed || m_leftPressed || m_upPressed || m_downPressed; + auto button = m_aPressed || m_bPressed || m_selectPressed || m_startPressed; + pokeRegister(P1, + (int)!(m_rightPressed || m_aPressed) + | ((int)!(m_leftPressed || m_bPressed) << 1) + | ((int)!(m_upPressed || m_selectPressed) << 2) + | ((int)!(m_downPressed || m_startPressed) << 3) + | (direction << 4) + | (button << 5) + | Processor::Bit6 | Processor::Bit7); + } break; case SB: break;