Skip to content

Commit

Permalink
More accurate timing emulation by using decimal values
Browse files Browse the repository at this point in the history
Using counter allows us to use decimal values for the
`CPU_CYCLES_PER_FRAME` number.

Signed-off-by: Amjad Alsharafi <[email protected]>
  • Loading branch information
Amjad50 committed Dec 26, 2024
1 parent ed9c093 commit cc786f2
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions plastic_core/src/nes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ pub struct NES {

/// CPU and containing all components through the `CPUBus`.
cpu: CPU6502<CPUBus>,

frame_counter: f32,
}

impl NES {
Expand Down Expand Up @@ -287,7 +289,11 @@ impl NES {

cpu.reset();

Self { cartridge, cpu }
Self {
cartridge,
cpu,
frame_counter: 0.,
}
}

/// Reset the NES emulator using the same cartridge loaded already.
Expand All @@ -310,12 +316,14 @@ impl NES {
return;
}

const N: usize = 29780; // number of CPU cycles per loop, one full frame
const CPU_CYCLES_PER_FRAME: f32 = 29780.5; // number of CPU cycles per loop, one full frame

for _ in 0..N {
self.cpu.bus_mut().apu.clock();
self.frame_counter += CPU_CYCLES_PER_FRAME;

while self.frame_counter >= 0. {
self.frame_counter -= 1.;
self.cpu.run_next();
self.cpu.bus_mut().apu.clock();
{
let ppu = &mut self.cpu.bus_mut().ppu;
ppu.clock();
Expand Down

0 comments on commit cc786f2

Please sign in to comment.