Better memory errors and in general more runtime memory

This commit is contained in:
Konstantin Nazarov 2024-12-21 20:51:52 +00:00
parent 65e5011f7f
commit 2e9bb3fd76
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
3 changed files with 20 additions and 10 deletions

View file

@ -158,7 +158,9 @@ std::vector<uint8_t> load_elf(const std::string& filename, size_t memory_size) {
for (const Elf32Section& shdr : sectionHeaders) {
if (should_load_section(shdr)) {
if (shdr.sh_type == SHT_NOBITS) {
throw std::runtime_error("Trying to load section without data");
// Section contains no data - just skip it. It will be initialized
// to zero.
continue;
}
file.seekg(shdr.sh_offset);

View file

@ -10,7 +10,7 @@
#include "vm.hpp"
int main(int argc, char *argv[]) {
const size_t MEMORY_SIZE = 512 * 1024;
const size_t MEMORY_SIZE = 64 * 1024 * 1024;
bool debug = false;
std::string program_filename = "";

View file

@ -32,7 +32,8 @@ void VM::setreg(size_t regnum, uint32_t value) {
void UART::read_mem_u8(uint8_t* dst, size_t addr) {
if (addr < UART_ADDR || addr + 1 > UART_ADDR + 8) {
throw std::runtime_error("Memory access out of bounds");
throw std::runtime_error(std::string("Memory access out of bounds: ") +
std::format("{:x}", addr));
}
addr -= UART_ADDR;
switch (addr) {
@ -46,7 +47,8 @@ void UART::read_mem_u8(uint8_t* dst, size_t addr) {
void UART::write_mem_u8(uint8_t* src, size_t addr) {
if (addr < UART_ADDR || addr + 1 > UART_ADDR + 8) {
throw std::runtime_error("Memory access out of bounds");
throw std::runtime_error(std::string("Memory access out of bounds: ") +
std::format("{:x}", addr));
}
addr -= UART_ADDR;
switch (addr) {
@ -91,7 +93,8 @@ void VM::read_mem_u8(uint8_t* dst, size_t addr) {
return;
}
}
throw std::runtime_error("Memory access out of bounds");
throw std::runtime_error(std::string("Memory access out of bounds: ") +
std::format("{:x}", addr));
}
void VM::read_mem_u16(uint16_t* dst, size_t addr) {
@ -101,7 +104,8 @@ void VM::read_mem_u16(uint16_t* dst, size_t addr) {
return;
}
}
throw std::runtime_error("Memory access out of bounds");
throw std::runtime_error(std::string("Memory access out of bounds: ") +
std::format("{:x}", addr));
}
void VM::read_mem_u32(uint32_t* dst, size_t addr) {
@ -111,7 +115,8 @@ void VM::read_mem_u32(uint32_t* dst, size_t addr) {
return;
}
}
throw std::runtime_error("Memory access out of bounds");
throw std::runtime_error(std::string("Memory access out of bounds: ") +
std::format("{:x}", addr));
}
void VM::write_mem_u8(uint8_t* src, size_t addr) {
@ -121,7 +126,8 @@ void VM::write_mem_u8(uint8_t* src, size_t addr) {
return;
}
}
throw std::runtime_error("Memory access out of bounds");
throw std::runtime_error(std::string("Memory access out of bounds: ") +
std::format("{:x}", addr));
}
void VM::write_mem_u16(uint16_t* src, size_t addr) {
@ -131,7 +137,8 @@ void VM::write_mem_u16(uint16_t* src, size_t addr) {
return;
}
}
throw std::runtime_error("Memory access out of bounds");
throw std::runtime_error(std::string("Memory access out of bounds: ") +
std::format("{:x}", addr));
}
void VM::write_mem_u32(uint32_t* src, size_t addr) {
@ -141,7 +148,8 @@ void VM::write_mem_u32(uint32_t* src, size_t addr) {
return;
}
}
throw std::runtime_error("Memory access out of bounds");
throw std::runtime_error(std::string("Memory access out of bounds: ") +
std::format("{:x}", addr));
}
std::vector<uint8_t> VM::read_memory(size_t start, size_t size) {