Skip to main content

LengthCounter

Struct LengthCounter 

Source
pub struct LengthCounter {
    pub count: u8,
    pub halt: bool,
    pub new_halt: bool,
    pub enabled: bool,
    pub reload_val: u8,
    pub previous_count: u8,
}
Expand description

Length counter shared by pulse, triangle, noise channels.

Fields§

§count: u8

Current count (0..=254). 0 = silenced.

§halt: bool

Effective halt flag consulted by clock (also serves as envelope-loop on pulse/noise; control on triangle). Updated from new_halt in reload.

§new_halt: bool

Latched halt value from the most recent $4000/$4004/$4008/$400C write, promoted to halt by reload after the half-frame clock (the “halt change occurs after clocking length” rule). See the module docs.

§enabled: bool

Channel-enable flag from $4015 write.

§reload_val: u8

Pending reload value from the most recent length load. 0 = no pending reload (a real load never selects table entry 0 for index 0 → value 10, so 0 is an unambiguous “empty” sentinel here; the table has no 0 entry). Consumed by reload.

§previous_count: u8

Snapshot of count captured at load time. If the half-frame clock decremented the counter this cycle, the post-clock count differs from this snapshot and the pending reload is dropped (the “reload ignored during clocking when ctr > 0” rule). See the module docs.

Implementations§

Source§

impl LengthCounter

Source

pub fn load(&mut self, raw: u8)

Load a new value from a $4003/$4007/$400B/$400F write. Lookup index = top 5 bits of the value.

The reload is deferred: it latches reload_val and snapshots the current count into previous_count; reload applies it (or drops it, if a same-cycle half-frame clock moved the count). A disabled channel ignores the load entirely.

Source

pub const fn set_halt(&mut self, halt: bool)

Latch a halt-flag change from a $4000/$4004/$4008/$400C write. The value takes effect at the next reload (after the half-frame clock), not immediately — see the module docs.

Source

pub const fn set_enabled(&mut self, enabled: bool)

Channel-enable update from $4015 write. Clearing the bit forces the count to 0 (silences the channel).

Source

pub const fn clock(&mut self)

Half-frame clock.

Source

pub const fn reload(&mut self)

Apply the deferred halt and reload. Called by the owning APU once per CPU cycle, after the half-frame clock and before the mixer samples the channel:

  • A pending reload is honoured only if the post-clock count still equals the previous_count snapshot taken at load — i.e. a same-cycle half-frame clock did not decrement it (it was already zero). Otherwise the reload is dropped.
  • The effective halt is refreshed from new_halt unconditionally, so a halt change becomes effective for the next clock.

On a cycle with no half-frame clock (the overwhelmingly common case), the count is untouched between the write and this call, so count == previous_count holds and the reload applies in-cycle — byte-identical to an immediate load.

Source

pub const fn active(&self) -> bool

$4015 read — bit set if count > 0.

Trait Implementations§

Source§

impl Clone for LengthCounter

Source§

fn clone(&self) -> LengthCounter

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for LengthCounter

Source§

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

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

impl Default for LengthCounter

Source§

fn default() -> LengthCounter

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

impl Copy for LengthCounter

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
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
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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.