aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/cpu
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/cpu')
-rw-r--r--src/main/kotlin/cpu/Cpu.kt2
-rw-r--r--src/main/kotlin/cpu/opcodes/Arithmetic16Bit.kt60
-rw-r--r--src/main/kotlin/cpu/opcodes/Arithmetic8Bit.kt20
-rw-r--r--src/main/kotlin/cpu/opcodes/Loads16Bit.kt2
4 files changed, 73 insertions, 11 deletions
diff --git a/src/main/kotlin/cpu/Cpu.kt b/src/main/kotlin/cpu/Cpu.kt
index 53f1a8d..77e26a1 100644
--- a/src/main/kotlin/cpu/Cpu.kt
+++ b/src/main/kotlin/cpu/Cpu.kt
@@ -1,5 +1,6 @@
package cpu
+import cpu.opcodes.arithmetic16Bit
import cpu.opcodes.arithmetic8Bit
import cpu.opcodes.loads16Bit
import cpu.opcodes.loads8Bit
@@ -16,6 +17,7 @@ class Cpu {
commandGroups.putAll(loads8Bit)
commandGroups.putAll(loads16Bit)
commandGroups.putAll(arithmetic8Bit)
+ commandGroups.putAll(arithmetic16Bit)
opcodes = commandGroups.toMap()
}
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
diff --git a/src/main/kotlin/cpu/opcodes/Arithmetic8Bit.kt b/src/main/kotlin/cpu/opcodes/Arithmetic8Bit.kt
index 99d896e..6addc95 100644
--- a/src/main/kotlin/cpu/opcodes/Arithmetic8Bit.kt
+++ b/src/main/kotlin/cpu/opcodes/Arithmetic8Bit.kt
@@ -108,7 +108,7 @@ var arithmetic8Bit = mapOf(
0x35 to Operation("DEC (HL)", 0, 12, {r, m, _ -> m.writeByte(r.HL, dec(m.readByte(r.HL), r))})
)
-fun add(n1: Int, n2: Int, r: Registers): Int {
+private fun add(n1: Int, n2: Int, r: Registers): Int {
val result = maskedAdd(n1, n2)
@@ -120,7 +120,7 @@ fun add(n1: Int, n2: Int, r: Registers): Int {
return result
}
-fun addWithCarry(n1: Int, n2: Int, r: Registers): Int {
+private fun addWithCarry(n1: Int, n2: Int, r: Registers): Int {
val carry = r.getFlag(Flag.CARRY)
val result = maskedAdd(n1, n2, carry)
@@ -133,11 +133,11 @@ fun addWithCarry(n1: Int, n2: Int, r: Registers): Int {
return result
}
-fun maskedAdd(vararg n: Int): Int {
+private fun maskedAdd(vararg n: Int): Int {
return (n.sum()) and 0xFF
}
-fun subtract(n1: Int, n2: Int, r: Registers): Int {
+private fun subtract(n1: Int, n2: Int, r: Registers): Int {
r.setFlag(Flag.SUBTRACT)
r.setFlagFromBool(Flag.ZERO, (n1 - n2) and 0xFF == 0)
@@ -147,7 +147,7 @@ fun subtract(n1: Int, n2: Int, r: Registers): Int {
return (n1 - n2) % 0xFF
}
-fun subtractWithCarry(n1: Int, n2: Int, r: Registers): Int {
+private fun subtractWithCarry(n1: Int, n2: Int, r: Registers): Int {
val carry = r.getFlag(Flag.CARRY)
@@ -159,7 +159,7 @@ fun subtractWithCarry(n1: Int, n2: Int, r: Registers): Int {
return (n1 - n2 - carry) % 0xFF
}
-fun and(n1: Int, n2: Int, r: Registers): Int {
+private fun and(n1: Int, n2: Int, r: Registers): Int {
val result = n1 and n2
@@ -171,7 +171,7 @@ fun and(n1: Int, n2: Int, r: Registers): Int {
return result
}
-fun or(n1: Int, n2: Int, r: Registers): Int {
+private fun or(n1: Int, n2: Int, r: Registers): Int {
val result = n1 or n2
@@ -183,7 +183,7 @@ fun or(n1: Int, n2: Int, r: Registers): Int {
return result
}
-fun xor(n1: Int, n2: Int, r: Registers): Int {
+private fun xor(n1: Int, n2: Int, r: Registers): Int {
val result = n1 xor n2
@@ -195,7 +195,7 @@ fun xor(n1: Int, n2: Int, r: Registers): Int {
return result
}
-fun inc(n: Int, r: Registers): Int {
+private fun inc(n: Int, r: Registers): Int {
val result = (n + 1) and 0xFF
@@ -206,7 +206,7 @@ fun inc(n: Int, r: Registers): Int {
return result
}
-fun dec(n: Int, r: Registers): Int {
+private fun dec(n: Int, r: Registers): Int {
val result = (n - 1) and 0xFF
diff --git a/src/main/kotlin/cpu/opcodes/Loads16Bit.kt b/src/main/kotlin/cpu/opcodes/Loads16Bit.kt
index 3db4f7d..134b3a9 100644
--- a/src/main/kotlin/cpu/opcodes/Loads16Bit.kt
+++ b/src/main/kotlin/cpu/opcodes/Loads16Bit.kt
@@ -66,7 +66,7 @@ var loads16Bit = mapOf(
)
-fun pop(r: Registers, m: Ram): Int {
+private fun pop(r: Registers, m: Ram): Int {
val lsb = m.readByte(r.getAndIncrementSP())
val msb = m.readByte(r.getAndIncrementSP())
return bm.bytesToWord(msb, lsb)