Skip to content

Commit c258fa9

Browse files
committed
chore: refactor some bitflags to bitfields
1 parent b28c967 commit c258fa9

File tree

3 files changed

+87
-47
lines changed

3 files changed

+87
-47
lines changed

Cargo.lock

+42
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
bitfields = "0.6.0"
78
bitflags = "2.6.0"
89
env_logger = "0.11.5"
910
log = "0.4.22"

src/apu/pulse_register.rs

+44-47
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,40 @@
1-
use bitflags::bitflags;
2-
3-
bitflags! {
4-
/// PulseEnvelope
5-
/// https://www.nesdev.org/wiki/APU_Envelope
6-
struct PulseEnvelope: u8 {
7-
/// Duty Cycle
8-
const D = 0b1100_0000;
9-
/// Envelope loop / length counter halt
10-
const L = 0b0010_0000;
11-
/// Constant volume/envelope flag
12-
const C = 0b0001_0000;
13-
/// Volume/envelope divider period
14-
const V = 0b0000_1111;
15-
}
1+
use bitfields::bitfield;
2+
3+
/// https://www.nesdev.org/wiki/APU_Envelope
4+
#[bitfield(u8)]
5+
// #[derive(Copy, Clone)] // Attributes are passed to the struct.
6+
struct PulseEnvelope {
7+
/// Duty Cycle
8+
#[bits(2)]
9+
D: u8,
10+
/// Envelope loop / length counter halt
11+
L: bool,
12+
/// Constant volume/envelope flag
13+
C: bool,
14+
/// Volume/envelope divider period
15+
#[bits(4)]
16+
V: u8,
1617
}
1718

18-
bitflags! {
19-
/// PulseSweep
20-
/// https://www.nesdev.org/wiki/APU_Sweep
21-
struct PulseSweep: u8 {
22-
/// Enabled flag
23-
const E = 0b1000_0000;
24-
/// Period
25-
/// The divider's period is P + 1 half-frames
26-
const P = 0b0111_0000;
27-
/// Negate flag
28-
/// 0: add to period, sweeping toward lower frequencies
29-
/// 1: subtract from period, sweeping toward higher frequencies
30-
const N = 0b0000_1000;
31-
/// Shift count (number of bits).
32-
/// If SSS is 0, then behaves like E=0.
33-
const S = 0b0000_0111;
34-
}
19+
/// PulseSweep
20+
/// https://www.nesdev.org/wiki/APU_Sweep
21+
#[bitfield(u8)]
22+
// #[derive(Copy, Clone)] // Attributes are passed to the struct.
23+
struct PulseSweep {
24+
/// Enabled flag
25+
E: bool,
26+
/// Period
27+
/// The divider's period is P + 1 half-frames
28+
#[bits(3)]
29+
P: u8,
30+
/// Negate flag
31+
/// 0: add to period, sweeping toward lower frequencies
32+
/// 1: subtract from period, sweeping toward higher frequencies
33+
N: bool,
34+
/// Shift count (number of bits).
35+
/// If SSS is 0, then behaves like E=0.
36+
#[bits(3)]
37+
S: u8,
3538
}
3639

3740
/// https://www.nesdev.org/wiki/APU_Pulse
@@ -53,44 +56,38 @@ impl PulseRegister {
5356
// data[0]
5457

5558
fn duty_cycle(&self) -> u8 {
56-
let duty = (self.data[0] & PulseEnvelope::D.bits()) >> 6;
57-
assert!(duty < 4);
58-
duty
59+
PulseEnvelope::from_bits(self.data[0]).D()
5960
}
6061

6162
fn is_length_counter_halted(&self) -> bool {
62-
PulseEnvelope::from_bits_truncate(self.data[0]).contains(PulseEnvelope::L)
63+
PulseEnvelope::from_bits(self.data[0]).L()
6364
}
6465

6566
fn is_constant_volume(&self) -> bool {
66-
PulseEnvelope::from_bits_truncate(self.data[0]).contains(PulseEnvelope::C)
67+
PulseEnvelope::from_bits(self.data[0]).C()
6768
}
6869

6970
fn envelope_period(&self) -> u8 {
70-
let pe = PulseEnvelope::from_bits_truncate(self.data[0]);
71-
PulseEnvelope::V.intersection(pe).bits()
71+
PulseEnvelope::from_bits(self.data[0]).V()
7272
}
7373

7474
// data[1]
7575

7676
fn is_sweep_enabled(&self) -> bool {
77-
let ssc = self.sweep_shift_count();
78-
let enabled = PulseSweep::from_bits_truncate(self.data[1]).contains(PulseSweep::E);
79-
ssc > 0 && enabled
77+
let s = PulseSweep::from_bits(self.data[1]);
78+
s.E() && s.S() > 0
8079
}
8180

8281
fn is_sweep_negated(&self) -> bool {
83-
PulseSweep::from_bits_truncate(self.data[1]).contains(PulseSweep::N)
82+
PulseSweep::from_bits(self.data[1]).N()
8483
}
8584

8685
fn sweep_period(&self) -> u8 {
87-
let s = PulseSweep::from_bits_truncate(self.data[1]);
88-
PulseSweep::P.intersection(s).bits() >> 4
86+
PulseSweep::from_bits(self.data[1]).P()
8987
}
9088

9189
fn sweep_shift_count(&self) -> u8 {
92-
let s = PulseSweep::from_bits_truncate(self.data[1]);
93-
PulseSweep::S.intersection(s).bits()
90+
PulseSweep::from_bits(self.data[1]).S()
9491
}
9592

9693
// data[2] and data[3]

0 commit comments

Comments
 (0)