Kjetil's Information Center: A Blog About My Projects

Terminal Mode Commodore 64 Emulator

Just for fun I have made a primitive Commodore 64 emulator, or more correctly mostly a 6502 CPU emulator that happens to run the Commodore 64 ROMs with just a few tweaks. It was mostly to learn about the 6502 CPU itself, so if you actually need a proper emulator, then use VICE instead! The goal was to be able to load and run the Commodore BASIC ROM, which it does, in a Linux terminal.

Anyway, I am releasing the source code under the MIT license and it can be downloaded here.

The 6502 CPU is fully emulated and even supports decimal mode, although only with NMOS (not CMOS) compatibility, so the N, V and Z flags are not set correctly. Nevertheless it passes the excellent test suite made by Klaus Dormann, and even some of the tests made by Wolfgang Lorenz.

To support the C64 architecture, some simple memory bank switching support has been implemented, and hooks are placed when reading and writing to certain memory locations to capture the user input and output. The emulator blocks on input when waiting for a keyboard press, which is hugely beneficial to CPU load. I thought about using curses, but this makes things more complicated so the emulator sets the terminal into canonical mode (with no echo) for the BASIC editor to work instead.

To find the ROMs the emulator assumes that you have VICE installed from before, which puts these at "/usr/lib64/vice/C64/". The location can be changed by editing a #define if they happen to be elsewhere.

Here is what it looks like running in XTerm, and you can also see the CPU disassembly/trace and memory dump functionality embedded into the emulator:

Terminal Mode Commodore 64 Emulator

Topic: Open Source, by Kjetil @ 01/06-2021, Article Link