diff options
Diffstat (limited to 'emulator/hart.c')
-rw-r--r-- | emulator/hart.c | 65 |
1 files changed, 63 insertions, 2 deletions
diff --git a/emulator/hart.c b/emulator/hart.c index 101d77f..297148b 100644 --- a/emulator/hart.c +++ b/emulator/hart.c @@ -220,7 +220,7 @@ static void execute_op(Hart* hart, uint32_t instruction) }
else if (bank == 1)
{
- if ((inst.funct3 & 0x8) == 0)
+ if ((inst.funct3 & 0x4) == 0)
{
uint64_t a = hart->regs[inst.rs1];
uint64_t b = hart->regs[inst.rs2];
@@ -241,7 +241,68 @@ static void execute_op(Hart* hart, uint32_t instruction) }
else
{
- assert(!"Unhandled M OP");
+ uint32_t a = hart->regs[inst.rs1];
+ uint32_t b = hart->regs[inst.rs2];
+
+ switch (inst.funct3 & 0x3)
+ {
+ case 0: // DIV
+ {
+ if (b == 0)
+ {
+ hart->regs[inst.rd] = 0xFFFFFFFF;
+ }
+ else if (a == 0x80000000 && b == 0xFFFFFFFF)
+ {
+ hart->regs[inst.rd] = 0x80000000;
+ }
+ else
+ {
+ hart->regs[inst.rd] = (uint32_t)((int32_t)a / (int32_t)b);
+ }
+ break;
+ }
+ case 1: // DIVU
+ {
+ if (b == 0)
+ {
+ hart->regs[inst.rd] = 0xFFFFFFFF;
+ }
+ else
+ {
+ hart->regs[inst.rd] = a / b;
+ }
+ break;
+ }
+ case 2: // REM
+ {
+ if (b == 0)
+ {
+ hart->regs[inst.rd] = a;
+ }
+ else if (a == 0x80000000 && b == 0xFFFFFFFF)
+ {
+ hart->regs[inst.rd] = 0;
+ }
+ else
+ {
+ hart->regs[inst.rd] = (uint32_t)((int32_t)a % (int32_t)b);
+ }
+ break;
+ }
+ case 3: // REMU
+ {
+ if (b == 0)
+ {
+ hart->regs[inst.rd] = a;
+ }
+ else
+ {
+ hart->regs[inst.rd] = a % b;
+ }
+ break;
+ }
+ }
}
}
else
|