// SPDX-License-Identifier: MIT //! Rung 1: the register model. Each case cites its rule in docs/behaviour.md. //! //! Transcribed from pokey-python's `tests/test_registers.py` (at 6ed1ffa). Not transcribed: //! `test_attribute_count_under_80`, which guards PyPy's 80-attribute limit on Python objects //! (nothing in Rust to guard). // The reference's expressions are kept as written (`0xFF & ~0x10 | 0x10`, fields set on a // fresh chip), so two style lints are off for this file. #![allow(clippy::identity_op, clippy::field_reassign_with_default)] use pokey::chip::{IRQEN, IRQST, KBCODE, SERIN, SKCTL, SKRES, SKSTAT}; use pokey::POKEY; fn chip_after(writes: &[(u8, u8)]) -> POKEY { let mut chip = POKEY::default(); for &(register, value) in writes { chip.write(register, value); chip.tick(); } chip } #[test] fn test_unmapped_reads_ff() { // B1.2: AHRM 5.1; Acid800 pokey_default.s expects $FF at $0C for register in [0x0B, 0x0C] { assert_eq!(POKEY::default().read(register), 0xFF); } } #[test] fn test_mirrors_use_a3_a0_only() { // B1.1: the board decodes chip selects and mirrors; the core sees A3-A0 let mut chip = POKEY::default(); assert_eq!(chip.read(0x1C), chip.read(0x0C)); assert_eq!(chip.read(0x0C), 0xFF); } #[test] fn test_power_on_is_init_mode_all_zero() { // B1.8 (decision 8): registers 0, so SKCTL = 0 is initialisation mode (B1.7) let mut chip = POKEY::default(); assert!(chip.init_mode()); assert!(chip.read(KBCODE) == 0 && chip.read(SERIN) == 0); assert_eq!(chip.read(IRQST), 0xF7); // B1.5: bit 3 low, the output shifter is idle assert_eq!(chip.read(SKSTAT), (0xFF & !0x10) | 0x10); // SID idles high (mark) } #[test] fn test_irqst_bit3_shows_whatever_irqen_says() { // B1.5: DS 23, AHRM 5.7: not a latch, visible with the enable clear assert_eq!(chip_after(&[(IRQEN, 0x00)]).read(IRQST) & 0x08, 0); assert_eq!(chip_after(&[(IRQEN, 0xFF)]).read(IRQST) & 0x08, 0); } #[test] fn test_irq_pin_bit3_only_when_enabled() { // B1.5 (C6, closed box): status visible, pin only with IRQEN bit 3 set assert!(!chip_after(&[(IRQEN, 0x00)]).irq); assert!(chip_after(&[(IRQEN, 0x08)]).irq); } #[test] fn test_write_reaches_the_logic_after_its_tick() { // B0.2: the write is applied at the end of its cycle's tick let mut chip = POKEY::default(); chip.write(SKCTL, 3); assert!(chip.init_mode()); chip.tick(); assert!(!chip.init_mode()); } #[test] fn test_irqen_zero_acknowledges() { // B1.4: writing 0 to an IRQEN bit sets its IRQST bit back to 1 let mut chip = chip_after(&[(IRQEN, 0x01)]); chip.latched |= 0x01; // a pending timer 1 interrupt assert_eq!(chip.read(IRQST) & 0x01, 0); chip.write(IRQEN, 0x00); chip.tick(); chip.tick(); assert_ne!(chip.read(IRQST) & 0x01, 0); } #[test] fn test_skres_resets_status_latches() { // B1.6: DS 22, AHRM 5.6/5.8: SKRES sets bits 7-5 back to 1 let mut chip = POKEY::default(); chip.kb_overrun = 1; chip.ser_overrun = 1; chip.frame_error = 1; assert_eq!(chip.read(SKSTAT) & 0xE0, 0); chip.write(SKRES, 0); chip.tick(); assert_eq!(chip.read(SKSTAT) & 0xE0, 0xE0); } #[test] fn test_skstat_sid_is_the_pin() { // B1.6: bit 4 reads the SID pin directly (AHRM 5.6 "Direct input") let mut chip = POKEY::default(); chip.sid = 0; assert_eq!(chip.read(SKSTAT) & 0x10, 0); chip.sid = 1; assert_ne!(chip.read(SKSTAT) & 0x10, 0); } #[test] fn test_seeded_power_on_differs_and_repeats() { // B1.8: power_on(seed) randomises what the data sheet leaves undefined let (mut a, mut b) = (POKEY::default(), POKEY::default()); a.power_on(Some(1)); b.power_on(Some(1)); assert_eq!(a.snapshot(), b.snapshot()); b.power_on(Some(2)); assert_ne!(a.snapshot(), b.snapshot()); } #[test] fn test_snapshot_restore_round_trip() { let chip = chip_after(&[(SKCTL, 3), (IRQEN, 0x41)]); let state = chip.snapshot(); let mut other = POKEY::default(); other.restore(&state); assert_eq!(other.snapshot(), state); }