fixed some bugs and added VMState_InternalError

This commit is contained in:
2024-11-16 23:09:33 +05:00
parent bc54f34a4d
commit a073d0ebd9
4 changed files with 32 additions and 18 deletions

View File

@@ -20,6 +20,7 @@ void _VM_setError(VM* vm, const char* context, const char* format, ...){
strcpy(buf, "SPRINTF FAILED");
}
vm->error_message = buf;
vm->state = VMState_InternalError;
}
bool VM_loadProgram(VM* vm, u8* data, size_t size){
@@ -43,22 +44,26 @@ i32 VM_executeProgram(VM* vm){
return -1;
}
vm->state = VMState_Executing;
vm->current_pos = 0;
while (vm->current_pos < vm->data_size){
u8 opcode = vm->data[vm->current_pos];
const Instruction* instr = Instruction_getFromOpcode(opcode);
// printfe("[at 0x%x] %02X %s\n", (u32)vm->current_pos, opcode, instr->name);
if(instr == NULL){
VM_setError(vm, "unknown opcode %02X", opcode);
return -1;
}
vm->current_pos++;
i32 bytes_read = instr->implementation(vm);
// internal error occured
if(bytes_read < 0)
return -1;
vm->current_pos += bytes_read;
if(vm->state == VMState_Exited)
break;
}
if(vm->state != VMState_Exited){