Implement main thread stack

This commit is contained in:
wheremyfoodat 2022-09-15 17:50:14 +03:00
parent 275c6dfd0c
commit c33d7e5dfb
3 changed files with 20 additions and 2 deletions

View file

@ -11,8 +11,10 @@ namespace VirtualAddrs {
ExecutableEnd = 0x00100000 + 0x03F00000,
// Stack for main ARM11 thread.
// Typically 0x4000 bytes
StackTop = 0x10000000
// Typically 0x4000 bytes, determined by exheader
StackTop = 0x10000000,
StackBottom = 0x0FFFC000,
StackSize = 0x4000
};
}

View file

@ -5,6 +5,8 @@ Memory::Memory() {
readTable.resize(totalPageCount, 0);
writeTable.resize(totalPageCount, 0);
constexpr u32 fcramPageCount = 128_MB / pageSize;
// Map 63MB of FCRAM in the executable section as read/write
// TODO: This should probably be read-only, but making it r/w shouldn't hurt?
// Because if that were the case then writes would cause data aborts, so games wouldn't write to read-only memory
@ -15,6 +17,19 @@ Memory::Memory() {
readTable[page] = pointer;
writeTable[page++] = pointer;
}
// Map stack pages as R/W
// We have 16KB for the stack, so we allocate the last 4 pages of FCRAM for the stack
u32 stackPageCount = VirtualAddrs::StackSize / pageSize;
u32 fcramStackPage = fcramPageCount - 4;
page = VirtualAddrs::StackBottom / pageSize;
for (u32 i = 0; i < 4; i++) {
auto pointer = (uintptr_t)&fcram[fcramStackPage * pageSize];
readTable[page] = pointer;
writeTable[page++] = pointer;
fcramStackPage++;
}
}
u8 Memory::read8(u32 vaddr) {

View file

@ -38,6 +38,7 @@ bool Emulator::loadELF(std::filesystem::path& path) {
if (!entrypoint.has_value())
return false;
cpu.setReg(13, VirtualAddrs::StackTop); // Set initial SP
cpu.setReg(15, entrypoint.value()); // Set initial PC
return true;
}