Skip to content

Commit

Permalink
nintendo logo is showing
Browse files Browse the repository at this point in the history
  • Loading branch information
pcasaretto committed Apr 16, 2024
1 parent 71a575c commit 7c4f33a
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 17 deletions.
42 changes: 42 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ edition = "2021"
env_logger = "0.11.3"
log = "0.4.21"
rand = "0.8.5"
sdl2 = "0.36.0"
serde = {version = "1.0.197", features = ["derive"]}
serde_json = "1.0.115"
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
with pkgs;
{
devShells.default = mkShell {
buildInputs = [ rustToolchain ];
buildInputs = [ rustToolchain SDL2 ];
};
}
);
Expand Down
52 changes: 37 additions & 15 deletions src/gameboy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::cpu::{Register16bTarget, CPU};
use crate::instructions;
use crate::memory::MemoryBus;
use crate::opcode_info::{OpcodeInfo, OperandInformation};
use crate::ppu::PPU;
use crate::{instructions, ppu};
use std::time::Instant;

pub struct Gameboy<'a> {
Expand Down Expand Up @@ -45,36 +46,47 @@ impl<'a> Gameboy<'a> {
pub fn run(&mut self, cartridge: &'a [u8; 0x200000]) {
self.bus.cartridge_rom = cartridge;

// load first 0x8000 bytes of cartridge into memory
// self.bus.memory[..0x8000].copy_from_slice(&self.cartridge[..0x8000]);
let sdl_context = sdl2::init().unwrap();

loop {
// if self.cpu.registers.get_u16(Register16bTarget::PC) >= 0x100 {
// break;
// }
let mut ppu = PPU::new(&sdl_context);

let mut event_pump = sdl_context.event_pump().unwrap();

'running: loop {
let before = Instant::now();
self.run_frame();
for event in event_pump.poll_iter() {
match event {
sdl2::event::Event::Quit { .. }
| sdl2::event::Event::KeyDown {
keycode: Some(sdl2::keyboard::Keycode::Escape),
..
} => break 'running,
_ => {}
}
}

self.run_frame(&mut ppu);
let elapsed = before.elapsed();
let fps = 1.0 / elapsed.as_secs_f64();
println!("FPS: {:.2?}", fps);
ppu.draw();
}
}

fn run_frame(&mut self) {
fn run_frame(&mut self, ppu: &mut PPU) {
const MAX_TICKS: u64 = 69905 * 4;
// frame
{
let mut ticks: u64 = 0;
while ticks < MAX_TICKS {
log::debug!("{:?}", self.cpu.registers);
// if self.cpu.registers.get_u16(Register16bTarget::PC) >= 0x100 {
// break;
// }
// if self.cpu.registers.get_u16(Register16bTarget::PC) >= 0x100 {
ticks += self.run_next_instruction();

self.handle_interrupts();
self.serial_comm();
self.update_graphics(ticks);
self.update_graphics(ticks, ppu);
}
}
}
Expand Down Expand Up @@ -169,13 +181,23 @@ impl<'a> Gameboy<'a> {
self.bus.read_byte(new_address)
}

fn update_graphics(&mut self, ticks: u64) {
fn update_graphics(&mut self, ticks: u64, ppu: &mut PPU) {
self.scanline_counter += ticks;
if self.scanline_counter >= 456 * 4 {
self.scanline_counter = 0;
// self.update_scanline();
self.bus
.write_byte(0xFF44, self.bus.read_byte(0xFF44).wrapping_add(1));
let mut current_scanline = self.bus.read_byte(0xFF44);
if current_scanline == 144 {
// RequestInterupt(0);
}
if current_scanline > 153 {
current_scanline = 0;
} else {
current_scanline += 1;
}
// TODO: writing directly to memory
self.bus.memory[0xFF44] = current_scanline;
ppu.update(self, current_scanline);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod gameboy;
pub mod instructions;
pub mod memory;
pub mod opcode_info;
pub mod ppu;
10 changes: 9 additions & 1 deletion src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ impl<'a> MemoryBus<'a> {
}

pub fn write_byte(&mut self, address: u16, value: u8) {
log::debug!("Writing {:02X} to {:04X}", value, address);
log::info!("Writing {:02X} to {:04X}", value, address);
match address {
0xFF46 => {
// DMA transfer
let start_address = (value as u16) << 8;
for i in 0..0xA0 {
let byte = self.memory[(start_address + i) as usize];
self.memory[0xFE00 + i as usize] = byte;
}
}
0xFF50 if self.boot_rom_enabled => {
log::info!("Disabling boot ROM");
self.boot_rom_enabled = false;
Expand Down
Loading

0 comments on commit 7c4f33a

Please sign in to comment.