disbrowser/src/jvmMain/kotlin/com/smallhacker/disbrowser/asm/SnesAddress.kt

34 lines
1.4 KiB
Kotlin
Raw Normal View History

2019-01-07 18:19:37 +00:00
package com.smallhacker.disbrowser.asm
2019-01-11 05:09:12 +00:00
import com.fasterxml.jackson.annotation.JsonCreator
2019-01-11 03:19:08 +00:00
import com.fasterxml.jackson.annotation.JsonValue
import com.smallhacker.disbrowser.util.UInt24
import com.smallhacker.disbrowser.util.toUInt24
2019-01-11 05:09:12 +00:00
import com.smallhacker.disbrowser.util.tryParseInt
2019-01-11 03:19:08 +00:00
2019-01-11 16:35:35 +00:00
data class SnesAddress(val value: UInt24) : Comparable<SnesAddress> {
operator fun plus(offset: Int) = SnesAddress(value + offset)
operator fun minus(offset: Int) = SnesAddress(value - offset)
2019-01-07 18:19:37 +00:00
operator fun inc() = plus(1)
2019-01-11 03:19:08 +00:00
operator fun dec() = minus(1)
2019-01-07 18:19:37 +00:00
override fun toString(): String = toFormattedString()
2019-01-11 03:19:08 +00:00
fun toFormattedString(): String = String.format("$%02x:%04x", (value shr 16).toInt(), (value and 0xFFFFu).toInt())
2019-01-11 05:09:12 +00:00
@JsonValue
2019-01-11 03:19:08 +00:00
fun toSimpleString(): String = String.format("%06x", value.toInt())
2019-01-11 16:35:35 +00:00
fun withinBank(value: UShort): SnesAddress = SnesAddress((this.value and 0xFF_0000u) or value.toUInt24())
2019-01-11 03:19:08 +00:00
2019-01-11 16:35:35 +00:00
override fun compareTo(other: SnesAddress) = value.toUInt().compareTo(other.value.toUInt())
2019-01-11 03:19:08 +00:00
2019-01-11 16:35:35 +00:00
infix fun distanceTo(other: SnesAddress) = Math.abs(value.toInt() - other.value.toInt()).toUInt()
2019-01-11 05:09:12 +00:00
companion object {
@JvmStatic
@JsonCreator
2019-01-11 16:35:35 +00:00
fun parse(address: String): SnesAddress? = tryParseInt(address, 16)
?.let { SnesAddress(it.toUInt24()) }
2019-01-11 05:09:12 +00:00
}
2019-01-11 03:19:08 +00:00
}
2019-01-07 18:19:37 +00:00
2019-01-12 02:16:50 +00:00
fun address(snesAddress: Int) = SnesAddress(snesAddress.toUInt24())