cscg22-gearboy

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

Memory.h (7734B)


      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 MEMORY_H
     21#define	MEMORY_H
     22
     23#include "definitions.h"
     24#include "MemoryRule.h"
     25#include <vector>
     26
     27class Processor;
     28class Video;
     29class CommonMemoryRule;
     30class IORegistersMemoryRule;
     31
     32class Memory
     33{
     34public:
     35    struct stDisassembleRecord
     36    {
     37        u16 address;
     38        char name[32];
     39        char bytes[16];
     40        int size;
     41        int bank;
     42        u8 opcodes[4];
     43        bool jump;
     44        u16 jump_address;
     45    };
     46
     47    struct stMemoryBreakpoint
     48    {
     49        u16 address1;
     50        u16 address2;
     51        bool read;
     52        bool write;
     53        bool range;
     54    };
     55
     56public:
     57    Memory();
     58    ~Memory();
     59    void SetProcessor(Processor* pProcessor);
     60    void SetVideo(Video* pVideo);
     61    void Init();
     62    void Reset(bool bCGB);
     63    void SetCurrentRule(MemoryRule* pRule);
     64    void SetCommonRule(CommonMemoryRule* pRule);
     65    void SetIORule(IORegistersMemoryRule* pRule);
     66    MemoryRule* GetCurrentRule();
     67    u8* GetMemoryMap();
     68    u8 Read(u16 address);
     69    void Write(u16 address, u8 value);
     70    u8 ReadCGBWRAM(u16 address);
     71    void WriteCGBWRAM(u16 address, u8 value);
     72    void SwitchCGBWRAM(u8 value);
     73    u8 ReadCGBLCDRAM(u16 address, bool forceBank1);
     74    void WriteCGBLCDRAM(u16 address, u8 value);
     75    void SwitchCGBLCDRAM(u8 value);
     76    u8 Retrieve(u16 address);
     77    void Load(u16 address, u8 value);
     78    stDisassembleRecord** GetDisassembledMemoryMap();
     79    stDisassembleRecord** GetDisassembledROMMemoryMap();
     80    void LoadBank0and1FromROM(u8* pTheROM);
     81    void MemoryDump(const char* szFilePath);
     82    void PerformDMA(u8 value);
     83    void SwitchCGBDMA(u8 value);
     84    unsigned int PerformHDMA();
     85    void PerformGDMA(u8 value);
     86    bool IsHDMAEnabled() const;
     87    void SetHDMARegister(int reg, u8 value);
     88    u8 GetHDMARegister(int reg) const;
     89    u8* GetCGBRAM();
     90    int GetCurrentCGBRAMBank();
     91    int GetCurrentLCDRAMBank();
     92    void SaveState(std::ostream& stream);
     93    void LoadState(std::istream& stream);
     94    u8* GetROM0();
     95    u8* GetROM1();
     96    u8* GetVRAM();
     97    u8* GetRAM();
     98    u8* GetWRAM0();
     99    u8* GetWRAM1();
    100    std::vector<stDisassembleRecord*>* GetBreakpointsCPU();
    101    std::vector<stMemoryBreakpoint>* GetBreakpointsMem();
    102    stDisassembleRecord* GetRunToBreakpoint();
    103    void SetRunToBreakpoint(stDisassembleRecord* pBreakpoint);
    104    void EnableBootromDMG(bool enable);
    105    void EnableBootromGBC(bool enable);
    106    void LoadBootromDMG(const char* szFilePath);
    107    void LoadBootromGBC(const char* szFilePath);
    108    bool IsBootromEnabled();
    109    void DisableBootromRegistry();
    110    bool IsBootromRegistryEnabled();
    111    void ResetDisassembledMemory();
    112    void ResetBootromDisassembledMemory();
    113
    114private:
    115    void LoadBootroom(const char* szFilePath, bool gbc);
    116    void CheckBreakpoints(u16 address, bool write);
    117
    118private:
    119    Processor* m_pProcessor;
    120    Video* m_pVideo;
    121    CommonMemoryRule* m_pCommonMemoryRule;
    122    IORegistersMemoryRule* m_pIORegistersMemoryRule;
    123    MemoryRule* m_pCurrentMemoryRule;
    124    u8* m_pMap;
    125    stDisassembleRecord** m_pDisassembledMap;
    126    stDisassembleRecord** m_pDisassembledROMMap;
    127    std::vector<stDisassembleRecord*> m_BreakpointsCPU;
    128    std::vector<stMemoryBreakpoint> m_BreakpointsMem;
    129    stDisassembleRecord* m_pRunToBreakpoint;
    130    bool m_bCGB;
    131    int m_iCurrentWRAMBank;
    132    int m_iCurrentLCDRAMBank;
    133    u8* m_pWRAMBanks;
    134    u8* m_pLCDRAMBank1;
    135    bool m_bHDMAEnabled;
    136    int m_iHDMABytes;
    137    u8 m_HDMA[5];
    138    u16 m_HDMASource;
    139    u16 m_HDMADestination;
    140    bool m_bBootromDMGEnabled;
    141    bool m_bBootromGBCEnabled;
    142    bool m_bBootromDMGLoaded;
    143    bool m_bBootromGBCLoaded;
    144    u8* m_pBootromDMG;
    145    u8* m_pBootromGBC;
    146    bool m_bBootromRegistryDisabled;
    147};
    148
    149#include "Memory_inline.h"
    150
    151// From Gambatte emulator
    152const u8 kInitialValuesForFFXX[256] = {
    153    0xCF, 0x00, 0x7E, 0xFF, 0xD3, 0x00, 0x00, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE1,
    154    0x80, 0xBF, 0xF3, 0xFF, 0xBF, 0xFF, 0x3F, 0x00, 0xFF, 0xBF, 0x7F, 0xFF, 0x9F, 0xFF, 0xBF, 0xFF,
    155    0xFF, 0x00, 0x00, 0xBF, 0x77, 0xF3, 0xF1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    156    0x71, 0x72, 0xD5, 0x91, 0x58, 0xBB, 0x2A, 0xFA, 0xCF, 0x3C, 0x54, 0x75, 0x48, 0xCF, 0x8F, 0xD9,
    157    0x91, 0x80, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
    158    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    159    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    160    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    161    0x2B, 0x0B, 0x64, 0x2F, 0xAF, 0x15, 0x60, 0x6D, 0x61, 0x4E, 0xAC, 0x45, 0x0F, 0xDA, 0x92, 0xF3,
    162    0x83, 0x38, 0xE4, 0x4E, 0xA7, 0x6C, 0x38, 0x58, 0xBE, 0xEA, 0xE5, 0x81, 0xB4, 0xCB, 0xBF, 0x7B,
    163    0x59, 0xAD, 0x50, 0x13, 0x5E, 0xF6, 0xB3, 0xC1, 0xDC, 0xDF, 0x9E, 0x68, 0xD7, 0x59, 0x26, 0xF3,
    164    0x62, 0x54, 0xF8, 0x36, 0xB7, 0x78, 0x6A, 0x22, 0xA7, 0xDD, 0x88, 0x15, 0xCA, 0x96, 0x39, 0xD3,
    165    0xE6, 0x55, 0x6E, 0xEA, 0x90, 0x76, 0xB8, 0xFF, 0x50, 0xCD, 0xB5, 0x1B, 0x1F, 0xA5, 0x4D, 0x2E,
    166    0xB4, 0x09, 0x47, 0x8A, 0xC4, 0x5A, 0x8C, 0x4E, 0xE7, 0x29, 0x50, 0x88, 0xA8, 0x66, 0x85, 0x4B,
    167    0xAA, 0x38, 0xE7, 0x6B, 0x45, 0x3E, 0x30, 0x37, 0xBA, 0xC5, 0x31, 0xF2, 0x71, 0xB4, 0xCF, 0x29,
    168    0xBC, 0x7F, 0x7E, 0xD0, 0xC7, 0xC3, 0xBD, 0xCF, 0x59, 0xEA, 0x39, 0x01, 0x2E, 0x00, 0x69, 0x00
    169};
    170
    171const u8 kInitialValuesForColorFFXX[256] = {
    172    0xCF, 0x00, 0x7C, 0xFF, 0x44, 0x00, 0x00, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE1,
    173    0x80, 0xBF, 0xF3, 0xFF, 0xBF, 0xFF, 0x3F, 0x00, 0xFF, 0xBF, 0x7F, 0xFF, 0x9F, 0xFF, 0xBF, 0xFF,
    174    0xFF, 0x00, 0x00, 0xBF, 0x77, 0xF3, 0xF1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    175    0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
    176    0x91, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x7E, 0xFF, 0xFE,
    177    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    178    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC0, 0xFF, 0xC1, 0x00, 0xFE, 0xFF, 0xFF, 0xFF,
    179    0xF8, 0xFF, 0x00, 0x00, 0x00, 0x8F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    180    0xCE, 0xED, 0x66, 0x66, 0xCC, 0x0D, 0x00, 0x0B, 0x03, 0x73, 0x00, 0x83, 0x00, 0x0C, 0x00, 0x0D,
    181    0x00, 0x08, 0x11, 0x1F, 0x88, 0x89, 0x00, 0x0E, 0xDC, 0xCC, 0x6E, 0xE6, 0xDD, 0xDD, 0xD9, 0x99,
    182    0xBB, 0xBB, 0x67, 0x63, 0x6E, 0x0E, 0xEC, 0xCC, 0xDD, 0xDC, 0x99, 0x9F, 0xBB, 0xB9, 0x33, 0x3E,
    183    0x45, 0xEC, 0x42, 0xFA, 0x08, 0xB7, 0x07, 0x5D, 0x01, 0xF5, 0xC0, 0xFF, 0x08, 0xFC, 0x00, 0xE5,
    184    0x0B, 0xF8, 0xC2, 0xCA, 0xF4, 0xF9, 0x0D, 0x7F, 0x44, 0x6D, 0x19, 0xFE, 0x46, 0x97, 0x33, 0x5E,
    185    0x08, 0xFF, 0xD1, 0xFF, 0xC6, 0x8B, 0x24, 0x74, 0x12, 0xFC, 0x00, 0x9F, 0x94, 0xB7, 0x06, 0xD5,
    186    0x40, 0x7A, 0x20, 0x9E, 0x04, 0x5F, 0x41, 0x2F, 0x3D, 0x77, 0x36, 0x75, 0x81, 0x8A, 0x70, 0x3A,
    187    0x98, 0xD1, 0x71, 0x02, 0x4D, 0x01, 0xC1, 0xFF, 0x0D, 0x00, 0xD3, 0x05, 0xF9, 0x00, 0x0B, 0x00
    188};
    189
    190#endif	/* MEMORY_H */