diff --git a/src/main.c b/src/main.c index 0ade89b..4ee2632 100644 --- a/src/main.c +++ b/src/main.c @@ -67,20 +67,78 @@ static const char* op_reg_str(uint16_t reg) return strs[reg]; } +static void op_print_specs(const Op* op) +{ + if (op->is_byte) + printf(" \033[0;33mbyte"); + if (op->is_rel) + printf(" \033[0;33mrel"); +} + +static void op_print_storage_specs(const Op* op) +{ + if (op->is_byte) + printf(" \033[0;33mbyte"); + else + printf(" \033[0;33mword"); +} + +static void op_print_op1(const Op* op) +{ + printf(" \033[1;36m%s", op_reg_str(op->op1_reg)); +} + +static void op_print_op2(const Op* op) +{ + if (op->use_imm) { + printf("\033[0;35m0x%x", op->op2_reg_or_imm); + } else { + printf("\033[1;36m%s", op_reg_str(op->op2_reg_or_imm)); + } +} + void op_print(const Op* op) { - printf("%s", opty_str(op->ty)); - printf(" %s", op_reg_str(op->op1_reg)); - if (op->use_imm) { - printf(" %d", op->op2_reg_or_imm); - } else { - printf(" %s", op_reg_str(op->op2_reg_or_imm)); + printf("\033[1;31m%s", opty_str(op->ty)); + + switch (op->ty) { + + case Op_Hlt: + break; + case Op_Load: + op_print_storage_specs(op); + op_print_op1(op); + printf("\033[37m, ["); + op_print_op2(op); + printf("\033[37m]"); + break; + case Op_Store: + op_print_storage_specs(op); + printf(" \033[37m["); + op_print_op2(op); + printf("\033[37m],"); + op_print_op1(op); + break; + case Op_Jmp: + op_print_specs(op); + printf(" "); + op_print_op2(op); + break; + default: + op_print_specs(op); + op_print_op1(op); + printf("\033[37m, "); + op_print_op2(op); + break; } - if (op->is_byte) - printf(" byte"); - if (op->is_rel) - printf(" rel"); - printf("\n"); + + printf("\033[0m\n"); +} + +void print_op_line(const Op* op, uint16_t pc) +{ + printf("\033[37m[\033[35m%04x\033[37m] ", pc); + op_print(op); } typedef enum : uint16_t { @@ -313,8 +371,7 @@ static int vm_run_op(VM* vm, VMState* s, const Op* op) uint16_t* fl = &s->regs[Reg_Fl]; uint16_t* pc = &s->regs[Reg_Pc]; - printf("[%04x] ", *pc); - op_print(op); + print_op_line(op, *pc); switch (op->ty) { case Op_Nop: @@ -409,8 +466,17 @@ void vm_run(VM* vm) bool is_running = true; + bool wait_next_iter = false; + while (is_running) { mtx_lock(&vm->mtx); + + if (wait_next_iter) { + printf("=== halting ===\n"); + cnd_wait(&vm->cnd, &vm->mtx); + } + wait_next_iter = false; + VMState s = vm->state; uint16_t* pc = &s.regs[Reg_Pc]; @@ -444,7 +510,7 @@ void vm_run(VM* vm) *pc += op_size * 2; if (op.ty == Op_Hlt) { - cnd_wait(&vm->cnd, &vm->mtx); + wait_next_iter = true; break; } } @@ -520,7 +586,7 @@ static void vcd_render_char(Vcd* vcd, uint8_t ch, int x, int y) for (int ch_x = 0; ch_x < 8; ++ch_x) { bool ch = row >> (7 - ch_x) & 1; - buffer[(y + ch_y) * 40 * 8 + x * 8 + ch_x] = ch ? fg : bg; + buffer[(y * 8 + ch_y) * 40 * 8 + x * 8 + ch_x] = ch ? fg : bg; } } @@ -860,7 +926,7 @@ void assemble(uint16_t program[], size_t program_size) mov_i(a, r1, 'R'); store_bi(a, 0xfb05, r1); - mov_i(a, r0, 0x3e); + mov_i(a, r0, 0x3a); store_wi(a, 0xff04, r0); mov_i(a, r1, 0); hlt(a); @@ -993,43 +1059,43 @@ static const char* opty_str(OpTy ty) { switch (ty) { case Op_Nop: - return "Nop"; + return "nop"; case Op_Hlt: - return "Hlt"; + return "hlt"; case Op_Mov: - return "Mov"; + return "mov"; case Op_Load: - return "Load"; + return "load"; case Op_Store: - return "Store"; + return "store"; case Op_Or: - return "Or"; + return "or"; case Op_Xor: - return "Xor"; + return "xor"; case Op_And: - return "And"; + return "and"; case Op_Not: - return "Not"; + return "not"; case Op_Shr: - return "Shr"; + return "shr"; case Op_Shl: - return "Shl"; + return "shl"; case Op_RShr: - return "RShr"; + return "rshr"; case Op_RShl: - return "RShl"; + return "rshl"; case Op_Add: - return "Add"; + return "add"; case Op_Sub: - return "Sub"; + return "sub"; case Op_RSub: - return "RSub"; + return "rsub"; case Op_Cmp: - return "Cmp"; + return "cmp"; case Op_Jmp: - return "Jmp"; + return "jmp"; case Op_Jnz: - return "Jnz"; + return "jnz"; } assert(false); }