aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/cpu/opcodes
diff options
context:
space:
mode:
authorJames Barnett <noreply@jamesbarnett.xyz>2018-07-08 19:36:44 +0100
committerJames Barnett <noreply@jamesbarnett.xyz>2018-07-08 19:36:44 +0100
commit1193b3a6707e0a6a5626d8a3238252dd2eeaa31b (patch)
tree20eb013c55434e2265655e9f2226cadbeeae3bdb /src/main/kotlin/cpu/opcodes
parent748471a5310f2413afc26c00b282ade64e718a3e (diff)
downloadKGB-1193b3a6707e0a6a5626d8a3238252dd2eeaa31b.tar.xz
KGB-1193b3a6707e0a6a5626d8a3238252dd2eeaa31b.zip
Add 'misc' ops (SWAP, DAA, CPL, CCF, SCF etc.)
Diffstat (limited to 'src/main/kotlin/cpu/opcodes')
-rw-r--r--src/main/kotlin/cpu/opcodes/Misc.kt61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/main/kotlin/cpu/opcodes/Misc.kt b/src/main/kotlin/cpu/opcodes/Misc.kt
new file mode 100644
index 0000000..2b8f673
--- /dev/null
+++ b/src/main/kotlin/cpu/opcodes/Misc.kt
@@ -0,0 +1,61 @@
+package cpu.opcodes
+
+import cpu.Operation
+import cpu.Registers
+import cpu.Registers.Flag
+import BitManipulation as bm
+
+var misc = mapOf(
+
+ 0x37 to Operation("SWAP A", 0, 8, {r, _, _ -> r.A = swap(r.A, r)}),
+ 0x30 to Operation("SWAP B", 0, 8, {r, _, _ -> r.B = swap(r.B, r)}),
+ 0x31 to Operation("SWAP C", 0, 8, {r, _, _ -> r.C = swap(r.C, r)}),
+ 0x32 to Operation("SWAP D", 0, 8, {r, _, _ -> r.D = swap(r.D, r)}),
+ 0x33 to Operation("SWAP E", 0, 8, {r, _, _ -> r.E = swap(r.E, r)}),
+ 0x34 to Operation("SWAP H", 0, 8, {r, _, _ -> r.H = swap(r.H, r)}),
+ 0x35 to Operation("SWAP L", 0, 8, {r, _, _ -> r.L = swap(r.L, r)}),
+ 0x36 to Operation("SWAP (HL)", 0, 16, {r, m, _ -> m.writeByte(r.HL, swap(m.readByte(r.HL), r))}),
+
+ 0x27 to Operation("DAA", 0, 4, {r, _, _ ->
+ // TODO
+ }),
+
+ 0x2F to Operation("CPL", 0, 4, {r, _, _ ->
+ r.A = r.A.inv() and 0xFF
+ r.setFlag(Flag.SUBTRACT)
+ r.setFlag(Flag.HALF_CARRY)
+ }),
+
+ 0x3F to Operation("CCF", 0, 4, {r, _, _ ->
+ r.setFlagFromBool(Flag.CARRY, r.getFlag(Flag.CARRY) == 0)
+ r.clearFlag(Flag.SUBTRACT)
+ r.clearFlag(Flag.HALF_CARRY)
+ }),
+
+ 0x37 to Operation("SCF", 0, 4, {r, _, _ ->
+ r.clearFlag(Flag.SUBTRACT)
+ r.clearFlag(Flag.HALF_CARRY)
+ r.setFlag(Flag.CARRY)
+ }),
+
+ 0x00 to Operation("NOP", 0, 4, {_, _, _ -> }),
+
+ // TODO - interrupt related
+ 0x76 to Operation("HALT", 0, 4, {_, _, _ -> }),
+ 0x10 to Operation("STOP", 0, 4, {_, _, _ -> }),
+ 0xD3 to Operation("DI", 0, 4, {_, _, _ -> }),
+ 0xFB to Operation("EI", 0, 4, {_, _, _ -> })
+
+)
+
+
+private fun swap(n: Int, r: Registers): Int {
+
+ val topNibble = n and 0xF0
+ val bottomNibble = n and 0x0F
+ val result = (bottomNibble shl 4) or (topNibble shr 4)
+ r.clearFlags()
+ r.setFlagFromBool(Flag.ZERO, result == 0)
+
+ return result
+}