aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/runtime.c107
-rw-r--r--src/runtime.h10
2 files changed, 116 insertions, 1 deletions
diff --git a/src/runtime.c b/src/runtime.c
index cb4ed99..b744530 100644
--- a/src/runtime.c
+++ b/src/runtime.c
@@ -10,6 +10,7 @@
* Description: Virtual machine implementation
*/
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -70,6 +71,112 @@ void vm_load_program(vm_t *vm, inst_t *instructions, size_t size)
vm->program.ptr = 0;
}
+void vm_print_registers(vm_t *vm, FILE *fp)
+{
+ struct Registers reg = vm->registers;
+ fprintf(fp, "Registers.ret = %lX\n", reg.ret);
+ fprintf(fp, "Registers.b = [");
+ for (size_t i = 0; i < VM_BYTE_REGISTERS; ++i)
+ {
+ fprintf(fp, "{%lu:%X}", i, reg.b[i]);
+ if (i != VM_BYTE_REGISTERS - 1)
+ fprintf(fp, ", ");
+ }
+ fprintf(fp, "]\n");
+ fprintf(fp, "Registers.w = [");
+ for (size_t i = 0; i < VM_WORD_REGISTERS; ++i)
+ {
+ fprintf(fp, "{%lu:%lX}", i, reg.w[i]);
+ if (i != VM_WORD_REGISTERS - 1)
+ fprintf(fp, reg.w[i] == 0 ? ", " : ",\n");
+ }
+ fprintf(fp, "]\n");
+ fprintf(fp, "Registers.f = [");
+ for (size_t i = 0; i < VM_FLOAT_REGISTERS; ++i)
+ {
+ fprintf(fp, "{%lu:%f}", i, reg.f[i]);
+ if (i != VM_FLOAT_REGISTERS - 1)
+ fprintf(fp, reg.f[i] == 0 ? ", " : ",\n");
+ }
+ fprintf(fp, "]\n");
+}
+
+void vm_print_stack(vm_t *vm, FILE *fp)
+{
+ struct Stack stack = vm->stack;
+ fprintf(fp, "Stack.max = %lu\nStack.ptr = %lu\nStack.data = [", stack.max,
+ stack.ptr);
+ if (stack.ptr == 0)
+ {
+ fprintf(fp, "]\n");
+ return;
+ }
+ for (size_t i = stack.ptr; i > 0; --i)
+ {
+ byte b = stack.data[i - 1];
+ fprintf(fp, "{%lu: %X}", stack.ptr - i, b);
+ if (i != 1)
+ fprintf(fp, ", ");
+
+ if (((stack.ptr - i + 1) % 4) == 0)
+ fprintf(fp, "\n");
+ }
+ fprintf(fp, "]\n");
+}
+
+void vm_print_program(vm_t *vm, FILE *fp)
+{
+ struct Program program = vm->program;
+ fprintf(fp, "Program.max = %lu\nProgram.ptr = %lu\nProgram.instructions = [",
+ program.max, program.ptr);
+ if (program.ptr == 0)
+ {
+ fprintf(fp, "]\n");
+ return;
+ }
+
+ fprintf(fp, "\n");
+ size_t beg = 0;
+ if (program.ptr >= VM_PRINT_PROGRAM_EXCERPT)
+ {
+ fprintf(fp, "\t...\n");
+ beg = program.ptr - VM_PRINT_PROGRAM_EXCERPT;
+ }
+ else
+ beg = 0;
+ size_t end = MIN(program.ptr + VM_PRINT_PROGRAM_EXCERPT, program.max);
+ for (size_t i = beg; i < end; ++i)
+ {
+ fprintf(fp, "\t%lu: ", i);
+ inst_print(program.instructions[i], stdout);
+ if (i == program.ptr)
+ fprintf(fp, " <---");
+ fprintf(fp, "\n");
+ }
+ if (end != program.max)
+ fprintf(fp, "\t...\n");
+ fprintf(fp, "]\n");
+}
+
+void vm_print_all(vm_t *vm, FILE *fp)
+{
+ fputs("----------------------------------------------------------------------"
+ "----------\n",
+ fp);
+ vm_print_registers(vm, fp);
+ fputs("----------------------------------------------------------------------"
+ "----------\n",
+ fp);
+ vm_print_stack(vm, fp);
+ fputs("----------------------------------------------------------------------"
+ "----------\n",
+ fp);
+ vm_print_program(vm, fp);
+ fputs("----------------------------------------------------------------------"
+ "----------\n",
+ fp);
+}
+
void vm_push_byte(vm_t *vm, data_t b)
{
if (vm->stack.ptr >= vm->stack.max)
diff --git a/src/runtime.h b/src/runtime.h
index 5505fc0..f512992 100644
--- a/src/runtime.h
+++ b/src/runtime.h
@@ -13,6 +13,7 @@
#ifndef RUNTIME_H
#define RUNTIME_H
+#include <stdio.h>
#include <stdlib.h>
#include "./base.h"
@@ -42,10 +43,17 @@ typedef struct
} program;
} vm_t;
+void vm_execute(vm_t *);
+
void vm_load_stack(vm_t *, byte *, size_t);
void vm_load_program(vm_t *, inst_t *, size_t);
-void vm_execute(vm_t *);
+// Print routines
+void vm_print_registers(vm_t *, FILE *);
+void vm_print_stack(vm_t *, FILE *);
+#define VM_PRINT_PROGRAM_EXCERPT 5
+void vm_print_program(vm_t *, FILE *);
+void vm_print_all(vm_t *, FILE *);
void vm_push_byte(vm_t *, data_t);
void vm_push_word(vm_t *, data_t);