From 8a852d2cab145e74e1b5d791aa6a25d82bdf4683 Mon Sep 17 00:00:00 2001 From: Wunkolo Date: Sun, 11 Jun 2023 01:05:44 -0700 Subject: [PATCH 1/3] Implement `SaveDataArchive::createFile` Based on `ExtSaveDataArchive::createFile`. Gets some games farther that try to create save-data upon boot. --- src/core/fs/archive_save_data.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/core/fs/archive_save_data.cpp b/src/core/fs/archive_save_data.cpp index 758c1312..5e020fbb 100644 --- a/src/core/fs/archive_save_data.cpp +++ b/src/core/fs/archive_save_data.cpp @@ -5,7 +5,29 @@ namespace fs = std::filesystem; FSResult SaveDataArchive::createFile(const FSPath& path, u64 size) { - Helpers::panic("[SaveData] CreateFile not yet supported"); + if (size == 0) + Helpers::panic("SaveData file does not support size == 0"); + + if (path.type == PathType::UTF16) { + if (!isPathSafe(path)) + Helpers::panic("Unsafe path in SaveData::CreateFile"); + + fs::path p = IOFile::getAppData() / "SaveData"; + p += fs::path(path.utf16_string).make_preferred(); + + if (fs::exists(p)) + return FSResult::AlreadyExists; + + // Create a file of size "size" by creating an empty one, seeking to size - 1 and just writing a 0 there + IOFile file(p.string().c_str(), "wb"); + if (file.seek(size - 1, SEEK_SET) && file.writeBytes("", 1).second == 1) { + return FSResult::Success; + } + + return FSResult::FileTooLarge; + } + + Helpers::panic("SaveDataArchive::OpenFile: Failed"); return FSResult::Success; } From 5fa4e9e1789db9ea2a97d4d3db6ca0e012cd9b55 Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Sun, 11 Jun 2023 12:31:42 +0300 Subject: [PATCH 2/3] [SaveData] Handle size == 0 in CreateFile --- src/core/fs/archive_save_data.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/core/fs/archive_save_data.cpp b/src/core/fs/archive_save_data.cpp index 5e020fbb..572d22e0 100644 --- a/src/core/fs/archive_save_data.cpp +++ b/src/core/fs/archive_save_data.cpp @@ -5,9 +5,6 @@ namespace fs = std::filesystem; FSResult SaveDataArchive::createFile(const FSPath& path, u64 size) { - if (size == 0) - Helpers::panic("SaveData file does not support size == 0"); - if (path.type == PathType::UTF16) { if (!isPathSafe(path)) Helpers::panic("Unsafe path in SaveData::CreateFile"); @@ -17,10 +14,16 @@ FSResult SaveDataArchive::createFile(const FSPath& path, u64 size) { if (fs::exists(p)) return FSResult::AlreadyExists; - - // Create a file of size "size" by creating an empty one, seeking to size - 1 and just writing a 0 there + IOFile file(p.string().c_str(), "wb"); - if (file.seek(size - 1, SEEK_SET) && file.writeBytes("", 1).second == 1) { + + // If the size is 0, leave the file empty and return success + if (size == 0) { + return FSResult::Success; + } + + // If it is not empty, seek to size - 1 and write a 0 to create a file of size "size" + else if (file.seek(size - 1, SEEK_SET) && file.writeBytes("", 1).second == 1) { return FSResult::Success; } @@ -192,4 +195,4 @@ Rust::Result SaveDataArchive::openArchive(const FSPath& std::optional SaveDataArchive::readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) { Helpers::panic("Unimplemented SaveData::ReadFile"); return 0; -} \ No newline at end of file +} From cba1ce8fb8ff99c73f5620ccdadd05c04c642e15 Mon Sep 17 00:00:00 2001 From: wheremyfoodat Date: Sun, 11 Jun 2023 13:14:35 +0300 Subject: [PATCH 3/3] Implement 8-bit VRAM writes --- include/memory.hpp | 4 +++- src/core/PICA/gpu.cpp | 2 +- src/core/memory.cpp | 10 +++++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/include/memory.hpp b/include/memory.hpp index 0e75f36c..33b18ca5 100644 --- a/include/memory.hpp +++ b/include/memory.hpp @@ -95,6 +95,7 @@ namespace KernelMemoryTypes { class Memory { u8* fcram; u8* dspRam; + u8* vram; // Provided to the memory class by the GPU class u64& cpuTicks; // Reference to the CPU tick counter using SharedMemoryBlock = KernelMemoryTypes::SharedMemoryBlock; @@ -237,6 +238,7 @@ public: u8* getDSPMem() { return dspRam; } u8* getDSPDataMem() { return &dspRam[DSP_DATA_MEMORY_OFFSET]; } u8* getDSPCodeMem() { return &dspRam[DSP_CODE_MEMORY_OFFSET]; } - u32 getUsedUserMem() { return usedUserMemory; } + + void setVRAM(u8* pointer) { vram = pointer; } }; \ No newline at end of file diff --git a/src/core/PICA/gpu.cpp b/src/core/PICA/gpu.cpp index 9d4c06bc..599f79fd 100644 --- a/src/core/PICA/gpu.cpp +++ b/src/core/PICA/gpu.cpp @@ -7,6 +7,7 @@ using namespace Floats; GPU::GPU(Memory& mem) : mem(mem), renderer(*this, regs) { vram = new u8[vramSize]; + mem.setVRAM(vram); // Give the bus a pointer to our VRAM } void GPU::reset() { @@ -256,7 +257,6 @@ void GPU::fireDMA(u32 dest, u32 source, u32 size) { std::memcpy(&vram[dest - vramStart], &fcram[source - fcramStart], size); } else { printf("Non-trivially optimizable GPU DMA. Falling back to byte-by-byte transfer"); - std::memcpy(&vram[dest - vramStart], mem.getReadPointer(source), size); for (u32 i = 0; i < size; i++) { mem.write8(dest + i, mem.read8(source + i)); diff --git a/src/core/memory.cpp b/src/core/memory.cpp index 6d64f3f8..37c13c7d 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -143,7 +143,15 @@ void Memory::write8(u32 vaddr, u8 value) { *(u8*)(pointer + offset) = value; } else { - Helpers::panic("Unimplemented 8-bit write, addr: %08X, val: %02X", vaddr, value); + // VRAM write + if (vaddr >= VirtualAddrs::VramStart && vaddr < VirtualAddrs::VramStart + VirtualAddrs::VramSize) { + // TODO: Invalidate renderer caches here + vram[vaddr - VirtualAddrs::VramStart] = value; + } + + else { + Helpers::panic("Unimplemented 8-bit write, addr: %08X, val: %02X", vaddr, value); + } } }