cscg22-gearboy

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

Input.h (1694B)


      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#ifndef INPUT_H
     21#define	INPUT_H
     22
     23#include "definitions.h"
     24
     25class Memory;
     26class Processor;
     27
     28class Input
     29{
     30public:
     31    Input(Memory* pMemory, Processor* pProcessor);
     32    void Init();
     33    void Reset();
     34    void Tick(unsigned int clockCycles);
     35    void KeyPressed(Gameboy_Keys key);
     36    void KeyReleased(Gameboy_Keys key);
     37    void Write(u8 value);
     38    u8 Read();
     39    void SaveState(std::ostream& stream);
     40    void LoadState(std::istream& stream);
     41
     42private:
     43    void Update();
     44
     45private:
     46    Memory* m_pMemory;
     47    Processor* m_pProcessor;
     48    u8 m_JoypadState;
     49    u8 m_P1;
     50    int m_iInputCycles;
     51};
     52
     53inline void Input::Tick(unsigned int clockCycles)
     54{
     55    m_iInputCycles += clockCycles;
     56
     57    // Joypad Poll Speed (64 Hz)
     58    if (m_iInputCycles >= 65536)
     59    {
     60        m_iInputCycles -= 65536;
     61        Update();
     62    }
     63}
     64
     65inline void Input::Write(u8 value)
     66{
     67    m_P1 = (m_P1 & 0xCF) | (value & 0x30);
     68    Update();
     69}
     70
     71inline u8 Input::Read()
     72{
     73    return m_P1;
     74}
     75
     76#endif	/* INPUT_H */