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

68 lines
1.9 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
import razorvine.ksim65.Bus
2019-09-11 00:17:59 +00:00
typealias UByte = Short
typealias Address = Int
/**
* Base class for any component connected to the system bus.
*/
2019-09-11 00:17:59 +00:00
abstract class BusComponent {
lateinit var bus: Bus
2019-09-15 03:04:57 +00:00
/**
* One clock cycle on the bus
*/
2019-09-11 00:17:59 +00:00
abstract fun clock()
2019-09-15 03:04:57 +00:00
/**
* Reset all devices on the bus
*/
2019-09-11 00:17:59 +00:00
abstract fun reset()
}
/**
* Base class for components that have registers mapped into the cpu's address space.
* Most I/O components fall into this category.
*/
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
}
}
}
/**
* Base class for components that actually contain memory (RAM or ROM chips).
*/
2019-09-11 00:19:33 +00:00
abstract class MemoryComponent(startAddress: Address, endAddress: Address) :
MemMappedComponent(startAddress, endAddress) {
abstract fun copyOfMem(): Array<UByte>
2019-09-15 03:04:57 +00:00
init {
require(startAddress and 0xff == 0 && endAddress and 0xff == 0xff) {"address range must span complete page(s)"}
}
2019-09-11 00:17:59 +00:00
}