fix interrupt

This commit is contained in:
sfja 2026-11-04 11:56:57 +01:00
parent 58dd27253e
commit a2671e053f

View File

@ -67,20 +67,78 @@ static const char* op_reg_str(uint16_t reg)
return strs[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) void op_print(const Op* op)
{ {
printf("%s", opty_str(op->ty)); printf("\033[1;31m%s", opty_str(op->ty));
printf(" %s", op_reg_str(op->op1_reg));
if (op->use_imm) { switch (op->ty) {
printf(" %d", op->op2_reg_or_imm);
} else { case Op_Hlt:
printf(" %s", op_reg_str(op->op2_reg_or_imm)); 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"); printf("\033[0m\n");
if (op->is_rel) }
printf(" rel");
printf("\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 { 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* fl = &s->regs[Reg_Fl];
uint16_t* pc = &s->regs[Reg_Pc]; uint16_t* pc = &s->regs[Reg_Pc];
printf("[%04x] ", *pc); print_op_line(op, *pc);
op_print(op);
switch (op->ty) { switch (op->ty) {
case Op_Nop: case Op_Nop:
@ -409,8 +466,17 @@ void vm_run(VM* vm)
bool is_running = true; bool is_running = true;
bool wait_next_iter = false;
while (is_running) { while (is_running) {
mtx_lock(&vm->mtx); 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; VMState s = vm->state;
uint16_t* pc = &s.regs[Reg_Pc]; uint16_t* pc = &s.regs[Reg_Pc];
@ -444,7 +510,7 @@ void vm_run(VM* vm)
*pc += op_size * 2; *pc += op_size * 2;
if (op.ty == Op_Hlt) { if (op.ty == Op_Hlt) {
cnd_wait(&vm->cnd, &vm->mtx); wait_next_iter = true;
break; 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) { for (int ch_x = 0; ch_x < 8; ++ch_x) {
bool ch = row >> (7 - ch_x) & 1; 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'); mov_i(a, r1, 'R');
store_bi(a, 0xfb05, r1); store_bi(a, 0xfb05, r1);
mov_i(a, r0, 0x3e); mov_i(a, r0, 0x3a);
store_wi(a, 0xff04, r0); store_wi(a, 0xff04, r0);
mov_i(a, r1, 0); mov_i(a, r1, 0);
hlt(a); hlt(a);
@ -993,43 +1059,43 @@ static const char* opty_str(OpTy ty)
{ {
switch (ty) { switch (ty) {
case Op_Nop: case Op_Nop:
return "Nop"; return "nop";
case Op_Hlt: case Op_Hlt:
return "Hlt"; return "hlt";
case Op_Mov: case Op_Mov:
return "Mov"; return "mov";
case Op_Load: case Op_Load:
return "Load"; return "load";
case Op_Store: case Op_Store:
return "Store"; return "store";
case Op_Or: case Op_Or:
return "Or"; return "or";
case Op_Xor: case Op_Xor:
return "Xor"; return "xor";
case Op_And: case Op_And:
return "And"; return "and";
case Op_Not: case Op_Not:
return "Not"; return "not";
case Op_Shr: case Op_Shr:
return "Shr"; return "shr";
case Op_Shl: case Op_Shl:
return "Shl"; return "shl";
case Op_RShr: case Op_RShr:
return "RShr"; return "rshr";
case Op_RShl: case Op_RShl:
return "RShl"; return "rshl";
case Op_Add: case Op_Add:
return "Add"; return "add";
case Op_Sub: case Op_Sub:
return "Sub"; return "sub";
case Op_RSub: case Op_RSub:
return "RSub"; return "rsub";
case Op_Cmp: case Op_Cmp:
return "Cmp"; return "cmp";
case Op_Jmp: case Op_Jmp:
return "Jmp"; return "jmp";
case Op_Jnz: case Op_Jnz:
return "Jnz"; return "jnz";
} }
assert(false); assert(false);
} }