//! Save states (the `serde` feature; not in ym2612-python, which has none): the whole chip //! goes through bincode half way through a busy stream and carries on identically. #![cfg(feature = "serde")] mod helpers; use helpers::*; use ym2612::{YM2612, YM2612_DISCRETE, YM3438_ASIC}; fn busy(fm: &mut YM2612) { for channel in 0..6 { voice( fm, Voice { channel, algorithm: (channel % 8) as u8, total_levels: [20, 10, 30, 0], feedback: 3, attack: 24, ..V }, ); key(fm, channel, 0xF); } op_reg(fm, 1, 3, 0x90, 0x0A); // SSG-EG reg(fm, 0x22, 0x0B); // LFO reg(fm, 0xB4, 0xF5); reg(fm, 0x24, 0xF8); reg(fm, 0x26, 0xF0); reg(fm, 0x27, 0x9F); // CSM, both timers reg(fm, 0xA4, 0x22); // a pending high byte fm.write(0, 0x2A); // a latched address } #[test] fn a_restored_chip_continues_identically() { for variant in [YM2612_DISCRETE, YM3438_ASIC] { let mut fm = YM2612::new(variant); busy(&mut fm); fm.run(144 * 777 + 55); let bytes = bincode::serialize(&fm).unwrap(); let mut restored: YM2612 = bincode::deserialize(&bytes).unwrap(); assert_eq!(restored, fm); for value in [0x10u8, 0x90, 0xF0] { fm.write(1, value); restored.write(1, value); fm.run(144 * 400 + 7); restored.run(144 * 400 + 7); assert_eq!(fm.read(0), restored.read(0)); } assert_eq!(fm.take_samples(), restored.take_samples()); assert_eq!(fm, restored); } } #[test] fn an_unknown_variant_name_is_refused() { let fm = YM2612::new(ym2612::FMVariant { name: "YM2612", ..YM3438_ASIC }); let bytes = bincode::serialize(&fm).unwrap(); let restored: YM2612 = bincode::deserialize(&bytes).unwrap(); assert_eq!(restored.variant, fm.variant); let json_like = bincode::serialize(&ym2612::FMVariant { name: "OPN2C", ..YM3438_ASIC }) .unwrap(); assert!(bincode::deserialize::(&json_like).is_err()); }