From 8b7558a31bd06d815b138661be0af543cd596ec5 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Tue, 22 Jul 2025 02:43:20 -0400 Subject: oemu: cpu: Introduce decoding logic for DIV Signed-off-by: Ian Moffett --- usr.bin/oemu/cpu.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'usr.bin/oemu') diff --git a/usr.bin/oemu/cpu.c b/usr.bin/oemu/cpu.c index ccac0b1..5689953 100644 --- a/usr.bin/oemu/cpu.c +++ b/usr.bin/oemu/cpu.c @@ -168,6 +168,35 @@ cpu_mul(struct oemu_cpu *cpu, inst_t *inst) imm, inst->imm, inst->rd, regs->xreg[inst->rd]); } +/* + * Decode the INST_DIV instruction + * + * @cpu: CPU that is executing + * @inst: Instruction dword + */ +static void +cpu_div(struct oemu_cpu *cpu, inst_t *inst) +{ + struct cpu_regs *regs = &cpu->regs; + imm_t imm; + + if (inst->rd > NELEM(regs->xreg)) { + printf("bad register operand for 'div'\n"); + return; + } + + imm = regs->xreg[inst->rd]; + if (imm == 0) { + /* TODO: Some sort of interrupt */ + printf("** DIVIDE BY ZERO **\n"); + return; + } + + regs->xreg[inst->rd] /= inst->imm; + printf("%d / %d -> X%d, new=%d\n", + imm, inst->imm, inst->rd, regs->xreg[inst->rd]); +} + /* * Reset a CPU to a default state */ @@ -213,6 +242,9 @@ cpu_kick(struct oemu_cpu *cpu, struct sysmem *mem) case INST_MUL: cpu_mul(cpu, inst); break; + case INST_DIV: + cpu_div(cpu, inst); + break; } /* Is this a halt instruction? */ -- cgit v1.2.3