From 74fd023ca1f7924d6c40bd8cdcbdc30898f9bf03 Mon Sep 17 00:00:00 2001 From: Lukasz Czarnik Date: Wed, 7 May 2025 23:24:00 +0200 Subject: [PATCH] refactor Memory class --- include/core/memory.hpp | 39 ++++++++++++++++++++++++++++ src/core/memory.cpp | 57 ++++++++++++++++++----------------------- 2 files changed, 64 insertions(+), 32 deletions(-) diff --git a/include/core/memory.hpp b/include/core/memory.hpp index a7b3868..a89b7f3 100644 --- a/include/core/memory.hpp +++ b/include/core/memory.hpp @@ -1,5 +1,6 @@ #pragma once #include "core/cartridge.hpp" +#include #include struct Memory { @@ -76,6 +77,44 @@ struct Memory { OBJ_PALETTE_DATA = 0xFF6B }; + //helpers + bool inRom00( const uint16_t index ) const { + return index < ROM_0N; + } + bool inRom0N( const uint16_t index ) const { + return ROM_0N <= index and index <= VIDEO_RAM; + } + bool inVideoRam( const uint16_t index ) const { + return VIDEO_RAM <= index and index <= EXTERNAL_RAM; + } + bool inExternalRam( const uint16_t index ) const { + return EXTERNAL_RAM <= index and index <= WORK_RAM_00; + } + bool inWorkRam00( const uint16_t index ) const { + return WORK_RAM_00 <= index and index <= WORK_RAM_0N; + } + bool inWorkRam0N( const uint16_t index ) const { + return WORK_RAM_0N <= index and index <= ECHO_RAM_00; + } + bool inEchoRam00( const uint16_t index ) const { + return ECHO_RAM_00 <= index and index <= ECHO_RAM_0N; + } + bool inEchoRam0N( const uint16_t index ) const { + return ECHO_RAM_0N <= index and index <= OBJECT_ATTRIBUTE_MEMORY; + } + bool inObjectAttributeMemory( const uint16_t index ) const { + return OBJECT_ATTRIBUTE_MEMORY <= index and index <= NOT_USABLE; + } + bool inNotUsable( const uint16_t index ) const { + return NOT_USABLE <= index and index <= IO_REGISTERS; + } + bool inIoRegisters( const uint16_t index ) const { + return IO_REGISTERS <= index and index <= HIGH_RAM; + } + bool inHighRam( const uint16_t index ) const { + return HIGH_RAM <= index and index <= INTERRUPT_ENABLE_REGISTER; + } + uint8_t read( const uint16_t index ) const; void write( const uint16_t index, uint8_t value ); void setVramLock( bool locked ); diff --git a/src/core/memory.cpp b/src/core/memory.cpp index 334db44..a8e635f 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -2,76 +2,69 @@ #include uint8_t Memory::read( const uint16_t index ) const { - if( index < VIDEO_RAM || ( index >= EXTERNAL_RAM && index <= WORK_RAM_00 ) ) + if( inRom00( index ) or inRom0N( index ) or inExternalRam( index ) ) return cartridge.read( index ); - else if( index < VIDEO_RAM + sizeof( videoRam ) ) { + if( inVideoRam( index ) ) { if( vramLock ) return 0xFF; - else - return videoRam[index - VIDEO_RAM]; - } else if( index < WORK_RAM_00 + sizeof( workRam00 ) ) + return videoRam[index - VIDEO_RAM]; + } + if( inWorkRam00( index ) ) return workRam00[index - WORK_RAM_00]; - else if( index < WORK_RAM_0N + sizeof( workRam0N ) ) + if( inWorkRam0N( index ) ) return workRam0N[index - WORK_RAM_0N]; - else if( index < ECHO_RAM_00 + sizeof( workRam00 ) ) + if( inEchoRam00( index ) ) return workRam00[index - ECHO_RAM_00]; - else if( index < OBJECT_ATTRIBUTE_MEMORY ) //echo RAM 0N is smaller than work RAM 0N + if( inEchoRam0N( index ) ) //echo RAM 0N is smaller than work RAM 0N return workRam0N[index - ECHO_RAM_0N]; - else if( index < OBJECT_ATTRIBUTE_MEMORY + sizeof( oam ) ) { + if( inObjectAttributeMemory( index ) ) { if( oamLock ) return 0xFF; - else - return oam[index - OBJECT_ATTRIBUTE_MEMORY]; + return oam[index - OBJECT_ATTRIBUTE_MEMORY]; } // TODO else if (index < NOT_USABLE + X) - else if( index < IO_REGISTERS + sizeof( ioRegisters ) ) + if( inIoRegisters( index ) ) return ioRegisters[index - IO_REGISTERS]; - else if( index < HIGH_RAM + sizeof( highRam ) ) + if( inHighRam( index ) ) return highRam[index - HIGH_RAM]; - else if( index == 0xFFFF ) + if( index == INTERRUPT_ENABLE_REGISTER ) return interruptEnableRegister; + return 0; } - void Memory::write( const uint16_t index, uint8_t value ) { - if( index < VIDEO_RAM || ( index >= EXTERNAL_RAM && index <= WORK_RAM_00 ) ) + if( inRom00( index ) or inRom0N( index ) or inExternalRam( index ) ) cartridge.write( index, value ); - else if( index < VIDEO_RAM + sizeof( videoRam ) ) + else if( inVideoRam( index ) ) videoRam[index - VIDEO_RAM] = value; - else if( index < WORK_RAM_00 + sizeof( workRam00 ) ) + else if( inWorkRam00( index ) ) workRam00[index - WORK_RAM_00] = value; - else if( index < WORK_RAM_0N + sizeof( workRam0N ) ) + else if( inWorkRam0N( index ) ) workRam0N[index - WORK_RAM_0N] = value; else if( index < ECHO_RAM_00 + sizeof( workRam00 ) ) workRam00[index - ECHO_RAM_00] = value; - else if( index < OBJECT_ATTRIBUTE_MEMORY ) //echo RAM 0N is smaller than work RAM 0N + else if( inEchoRam0N( index ) ) //echo RAM 0N is smaller than work RAM 0N workRam0N[index - ECHO_RAM_0N] = value; - else if( index < OBJECT_ATTRIBUTE_MEMORY + sizeof( oam ) ) + else if( inObjectAttributeMemory( index ) ) oam[index - OBJECT_ATTRIBUTE_MEMORY] = value; // TODO else if (index < NOT_USABLE + X) - else if( index < IO_REGISTERS + sizeof( ioRegisters ) ) { + else if( inIoRegisters( index ) ) { ioRegisters[index - IO_REGISTERS] = value; // side effects if( index == LCD_Y ) { if( value == read( LYC ) ) { - ioRegisters[static_cast( LCD_STATUS ) - static_cast( IO_REGISTERS )] |= - ( 1 << 2 ); + ioRegisters[(uint16_t)LCD_STATUS - IO_REGISTERS] |= ( 1 << 2 ); //TODO interrupt } else - ioRegisters[static_cast( LCD_STATUS ) - static_cast( IO_REGISTERS )] &= - ~( 1 << 2 ); + ioRegisters[(uint16_t)LCD_STATUS - IO_REGISTERS] &= ~( 1 << 2 ); } - - - } else if( index < HIGH_RAM + sizeof( highRam ) ) + } else if( inHighRam( index ) ) highRam[index - HIGH_RAM] = value; - else if( index == 0xFFFF ) + else if( index == INTERRUPT_ENABLE_REGISTER ) interruptEnableRegister = value; } - void Memory::setVramLock( bool locked ) { vramLock = locked; } - void Memory::setOamLock( bool locked ) { oamLock = locked; } -- GitLab