Make VM memory a vector

This commit is contained in:
Konstantin Nazarov 2024-12-09 00:29:19 +00:00
parent 4cf11ae1e4
commit 78a2a460e8
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
3 changed files with 73 additions and 39 deletions

View file

@ -8,33 +8,9 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
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<char*>(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[]) { int main(int argc, char* argv[]) {
const size_t MEMORY_SIZE = 128*1024; const size_t MEMORY_SIZE = 128*1024;
uint8_t memory[MEMORY_SIZE] = {0};
bool debug = false; bool debug = false;
std::string program_filename = ""; std::string program_filename = "";
@ -50,27 +26,28 @@ int main(int argc, char* argv[]) {
return 1; return 1;
} }
std::vector<uint8_t> memory;
try { try {
load_program(program_filename, memory, MEMORY_SIZE); memory = load_program(program_filename, MEMORY_SIZE);
} catch (const std::exception &e) { } catch (const std::exception &e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
return 1; return 1;
} }
uint32_t *res1 = (uint32_t *)&memory[0x1000]; VM vm(memory);
if (!debug) {
try { try {
eval(memory, MEMORY_SIZE); vm.eval();
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << "Emulator error: " << e.what() << std::endl; std::cerr << "Emulator error: " << e.what() << std::endl;
return 1; return 1;
} }
std::vector res_mem = vm.read_memory(0x1000, 4);
uint32_t *res = (uint32_t *)&memory[0x1000]; uint32_t *res = (uint32_t *)&res_mem[0];
std::cout << "result: " << *res << std::endl; std::cout << "result: " << *res << std::endl;
} else {
if (debug) {
// to debug, do: "set debug remote 1" in gdb // to debug, do: "set debug remote 1" in gdb
// and then "target remote :1234" // and then "target remote :1234"
GDBStub stub(1234); GDBStub stub(1234);

View file

@ -5,6 +5,7 @@
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <vector> #include <vector>
#include <fstream>
const int NUM_REGISTERS = 32; // Standard RISC-V has 32 registers 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; return (value ^ mask) - mask;
} }
void eval(uint8_t* memory, size_t memory_size) { std::vector<uint8_t> load_program(const std::string& filename, size_t memory_size)
{
std::vector<uint8_t> 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<char*>(&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<uint8_t> memory)
: memory_(memory) {
}
std::vector<uint8_t> VM::read_memory(size_t start, size_t size) {
return std::vector<uint8_t>(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 registers[NUM_REGISTERS] = {0};
uint32_t pc = 0; uint32_t pc = 0;

View file

@ -2,5 +2,19 @@
#include <cstdint> #include <cstdint>
#include <cstddef> #include <cstddef>
#include <string>
#include <vector>
void eval(uint8_t* memory, size_t memory_size); std::vector<uint8_t> load_program(const std::string& filename, size_t memory_size);
class VM {
public:
VM(std::vector<uint8_t> memory);
void eval();
std::vector<uint8_t> read_memory(size_t start, size_t size);
private:
std::vector<uint8_t> memory_;
};