1
0
mirror of https://github.com/irmen/ksim65.git synced 2024-06-26 02:29:38 +00:00
ksim65/src/main/kotlin/razorvine/ksim65/components/Component.kt

45 lines
1.4 KiB
Kotlin
Raw Normal View History

2019-09-11 00:39:58 +00:00
package razorvine.ksim65.components
2019-09-11 00:17:59 +00:00
typealias UByte = Short
typealias Address = Int
abstract class BusComponent {
lateinit var bus: Bus
abstract fun clock()
abstract fun reset()
}
2019-09-11 00:19:33 +00:00
abstract class MemMappedComponent(val startAddress: Address, val endAddress: Address) : BusComponent() {
2019-09-11 00:17:59 +00:00
abstract operator fun get(address: Address): UByte
abstract operator fun set(address: Address, data: UByte)
init {
2019-09-11 00:19:33 +00:00
require(endAddress >= startAddress)
require(startAddress >= 0 && endAddress <= 0xffff) { "can only have 16-bit address space" }
2019-09-11 00:17:59 +00:00
}
2019-09-14 16:58:45 +00:00
fun hexDump(from: Address, to: Address, charmapper: ((Short)->Char)? = null) {
2019-09-11 00:19:33 +00:00
(from..to).chunked(16).forEach {
2019-09-11 00:17:59 +00:00
print("\$${it.first().toString(16).padStart(4, '0')} ")
val bytes = it.map { address -> get(address) }
bytes.forEach { byte ->
print(byte.toString(16).padStart(2, '0') + " ")
}
print(" ")
2019-09-14 16:58:45 +00:00
val chars =
if(charmapper!=null)
bytes.map { b -> charmapper(b) }
else
bytes.map { b -> if(b in 32..255) b.toChar() else '.' }
println(chars.joinToString(""))
2019-09-11 00:17:59 +00:00
}
}
}
2019-09-11 00:19:33 +00:00
abstract class MemoryComponent(startAddress: Address, endAddress: Address) :
MemMappedComponent(startAddress, endAddress) {
2019-09-11 00:17:59 +00:00
abstract fun cloneContents(): Array<UByte>
}