aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJames Barnett <noreply@jamesbarnett.xyz>2018-06-15 22:10:47 +0100
committerJames Barnett <noreply@jamesbarnett.xyz>2018-06-15 22:10:47 +0100
commitd8dbfe55f4741a9cf70c1b95762461e08eb08f22 (patch)
tree779df3fc4192b251da8f9f1e9e712376d9f096c9 /src
parent22f3463e3128947df69cb6444c82bd73350d5cc5 (diff)
downloadKGB-d8dbfe55f4741a9cf70c1b95762461e08eb08f22.tar.xz
KGB-d8dbfe55f4741a9cf70c1b95762461e08eb08f22.zip
Add all 8-Bit Load opcodes
Diffstat (limited to 'src')
-rw-r--r--src/main/kotlin/BitManipulation.kt4
-rw-r--r--src/main/kotlin/cpu/Operation.kt2
-rw-r--r--src/main/kotlin/cpu/Registers.kt16
-rw-r--r--src/main/kotlin/cpu/opcodes/Loads8Bit.kt24
4 files changed, 43 insertions, 3 deletions
diff --git a/src/main/kotlin/BitManipulation.kt b/src/main/kotlin/BitManipulation.kt
index a9f9493..b4215e8 100644
--- a/src/main/kotlin/BitManipulation.kt
+++ b/src/main/kotlin/BitManipulation.kt
@@ -4,6 +4,10 @@ object BitManipulation {
return msb.shl(8) + lsb
}
+ fun argsToWord(args: IntArray): Int {
+ return bytesToWord(args[1], args[0])
+ }
+
fun getMsb(value: Int): Int {
return value.shr(8)
}
diff --git a/src/main/kotlin/cpu/Operation.kt b/src/main/kotlin/cpu/Operation.kt
index 71c3076..2291ef8 100644
--- a/src/main/kotlin/cpu/Operation.kt
+++ b/src/main/kotlin/cpu/Operation.kt
@@ -2,4 +2,4 @@ package cpu
import ram.Ram
-class Operation(val name: String, val length: Int, val cycles: Int, val command: (Registers, Ram, List<Int>) -> Unit) \ No newline at end of file
+class Operation(val name: String, val length: Int, val cycles: Int, val command: (Registers, Ram, IntArray) -> Unit) \ No newline at end of file
diff --git a/src/main/kotlin/cpu/Registers.kt b/src/main/kotlin/cpu/Registers.kt
index 7aa144f..6d1e626 100644
--- a/src/main/kotlin/cpu/Registers.kt
+++ b/src/main/kotlin/cpu/Registers.kt
@@ -99,5 +99,21 @@ class Registers {
L = bm.getLsb(value)
}
+ fun getAndDecrementHL(): Int {
+ val currentHL = HL
+ if(HL > 0x0000) { // TODO - is this correct? Or should it underflow to 0xFFFF?. Also for increment op.
+ HL--
+ }
+ return currentHL
+ }
+
+ fun getAndIncrementHL(): Int {
+ val currentHL = HL
+ if(HL < 0xFFFF) {
+ HL++
+ }
+ return currentHL
+ }
+
} \ No newline at end of file
diff --git a/src/main/kotlin/cpu/opcodes/Loads8Bit.kt b/src/main/kotlin/cpu/opcodes/Loads8Bit.kt
index 091db7f..99eb933 100644
--- a/src/main/kotlin/cpu/opcodes/Loads8Bit.kt
+++ b/src/main/kotlin/cpu/opcodes/Loads8Bit.kt
@@ -1,6 +1,7 @@
package cpu.opcodes
import cpu.Operation
+import BitManipulation as bm
// 8-Bit Loads
var loads8Bit = mapOf(
@@ -21,7 +22,8 @@ var loads8Bit = mapOf(
0x0A to Operation("LD A,(BC)", 0, 8, {r, m, _ -> r.A = m.readByte(r.BC)}),
0x1A to Operation("LD A,(DE)", 0, 8, {r, m, _ -> r.A = m.readByte(r.DE)}),
0x7E to Operation("LD A,(HL)", 0, 8, {r, m, _ -> r.A = m.readByte(r.HL)}),
- 0x3E to Operation("LD A, n", 0, 8, {r, _, a -> r.A = a[0]}),
+ 0x3E to Operation("LD A, n", 1, 8, {r, _, a -> r.A = a[0]}),
+ 0xFA to Operation("LD A,(nn)", 2, 16, {r, m, a -> r.A = m.readByte(bm.argsToWord(a))}),
0x40 to Operation("LD B,B", 0, 4, {r, _, _ -> r.B = r.B}),
0x41 to Operation("LD B,C", 0, 4, {r, _, _ -> r.B = r.C}),
@@ -86,6 +88,24 @@ var loads8Bit = mapOf(
0x57 to Operation("LD D,A", 0, 4, {r, _, _ -> r.D = r.A}),
0x5F to Operation("LD E,A", 0, 4, {r, _, _ -> r.E = r.A}),
0x67 to Operation("LD H,A", 0, 4, {r, _, _ -> r.H = r.A}),
- 0x6F to Operation("LD L,A", 0, 4, {r, _, _ -> r.L = r.A})
+ 0x6F to Operation("LD L,A", 0, 4, {r, _, _ -> r.L = r.A}),
+ 0x02 to Operation("LD (BC),A", 0, 8, {r, m, _ -> m.writeByte(r.BC, r.A)}),
+ 0x12 to Operation("LD (DE),A", 0, 8, {r, m, _ -> m.writeByte(r.DE, r.A)}),
+ 0x77 to Operation("LD (HL),A", 0, 8, {r, m, _ -> m.writeByte(r.HL, r.A)}),
+ 0xEA to Operation("LD (nn),A", 2, 16, {r, m, a -> m.writeByte(bm.argsToWord(a), r.A)}),
+
+ 0xF2 to Operation("LD A,(C)", 0, 8, {r, m, _ -> r.A = m.readByte(0xFF00 + r.C)}),
+ 0xE2 to Operation("LD (C),A", 0, 8, {r, m, _ -> m.writeByte(0xFF00 + r.C, r.A)}),
+
+ 0x3A to Operation("LDD A,(HL)", 0, 8, {r, m, _ -> r.A = m.readByte(r.getAndDecrementHL())}),
+ 0x32 to Operation("LDD (HL),A", 0, 8, {r, m, _ -> m.writeByte(r.getAndDecrementHL(), r.A)}),
+
+ 0x2A to Operation("LDI A,(HL)", 0, 8, {r, m, _ -> r.A = m.readByte(r.getAndIncrementHL())}),
+ 0x22 to Operation("LDI (HL),A", 0, 8, {r, m, _ -> m.writeByte(r.getAndIncrementHL(), r.A)}),
+
+ 0xE0 to Operation("LD (n),A", 1, 8, {r, m, a -> m.writeByte(0xFF00 + a[0], r.A)}),
+ 0xF0 to Operation("LD A,(n)", 1, 8, {r, m, a -> r.A = m.readByte(0xFF00 + a[0])})
+
+
) \ No newline at end of file