Blitz BASIC C64 emulation By Lee Davison.

Introduction.

Recently on the 6502.org forum a new user joined and in this post presented some BASIC source that was alleged to be a Commodore 64 emulator with just a few bugs.

It turns out that it's Blitz3D BASIC and, like a lot of BASIC source code, is a bit shy of comments and has left over bits and pieces from previous attempts to make it work.

What was wrong.

Most of the code was in place and working but the routines for the compare opcodes, CMP, CPX and CPY were broken as well as the code for the ADC and SBC opcodes which were also incomplete. Code for the BIT opcode had been duplicated and edited at some time but the working version was commented out. There was also quite a bit of unnecessary code to handle values in the Blitz3D 32 bit signed integer variables becoming negative that really wasn't needed.

What I fixed.

CMP, CPX, CPY, ADC, SBC and BIT as well as adding PHP and PLP as functions to handle the break flag properly. I also removed the unnecessary code for negative values and broke the very long multi statement lines into multiple lines with comments.

I made sure that values that were smaller than 32 bit, which is all of them, were masked whenever they were changed and that, as much as possible, it was done the same way every time.

What happened.

You would expect that as things were fixed the code would work better and better. What really happened was that it would almost work then I'd fix another thing and be left staring at a blank screen - again.

Eventually it all came right and I got the familiar C64 sign on screen but not until after having to write test ROM images and adding some debug code, which is still there, so I could tell which opcodes were failing and how.

After that it was just a case of going over the code again to remove extra sets of parentheses and unifying logical tests to make the code more self consistent.

What's been changed since.

I replaced the busy wait at the end of the main loop with a delay wait. This means a lower CPU load on fast machines and faster execution on slow machines.

I corrected the cycle timings for most opcodes and address modes except for some cases of address,{X|Y} indexing. The error is only one cycle at most. This means that on a fast enough machine this now runs at about the same speed as a real Commodore 64.

I also corrected the cycle timings for the IRQ and NMI interrupts and made reset() clear the cycle counter.

I added the showtime constant and some extra code at the end of the main loop that will show how many milliseconds late the code was in completing 20000 CPU cycles. To see this time set the showtime constant near the top of the source to True.

The arrays that held the ROM images and RAM image have been replaced with banks. This makes loading the roms much simpler and reading or writing the memory much quicker.

The program now runs at under 100% CPU loading on my old laptop even in debug mode.

I replaced the pixel by pixel drawing of characters on the screen with tiles loaded from an image. This is the nearest way I have found, so far, of making screen writes character based instead of pixel based. This makes drawing a whole screen of characters, as is done whenever the screen scrolls, five or six times faster than previously.

I fixed the keyboard shift sense but shift lock key still does nothing.

The cycle counter has been corrected for a PAL CPU.

Added a virtual i/o area so that upper case and lower case characters can be diaplayed. It's a bit crap but POKE 53272,22 will change a whole screen of characters to lower case and POKE 53272,20 will change them back to upper case.

Corrected the remaining address,{X|Y} cycle timings.

Maped the cursor, backspace, delete, insert and home keys to CBM keycodes.

The code.

V0.67 is here and includes a copy of the original source as posted on the 6502.org forum.

To get it to run you will also need Blitz3D and images of the kernal, basic and character ROMs and will need to call them kernal.rom, basic.rom and char.rom respectively. The images are readily available online.

This has been updated to include the details of the author of the original PASCAL source, Marc Dendooven.

What's left to do?

Just about everything apart from the CPU and some pretend I/O. There's no colour, no colour RAM, no graphics, no sound. No joystick ports, no user port, no cartridge port, no memory banking, no VIC memory mapping, no ROM/RAM memory mapping and no storage devices. There's not even a fully working keyboard, yet.

What else is available?

With just a few, small changes the code becomes a VIC 20 emulator.


Last page update: 13th June, 2011. e-mail me