diff --git a/src/rve.cpp b/src/rve.cpp index a9f3b02..e7e7786 100644 --- a/src/rve.cpp +++ b/src/rve.cpp @@ -8,33 +8,9 @@ #include #include -void load_program(const std::string& filename, uint8_t* memory, size_t memory_size) { - std::ifstream file(filename, std::ios::binary|std::ios::ate); - - if (!file.is_open()) { - throw std::runtime_error("Failed to open file: " + filename); - } - - size_t file_size = file.tellg(); - - if (file_size > memory_size) { - throw std::runtime_error("File is too big"); - } - - file.seekg(0, std::ios::beg); - - file.read(reinterpret_cast(memory), file_size); - - if (!file) { - throw std::runtime_error("Failed to read the complete program into memory."); - } - - file.close(); -} int main(int argc, char* argv[]) { const size_t MEMORY_SIZE = 128*1024; - uint8_t memory[MEMORY_SIZE] = {0}; bool debug = false; std::string program_filename = ""; @@ -50,27 +26,28 @@ int main(int argc, char* argv[]) { return 1; } + std::vector memory; try { - load_program(program_filename, memory, MEMORY_SIZE); + memory = load_program(program_filename, MEMORY_SIZE); } catch (const std::exception &e) { std::cerr << e.what() << std::endl; return 1; } - uint32_t *res1 = (uint32_t *)&memory[0x1000]; + VM vm(memory); - try { - eval(memory, MEMORY_SIZE); - } catch (const std::exception& e) { - std::cerr << "Emulator error: " << e.what() << std::endl; - return 1; - } + if (!debug) { + try { + vm.eval(); + } catch (const std::exception& e) { + std::cerr << "Emulator error: " << e.what() << std::endl; + return 1; + } + std::vector res_mem = vm.read_memory(0x1000, 4); + uint32_t *res = (uint32_t *)&res_mem[0]; - uint32_t *res = (uint32_t *)&memory[0x1000]; - - std::cout << "result: " << *res << std::endl; - - if (debug) { + std::cout << "result: " << *res << std::endl; + } else { // to debug, do: "set debug remote 1" in gdb // and then "target remote :1234" GDBStub stub(1234); diff --git a/src/vm.cpp b/src/vm.cpp index 4ed8489..b04a73b 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -5,6 +5,7 @@ #include #include #include +#include const int NUM_REGISTERS = 32; // Standard RISC-V has 32 registers @@ -13,7 +14,49 @@ inline int32_t sign_extend(int32_t value, int bits) { return (value ^ mask) - mask; } -void eval(uint8_t* memory, size_t memory_size) { +std::vector load_program(const std::string& filename, size_t memory_size) +{ + std::vector memory(memory_size, 0); + + std::ifstream file(filename, std::ios::binary|std::ios::ate); + + if (!file.is_open()) { + throw std::runtime_error("Failed to open file: " + filename); + } + + size_t file_size = file.tellg(); + + if (file_size > memory_size) { + throw std::runtime_error("File is too big"); + } + + file.seekg(0, std::ios::beg); + + file.read(reinterpret_cast(&memory[0]), file_size); + + if (!file) { + throw std::runtime_error("Failed to read the complete program into memory."); + } + + file.close(); + + return memory; +} + +VM::VM(std::vector memory) + : memory_(memory) { + +} + +std::vector VM::read_memory(size_t start, size_t size) { + return std::vector(memory_.begin() + start, + memory_.begin() + start + size); +} + +void VM::eval() { + size_t memory_size = memory_.size(); + uint8_t *memory = &memory_[0]; + uint32_t registers[NUM_REGISTERS] = {0}; uint32_t pc = 0; diff --git a/src/vm.hpp b/src/vm.hpp index 3fec57a..7bfbc2b 100644 --- a/src/vm.hpp +++ b/src/vm.hpp @@ -2,5 +2,19 @@ #include #include +#include +#include -void eval(uint8_t* memory, size_t memory_size); +std::vector load_program(const std::string& filename, size_t memory_size); + +class VM { +public: + VM(std::vector memory); + + void eval(); + + std::vector read_memory(size_t start, size_t size); + +private: + std::vector memory_; +};