aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/cpu/Registers.kt
diff options
context:
space:
mode:
authorJames Barnett <noreply@jamesbarnett.xyz>2018-06-14 21:18:14 +0100
committerJames Barnett <noreply@jamesbarnett.xyz>2018-06-14 21:18:14 +0100
commitcf0f98348c5f4df57d3adb44418cc2911e7b37b7 (patch)
tree8ee74dfcbd402e7966f8e57440a54ba34d35d899 /src/main/kotlin/cpu/Registers.kt
parent1d66a2c79cf98970ee80cbb1fe4262fa95c885dd (diff)
downloadKGB-cf0f98348c5f4df57d3adb44418cc2911e7b37b7.tar.xz
KGB-cf0f98348c5f4df57d3adb44418cc2911e7b37b7.zip
Make a start on opcodes
Diffstat (limited to 'src/main/kotlin/cpu/Registers.kt')
-rw-r--r--src/main/kotlin/cpu/Registers.kt124
1 files changed, 124 insertions, 0 deletions
diff --git a/src/main/kotlin/cpu/Registers.kt b/src/main/kotlin/cpu/Registers.kt
new file mode 100644
index 0000000..6cf417e
--- /dev/null
+++ b/src/main/kotlin/cpu/Registers.kt
@@ -0,0 +1,124 @@
+package cpu
+
+class Registers {
+
+ // General purpose registers
+ var B: Int = 0
+ set(value) {
+ validateUnsigned8Bit(value)
+ field = value
+ }
+ var C: Int = 0
+ set(value) {
+ validateUnsigned8Bit(value)
+ field = value
+ }
+ var D: Int = 0
+ set(value) {
+ validateUnsigned8Bit(value)
+ field = value
+ }
+ var E: Int = 0
+ set(value) {
+ validateUnsigned8Bit(value)
+ field = value
+ }
+ var H: Int = 0
+ set(value) {
+ validateUnsigned8Bit(value)
+ field = value
+ }
+ var L: Int = 0
+ set(value) {
+ validateUnsigned8Bit(value)
+ field = value
+ }
+
+ // Special registers
+ var A: Int = 0
+ set(value) {
+ validateUnsigned8Bit(value)
+ field = value
+ }
+ var F: Int = 0
+ set(value) {
+ validateUnsigned8Bit(value)
+ field = value
+ }
+ var SP: Int = 0
+ set(value) {
+ validateUnsigned16Bit(value)
+ field = value
+ }
+ var PC: Int = 0
+ set(value) {
+ validateUnsigned16Bit(value)
+ field = value
+ }
+
+ // 16-Bit accessors
+ var AF: Int
+ get() {
+ return bytesToWord(A, F)
+ }
+ set(value) {
+ validateUnsigned16Bit(value)
+ A = getMsb(value)
+ F = getLsb(value)
+ }
+
+ var BC: Int
+ get() {
+ return bytesToWord(B, C)
+ }
+ set(value) {
+ validateUnsigned16Bit(value)
+ B = getMsb(value)
+ C = getLsb(value)
+ }
+
+ var DE: Int
+ get() {
+ return bytesToWord(D, E)
+ }
+ set(value) {
+ validateUnsigned16Bit(value)
+ D = getMsb(value)
+ E = getLsb(value)
+ }
+
+ var HL: Int
+ get() {
+ return bytesToWord(H, L)
+ }
+ set(value) {
+ validateUnsigned16Bit(value)
+ H = getMsb(value)
+ L = getLsb(value)
+ }
+
+ private fun bytesToWord(msb: Int, lsb: Int): Int {
+ return msb.shl(8) + lsb
+ }
+
+ private fun getMsb(value: Int): Int {
+ return value.shr(8)
+ }
+
+ private fun getLsb(value: Int): Int {
+ return value.and(0xFF)
+ }
+
+ private fun validateUnsigned8Bit(value: Int) {
+ if(value < 0 || value > 255) {
+ throw IllegalArgumentException("Value $value is not an unsigned 8-Bit value")
+ }
+ }
+
+ private fun validateUnsigned16Bit(value: Int) {
+ if(value < 0 || value > 65535) {
+ throw IllegalArgumentException("Value $value is not an unsigned 16-Bit value")
+ }
+ }
+
+} \ No newline at end of file