Skip to content

Commit

Permalink
Code indentation and redundant code removal
Browse files Browse the repository at this point in the history
This commit is a result of a SCR (self code review).
  • Loading branch information
alloncm committed Sep 3, 2021
1 parent dcd3e63 commit b71436e
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 32 deletions.
27 changes: 4 additions & 23 deletions lib_gb/src/machine/gameboy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use crate::{
cpu::gb_cpu::GbCpu,
keypad::{joypad::Joypad, joypad_provider::JoypadProvider, joypad_register_updater},
mmu::{carts::mbc::Mbc, gb_mmu::{GbMmu, BOOT_ROM_SIZE}, memory::Memory},
ppu::{gb_ppu::CYCLES_PER_FRAME, gfx_device::GfxDevice}, utils::bit_masks::BIT_7_MASK
ppu::{gb_ppu::CYCLES_PER_FRAME, gfx_device::GfxDevice}
};

use super::interrupts_handler::InterruptsHandler;
use std::boxed::Box;
use log::debug;
Expand All @@ -14,9 +13,7 @@ use log::debug;
pub struct GameBoy<'a, JP: JoypadProvider, AD:AudioDevice, GFX:GfxDevice> {
cpu: GbCpu,
mmu: GbMmu::<'a, AD, GFX>,
opcode_resolver:OpcodeResolver::<GbMmu::<'a, AD, GFX>>,
interrupts_handler:InterruptsHandler,
cycles_counter:u32,
joypad_provider: JP
}

Expand All @@ -27,7 +24,6 @@ impl<'a, JP:JoypadProvider, AD:AudioDevice, GFX:GfxDevice> GameBoy<'a, JP, AD, G
cpu:GbCpu::default(),
mmu:GbMmu::new_with_bootrom(mbc, boot_rom, GbApu::new(audio_device), gfx_device),
interrupts_handler: InterruptsHandler::default(),
cycles_counter:0,
joypad_provider: joypad_provider
}
}
Expand All @@ -46,17 +42,16 @@ impl<'a, JP:JoypadProvider, AD:AudioDevice, GFX:GfxDevice> GameBoy<'a, JP, AD, G
cpu:cpu,
mmu:GbMmu::new(mbc, GbApu::new(audio_device), gfx_device),
interrupts_handler: InterruptsHandler::default(),
cycles_counter:0,
joypad_provider: joypad_provider,
}
}

pub fn cycle_frame(&mut self){
let mut joypad = Joypad::default();

let mut last_ppu_power_state:bool = (self.mmu.io_components.ppu.lcd_control & BIT_7_MASK) != 0;
let mut cycles_counter = 0;

while self.cycles_counter < CYCLES_PER_FRAME{
while cycles_counter < CYCLES_PER_FRAME{
self.joypad_provider.provide(&mut joypad);
joypad_register_updater::update_joypad_registers(&joypad, &mut self.mmu);

Expand All @@ -73,22 +68,8 @@ impl<'a, JP:JoypadProvider, AD:AudioDevice, GFX:GfxDevice> GameBoy<'a, JP, AD, G
if interrupt_cycles != 0{
self.mmu.cycle(interrupt_cycles);
}

let iter_total_cycles= cpu_cycles_passed as u32 + interrupt_cycles as u32;


//In case the ppu just turned I want to keep it sync with the actual screen and thats why Im reseting the loop to finish
//the frame when the ppu finishes the frame
if !last_ppu_power_state && (self.mmu.io_components.ppu.lcd_control & BIT_7_MASK) != 0{
self.cycles_counter = 0;
}

self.cycles_counter += iter_total_cycles;
last_ppu_power_state = (self.mmu.io_components.ppu.lcd_control & BIT_7_MASK) != 0;
}

if self.cycles_counter >= CYCLES_PER_FRAME{
self.cycles_counter -= CYCLES_PER_FRAME;
cycles_counter += cpu_cycles_passed as u32 + interrupt_cycles as u32;
}
}

Expand Down
7 changes: 5 additions & 2 deletions lib_gb/src/mmu/io_components.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::{apu::{audio_device::AudioDevice, gb_apu::GbApu, set_nr11, set_nr12, set_nr13}, ppu::{fifo::fifo_ppu::FifoPpu, fifo::fifo_register_updater::*, gfx_device::GfxDevice}, timer::timer_register_updater::*, utils::memory_registers::*};
use crate::apu::*;
use crate::{apu::{*,audio_device::AudioDevice, gb_apu::GbApu},
ppu::{fifo::fifo_ppu::FifoPpu, fifo::fifo_register_updater::*, gfx_device::GfxDevice},
timer::timer_register_updater::*,
utils::memory_registers::*
};
use crate::timer::gb_timer::GbTimer;
use super::{access_bus::AccessBus, memory::*, oam_dma_transfer::OamDmaTransfer, ram::Ram};
use super::io_ports::*;
Expand Down
1 change: 0 additions & 1 deletion lib_gb/src/ppu/fifo/bg_fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{mmu::vram::VRam, utils::{bit_masks::*, vec2::Vec2}};

use super::fetching_state::FethcingState;

pub struct BGFetcher{
Expand Down
5 changes: 2 additions & 3 deletions lib_gb/src/ppu/fifo/fifo_ppu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ impl<GFX:GfxDevice> FifoPpu<GFX>{
pub fn turn_off(&mut self){
self.screen_buffer_index = 0;
self.t_cycles_passed = 0;
unsafe{
std::ptr::write_bytes(self.screen_buffer.as_mut_ptr(), 0xFF, self.screen_buffer.len());
}
//This is an expensive operation!
unsafe{std::ptr::write_bytes(self.screen_buffer.as_mut_ptr(), 0xFF, self.screen_buffer.len())};
self.gfx_device.swap_buffer(&self.screen_buffer);
self.state = PpuState::Hblank;
self.ly_register = 0;
Expand Down
4 changes: 1 addition & 3 deletions lib_gb/src/ppu/fifo/sprite_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ pub struct SpriteFetcher{
impl SpriteFetcher{
pub fn new()->Self{
let oam_entries = {
let mut data: [MaybeUninit<SpriteAttribute>; 10] = unsafe{
MaybeUninit::uninit().assume_init()
};
let mut data: [MaybeUninit<SpriteAttribute>; 10] = unsafe{MaybeUninit::uninit().assume_init()};

for elem in &mut data[..]{
*elem = MaybeUninit::new(SpriteAttribute::new(0, 0, 0, 0));
Expand Down

0 comments on commit b71436e

Please sign in to comment.