Close
0%
0%

VR65C02: Real 6502 + AVR For Everything Else

6502 computer with an ATmega4809 emulating all memory and I/O

Similar projects worth following
A 65C02 computer system with an ATmega4809 simulating all of the memory and I/O. Two ICs are enough to run native 65C02 code on the real silicon. All of the 65C02's interactions with the outside world go through the microcontroller.

It's like the 65C02 is wearing the ATmega as a VR headset (AVR headset?).

- Real 6502 code running on a real 6502
- ATmega4809 microcontroller emulates everything else
- 5KB of emulated RAM, up to 32KB emulated ROM
- Memory-mapped serial port, GPIO, and I2C support
- 6502 is programmed by uploading an Arduino sketch
- Eye-watering clock speed of 257kHz!

Overview

Memory and I/O Emulation

VR65C02 is a fully functional computer system. The ATmega microcontroller provides emulated RAM and ROM as well as a passthrough UART and four GPIO pins that the 65C02 can access.

The microcontroller controls the RESET and CLOCK lines of the CPU, so all memory access is handled synchronously by the Arduino code. For each bus cycle, the microcontroller reads the address bus and R/W pin from the CPU. If the CPU is reading a memory address, the microcontroller will decode the address into a memory value or an I/O operation and write the appropriate value out to the data bus. On a write cycle, the microcontroller reads the data bus value into its simulated RAM or updates the appropriate I/O system. The microcontroller implements address decoding in code in a similar way to a traditional system using logic gates or PLDs.

Both RAM and ROM are backed by large byte arrays in the microcontroller's memory, the RAM using onboard SRAM and the ROM using onboard Flash.

The UART is a memory-mapped I/O device from the perspective of the 65C02, similar to a 6551 ACIA. The CPU can read and write bytes and the microcontroller passes them through to one of its native serial ports.

Similarly, there are four GPIO pins accessible as a memory-mapped register to the 65C02.

Memory Map

The memory map of the 65C02 is as follows:

  1. 5KB "RAM" at 0x0000
  2. I/O regisers at 0x7F00
  3. 8KB "ROM" at 0xE000

The ATmega4809 has enough flash memory to support up to 32KB of ROM if necessary, but the total onboard SRAM is only 6KB and some is set aside for the Arduino code.

Software

The core of VR65C02 is the Arduino sketch running on the ATmega4809. As described above, this program simulates the entire bus cycle of the physical 65C02 CPU and all of the memory and I/O interactions.

Real 65C02 machine code is running on the CPU. This can be assembly or C code compiled with cc65. Since there is no physical ROM or RAM, the code has to live somewhere. My approach is to use the large amount of onboard Flash memory of the ATmega to store this. The main trick of VR65C02 is taking this compiled binary and getting it into the microcontroller Flash as part of the Arduino sketch upload.

The build process is done in a single script (build.sh). The process looks like this:

  1. Compile 6502 C code into a binary ROM file.
  2. Run a Python script to convert this binary file into a C header file with a large const array of bytes.
  3. Compile the Arduino sketch with this generated header file.
  4. Upload Arduino sketch to microcontroller with new 6502 code.

Programming the ATmega (and by extension the 65C02)

The ATmega4809 is programmed via UPDI, specifically, I am using a standard FTDI USB-serial adapter (with a 4.7k resister between Rx and Tx) and the SerialUPDI programmer in the MegaCoreX Arduino board package. Since the 6502 code is uploaded as part of the Arduino sketch, it's almost like programming a 6502 as an Arduino.

Limitations

All of the limitations of this system come down to hitting the limits of the ATmega4809 itself in one way or another.

Clock Speed

The main drawback is obviously clock speed. Simulating the whole bus cycle and all of the potential memory and I/O operations of the 6502 on a 20 MHz 8-bit microcontroller is not exactly quick. Measuring the PHI2 output on the CPU with an oscilloscope shows that the effective clock speed is about 257 kHz. Even at this speed, VR65C02 is surprisingly usable for simple GPIO tasks or interacting over the serial port, but it's about 4x slower than a Commodore 64 or Apple II.

Because not all the emulated peripherals respond at the same speed, the effective clock speed is actually somewhat variable depending on the I/O demands of the 6502 code. This doesn't fluctuate too dramatically, but heavy I/O, e.g. polling the serial port, will run at around 257 kHz, whereas no I/O at all will get the clock up to about 270 kHz.

Not Enough Pins

Besides the slow speed, the other main...

Read more »

vr65c02-v1.pdf

PCB schematic

Adobe Portable Document Format - 188.15 kB - 09/20/2024 at 21:12

Preview

vr65c02-v1.gbr.zip

Gerbers for PCB for v1

Zip Archive - 121.43 kB - 09/20/2024 at 21:12

Download

  • PCB v1

    Colin M09/20/2024 at 21:19 0 comments

    I got the PCBs back from manufacturing and the boards are working well. The circuit is identical to the prototype so I didn't expect too many issues.

    I did make an update to the Arduino code to allow I2C peripheral passthrough. The 6502 can access I2C devices using a simple memory-mapped I/O register scheme. I tested this functionality with an RTC breakout board.

    I'm considering this project to be complete for now. There's a hundred directions I could go with expansion, but I think I got what I wanted out of it. 

    I've included the schematic PDF and the Gerber files for the PCB if you're interested in making a VR65C02 yourself. The Github repo should contain all the documentation required to set up the software.

    https://github.com/crmaykish/VR65C02

  • An Unnecessary PCB Design

    Colin M09/05/2024 at 04:20 0 comments

    This project probably does not warrant a PCB (it's kind of a joke/toy), but I designed one anyway. I put together a pin-for-pin copy of the hand-wired prototype. This should be functionally identical to the prototype with the inclusion of some power select jumpers and extra pin headers. It's a two layer board and it's well under 100x100mm so it should be really cheap at most manufacturers. I'll include this in my next order.

    Render of the first PCB revision

View all 2 project logs

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates