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
impl NES
Sourcepub fn new<P: AsRef<Path>>(filename: P) -> Result<Self, CartridgeError>
pub fn new<P: AsRef<Path>>(filename: P) -> Result<Self, CartridgeError>
Creates a new NES instance from a given file path.
Sourcepub fn new_without_file() -> Self
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.
Sourcepub fn clock_for_frame(&mut self)
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.
Sourcepub fn clock(&mut self) -> Option<CPURunState>
pub fn clock(&mut self) -> Option<CPURunState>
Run the NES emulator for one CPU cycle.
This is useful for debugging and testing purposes.
Sourcepub fn pixel_buffer(&self) -> &[u8] ⓘ
pub fn pixel_buffer(&self) -> &[u8] ⓘ
Return the pixel buffer as RGB format
The size of the buffer will be TV_BUFFER_SIZE
Sourcepub fn audio_buffer(&mut self) -> Vec<f32>
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.
Sourcepub fn set_controller_state(&mut self, key: NESKey, pressed: bool)
pub fn set_controller_state(&mut self, key: NESKey, pressed: bool)
Set the state of a controller key. pressed or released.
Sourcepub fn save_state_file_name(&self, slot: u8) -> Option<String>
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.