1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//
// apu.rs
//
// @author Natesh Narain <nnaraindev@gmail.com>
// @date Mar 31 2020
//
pub mod bus;
mod chnl;
mod seq;

use seq::{FrameSequencer, Event};
use chnl::{SoundChannel, Pulse, Triangle, Noise, Dmc, LengthCounterUnit, EnvelopeUnit, NegateAddMode};

pub type Sample = f32;

pub const APU_OUTPUT_RATE: usize = 1_790_000;

use crate::common::{IoAccess, IoAccessRef, Clockable, Register, Interrupt};

#[cfg(feature="events")]
use std::sync::mpsc::Sender;

#[cfg(feature="events")]
pub mod events {
    #[derive(Debug)]
    pub struct ApuEvent {
        pub pulse1: f32,
        pub pulse2: f32,
        pub triangle: f32,
        pub noise: f32,
        pub dmc: f32,
        pub mixer: f32,
    }
}

/// NES APU
pub struct Apu {
    pulse1: Pulse,
    pulse2: Pulse,
    triangle: Triangle,
    noise: Noise,
    dmc: Dmc,

    sequencer: FrameSequencer,

    pulse_table: [f32; 31],
    tnd_table: [f32; 203],

    bus: Option<IoAccessRef>,

    // Event logging
    #[cfg(feature="events")]
    logger: Option<Sender<events::ApuEvent>>,
}

impl Default for Apu {
    fn default() -> Self {
        // TODO: Would be nice if this could be done inline with (0..31).map(|n| ...)
        // Or use const fn?

        // Pulse look up table
        let mut pulse_table = [0f32; 31];
        for (n, e) in pulse_table.iter_mut().enumerate() {
            *e = 95.52 / (8128.0 / (n as f32) + 100f32);
        }

        // tnd look up table
        let mut tnd_table = [0f32; 203];
        for (n, e) in tnd_table.iter_mut().enumerate() {
            *e = 163.67 / (24329.0 / (n as f32) + 100f32);
        }

        Apu {
            pulse1: Pulse::default(),
            pulse2: Pulse::default().add_mode(NegateAddMode::TwosComplement),
            triangle: Triangle::default(),
            noise: Noise::default(),
            dmc: Dmc::default(),

            sequencer: FrameSequencer::default(),

            // Mixer lookup tables
            pulse_table: pulse_table,
            tnd_table: tnd_table,

            bus: None,

            #[cfg(feature="events")]
            logger: None,
        }
    }
}

impl Clockable<Sample> for Apu {
    fn tick(&mut self) -> Sample {
        // Clock the frame sequencer to generate low frequency clock events and process them
        for event in self.sequencer.tick().iter() {
            match event {
                Event::EnvelopAndLinear => {
                    self.clock_envelope();
                    self.triangle.clock_linear();
                },
                Event::LengthAndSweep => {
                    self.clock_length();
                    self.clock_sweep();
                },
                Event::Irq => {
                    if let Some(ref mut bus) = self.bus {
                        bus.borrow_mut().raise_interrupt(Interrupt::Irq);
                    }
                },
                Event::None => {}
            }
        }

        // Clock the pulse channels every APU cycle
        self.pulse1.tick();
        self.pulse2.tick();

        // The triangle channel is clocked at twice the rate of the APU
        self.triangle.tick();
        self.triangle.tick();

        // Clock noise channel
        self.noise.tick();

        // Clock DMC
        self.dmc.tick();

        self.mix()
    }
}

impl IoAccess for Apu {
    fn read_byte(&self, addr: u16) -> u8 {
        match addr {
            0x4000..=0x4003 => self.pulse1.read_byte(addr - 0x4000),
            0x4004..=0x4007 => self.pulse2.read_byte(addr - 0x4004),
            0x4008..=0x400B => self.triangle.read_byte(addr - 0x4008),
            0x400C..=0x400F => self.noise.read_byte(addr - 0x400C),
            0x4010..=0x4013 => self.dmc.read_byte(addr - 0x4010),
            0x4015 => self.status(),
            0x4017 => self.sequencer.value(),
            _ => panic!("Invalid address for APU: ${:04X}", addr),
        }
    }

    fn write_byte(&mut self, addr: u16, data: u8) {
        match addr {
            0x4000..=0x4003 => self.pulse1.write_byte(addr - 0x4000, data),
            0x4004..=0x4007 => self.pulse2.write_byte(addr - 0x4004, data),
            0x4008..=0x400B => self.triangle.write_byte(addr - 0x4008, data),
            0x400C..=0x400F => self.noise.write_byte(addr - 0x400C, data),
            0x4010..=0x4013 => self.dmc.write_byte(addr - 0x4010, data),
            0x4015 => {
                self.pulse1.enable_length(bit_is_set!(data, 0));
                self.pulse2.enable_length(bit_is_set!(data, 1));
                self.triangle.enable_length(bit_is_set!(data, 2));
                self.noise.enable_length(bit_is_set!(data, 3));
                self.dmc.set_enable(bit_is_set!(data, 4));
            },
            0x4017 => {
                self.sequencer.load(data);

                if bit_is_set!(data, 7) {
                    // Immediately clock length units
                    self.pulse1.clock_length();
                    self.pulse2.clock_length();
                    self.triangle.clock_length();
                    self.noise.clock_length();
                }
            },
            _ => panic!("Invalid address for APU: ${:04X}", addr),
        }
    }
}

impl Apu {
    fn mix(&self) -> Sample {
        let pulse1 = self.pulse1.output() as f32;
        let pulse2 = self.pulse2.output() as f32;
        let triangle = self.triangle.output() as f32;
        let noise = self.noise.output() as f32;
        let dmc = self.dmc.output() as f32;

        let pulse_out = self.pulse_table[(pulse1 + pulse2) as usize];

        let tnd_out = self.tnd_table[(3.0 * triangle + 2.0 * noise + dmc) as usize];

        let mixed = pulse_out + tnd_out;

        #[cfg(feature="events")]
        {
            let data = events::ApuEvent {
                pulse1,
                pulse2,
                triangle,
                noise,
                dmc,
                mixer: mixed,
            };

            if let Some(ref logger) = self.logger {
                match logger.send(data) {
                    Ok(_) => {},
                    Err(_) => {},
                }
            }
        }

        mixed
    }

    fn status(&self) -> u8 {
        (self.pulse1.length_status() as u8)
        | (self.pulse2.length_status() as u8) << 1
        | (self.triangle.length_status() as u8) << 2
        | (self.noise.length_status() as u8) << 3
        | (self.dmc.status() as u8) << 4
        | (self.sequencer.irq_status() as u8) << 6
    }

    fn clock_length(&mut self) {
        self.pulse1.clock_length();
        self.pulse2.clock_length();
        self.triangle.clock_length();
        self.noise.clock_length();
    }

    fn clock_envelope(&mut self) {
        self.pulse1.clock_envelope();
        self.pulse2.clock_envelope();
        self.noise.clock_envelope();
    }

    fn clock_sweep(&mut self) {
        self.pulse1.clock_sweep();
        self.pulse2.clock_sweep();
    }

    pub fn load_bus(&mut self, bus: IoAccessRef) {
        self.dmc.load_bus(bus.clone());
        self.bus = Some(bus);
    }

    #[cfg(feature="events")]
    pub fn set_event_sender(&mut self, sender: Sender<events::ApuEvent>) {
        self.logger = Some(sender);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::rc::Rc;
    use std::cell::RefCell;

    #[test]
    fn pulse_lenctr() {
        let mut apu = init_apu();

        // Mode Step4
        apu.write_byte(0x4017, 0x00);

        // Enable length counters
        apu.write_byte(0x4015, 0x03);
        // Set length counter to ten ticks
        apu.write_byte(0x4003, 0x00);
        apu.write_byte(0x4007, 0x00);

        // Check the status and ensure the length counters report active
        let status = apu.read_byte(0x4015);
        assert!(bit_is_set!(status, 0));
        assert!(bit_is_set!(status, 1));

        // The length counter clock twice per frame
        // Run for 4 frames
        for _ in 0..4 {
            run_for_step4_frame(&mut apu);
        }

        // Ensure the length counters are still active
        let status = apu.read_byte(0x4015);
        assert!(bit_is_set!(status, 0));
        assert!(bit_is_set!(status, 1));

        // Run for another frame
        // The length counter should be reported as inactive
        run_for_step4_frame(&mut apu);

        let status = apu.read_byte(0x4015);
        assert!(bit_is_clear!(status, 0));
        assert!(bit_is_clear!(status, 1));
    }

    fn run_for_step4_frame(apu: &mut dyn Clockable<Sample>) {
        for _ in 0..14915 {
            apu.tick();
        }
    }

    struct FakeBus {
        vram: [u8; 0x4000],
    }

    impl Default for FakeBus {
        fn default() -> Self {
            FakeBus {
                vram: [0; 0x4000],
            }
        }
    }

    impl IoAccess for FakeBus {
        fn read_byte(&self, addr: u16) -> u8 {
            self.vram[addr as usize]
        }
        fn write_byte(&mut self, addr: u16, value: u8) {
            self.vram[addr as usize] = value;
        }
    }

    fn init_apu() -> Apu {
        let mut apu: Apu = Apu::default();
        apu.load_bus(Rc::new(RefCell::new(FakeBus::default())));

        apu
    }
}