How to write a VIC 20 emulator

How to write a VIC 20 emulator


Introduction

As I mentioned before, I am no where near the best programmer and at the start of this project I had no idea at all how to write an emulator. The following information is based on how I did it and is not likely to be the best way. Please contact me if you see any major inefficiencies.

In this page I will take you through the steps that I took in writing V20. I will also attempt to make available documentation of how some of the VIC 20's hardware works for those who don't have such details in manuals at home.

A note before we start: The purpose of this page is to encourage people to create their own VIC 20 emulators. Everyone has different ideas about how to achieve something. The last thing we want is a new VIC 20 emulator that has the same errors as my emulator so please don't ask for my source code.


Starting

Where to start is always a problem no matter what the task. I began by observing other existing emulators and how they operated. The most revealing sources of information are those emulators whose source code is also available. You will find the BBC emulator BeebWin of particular interest because the BBC uses the same 6502 processor and 6522 VIA chips. The source code for VICE is also very useful.

These emulators gave me a general idea, but to fully understand what needed to be done, I asked the author of an existing emulator for a broad overview. I will now give you such an overview of my emulator.


Overview

I have a 64K area of memory which represents the VIC's memory. A main emulation routine gets the instruction byte from the program counter, and with a huge case..of (switch()) statement jumps to the appropriate routine for that opcode. Memory accesses to main memory go through a 'write memory' routine; if the memory-maped I/O is being accessed then another routine is called which will emulate the writing of that particular bit (change screen parameters, timers etc). If RAM is being accessed then that address is changed; and ROM writes are masked out.

The VIC also has some timers which need updating, polling of hardware such as the keyboard, IRQ's that need to be processed and a scanline needs to be drawn at regular intervals. I achieve this by breaking out of the main loop every 128 cycles to do the following tasks:


Hardware Emulation

Emulating the VIC 20's hardware will require you to have accurate documentation. For my emulator I relied heavily on three books. Most of this information is not available on the internet, so I will try to provide all necessary information as text documents.

To create a usuable emulator, the bare minimum you need to emulate is:

Once you have the emulator running with this bare skeleton, you can start to add other features to your emulator. The source code for my emulator is split into several modules or files. In this way the emulator is broken down into manageable parts. The first module I needed to create was the memory unit because almost everything refers to positions in the VIC's memory. I then wrote the video routines followed by the 6502 routines, the 6522 routines and finally the keyboard routines. Of course, you will then have to write the main program which will tie it all together. My main program is the debug monitor that you are presented with when V20 is executed.


VIC memory links

6502/6522/6561 Code




More later. . .