pikopiko-runtime 0.1.0

no_std, heap-free runtime for pikopiko: synth trait, event-binary sequencer, effects and built-in synths (AY-3-8912, buzzer, sfx).
Documentation
//! 割り込みの内部状態:パラメータの番号の集合(`Bits`)・値の表(`ParamTable`)・BGM の影の状態(`Shadow`)・効果音の枠(`Slot`)。
//!
//! どれも [`Interrupt`] の中だけで使う(公開しない)。

use super::*;

/// 256 ビットの集合(パラメータの番号)。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub(super) struct Bits(pub(super) [u32; PARAM_IDS / 32]);

impl Bits {
    pub(super) const EMPTY: Bits = Bits([0; PARAM_IDS / 32]);

    pub(super) fn has(&self, i: u8) -> bool {
        self.0[i as usize / 32] & (1 << (i % 32)) != 0
    }

    pub(super) fn set(&mut self, i: u8) {
        self.0[i as usize / 32] |= 1 << (i % 32);
    }

    pub(super) fn clear(&mut self, i: u8) {
        self.0[i as usize / 32] &= !(1 << (i % 32));
    }
}

/// パラメータの値の小さな表(番号 → 値、`K` 件まで)。
#[derive(Debug, Clone, Copy)]
pub(super) struct ParamTable<const K: usize> {
    pub(super) ids: [u8; K],
    pub(super) values: [u32; K],
    pub(super) len: usize,
}

impl<const K: usize> ParamTable<K> {
    pub(super) const EMPTY: Self = ParamTable {
        ids: [0; K],
        values: [0; K],
        len: 0,
    };

    pub(super) fn get(&self, id: u8) -> Option<u32> {
        self.ids[..self.len]
            .iter()
            .position(|&i| i == id)
            .map(|i| self.values[i])
    }

    /// 入れる。いっぱいなら入れない(返すときに戻せない)。
    pub(super) fn put(&mut self, id: u8, value: u32) {
        if let Some(i) = self.ids[..self.len].iter().position(|&i| i == id) {
            self.values[i] = value;
        } else if self.len < K {
            self.ids[self.len] = id;
            self.values[self.len] = value;
            self.len += 1;
        }
    }

    pub(super) fn iter(&self) -> impl Iterator<Item = (u8, u32)> + '_ {
        self.ids[..self.len]
            .iter()
            .copied()
            .zip(self.values[..self.len].iter().copied())
    }
}

/// BGM の 1 チャンネルの影の状態。
#[derive(Debug, Clone, Copy)]
pub(super) struct Shadow<const K: usize> {
    pub(super) tone: Option<u8>,
    /// BGM が最後に書いた波形メモリ(`@wave`)。
    pub(super) wave: Option<[i8; crate::WAVE_LEN]>,
    /// 波形メモリを音色より先に書いたか(どちらも波形メモリを書き換える音源で、戻す順を合わせる)。
    pub(super) wave_before_tone: bool,
    /// 音色か波形メモリを最後に書いた順番([`super::Interrupt`] の通し番号。0 は書いていない)。
    /// 波形メモリを共有するチャンネルどうしで、どちらが後に書いたかを比べる。
    pub(super) wave_seq: u32,
    pub(super) params: ParamTable<K>,
    /// BGM が初めて書く前の音源の値(`Synth::param` で読めたものだけ)。
    pub(super) initial: ParamTable<K>,
    pub(super) on: bool,
    pub(super) keys: [u8; CHORD_MAX],
    pub(super) nkeys: u8,
    pub(super) vel: u8,
    pub(super) pitch: i32,
}

impl<const K: usize> Shadow<K> {
    pub(super) const EMPTY: Self = Shadow {
        tone: None,
        wave: None,
        wave_before_tone: false,
        wave_seq: 0,
        params: ParamTable::EMPTY,
        initial: ParamTable::EMPTY,
        on: false,
        keys: [0; CHORD_MAX],
        nkeys: 0,
        vel: 0,
        pitch: 0,
    };
}

/// 割り込みの枠。
#[derive(Debug, Clone, Copy)]
pub(super) struct Slot<const K: usize> {
    pub(super) active: bool,
    pub(super) priority: u8,
    /// 効果音のトラック → 音源のチャンネル。
    pub(super) map: [u8; MAX_SFX_TRACKS],
    pub(super) tracks: u8,
    /// この効果音が書いた、音源全体のパラメータ。
    pub(super) touched: Bits,
    /// この効果音が書いた、トラック単位のパラメータ。
    pub(super) touched_track: Bits,
    /// 効果音が初めて書く前の音源の値(トラックごと)。返すときに戻す。
    pub(super) saved: [ParamTable<K>; MAX_SFX_TRACKS],
    /// 効果音が初めて書く前の音源全体のパラメータの値。
    pub(super) saved_global: ParamTable<K>,
}

impl<const K: usize> Slot<K> {
    pub(super) const EMPTY: Slot<K> = Slot {
        active: false,
        priority: 0,
        map: [0; MAX_SFX_TRACKS],
        tracks: 0,
        touched: Bits::EMPTY,
        touched_track: Bits::EMPTY,
        saved: [ParamTable::EMPTY; MAX_SFX_TRACKS],
        saved_global: ParamTable::EMPTY,
    };
}