BeagleBone and 6502 – The Ultimate Geek Toys
I recently attended the Embedded Linux Conference – Europe 2012 in Barcelona. It was great to see all my open source friends (and meet some new ones) from around the globe. By far, the most interesting and entertaining technical session that I attended was presented by Matt Porter. It was titled “What’s Old Is New: A 6502-based Remote Processor”. In summary, Matt developed a working prototype of a BeagleBone integrated with a 6502 microprocessor connected in such a way that the Linux RemoteProc facilities can manage it and treat it as a Linux remote processor resource. The details were spectacular, especially for those in an age group similar to my own.
One of the first computers (thought not actually the very first) that I owned was the popular Commodore VIC-20 which contained a 6502 processor. I logged many hours playing Gorf on my VIC-20 connected to my Heathkit console model color TV. I also used it as a learning platform, building custom hardware gadgets and software that worked with it. I suspect some of you reading this had similar experiences with either this or a similar platform. As it turns out, there is a vibrant hobbyist community around “retro” processors and tools such as the 6502. However, I also discovered that the 6502 still has a vigorous commercial market. The commercial home of the 6502, Western Design Center makes some bold claims about annual volumes in the hundreds of millions of units annually! (http://www.westerndesigncenter.com/wdc/)
The design was conceptually quite simple, though some of the actual techniques used to realize the design turned out to have some interesting complexity. I won’t go into those details – you can find Matt’s presentation online at elinux.org (http://goo.gl/ZEkkB) and you can watch a recording of his session for those gory details. (Note: as of the date of this writing, the videos have not yet been published. Keep checking at elinux.org and/or linuxfoundation.org.)
This project consisted of connecting a bare 6502 processor (using only 4 octal bus transceivers) directly to the BeagleBone’s TI Sitara™ AM335x system-on-chip (SOC). It turns out the the AM335x SOC used on the BeagleBone has a cool subsystem called Programmable Realtime Unit (PRU). It is basically a dual-core, 32-bit RISC processing engine with single-cycle instruction execution (so long as there are no off-subsystem accesses.) This determinism makes this subsystem very suitable for a variety of tasks with typical real time characteristics. In his presentation, Matt called it “The Ultimate Bitbanger”.
The PRU was used to implement a bit-banged memory subsystem for the 6502 processor. Reset was controlled by a GPIO pin, and the clock for the 6502 was supplied by an on-chip PWM. In his presentation, Matt described the actual PRU assembler code he developed for the memory low-level read and write cycles that executed on a PRU 32-bit core.
(Re)Using Open Source
Perhaps one of the most interesting aspects of this project is just how much open source infrastructure already existed to make this project a reality. The Linux kernel’s RemoteProc framework was used to manage the “remote” 6502. RemoteProc was also responsible for downloading the firmware to the 6502 and bring the microprocessor out of reset. This is the very same infrastructure that manages multi-core systems and downloads firmware to modules on platforms that contain remote processors. A good example of the use of RemoteProc is in managing the DSPs commonly found in cell phones and other network gear.
The toolchain used for compiling and linking 6502 code is freely available for download. It is called cc65 and can be found at http://www.cc65.org. This toolchain is easily compiled on your favorite host machine. It is claimed to be supported on many host operating systems including Linux, MAC OS X and that “other” popular desktop operating system. Indeed, in this project, the 6502 C tools were run directly on the ARM Cortex-A8 based BeagleBone running an Angstrom distribution.
The point is that very little original software needed to be written for this project to be realized. The RemoteProc framework was used almost unmodified. Matt added a simple userland interface to boot/halt a remote processor from userspace, as this feature is only available from kernel space in the framework. He also wrote a small driver stub for RemoteProc which describes the 6502/PRU hardware particulars (SRAM, clock and reset, etc). Due to some technical limitations (for details, view the video) the virtual console infrastructure used by the RemoteProc framework could not be used, so Matt had to write a trivial virtual console driver. Beyond the code that ran on the PRU, virtually all of the software used to make this work was either Linux kernel mainline or freely available on the Internet, with minor or no modifications.
One of the requirements that Matt set for himself in this project was to be able to exercise the infrastructure from userland. It would have been cumbersome at best if he required kernel context (a driver) just to download and run a hello world on the 6502. A userspace loader program (
b6502_pruss) was used to download the PRU firmware and start it running. This firmware was the heart of the “hardware” design which implemented the bitbanging algorithm that operated the 6502 memory bus.
The 6502 toolchain was used to compile and link the programs that the 6502 was to execute. The steps to compile and run a hello world program on the 6502 looked like this:
- Write and compile (assemble and link) the hello world program.
- Copy the resultant binary to the standard location on Linux machines where firmware is stored (
/lib/firmwareon most distros).
- Using the
b6502_prussprogram, download the PRU firmware and start it running.
- Poke a location in
/systo start the 6502 processor. (This is the userspace hack to boot/halt a remote processor that Matt added to RemoteProc.) Note that the RemoteProc framework is also responsible for locating and downloading the firmware image (in this case, the 6502 hello world application) to the 6502. The firmware image is downloaded to the PRU module’s SRAM, which is mapped to the 6502 bus.
The highlight of the demo at the end of the presentation showed the legendary WOZ monitor (http://www.sbprojects.com/projects/apple1/wozmon.php) slightly modified for this project, running on the 6502 microprocessor through a Linux virtual console. Of course, the Woz monitor had to be modified to accomodate a virtual console through the PRUs SRAM. The room broke into applause when Matt hand-typed in a short assembly language program and ran it, without referring to a script or document. That is, he had these op codes committed to memory. I suspect the reason for the applause was because, like me, many of those in attendance can recall entering lengthy assembly language programs where many of the common op codes were stuck in our brain, and we didn’t need any references for it. For those of us packed into this ELC-E 2012 session, our familiarity with computing platforms of this era contributed to our collective enjoyment of Matt’s session.
BeagleBone Can Do That
I’m sure I’m not alone when I say that Matts ELC-E presentation helped me to realize the flexibility of the BeagleBone, and the family of TI Sitara™ AM335x SOCs. I didn’t realize that several models of the Sitara™ family contain a dual-core 32-bit RISC realtime capable processing engine (PRU) on the chip. I can certainly think of several applications for such a subsystem ranging from communications to medical and industrial systems. Of course, the PRU is but a small subset of capability in this family of ARM Cortex-A8 based SOCs. The best news is that there is plenty of information readily available from TI and on the Internet in the form of open source projects for just about any type of design based on the AM335x SOC.
Thanks to Matt Porter for suffering through my questions while writing this.
- Embedded Device Security is Everyone’s Business
- AMD R-Series and Mentor Embedded Linux – My Embedded Graphics Playground
- strace: My Favorite Tool for Debugging and Profiling Linux Applications
- Achieving Faster Linux Boot Time: 2013 Version
- BeagleBone and 6502 – The Ultimate Geek Toys
- Fun with toolchain versions
- More on Yocto Terminology – recipes and packages
- Yocto versus Poky versus Angstrom, etc
- Thoughts on Embedded Linux Terminology
- Welcome to my little corner of the world