diff --git a/src/core/cpu/arm/data_processing.zig b/src/core/cpu/arm/data_processing.zig index 7bc3f44..2e92b38 100644 --- a/src/core/cpu/arm/data_processing.zig +++ b/src/core/cpu/arm/data_processing.zig @@ -24,7 +24,7 @@ pub fn dataProcessing(comptime I: bool, comptime S: bool, comptime kind: u4) Ins if (!I and opcode >> 4 & 1 == 1) cpu.r[15] -= 4; var result: u32 = undefined; - var overflow: bool = undefined; + var overflow: u1 = undefined; // Perform Data Processing Logic switch (kind) { @@ -62,7 +62,9 @@ pub fn dataProcessing(comptime I: bool, comptime S: bool, comptime kind: u4) Ins if (rd == 0xF) return undefinedTestBehaviour(cpu); - overflow = @addWithOverflow(u32, op1, op2, &result); + const tmp = @addWithOverflow(op1, op2); + result = tmp[0]; + overflow = tmp[1]; }, 0xC => result = op1 | op2, // ORR 0xD => result = op2, // MOV @@ -110,7 +112,7 @@ pub fn dataProcessing(comptime I: bool, comptime S: bool, comptime kind: u4) Ins // ADD, ADC Flags cpu.cpsr.n.write(result >> 31 & 1 == 1); cpu.cpsr.z.write(result == 0); - cpu.cpsr.c.write(overflow); + cpu.cpsr.c.write(overflow == 0b1); cpu.cpsr.v.write(((op1 ^ result) & (op2 ^ result)) >> 31 & 1 == 1); }, 0x6, 0x7 => if (S and rd != 0xF) { @@ -141,7 +143,7 @@ pub fn dataProcessing(comptime I: bool, comptime S: bool, comptime kind: u4) Ins cpu.cpsr.v.write(((op1 ^ result) & (~op2 ^ result)) >> 31 & 1 == 1); } else if (kind == 0xB) { // CMN specific - cpu.cpsr.c.write(overflow); + cpu.cpsr.c.write(overflow == 0b1); cpu.cpsr.v.write(((op1 ^ result) & (op2 ^ result)) >> 31 & 1 == 1); } else { // TST, TEQ specific @@ -162,19 +164,19 @@ pub fn sbc(left: u32, right: u32, old_carry: u1) u32 { return ret; } -pub fn add(overflow: *bool, left: u32, right: u32) u32 { - var ret: u32 = undefined; - overflow.* = @addWithOverflow(u32, left, right, &ret); - return ret; +pub fn add(overflow: *u1, left: u32, right: u32) u32 { + const ret = @addWithOverflow(left, right); + overflow.* = ret[1]; + + return ret[0]; } -pub fn adc(overflow: *bool, left: u32, right: u32, old_carry: u1) u32 { - var ret: u32 = undefined; - const first = @addWithOverflow(u32, left, right, &ret); - const second = @addWithOverflow(u32, ret, old_carry, &ret); +pub fn adc(overflow: *u1, left: u32, right: u32, old_carry: u1) u32 { + const tmp = @addWithOverflow(left, right); + const ret = @addWithOverflow(tmp[0], old_carry); + overflow.* = tmp[1] | ret[1]; - overflow.* = first or second; - return ret; + return ret[0]; } fn undefinedTestBehaviour(cpu: *Arm7tdmi) void { diff --git a/src/core/cpu/thumb/alu.zig b/src/core/cpu/thumb/alu.zig index 778da99..a8038c8 100644 --- a/src/core/cpu/thumb/alu.zig +++ b/src/core/cpu/thumb/alu.zig @@ -21,7 +21,8 @@ pub fn fmt4(comptime op: u4) InstrFn { const op2 = cpu.r[rs]; var result: u32 = undefined; - var overflow: bool = undefined; + var overflow: u1 = undefined; + switch (op) { 0x0 => result = op1 & op2, // AND 0x1 => result = op1 ^ op2, // EOR @@ -34,7 +35,12 @@ pub fn fmt4(comptime op: u4) InstrFn { 0x8 => result = op1 & op2, // TST 0x9 => result = 0 -% op2, // NEG 0xA => result = op1 -% op2, // CMP - 0xB => overflow = @addWithOverflow(u32, op1, op2, &result), // CMN + 0xB => { + // CMN + const tmp = @addWithOverflow(op1, op2); + result = tmp[0]; + overflow = tmp[1]; + }, 0xC => result = op1 | op2, // ORR 0xD => result = @truncate(u32, @as(u64, op2) * @as(u64, op1)), 0xE => result = op1 & ~op2, @@ -71,7 +77,7 @@ pub fn fmt4(comptime op: u4) InstrFn { // ADC, CMN cpu.cpsr.n.write(result >> 31 & 1 == 1); cpu.cpsr.z.write(result == 0); - cpu.cpsr.c.write(overflow); + cpu.cpsr.c.write(overflow == 0b1); cpu.cpsr.v.write(((op1 ^ result) & (op2 ^ result)) >> 31 & 1 == 1); }, 0x6 => { diff --git a/src/core/cpu/thumb/data_processing.zig b/src/core/cpu/thumb/data_processing.zig index b9c3337..a8a97e1 100644 --- a/src/core/cpu/thumb/data_processing.zig +++ b/src/core/cpu/thumb/data_processing.zig @@ -64,7 +64,7 @@ pub fn fmt5(comptime op: u2, comptime h1: u1, comptime h2: u1) InstrFn { const op2 = cpu.r[rs]; var result: u32 = undefined; - var overflow: bool = undefined; + var overflow: u1 = undefined; switch (op) { 0b00 => result = add(&overflow, op1, op2), // ADD 0b01 => result = op1 -% op2, // CMP @@ -126,13 +126,13 @@ pub fn fmt2(comptime I: bool, is_sub: bool, rn: u3) InstrFn { cpu.cpsr.v.write(((op1 ^ result) & (~op2 ^ result)) >> 31 & 1 == 1); } else { // ADD - var overflow: bool = undefined; + var overflow: u1 = undefined; const result = add(&overflow, op1, op2); cpu.r[rd] = result; cpu.cpsr.n.write(result >> 31 & 1 == 1); cpu.cpsr.z.write(result == 0); - cpu.cpsr.c.write(overflow); + cpu.cpsr.c.write(overflow == 0b1); cpu.cpsr.v.write(((op1 ^ result) & (op2 ^ result)) >> 31 & 1 == 1); } } @@ -145,7 +145,7 @@ pub fn fmt3(comptime op: u2, comptime rd: u3) InstrFn { const op1 = cpu.r[rd]; const op2: u32 = opcode & 0xFF; // Offset - var overflow: bool = undefined; + var overflow: u1 = undefined; const result: u32 = switch (op) { 0b00 => op2, // MOV 0b01 => op1 -% op2, // CMP @@ -169,7 +169,7 @@ pub fn fmt3(comptime op: u2, comptime rd: u3) InstrFn { }, 0b10 => { // ADD - cpu.cpsr.c.write(overflow); + cpu.cpsr.c.write(overflow == 0b1); cpu.cpsr.v.write(((op1 ^ result) & (op2 ^ result)) >> 31 & 1 == 1); }, }