cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

Input.cpp (2495B)


      1/*
      2 * Gearboy - Nintendo Game Boy Emulator
      3 * Copyright (C) 2012  Ignacio Sanchez
      4
      5 * This program is free software: you can redistribute it and/or modify
      6 * it under the terms of the GNU General Public License as published by
      7 * the Free Software Foundation, either version 3 of the License, or
      8 * any later version.
      9
     10 * This program is distributed in the hope that it will be useful,
     11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     13 * GNU General Public License for more details.
     14
     15 * You should have received a copy of the GNU General Public License
     16 * along with this program.  If not, see http://www.gnu.org/licenses/
     17 *
     18 */
     19
     20#include "Input.h"
     21#include "Memory.h"
     22#include "Processor.h"
     23
     24Input::Input(Memory* pMemory, Processor* pProcessor)
     25{
     26    m_pMemory = pMemory;
     27    m_pProcessor = pProcessor;
     28    m_JoypadState = 0xFF;
     29    m_P1 = 0xFF;
     30    m_iInputCycles = 0;
     31}
     32
     33void Input::Init()
     34{
     35    Reset();
     36}
     37
     38void Input::Reset()
     39{
     40    m_JoypadState = 0xFF;
     41    m_P1 = 0xFF;
     42    m_iInputCycles = 0;
     43}
     44
     45void Input::KeyPressed(Gameboy_Keys key)
     46{
     47    m_JoypadState = UnsetBit(m_JoypadState, key);
     48}
     49
     50void Input::KeyReleased(Gameboy_Keys key)
     51{
     52    m_JoypadState = SetBit(m_JoypadState, key);
     53}
     54
     55void Input::Update()
     56{
     57    u8 current = m_P1 & 0xF0;
     58
     59    switch (current & 0x30)
     60    {
     61        case 0x10:
     62        {
     63            u8 topJoypad = (m_JoypadState >> 4) & 0x0F;
     64            current |= topJoypad;
     65            break;
     66        }
     67        case 0x20:
     68        {
     69            u8 bottomJoypad = m_JoypadState & 0x0F;
     70            current |= bottomJoypad;
     71            break;
     72        }
     73        case 0x30:
     74            current |= 0x0F;
     75            break;
     76    }
     77
     78    if ((m_P1 & ~current & 0x0F) != 0)
     79        m_pProcessor->RequestInterrupt(Processor::Joypad_Interrupt);
     80
     81    m_P1 = current;
     82}
     83
     84void Input::SaveState(std::ostream& stream)
     85{
     86    using namespace std;
     87
     88    stream.write(reinterpret_cast<const char*> (&m_JoypadState), sizeof(m_JoypadState));
     89    stream.write(reinterpret_cast<const char*> (&m_P1), sizeof(m_P1));
     90    stream.write(reinterpret_cast<const char*> (&m_iInputCycles), sizeof(m_iInputCycles));
     91}
     92
     93void Input::LoadState(std::istream& stream)
     94{
     95    using namespace std;
     96
     97    stream.read(reinterpret_cast<char*> (&m_JoypadState), sizeof(m_JoypadState));
     98    stream.read(reinterpret_cast<char*> (&m_P1), sizeof(m_P1));
     99    stream.read(reinterpret_cast<char*> (&m_iInputCycles), sizeof(m_iInputCycles));
    100}