6502 by dbhqGitHub

· All entries

The Atari VCS's RIOT

Task 3 of the Atari 2600 plan: Riot6532, the 6532 RIOT, chip alone, built from riot.md. Nothing runs a program yet: the bus is task 4, and the suite’s riot/ tests run from there, so none is claimed here.

What was built

src/Dbhq.Machines.Atari2600/Riot6532.cs, with the plan’s interface: 128 bytes of RAM, ports A and B with their direction registers, the interval timer, the PA7 edge detect, the flag register and the chip’s own IRQ pin, which goes nowhere on the 2600. ReadRegister is the CPU’s read with its side effects; PeekRegister returns the same byte with none, for the bus’s Peek. The chip has no clock of its own: the bus calls Tick once at the start of every CPU cycle, halted or not, then makes the cycle’s access. So a timer write’s cycle is the sheet’s cycle 0 and the count first falls on cycle 1. The register methods decode A0 to A4 only and the RAM methods A0 to A6, so the bus can pass any address of the chip’s half, mirrors included.

It is a new class, as the spec decided. The KIM-1’s Rriot6530 was read for its shape (the timer’s countdown and its “set this cycle” guard came out the same way) and nothing was taken from it or changed in it.

The 6530 against the 6532, row by row

riot.md section 1 checked the spec’s table against the data sheets. As built:

RowVerdictIn the code
MemoryConfirmed: no ROM, 128 bytes of RAMRamSize, A0 to A6
Selecting RAM or I/O6530 corrected (a mask-programmed decode of RS0 and A6 to A9), 6532 confirmed: RS, which the 2600 wires to A9The bus’s, in task 4; the chip has separate RAM and register methods
Interrupt outputConfirmed: a pin of its own, not connectedIrq, for the chip’s tests only
PA7 edge detectConfirmed, with detail: polarity and enable come from the write’s address (A0 and A1), the data is ignored, and the flag is set whether or not the enable is onWriteRegister with A2 high and A4 low
Reading the flagsConfirmed, with detail: bit 7 the timer, bit 6 the edge, bits 5 to 0 zero; reading clears the edge flag only; the timer flag is cleared by a timer read or writeReadRegister with A2 and A0 high
TimerConfirmed, and the open point settled: the flag is set on cycle D x N + 1, and a read on that very cycle returns $FF and leaves the flag setTick and ReadRegister

Decisions

  • Tick runs before the cycle’s access, as the KIM-1’s does, over after it. The sheet’s cycle numbers then fall out with no offset: the write is cycle 0 and its own cycle does not count. Task 4’s Cycle calls it in that order.
  • The edge detect runs when the pin can change (an ORA or DDRA write, or a new PortAInput), over comparing the pin on every Tick. A flag set on the change is there for a read on any later cycle, which is all the suite needs, and Tick, which runs every cycle, does no work for the ports. When the real flag becomes visible is not stated (riot.md section 12 item 5) [guessing - verify].
  • A port A output pin is the AND of ORA and what drives it, so an output at 1 reads 0 while its switch is closed. This is riot.md section 5’s inference, marked [guessing - verify] there and in the code; task 9 decides it for the joysticks and may change it. Port B’s outputs are push-pull and read ORB, which the sheet does state.
  • The inputs default to $FF, nothing pressed and every pull-up high, so the chip alone reads as a 2600 with nothing plugged in. Task 9’s controllers set them.
  • The power-on state. RES’s part as the sheet gives it (the I/O registers zeroed, both interrupts off, the negative edge armed). The rest is the model’s choice, so runs repeat: the RAM clear, both flags clear, and the count at $00 at divide by 1, so the first cycle passes zero and the timer free-runs, as the real one is taken to have since its own reset ended, before the CPU’s: that rests on the two reset circuits’ RC values, which riot.md section 9 marks [guessing - verify]. Chosen over a count at $00 waiting at divide by 1024, which a real chip would not be doing by the time the program starts. In docs/known-differences.md.
  • The constructor powers the chip on, so a new chip and a powered-on one are the same.

What is guessed, and marked

Each is [guessing - verify] in a comment in Riot6532.cs and in docs/known-differences.md:

  1. After a read ends the free run, the first fall comes a whole divider after the read (riot.md section 12 item 1).
  2. A read on the cycle the flag is set leaves the free run going, as it leaves the flag (section 12 item 2).
  3. In the free run, when the count wraps past $00 again, a read on that cycle clears the flag: only the first pass of zero is “the interrupt occurring”. No source reaches this; the KIM-1’s Rriot6530 protects a read on every wrap, which is the other reading.
  4. A port A output at 1 reads 0 against a closed switch (section 5).
  5. The edge flag is seen from the cycle the pin changes (section 12 item 5).
  6. PB2, PB4 and PB5 read 1 as inputs, through the pull-ups; no source states the value read on a 2600 (section 12 item 6).
  7. The real timer has been running since the RIOT’s own reset ended, before the CPU’s, which the power-on choice leans on (section 9).

The tests, test first

tests/Dbhq.Machines.Atari2600.Tests/Riot6532Tests.cs, each test named after the section or worked example it is taken from: RAM and its address-identity march (section 4); the decode’s mirrors and A4 on a write (worked example 1); port A’s output register (worked example 2) and port B’s (worked example 3); the timer at each divider (worked example 4), the count on every cycle by section 6’s rule, the data sheet’s own case of 52 at divide by 8, a write of 0, the flag’s clearing rules (worked example 5), the read on the cycle it passes zero (worked example 6, the suite’s timer-underflow-read), the suite’s timer-vblank-spin counts at one-cycle resolution (worked example 7); the enables and the IRQ pin (worked example 8); the PA7 edges (worked example 9), arming that sets nothing, the ignored data, and edges from the joystick and from DDRA; power on; and PeekRegister against a twin chip that is never peeked.

The file was written before the chip: the first build failed to compile with CS0246, Riot6532 not found. With the chip written, every test passed on the first run. On 14 October 2026 dotnet test -c Release tests/Dbhq.Machines.Atari2600.Tests --filter "FullyQualifiedName~Riot6532Tests" passed 43 test cases, and the whole project (dotnet test -c Release tests/Dbhq.Machines.Atari2600.Tests) passed 249. dotnet build -c Release of the solution gave no warnings.

Every test shown able to fail

The fault pass ran on 14 October 2026 in a scratch copy outside the repository (python3 -I tools/probes/atari-2600/riot-faults.py /tmp/atari-riot-faults, which copies the projects, plants one fault at a time in Riot6532.cs, builds, runs Riot6532Tests and puts the file back). Two faults, T4 and D1, did not build at first, because warnings are errors (an unused field and unreachable code); they were rewritten to build and run again on their own (... riot-faults.py /tmp/atari-riot-faults-2 T4 D1). There is one test file, Riot6532Tests, so every fault was written against it. The figure is the number of its test cases that failed.

FaultPlantedTest cases that failed
T1first fall a whole divider after the write16
T2flag set on reaching zero, one cycle early (the 1981 figure)24
T3count after zero held at the divider8
T4a read on the flag’s cycle clears it2
T5TIMINT read clears the timer flag5
T6INTIM read leaves the timer flag5
T7a read after zero leaves the free run1
T8the re-armed divider one cycle sooner1
T9dividers 8 and 64 swapped11
T10a timer write leaves the flag8
T11the free run’s wrap protects a read1
D1A4 not decoded on a write: edge control starts the timer4
D2the port registers decode A31
D3TIMINT drives bits 5 to 0 from the count2
D4RAM of 64 bytes on write1
I1an INTIM read ignores A31
I2a timer write ignores A32
I3the IRQ pin ignores the enables3
E1the edge polarity inverted10
E2TIMINT read leaves the edge flag9
E3an INTIM read clears the edge flag1
E4arming sets the flag when the level matches3
E5the detector ignores DDRA3
E6the detector ignores the joystick2
E7the edge interrupt enable from A01
P1port A output reads ORA, not the pin1
P2port B output reads the pin1
P3an ORA write kept only for output bits2
P4port B reads its output register for every bit3
O1power on keeps the RAM1
O2power on keeps ORA and DDRA1
O3power on keeps the polarity2
O4power on keeps the interrupt enables1
O5the power-on timer at divide by 10241
O6a new chip not powered on6
O7power on keeps the flags1
K1Peek of TIMINT clears the edge flag2
K2Peek of INTIM clears the timer flag2
K3Peek of INTIM ends the free run4

No fault survived, and no test case went uncaught: the probe lists each test case with the faults that made it fail, and every one has at least one. The cases reached by a single fault only:

  • Ram_holds_all_128_bytes_and_no_two_addresses_share_a_cell: D4
  • Section_5_a_port_a_output_reads_low_while_its_switch_pulls_it_low: P1
  • Section_5_an_input_pin_reads_what_drives_it: P4
  • Section_6_the_count_on_every_cycle_by_the_rule(timer: 660, divider: 1): T2
  • Worked_example_1_read_028c_is_intim_and_sets_the_timer_interrupt_enable: I1
  • Worked_example_1_the_port_registers_repeat_every_eight_and_ignore_a5_and_a6: D2
  • Worked_example_2_port_a_output_register_keeps_all_eight_bits: P3
  • Worked_example_6_the_read_on_the_cycle_it_passes_zero(v: 16, intim: 0, flagOnCycle23: True): T2

After the review

The review of this task, the same day, planted four faults that every test above let through, and found the test that should have caught each one weaker than its name. Each test was strengthened, and each fault was added to riot-faults.py and run against the new tests alone (python3 -I tools/probes/atari-2600/riot-faults.py <scratch> I4 I5 E8 D5, 14 October 2026). Each was caught by the one test case written for it:

FaultPlantedCaught by
I4any A2-high read sets the timer enable from A3Worked_example_8_the_timer_enable_follows_a3_on_every_timer_read_and_write, which now reads TIMINT at $0285, where A3 is 0, with the enable on
I5an edge control write sets the timer enable from A3the same test, which now writes $0286 with the enable on
E8a port read clears the edge flagSection_8_only_a_timint_read_clears_the_edge_flag, which now reads all four port registers with the flag set
D5RAM writes decode A0 to A7Ram_offsets_above_127_reach_the_cell_a0_to_a6_pick, new: the bus passes a mirror’s address whole

The old TIMINT check read $028D, whose A3 is 1, with the enable already on, so a fault that set the enable from A3 could not show. The review also found that the guesses in docs/known-differences.md were described as guesses but not tagged; each now carries [guessing - verify], the power-on bullet and the comment on PowerOn no longer state the timer’s run before the CPU’s reset as fact, and PB2, PB4 and PB5 reading 1 joins the list above as guess 6, with the timer’s early run as guess 7.