# SPDX-License-Identifier: MIT """The RIOT on its own: decode, ports, and the interval timer's edges (riot.py).""" from __future__ import annotations from a2600_python.riot import PA7_FLAG, RIOT, TIMER_FLAG INTIM, TIMINT = 0x284, 0x285 TIM1T, TIM8T, TIM64T, T1024T = 0x294, 0x295, 0x296, 0x297 def riot() -> RIOT: return RIOT(ram_fill=0, timer_value=0, timer_divider=1024, first_decrement=1024) def test_ram_and_its_mirror(): r = riot() r.write(0x80, 0x12, 0) assert r.read(0x180, 1) == 0x12 # the stack page is the same RAM assert r.read(0x0D80, 2) == 0x12 # only A9 and A0-A6 decode def test_first_decrement_one_cycle_after_the_write_then_every_interval(): r = riot() r.write(TIM8T, 10, 100) assert r.read(INTIM, 100) == 10 assert r.read(INTIM, 101) == 9 assert r.read(INTIM, 108) == 9 assert r.read(INTIM, 109) == 8 assert r.read(INTIM, 101 + 8 * 9) == 0 def test_wrap_sets_the_flag_and_counts_every_cycle(): r = riot() r.write(TIM64T, 2, 0) wrap = 1 + 64 * 2 # the third decrement assert r.read(TIMINT, wrap - 1) & TIMER_FLAG == 0 assert r.read(TIMINT, wrap) & TIMER_FLAG assert r.read(TIMINT, wrap + 5) & TIMER_FLAG # reading TIMINT leaves it set r2 = riot() r2.write(TIM64T, 2, 0) assert r2.timer_at(wrap) == 0xFF assert r2.timer_at(wrap + 3) == 0xFC def test_reading_intim_clears_the_flag_except_in_the_cycle_of_the_wrap(): r = riot() r.write(TIM1T, 0, 0) # value 0: the first decrement (cycle 1) wraps. assert r.read(INTIM, 1) == 0xFF assert r.flags & TIMER_FLAG # read in the wrap's own cycle: not cleared assert r.read(INTIM, 2) == 0xFE assert not r.flags & TIMER_FLAG def test_after_the_flag_clears_the_prescaler_keeps_its_phase(): """Stella #97's console results (C7): the interval resumes mid-period.""" r = riot() r.write(TIM8T, 0, 0) # wraps at cycle 1 assert 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 r.read(INTIM, 8) == 0xFB assert r.read(INTIM, 9) == 0xFA def test_a_write_reloads_and_clears(): r = riot() r.write(TIM1T, 0, 0) r.read(TIMINT, 3) r.write(T1024T, 3, 10) assert not r.flags & TIMER_FLAG assert r.read(INTIM, 11) == 2 def test_port_a_is_wired_and_and_the_edge_flag_is_cleared_by_timint(): r = riot() r.set_input_a(0xFF) r.write(0x281, 0xF0, 0) # SWACNT: high nibble output r.write(0x280, 0x5A, 0) assert 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 r.read(TIMINT, 4) & PA7_FLAG assert not r.read(TIMINT, 5) & PA7_FLAG def test_port_b_reads_switches_where_ddr_is_input(): r = riot() r.input_b = 0x3F r.write(0x283, 0x80, 0) r.write(0x282, 0x00, 0) assert r.read(0x282, 1) == 0x3F def test_a_write_in_the_cycle_of_the_wrap_keeps_the_flag(): """MCS6500 Hardware Manual p.78; Stella #606's console results (C6).""" r = riot() r.write(TIM8T, 0, 0) # wraps at cycle 1 r.write(TIM64T, 0x40, 1) assert r.flags & TIMER_FLAG assert r.timer_at(4) == 0x3D # still counting every cycle, from the new value def test_later_wraps_also_protect_the_flag(): """#606: accessing the timer on a later wrap, with the flag already set, behaves the same as on the first.""" r = riot() r.write(TIM1T, 0, 0) # wraps at cycle 1, then every 256 cycles assert r.read(INTIM, 257) == 0xFF assert r.flags & TIMER_FLAG r.write(TIM8T, 0x10, 513) assert r.flags & TIMER_FLAG r.write(TIM8T, 0x10, 520) assert not r.flags & TIMER_FLAG