Caleb Connolly's 6502.sh Is a Fully-Functional MOS 6502 Emulator Written as a Shell Script

Taking up around 3,000 lines BusyBox ash script, 6502.sh includes an emulated serial port and can run a BASIC ROM.

ghalfacree
8 months ago Retro Tech

Kernel engineer Caleb Connolly has created a working emulator for the MOS Technology 6502 in possibly the least-likely language possible: the ash shell scripting language.

"I'm proud to announce 6502.sh, because the world needs another 6502 emulator," Connolly says of their creation. "6502.sh is about 3k lines of busybox ash compatible shell script, [and] it provides an emulated ACIA serial port and is capable of running BASIC. It has an integrated interactive debugger, with breakpoints, single stepping, and a myriad of other features."

Sure, you've seen a 6502 emulator before — but have you ever seen one written as a shell script? (📷: Caleb Connolly)

The MOS Technology 6502, developed by Chuck Peddle and colleagues and launched in 1975 as the at-the-time cheapest microprocessor around, has a long and storied history as the driving force behind some of the biggest names in personal computing: Apple's early systems, Commodore's 64 family, the BBC Micro, and even consoles like the Atari 2600 and Nintendo Entertainment Systems (NES) were powered by the chip, which is still in production at Western Design Center (WDC) today.

While you can still get your hands on real-silicon 6502s, it's also a heavily-emulated and -replicated part: emulators for almost every 6502-based system are readily available, as are FPGA recreations of the chip; four years ago PragmatIC launched a reimplementation built on a fully-flexible substrate. Connolly's recreation is strictly a software emulation, but in an usual medium: a BusyBox ash-shell compatible shell script.

"Loading the ROM was a bit of a headscratcher, for a while I did something really cursed reading hexdump output into a variable and then using subshells and calling memset for each byte but this was PAINFULLY slow," Connolly writes of some of the challenges experienced in writing the emulator. "Then I discovered that BusyBox hexdump is actually really powerful, and it can directly output in the format I picked.

The project includes built-in unit tests, and can run Steve Wozniak's monitor, wozmon, and a BASIC ROM. (📷: Caleb Connolly)

"If you run with the -d flag then all the debug logs get written to a socket," Connolly adds, "so you can interact with the emulator and see the instructions being decoded and executed in a second terminal. This felt super cool to implement and have actually work. Often times one terminal just isn't enough for all the logging you want."

The project's source code is available on Codeberg under an unspecified license; technical details of its implementation can be found in Connolly's Mastodon thread.

ghalfacree

Freelance journalist, technical author, hacker, tinkerer, erstwhile sysadmin. For hire: freelance@halfacree.co.uk.

Latest Articles