Open Source Xojo 6502 CPU emulator

Hi all,

Those of you that know me are aware that I’m always trying to push the boundaries of what Xojo can do and that I can’t resist re-inventing the wheel. With that being said, I’m pleased to announce a cycle-accurate emulator of the MOS Technology 6502 8-bit CPU written entirely in Xojo code.

What is the 6502?

The 6502 is an 8-bit microprocessor (CPU) that was incredibly popular in the 1980s. It formed the heart of loads of systems you probably love (the Apple II, BBC Micro and the Nintendo Entertainment System to name a few).

What does this project do?

This project emulates the 6502. That is, it takes a memory dump (typically a ROM in the form of bytes) and executes the instructions one after another, adjusting its internal registers and manipulating the memory just like a real-life CPU.

It’s not a full system emulator. This means that there is no graphical output, keyboard handling, sound, etc. These components would also need to be emulated. The CPU however is often the most complex component in a system.

I may consider pushing this project further if there is interest and try to emulate an entire system (probably the Apple II as I have read this is the most straightforward system to mimic).

Project contents

The project is a desktop project that contains a comprehensive test suite that you can run. All 151 opcodes are tested 10,000 times each with different register and memory addresses and compared to this project. That’s right, there are over 1.5 million tests and they all pass so I’m confident this is a high quality emulator.

Usage

It’s best to inspect the code in the project for a full explanation as I have painstakingly documented every property and method and deliberately been verbose in my implementation to make it clear what is happening.

// Default to 64K of memory.
// To do anything useful you'll need to populate the memory with an actual program.
Var mem As New MOS6502.Memory

Var cpu As New MOS6502.CPU(mem)

cpu.Reset
cpu.Execute

I welcome any and all feedback. I hope people find this interesting and that it’s proof that Xojo can do anything another language can do.

The repository is hosted on GitHub.

My other projects can be found here.

20 Likes

How suddenly all the memories came back; I immediately remembered those thousands of hours of C64 programming :smiley: It almost brought a tear to my eye. Thank you for this.

4 Likes

My first machine was an Apple ][+, in fact it was a EuroPlus (which I think just meant that we had a £ on the keyboard. I still remember “call -151” which would enter the machine code monitor. It could disassemble code (ie turn machine opcodes into assembly language). It could also allow you to enter programs (only as machine code). You could also run them. I can even remember the “safe space” to enter your programs was memory address 1DFC.

I remember having a Drag 32 home computer and creating an equivalent system that could also do debugging operations including single step, run to address etc. Great days.

1 Like

40 years ago I’ve written tons of 6502 assembly code. And I had my own written assembler, simulator and emulator, made in MS Quick basic and later Visual Basic.

2 Likes

Very neat! I used to put 6502’s into custom lab hardware I used to build before microcontrollers came along. And like Joost I also wrote a 6502 assembler in MS Basic to then burn UV EPROMs to run the hardware. How things have changed…

1 Like

I love this! My early experience was very similar to Ian Kennedy’s. Since that time I’ve written CPU emulators, too, although none were of this fidelity. Thanks Garry!

1 Like

Beside running the Opcodes test, what can be done ?

At this moment in time nothing useful.

The CPU is typically the most time consuming component of a system emulator.

If you wanted to emulate say an Apple II computer you’d need to write a class to mimic the display controller, the display output (a Canvas subclass should work) and then add a check around the CPU.Execute method to check for interrupts, etc every X clock cycles.

The project exists mostly to demonstrate how the internals of a CPU work.