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 ,
16
17
}
17
18
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 ,
35
38
}
36
39
37
40
/// https://www.nesdev.org/wiki/APU_Pulse
@@ -53,44 +56,38 @@ impl PulseRegister {
53
56
// data[0]
54
57
55
58
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 ( )
59
60
}
60
61
61
62
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 ( )
63
64
}
64
65
65
66
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 ( )
67
68
}
68
69
69
70
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 ( )
72
72
}
73
73
74
74
// data[1]
75
75
76
76
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
80
79
}
81
80
82
81
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 ( )
84
83
}
85
84
86
85
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 ( )
89
87
}
90
88
91
89
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 ( )
94
91
}
95
92
96
93
// data[2] and data[3]
0 commit comments