cscg22-gearboy

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

RomOnlyMemoryRule.cpp (3672B)


      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 "RomOnlyMemoryRule.h"
     21#include "Video.h"
     22#include "Memory.h"
     23#include "Processor.h"
     24#include "Input.h"
     25#include "Cartridge.h"
     26
     27RomOnlyMemoryRule::RomOnlyMemoryRule(Processor* pProcessor,
     28        Memory* pMemory, Video* pVideo, Input* pInput,
     29        Cartridge* pCartridge, Audio* pAudio) : MemoryRule(pProcessor,
     30pMemory, pVideo, pInput, pCartridge, pAudio)
     31{
     32    Reset(false);
     33}
     34
     35RomOnlyMemoryRule::~RomOnlyMemoryRule()
     36{
     37}
     38
     39void RomOnlyMemoryRule::Reset(bool bCGB)
     40{
     41    m_bCGB = bCGB;
     42}
     43
     44u8 RomOnlyMemoryRule::PerformRead(u16 address)
     45{
     46    if (address >= 0xA000 && address < 0xC000)
     47    {
     48        if (m_pCartridge->GetRAMSize() > 0)
     49            return m_pMemory->Retrieve(address);
     50        else
     51        {
     52            Log("--> ** Attempting to read from RAM without ram in cart %X", address);
     53            return 0xFF;
     54        }
     55    }
     56    else
     57        return m_pMemory->Retrieve(address);
     58}
     59
     60void RomOnlyMemoryRule::PerformWrite(u16 address, u8 value)
     61{
     62    if (address < 0x8000)
     63    {
     64        // ROM
     65        Log("--> ** Attempting to write on ROM address %X %X", address, value);
     66    }
     67    else if (address >= 0xA000 && address < 0xC000)
     68    {
     69        if (m_pCartridge->GetRAMSize() > 0)
     70        {
     71            m_pMemory->Load(address, value);
     72        }
     73        else
     74        {
     75            Log("--> ** Attempting to write to RAM without ram in cart  %X %X", address, value);
     76        }
     77    }
     78    else
     79        m_pMemory->Load(address, value);
     80}
     81
     82void RomOnlyMemoryRule::SaveRam(std::ostream &file)
     83{
     84    Log("RomOnlyMemoryRule save RAM...");
     85
     86    for (int i = 0xA000; i < 0xC000; i++)
     87    {
     88        u8 ram_byte = m_pMemory->Retrieve(i);
     89        file.write(reinterpret_cast<const char*> (&ram_byte), 1);
     90    }
     91
     92    Log("RomOnlyMemoryRule save RAM done");
     93}
     94
     95bool RomOnlyMemoryRule::LoadRam(std::istream &file, s32 fileSize)
     96{
     97    Log("RomOnlyMemoryRule load RAM...");
     98
     99    if ((fileSize > 0) && (fileSize != 0x2000))
    100    {
    101        Log("RomOnlyMemoryRule incorrect size. Expected: %d Found: %d", 0x2000, fileSize);
    102        return false;
    103    }
    104
    105    for (int i = 0xA000; i < 0xC000; i++)
    106    {
    107        u8 ram_byte = 0;
    108        file.read(reinterpret_cast<char*> (&ram_byte), 1);
    109        m_pMemory->Load(i, ram_byte);
    110    }
    111
    112    Log("RomOnlyMemoryRule load RAM done");
    113
    114    return true;
    115}
    116
    117size_t RomOnlyMemoryRule::GetRamSize()
    118{
    119    return m_pCartridge->GetRAMBankCount() * 0x2000;
    120}
    121
    122u8* RomOnlyMemoryRule::GetRamBanks()
    123{
    124    return m_pMemory->GetMemoryMap() + 0xA000;
    125}
    126
    127u8* RomOnlyMemoryRule::GetCurrentRamBank()
    128{
    129    return m_pMemory->GetMemoryMap() + 0xA000;
    130}
    131
    132int RomOnlyMemoryRule::GetCurrentRamBankIndex()
    133{
    134    return 0;
    135}
    136
    137u8* RomOnlyMemoryRule::GetCurrentRomBank1()
    138{
    139    return m_pMemory->GetMemoryMap() + 0x4000;
    140}
    141
    142int RomOnlyMemoryRule::GetCurrentRomBank1Index()
    143{
    144    return 1;
    145}
    146
    147u8* RomOnlyMemoryRule::GetRomBank0()
    148{
    149    return m_pMemory->GetMemoryMap() + 0x0000;
    150}
    151
    152int RomOnlyMemoryRule::GetCurrentRomBank0Index()
    153{
    154    return 0;
    155}