Skip to content

Commit

Permalink
Fix DLDI address overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Hydr8gon committed Nov 20, 2023
1 parent 2804863 commit 6f8dfc3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
21 changes: 9 additions & 12 deletions src/dldi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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<uint8_t>(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];
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/dldi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 6f8dfc3

Please sign in to comment.