Skip to content

Commit

Permalink
Implement oamaddr register, run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrakisk committed Dec 5, 2024
1 parent b597135 commit bd455fa
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 22 deletions.
6 changes: 5 additions & 1 deletion examples/color_test/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ fn main() {

let ppu = Rc::new(RefCell::new(PPU::new(rom.chr_rom.clone())));
let controller = Rc::new(RefCell::new(Controller::new()));
let cpu_mapper = Rc::new(RefCell::new(BasicMapper::new(rom, ppu.clone(), controller.clone())));
let cpu_mapper = Rc::new(RefCell::new(BasicMapper::new(
rom,
ppu.clone(),
controller.clone(),
)));
let mut cpu = CPU::new(cpu_mapper.clone());

cpu.reset();
Expand Down
8 changes: 6 additions & 2 deletions examples/pacman/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ fn main() {
println!("prg rom len: {}", rom.prg_rom.len());
println!("chr rom len: {}", rom.chr_rom.len());
let ppu = Rc::new(RefCell::new(PPU::new(rom.chr_rom.clone())));

let controller = Rc::new(RefCell::new(Controller::new()));
let cpu_mapper = Rc::new(RefCell::new(BasicMapper::new(rom, ppu.clone(), controller.clone())));
let cpu_mapper = Rc::new(RefCell::new(BasicMapper::new(
rom,
ppu.clone(),
controller.clone(),
)));
let mut cpu = CPU::new(cpu_mapper.clone());

cpu.reset();
Expand Down
6 changes: 5 additions & 1 deletion examples/snake/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ fn main() {
let rom = Rom::try_from(&rom_bytes).unwrap();
let ppu = Rc::new(RefCell::new(PPU::new(vec![])));
let controller = Rc::new(RefCell::new(Controller::new()));
let cpu_mapper = Rc::new(RefCell::new(BasicMapper::new(rom.clone(), ppu.clone(), controller)));
let cpu_mapper = Rc::new(RefCell::new(BasicMapper::new(
rom.clone(),
ppu.clone(),
controller,
)));
let mut cpu = CPU::new(cpu_mapper.clone());

println!("rom len: {}", rom.prg_rom.len());
Expand Down
6 changes: 2 additions & 4 deletions src/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Controller {
}
fn read(&mut self) -> ButtonState {
if self.index > 7 {
return ButtonState::PRESSED
return ButtonState::PRESSED;
}
let result = match self.strobe_mode {
StrobeMode::ON => self.status & BUTTON_A,
Expand All @@ -67,7 +67,7 @@ impl Controller {
mask = mask << self.index;
self.index += 1;
self.status & mask
},
}
};

if result == 0 {
Expand All @@ -84,7 +84,6 @@ impl Controller {
pub fn release_button(&mut self, button: u8) {
self.status = self.status & (!button);
}

}

#[cfg(test)]
Expand Down Expand Up @@ -120,5 +119,4 @@ mod test_controller {

#[test]
fn test_write() {}

}
7 changes: 3 additions & 4 deletions src/cpu/mappers/basic_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use core::panic;
use std::cell::RefCell;
use std::rc::Rc;


use crate::controller::*;
use crate::memory::*;
use crate::ppu::PPU;
use crate::rom::*;
use crate::controller::*;

const RAM_START: u16 = 0x0000;
const RAM_MIRRORS_END: u16 = 0x1FFF;
Expand Down Expand Up @@ -62,7 +61,7 @@ impl Memory for BasicMapper {
0x2000 => panic!("Control register is write-only!"),
0x2001 => panic!("Mask register is write-only!"),
0x2002 => self.ppu.borrow_mut().read_status(),
0x2003 => todo!("OAMADDR register is not implemented yet!"),
0x2003 => panic!("OAMADDR register is write only!"),
0x2004 => panic!("OAMDATA register is not implemented yet!"),
0x2005 => panic!("Scroll register is write-only!"),
0x2006 => panic!("Address register is write-only!"),
Expand Down Expand Up @@ -95,7 +94,7 @@ impl Memory for BasicMapper {
0x2000 => self.ppu.borrow_mut().write_control(data),
0x2001 => self.ppu.borrow_mut().write_mask(data),
0x2002 => panic!("Status register is read-only!"),
0x2003 => println!("OAMADDR register is not implemented yet!"),
0x2003 => self.ppu.borrow_mut().write_oam_address(data),
0x2004 => println!("OAMDATA register is not implemented yet!"),
0x2005 => println!("Scroll register is not implemented yet!"),
0x2006 => self.ppu.borrow_mut().write_address(data),
Expand Down
28 changes: 18 additions & 10 deletions src/ppu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use registers::data::Data;
use registers::write_toggle::WriteToggle;
use registers::{Register16, Register8};

use crate::ppu::frame::Frame;
use crate::ppu::registers::address::Address;
use crate::ppu::registers::control::Control;
use crate::ppu::registers::mask::Mask;
use crate::ppu::registers::status::Status;
use frame::Frame;
use registers::address::Address;
use registers::control::Control;
use registers::mask::Mask;
use registers::status::Status;

use registers::oam_address::OAMAddress;
#[rustfmt::skip]
pub static SYSTEM_PALLETE: [(u8,u8,u8); 64] = [
(0x80, 0x80, 0x80), (0x00, 0x3D, 0xA6), (0x00, 0x12, 0xB0), (0x44, 0x00, 0x96), (0xA1, 0x00, 0x5E),
Expand All @@ -33,13 +34,14 @@ pub struct PPU {
control: Control,
mask: Mask,
status: Status,
oamaddr: u16,
oamaddr: OAMAddress,
oamdata: u16,
ppuscroll: u16,
address: Address,
data: Data,
pub frame: Frame,
pub vram: [u8; 2048],
oam_ram: [u8; 256],
pallete_ram: [u8; 32],
w: WriteToggle,
pub cycles: u16,
Expand All @@ -55,13 +57,14 @@ impl PPU {
control: Control::new(0b0000_0000),
mask: Mask::new(0b0000_0000),
status: Status::new(0b1010_0000),
oamaddr: 0b0000_0000,
oamaddr: OAMAddress::new(0b0000_0000),
oamdata: 0b0000_0000,
ppuscroll: 0b0000_0000,
address: Address::new(0x0000),
data: Data::new(0b0000_0000),
frame: Frame::new(),
vram: [0; 2048],
oam_ram: [0; 256],
pallete_ram: [0; 32],
w: WriteToggle::FirstWrite,
cycles: 0,
Expand All @@ -88,6 +91,10 @@ impl PPU {
return result;
}

pub fn write_oam_address(&mut self, data: u8) {
self.oamaddr.write_u8(data);
}

pub fn write_mask(&mut self, data: u8) {
self.mask.write_u8(data)
}
Expand Down Expand Up @@ -154,13 +161,14 @@ impl PPU {
}

pub fn tick(&mut self) {
// println!("Control {:08b}", self.control.read_u8());
// println!("Control {:08b}",. self.control.read_u8());
// println!("cycle: {}, scanline: {}", self.cycles, self.scanline);
self.cycles += 1;
self.cycles += 1; // todo: move this at the end of the method
match self.cycles {
0 => (),
1..=256 => {
if self.scanline < 240 {
// todo: when cycle increment is moved, remove -1
self.render_pixel(self.cycles - 1, self.scanline);
}
}
Expand Down Expand Up @@ -235,7 +243,7 @@ mod test_ppu {
assert_eq!(0b0000_0000, ppu.mask.read_u8());
assert_eq!(0b1010_0000, ppu.status.read_u8());
assert_eq!(0b0000_0000, ppu.ppuscroll);
assert_eq!(0b0000_0000, ppu.oamaddr);
assert_eq!(0b0000_0000, ppu.oamaddr.read_u8());
assert_eq!(0x0000, ppu.address.read_u16());
assert_eq!(0b0000_0000, ppu.data.read_u8());
assert_eq!([0; 2048], ppu.vram)
Expand Down
1 change: 1 addition & 0 deletions src/ppu/registers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod address;
pub mod control;
pub mod data;
pub mod mask;
pub mod oam_address;
pub mod status;
pub mod write_toggle;

Expand Down
20 changes: 20 additions & 0 deletions src/ppu/registers/oam_address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use super::Register8;

pub struct OAMAddress {
value: u8,
}

impl OAMAddress {
pub fn new(value: u8) -> Self {
return OAMAddress { value: value };
}
}
impl Register8 for OAMAddress {
fn read_u8(&self) -> u8 {
return self.value;
}

fn write_u8(&mut self, data: u8) {
self.value = data;
}
}

0 comments on commit bd455fa

Please sign in to comment.