Skip to content

Commit

Permalink
Implement first version of Rom
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrakisk committed Sep 30, 2024
1 parent d930926 commit 9e9f3de
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 29 deletions.
39 changes: 10 additions & 29 deletions examples/snake/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use LochNES::bus::*;
use LochNES::cpu::*;

use LochNES::rom::*;
use rand::Rng;

use sdl2::event::Event;
Expand All @@ -10,32 +10,9 @@ use sdl2::pixels::PixelFormatEnum;
use sdl2::EventPump;

use std::sync::{Arc, Mutex};

use std::fs::read;
use std::path::PathBuf;
fn main() {
let game = vec![
0x20, 0x06, 0x06, 0x20, 0x38, 0x06, 0x20, 0x0d, 0x06, 0x20, 0x2a, 0x06, 0x60, 0xa9, 0x02,
0x85, 0x02, 0xa9, 0x04, 0x85, 0x03, 0xa9, 0x11, 0x85, 0x10, 0xa9, 0x10, 0x85, 0x12, 0xa9,
0x0f, 0x85, 0x14, 0xa9, 0x04, 0x85, 0x11, 0x85, 0x13, 0x85, 0x15, 0x60, 0xa5, 0xfe, 0x85,
0x00, 0xa5, 0xfe, 0x29, 0x03, 0x18, 0x69, 0x02, 0x85, 0x01, 0x60, 0x20, 0x4d, 0x06, 0x20,
0x8d, 0x06, 0x20, 0xc3, 0x06, 0x20, 0x19, 0x07, 0x20, 0x20, 0x07, 0x20, 0x2d, 0x07, 0x4c,
0x38, 0x06, 0xa5, 0xff, 0xc9, 0x77, 0xf0, 0x0d, 0xc9, 0x64, 0xf0, 0x14, 0xc9, 0x73, 0xf0,
0x1b, 0xc9, 0x61, 0xf0, 0x22, 0x60, 0xa9, 0x04, 0x24, 0x02, 0xd0, 0x26, 0xa9, 0x01, 0x85,
0x02, 0x60, 0xa9, 0x08, 0x24, 0x02, 0xd0, 0x1b, 0xa9, 0x02, 0x85, 0x02, 0x60, 0xa9, 0x01,
0x24, 0x02, 0xd0, 0x10, 0xa9, 0x04, 0x85, 0x02, 0x60, 0xa9, 0x02, 0x24, 0x02, 0xd0, 0x05,
0xa9, 0x08, 0x85, 0x02, 0x60, 0x60, 0x20, 0x94, 0x06, 0x20, 0xa8, 0x06, 0x60, 0xa5, 0x00,
0xc5, 0x10, 0xd0, 0x0d, 0xa5, 0x01, 0xc5, 0x11, 0xd0, 0x07, 0xe6, 0x03, 0xe6, 0x03, 0x20,
0x2a, 0x06, 0x60, 0xa2, 0x02, 0xb5, 0x10, 0xc5, 0x10, 0xd0, 0x06, 0xb5, 0x11, 0xc5, 0x11,
0xf0, 0x09, 0xe8, 0xe8, 0xe4, 0x03, 0xf0, 0x06, 0x4c, 0xaa, 0x06, 0x4c, 0x35, 0x07, 0x60,
0xa6, 0x03, 0xca, 0x8a, 0xb5, 0x10, 0x95, 0x12, 0xca, 0x10, 0xf9, 0xa5, 0x02, 0x4a, 0xb0,
0x09, 0x4a, 0xb0, 0x19, 0x4a, 0xb0, 0x1f, 0x4a, 0xb0, 0x2f, 0xa5, 0x10, 0x38, 0xe9, 0x20,
0x85, 0x10, 0x90, 0x01, 0x60, 0xc6, 0x11, 0xa9, 0x01, 0xc5, 0x11, 0xf0, 0x28, 0x60, 0xe6,
0x10, 0xa9, 0x1f, 0x24, 0x10, 0xf0, 0x1f, 0x60, 0xa5, 0x10, 0x18, 0x69, 0x20, 0x85, 0x10,
0xb0, 0x01, 0x60, 0xe6, 0x11, 0xa9, 0x06, 0xc5, 0x11, 0xf0, 0x0c, 0x60, 0xc6, 0x10, 0xa5,
0x10, 0x29, 0x1f, 0xc9, 0x1f, 0xf0, 0x01, 0x60, 0x4c, 0x35, 0x07, 0xa0, 0x00, 0xa5, 0xfe,
0x91, 0x00, 0x60, 0xa6, 0x03, 0xa9, 0x00, 0x81, 0x10, 0xa2, 0x00, 0xa9, 0x01, 0x81, 0x10,
0x60, 0xa2, 0x00, 0xea, 0xea, 0xca, 0xd0, 0xfb, 0x60,
];

let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let window = video_subsystem
Expand All @@ -56,11 +33,15 @@ fn main() {
let mut screen_state = [0 as u8; 32 * 3 * 32];
let mut rng = rand::thread_rng();

let rom_bytes = read(PathBuf::from("examples/test_rom/snake.nes")).unwrap();
let rom = Rom::try_from(&rom_bytes).unwrap();
let bus = Bus::new();

let mut cpu = CPU::new(Arc::new(Mutex::new(bus)));

cpu.program_counter = 0x0600;
cpu.load(game);
println!("rom len: {}", rom.prg_rom.len());
cpu.load((rom.prg_rom));
cpu.reset();

cpu.run_with_callback(move |cpu| {
handle_user_input(cpu, &mut event_pump);
Expand Down Expand Up @@ -144,4 +125,4 @@ fn read_screen_state(cpu: &CPU, frame: &mut [u8; 32 * 3 * 32]) -> bool {
frame_idx += 3;
}
update
}
}
Binary file added examples/snake/snake.nes
Binary file not shown.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod bus;
pub mod cpu;
pub mod rom;
76 changes: 76 additions & 0 deletions src/rom/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#[derive(Debug, Clone, PartialEq)]
pub enum Mirroring {
VERTICAL,
HORIZONTAL,
FOUR_SCREEN
}

#[derive(Debug, Clone, PartialEq)]
pub struct Rom {
pub prg_rom: Vec<u8>,
pub chr_rom: Vec<u8>,
pub mapper: u8,
pub screen_mirroring: Mirroring,
}

impl Rom {
pub fn new(prg_rom: Vec<u8>, chr_rom: Vec<u8>, mapper: u8, screen_mirroring: Mirroring)-> Self {
Rom {
prg_rom: prg_rom,
chr_rom: chr_rom,
mapper: mapper,
screen_mirroring: screen_mirroring
}
}
}

impl TryFrom<&Vec<u8>> for Rom {
type Error = String;

fn try_from(raw: &Vec<u8>) -> Result<Self, Self::Error> {
if &raw[0..4] != vec![0x4E, 0x45, 0x53, 0x1A] {
return Err("File is not in iNES file format".to_string());
}

let mapper = (raw[7] & 0b1111_0000) | (raw[6] >> 4);

let ines_version = (raw[7] >> 2) & 0b11;

if ines_version != 0 {
return Err("Nes2.0 format is not supported".to_string());
}

let is_mirroring_four_screen = raw[6] & 0b1000 != 0;
let is_mirroring_vertical = raw[6] & 0b1 != 0;

let mirroring = match (is_mirroring_four_screen, is_mirroring_vertical) {
(true, _) => Mirroring::FOUR_SCREEN,
(false, true) => Mirroring::VERTICAL,
(false, false) => Mirroring::HORIZONTAL
};

println!("Dump file size: {}", raw.len());
const PRG_ROM_PAGE_BYTES: usize = 16384;
let prg_rom_size = raw[4] as usize * PRG_ROM_PAGE_BYTES;
println!("prg rom size: {}", prg_rom_size);

const CHR_ROM_PAGE_BYTES: usize = 8192;
let chr_rom_size = raw[5] as usize * CHR_ROM_PAGE_BYTES;

let has_trainer = raw[6] & 0b100 != 0;

let prg_rom_start = match has_trainer {
true => 528,
false => 16
};

let chr_rom_start = prg_rom_start + prg_rom_size;

Ok(Rom {
prg_rom: raw[prg_rom_start..(prg_rom_start + prg_rom_size)].to_vec(),
chr_rom: raw[chr_rom_start..(chr_rom_start + chr_rom_size )].to_vec(),
mapper: mapper,
screen_mirroring: mirroring
})
}
}

0 comments on commit 9e9f3de

Please sign in to comment.