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

68 lines
1.8 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 java.time.LocalDate
import java.time.LocalTime
/**
* A real-time time of day clock.
* (System timers are elsewhere)
*
2019-09-15 03:04:57 +00:00
* reg. value
* ---- ----------
* 00 year (lsb)
* 01 year (msb)
* 02 month, 1-12
* 03 day, 1-31
* 04 hour, 0-23
* 05 minute, 0-59
* 06 second, 0-59
* 07 millisecond, 0-999 (lsb)
* 08 millisecond, 0-999 (msb)
2019-09-11 00:17:59 +00:00
*/
class RealTimeClock(startAddress: Address, endAddress: Address) : MemMappedComponent(startAddress, endAddress) {
init {
require(endAddress - startAddress + 1 == 9) { "rtc needs exactly 9 memory bytes" }
}
override fun clock() {
/* not updated on clock pulse */
}
override fun reset() {
/* never reset */
}
override operator fun get(address: Address): UByte {
return when (address - startAddress) {
2019-09-15 03:04:57 +00:00
0x00 -> {
2019-09-11 00:17:59 +00:00
val year = LocalDate.now().year
(year and 255).toShort()
}
2019-09-15 03:04:57 +00:00
0x01 -> {
2019-09-11 00:17:59 +00:00
val year = LocalDate.now().year
(year ushr 8).toShort()
}
2019-09-15 03:04:57 +00:00
0x02 -> LocalDate.now().monthValue.toShort()
0x03 -> LocalDate.now().dayOfMonth.toShort()
0x04 -> LocalTime.now().hour.toShort()
0x05 -> LocalTime.now().minute.toShort()
0x06 -> LocalTime.now().second.toShort()
0x07 -> {
2019-09-11 00:17:59 +00:00
val ms = LocalTime.now().nano / 1000
(ms and 255).toShort()
}
2019-09-15 03:04:57 +00:00
0x08 -> {
2019-09-11 00:17:59 +00:00
val ms = LocalTime.now().nano / 1000
(ms ushr 8).toShort()
}
2019-09-15 03:04:57 +00:00
else -> 0xff
2019-09-11 00:17:59 +00:00
}
}
override operator fun set(address: Address, data: UByte) {
/* real time clock can't be set */
}
}