6502 by dbhqGitHub

· All entries

The KIM-1

The KIM-1 is MOS Technology’s 1976 board: a 6502 at 1 MHz, two 6530 chips (each a ROM, a little RAM, two I/O ports and a timer), 1 KB of RAM, a hex keypad and six LED digits. It is the first machine on the core, and the first that can count towards “machines implemented”. It does not count yet: a machine counts when it runs in the browser as well as passing its test in CI, and the browser page is the next piece of work. Until then the registry shows it as in progress.

What was built

  • src/Dbhq.Machines.Kim1/, a library that depends only on the core. Rriot6530 is the 6530: ROM, RAM, two ports with data direction registers, and the interval timer. Kim1Bus is the address decoding and the clock: every read or write is one cycle, and both 6530s are clocked at the start of it. Kim1Keypad is the key matrix, Kim1Display the six digits, and Kim1Machine puts them together with the RS and ST keys and the SST switch.
  • tests/Dbhq.Machines.Kim1.Tests/, with Kim1AcceptanceTests as the suite the registry names.
  • The registry entry for kim-1: maker, rights, notes, the acceptance suite, and the status in-progress.

Nothing in the core changed. It still names no machine.

The ROM

Finding it

The monitor is 2 KB: 1 KB in the 6530-003 at $1800-$1BFF (the cassette routines) and 1 KB in the 6530-002 at $1C00-$1FFF (the keypad, display and teletype monitor). Four copies were downloaded and compared:

  • MAME’s KIM-1 driver (src/mame/commodore/kim1.cpp in mamedev/mame) does not ship the ROM; it records its hashes. 6530-002: CRC 2b08e923, SHA-1 054f7f6989af3a59462ffb0372b6f56f307b5362. 6530-003: CRC a2a56502, SHA-1 60b6e48f35fe4899e29166641bac3e81e3b9d220.
  • https://www.zimmers.net/anonftp/pub/cbm/src/kim-1/ (6530-002.bin and 6530-003.bin, dated 1981) and the brainwagon/kim-1 repository on GitHub, at commit 2daa8c8104315374dabda40855e2d668c13354b7, both matched MAME’s hashes exactly. brainwagon’s ORIG.bin is the two halves joined, 003 first.
  • Hans Otten’s KIM-1 ROMs page, http://retro.hansotten.nl/6502-sbc/kim-1-manuals-and-software/kim_1-roms/, offers two versions of each half. He writes that the copies found all over the internet are the result of assembling the source, with $FF in the unused bytes, and that “the original KIM-1 ROMs have a filler byte $00, verified by me dumping the real 6530-002 and 6530-003 RRIOT ROMs”. His $FF files match MAME’s hashes. His $00 files are different files.

cmp -l on the two versions, on 1 October, found 3 bytes different in the 6530-002 and 356 in the 6530-003, and every one of them was $00 in his dump against $FF in the assembled image. The code is identical.

What was chosen

Hans Otten’s $00 dumps, of the real chips. Dan asked for the original ROM, and these are the bytes the chips hold; the $FF images are what an assembler makes from the published source. No program can tell the two apart by running the monitor, but a program that reads an unused byte can.

Pinned to the Internet Archive’s copy of his files, not his site. His site answers over plain HTTP, but an HTTPS request fails before any data is sent (curl error 35, “sslv3 alert handshake failure”, on 1 October). The Wayback Machine’s raw copies (id_) of 23 June 2025 serve the same bytes over HTTPS from a URL that never changes:

ChipURLSHA-256
6530-002https://web.archive.org/web/20250623140338id_/http://retro.hansotten.nl/uploads/files/6530-002%20fillerbyte00.bine9e5245854603cdbc0208235310db1bf6e6a75904960385baaddd38fe60ef750
6530-003https://web.archive.org/web/20250623140339id_/http://retro.hansotten.nl/uploads/files/6530-003%20fillerbyte00.binf112a707188a82b87de5be78b7ffa014e18240aa08825055c90d2e6626092fd7

Both were checked on 1 October against his live HTTP files with sha256sum, and matched. For anyone comparing with other emulators: their CRC-32s are d63aa9be (002) and 9479e729 (003), and their SHA-1s d0b5ceb279303cad219d6609d7db03a120f59ab8 and 16caaf9d4c8327e0d3198887499fe30056df3084.

The pins live in tests/Dbhq.Cpu6502.TestSupport/Pins.cs beside every other third-party file, and are fetched through the same PinnedFiles.Fetch, into .testdata/kim-1/. That was chosen over keeping them in the machine library because it reuses the one mechanism there is, and because the CI cache key already hashes Pins.cs, so a new pin downloads afresh without anyone remembering to change the key.

Rights

Rule 4 in AGENTS.md, Dan’s decision of 1 October 2026: a machine’s system ROM is used when its rights are documented. This is that documentation, from what was found while building. A legwork research pass was started and stopped on Dan’s instruction.

  • Who wrote it. MOS Technology. The program listings in the KIM-1 User Manual carry “COPYRIGHT MOS TECHNOLOGY, INC DATE: OCT 18, 1975 REV-D” for the 6530-003 and “COPYRIGHT MOS TECHNOLOGY INC. DATE OCT 13 1975 REV E” for the 6530-002, and the manual itself ”© MOS TECHNOLOGY, INC. 1976 All Rights Reserved” (second edition, August 1976, read in the transcription at http://retro.hansotten.nl/uploads/6502docs/usrman.htm).
  • What happened to MOS. From https://en.wikipedia.org/wiki/MOS_Technology: Commodore bought MOS on 25 September 1976, and MOS became the Commodore Semiconductor Group. After Commodore’s bankruptcy in 1994 the group was bought by its former management and traded as GMT Microelectronics, which “discontinued operations and abandoned all of its assets” in 2000.
  • Who holds the copyright now: not established. Nothing opened today says whether it went with the semiconductor group or with Commodore’s other assets, or where either went after that.
  • Permission: none found from any holder. No licence, permission or statement of policy from MOS, Commodore, or a successor was found.
  • What others say, none of it a licence. Ruud Baltissen’s copyright page, http://www.baltissen.org/newhtm/copyrights.htm, says of his site’s Commodore material that “the holders of the brand name don’t mind as long as it is not used for commercial purposes”, with no source given. The brainwagon/kim-1 README adds “no special restrictions” of its own, “consistent with any pre-existing licensing”, which covers only that author’s work. Hans Otten’s ROMs page states no copyright or licence. A web search summary attributed a statement about a later owner’s policy to a copy of Baltissen’s page; the page as opened today does not contain it, so it is not relied on.
  • Where our copy comes from. Hans Otten’s dump of the real chips, through the Internet Archive URLs and hashes above. It is downloaded at test time, checked against its hash and kept in the git-ignored .testdata/. It is never committed.

Under rule 4, that is documented, and the ROM is used. The registry’s rights field says the same in two sentences, and the machine’s page will say whose ROM it runs.

What was read

  • The KIM-1 User Manual. The text transcription at Hans Otten’s site (above) for the memory map (section 3.2), the decoder (section 6.1), the keys and switches (chapter 4) and the 6530’s timer (Appendix H). The scanned second edition, KIM-1 User Manual 15A.pdf on the same site, to check one step of the first program (see below).
  • The MCS6530 data sheet, March 1976, from the Internet Archive item mos-6530 (MOS6530_djvu.txt, contributed by Andy Finkel): the ports, the pull-ups, the timer, the RES line and the address decoding.
  • Ruud Baltissen’s KIM-1 page, http://www.baltissen.org/newhtm/kim.htm: the full decode of $1400-$17FF, including that $1400-$16FF holds nothing, that each 64-byte I/O area repeats its 16 registers four times, and that single step is masked over the upper half of the ROM.
  • The monitor’s own source, in the manual’s listing and in the brainwagon/kim-1 cleanup, for what it does with the ports: SCAND at $1F19, CONVD, AK and GETKEY. That is where the keypad wiring was read from.
  • MAME’s KIM-1 driver, as a cross-check of facts only (it is GPL, and no code was taken): its key rows agree with the wiring read from GETKEY, its memory map with the decode above, and its single-step NMI with K7 masking.

Decisions

  • Two 6530s as one class, created twice, over a class per chip. The 6530-002 and 6530-003 differ only in their mask: what is in the ROM, and which value of A6 selects each. That difference lives in the bus.
  • The bus clocks both 6530s at the start of every access, before serving it, over clocking them after. A write that starts the timer then lands after that cycle’s tick, so the first decrement falls on the next clock, which is what the data sheet’s example needs.
  • The timer passes zero on clock N x k + 1 after the write, over the data sheet’s Figure 5, which says clock 416 for 52 at divide by 8. The data sheet’s own text says 417, and its two later readings agree with 417. Both are in docs/known-differences.md.
  • A read of the timer takes the interrupt enable from A3, as the data sheet says for every read and write, over Appendix H, which says a read after zero disables it from either address. Also in known differences. Nothing on a stock KIM-1 wires the timer’s interrupt to the CPU, so the monitor does not depend on it.
  • The keypad is wired as GETKEY decodes it: row r holds keys 7r to 7r + 6, the first on PA6 and the last on PA0, so each key’s number is the code the monitor returns for it. Chosen over a table copied from anywhere, because the board only works if wiring and ROM agree, and the acceptance test proves they do.
  • The display models the eye, not the LEDs. A digit shows each segment that was on for more than half of the time it was selected, and goes dark 20 ms after its last scan. Chosen over “the last value written”, which reads blank: CONVD turns the segments off before it moves to the next digit, so the last value on every digit is nothing.
  • The display is read from the segments, never from RAM. The monitor keeps the address it shows in RAM at $FA and $FB. A test that read those would pass with a broken scan. The tests read the digits.
  • Single step raises NMI on the opcode fetch outside K7 ($1C00-$1FFF), over stepping every fetch. The board masks the monitor’s half of the ROM so that the NMI handler is not itself stepped. The machine marks the first access of each instruction as the fetch, since the core has no SYNC line.
  • RAM starts as zeros, and the machine waits for RS, as the manual says to press RS after power on. Zeros were chosen over random contents so the boot test can say exactly what the digits show.
  • A read where nothing answers returns the last byte on the data bus. This is the usual NMOS behaviour and was not measured on a KIM-1; it is listed as modelled in known differences.
  • Out of scope: the teletype interface (the machine is always in keypad mode: PA0 stays high, as with no teletype jumper fitted), the cassette interface and the expansion bus. The registry’s notes say so.
  • The class is Kim1Machine, not Kim1, which would share its name with its own namespace and make every test file say Dbhq.Machines.Kim1.Kim1.
  • The status is in-progress, not running. The site counts a machine only when it runs in the browser, and that page does not exist yet.

The acceptance test

Kim1AcceptanceTests drives the machine the way a person does: a key held for 40 ms and let go for 40 ms, the digits read off their segments.

  • Boot. Before RS the digits are dark. After it the monitor shows 0000 00, the address in its pointer and the byte there, and a digit key goes into the address, so it is in address mode.
  • The User Manual’s first program, section 2.4, typed in exactly as the manual lists it, steps 1 to 23, with the display the manual prints checked after every step, x for “don’t care” included. GO runs it, it adds 02 and 03, and the address shown is 0005, as the manual says: “the result, 05, appears in the right two digits of the address display”. This was chosen over a program of our own because the manual gives an outside answer for every step.
  • Deposit, run, stop, read back. The NMI vector set to $1C00 by keypad (manual, section 4.6), a program that loads, adds, stores and loops, GO, the digits dark while it runs, ST, the stop address 020A shown, then the result read off the digits at 0212, from the saved accumulator at 00F3, and the PC key recalling 020A.
  • Single step: with SST on, each GO shows the next instruction’s address and byte.
  • The scan, not memory: a program that loops forever leaves the digits dark although the address is still in RAM.

The other suites check the 6530 against the data sheet’s worked example and at every prescale, the memory map and its mirrors, the ports against their direction registers, the RS key resetting the ports, and a five-million-cycle run of the monitor with the keypad in use on which every instruction fetched is a documented opcode inside the ROM.

What the tests showed

  • dotnet test tests/Dbhq.Machines.Kim1.Tests -c Release, on 1 October, after the last change: 35 passed, 0 failed.
  • Every assertion was shown to fail when its behaviour was broken. A script applied one change to the machine at a time in a scratch copy, rebuilt it and ran the KIM-1 tests: 30 changes, each named for the behaviour it breaks (the prescale after zero, the first decrement, divide by 1024, the flag on the clock it is set, the flag read, the ports’ direction registers, A3, RES, the 8 KB mirror, writes clocking the timers, RAM and I/O swapped, the two chips swapped, $1400-$16FF, open bus, SST’s K7 mask, SST on the fetch, ST, the key columns and rows, the digit order, persistence, the half-the-time rule, reading the display from RAM, one wrong ROM byte, a JAM in the display routine, and the two ROMs swapped). After one test was added, all 30 were caught.
  • The one that got through at first was RS not resetting the 6530s: nothing checked it, because the monitor sets up its own chip’s ports again after every reset. A test now writes $FF to the user port’s direction register with the keypad, presses RS, and reads 00 back off the digits.
  • The whole suite: dotnet test --configuration Release --logger trx --results-directory <folder> at the repository root, on 1 October, ended with 35 of 35 passing in the KIM-1 tests and 1,480 of 1,480 in the core’s.

Surprises and mistakes

  • The test results file was being overwritten. The workflows ran dotnet test with --logger "trx;LogFileName=results.trx". With one test project that was fine; with two, both write the same file, and the run said WARNING: Overwriting results file. The site’s figures would have lost one project’s suites without failing anything. The logger is now given no file name, and writes one file per project; a probe showed it adds [1] and [2] to a name rather than overwrite one. Both workflows and make-results.mjs changed, and the script now clears old .trx files before a fresh run, because it counts every file in the folder.
  • AD is a key and a byte. The first key script wrote the program’s LDA absolute ($AD) as AD, which pressed the AD key: the display read 0B02 0B where 020C 02 was expected. Key names are now in brackets, as the manual prints them on keycaps: [AD] 0200 [DA] AD.
  • The text copy of the manual drops two keys. Its step 21 reads [DA] and the display 0000 02. The scan of the manual shows [DA] [0] [2].
  • zimmers.net labels 6530-002.bin “the lower ROM”. The 6530-002 is the upper half, at $1C00. The file is right, and matches MAME’s hash; only the label is not.
  • The data sheet’s text, as OCR’d, says the I/O registers are addressed “when A2 is high”, but the KIM-1’s own addresses put the ports at A2 low ($1740-$1743) and the timer at A2 high ($1744-$174F). The figure that would settle it did not survive the OCR. The board’s addresses were followed.

Not done

  • The browser page. The next piece of work: the six digits, the keypad and the RS, ST and SST controls, on the same machine compiled to WebAssembly. Once it is live and the acceptance test passes in CI, the registry’s status can become running, and the site will count the KIM-1.
  • The teletype, the cassette and the expansion bus.
  • The timer’s interrupt is not wired to the CPU, as on a stock board. The manual says to wire PB7 to IRQ or NMI to use it.

CI no longer depends on anyone else’s server

On 2 October 2026 Dan ruled that CI must not depend on external sources, and that this is a rule for everything the tests and the build fetch, not only the KIM-1 ROM. Until then three kinds of third party sat in the path: GitHub repositories we did not own (Harte’s SingleStepTests, Dormann’s tests, nestest), perfect6502 for the generator, and the Internet Archive for the KIM-1 ROM, which had a single source.

What changed, in two steps. The four GitHub repositories became forks in the dbhq-uk organisation (65x02, 6502_65C02_functional_tests, nes-test-roms, perfect6502), and every pin points there with the same commits and hashes. The ROM went first into a new repository, dbhq-uk/6502-roms, and an hour later into this repository, under roms/, because Dan asked for everything in one place. The reason for the first choice, recorded because it was mine and it was wrong for this project: rule 3 then said nothing third-party was committed, this repository is MIT, and a separate repository kept that rule intact. But the files are 2 KB, not GPL, and they are the very thing a person reading this repository wants to see next to the code that runs them, so a second repository only added a place to look. The tests now read roms/kim-1/*.bin straight from the working tree and check each against its pinned SHA-256, so there is no download for the ROM at all. dbhq-uk/6502-roms was archived.

How it was checked.

  • gh api repos/dbhq-uk/<repo>/commits/<sha> returned each of the pinned commits from its fork.
  • A Harte file fetched from the fork (nes6502/v1/a9.json) hashed with git hash-object to d1f3288004da3d0f2869a2c0e6b37f533addf00a, the blob hash the manifest records for it.
  • With the cached Dormann and nestest downloads deleted, dotnet test fetched them again from the forks and passed, and the downloads were byte-identical to the earlier ones (diff -rq).
  • CI re-downloaded the 5 GB of Harte data from the fork and its test job passed in 4 minutes 10 seconds.
  • After the ROMs moved in, dotnet test --logger trx passed 1,520 tests in 20 suites (39 KIM-1, 1,481 core) and make-results.mjs counted the same.

The rule is enforced. site/tests/mirrors.test.mjs scans the test, tool, bench and source folders and the site’s scripts and fails on any fetch from a host outside dbhq-uk (comments and local addresses are exempt). It also requires every pin to be a full 40-character commit. AGENTS.md rule 3 says all of this.

What the rule does not cover, honestly. A fork is still on GitHub, so this removes our dependence on other people’s repositories and on the Internet Archive, not on GitHub itself. Package registries (npm, NuGet) and GitHub Actions are locked and pinned as before and are a separate question.