cscg22-gearboy

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

CommonMemoryRule.h (3150B)


      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 COMMONMEMORYRULE_H
     21#define	COMMONMEMORYRULE_H
     22
     23#include "definitions.h"
     24
     25class Memory;
     26
     27class CommonMemoryRule
     28{
     29public:
     30    CommonMemoryRule(Memory* pMemory);
     31    ~CommonMemoryRule();
     32    u8 PerformRead(u16 address);
     33    void PerformWrite(u16 address, u8 value);
     34    void Reset(bool bCGB);
     35
     36private:
     37    Memory* m_pMemory;
     38    bool m_bCGB;
     39};
     40
     41#include "Memory.h"
     42
     43inline u8 CommonMemoryRule::PerformRead(u16 address)
     44{
     45    if (m_bCGB)
     46    {
     47        switch (address & 0xE000)
     48        {
     49            case 0x8000:
     50            {
     51                return m_pMemory->ReadCGBLCDRAM(address, false);
     52            }
     53            case 0xC000:
     54            {
     55                return m_pMemory->ReadCGBWRAM(address);
     56            }
     57        }
     58    }
     59    else if (address >= 0xFEA0 && address < 0xFF00)
     60    {
     61        return ((((address + ((address >> 4) - 0x0FEA)) >> 2) & 1) ? 0x00 : 0xFF);
     62    }
     63
     64    return m_pMemory->Retrieve(address);
     65}
     66
     67inline void CommonMemoryRule::PerformWrite(u16 address, u8 value)
     68{
     69    switch (address & 0xE000)
     70    {
     71        case 0x8000:
     72        {
     73            if (m_bCGB)
     74                m_pMemory->WriteCGBLCDRAM(address, value);
     75            else
     76                m_pMemory->Load(address, value);
     77            break;
     78        }
     79        case 0xC000:
     80        {
     81            if (address < 0xDE00)
     82            {
     83                if (m_bCGB)
     84                    m_pMemory->WriteCGBWRAM(address, value);
     85                else
     86                    m_pMemory->Load(address, value);
     87
     88                m_pMemory->Load(address + 0x2000, value);
     89            }
     90            else if (m_bCGB)
     91            {
     92                m_pMemory->WriteCGBWRAM(address, value);
     93            }
     94            else
     95            {
     96                m_pMemory->Load(address, value);
     97            }
     98            break;
     99        }
    100        case 0xE000:
    101        {
    102            if (address < 0xFE00)
    103            {
    104                if (m_bCGB)
    105                    m_pMemory->WriteCGBWRAM(address - 0x2000, value);
    106                else
    107                    m_pMemory->Load(address - 0x2000, value);
    108
    109                m_pMemory->Load(address, value);
    110            }
    111            else
    112            {
    113                m_pMemory->Load(address, value);
    114            }
    115            break;
    116        }
    117        default:
    118        {
    119            Log("--> ** Writing to invalid area %X %X", address, value);
    120            m_pMemory->Load(address, value);
    121        }
    122    }
    123}
    124
    125#endif	/* COMMONMEMORYRULE_H */