diff --git a/sources/Memory/ARectangleMemory.cpp b/sources/Memory/ARectangleMemory.cpp index e1be35b..4252f74 100644 --- a/sources/Memory/ARectangleMemory.cpp +++ b/sources/Memory/ARectangleMemory.cpp @@ -13,7 +13,7 @@ namespace ComSquare::Memory uint8_t bank = addr >> 16u; uint16_t page = addr; unsigned bankCount = bank - this->_startBank; - unsigned pageCount = this->_endPage - this->_startPage; + unsigned pageCount = this->_endPage + 1 - this->_startPage; if (bank < this->_startBank || bank > this->_endBank) throw InvalidAddress("Rectangle memory: Invalid Bank", addr); diff --git a/sources/Memory/RectangleShadow.cpp b/sources/Memory/RectangleShadow.cpp index 8a7ad5e..325645d 100644 --- a/sources/Memory/RectangleShadow.cpp +++ b/sources/Memory/RectangleShadow.cpp @@ -21,7 +21,7 @@ namespace ComSquare::Memory uint24_t RectangleShadow::getRelativeAddress(uint24_t addr) const { uint24_t base = ARectangleMemory::getRelativeAddress(addr); - return base + this->_bankOffset * (this->_endPage - this->_startPage); + return base + this->_bankOffset * (1 + this->_endPage - this->_startPage); } uint8_t RectangleShadow::read(uint24_t addr) const @@ -34,7 +34,7 @@ namespace ComSquare::Memory return this->_initial->write(addr, data); } - RectangleShadow *RectangleShadow::setBankOffset(uint8_t bankOffset) + RectangleShadow *RectangleShadow::setBankOffset(int bankOffset) { this->_bankOffset = bankOffset; return this; diff --git a/sources/Memory/RectangleShadow.hpp b/sources/Memory/RectangleShadow.hpp index 2365550..8873f97 100644 --- a/sources/Memory/RectangleShadow.hpp +++ b/sources/Memory/RectangleShadow.hpp @@ -15,7 +15,7 @@ namespace ComSquare::Memory //! @brief Memory to shadow from. std::shared_ptr _initial; //! @brief The number of banks to add to the memory before accessing it from the initial data. - uint8_t _bankOffset = 0; + int _bankOffset = 0; public: //! @brief Create a shadow for the memory given as parameter. explicit RectangleShadow(std::shared_ptr initial, uint8_t startBank, uint8_t endBank, uint16_t startPage, uint16_t endPage); @@ -52,6 +52,7 @@ namespace ComSquare::Memory //! @return nullptr if isMirror is false, the source otherwise. std::shared_ptr getMirrored() const override; - RectangleShadow *setBankOffset(uint8_t bankOffset); + //! @brief Set the number of bank this component do not shadow. Referring to the first byte of this component will refer to the first byte of the bank at (bankOffset + start of initial memory). + RectangleShadow *setBankOffset(int bankOffset); }; } \ No newline at end of file diff --git a/tests/CPU/testDMA.cpp b/tests/CPU/testDMA.cpp index ee5243f..e661701 100644 --- a/tests/CPU/testDMA.cpp +++ b/tests/CPU/testDMA.cpp @@ -13,8 +13,8 @@ Test(DMA, RomToVRAM) snes.cartridge->_size = 4000000; snes.cartridge->_data = new uint8_t[snes.cartridge->_size]; for (unsigned i = 0; i < 0x400; i++) { - snes.cartridge->_data[0x9bded + i * 2] = i; - snes.cartridge->_data[0x9bded + i * 2 + 1] = i >> 8; + snes.cartridge->_data[0x9be00 + i * 2] = i; + snes.cartridge->_data[0x9be00 + i * 2 + 1] = i >> 8; } // Transferring $800 bytes from ROM ($13BE00) to VRam ($2000) via DMA channel 0 diff --git a/tests/testRectangleMemory.cpp b/tests/testRectangleMemory.cpp index c0b8376..69baaab 100644 --- a/tests/testRectangleMemory.cpp +++ b/tests/testRectangleMemory.cpp @@ -12,7 +12,7 @@ using namespace ComSquare; Test(RectangleMemory, HorizontalRamRead) { Ram::Ram ram(0xFF, Component::Rom, "Rom"); - ram.setMemoryRegion(0x00, 0xFF, 0x0000, 0x0001); + ram.setMemoryRegion(0x00, 0xFF, 0x0000, 0x0000); for (int i = 0x00; i < 0xFF; i++) ram._data[i] = i; for (uint24_t i = 0x000000; i < 0xFF0000; i += 0x010000) { @@ -24,7 +24,7 @@ Test(RectangleMemory, HorizontalRamRead) Test(RectangleMemory, HorizontalRamWrite) { Ram::Ram ram(0xFF, Component::Rom, "Rom"); - ram.setMemoryRegion(0x00, 0xFF, 0x0000, 0x0001); + ram.setMemoryRegion(0x00, 0xFF, 0x0000, 0x0000); for (uint24_t i = 0x000000; i < 0xFF0000; i += 0x010000) ram.write(ram.getRelativeAddress(i), i >> 16u); for (int i = 0x00; i < 0xFF; i++) @@ -34,7 +34,7 @@ Test(RectangleMemory, HorizontalRamWrite) Test(RectangleMemory, DualLineRamRead) { Ram::Ram ram(0xFF * 2, Component::Rom, "Rom"); - ram.setMemoryRegion(0x00, 0xFF, 0x0000, 0x0002); + ram.setMemoryRegion(0x00, 0xFF, 0x0000, 0x0001); for (int i = 0x00; i < 0xFF * 2; i++) ram._data[i] = i; for (uint24_t i = 0x000000, v = 0; v < 0xFF * 2; i += 0x010000, v += 2) { @@ -48,8 +48,8 @@ Test(RectangleMemory, DualLineRamRead) Test(RectangleMemory, HorizontalRamShadowRead) { std::shared_ptr ram = std::make_shared(0xFF, Component::Rom, "Rom"); - ram->setMemoryRegion(0x00, 0xFF, 0x0000, 0x0001); - Memory::RectangleShadow shadow(ram, 0x00, 0xFF, 0x8000, 0x8001); + ram->setMemoryRegion(0x00, 0xFF, 0x0000, 0x0000); + Memory::RectangleShadow shadow(ram, 0x00, 0xFF, 0x8000, 0x8000); for (int i = 0x00; i < 0xFF; i++) ram->_data[i] = i; for (uint24_t i = 0x008000; i < 0xFF8000; i += 0x010000) { @@ -60,8 +60,8 @@ Test(RectangleMemory, HorizontalRamShadowRead) Test(RectangleMemory, HorizontalRamShadowReadWithBankOffset) { std::shared_ptr ram = std::make_shared(0xFF, Component::Rom, "Rom"); - ram->setMemoryRegion(0x00, 0xFF, 0x0000, 0x0001); - Memory::RectangleShadow shadow(ram, 0x80, 0xFF, 0x8000, 0x8001); + ram->setMemoryRegion(0x00, 0xFF, 0x0000, 0x0000); + Memory::RectangleShadow shadow(ram, 0x80, 0xFF, 0x8000, 0x8000); for (int i = 0x00; i < 0xFF; i++) ram->_data[i] = i; shadow.setBankOffset(0x80); @@ -79,7 +79,7 @@ Test(RectangleMemory, ShadowOffsetCartridge) for (int i = 0x00; i < 0x3fff80; i++) ram->_data[i] = i; shadow.setBankOffset(0x40); - for (uint24_t i = 0xC00000; i < 0xEF7FFF; i += 0x1) { + for (uint24_t i = 0xC00000; i <= 0xEF7FFF; i += 0x1) { if ((uint16_t)i > 0x7FFFu) i += 0x010000 - 0x8000; uint8_t v = shadow.read(shadow.getRelativeAddress(i));