<commit_before>use arm::Cycles;

use crate::{
    memory::io::{FifoChannel, IoRegisters},
    Gba,
};

#[derive(Default)]
pub struct GbaAudio {
    commands: Vec<Command>,
    last_update_time: u64,
}

impl GbaAudio {
    pub fn clear(&mut self) {
        self.commands.clear();
    }

    pub fn commands(&self) -> &[Command] {
        &self.commands
    }

    fn fifo_play(&mut self, chan: FifoChannel, ioregs: &mut IoRegisters, late: Cycles) {
        self.wait(ioregs.time, late);
        let sample = if chan == FifoChannel::A {
            ioregs.fifo_a.pop_sample()
        } else {
            ioregs.fifo_b.pop_sample()
        };
        self.commands.push(Command::PlaySample(chan, sample as i8));
    }

    fn wait(&mut self, now: u64, late: arm::Cycles) {
        if now > self.last_update_time {
            let elapsed =
                (now - self.last_update_time).saturating_sub(u32::from(late) as u64) as u32;
            log::debug!("writing sample after {elapsed} cycles");
            if let Some(Command::Wait(ref mut cycles)) = self.commands.last_mut() {
                *cycles += elapsed;
            } else {
                self.commands.push(Command::Wait(elapsed));
            }
        }
        self.last_update_time = now;
    }
}

pub fn check_fifo_timer_overflow(timer: usize, gba: &mut Gba, late: Cycles) {
    if gba.mem.ioregs.soundcnt_h.dma_enable(FifoChannel::A)
        && gba.mem.ioregs.soundcnt_h.dma_timer_select(FifoChannel::A) == timer
    {
        gba.audio
            .fifo_play(FifoChannel::A, &mut gba.mem.ioregs, late)
    }

    if gba.mem.ioregs.soundcnt_h.dma_enable(FifoChannel::B)
        && gba.mem.ioregs.soundcnt_h.dma_timer_select(FifoChannel::B) == timer
    {
        gba.audio
            .fifo_play(FifoChannel::B, &mut gba.mem.ioregs, late)
    }
}

#[derive(Clone, Copy, Debug)]
pub enum Command {
    Wait(u32),
    PlaySample(FifoChannel, i8),
}
<commit_msg>Remove debug message in sound wait<commit_after>use arm::Cycles;

use crate::{
    memory::io::{FifoChannel, IoRegisters},
    Gba,
};

#[derive(Default)]
pub struct GbaAudio {
    commands: Vec<Command>,
    last_update_time: u64,
}

impl GbaAudio {
    pub fn clear(&mut self) {
        self.commands.clear();
    }

    pub fn commands(&self) -> &[Command] {
        &self.commands
    }

    fn fifo_play(&mut self, chan: FifoChannel, ioregs: &mut IoRegisters, late: Cycles) {
        self.wait(ioregs.time, late);
        let sample = if chan == FifoChannel::A {
            ioregs.fifo_a.pop_sample()
        } else {
            ioregs.fifo_b.pop_sample()
        };
        self.commands.push(Command::PlaySample(chan, sample as i8));
    }

    fn wait(&mut self, now: u64, late: arm::Cycles) {
        if now > self.last_update_time {
            let elapsed =
                (now - self.last_update_time).saturating_sub(u32::from(late) as u64) as u32;
            if let Some(Command::Wait(ref mut cycles)) = self.commands.last_mut() {
                *cycles += elapsed;
            } else {
                self.commands.push(Command::Wait(elapsed));
            }
        }
        self.last_update_time = now;
    }
}

pub fn check_fifo_timer_overflow(timer: usize, gba: &mut Gba, late: Cycles) {
    if gba.mem.ioregs.soundcnt_h.dma_enable(FifoChannel::A)
        && gba.mem.ioregs.soundcnt_h.dma_timer_select(FifoChannel::A) == timer
    {
        gba.audio
            .fifo_play(FifoChannel::A, &mut gba.mem.ioregs, late)
    }

    if gba.mem.ioregs.soundcnt_h.dma_enable(FifoChannel::B)
        && gba.mem.ioregs.soundcnt_h.dma_timer_select(FifoChannel::B) == timer
    {
        gba.audio
            .fifo_play(FifoChannel::B, &mut gba.mem.ioregs, late)
    }
}

#[derive(Clone, Copy, Debug)]
pub enum Command {
    Wait(u32),
    PlaySample(FifoChannel, i8),
}

{'fraction_non_alphanumeric': 0.1357086717317265, 'fraction_numerical': 0.007335603877390621, 'mean_word_length': 2.393777777777778, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5783999', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1357, 'n_words': 244}