From 22f3463e3128947df69cb6444c82bd73350d5cc5 Mon Sep 17 00:00:00 2001 From: James Barnett Date: Thu, 14 Jun 2018 21:28:31 +0100 Subject: Move bit manipulation helpers out into their own object --- src/main/kotlin/BitManipulation.kt | 27 +++++++++++++ src/main/kotlin/cpu/Registers.kt | 77 ++++++++++++++------------------------ 2 files changed, 55 insertions(+), 49 deletions(-) create mode 100644 src/main/kotlin/BitManipulation.kt (limited to 'src') 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 -- cgit v1.2.3