cge_nes 0.1.2

Cycle-accurate NES (Nintendo Entertainment System) emulator library: CPU, PPU, cartridge, input, and iNES ROM loading.
Documentation
//! High-level OAM array backed by typed `Entry` values.
//!
//! `OamArray` stores 64 `Entry` values and provides safe accessors for OAM
//! fields (y, tile number, attributes, x) as well as raw address-based
//! read/write and an iterator over entries.

use crate::ppu::oam;
use crate::ppu::oam::{entry, Entry, Iter, OAM_ARRAY_ENTRIES_COUNT};

/// Typed representation of the Object Attribute Memory (OAM).
pub struct OamArray {
    data: [Entry; OAM_ARRAY_ENTRIES_COUNT],
}

impl Default for OamArray {
    fn default() -> Self {
        Self {
            data: [Entry::default(); OAM_ARRAY_ENTRIES_COUNT],
        }
    }
}

#[allow(dead_code)]
impl OamArray {
    /// Create a new, zero-initialized OamArray.
    pub fn new() -> Self {
        Self::default()
    }

    /// Read a byte from OAM using the PPU's byte-oriented addressing.
    ///
    /// `addr` is the PPU's OAM address where the low two bits select the
    /// field within a 4-byte entry and the remaining bits select the entry
    /// index.
    pub fn data(&self, addr: u8) -> u8 {
        let index = (addr >> 2) as usize;
        let selected_fied = entry::SelectedField::from(addr);
        self.data[index].read_field(selected_fied)
    }

    /// Write a byte to OAM at the specified byte-oriented address.
    pub fn write_data(&mut self, addr: u8, data: u8) {
        let index = (addr >> 2) as usize;
        let selected_fied = entry::SelectedField::from(addr);
        self.data[index].write_field(selected_fied, data);
    }

    /// Get the Y coordinate for the given sprite index.
    pub fn y(&self, sprite_index: u8) -> u8 {
        self.data[sprite_index as usize].y()
    }

    /// Get the tile number for the given sprite index.
    pub fn tile_number(&self, sprite_index: u8) -> entry::TileIndexNumber {
        self.data[sprite_index as usize].tile_num()
    }

    /// Get the attribute flags for the given sprite index.
    pub fn attributes(&self, sprite_index: u8) -> entry::Attributes {
        self.data[sprite_index as usize].attributes()
    }

    /// Get the X coordinate for the given sprite index.
    pub fn x(&self, sprite_index: u8) -> u8 {
        self.data[sprite_index as usize].x()
    }

    /// Return a copy of the `Entry` at the given sprite index.
    pub fn entry(&self, sprite_index: u8) -> oam::Entry {
        self.data[sprite_index as usize]
    }

    /// Iterate over all entries in the OAM array.
    pub fn iter(&self) -> Iter<'_> {
        Iter {
            array: self,
            index: 0,
        }
    }
}