Skip to content

Commit 76ab3d9

Browse files
committed
chore: wire APU into Bus
1 parent ee1a59d commit 76ab3d9

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

justfile

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
alias cc := code_coverage
2+
alias pc := pre_commit
3+
alias t := test_all
24

35
run:
46
cargo run
@@ -15,20 +17,19 @@ lint_fix:
1517
build:
1618
cargo build
1719

18-
test:
20+
test_all:
1921
cargo test
2022

21-
test_watch_all:
22-
git ls-files | entr cargo test
23+
test TEST:
24+
cargo test {{TEST}}
2325

2426
test_watch TEST:
25-
git ls-files | entr cargo test {{TEST}}::tests
26-
27+
git ls-files | entr cargo test {{TEST}}
2728

2829
code_coverage:
29-
cargo tarpaulin -o html
30+
cargo tarpaulin -o html && open tarpaulin-report.html
3031

3132
nestest:
3233
NESTEST_HACK=1 cargo run roms/nestest.nes > myout.log
3334

34-
pre_commit: lint test build
35+
pre_commit: lint test_all build

src/apu/mod.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,6 @@ mod tests {
8080
assert_eq!(apu.frame_counter, 222);
8181
}
8282

83-
#[test]
84-
#[should_panic(expected = "attempt to read from write only APU register")]
85-
fn test_apu_invalid_mem_read_panics() {
86-
let mut apu = Apu::new();
87-
_ = apu.mem_read(0x4000);
88-
}
89-
9083
#[test]
9184
fn test_apu_mem_read_status() {
9285
let mut apu = Apu::new();
@@ -97,4 +90,25 @@ mod tests {
9790
let data = apu.mem_read(0x4015);
9891
assert_eq!(data, 123);
9992
}
93+
94+
#[test]
95+
#[should_panic(expected = "attempt to read from write only APU register")]
96+
fn test_apu_invalid_mem_read_of_read_only_register_panics() {
97+
let mut apu = Apu::new();
98+
_ = apu.mem_read(0x4000);
99+
}
100+
101+
#[test]
102+
#[should_panic(expected = "invalid lookup")]
103+
fn test_apu_out_of_bounds_mem_read_panics() {
104+
let mut apu = Apu::new();
105+
_ = apu.mem_read(0x4000 - 1);
106+
}
107+
108+
#[test]
109+
#[should_panic(expected = "invalid lookup")]
110+
fn test_apu_out_of_bounds_mem_write_panics() {
111+
let mut apu = Apu::new();
112+
apu.mem_write(0x4000 - 1, 0);
113+
}
100114
}

src/bus.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{core::Mem, gamepad::GamepadRegister, ppu::Ppu, rom::Rom};
1+
use crate::{apu::Apu, core::Mem, gamepad::GamepadRegister, ppu::Ppu, rom::Rom};
22

33
const RAM: u16 = 0x0000;
44
const RAM_MIRROR_END: u16 = 0x2000;
@@ -13,6 +13,7 @@ pub struct Bus<'call> {
1313
cpu_vram: [u8; 0x800], // 2048
1414
rom: Rom,
1515
ppu: Ppu,
16+
apu: Apu,
1617
gamepad1: GamepadRegister,
1718
gamepad2: GamepadRegister,
1819
cycles: usize,
@@ -30,11 +31,13 @@ impl<'a> Bus<'a> {
3031
F: FnMut(&Ppu, &mut GamepadRegister, &mut GamepadRegister) + 'call + 'a,
3132
{
3233
let ppu = Ppu::new(rom.chr_rom.clone(), rom.mirroring);
34+
let apu = Apu::new();
3335

3436
Bus {
3537
cpu_vram: [0; 0x800],
3638
rom,
3739
ppu,
40+
apu,
3841
cycles: 0,
3942
gamepad1: GamepadRegister::new(),
4043
gamepad2: GamepadRegister::new(),
@@ -97,7 +100,7 @@ impl Mem for Bus<'_> {
97100
0x4014 => {
98101
panic!("attempt to read from write-only PPU register: 0x4014 (OAMDMA - Sprite DMA)")
99102
}
100-
0x4000..=0x4013 | 0x4015 => 0, // TODO: implement APU,
103+
0x4000..=0x4013 | 0x4015 => self.apu.mem_read(addr),
101104
0x4016 => self.gamepad1.read(),
102105
0x4017 => self.gamepad2.read(),
103106
0x4018..0x4020 => 0, // APU and I/O functionality that is normally disabled
@@ -142,7 +145,7 @@ impl Mem for Bus<'_> {
142145

143146
// TODO: Eventually figure out what CPU cycles need to be added for OAM DMA write
144147
}
145-
0x4000..=0x4013 | 0x4015 => (), // TODO: implement APU,
148+
0x4000..=0x4013 | 0x4015 => self.apu.mem_write(addr, data),
146149
0x4016 => self.gamepad1.write(data),
147150
0x4017 => self.gamepad2.write(data),
148151
0x4018..0x4020 => (), // APU and I/O functionality that is normally disabled

0 commit comments

Comments
 (0)