Skip to main content

CmosRtc

Struct CmosRtc 

Source
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: u8

Currently selected register (0..=127).

§nmi_disabled: bool

NMI disabled by the last index-port write (bit 7 of the index byte).

§unix_time: u64

Current UNIX timestamp, in seconds.

§frozen: Option<u64>

Timestamp captured when status B SET was written.

§status_a: u8

Status register A (UIP excluded when read).

§status_b: u8

Status register B.

§status_c: u8

Status register C flags, cleared on read.

§sub_second_remainder: u64

Sub-second remainder of the 32.768 kHz reference clock.

§periodic_accumulator: u64

Phase 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

Source

pub fn new() -> Self

Creates a clock at the UNIX epoch, BCD, 24-hour, with a valid calendar.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub const fn periodic_interrupt_enabled(&self) -> bool

True when the periodic interrupt is enabled in status B.

Source

pub const fn alarm_interrupt_enabled(&self) -> bool

True when the alarm interrupt is enabled in status B.

Source

pub const fn update_ended_interrupt_enabled(&self) -> bool

True when the update-ended interrupt is enabled in status B.

Source

pub const fn hours_24_mode(&self) -> bool

True in 24-hour mode (status B bit 1).

Source

pub const fn binary_mode(&self) -> bool

True in binary data mode (status B bit 2).

Source

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.

Source

pub const fn clock_frozen(&self) -> bool

True while status B SET freezes the guest-visible calendar.

Source

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.

Source

pub const fn index(&self) -> u8

Currently selected register index (0..=127).

Source

pub const fn nmi_disabled(&self) -> bool

True when the last index-port write disabled the NMI.

Source

pub fn read_data(&mut self) -> u8

Reads the data port for the selected register.

Source

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.

Source

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.

Source

pub fn ram(&self, register: u8) -> u8

Reads a CMOS RAM byte directly.

Source

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.

Source

fn write_status_b(&mut self, value: u8)

Writes status register B, capturing or releasing the SET freeze.

Source

fn clock(&self) -> u64

Timestamp the computed registers are derived from.

Source

fn seconds(&self) -> u64

Seconds (0..=59).

Source

fn minutes(&self) -> u64

Minutes (0..=59).

Source

fn hours(&self) -> u64

Hours in 24-hour form (0..=23).

Source

fn weekday(&self) -> u64

Day of week, 1 = Sunday .. 7 = Saturday.

Source

fn day(&self) -> u64

Day of the month (1..=31).

Source

fn month(&self) -> u64

Month (1..=12).

Source

fn year(&self) -> u64

Full Gregorian year.

Source

fn days(&self) -> u64

Whole days since the UNIX epoch.

Source

fn encode(&self, value: u64) -> u8

Encodes a value in the format selected by status B (binary or BCD).

Source

fn encode_hours(&self) -> u8

Encodes the hours register, honouring the 12/24-hour mode.

Source

fn encode_hours_value(&self, hours: u64) -> u8

Encodes a 24-hour value into the guest-visible hours byte.

Source

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§

Source§

impl Clone for CmosRtc

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CmosRtc

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CmosRtc

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for CmosRtc

Source§

impl PartialEq for CmosRtc

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for CmosRtc

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.