Skip to content

Commit

Permalink
Move cpu_steps_taken back into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Miwgt committed Jun 13, 2024
1 parent 022a44a commit 9ffa59b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 74 deletions.
48 changes: 25 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,15 @@ async fn main() {
cpu.poll_inputs();
cpu.blarg_print();

ppu.step(cpu_cycles_taken, &mut cpu, &mut final_image, &PALETTE);
for _ in 0..cpu_cycles_taken {
ppu.step(&mut cpu, &mut final_image, &PALETTE);

// Redraw UI at 30 frames per second
if (ppu_time.elapsed().as_millis() as f32) >= TIME_PER_FRAME {
ppu_time = time::Instant::now();
// Redraw UI at 30 frames per second
if (ppu_time.elapsed().as_millis() as f32) >= TIME_PER_FRAME {
ppu_time = time::Instant::now();

// Inform about the time it took to render the frame
root_ui().label(
// Inform about the time it took to render the frame
root_ui().label(
None,
format!(
"Dots: {:?} | Frame time: {:?} | CPU Cycle: {:?} | Frame: {:?} | Frame Cycle: {:?}",
Expand All @@ -142,23 +143,24 @@ async fn main() {
ppu.get_frame_cycles()
)
.as_str(),
);
last_frame_time = time::Instant::now();

// Update Debugging Views
update_atlas_from_memory(&cpu, 16 * 24, &mut tile_atlas, &PALETTE);
update_background_from_memory(&cpu, &mut background_image, &PALETTE, false, true);
background_viewer.draw(&background_image);
tile_viewer.draw(&tile_atlas);

gb_display.draw(&final_image);
next_frame().await;
frame += 1;

// Dump memory every 3 seconds
if !WINDOWS && dump_time.elapsed().as_secs() >= 3 {
dump_time = time::Instant::now();
cpu.dump_memory();
);
last_frame_time = time::Instant::now();

// Update Debugging Views
update_atlas_from_memory(&cpu, 16 * 24, &mut tile_atlas, &PALETTE);
update_background_from_memory(&cpu, &mut background_image, &PALETTE, false, true);
background_viewer.draw(&background_image);
tile_viewer.draw(&tile_atlas);

gb_display.draw(&final_image);
next_frame().await;
frame += 1;

// Dump memory every 3 seconds
if !WINDOWS && dump_time.elapsed().as_secs() >= 3 {
dump_time = time::Instant::now();
cpu.dump_memory();
}
}
}
}
Expand Down
94 changes: 43 additions & 51 deletions src/rendering/line_rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,67 +60,59 @@ impl Ppu {
}
}

pub fn step(
&mut self,
cpu_cycles_taken: u8,
cpu: &mut CPU,
final_image: &mut Image,
palette: &[Color; 4],
) {
pub fn step(&mut self, cpu: &mut CPU, final_image: &mut Image, palette: &[Color; 4]) {
if cpu.get_lcdc_ppu_enabled() && !self.enabled {
self.frame_cycles = 0;
self.enabled = true;
}

for _ in 0..cpu_cycles_taken {
let dot = self.frame_cycles * DOTS_PER_CYCLE;
self.frame_cycles += 1;
let dot = self.frame_cycles * DOTS_PER_CYCLE;
self.frame_cycles += 1;

let ppu_mode = PpuMode::try_from(cpu.get_ppu_mode()).expect("Invalid PPU Mode");
let scanline = cpu.get_lcd_y_coordinate();
let ppu_mode = PpuMode::try_from(cpu.get_ppu_mode()).expect("Invalid PPU Mode");
let scanline = cpu.get_lcd_y_coordinate();

match ppu_mode {
PpuMode::OamScan => {
if dot % DOTS_PER_LINE == SCAN_DOTS - DOTS_PER_CYCLE {
oam_scan(&cpu);
cpu.set_ppu_mode(PpuMode::Drawing);
} else if dot % DOTS_PER_LINE >= SCAN_DOTS {
panic!("dot must be < 80 in OAM Scan Mode");
}
match ppu_mode {
PpuMode::OamScan => {
if dot % DOTS_PER_LINE == SCAN_DOTS - DOTS_PER_CYCLE {
oam_scan(&cpu);
cpu.set_ppu_mode(PpuMode::Drawing);
} else if dot % DOTS_PER_LINE >= SCAN_DOTS {
panic!("dot must be < 80 in OAM Scan Mode");
}
PpuMode::Drawing => {
// TODO Implement Variable Drawing Mode duration
if dot % DOTS_PER_LINE == SCAN_DOTS + MIN_DRAW_DOTS - DOTS_PER_CYCLE {
draw_pixels(cpu, final_image, &palette);
cpu.set_ppu_mode(PpuMode::HorizontalBlank);
} else if dot % DOTS_PER_LINE >= SCAN_DOTS + MIN_DRAW_DOTS {
panic!("dot has an invalid value");
}
}
PpuMode::Drawing => {
// TODO Implement Variable Drawing Mode duration
if dot % DOTS_PER_LINE == SCAN_DOTS + MIN_DRAW_DOTS - DOTS_PER_CYCLE {
draw_pixels(cpu, final_image, &palette);
cpu.set_ppu_mode(PpuMode::HorizontalBlank);
} else if dot % DOTS_PER_LINE >= SCAN_DOTS + MIN_DRAW_DOTS {
panic!("dot has an invalid value");
}
PpuMode::HorizontalBlank => {
if dot % DOTS_PER_LINE == DOTS_PER_LINE - DOTS_PER_CYCLE {
cpu.set_lcd_y_coordinate(scanline + 1);

// Check if in extra scanlines area
if scanline + 1 < SCANLINES_ACTUAL {
cpu.set_ppu_mode(PpuMode::OamScan);
} else {
// Set the VBlank interrupt since we are done with the frame
cpu.set_vblank_interrupt();
cpu.set_ppu_mode(PpuMode::VerticalBlank);
};
}
}
PpuMode::HorizontalBlank => {
if dot % DOTS_PER_LINE == DOTS_PER_LINE - DOTS_PER_CYCLE {
cpu.set_lcd_y_coordinate(scanline + 1);

// Check if in extra scanlines area
if scanline + 1 < SCANLINES_ACTUAL {
cpu.set_ppu_mode(PpuMode::OamScan);
} else {
// Set the VBlank interrupt since we are done with the frame
cpu.set_vblank_interrupt();
cpu.set_ppu_mode(PpuMode::VerticalBlank);
};
}
PpuMode::VerticalBlank => {
//log::info!("Dot: {}", dot % DOTS_PER_LINE);
if dot % DOTS_PER_LINE == DOTS_PER_LINE - DOTS_PER_CYCLE {
cpu.set_lcd_y_coordinate(scanline + 1);

if scanline + 1 == SCANLINES_ACTUAL + SCANLINES_EXTRA - 1 {
self.frame_cycles = 0;
cpu.set_lcd_y_coordinate(0);
cpu.set_ppu_mode(PpuMode::OamScan)
}
}
PpuMode::VerticalBlank => {
//log::info!("Dot: {}", dot % DOTS_PER_LINE);
if dot % DOTS_PER_LINE == DOTS_PER_LINE - DOTS_PER_CYCLE {
cpu.set_lcd_y_coordinate(scanline + 1);

if scanline + 1 == SCANLINES_ACTUAL + SCANLINES_EXTRA - 1 {
self.frame_cycles = 0;
cpu.set_lcd_y_coordinate(0);
cpu.set_ppu_mode(PpuMode::OamScan)
}
}
}
Expand Down

0 comments on commit 9ffa59b

Please sign in to comment.