aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/cpu/opcodes/Calls.kt
diff options
context:
space:
mode:
authorJames Barnett <noreply@jamesbarnett.xyz>2018-07-16 21:57:53 +0100
committerJames Barnett <noreply@jamesbarnett.xyz>2018-07-16 21:57:53 +0100
commita6e35c04ce8a37e78702997977c95e1d1bafb932 (patch)
tree5e1618eaa805ffe1dc686d67406fe3a389a2a589 /src/main/kotlin/cpu/opcodes/Calls.kt
parentae3bb9819aad653d6a2ed972000e3c3696e39bf1 (diff)
downloadKGB-a6e35c04ce8a37e78702997977c95e1d1bafb932.tar.xz
KGB-a6e35c04ce8a37e78702997977c95e1d1bafb932.zip
Add CALLs
Diffstat (limited to 'src/main/kotlin/cpu/opcodes/Calls.kt')
-rw-r--r--src/main/kotlin/cpu/opcodes/Calls.kt28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/main/kotlin/cpu/opcodes/Calls.kt b/src/main/kotlin/cpu/opcodes/Calls.kt
new file mode 100644
index 0000000..697b56c
--- /dev/null
+++ b/src/main/kotlin/cpu/opcodes/Calls.kt
@@ -0,0 +1,28 @@
+package cpu.opcodes
+
+import cpu.Operation
+import cpu.Registers
+import cpu.Registers.Flag
+import ram.Ram
+import BitManipulation as bm
+
+val calls = mapOf(
+
+ 0xCD to Operation("CALL nn", 2, 12, {r, m, a -> call(bm.argsToWord(a), r, m)}),
+
+ 0xC4 to Operation("CALL NZ,nn", 2, 12, {r, m, a -> if(r.getFlag(Flag.ZERO) == 0) call(bm.argsToWord(a), r, m)}),
+ 0xCC to Operation("CALL Z,nn", 2, 12, {r, m, a -> if(r.getFlag(Flag.ZERO) == 1) call(bm.argsToWord(a), r, m)}),
+ 0xD4 to Operation("CALL NC,nn", 2, 12, {r, m, a -> if(r.getFlag(Flag.CARRY) == 0) call(bm.argsToWord(a), r, m)}),
+ 0xDC to Operation("CALL C,nn", 2, 12, {r, m, a -> if(r.getFlag(Flag.CARRY) == 1) call(bm.argsToWord(a), r, m)})
+
+)
+
+private fun call(word: Int, r: Registers, m: Ram) {
+
+ val nextAddress = (r.PC + 3) and 0xFFFF
+
+ m.writeByte(r.decrementAndGetSP(), bm.getMsb(nextAddress))
+ m.writeByte(r.decrementAndGetSP(), bm.getLsb(nextAddress))
+
+ r.PC = word
+} \ No newline at end of file