Happy 50th, 6502
I’ve lately become infatuated with the 6502 processor. It started when I backed the NES Maker project on Kickstarter around 2018. This WYSIWYG tool allows people to create games for the NES without any coding – everything can be done through the editor. However, as with most tools, more can be done if you’re willing to dig into the source code a bit.
The main issue with the source code for an NES is that it’s written in assembly. I’ve got experience with Python, Java, and C++, but I’ve never written a lower-level language like assembly before, so I had some learning to do. Once I started reading though, I was intrigued.
The NES uses a modified 6502 processor, coupled with a Picture Processing Unit (PPU) and an Audio Processing Unit (APU) to play games. The code is provided to the system through the cartridge, and between the two some of the most engaging games of my childhood were produced.
Source
I think I’m getting ahead of myself a bit. Where did the 6502 come from?
The 6502 was invented in 1975 by MOS (as I’ve learned, it’s em-oh-ess, not moss) and was, at the time, the cheapest microprocessor available. It was released at the Wescon trade show and sold out of two big glass jars for $25 each, which was significantly less expensive than other microprocessors. (For example, the Z-80 cost $200 at the time.) The low cost of the 6502 made it incredibly popular for decades.
“We had two glass jars filled with chips,” he says, “and I had my wife sit there selling them.” (In 2016, Peddle admitted that at the time he had only enough working processors for the upper layers of the jars—they were mostly filled with nonworking chips.) Source
As I mentioned above, the NES ran on a modified version of the chip. So did the Atari 2600, the Commodore 64, the Apple II, many arcade games including Asteroids and Tempest, and even the Furby. The 6502 and its variants were so popular that it would be impossible to list all of them here. In fact, some of the toys my kids played with in the last decade were based on the 6502. This chip had some serious legs.
So what does coding in assembly look like? Well, it’s complex. A lot more complex than a language like Python. While assembly on the 6502 is not technically a RISC language, it only has 56 instructions available. One of the glaring omissions to a modern programmer is the lack of a multiply instruction. If a user wants to multiply, they have to follow some pretty advanced instructions.
For those who are interested, here's the 1975 6502 programming manual in all of its glory.
I’m not going to get into the weeds about how programming in assembly works, mostly because there are so many other people who have done a better job than I could ever do. With that said, I really like working so closely with the processor. Instead of traditional variables there are memory addresses, and instead of OOP there are subroutines and loops. Programming for the 6502 and its variants requires a level of attention and detail that I've not dealt with before. I had to pay attention to processor flags for the first time, and overlooking something simple like a number going above 255 means I have an overflow problem. There are solutions to that problem, but it involves the carry flag, which is another detail to consider.
I'm not sure that there's a practical use to learning this now, but it's been a fun exercise. The 6502 is a limited processor, but people were able to use those limitations to create some pretty amazing things. Without it I’m not sure what the modern state of PCs would look like. Would we have Apple? Would the NES have been too expensive to develop? It's been a pretty fun bit of computing history to learn, and in doing so I understand how modern machines work a lot better.
So happy birthday, 6502. May you continue to inspire people to create unique things for another 50 years.
Further reading/viewing
Classic Game Programming on the NES book
A blog by Michał Sapka who is working through assembly on the C64