From 1d9de64e5f6ef60de43ce1d867d8830aebd88a2d Mon Sep 17 00:00:00 2001 From: jn Date: Fri, 27 Jan 2023 01:38:01 +0100 Subject: [PATCH 1/3] Print IP and SP in debug mode --- src/cpu.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cpu.c b/src/cpu.c index edda79e..46c8c65 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -880,19 +880,19 @@ static void vm_skipparam(vm_t *vm, uint32_t size, uint8_t prtype) { break; \ } -static void vm_debug(vm_t *vm, asm_instr_t instr, uint32_t address) { +static void vm_debug(vm_t *vm, asm_instr_t instr, uint32_t ip, uint32_t sp) { const asm_iinfo_t *iinfo = asm_iinfo_get(instr.opcode); uint32_t params_size = asm_disas_paramssize(instr, iinfo); uint8_t *params_data = NULL; if (params_size > 0) { - params_data = vm_findmemory(vm, address + SIZE16, params_size, false); + params_data = vm_findmemory(vm, ip + SIZE16, params_size, false); } char buffer[128] = {}; asm_disas_print(instr, iinfo, params_data, buffer); - printf("%08X %s\n", address, buffer); + printf("SP=%08X IP=%08X %s\n", sp, ip, buffer); } static void vm_execute(vm_t *vm) { @@ -903,7 +903,7 @@ static void vm_execute(vm_t *vm) { vm->pointer_instr_mut = instr_base + SIZE16; - if (vm->debug) vm_debug(vm, instr, instr_base); + if (vm->debug) vm_debug(vm, instr, instr_base, vm->pointer_stack); switch (instr.opcode) { case OP(SZ_BYTE, OP_NOP): From 6ddf60fba66f09fbf4393f28575d46c809d8bd7c Mon Sep 17 00:00:00 2001 From: jn Date: Fri, 27 Jan 2023 01:40:12 +0100 Subject: [PATCH 2/3] Handle division by zero instead of crashing --- src/cpu.c | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/cpu.c b/src/cpu.c index 46c8c65..6d0bb6d 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -861,6 +861,20 @@ static void vm_skipparam(vm_t *vm, uint32_t size, uint8_t prtype) { break; \ } +#define VM_IMPL_DIV(_size, _type, _type_target, _vm_source, _vm_source_stay, _vm_target, _oper) { \ + VM_PRELUDE_2(_size); \ + _type a = (_type) _vm_source(vm, instr.source); \ + _type b = (_type) _vm_source_stay(vm, instr.target); \ + if (a == 0) { \ + vm_panic(vm, FOX32_ERR_DIVZERO); \ + break; \ + } \ + _type x = _oper(b, a); \ + vm->flag_zero = x == 0; \ + _vm_target(vm, instr.target, (_type_target) x); \ + break; \ +} + #define VM_IMPL_CMP(_size, _type, _vm_source) { \ VM_PRELUDE_0(); \ _type a = _vm_source(vm, instr.source); \ @@ -1020,18 +1034,18 @@ static void vm_execute(vm_t *vm) { case OP(SZ_HALF, OP_IMUL): VM_IMPL_ADD(SIZE16, int16_t, uint16_t, vm_source16, vm_source16_stay, vm_target16, CHECKED_MUL); case OP(SZ_WORD, OP_IMUL): VM_IMPL_ADD(SIZE32, int32_t, uint32_t, vm_source32, vm_source32_stay, vm_target32, CHECKED_MUL); - case OP(SZ_BYTE, OP_DIV): VM_IMPL_AND(SIZE8, uint8_t, uint8_t, vm_source8, vm_source8_stay, vm_target8, OPER_DIV); - case OP(SZ_HALF, OP_DIV): VM_IMPL_AND(SIZE16, uint16_t, uint16_t, vm_source16, vm_source16_stay, vm_target16, OPER_DIV); - case OP(SZ_WORD, OP_DIV): VM_IMPL_AND(SIZE32, uint32_t, uint32_t, vm_source32, vm_source32_stay, vm_target32, OPER_DIV); - case OP(SZ_BYTE, OP_REM): VM_IMPL_AND(SIZE8, uint8_t, uint8_t, vm_source8, vm_source8_stay, vm_target8, OPER_REM); - case OP(SZ_HALF, OP_REM): VM_IMPL_AND(SIZE16, uint16_t, uint16_t, vm_source16, vm_source16_stay, vm_target16, OPER_REM); - case OP(SZ_WORD, OP_REM): VM_IMPL_AND(SIZE32, uint32_t, uint32_t, vm_source32, vm_source32_stay, vm_target32, OPER_REM); - case OP(SZ_BYTE, OP_IDIV): VM_IMPL_AND(SIZE8, int8_t, uint8_t, vm_source8, vm_source8_stay, vm_target8, OPER_DIV); - case OP(SZ_HALF, OP_IDIV): VM_IMPL_AND(SIZE16, int16_t, uint16_t, vm_source16, vm_source16_stay, vm_target16, OPER_DIV); - case OP(SZ_WORD, OP_IDIV): VM_IMPL_AND(SIZE32, int32_t, uint32_t, vm_source32, vm_source32_stay, vm_target32, OPER_DIV); - case OP(SZ_BYTE, OP_IREM): VM_IMPL_AND(SIZE8, int8_t, uint8_t, vm_source8, vm_source8_stay, vm_target8, OPER_REM); - case OP(SZ_HALF, OP_IREM): VM_IMPL_AND(SIZE16, int16_t, uint16_t, vm_source16, vm_source16_stay, vm_target16, OPER_REM); - case OP(SZ_WORD, OP_IREM): VM_IMPL_AND(SIZE32, int32_t, uint32_t, vm_source32, vm_source32_stay, vm_target32, OPER_REM); + case OP(SZ_BYTE, OP_DIV): VM_IMPL_DIV(SIZE8, uint8_t, uint8_t, vm_source8, vm_source8_stay, vm_target8, OPER_DIV); + case OP(SZ_HALF, OP_DIV): VM_IMPL_DIV(SIZE16, uint16_t, uint16_t, vm_source16, vm_source16_stay, vm_target16, OPER_DIV); + case OP(SZ_WORD, OP_DIV): VM_IMPL_DIV(SIZE32, uint32_t, uint32_t, vm_source32, vm_source32_stay, vm_target32, OPER_DIV); + case OP(SZ_BYTE, OP_REM): VM_IMPL_DIV(SIZE8, uint8_t, uint8_t, vm_source8, vm_source8_stay, vm_target8, OPER_REM); + case OP(SZ_HALF, OP_REM): VM_IMPL_DIV(SIZE16, uint16_t, uint16_t, vm_source16, vm_source16_stay, vm_target16, OPER_REM); + case OP(SZ_WORD, OP_REM): VM_IMPL_DIV(SIZE32, uint32_t, uint32_t, vm_source32, vm_source32_stay, vm_target32, OPER_REM); + case OP(SZ_BYTE, OP_IDIV): VM_IMPL_DIV(SIZE8, int8_t, uint8_t, vm_source8, vm_source8_stay, vm_target8, OPER_DIV); + case OP(SZ_HALF, OP_IDIV): VM_IMPL_DIV(SIZE16, int16_t, uint16_t, vm_source16, vm_source16_stay, vm_target16, OPER_DIV); + case OP(SZ_WORD, OP_IDIV): VM_IMPL_DIV(SIZE32, int32_t, uint32_t, vm_source32, vm_source32_stay, vm_target32, OPER_DIV); + case OP(SZ_BYTE, OP_IREM): VM_IMPL_DIV(SIZE8, int8_t, uint8_t, vm_source8, vm_source8_stay, vm_target8, OPER_REM); + case OP(SZ_HALF, OP_IREM): VM_IMPL_DIV(SIZE16, int16_t, uint16_t, vm_source16, vm_source16_stay, vm_target16, OPER_REM); + case OP(SZ_WORD, OP_IREM): VM_IMPL_DIV(SIZE32, int32_t, uint32_t, vm_source32, vm_source32_stay, vm_target32, OPER_REM); case OP(SZ_BYTE, OP_AND): VM_IMPL_AND(SIZE8, uint8_t, uint8_t, vm_source8, vm_source8_stay, vm_target8, OPER_AND); case OP(SZ_HALF, OP_AND): VM_IMPL_AND(SIZE16, uint16_t, uint16_t, vm_source16, vm_source16_stay, vm_target16, OPER_AND); From 4758c451713073be3d85ecfb62f6ba515dd1a31f Mon Sep 17 00:00:00 2001 From: jn Date: Fri, 27 Jan 2023 17:37:06 +0100 Subject: [PATCH 3/3] Fix skipping of CMP instructions --- src/cpu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cpu.c b/src/cpu.c index 6d0bb6d..ee11bf9 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -876,7 +876,7 @@ static void vm_skipparam(vm_t *vm, uint32_t size, uint8_t prtype) { } #define VM_IMPL_CMP(_size, _type, _vm_source) { \ - VM_PRELUDE_0(); \ + VM_PRELUDE_2(_size); \ _type a = _vm_source(vm, instr.source); \ _type b = _vm_source(vm, instr.target); \ _type x; \