Skip to content

Commit a7a53e5

Browse files
committed
chore: start work on APU cycles
1 parent 76ab3d9 commit a7a53e5

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/apu/README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
# APU - Audio Processing Unit
22

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

15-
Combing wave forms:
14+
Combining wave forms:
1615
https://0xc45.com/blog/digital-audio-synthesizer-in-rust/
1716

1817
Other synthesizer examples:

src/apu/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,35 @@ pub struct Apu {
1414
dmc: [u8; 4],
1515
status: u8,
1616
frame_counter: u8,
17+
18+
cpu_cycles: usize,
19+
is_between_apu_cycle: bool,
1720
}
1821

1922
impl Apu {
2023
pub fn new() -> Self {
2124
Self {
25+
// TODO: Pulse2 differs from Pulse1 slightly.
26+
// Known diff is how in APU sweep "Calculating the target period"
2227
pulse1: PulseRegister::new(),
2328
pulse2: PulseRegister::new(),
2429
triangle: [0; 4],
2530
noise: [0; 4],
2631
dmc: [0; 4],
2732
status: 0,
2833
frame_counter: 0,
34+
35+
cpu_cycles: 0,
36+
is_between_apu_cycle: false,
37+
}
38+
}
39+
40+
/// tick tracks CPU cycles. APU cycles occur every 2 CPU cycles
41+
pub fn tick_cpu_cycles(&mut self, cycles: usize) {
42+
for _ in 0..cycles {
43+
self.cpu_cycles += 1;
44+
// or just cpu_cycles %2
45+
self.is_between_apu_cycle = !self.is_between_apu_cycle
2946
}
3047
}
3148
}

src/bus.rs

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ impl<'a> Bus<'a> {
5252
if should_rerender {
5353
(self.gameloop_callback)(&self.ppu, &mut self.gamepad1, &mut self.gamepad2)
5454
}
55+
56+
self.apu.tick_cpu_cycles(cycles);
5557
}
5658

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

0 commit comments

Comments
 (0)