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

33 lines
1015 B
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 java.io.File
/**
* A ROM chip (read-only memory).
*/
2019-09-16 23:18:32 +00:00
class Rom(startAddress: Address, endAddress: Address, initialData: Array<UByte>? = null) : MemoryComponent(startAddress, endAddress) {
2021-07-06 21:37:19 +00:00
override val data: Array<UByte> = initialData?.copyOf() ?: Array(endAddress-startAddress+1) { 0 }
2019-09-11 00:17:59 +00:00
init {
2019-10-12 10:35:18 +00:00
require(endAddress-startAddress+1 == data.size) { "rom address range doesn't match size of data bytes" }
}
override operator fun get(offset: Int): UByte = data[offset]
override operator fun set(offset: Int, data: UByte) { /* read-only */ }
2019-09-11 00:17:59 +00:00
override fun clock() {}
override fun reset() {}
/**
* load a binary program at the given address
*/
fun load(filename: String) {
val bytes = File(filename).readBytes()
load(bytes)
}
2020-02-04 22:56:58 +00:00
fun load(data: Array<UByte>) = data.copyInto(this.data)
2019-10-12 10:35:18 +00:00
2020-02-04 22:56:58 +00:00
fun load(data: ByteArray) = data.map { (it.toInt() and 255).toShort() }.toTypedArray().copyInto(this.data)
2019-09-11 00:17:59 +00:00
}