rve/src/vm.cpp

406 lines
14 KiB
C++
Raw Normal View History

#include "vm.hpp"
#include <cstdint>
#include <cstring>
2024-12-09 20:17:21 +00:00
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <vector>
inline int32_t sign_extend(int32_t value, int bits) {
int32_t mask = 1 << (bits - 1);
return (value ^ mask) - mask;
}
uint8_t UART::read_register(uint32_t address) {
switch (address) {
case UART_LSR:
// Always ready to transmit
return LSR_TRANSMITTER_EMPTY;
default:
return 0;
}
}
void UART::write_register(uint32_t address, uint8_t value) {
switch (address) {
case UART_THR:
std::cout.put(static_cast<char>(value));
break;
}
}
bool UART::is_transmitter_ready() {
return read_register(UART_LSR) & LSR_TRANSMITTER_EMPTY;
}
VM::VM(const std::vector<uint8_t>& memory, const std::string& file_path)
: memory_(memory), file_path(file_path) {}
2024-12-09 00:29:19 +00:00
void VM::setreg(int regnum, uint32_t value) {
if (regnum == 0) {
return;
}
registers[regnum] = value;
}
2024-12-09 00:29:19 +00:00
std::vector<uint8_t> VM::read_memory(size_t start, size_t size) {
2024-12-09 20:17:21 +00:00
if (start + size > memory_.size()) {
return std::vector<uint8_t>(size, 0);
}
2024-12-09 00:29:19 +00:00
return std::vector<uint8_t>(memory_.begin() + start,
memory_.begin() + start + size);
}
uint32_t VM::read_memory_word(size_t pos) {
if (pos + 3 >= memory_.size()) {
throw std::runtime_error("Memory access out of bounds");
}
return *(uint32_t*)&memory_[pos];
}
uint16_t VM::read_memory_half_word(size_t pos) {
if (pos + 1 >= memory_.size()) {
throw std::runtime_error("Memory access out of bounds");
}
return *(uint16_t*)&memory_[pos];
}
uint8_t VM::read_memory_byte(size_t pos) {
if (pos >= memory_.size()) {
throw std::runtime_error("Memory access out of bounds");
}
return memory_[pos];
}
2024-12-09 21:21:47 +00:00
void VM::write_memory_word(size_t pos, uint32_t value) {
if (pos + 1 >= memory_.size()) {
throw std::runtime_error("Memory access out of bounds");
}
2024-12-09 21:21:47 +00:00
*(uint32_t*)&memory_[pos] = value;
}
void VM::write_memory_half_word(size_t pos, uint16_t value) {
if (pos + 3 >= memory_.size()) {
throw std::runtime_error("Memory access out of bounds");
}
*(uint16_t*)&memory_[pos] = value;
}
void VM::write_memory_byte(size_t pos, uint8_t value) {
if (is_mmap(pos, 1)) {
if (pos >= UART_ADDR && pos < UART_ADDR + 8) {
uart.write_register(pos - UART_ADDR, value);
}
return;
}
if (pos >= memory_.size()) {
throw std::runtime_error("Memory access out of bounds");
}
memory_[pos] = value;
}
bool VM::is_mmap(size_t pos, size_t size) {
if (pos + size < UART_ADDR) return false;
if (pos >= UART_ADDR + 8) return false;
return (pos < UART_ADDR + 8) && (pos + size >= UART_ADDR - 1);
}
uint32_t VM::read_register(size_t regnum) {
2024-12-09 20:17:21 +00:00
if (regnum == 32) return pc;
if (regnum >= NUM_REGISTERS) {
throw std::runtime_error("Register out of range");
}
return registers[regnum];
}
const std::string& VM::get_file_path() { return file_path; }
2024-12-09 20:17:21 +00:00
void VM::step() {
uint32_t instr = *(uint32_t*)&memory_[pc];
2024-12-09 20:17:21 +00:00
// std::cout << "pc: " << std::hex << pc << std::dec << "\n";
// std::cout << "instr: " << std::hex << instr << "\n";
2024-12-09 20:17:21 +00:00
pc += 4;
2024-12-09 20:17:21 +00:00
// Decode instruction
uint32_t opcode = instr & 0x7F;
uint32_t rd = (instr >> 7) & 0x1F;
uint32_t funct3 = (instr >> 12) & 0x7;
uint32_t rs1 = (instr >> 15) & 0x1F;
uint32_t rs2 = (instr >> 20) & 0x1F;
uint32_t funct7 = (instr >> 25);
int32_t imm;
2024-12-09 20:17:21 +00:00
switch (opcode) {
case 0x33: { // R-type
2024-12-06 21:58:16 +00:00
if (funct7 == 0x00) {
2024-12-09 20:17:21 +00:00
if (funct3 == 0x0) { // ADD
setreg(rd, registers[rs1] + registers[rs2]);
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x04) { // XOR
setreg(rd, registers[rs1] ^ registers[rs2]);
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x06) { // OR
2024-12-14 23:24:36 +00:00
setreg(rd, registers[rs1] | registers[rs2]);
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x07) { // AND
setreg(rd, registers[rs1] & registers[rs2]);
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x01) { // SLL
setreg(rd, registers[rs1] << registers[rs2]);
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x05) { // SRL
2024-12-06 21:58:16 +00:00
uint32_t value = registers[rs1];
uint32_t shift_amount = registers[rs2] & 0x1F;
setreg(rd, value >> shift_amount);
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x02) { // SLT
2024-12-14 23:24:36 +00:00
setreg(rd, (static_cast<int32_t>(registers[rs1]) <
static_cast<int32_t>(registers[rs2]))
? 0
: 1);
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x03) { // SLTU
setreg(rd, (registers[rs1] < registers[rs2]) ? 1 : 0);
2024-12-06 21:58:16 +00:00
} else {
throw std::runtime_error("Unknown R-type instruction");
}
2024-12-06 21:58:16 +00:00
} else if (funct7 == 0x20) {
2024-12-09 20:17:21 +00:00
if (funct3 == 0x0) { // SUB
2024-12-14 23:24:36 +00:00
setreg(rd, registers[rs1] - registers[rs2]);
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x05) { // SRA
2024-12-06 21:58:16 +00:00
// Only the lower 5 bits are used for shift
int32_t value = static_cast<int32_t>(registers[rs1]);
int32_t shift_amount = registers[rs2] & 0x1F;
setreg(rd, value >> shift_amount);
2024-12-06 21:58:16 +00:00
} else {
throw std::runtime_error("Unknown R-type instruction");
}
} else if (funct7 == 0x01) {
2024-12-09 20:17:21 +00:00
if (funct3 == 0x0) { // MUL
int64_t result =
static_cast<int64_t>(static_cast<int32_t>(registers[rs1])) *
static_cast<int64_t>(static_cast<int32_t>(registers[rs2]));
setreg(rd, static_cast<uint32_t>(result));
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x1) { // MULH
int64_t result =
static_cast<int64_t>(static_cast<int32_t>(registers[rs1])) *
static_cast<int64_t>(static_cast<int32_t>(registers[rs2]));
setreg(rd, static_cast<uint32_t>(result >> 32));
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x2) { // MULSU
int64_t result =
static_cast<int64_t>(static_cast<int32_t>(registers[rs1])) *
static_cast<uint64_t>(registers[rs2]);
setreg(rd, static_cast<uint32_t>(result >> 32));
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x3) { // MULU
2024-12-06 21:58:16 +00:00
uint64_t result = static_cast<uint64_t>(registers[rs1]) *
2024-12-09 20:17:21 +00:00
static_cast<uint64_t>(registers[rs2]);
setreg(rd, static_cast<uint32_t>(result >> 32)); // Upper 32 bits
} else if (funct3 == 0x4) { // DIV
2024-12-06 21:58:16 +00:00
int32_t dividend = static_cast<int32_t>(registers[rs1]);
int32_t divisor = static_cast<int32_t>(registers[rs2]);
if (divisor == 0) {
setreg(rd, -1); // Division by zero result
2024-12-06 21:58:16 +00:00
} else if (dividend == INT32_MIN && divisor == -1) {
setreg(rd, dividend); // Overflow case
2024-12-06 21:58:16 +00:00
} else {
setreg(rd, dividend / divisor);
2024-12-06 21:58:16 +00:00
}
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x5) { // DIVU
2024-12-06 21:58:16 +00:00
uint32_t dividend = registers[rs1];
uint32_t divisor = registers[rs2];
setreg(rd, (divisor == 0) ? UINT32_MAX : dividend / divisor);
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x6) { // REM
2024-12-06 21:58:16 +00:00
int32_t dividend = static_cast<int32_t>(registers[rs1]);
int32_t divisor = static_cast<int32_t>(registers[rs2]);
if (divisor == 0) {
setreg(rd,
dividend); // Remainder with zero divisor is the dividend
2024-12-06 21:58:16 +00:00
} else if (dividend == INT32_MIN && divisor == -1) {
setreg(rd, 0); // Overflow case
2024-12-06 21:58:16 +00:00
} else {
setreg(rd, dividend % divisor);
2024-12-06 21:58:16 +00:00
}
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x7) { // REMU
2024-12-06 21:58:16 +00:00
uint32_t dividend = registers[rs1];
uint32_t divisor = registers[rs2];
setreg(rd, (divisor == 0) ? dividend : dividend % divisor);
2024-12-06 21:58:16 +00:00
} else {
throw std::runtime_error("Unknown R-type instruction");
}
} else {
throw std::runtime_error("Unknown R-type instruction");
}
break;
}
case 0x13: { // I-type (ADDI and friends)
2024-12-09 20:17:21 +00:00
imm = sign_extend(instr >> 20, 12); // Extract 12-bit immediate
if (funct3 == 0x0) { // ADDI
setreg(rd, registers[rs1] + imm);
} else if (funct3 == 0x4) { // XORI
setreg(rd, registers[rs1] ^ imm);
} else if (funct3 == 0x6) { // ORI
setreg(rd, registers[rs1] | imm);
} else if (funct3 == 0x07) { // ANDI
setreg(rd, registers[rs1] & imm);
} else if (funct3 == 0x01) {
if (((imm >> 5) & 0x7f) == 0x0) { // SLLI
uint32_t value = registers[rs1];
uint32_t shift_amount = imm & 0x1F;
setreg(rd, value << shift_amount);
} else {
throw std::runtime_error("Unknown I-type instruction");
}
} else if (funct3 == 0x05) {
if (((imm >> 5) & 0x7f) == 0x20) { // SRAI
int32_t value = static_cast<int32_t>(imm & 0x1f);
int32_t shift_amount = imm & 0x1F;
setreg(rd, value >> shift_amount);
} else if (((imm >> 5) & 0x7f) == 0x0) { // SRLI
uint32_t value = registers[rs1];
uint32_t shift_amount = imm & 0x1F;
setreg(rd, value >> shift_amount);
} else {
throw std::runtime_error("Unknown I-type instruction");
}
2024-12-15 00:36:09 +00:00
} else if (funct3 == 0x02) { // SLTI
setreg(rd, (static_cast<int32_t>(registers[rs1]) <
static_cast<int32_t>(imm))
? 0
: 1);
} else if (funct3 == 0x03) { // SLTIU
setreg(rd, (registers[rs1] < static_cast<uint32_t>(imm)) ? 1 : 0);
2024-12-06 21:58:16 +00:00
} else {
throw std::runtime_error("Unknown I-type instruction");
}
break;
}
2024-12-09 20:17:21 +00:00
case 0x63: { // B-type (branches)
imm = ((int64_t)(int32_t)(instr & 0x80000000) >> 19) |
((instr & 0x80) << 4) // imm[11]
| ((instr >> 20) & 0x7e0) // imm[10:5]
| ((instr >> 7) & 0x1e);
if (funct3 == 0x0) { // BEQ
2024-12-06 21:58:16 +00:00
if (registers[rs1] == registers[rs2]) {
2024-12-09 20:17:21 +00:00
pc += imm - 4; // Offset PC (adjust for pre-increment)
2024-12-06 21:58:16 +00:00
}
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x1) { // BNE
2024-12-06 21:58:16 +00:00
if (registers[rs1] != registers[rs2]) {
2024-12-09 20:17:21 +00:00
pc += imm - 4; // Offset PC
2024-12-06 21:58:16 +00:00
}
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x4) { // BLT
2024-12-06 21:58:16 +00:00
if (static_cast<int32_t>(registers[rs1]) <
static_cast<int32_t>(registers[rs2])) {
pc += imm - 4;
}
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x5) { // BGE
2024-12-06 21:58:16 +00:00
if (static_cast<int32_t>(registers[rs1]) >=
static_cast<int32_t>(registers[rs2])) {
pc += imm - 4;
}
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x6) { // BLTU
2024-12-06 21:58:16 +00:00
if (registers[rs1] < registers[rs2]) pc += imm - 4;
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x7) { // BGEU
2024-12-06 21:58:16 +00:00
if (registers[rs1] >= registers[rs2]) pc += imm - 4;
} else {
throw std::runtime_error("Unknown B-type instruction");
}
break;
}
2024-12-09 20:17:21 +00:00
case 0x03: { // I-type (loads)
imm = sign_extend(instr >> 20, 12); // Extract 12-bit immediate
if (funct3 == 0x00) { // LB
2024-12-06 21:58:16 +00:00
uint32_t addr = registers[rs1] + imm;
setreg(rd, read_memory_byte(addr));
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x01) { // LH
2024-12-06 21:58:16 +00:00
uint32_t addr = registers[rs1] + imm;
setreg(rd, read_memory_half_word(addr));
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x2) { // LW
2024-12-06 21:58:16 +00:00
uint32_t addr = registers[rs1] + imm;
setreg(rd, read_memory_word(addr));
} else if (funct3 == 0x4) { // LBU
uint32_t addr = registers[rs1] + imm;
setreg(rd, read_memory_byte(addr));
} else if (funct3 == 0x5) { // LHU
uint32_t addr = registers[rs1] + imm;
setreg(rd, read_memory_half_word(addr));
2024-12-06 21:58:16 +00:00
} else {
throw std::runtime_error("Unknown load instruction");
}
break;
}
2024-12-09 20:17:21 +00:00
case 0x23: { // S-type (SW)
imm = ((instr >> 7) & 0x1F) | (((instr >> 25) & 0x7F) << 5);
2024-12-09 20:17:21 +00:00
imm = sign_extend(imm, 12); // Sign-extend 12-bit immediate
if (funct3 == 0x0) { // SB
2024-12-06 21:58:16 +00:00
uint32_t addr = registers[rs1] + imm;
write_memory_byte(addr, registers[rs2]);
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x1) { // SH
2024-12-06 21:58:16 +00:00
uint32_t addr = registers[rs1] + imm;
if (addr + 2 > memory_.size()) {
2024-12-06 21:58:16 +00:00
throw std::runtime_error("Memory access out of bounds");
}
std::memcpy(&memory_[addr], &registers[rs2], sizeof(uint16_t));
2024-12-09 20:17:21 +00:00
} else if (funct3 == 0x2) { // SW
2024-12-06 21:58:16 +00:00
uint32_t addr = registers[rs1] + imm;
if (addr + 4 > memory_.size()) {
2024-12-06 21:58:16 +00:00
throw std::runtime_error("Memory access out of bounds");
}
std::memcpy(&memory_[addr], &registers[rs2], sizeof(uint32_t));
2024-12-06 21:58:16 +00:00
} else {
throw std::runtime_error("Unknown store instruction");
}
break;
}
2024-12-09 20:17:21 +00:00
case 0x6F: { // JAL
int32_t offset =
((instr & 0x80000000) ? 0xFFF00000
: 0) | // Sign-extension for imm[20]
((instr >> 21) & 0x3FF) << 1 | // imm[10:1]
((instr >> 20) & 0x1) << 11 | // imm[11]
((instr & 0xFF000)); // imm[19:12]
2024-12-06 21:58:16 +00:00
setreg(rd, pc); // Save return address
2024-12-06 21:58:16 +00:00
pc += offset - 4;
break;
}
2024-12-09 20:17:21 +00:00
case 0x67: { // JALR
int32_t offset = (instr >> 20); // Sign-extended 12-bit immediate
uint32_t target =
(registers[rs1] + offset) & ~1; // Target address (LSB cleared)
2024-12-06 21:58:16 +00:00
setreg(rd, pc); // Save return address
pc = target;
2024-12-06 21:58:16 +00:00
break;
}
2024-12-09 20:17:21 +00:00
case 0x37: { // LUI
uint32_t imm = (instr >> 12) & 0xFFFFF; // Extract 20-bit immediate
setreg(rd,
imm << 12); // Shift the immediate to the upper 20 bits of the
// register
2024-12-06 21:58:16 +00:00
break;
}
2024-12-09 20:17:21 +00:00
case 0x17: { // AUIPC
uint32_t imm = (instr >> 12) & 0xFFFFF; // Extract 20-bit immediate
setreg(rd,
pc - 4 + (imm << 12)); // Add the immediate (shifted left) to
// the current PC
2024-12-06 21:58:16 +00:00
break;
}
2024-12-09 21:21:47 +00:00
case 0x73: { // EBREAK
pc -= 4;
throw EbreakException();
2024-12-06 23:50:21 +00:00
break;
}
2024-12-06 21:58:16 +00:00
default:
throw std::runtime_error("Unknown opcode");
2024-12-09 20:17:21 +00:00
}
}
void VM::eval() {
2024-12-09 21:21:47 +00:00
while (true) {
2024-12-09 20:17:21 +00:00
step();
2024-12-06 21:58:16 +00:00
}
}