NES

Struct NES 

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

The main NES emulator struct, containing all components and what is actually doing the emulation.

§Example

use plastic_core::NES;

fn main() {
   let mut nes = NES::new("path/to/rom-file.nes").unwrap();
    
   loop {
       nes.clock_for_frame();

       let pixel_buffer = nes.pixel_buffer();
       display(&pixel_buffer);

       let audio_buffer = nes.audio_buffer();
       play_audio(&audio_buffer);
   }
}

Implementations§

Source§

impl NES

Source

pub fn new<P: AsRef<Path>>(filename: P) -> Result<Self, CartridgeError>

Creates a new NES instance from a given file path.

Source

pub fn new_without_file() -> Self

Creates a new NES instance without loading a cartridge from a file.

Returns a new NES instance with an empty cartridge.

Do note that running NES::clock_for_frame or NES::clock will not do anything if the cartridge is empty.

Source

pub fn reset(&mut self)

Reset the NES emulator using the same cartridge loaded already.

Source

pub fn clock_for_frame(&mut self)

Run the NES emulator for one video frame, which is equal to 29780 CPU cycles.

This is the main function to run the emulator, call this once, and then render and play audio.

Source

pub fn clock(&mut self) -> Option<CPURunState>

Run the NES emulator for one CPU cycle.

This is useful for debugging and testing purposes.

Source

pub fn pixel_buffer(&self) -> &[u8]

Return the pixel buffer as RGB format

The size of the buffer will be TV_BUFFER_SIZE

Source

pub fn audio_buffer(&mut self) -> Vec<f32>

Take and return the audio buffer as f32 format stereo (2 channels)

Take here means that if you call the function again, it will return an empty buffer until the emulator runs again.

The emulator keeps accumulating audio samples until this function is called, so its better to call this function even if audio isn’t needed in order to free up space.

Source

pub fn is_empty(&self) -> bool

Check if there is no cartridge loaded in the emulator.

Source

pub fn set_controller_state(&mut self, key: NESKey, pressed: bool)

Set the state of a controller key. pressed or released.

Source

pub fn save_state_file_name(&self, slot: u8) -> Option<String>

Get the name of the save state file that can be associated with the current cartridge.

This is just a helper function, and the emulator implementation at [save_state] doesn’t use it.

Just a convenience.

Source

pub fn save_state<W: Write>(&self, writer: W) -> Result<(), SaveError>

Save the current state of the emulator to a writer.

Source

pub fn load_state<R: Read>(&mut self, reader: R) -> Result<(), SaveError>

Load the state of the emulator from a reader.

Auto Trait Implementations§

§

impl !Freeze for NES

§

impl !RefUnwindSafe for NES

§

impl !Send for NES

§

impl !Sync for NES

§

impl Unpin for NES

§

impl !UnwindSafe for NES

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> 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, 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.