The main emulator class that contains CPU and Memory.
Machine(std::string_view binary, const MachineOptions& options = {});
Machine(const std::vector<uint8_t>& binary, const MachineOptions& options = {});Execution:
bool simulate(uint64_t max_instructions = UINT64_MAX, uint64_t counter = 0)- Execute programvoid stop()- Stop executionbool stopped() const- Check if stopped
System calls:
void install_syscall_handler(int syscall_number, syscall_t* handler)- Install syscalladdress_t system_call(int syscall_number)- Execute syscall
Function calls:
address_t vmcall(address_t func_addr, Args&&... args)- Call guest function by addressaddress_t vmcall(const std::string& func_name, Args&&... args)- Call guest function by name
Memory:
T read<T>(address_t addr)- Read memoryvoid write<T>(address_t addr, T value)- Write memory
Counters:
uint64_t instruction_counter() const- Get instruction countvoid set_instruction_counter(uint64_t val)- Set instruction countuint64_t max_instructions() const- Get instruction limitvoid set_max_instructions(uint64_t val)- Set instruction limit
CPU cpu- CPU stateMemory memory- Memory subsystem
CPU emulation with registers and execution.
Execution:
bool simulate(address_t pc, uint64_t icounter, uint64_t maxcounter)- Execute from PCvoid simulate_inaccurate(address_t pc)- Fast execution without instruction countingvoid simulate_precise()- Precise single-step executionvoid step_one(bool use_instruction_counter = true)- Execute one instruction
Register access:
Registers& registers()- Get register fileauto& reg(uint32_t idx)- Access general registeraddress_t pc() const- Get program countervoid jump(address_t addr)- Jump to address
Memory:
Memory& memory()- Get memory subsystem
Exceptions:
static void trigger_exception(ExceptionType type, address_t data = 0)- Trigger exception
Memory management and paging.
Memory(Machine& machine, std::string_view binary, const MachineOptions& options);Memory access:
T read<T>(address_t addr)- Read typed valuevoid write<T>(address_t addr, T value)- Write typed valuesize_t strlen(address_t addr, size_t maxlen = 4096)- String lengthstd::string memstring(address_t addr, size_t maxlen = 4096)- Read string
Memory allocation:
address_t mmap_allocate(size_t size)- Allocate memory regionvoid mmap_deallocate(address_t addr, size_t size)- Free memory region
Page management:
Page& get_page(address_t addr)- Get page for addressPage& create_page(address_t pageno)- Create new pagevoid free_pages(address_t addr, size_t count)- Free pages
Information:
address_t start_address() const- Get entry pointaddress_t stack_address() const- Get stack addresssize_t pages_active() const- Get active page countsize_t memory_usage_counter() const- Get memory usage
Register file for LoongArch CPU.
address_t pc- Program counterstruct { uint32_t whole; } fcsr- Floating-point control/status
auto& get(uint32_t idx)- Access general registerauto& getfl(uint32_t idx)- Access floating-point registervoid reset()- Reset all registersstd::string to_string() const- Debug string
REG_ZERO = 0 // Always zero
REG_RA = 1 // Return address
REG_TP = 2 // Thread pointer
REG_SP = 3 // Stack pointer
REG_A0 = 4 // Argument/return 0
REG_A1 = 5 // Argument/return 1
REG_A2 = 6 // Argument 2
REG_A3 = 7 // Argument 3
REG_A4 = 8 // Argument 4
REG_A5 = 9 // Argument 5
REG_A6 = 10 // Argument 6
REG_A7 = 11 // Argument 7 (syscall number)
REG_T0-T8 // Temporaries
REG_S0-S8 // Saved registers
REG_FP = 22 // Frame pointerstruct MachineOptions {
size_t memory_max = 64 * 1024 * 1024; // Max memory
bool verbose_loader = false; // Verbose ELF loading
bool ignore_text_section = false; // Skip .text section
};class MachineException : public std::runtime_error {
ExceptionType type() const;
uint64_t data() const;
};ILLEGAL_OPCODE- Invalid instructionILLEGAL_OPERATION- Invalid operationPROTECTION_FAULT- Memory protection violationEXECUTION_SPACE_PROTECTION_FAULT- Execute permission violationMISALIGNED_INSTRUCTION- Misaligned instruction fetchUNIMPLEMENTED_INSTRUCTION- Unimplemented instructionMACHINE_TIMEOUT- Instruction limit exceededOUT_OF_MEMORY- Memory allocation failedINVALID_PROGRAM- Invalid ELF fileFEATURE_DISABLED- Feature not compiled in
using syscall_t = void(Machine&);