Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
joajfreitas committed Jul 28, 2024
1 parent 96f6fb2 commit 75a745e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
15 changes: 12 additions & 3 deletions fpt/src/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,24 @@ impl Bus {
.cartridge
.borrow_mut()
.write(address, value);
} else if map::IO_REGISTERS.contains(&address)
|| map::VRAM.contains(&address)
} else if map::IO_REGISTERS.contains(&address) {
self.memory_mut().mem[address as Address] = value;
if address == 0xFF46 {
// dma transfer takes time, we do it instantaneously
let oam_data =
self.copy_range(value as usize * 0x100..value as usize * 0x100 + 0x0A0);
self.clone_from_slice(map::OAM, &oam_data)
}
} else if map::VRAM.contains(&address)
|| map::HRAM.contains(&address)
|| map::WRAM.contains(&address)
|| map::NOT_USABLE2.contains(&address)
|| map::OAM.contains(&address)
|| address == map::IE
{
self.memory_mut().mem[address as Address] = value;
} else if map::OAM.contains(&address) {
println!("{}:{}", address, value);
self.memory_mut().mem[address as Address] = value;
} else if map::NOT_USABLE1.contains(&address) {
self.memory_mut().mem[address - 0x2000 as Address] = value;
} else {
Expand Down
27 changes: 25 additions & 2 deletions fpt/src/ppu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ use std::fmt::{Display, Formatter};

use tile::VRamContents;

use crate::{bw, memory::Bus};
use crate::{bw, memory::map, memory::Bus};

mod sprite;
pub mod tile;

use sprite::Sprite;

pub const WIDTH: usize = 160;
pub const HEIGHT: usize = 144;
pub type Frame = [u8; WIDTH * HEIGHT]; // TODO: wasteful, each pixel is 2 bits only
Expand All @@ -19,6 +22,7 @@ pub struct Ppu {
frame_counter: u32,
mode: Mode,
tilemap: VRamContents,
sprites: Vec<Sprite>,
}

#[repr(u8)]
Expand Down Expand Up @@ -62,6 +66,7 @@ impl Ppu {
frame_counter: 0,
mode: Mode::OamScan,
tilemap: VRamContents::default(),
sprites: Vec::new(),
}
}

Expand All @@ -78,6 +83,10 @@ impl Ppu {
fn oam_scan(&mut self) {
if self.dots_this_frame % 456 == (80 - 1) {
self.tilemap = self.bus.with_vram(VRamContents::load);
let oam = self.bus.copy_range(map::OAM);
self.sprites = (0..40)
.map(|sprite_index| Sprite::load(&oam[sprite_index * 4..(sprite_index * 4 + 4)]))
.collect::<Vec<Sprite>>();
self.set_mode(Mode::PixelTransfer);
}
}
Expand All @@ -102,7 +111,21 @@ impl Ppu {
let tile = self
.tilemap
.get_tile(tile_data_address as usize, bw::test_bit8::<4>(lcdc));
let pixel = tile.get_pixel(yy % 8, xx % 8);
let mut pixel = tile.get_pixel(yy % 8, xx % 8);

for sprite in self.sprites.iter() {
if (x as u8 > (sprite.x - 8) && (x as u8) < sprite.x)
&& (y as u8 > sprite.y - 8 && (y as u8) < sprite.y)
{
let tile = self.tilemap.get_tile(sprite.tile_index as usize, true);
let tile_x: usize = dbg!(sprite.x as usize - x);
let tile_y: usize = dbg!(sprite.y as usize - y);
let bit_index = tile_y * 8 + tile_x;
pixel = dbg!((tile.bytes[2 * tile_y] >> tile_x) & 1);
//pixel = tile.bytes[(dbg!(8*(dbg!(sprite.y) as i16 - dbg!(y) as i16) + dbg!(sprite.x) as i16 - dbg!(x) as i16)) as usize] ;
println!("found sprite");
}
}
self.frame[WIDTH * y + x] = pixel;
}

Expand Down
43 changes: 43 additions & 0 deletions fpt/src/ppu/sprite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::bw::test_bit8;

#[derive(Debug)]
struct Flags {
priority: bool,
y_flip: bool,
x_flip: bool,
dmg_palette: bool,
bank: bool,
cgb_palette: bool,
}

impl Flags {
pub fn from(memory: u8) -> Flags {
Flags {
priority: test_bit8::<0>(memory),
x_flip: test_bit8::<1>(memory),
y_flip: test_bit8::<2>(memory),
dmg_palette: test_bit8::<3>(memory),
bank: test_bit8::<4>(memory),
cgb_palette: test_bit8::<5>(memory),
}
}
}

#[derive(Debug)]
pub struct Sprite {
pub y: u8,
pub x: u8,
pub tile_index: u8,
flags: Flags,
}

impl Sprite {
pub fn load(memory: &[u8]) -> Sprite {
Sprite {
y: memory[0],
x: memory[1],
tile_index: memory[2],
flags: Flags::from(memory[3]),
}
}
}

0 comments on commit 75a745e

Please sign in to comment.