Implemented using Antigravity and it actually works!
This commit is contained in:
@@ -1,10 +1,94 @@
|
||||
#include "../../Headers/Sagittarius.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int32_t syscall_print(SagittariusCore* core){
|
||||
uint64_t str_offset=core->reg.head[40];
|
||||
printf("%s",core->memory->data+str_offset);
|
||||
uint64_t str_offset = 0;
|
||||
memcpy(&str_offset, &core->reg.head[40], 8);
|
||||
if (str_offset >= core->memory->size) {
|
||||
printf("[Syscall Print Error: String offset 0x%llx is OOB]\n", (unsigned long long)str_offset);
|
||||
return -1;
|
||||
}
|
||||
printf("%s", (char*)(core->memory->data + str_offset));
|
||||
return 0;
|
||||
}
|
||||
int main(int ac,char** av){
|
||||
printf("Copyright (C) 2026 Creeper Lv.\n");
|
||||
|
||||
int32_t my_panic_handler(SagittariusCore *core, uint64_t panic_id, const char *msg) {
|
||||
printf("\n--- VM PANIC! ID: 0x%llx, PC: 0x%llx ---\n", (unsigned long long)panic_id, (unsigned long long)core->pc);
|
||||
printf("Message: %s\n", msg);
|
||||
exit(1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int ac, char** av) {
|
||||
if (ac < 2) {
|
||||
printf("Usage: Sagittarius <compiled_program_file>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
char* filepath = av[1];
|
||||
FILE* f = fopen(filepath, "rb");
|
||||
if (!f) {
|
||||
printf("Error: Cannot open file '%s'\n", filepath);
|
||||
return 1;
|
||||
}
|
||||
|
||||
fseek(f, 0, SEEK_END);
|
||||
long sz = ftell(f);
|
||||
if (sz < 0) { fclose(f); return 1; }
|
||||
fseek(f, 0, SEEK_SET);
|
||||
|
||||
uint8_t* byte_buf = malloc(sz);
|
||||
if (!byte_buf) {
|
||||
fclose(f);
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t read_bytes = fread(byte_buf, 1, sz, f);
|
||||
fclose(f);
|
||||
|
||||
SagittariusProgram* prog = sagittarius_load_program_from_byte_array(byte_buf, read_bytes);
|
||||
free(byte_buf);
|
||||
|
||||
if (!prog) {
|
||||
printf("Error: Failed to load program from '%s'\n", filepath);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Create VM with 1 MB memory
|
||||
SagittariusVM* vm = sagittarius_vm_new(1024 * 1024);
|
||||
vm->panic_handler = my_panic_handler;
|
||||
|
||||
// Register print syscall (namespace 0, function 0 -> ID 0)
|
||||
sagittarius_register_syscall(vm, 0, syscall_print);
|
||||
|
||||
// Load program at offset 0
|
||||
sagittarius_load_program_to_mem(vm, prog, 0);
|
||||
|
||||
// Run VM
|
||||
while (1) {
|
||||
if (vm->core.pc >= vm->Memory.size) {
|
||||
printf("Error: PC out of bounds!\n");
|
||||
break;
|
||||
}
|
||||
|
||||
SagittariusInst inst;
|
||||
memcpy(&inst, &vm->Memory.data[vm->core.pc], sizeof(SagittariusInst));
|
||||
|
||||
uint8_t opcode = inst.data & 0xFF;
|
||||
if (opcode == 14) { // halt opcode
|
||||
break;
|
||||
}
|
||||
|
||||
sagittarius_step(vm);
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
sagittarius_vm_free(vm);
|
||||
free(prog->instructions);
|
||||
if (prog->data) free(prog->data);
|
||||
free(prog);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user