This story begins around 1988-89 when I first saw ZX Spectrum clone based on Zilog Z80 microprocessor. It had Sinclair BASIC and ran off the cassette recorder. This first computer was responsible for the interest in computers that I have ever since.
Fast-forward to nowadays, I continue to be interested in how hardware and software work and watched quite a few reverse engineering videos on YouTube, like Apollo Guidance Computer restoration series on CuriousMarc channel and others. Due to this YT algorithm suggests me similar videos. A couples of days ago it suggested me to watch the 27c3: Reverse Engineering the MOS 6502 CPU video about how 6502 CPU was reverse engineered by peeling layers from the chip, taking photographs and then reconstructing actual transistors from the photos. The talk was given by Michael Steil who extensively contributed to documenting everything there is about MOS 6502 CPU and various types of BASIC it ran.
The people who reversed engineered the MOS 6502, Greg James, Brian and Barry Silverman also wrote a JavaScript visualization of the inner workings of the 6502 as it chugs alone.
It turns out that Michael Steil implemented a C based simulation of the MOS 6502 and hooked it up to the Commodore BASIC emulator to see how it worked. And it worked indeed!
In the rest of the post I’ll explain how to run that perfect MOS 6502 CPU simulation that runs Commodore 64 BASIC V2 emulator in WSL2 Ubuntu. So hold on.
Prerequisites
- Interest in this topic 🙂
- Operating System: Linux or Windows 10 with Windows Subsystem For Linux 2 (WSL2)
- Source control software: git (should come preinstalled on WSL) to be able to check out the implementation of the 6502 CPU and BASIC
- make tool installed
- C compiler like, gcc installed
What to expect?
First I’ll explain how to install WSL2 if you don’t have one.
Then I’ll explain who to clone locally the repository that contains MOS 6502 and BASIC simulators.
Then we’ll build them and run them locally.
I’ll provide references to good resources on the subject.
Let’s begin
Install WSL and Ubuntu
As I mentioned Michael’s implementation requires a Linux distribution to run. So if you have Windows as I do then there is a need to install Windows Subsystem For Linux (aka WSL) to be able to run Ubuntu natively on Windows. The whole installation process is easy and takes a couple of minutes. To know how to do it just follow one of the tutorials below
- How to install Linux on Windows with WSL
- Install Ubuntu on WSL2 and get started with graphical applications
What you should see when Ubuntu is up and running
Clone GitHub repository
Now, that we have Ubuntu installed let’s download (clone) the MOS 6502 repository from GitHub to be able to build it locally.
- When you navigated to that repository in GitHub click on the code button in the right upper hand side,
- and click on copy icon
Create directory to clone repository into
Then go back to Ubuntu and run the following commands.
First, let’s create a directory for the MOS 6502 project.
mkdir perfect6502
cd perfect6502
Then clone the repository locally
git clone https://github.com/mist64/perfect6502.git
You should see similar output when git clones the repository locally
Cloning into 'perfect6502'...
remote: Enumerating objects: 643, done.
remote: Total 643 (delta 0), reused 0 (delta 0), pack-reused 643
Receiving objects: 100% (643/643), 997.31 KiB | 4.96 MiB/s, done.
Resolving deltas: 100% (391/391), done.
Install make and gcc on Ubuntu
After this we can build the project using Linux make tool. But since it’s not installed on Ubuntu WSL we need to install it first.
Also, make tool requires C compiler in order to compile the code. So we need to install one of the available C compilers for Linux. We’ll use gcc compiler.
Install make, by running the command below.
sudo apt install make
Next, let’s install gcc compiler. If you’ll be asked Do you want to continue? type Y.
sudo apt install gcc
Now that both of the tools are installed let’s run make command
make
What you’ll see that there will be an error and it has to do with the source code that we cloned
amc@MINE-LAPTOP-130JJTQ6:~/perfect6502$ make
cc -Werror -Wall -O3 -c -o perfect6502.o perfect6502.c
cc -Werror -Wall -O3 -c -o netlist_sim.o netlist_sim.c
netlist_sim.c: In function ‘getGroupValue’:
netlist_sim.c:390:1: error: control reaches end of non-void function [-Werror=return-type]
To fix this issue there is a need to apply Pull Request (PR) one of the users submitted.
To do this run this command
git fetch origin pull/10/head:pr_number10
You’d think that’s it. But you are wrong and there is a need to update Makefile to be able to build the project. To do this we need to comment out the line below in the file
OBJS+=measure.o
Commenting out is done using ‘#’ character as a first one in the line.
I am using vi editor to do this. On WSL it is possible to edit this file even in notepad.
Update Makefile using Windows notepad
When you are in the perfect6502 directory type
explorer.exe .
You should see Windows File Explorer opens
Now, you can select open with notepad and comment out that line in notepad.
Okay, now we are good to run make
amc@MINE-LAPTOP-130JJTQ6:~/perfect6502$ make
cc -Werror -Wall -O3 -c -o perfect6502.o perfect6502.c
cc -Werror -Wall -O3 -c -o netlist_sim.o netlist_sim.c
cc -Werror -Wall -O3 -c -o cbmbasic/cbmbasic.o cbmbasic/cbmbasic.c
cc -Werror -Wall -O3 -c -o cbmbasic/runtime.o cbmbasic/runtime.c
cc -Werror -Wall -O3 -c -o cbmbasic/runtime_init.o cbmbasic/runtime_init.c
cc -Werror -Wall -O3 -c -o cbmbasic/plugin.o cbmbasic/plugin.c
cc -Werror -Wall -O3 -c -o cbmbasic/console.o cbmbasic/console.c
cc -Werror -Wall -O3 -c -o cbmbasic/emu.o cbmbasic/emu.c
cc -o cbmbasic/cbmbasic perfect6502.o netlist_sim.o cbmbasic/cbmbasic.o cbmbasic/runtime.o cbmbasic/runtime_init.o cbmbasic/plugin.o cbmbasic/console.o cbmbasic/emu.o
If you saw similar output then there were no errors and we can finally run the Commodore 64 BASIC emulator on MOS 6502 CPU simulator.
cbmbasic/cbmbasic
And hooray you should see the screen below
What can you do now?
Well, the sky is the limit. Pay attention that since the MOS 6502 simulator simulates real transistors it’s quite slow. Also, there are still some bugs in the Commodore BASIC emulator so not all commands will work as expected. But most will do.
For example, below comes “Hello, world!” program that will print this sentence, and then concatenate two words in one string and print it out.
You can do even more pretty trick using BASIC to print “Hello, world!” in Assembly code. I saw this implementation in the “Hello World” on Commodore 64 in Assembly Language, Machine Code video.
If you’ll watch that video you’ll understand what’s going on in the program below.
You can watched how it worked in real time below
Some useful commands to know
Pay attention, that BASIC requires upper case for all commands.
Clear screen
PRINT CHR$(147)
Clean memory
NEW
Reset computer (hence emulator)
SYS 64738
List your program so far
LIST
To update particular line there is a need to retype it in this emulator, while in other emulators you can edit them. As you can see on the C64 online emulator
SUMMARY
Well, there you have it you can use your locally running BASIC with pretty good functionality or you can switch to that C64 online emulator which is faster and supports all of the commands.
REFERENECES
Revers engineering MOS 6502 CPU videos
6502 simulation GitHub repository of Michael Steil
Programming on Commodore
- “Hello World” on Commodore 64 in Assembly Language, Machine Code
- Bill Gates’ Personal Easter Eggs in 8 Bit BASIC
Commodore Programming references
Commodore BASIC wiki
Commodore BASIC Emulators
Interactive article about Commodore way of programming
Your last step for the fix don’t work !!!!!!!!!
all went good till here. I commented the line as you said and it still give the same error. I done that step before to update as well, all step by step. It does not MAKE.
so I fuckin wasted hours to do all step by step to finally fix the bugs -ie the cretin bug that does not accept password for user unless you are in su -and in the end now I have to stop because the f incompetence of these guy who are not even able to build a f* healthy ubuntu environment w/o 1000 bugs and TEMPORARY fixes who would pop up again with every new OS patch. this is why I hate Linux