Skip to content

Commit

Permalink
Merge pull request #43 from YushiOMOTE/add-test-9-10-12
Browse files Browse the repository at this point in the history
Update wave logic to pass ROM tests 9, 10, 12
  • Loading branch information
YushiOMOTE authored Jul 23, 2024
2 parents aab0567 + 077e44c commit 8ccdfea
Show file tree
Hide file tree
Showing 15 changed files with 702 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:
- run: cargo clippy --workspace --examples --tests --benches
- run: cargo build --verbose
- run: cargo build --verbose --examples
- run: cargo test --verbose
- run: cargo test --release --verbose
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ Test status of [Blargg's Gameboy hardware test ROMs](https://github.com/retrio/g
* [x] `06-overflow on trigger`
* [x] `07-len sweep period sync`
* [x] `08-len ctr during power`
* [ ] `09-wave read while on`
* [ ] `10-wave trigger while on`
* [x] `09-wave read while on`
* [x] `10-wave trigger while on`
* [ ] `11-regs after power`
* [ ] `12-wave write while on`
* [x] `12-wave write while on`
* [ ] `cgb_sound`

## Projects
Expand Down
38 changes: 19 additions & 19 deletions core/src/apu/clock_divider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ impl ClockDivider {
self.source_clock_rate = source_clock_rate;
}

pub fn step(&mut self, cycles: usize) -> bool {
pub fn step(&mut self, cycles: usize) -> usize {
self.counter += cycles;
if self.counter >= self.interval() {
self.counter -= self.interval();
true
} else {
false
}

let times = self.counter / self.interval();

self.counter = self.counter % self.interval();

times
}

fn interval(&self) -> usize {
Expand All @@ -39,20 +39,20 @@ fn test_clock_divider() {
let mut divider = ClockDivider::new(2, 1);

// 4 ticks -> 2 ticks
assert!(!divider.step(1));
assert!(divider.step(1));
assert!(!divider.step(1));
assert!(divider.step(1));
assert_eq!(divider.step(1), 0);
assert_eq!(divider.step(1), 1);
assert_eq!(divider.step(1), 0);
assert_eq!(divider.step(1), 1);

divider.set_source_clock_rate(4);

// 8 ticks -> 2 ticks
assert!(!divider.step(1));
assert!(!divider.step(1));
assert!(!divider.step(1));
assert!(divider.step(1));
assert!(!divider.step(1));
assert!(!divider.step(1));
assert!(!divider.step(1));
assert!(divider.step(1));
assert_eq!(divider.step(1), 0);
assert_eq!(divider.step(1), 0);
assert_eq!(divider.step(1), 0);
assert_eq!(divider.step(1), 1);
assert_eq!(divider.step(1), 0);
assert_eq!(divider.step(1), 0);
assert_eq!(divider.step(1), 0);
assert_eq!(divider.step(1), 1);
}
6 changes: 5 additions & 1 deletion core/src/apu/frame_sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ impl FrameSequencer {
}

pub fn step(&mut self, cycles: usize) -> Option<usize> {
if !self.divider.step(cycles) {
let times = self.divider.step(cycles);

if times == 0 {
return None;
}
// assert_eq!(times, 1);

let current_step = self.step;
self.step = (self.step + 1) % 8;
Some(current_step)
Expand Down
2 changes: 1 addition & 1 deletion core/src/apu/length_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl LengthCounter {
// trigger, enable, freeze

pub fn update(&mut self, trigger: bool, enable: bool) {
info!(
debug!(
"trigger={}, enable={}: {:p}: {:?}",
trigger, enable, self, self
);
Expand Down
1 change: 1 addition & 0 deletions core/src/apu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod timer;
mod tone;
mod util;
mod wave;
mod wave_buf;

pub struct Apu {
tones: [Tone; 2],
Expand Down
5 changes: 5 additions & 0 deletions core/src/apu/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ impl Timer {

pub fn set_interval(&mut self, interval: usize) {
self.interval = interval;
self.counter = 0;
}

pub fn remaining(&self) -> usize {
self.interval.saturating_sub(self.counter)
}

pub fn tick(&mut self) -> bool {
Expand Down
14 changes: 6 additions & 8 deletions core/src/apu/tone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ use log::*;

use crate::hardware::Stream;

use super::{
length_counter::LengthCounter,
sweep::Sweep,
util::{Envelop, WaveIndex},
};
use super::{length_counter::LengthCounter, sweep::Sweep, util::Envelop, wave_buf::WaveIndex};

#[derive(Debug, Clone)]
pub struct Tone {
Expand Down Expand Up @@ -174,7 +170,7 @@ impl ToneStream {
Self {
tone,
env,
index: WaveIndex::new(),
index: WaveIndex::new(4_194_304, 8),
}
}

Expand Down Expand Up @@ -215,8 +211,10 @@ impl Stream for ToneStream {
_ => unreachable!(),
};

let index = self.index.index(rate, freq * 8, 8);
if index <= duty {
self.index.set_source_clock_rate(rate);
self.index.update_index(freq * 8);

if self.index.index() <= duty {
0
} else {
amp as u16
Expand Down
22 changes: 0 additions & 22 deletions core/src/apu/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,6 @@ impl AtomicHelper for AtomicBool {
}
}

pub struct WaveIndex {
clock: usize,
index: usize,
}

impl WaveIndex {
pub fn new() -> Self {
Self { clock: 0, index: 0 }
}

pub fn index(&mut self, rate: usize, freq: usize, max: usize) -> usize {
self.clock += freq;

if self.clock >= rate {
self.clock -= rate;
self.index = (self.index + 1) % max;
}

self.index
}
}

pub struct Envelop {
amp: usize,
count: usize,
Expand Down
Loading

0 comments on commit 8ccdfea

Please sign in to comment.