pub struct CmosRtc {
index: u8,
nmi_disabled: bool,
unix_time: u64,
frozen: Option<u64>,
status_a: u8,
status_b: u8,
status_c: u8,
sub_second_remainder: u64,
periodic_accumulator: u64,
ram: [u8; 128],
}Expand description
Model of an MC146818-compatible CMOS RTC and its CMOS RAM.
Fields§
§index: u8Currently selected register (0..=127).
nmi_disabled: boolNMI disabled by the last index-port write (bit 7 of the index byte).
unix_time: u64Current UNIX timestamp, in seconds.
frozen: Option<u64>Timestamp captured when status B SET was written.
status_a: u8Status register A (UIP excluded when read).
status_b: u8Status register B.
status_c: u8Status register C flags, cleared on read.
sub_second_remainder: u64Sub-second remainder of the 32.768 kHz reference clock.
periodic_accumulator: u64Phase of the periodic interrupt, in reference-clock ticks.
ram: [u8; 128]CMOS RAM bytes; the computed registers are only mirrors of the clock.
Implementations§
Source§impl CmosRtc
impl CmosRtc
Sourcepub fn set_unix_time(&mut self, seconds: u64)
pub fn set_unix_time(&mut self, seconds: u64)
Sets the clock to seconds since the UNIX epoch.
If status B SET is asserted, the frozen snapshot is updated too, so the guest sees the newly programmed time.
Sourcepub const fn unix_time(&self) -> u64
pub const fn unix_time(&self) -> u64
Current UNIX timestamp, in seconds.
This is the host-side time: it keeps advancing even while status B SET freezes the guest-visible calendar registers.
Sourcepub fn advance(&mut self, seconds: u64)
pub fn advance(&mut self, seconds: u64)
Advances the clock by seconds (saturating at u64::MAX).
This is the historical helper used by the device model: it advances the
time, generates the same interrupt events as
Self::advance_events (so status C and Self::irq_asserted stay
coherent) and discards the returned events.
Sourcepub fn advance_events(
&mut self,
seconds: u64,
sub_second_ticks: u64,
) -> RtcEvents
pub fn advance_events( &mut self, seconds: u64, sub_second_ticks: u64, ) -> RtcEvents
Advances the clock and reports the interrupt events of the interval.
seconds is whole host seconds; sub_second_ticks is an additional
fraction expressed in 32.768 kHz reference-clock ticks
(REFERENCE_HZ) : typically elapsed_ticks / 32768 remainders passed
by a caller that only has a finer-grained clock. The two are added and
the sub-second remainder is carried across calls, so repeated calls with
small tick counts do not drift.
The returned RtcEvents carries at most one event of each kind:
periodic: at least one periodic period elapsed. The phase accumulator is maintained modulo the programmed period, so the rate stays exact even when a poll covers several periods;update_ended: at least one second boundary was crossed (the model has no update cycle to interrupt, so this is the advertised update-ended event);alarm: the alarm bytes matched at the new second boundary. A single large jump evaluates the alarm once, on the final second; poll more often if an intermediate alarm instant must not be missed.
Events always latch the matching status-C flag (PF, AF, UF), as on
the hardware; the status-B enables only decide whether IRQF is
asserted. Reading status C clears the flags.
The clock saturates at u64::MAX; events are then computed from the
ticks that actually elapsed.
Sourcepub fn periodic_rate_hz(&self) -> Option<u32>
pub fn periodic_rate_hz(&self) -> Option<u32>
Periodic interrupt frequency in hertz, or None when disabled.
The rate-select field RS3:0 follows the chip encoding:
rate = 32768 >> (RS - 1) for RS in 3..=15; RS below 3 disables the
periodic interrupt. The default status A (0x26) selects RS = 6, that
is 1024 Hz.
Sourcepub const fn irq_asserted(&self) -> bool
pub const fn irq_asserted(&self) -> bool
True while the interrupt request line is asserted.
Computed, not stored: the OR of the event flags whose enable bit is set
in status B : exactly the IRQF bit that a status-C read returns.
Sourcepub const fn periodic_interrupt_enabled(&self) -> bool
pub const fn periodic_interrupt_enabled(&self) -> bool
True when the periodic interrupt is enabled in status B.
Sourcepub const fn alarm_interrupt_enabled(&self) -> bool
pub const fn alarm_interrupt_enabled(&self) -> bool
True when the alarm interrupt is enabled in status B.
Sourcepub const fn update_ended_interrupt_enabled(&self) -> bool
pub const fn update_ended_interrupt_enabled(&self) -> bool
True when the update-ended interrupt is enabled in status B.
Sourcepub const fn hours_24_mode(&self) -> bool
pub const fn hours_24_mode(&self) -> bool
True in 24-hour mode (status B bit 1).
Sourcepub const fn binary_mode(&self) -> bool
pub const fn binary_mode(&self) -> bool
True in binary data mode (status B bit 2).
Sourcepub const fn daylight_saving_enabled(&self) -> bool
pub const fn daylight_saving_enabled(&self) -> bool
True when the daylight-saving hint is set (status B bit 0).
The model does not shift the clock: the bit is exposed for drivers that report or program it, and has no effect on the computed time.
Sourcepub const fn clock_frozen(&self) -> bool
pub const fn clock_frozen(&self) -> bool
True while status B SET freezes the guest-visible calendar.
Sourcepub fn write_index(&mut self, value: u8)
pub fn write_index(&mut self, value: u8)
Writes the index port.
Bit 7 disables the NMI; bits 6:0 select the CMOS register. The selected register is what the next data-port access reads or writes.
Sourcepub const fn nmi_disabled(&self) -> bool
pub const fn nmi_disabled(&self) -> bool
True when the last index-port write disabled the NMI.
Sourcepub fn write_data(&mut self, value: u8)
pub fn write_data(&mut self, value: u8)
Writes the data port for the selected register.
Computed time registers are accepted and kept as CMOS RAM bytes but do not move the clock (see the module documentation). Status C and D are read-only; writes to them are ignored.
Sourcepub fn set_ram(&mut self, register: u8, value: u8)
pub fn set_ram(&mut self, register: u8, value: u8)
Writes a CMOS RAM byte directly, without going through the index and data ports.
Firmware configuration (floppy drive types at 0x10, extended-memory
size at 0x30/0x31) is planted this way before the guest starts; the
guest may overwrite it through the normal ports afterwards.
Sourcepub fn raise_status_c(&mut self, flags: u8)
pub fn raise_status_c(&mut self, flags: u8)
Raises interrupt flags in status register C.
The device model calls this when an external event must be latched; the
guest observes the bits once, then the read that returns them clears
them. Only the event flags are accepted: IRQF is computed by the model
and a caller cannot force it.
Sourcefn write_status_b(&mut self, value: u8)
fn write_status_b(&mut self, value: u8)
Writes status register B, capturing or releasing the SET freeze.
Sourcefn encode(&self, value: u64) -> u8
fn encode(&self, value: u64) -> u8
Encodes a value in the format selected by status B (binary or BCD).
Sourcefn encode_hours(&self) -> u8
fn encode_hours(&self) -> u8
Encodes the hours register, honouring the 12/24-hour mode.
Sourcefn encode_hours_value(&self, hours: u64) -> u8
fn encode_hours_value(&self, hours: u64) -> u8
Encodes a 24-hour value into the guest-visible hours byte.
Sourcefn alarm_matches(&self) -> bool
fn alarm_matches(&self) -> bool
True when the guest-visible time matches the programmed alarm.
Seconds, minutes, hours and the model’s day-of-month byte are compared;
a byte in 0xC0..=0xFF is a wildcard. The comparison uses the current
display encoding, so a guest must program the alarm bytes in the same
BCD/binary and 12/24-hour mode as the time registers.
Trait Implementations§
impl Eq for CmosRtc
impl StructuralPartialEq for CmosRtc
Auto Trait Implementations§
impl Forget for CmosRtc
impl Freeze for CmosRtc
impl Move for CmosRtc
impl RefUnwindSafe for CmosRtc
impl Send for CmosRtc
impl Sync for CmosRtc
impl Unpin for CmosRtc
impl UnsafeUnpin for CmosRtc
impl UnwindSafe for CmosRtc
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more