Struct Nes

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

NES emulator.

use std::fs::File;
use std::io::Read;
use std::time::Duration;
use nes_rust::Nes;
use nes_rust::rom::Rom;
use nes_rust::default_input::DefaultInput;
use nes_rust::default_audio::DefaultAudio;
use nes_rust::default_display::DefaultDisplay;

let input = Box::new(DefaultInput::new());
let display = Box::new(DefaultDisplay::new());
let audio = Box::new(DefaultAudio::new());
let mut nes = Nes::new(input, display, audio);

// Load and set Rom from rom image binary
let filename = &args[1];
let mut file = File::open(filename)?;
let mut contents = vec![];
file.read_to_end(&mut contents)?;
let rom = Rom::new(contents);
nes.set_rom(rom);

// Go!
nes.bootup();
let mut rgba_pixels = [256 * 240 * 4];
loop {
  nes.step_frame();
  nes.copy_pixels(rgba_pixels);
  // Render rgba_pixels
  // @TODO: Audio buffer sample code is T.B.D.
  // Adjust sleep time for your platform
  std::thread::sleep(Duration::from_millis(1));
}

Implementations§

Source§

impl Nes

Source

pub fn new( input: Box<dyn Input>, display: Box<dyn Display>, audio: Box<dyn Audio>, ) -> Self

Creates a new Nes. You need to pass input::Input, display::Display, and audio::Audio traits for your platform specific Input/Output.

§Arguments
  • input For pad input
  • display For screen output
  • audio For audio output
Source

pub fn set_rom(&mut self, rom: Rom)

Sets up NES rom

§Arguments
  • rom
Source

pub fn bootup(&mut self)

Boots up

Source

pub fn reset(&mut self)

Resets

Source

pub fn step(&mut self)

Executes a CPU cycle

Source

pub fn step_frame(&mut self)

Executes a PPU (screen refresh) frame

Source

pub fn copy_pixels(&self, pixels: &mut [u8])

Copies RGB pixels of screen to passed pixels. The length and result should be specific to display passed via the constructor.

§Arguments
  • pixels
Source

pub fn copy_sample_buffer(&mut self, buffer: &mut [f32])

Copies audio buffer to passed buffer. The length and result should be specific to audio passed via the constructor.

§Arguments
  • buffer
Source

pub fn press_button(&mut self, button: Button)

Presses a pad button

§Arguments
  • button
Source

pub fn release_button(&mut self, button: Button)

Releases a pad button

§Arguments
  • buffer

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.