Skip to content

Commit 72091a7

Browse files
committed
fix the way ppu catches up with cpu
1 parent ca4514d commit 72091a7

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

src/cpu/bus.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,31 @@ pub struct Bus {
88
rom: NesRom,
99
prg_ram: Ram,
1010
pub ppu: Ppu<PpuMemory>,
11-
cycle: u32
11+
cycle: u32,
1212
}
1313

1414
impl Bus {
15-
1615
pub fn new(rom: NesRom) -> Self {
1716
Self {
1817
sram: Ram::new(0x8000),
1918
rom: rom.clone(),
2019
prg_ram: Ram::new(0x2000),
2120
ppu: Ppu::new(rom.chr_rom, rom.nametable_mirroring),
22-
cycle: 0
21+
cycle: 0,
2322
}
2423
}
2524

2625
pub fn tick(&mut self, cycle: u32) {
26+
let delta = cycle - self.cycle;
2727
self.cycle = cycle;
28-
self.ppu.tick(cycle * 3);
28+
self.ppu.tick(delta * 3);
2929
}
3030

31-
pub fn poll_nmi(&mut self) -> bool{
32-
self.ppu.poll_nmi()
31+
pub fn poll_nmi(&mut self) -> bool {
32+
self.ppu.poll_nmi()
3333
}
3434

35-
pub fn poll_new_frame(&mut self) -> bool{
35+
pub fn poll_new_frame(&mut self) -> bool {
3636
self.ppu.poll_new_frame()
3737
}
3838

@@ -50,6 +50,9 @@ impl Bus {
5050
a, register
5151
),
5252
}
53+
} else if a == 0x4016 || a == 0x4017 {
54+
// TODO
55+
0
5356
} else if (0x6000..0x8000).contains(&a) {
5457
self.prg_ram.read(a - 0x6000)
5558
} else if a >= 0x8000 {
@@ -67,7 +70,9 @@ impl Bus {
6770
match register {
6871
0 => self.ppu.write_ppu_ctrl(v),
6972
1 => self.ppu.write_ppu_mask(v),
70-
3 => unimplemented!(),
73+
3 => {
74+
// unimplemented!()
75+
}
7176
4 => unimplemented!(),
7277
5 => self.ppu.write_ppu_scroll(v),
7378
6 => self.ppu.write_ppu_addr(v),
@@ -78,7 +83,9 @@ impl Bus {
7883
),
7984
};
8085
} else if a == 0x4014 {
81-
unimplemented!()
86+
// unimplemented!()
87+
} else if (0x4000..=0x4017).contains(&a) {
88+
// TODO APU
8289
} else if (0x6000..0x8000).contains(&a) {
8390
self.prg_ram.write(a - 0x6000, v);
8491
} else {
@@ -91,5 +98,4 @@ impl Bus {
9198
let lo = self.rom.prg_rom[(0xFFFC - 0x8000) % self.rom.prg_rom.len()] as u16;
9299
(hi << 8) | lo
93100
}
94-
95101
}

src/ppu.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ impl<M: Memory> Ppu<M> {
170170
}
171171
}
172172

173-
pub fn tick(&mut self, cycle: u32) {
174-
self.cycle = cycle;
175-
if self.cycle > SCANLINE_CYCLES {
173+
pub fn tick(&mut self, delta: u32) {
174+
self.cycle += delta;
175+
while self.cycle > SCANLINE_CYCLES {
176176
self.cycle -= SCANLINE_CYCLES;
177177
self.scanline += 1;
178178

0 commit comments

Comments
 (0)