Skip to main content

FamilyKeyboardState

Struct FamilyKeyboardState 

Source
pub struct FamilyKeyboardState { /* private fields */ }
Expand description

The Famicom Family BASIC keyboard overlay state.

Per the NESdev “Family BASIC Keyboard” page, the keyboard is a 9 x 8 switch matrix (with the data-recorder lines unused here) read through the expansion port but software-visible on $4017. The protocol:

  • $4016 write — bit 0 (the “column” select; 0 selects the low 4 keys of the current row, 1 selects the high 4) and bit 1 (a clock; a 0->1 transition advances to the next row). Bit 2 enables the keyboard matrix; when bit 2 is 0 the matrix is disabled and $4017 reads 1s. Writing bit 1 = 0 while bit 2 = 1 resets the row counter to 0.
  • $4017 read — bits 4..1 carry the four key switches of the currently selected (row, column-half), active-low (0 = pressed). There are 9 rows x 2 halves = 18 selectable groups of 4 keys = 72 key positions.

We model the live pressed state as a 72-bit key bitmap ([u8; 9], one byte per row: low nibble = column-half 0, high nibble = column-half 1) and the row counter + column select per the write protocol. Determinism holds: it is a pure function of the writes + the live key bitmap.

Implementations§

Source§

impl FamilyKeyboardState

Source

pub const fn new() -> Self

New keyboard with no keys held, matrix reset + disabled.

Source

pub const fn set_keys(&mut self, keys: [u8; 9])

Set the full pressed-key bitmap (one byte per matrix row; low nibble = column-half 0, high nibble = column-half 1). The frontend builds this from host keys via its key map.

Source

pub const fn set_key(&mut self, index: usize, pressed: bool)

Set one key by linear index (0..72) — index = row * 8 + bit, matching the matrix layout (bit 0..=3 = column-half 0, 4..=7 = column-half 1). Out-of-range indices are ignored.

Source

pub const fn write_strobe(&mut self, value: u8)

Handle a $4016 write: latch column select (bit 0), advance the row on a clock (bit 1) rising edge, set the matrix-enable (bit 2). A clock low while enabled resets the row counter to 0.

Source

pub const fn read(&self) -> u8

Read the device byte for a $4017 access. The four selected key switches are returned on bits 4..1, active-low (0 = pressed). When the matrix is disabled, all four bits read 1 (no keys). The caller ORs in the open-bus upper bits.

Source

pub const fn peek(&self) -> u8

Side-effect-free sample of the device byte (debugger peek) — identical to Self::read (the keyboard read has no side effects).

Source

pub const fn from_parts( keys: [u8; 9], row: u8, column: bool, enabled: bool, clock: bool, ) -> Self

Reconstruct from save-state parts.

Source

pub const fn keys_raw(&self) -> [u8; 9]

Raw per-row key bitmap (save-state).

Source

pub const fn row_raw(&self) -> u8

Raw row counter (save-state).

Source

pub const fn column_raw(&self) -> bool

Raw column select (save-state).

Source

pub const fn enabled_raw(&self) -> bool

Raw matrix-enable (save-state).

Source

pub const fn clock_raw(&self) -> bool

Raw clock level (save-state).

Trait Implementations§

Source§

impl Clone for FamilyKeyboardState

Source§

fn clone(&self) -> FamilyKeyboardState

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 FamilyKeyboardState

Source§

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

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

impl Default for FamilyKeyboardState

Source§

fn default() -> Self

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

impl Copy for FamilyKeyboardState

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> Same for T

Source§

type Output = T

Should always be Self
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.