aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/kotlin/BitManipulation.kt27
-rw-r--r--src/main/kotlin/cpu/Registers.kt77
2 files changed, 55 insertions, 49 deletions
diff --git a/src/main/kotlin/BitManipulation.kt b/src/main/kotlin/BitManipulation.kt
new file mode 100644
index 0000000..a9f9493
--- /dev/null
+++ b/src/main/kotlin/BitManipulation.kt
@@ -0,0 +1,27 @@
+object BitManipulation {
+
+ fun bytesToWord(msb: Int, lsb: Int): Int {
+ return msb.shl(8) + lsb
+ }
+
+ fun getMsb(value: Int): Int {
+ return value.shr(8)
+ }
+
+ fun getLsb(value: Int): Int {
+ return value.and(0xFF)
+ }
+
+ fun validateUnsigned8Bit(value: Int) {
+ if(value < 0 || value > 255) {
+ throw IllegalArgumentException("Value $value is not an unsigned 8-Bit value")
+ }
+ }
+
+ 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
diff --git a/src/main/kotlin/cpu/Registers.kt b/src/main/kotlin/cpu/Registers.kt
index 6cf417e..7aa144f 100644
--- a/src/main/kotlin/cpu/Registers.kt
+++ b/src/main/kotlin/cpu/Registers.kt
@@ -1,124 +1,103 @@
package cpu
+import BitManipulation as bm
+
class Registers {
// General purpose registers
var B: Int = 0
set(value) {
- validateUnsigned8Bit(value)
+ bm.validateUnsigned8Bit(value)
field = value
}
var C: Int = 0
set(value) {
- validateUnsigned8Bit(value)
+ bm.validateUnsigned8Bit(value)
field = value
}
var D: Int = 0
set(value) {
- validateUnsigned8Bit(value)
+ bm.validateUnsigned8Bit(value)
field = value
}
var E: Int = 0
set(value) {
- validateUnsigned8Bit(value)
+ bm.validateUnsigned8Bit(value)
field = value
}
var H: Int = 0
set(value) {
- validateUnsigned8Bit(value)
+ bm.validateUnsigned8Bit(value)
field = value
}
var L: Int = 0
set(value) {
- validateUnsigned8Bit(value)
+ bm.validateUnsigned8Bit(value)
field = value
}
// Special registers
var A: Int = 0
set(value) {
- validateUnsigned8Bit(value)
+ bm.validateUnsigned8Bit(value)
field = value
}
var F: Int = 0
set(value) {
- validateUnsigned8Bit(value)
+ bm.validateUnsigned8Bit(value)
field = value
}
var SP: Int = 0
set(value) {
- validateUnsigned16Bit(value)
+ bm.validateUnsigned16Bit(value)
field = value
}
var PC: Int = 0
set(value) {
- validateUnsigned16Bit(value)
+ bm.validateUnsigned16Bit(value)
field = value
}
// 16-Bit accessors
var AF: Int
get() {
- return bytesToWord(A, F)
+ return bm.bytesToWord(A, F)
}
set(value) {
- validateUnsigned16Bit(value)
- A = getMsb(value)
- F = getLsb(value)
+ bm.validateUnsigned16Bit(value)
+ A = bm.getMsb(value)
+ F = bm.getLsb(value)
}
var BC: Int
get() {
- return bytesToWord(B, C)
+ return bm.bytesToWord(B, C)
}
set(value) {
- validateUnsigned16Bit(value)
- B = getMsb(value)
- C = getLsb(value)
+ bm.validateUnsigned16Bit(value)
+ B = bm.getMsb(value)
+ C = bm.getLsb(value)
}
var DE: Int
get() {
- return bytesToWord(D, E)
+ return bm.bytesToWord(D, E)
}
set(value) {
- validateUnsigned16Bit(value)
- D = getMsb(value)
- E = getLsb(value)
+ bm.validateUnsigned16Bit(value)
+ D = bm.getMsb(value)
+ E = bm.getLsb(value)
}
var HL: Int
get() {
- return bytesToWord(H, L)
+ return bm.bytesToWord(H, L)
}
set(value) {
- validateUnsigned16Bit(value)
- H = getMsb(value)
- L = getLsb(value)
+ bm.validateUnsigned16Bit(value)
+ H = bm.getMsb(value)
+ L = bm.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