From ae3bb9819aad653d6a2ed972000e3c3696e39bf1 Mon Sep 17 00:00:00 2001 From: James Barnett Date: Mon, 16 Jul 2018 21:27:33 +0100 Subject: Implement JUMP ops --- src/main/kotlin/BitManipulation.kt | 4 ++-- src/main/kotlin/cpu/Cpu.kt | 1 + src/main/kotlin/cpu/Registers.kt | 8 ++++++++ src/main/kotlin/cpu/opcodes/Arithmetic16Bit.kt | 2 +- src/main/kotlin/cpu/opcodes/Jumps.kt | 22 ++++++++++++++++++++++ src/main/kotlin/cpu/opcodes/Loads16Bit.kt | 2 +- 6 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/cpu/opcodes/Jumps.kt (limited to 'src/main/kotlin') diff --git a/src/main/kotlin/BitManipulation.kt b/src/main/kotlin/BitManipulation.kt index b655718..430d840 100644 --- a/src/main/kotlin/BitManipulation.kt +++ b/src/main/kotlin/BitManipulation.kt @@ -28,12 +28,12 @@ object BitManipulation { } } - fun isSignedBitNegative(value: Int): Boolean { + fun isSignedByteNegative(value: Int): Boolean { return value and (1 shl 7) != 0 } fun getAbsoluteValue(value: Int ): Int{ - return if(isSignedBitNegative(value)) { + return if(isSignedByteNegative(value)) { 0x0100 - value // 1 00000000 - 1xxxxxx = value, due to 2s compliment } else { value diff --git a/src/main/kotlin/cpu/Cpu.kt b/src/main/kotlin/cpu/Cpu.kt index feb89bc..ce5ba43 100644 --- a/src/main/kotlin/cpu/Cpu.kt +++ b/src/main/kotlin/cpu/Cpu.kt @@ -18,6 +18,7 @@ class Cpu { stdCommandGroup.putAll(arithmetic16Bit) stdCommandGroup.putAll(misc) stdCommandGroup.putAll(rotates) + stdCommandGroup.putAll(jumps) standardOpcodes = stdCommandGroup.toMap() val extCommandGroup: MutableMap = mutableMapOf() diff --git a/src/main/kotlin/cpu/Registers.kt b/src/main/kotlin/cpu/Registers.kt index 54539a8..81db1b0 100644 --- a/src/main/kotlin/cpu/Registers.kt +++ b/src/main/kotlin/cpu/Registers.kt @@ -131,6 +131,14 @@ class Registers { return currentSP } + fun addSignedByteToPC(byte: Int) { + PC = if (bm.isSignedByteNegative(byte)) { + (PC - bm.getAbsoluteValue(byte)) and 0xFFFF + } else { + (PC + bm.getAbsoluteValue(byte)) and 0xFFFF + } + } + fun getFlag(flag: Flag): Int { return (F shr flag.bitPosition) and 0x01 } diff --git a/src/main/kotlin/cpu/opcodes/Arithmetic16Bit.kt b/src/main/kotlin/cpu/opcodes/Arithmetic16Bit.kt index 0c2c9cb..0eff88a 100644 --- a/src/main/kotlin/cpu/opcodes/Arithmetic16Bit.kt +++ b/src/main/kotlin/cpu/opcodes/Arithmetic16Bit.kt @@ -17,7 +17,7 @@ var arithmetic16Bit = mapOf( val absoluteValue = bm.getAbsoluteValue(a[0]) - if (bm.isSignedBitNegative(a[0])) { + if (bm.isSignedByteNegative(a[0])) { r.setFlagFromBool(Flag.HALF_CARRY, r.SP and 0x0F < absoluteValue and 0x0F) r.setFlagFromBool(Flag.CARRY,r.SP and 0xFF < absoluteValue) r.SP = r.SP - absoluteValue diff --git a/src/main/kotlin/cpu/opcodes/Jumps.kt b/src/main/kotlin/cpu/opcodes/Jumps.kt new file mode 100644 index 0000000..6e0ea3e --- /dev/null +++ b/src/main/kotlin/cpu/opcodes/Jumps.kt @@ -0,0 +1,22 @@ +package cpu.opcodes + +import cpu.Operation +import cpu.Registers.Flag +import BitManipulation as bm + +val jumps = mapOf( + + 0xC3 to Operation("JP nn", 2, 12, {r, _, a -> r.PC = bm.argsToWord(a)}), + 0xC2 to Operation("JP NZ,nn", 2, 12, {r, _, a -> if(r.getFlag(Flag.ZERO) == 0) r.PC = bm.argsToWord(a)}), + 0xCA to Operation("JP Z,nn", 2, 12, {r, _, a -> if(r.getFlag(Flag.ZERO) == 1) r.PC = bm.argsToWord(a)}), + 0xD2 to Operation("JP NC,nn", 2, 12, {r, _, a -> if(r.getFlag(Flag.CARRY) == 0) r.PC = bm.argsToWord(a)}), + 0xDA to Operation("JP C,nn", 2, 12, {r, _, a -> if(r.getFlag(Flag.CARRY) == 1) r.PC = bm.argsToWord(a)}), + 0xE9 to Operation("JP (HL)", 0, 4, {r, m, _ -> r.PC = m.readByte(r.HL)}), + + 0x18 to Operation("JR n", 1, 8, {r, _, a -> r.addSignedByteToPC(a[0])}), + 0x20 to Operation("JR NZ,n", 1, 8, {r, _, a -> if(r.getFlag(Flag.ZERO) == 0) r.addSignedByteToPC(a[0])}), + 0x28 to Operation("JR Z,n", 1, 8, {r, _, a -> if(r.getFlag(Flag.ZERO) == 1) r.addSignedByteToPC(a[0])}), + 0x30 to Operation("JR NC,n", 1, 8, {r, _, a -> if(r.getFlag(Flag.CARRY) == 0) r.addSignedByteToPC(a[0])}), + 0x38 to Operation("JR C,n", 1, 8, {r, _, a -> if(r.getFlag(Flag.CARRY) == 1) r.addSignedByteToPC(a[0])}) + +) \ No newline at end of file diff --git a/src/main/kotlin/cpu/opcodes/Loads16Bit.kt b/src/main/kotlin/cpu/opcodes/Loads16Bit.kt index 134b3a9..2f679c3 100644 --- a/src/main/kotlin/cpu/opcodes/Loads16Bit.kt +++ b/src/main/kotlin/cpu/opcodes/Loads16Bit.kt @@ -24,7 +24,7 @@ var loads16Bit = mapOf( val absoluteValue = bm.getAbsoluteValue(a[0]) - if (bm.isSignedBitNegative(a[0])) { + if (bm.isSignedByteNegative(a[0])) { r.setFlagFromBool(Flag.HALF_CARRY, r.SP and 0x0F < absoluteValue and 0x0F) r.setFlagFromBool(Flag.CARRY,r.SP and 0xFF < absoluteValue) r.HL = r.SP - absoluteValue -- cgit v1.2.3