From d7bd8a6c2420517fa53e7d4dee1316a996795755 Mon Sep 17 00:00:00 2001 From: Lauchmelder Date: Tue, 23 Aug 2022 19:40:48 +0200 Subject: [PATCH] added basic cartridge functions --- roms/nestest.nes | Bin 0 -> 24592 bytes src/bus.rs | 11 +++++++++- src/cartridge.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++ src/cpu.rs | 5 ++++- src/main.rs | 1 + 5 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 roms/nestest.nes create mode 100644 src/cartridge.rs diff --git a/roms/nestest.nes b/roms/nestest.nes new file mode 100644 index 0000000000000000000000000000000000000000..fc2a88c36db9c5c2de9cca33be0e446e12007f17 GIT binary patch literal 24592 zcmeHue|%KcweOzHWCBTq0R%?*;jy5C6&xJYs1XJP5-`rPD_v}$T=9c&FbLGf6qP<_?(wPJv(V_PI7KIVSc-e+gdOv1hI z)6f0yB_wn9THm$TUVH7e*V^Zt;hOo2&vZCg-%r)Cw?mPGdgh6i0?pGaJ~(S+TBAd2 zETmtsbnOwhrZe%87!J5d>o9w^n#cLTs)B_2akrwP5k9@j=9VBGyR4H{&Az%!@C%wCu7ubFZ2=e}Ne6dA69MCggg5+B48F{yq2me3-CN!NCoX$^Ls20hSs z==~1!^YmfSf)qt*cltx2oDV}lhok`_UMF%bK_Xkto(}U^D1#pjKhvgZ*WbNLTe@&k;_?l*Imn()h}JQJf9t2S-*T)E_3m^r8RfvvgXv( z+j4qrHL_4-wpYipOUgt4NJaG<7Pwrkd1uC0M@$*+>TScqM& zEnIca(v|hgwAJCYXJ^=dwiMS~8_cz)YK}6)TEWHr7;~=@U9>5y!!@gyE!TYW=hjVC zYdi0%JcSDv&&?fSan-fCjO(lBNR&QT}j@_u*!!j zk&tB^2e6S2{jUFJ(T6@VCeVSumN9`v=kD(?k2mdap8vr)`(wY~{6SrFT_B*d&1|!0 z^9XI)ZtbJpTC83Bc#qcouJ*-VO@B{Y_rBJ80iVVzN(E;gFe?z&B^YRc}qse25Tb{l)r1)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kNlimdO$}SM zwg&n)u!;UqDCD2WR^9_{?R~6ahOgW=qd{JM5yDs0TsnaP75&PEPs`a^Zv51iv)bB~ zk(DcIu-Ksx>8n}aa3|@nS$ps56}V!&nwm?-i}5fL(RZ%6WP+fnALcoW=Ck2uQe4i~ z`XPUhyw;X`iN3R8J@i2dR58GpjE{uLFXR0fuN=$b^OZB?UCg+Em6Ll*%i*4KtczNi zOa_Z*t;+10niW^DPzW9neuM?MI-OU*T=MIzr46B3Y#59odq87eS4#QX6`&_V|3qA8 z3kQTkwX5#KJjAjJ{;GyMZznnE7xGv!Yn-*K-~h~c#T8Dcz&F%DZr1n5S77~1XM4Tl zX#Fr_*e9;=FJp54xRL`v!vnLOFtlOV__798K6l5{Pw$vZg8$*qaQ8^RAAe=|yBvS{ z^ncj-zXW{oZCC0aZ~teiR9;e7#?Z&_lod>Is+w)fG7jF;B5R zeRU?k-sIPv{Q8q$hw|$Ya$8Pa%CArPb!xzRWvg4Fj@jy0ejP(n&Z%elbuGWX<=44> z^)9dO<<~zdUiLaj^Rw4O;5l`XaC?2suag7S%e=Z-W3Qk2bu_=8=GWEy`kG&7^XqND zx|>&j<<`&E;rx1xdYV(0^XoI!O?#cruh+nH>UMtp2A)&Lh5np+o?F-R>wA8kAE4gn z)&2hUKmQz%dmhL?7l<7Ix5@K?x~g-6y5^i0a?TA{@!a!+I7emA5&7qd+;c_#`Qrce zoFUhF@N>`ugB}?4z@P^PJuv8jK@SXiV9*1D9vJk%pa%v$FzA6n4-9%>&;x@W81%rP O2L?Sb=z;&AJn-*m;2p96 literal 0 HcmV?d00001 diff --git a/src/bus.rs b/src/bus.rs index 6d7fa20..10a793a 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -2,10 +2,12 @@ use std::cell::RefCell; use std::rc::{Rc, Weak}; use crate::cpu::CPU; +use crate::cartridge::Cartridge; pub struct Bus { cpu: Weak>, + cartridge: Cartridge, ram: Vec } @@ -17,6 +19,7 @@ impl Bus Bus { cpu: Weak::new(), + cartridge: Cartridge::new("roms/nestest.nes"), ram: vec![0; 0x800] } } @@ -38,6 +41,12 @@ impl Bus pub fn read_cpu(&self, addr: u16) -> u8 { - self.ram[addr as usize] + match addr + { + 0..=0x1FFF => self.ram[(addr & 0x7FF) as usize], + 0x8000..=0xFFFF => self.cartridge.read_prg(addr & 0x7FFF), + + _ => panic!("Tried to access invalid memory address {}", addr) + } } } \ No newline at end of file diff --git a/src/cartridge.rs b/src/cartridge.rs new file mode 100644 index 0000000..e3cb62d --- /dev/null +++ b/src/cartridge.rs @@ -0,0 +1,54 @@ +use std::fs; + +struct Header +{ + prg_blocks: u8, + chr_blocks: u8 +} + +pub struct Cartridge +{ + header: Header, + + prg: Vec, + chr: Vec +} + +impl Cartridge +{ + pub fn new(filepath: &str) -> Cartridge + { + let contents = fs::read(filepath).expect("Failed to load ROM"); + let iter = contents.iter(); + + let mut curr_pos: usize = 0; + + let header_data = &contents[curr_pos..curr_pos + 16]; + let header = Header { + prg_blocks: header_data[4], + chr_blocks: header_data[5] + }; + + // TODO: For now assume there is no trainer + curr_pos = 16; + let prg_data = &contents[curr_pos..(curr_pos + 0x4000 * header.prg_blocks as usize)]; + + curr_pos += 0x4000 * header.prg_blocks as usize; + let chr_data = &contents[curr_pos..(curr_pos + 0x2000 * header.chr_blocks as usize)]; + + Cartridge + { + header: header, + + prg: prg_data.to_vec(), + chr: chr_data.to_vec() + } + } + + // TODO: For now all memio is hardcoded to work with nestest.nes for testing + + pub fn read_prg(&self, addr: u16) -> u8 + { + self.prg[(addr & 0x3FFF) as usize] + } +} \ No newline at end of file diff --git a/src/cpu.rs b/src/cpu.rs index e6dfd3a..d3c1818 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -41,6 +41,9 @@ impl CPU self.x = 0; self.y = 0; self.sp = 0xFD; + + // TODO: This is just for the nestest.nes + self.pc = 0xC000; } pub fn execute(&mut self) @@ -51,7 +54,7 @@ impl CPU match (opcode) { - _ => panic!("Unimplemented opcode {}", opcode) + _ => panic!("Unimplemented opcode {:X}", opcode) } } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f2fa933..442d5af 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod nes; mod bus; mod cpu; +mod cartridge; use nes::NES;