// SPDX-License-Identifier: MIT //! The RIOT on its own: decode, ports, and the interval timer's edges (the //! reference's tests/test_riot.py). use a2600::riot::{PA7_FLAG, RIOT, TIMER_FLAG}; const INTIM: u16 = 0x284; const TIMINT: u16 = 0x285; const TIM1T: u16 = 0x294; const TIM8T: u16 = 0x295; const TIM64T: u16 = 0x296; const T1024T: u16 = 0x297; fn riot() -> RIOT { RIOT::new(0, 0, 1024, 1024) } #[test] fn ram_and_its_mirror() { let mut r = riot(); r.write(0x80, 0x12, 0); assert_eq!(r.read(0x180, 1), 0x12); // the stack page is the same RAM assert_eq!(r.read(0x0D80, 2), 0x12); // only A9 and A0-A6 decode } #[test] fn first_decrement_one_cycle_after_the_write_then_every_interval() { let mut r = riot(); r.write(TIM8T, 10, 100); assert_eq!(r.read(INTIM, 100), 10); assert_eq!(r.read(INTIM, 101), 9); assert_eq!(r.read(INTIM, 108), 9); assert_eq!(r.read(INTIM, 109), 8); assert_eq!(r.read(INTIM, 101 + 8 * 9), 0); } #[test] fn wrap_sets_the_flag_and_counts_every_cycle() { let mut r = riot(); r.write(TIM64T, 2, 0); let wrap = 1 + 64 * 2; // the third decrement assert_eq!(r.read(TIMINT, wrap - 1) & TIMER_FLAG, 0); assert_ne!(r.read(TIMINT, wrap) & TIMER_FLAG, 0); assert_ne!(r.read(TIMINT, wrap + 5) & TIMER_FLAG, 0); // reading TIMINT leaves it set let mut r2 = riot(); r2.write(TIM64T, 2, 0); assert_eq!(r2.timer_at(wrap), 0xFF); assert_eq!(r2.timer_at(wrap + 3), 0xFC); } #[test] fn reading_intim_clears_the_flag_except_in_the_cycle_of_the_wrap() { let mut r = riot(); r.write(TIM1T, 0, 0); // value 0: the first decrement (cycle 1) wraps. assert_eq!(r.read(INTIM, 1), 0xFF); assert_ne!(r.flags & TIMER_FLAG, 0); // read in the wrap's own cycle: not cleared assert_eq!(r.read(INTIM, 2), 0xFE); assert_eq!(r.flags & TIMER_FLAG, 0); } /// Stella #97's console results (C7): the interval resumes mid-period. #[test] fn after_the_flag_clears_the_prescaler_keeps_its_phase() { let mut r = riot(); r.write(TIM8T, 0, 0); // wraps at cycle 1 assert_eq!(r.read(INTIM, 5), 0xFB); // every cycle since the wrap; clears the flag // The prescaler was loaded at 0 with decrements due at 1, 9, 17, ... assert_eq!(r.read(INTIM, 8), 0xFB); assert_eq!(r.read(INTIM, 9), 0xFA); } #[test] fn a_write_reloads_and_clears() { let mut r = riot(); r.write(TIM1T, 0, 0); r.read(TIMINT, 3); r.write(T1024T, 3, 10); assert_eq!(r.flags & TIMER_FLAG, 0); assert_eq!(r.read(INTIM, 11), 2); } #[test] fn port_a_is_wired_and_and_the_edge_flag_is_cleared_by_timint() { let mut r = riot(); r.set_input_a(0xFF); r.write(0x281, 0xF0, 0); // SWACNT: high nibble output r.write(0x280, 0x5A, 0); assert_eq!(r.read(0x280, 1), 0x5F); // outputs drive their bits, inputs read pins r.write(0x284, 0, 2); // edge select (A4 = 0): negative edge on PA7 r.write(0x281, 0x00, 3); r.set_input_a(0x7F); // PA7 falls assert_ne!(r.read(TIMINT, 4) & PA7_FLAG, 0); assert_eq!(r.read(TIMINT, 5) & PA7_FLAG, 0); } #[test] fn port_b_reads_switches_where_ddr_is_input() { let mut r = riot(); r.input_b = 0x3F; r.write(0x283, 0x80, 0); r.write(0x282, 0x00, 0); assert_eq!(r.read(0x282, 1), 0x3F); } /// MCS6500 Hardware Manual p.78; Stella #606's console results (C6). #[test] fn a_write_in_the_cycle_of_the_wrap_keeps_the_flag() { let mut r = riot(); r.write(TIM8T, 0, 0); // wraps at cycle 1 r.write(TIM64T, 0x40, 1); assert_ne!(r.flags & TIMER_FLAG, 0); assert_eq!(r.timer_at(4), 0x3D); // still counting every cycle, from the new value } /// #606: accessing the timer on a later wrap, with the flag already set, /// behaves the same as on the first. #[test] fn later_wraps_also_protect_the_flag() { let mut r = riot(); r.write(TIM1T, 0, 0); // wraps at cycle 1, then every 256 cycles assert_eq!(r.read(INTIM, 257), 0xFF); assert_ne!(r.flags & TIMER_FLAG, 0); r.write(TIM8T, 0x10, 513); assert_ne!(r.flags & TIMER_FLAG, 0); r.write(TIM8T, 0x10, 520); assert_eq!(r.flags & TIMER_FLAG, 0); }