aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/cpu/opcodes/Arithmetic16Bit.kt
diff options
context:
space:
mode:
authorJames Barnett <noreply@jamesbarnett.xyz>2018-07-08 17:47:02 +0100
committerJames Barnett <noreply@jamesbarnett.xyz>2018-07-08 17:47:02 +0100
commit748471a5310f2413afc26c00b282ade64e718a3e (patch)
treefda25ce5d9737cbfd3f6ce514b0485abae7de32e /src/main/kotlin/cpu/opcodes/Arithmetic16Bit.kt
parentd132a0689aede6d856b5057cdb3d8c11bc7986ba (diff)
downloadKGB-748471a5310f2413afc26c00b282ade64e718a3e.tar.xz
KGB-748471a5310f2413afc26c00b282ade64e718a3e.zip
Implement 16-Bit arithmetic ops
Diffstat (limited to 'src/main/kotlin/cpu/opcodes/Arithmetic16Bit.kt')
-rw-r--r--src/main/kotlin/cpu/opcodes/Arithmetic16Bit.kt60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/main/kotlin/cpu/opcodes/Arithmetic16Bit.kt b/src/main/kotlin/cpu/opcodes/Arithmetic16Bit.kt
new file mode 100644
index 0000000..0c2c9cb
--- /dev/null
+++ b/src/main/kotlin/cpu/opcodes/Arithmetic16Bit.kt
@@ -0,0 +1,60 @@
+package cpu.opcodes
+
+import cpu.Operation
+import cpu.Registers
+import cpu.Registers.Flag
+import BitManipulation as bm
+
+var arithmetic16Bit = mapOf(
+
+ 0x09 to Operation("ADD HL,BC", 0, 8, {r, _, _ -> r.HL = add(r.HL, r.BC, r)}),
+ 0x19 to Operation("ADD HL,DE", 0, 8, {r, _, _ -> r.HL = add(r.HL, r.DE, r)}),
+ 0x29 to Operation("ADD HL,HL", 0, 8, {r, _, _ -> r.HL = add(r.HL, r.HL, r)}),
+ 0x39 to Operation("ADD HL,SP", 0, 8, {r, _, _ -> r.HL = add(r.HL, r.SP, r)}),
+
+ 0xE8 to Operation("ADD SP,n", 1, 16, {r, _, a ->
+ r.clearFlags()
+
+ val absoluteValue = bm.getAbsoluteValue(a[0])
+
+ if (bm.isSignedBitNegative(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
+
+ } else {
+ r.setFlagFromBool(Flag.HALF_CARRY,(r.SP and 0x0F) + (absoluteValue and 0x0F) > 0x0F)
+ r.setFlagFromBool(Flag.CARRY,(r.SP and 0xFF) + absoluteValue > 0xFF)
+ r.SP = r.SP + absoluteValue
+ }
+ }),
+
+ 0x03 to Operation("INC BC", 0, 8, {r, _, _ -> r.BC = inc(r.BC)}),
+ 0x13 to Operation("INC DE", 0, 8, {r, _, _ -> r.DE = inc(r.DE)}),
+ 0x23 to Operation("INC HL", 0, 8, {r, _, _ -> r.HL = inc(r.HL)}),
+ 0x33 to Operation("INC SP", 0, 8, {r, _, _ -> r.SP = inc(r.SP)}),
+
+ 0x0B to Operation("DEC BC", 0, 8, {r, _, _ -> r.BC = dec(r.BC)}),
+ 0x1B to Operation("DEC DE", 0, 8, {r, _, _ -> r.DE = dec(r.DE)}),
+ 0x2B to Operation("DEC HL", 0, 8, {r, _, _ -> r.HL = dec(r.HL)}),
+ 0x3B to Operation("DEC SP", 0, 8, {r, _, _ -> r.SP = dec(r.SP)})
+
+)
+
+private fun add(n1: Int, n2: Int, r: Registers): Int {
+
+ r.clearFlag(Flag.SUBTRACT)
+ r.setFlagFromBool(Flag.HALF_CARRY, ((n1 and 0x0FFF) + (n2 and 0x0FFF)) > 0x0FFF)
+ r.setFlagFromBool(Flag.CARRY, (n1 + n2) > 0xFFFF)
+
+ return (n1 + n2) and 0xFFFF
+}
+
+
+private fun inc(n: Int): Int {
+ return (n + 1) and 0xFFFF
+}
+
+private fun dec(n: Int): Int {
+ return (n - 1) and 0xFFFF
+} \ No newline at end of file