Add the first files (ARMv8 basic emu)

This commit is contained in:
ownedbywuigi 2025-06-05 21:03:38 -04:00
parent 304d94e955
commit 3f2d75276c
5 changed files with 93 additions and 1 deletions

17
core/ARM/cpu.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef CPU_H
#define CPU_H
#include <cstdint>
#include <cstring>
struct CPU {
uint64_t regs[31] = {0}; // X0X30
uint64_t pc = 0;
static const size_t MEM_SIZE = 64 * 1024;
uint8_t memory[MEM_SIZE];
CPU() { std::memset(memory, 0, MEM_SIZE); }
uint64_t& x(int i) { return regs[i]; }
};
#endif

44
core/JIT/jit.cpp Normal file
View file

@ -0,0 +1,44 @@
#include "core/JIT/jit.h"
#include <sys/mman.h>
#include <cstring>
#include <cstdio>
#include <vector>
using JitFunc = void(*)();
void JIT::translate_and_run(CPU& cpu) {
uint8_t* code = (uint8_t*)mmap(nullptr, 64, PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANON, -1, 0);
size_t offset = 0;
// Decode mock instructions from cpu.memory
if (cpu.memory[0] == 0x05) { // MOVZ placeholder
code[offset++] = 0x48; // mov rax, imm64
code[offset++] = 0xB8;
uint64_t imm = 5;
std::memcpy(&code[offset], &imm, sizeof(imm));
offset += 8;
}
if (cpu.memory[4] == 0x03) { // ADD placeholder
code[offset++] = 0x48; // add rax, imm32
code[offset++] = 0x05;
uint32_t addval = 3;
std::memcpy(&code[offset], &addval, sizeof(addval));
offset += 4;
}
code[offset++] = 0xC3; // ret
JitFunc fn = reinterpret_cast<JitFunc>(code);
uint64_t result;
asm volatile (
"call *%1\n"
"mov %%rax, %0\n"
: "=r"(result)
: "r"(fn)
: "%rax"
);
cpu.regs[0] = result;
}

10
core/JIT/jit.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef JIT_H
#define JIT_H
#include "core/ARM/cpu.h"
class JIT {
public:
void translate_and_run(CPU& cpu);
};
#endif