Skip to content

Commit

Permalink
Add profiling feature
Browse files Browse the repository at this point in the history
To avoid heavy inlining and provide more context on where time is being
spent.
  • Loading branch information
Javier-varez committed May 22, 2024
1 parent eaef908 commit e1b748f
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ members = [
exclude = [
"rusty-date",
]

[profile.release]
debug = true
3 changes: 3 additions & 0 deletions ppu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ categories = ["embedded", "gaming"]
keywords = ["embedded", "gameboy", "playdate"]
readme = "../README.md"

[features]
profile = []

[dependencies]
sm83 = { path = "../sm83", version = "0.1.0" }
tock-registers = "0.9.0"
Expand Down
13 changes: 12 additions & 1 deletion ppu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ const OBJ_OFFSET_X: usize = 8;

static_assertions::const_assert_eq!(70224, LINE_LENGTH * NUM_LINES);

#[cfg_attr(feature = "profile", inline(never))]
fn mode_for_current_cycle_count(line_cycles: Cycles, line: usize) -> Mode {
// This is a simplified implementation that considers a fixed time mode 3.
let line_cycles: usize = line_cycles.into();
Expand Down Expand Up @@ -159,7 +160,8 @@ impl Ppu {
self.mode
}

pub fn update_line_and_cycles(&mut self, cycles: Cycles) {
#[cfg_attr(feature = "profile", inline(never))]
fn update_line_and_cycles(&mut self, cycles: Cycles) {
self.cycles = self.cycles + cycles;
if self.cycles >= Cycles::new(LINE_LENGTH) {
self.cycles = self.cycles - Cycles::new(LINE_LENGTH);
Expand All @@ -171,6 +173,7 @@ impl Ppu {
}

/// Runs the PPU for the given number of cycles and then returns the PPU state
#[cfg_attr(feature = "profile", inline(never))]
pub fn step(
&mut self,
cycles: Cycles,
Expand All @@ -193,6 +196,7 @@ impl Ppu {
&self.framebuffer
}

#[cfg_attr(feature = "profile", inline(never))]
fn update_lcd_irq(&mut self) -> Interrupts {
let lyc_eq_ly = self.regs.status.read(STAT::LYC_INT_SELECT) != 0
&& self.regs.status.read(STAT::LYC_EQ_LY) != 0;
Expand All @@ -217,6 +221,7 @@ impl Ppu {
}
}

#[cfg_attr(feature = "profile", inline(never))]
fn oam_scan(&mut self) {
self.selected_oam_entries = heapless::Vec::new();

Expand All @@ -238,6 +243,7 @@ impl Ppu {
.collect();
}

#[cfg_attr(feature = "profile", inline(never))]
fn step_inner(&mut self, new_mode: Mode, render: bool) -> (Interrupts, PpuResult) {
const NO_IRQ: Interrupts = Interrupts::new();
if self.mode == new_mode {
Expand Down Expand Up @@ -265,6 +271,7 @@ impl Ppu {
(NO_IRQ, PpuResult::InProgress(self.mode))
}

#[cfg_attr(feature = "profile", inline(never))]
fn draw_line_background(&self, line: &mut [PaletteIndex; DISPLAY_WIDTH]) -> Palette {
let bg_win_enable = self.regs.lcdc.read(regs::LCDC::BG_AND_WINDOW_ENABLE) != 0;
if !bg_win_enable {
Expand Down Expand Up @@ -319,6 +326,7 @@ impl Ppu {
self.regs.bg_palette
}

#[cfg_attr(feature = "profile", inline(never))]
fn draw_line_window(&self, line: &mut [PaletteIndex; DISPLAY_WIDTH]) {
let bg_win_enable = self.regs.lcdc.read(regs::LCDC::BG_AND_WINDOW_ENABLE) != 0;
if !bg_win_enable {
Expand Down Expand Up @@ -374,6 +382,7 @@ impl Ppu {
.for_each(|(dest, palette_index)| *dest = palette_index);
}

#[cfg_attr(feature = "profile", inline(never))]
fn draw_line_objects(
&self,
bg_line: &[PaletteIndex; DISPLAY_WIDTH],
Expand Down Expand Up @@ -445,6 +454,7 @@ impl Ppu {
}
}

#[cfg_attr(feature = "profile", inline(never))]
fn draw_line(&mut self) {
if self.regs.lcdc.read(regs::LCDC::ENABLE) == 0 {
return;
Expand Down Expand Up @@ -478,6 +488,7 @@ impl Ppu {
}
}

#[cfg_attr(feature = "profile", inline(never))]
fn update_registers(&mut self) {
let line = self.line as u8;
self.regs.ly = line;
Expand Down
2 changes: 2 additions & 0 deletions ppu/src/oam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,13 @@ impl Oam {
&*self.objects
}

#[cfg_attr(feature = "profile", inline(never))]
pub fn read(&self, address: sm83::memory::Address) -> u8 {
let (object_idx, object_member_offset) = Self::cpu_addr_to_object_addr(address);
self.objects[object_idx].read(object_member_offset)
}

#[cfg_attr(feature = "profile", inline(never))]
pub fn write(&mut self, address: sm83::memory::Address, value: u8) {
let (object_idx, object_member_offset) = Self::cpu_addr_to_object_addr(address);
self.objects[object_idx].write(object_member_offset, value);
Expand Down
2 changes: 2 additions & 0 deletions ppu/src/regs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ impl Registers {
.set((self.status.get() & RO_BITS) | (new_val & !RO_BITS));
}

#[cfg_attr(feature = "profile", inline(never))]
pub fn read(&self, address: sm83::memory::Address) -> u8 {
match address {
0xFF40 => self.lcdc.get(),
Expand All @@ -179,6 +180,7 @@ impl Registers {
}
}

#[cfg_attr(feature = "profile", inline(never))]
pub fn write(&mut self, address: sm83::memory::Address, value: u8) {
match address {
0xFF40 => self.lcdc.set(value),
Expand Down
6 changes: 6 additions & 0 deletions ppu/src/vram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ impl TileMap {
})
}

#[cfg_attr(feature = "profile", inline(never))]
pub fn line(&self, line: usize) -> &[TileIndex; TILE_MAP_WIDTH] {
let line = (line / TILE_HEIGHT) % TILE_MAP_HEIGHT;
&self.0[line]
Expand Down Expand Up @@ -288,13 +289,15 @@ impl Vram {
(tile_map_idx, tile_map_address)
}

#[cfg_attr(feature = "profile", inline(never))]
pub(crate) fn get_bg_tile_map(&self, map: crate::regs::LCDC::BG_TILE_MAP::Value) -> &TileMap {
match map {
crate::regs::LCDC::BG_TILE_MAP::Value::HighMap => &self.0.tile_maps[1],
crate::regs::LCDC::BG_TILE_MAP::Value::LowMap => &self.0.tile_maps[0],
}
}

#[cfg_attr(feature = "profile", inline(never))]
pub(crate) fn get_win_tile_map(
&self,
map: crate::regs::LCDC::WINDOW_TILE_MAP::Value,
Expand All @@ -305,6 +308,7 @@ impl Vram {
}
}

#[cfg_attr(feature = "profile", inline(never))]
pub(crate) fn get_tile(
&self,
index: TileIndex,
Expand All @@ -319,6 +323,7 @@ impl Vram {
self.0.tile_blocks[block].get_tile(index)
}

#[cfg_attr(feature = "profile", inline(never))]
pub fn read(&self, address: sm83::memory::Address) -> u8 {
if address < 0x9800 {
let (blk_idx, blk_address) = Self::vram_address_to_block_address(address);
Expand All @@ -329,6 +334,7 @@ impl Vram {
}
}

#[cfg_attr(feature = "profile", inline(never))]
pub fn write(&mut self, address: sm83::memory::Address, value: u8) {
if address < 0x9800 {
let (blk_idx, blk_address) = Self::vram_address_to_block_address(address);
Expand Down
3 changes: 3 additions & 0 deletions sm83/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ categories = ["embedded", "gaming"]
keywords = ["embedded", "gameboy", "playdate"]
readme = "../README.md"

[features]
profile = []

[dependencies]
sm83_decoder_macros = { path = "../sm83_decoder_macros" }

Expand Down
Loading

0 comments on commit e1b748f

Please sign in to comment.