Skip to content

Commit

Permalink
chore: start work on APU cycles
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanleiby committed Dec 16, 2024
1 parent 76ab3d9 commit a7a53e5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/apu/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# APU - Audio Processing Unit




Rust libs:

- synthesis, has osc for common wavs.. will it work well to generate one for each register?
https://docs.rs/twang/latest/twang/
- SDL2 can play sounds
- ex of squarewave: https://github.com/Rust-SDL2/rust-sdl2/blob/master/examples/audio-queue-squarewave.rs
- can play multiple sounds with mixer:
- https://rust-sdl2.github.io/rust-sdl2/sdl2/mixer/index.html
- usage ex: https://github.com/Rust-SDL2/rust-sdl2/blob/master/examples/mixer-demo.rs
- https://nicktasios.nl/posts/making-sounds-using-sdl-and-visualizing-them-on-a-simulated-oscilloscope.html

Combing wave forms:
Combining wave forms:
https://0xc45.com/blog/digital-audio-synthesizer-in-rust/

Other synthesizer examples:
Expand Down
17 changes: 17 additions & 0 deletions src/apu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,35 @@ pub struct Apu {
dmc: [u8; 4],
status: u8,
frame_counter: u8,

cpu_cycles: usize,
is_between_apu_cycle: bool,
}

impl Apu {
pub fn new() -> Self {
Self {
// TODO: Pulse2 differs from Pulse1 slightly.
// Known diff is how in APU sweep "Calculating the target period"
pulse1: PulseRegister::new(),
pulse2: PulseRegister::new(),
triangle: [0; 4],
noise: [0; 4],
dmc: [0; 4],
status: 0,
frame_counter: 0,

cpu_cycles: 0,
is_between_apu_cycle: false,
}
}

/// tick tracks CPU cycles. APU cycles occur every 2 CPU cycles
pub fn tick_cpu_cycles(&mut self, cycles: usize) {
for _ in 0..cycles {
self.cpu_cycles += 1;
// or just cpu_cycles %2
self.is_between_apu_cycle = !self.is_between_apu_cycle
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ impl<'a> Bus<'a> {
if should_rerender {
(self.gameloop_callback)(&self.ppu, &mut self.gamepad1, &mut self.gamepad2)
}

self.apu.tick_cpu_cycles(cycles);
}

pub fn poll_nmi_interrupt(&mut self) -> bool {
Expand Down

0 comments on commit a7a53e5

Please sign in to comment.