From 6f8dfc35a860364815e0e80e8869530581e74552 Mon Sep 17 00:00:00 2001 From: Sean Maas Date: Mon, 20 Nov 2023 01:22:29 -0500 Subject: [PATCH] Fix DLDI address overflow --- src/dldi.cpp | 21 +++++++++------------ src/dldi.h | 4 ++-- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/dldi.cpp b/src/dldi.cpp index c676934d..391ba63c 100644 --- a/src/dldi.cpp +++ b/src/dldi.cpp @@ -82,12 +82,12 @@ int Dldi::isInserted() return (sdImage ? 1 : 0); } -int Dldi::readSectors(bool cpu, int sector, int numSectors, uint32_t buf) +int Dldi::readSectors(bool cpu, uint32_t sector, uint32_t numSectors, uint32_t buf) { + // Get the SD offset and size in bytes if (!sdImage) return 0; - - const int offset = sector * 0x200; - const int size = numSectors * 0x200; + const uint64_t offset = uint64_t(sector) << 9; + const uint64_t size = uint64_t(numSectors) << 9; // Read data from the SD image uint8_t *data = new uint8_t[size]; @@ -97,17 +97,16 @@ int Dldi::readSectors(bool cpu, int sector, int numSectors, uint32_t buf) // Write the data to memory for (int i = 0; i < size; i++) core->memory.write(cpu, buf + i, data[i]); - delete[] data; return 1; } -int Dldi::writeSectors(bool cpu, int sector, int numSectors, uint32_t buf) +int Dldi::writeSectors(bool cpu, uint32_t sector, uint32_t numSectors, uint32_t buf) { + // Get the SD offset and size in bytes if (!sdImage) return 0; - - const int offset = sector * 0x200; - const int size = numSectors * 0x200; + const uint64_t offset = uint64_t(sector) << 9; + const uint64_t size = uint64_t(numSectors) << 9; // Read data from memory uint8_t *data = new uint8_t[size]; @@ -117,7 +116,6 @@ int Dldi::writeSectors(bool cpu, int sector, int numSectors, uint32_t buf) // Write the data to the SD image fseek(sdImage, offset, SEEK_SET); fwrite(data, sizeof(uint8_t), size, sdImage); - delete[] data; return 1; } @@ -130,9 +128,8 @@ int Dldi::clearStatus() int Dldi::shutdown() { - if (!sdImage) return 0; - // Close the SD image + if (!sdImage) return 0; fclose(sdImage); sdImage = nullptr; return 1; diff --git a/src/dldi.h b/src/dldi.h index 4bdf5f97..aaa46667 100644 --- a/src/dldi.h +++ b/src/dldi.h @@ -46,8 +46,8 @@ class Dldi int startup(); int isInserted(); - int readSectors(bool cpu, int sector, int numSectors, uint32_t buf); - int writeSectors(bool cpu, int sector, int numSectors, uint32_t buf); + int readSectors(bool cpu, uint32_t sector, uint32_t numSectors, uint32_t buf); + int writeSectors(bool cpu, uint32_t sector, uint32_t numSectors, uint32_t buf); int clearStatus(); int shutdown();