Spotlight › Commodore 64
A Commodore 64, in a window
The best-selling computer of the 1980s, running inside an operating system for the machine that beat it -- in a window you can drag, with the real Commodore ROMs at the BASIC prompt.
- emulates
- a MOS 6510
- written in
- 6,943 lines of C
- machine
- 4.77 MHz 8088
What a Commodore 64 was
Seventeen million of them, and a chip nobody has really replaced.
The home computer
The Commodore 64 went on sale in 1982 for 595 dollars and stayed on sale for eleven years. Estimates of how many were sold run from 12 to 17 million, which makes it the best-selling single model of computer there has ever been. It had 64 kilobytes of memory, a BASIC that started the instant you switched it on, and a cartridge slot -- and for a decade it was where most people in Europe and North America first met a computer.
Three custom chips did the work. The 6510 was the processor, a 6502 with an extra input/output port bolted to it. The VIC-II drew the screen, in sixteen colours with eight hardware sprites. The SID made the sound, and is the reason C64 music is still being written now.
What it is doing here
The IBM PC and the Commodore 64 were sold to different people for different money in the same years. The PC won the office; the 64 won the bedroom. This is one running inside the other, on an operating system written in 8086 assembly, in a window beside Minesweeper.
This is a port, not a rewrite from memory. The behaviour comes from
VICE, the emulator the Commodore world has used since 1996, and specifically from its
x64 machine. None of VICE's code is in the os8088 repository: the behaviour was
read out of its source, file by file, and written again in the C this system compiles. Every
file that carries something derived from VICE names the VICE file it came from. VICE is under
the GPL version 2 or later, so this program is too, and the licence travels on the floppy with
it.
See it running
Every picture here came out of the emulator, on the disk you can download.
10 PRINT CHR$(205.5+RND(1));:GOTO 10 picks one of two diagonal characters at
random, forever. Typed in and run: the ROM's BASIC interpreter, the 6510 executing it, and
the screen scrolling underneath.
How you fit a Commodore 64 inside an 8086
The processor is the easy half. Getting the picture onto the glass is not.
The processor
Emulating a processor means reading one of its instructions, working out what it does, doing that, and moving on -- millions of times over. It is the innermost loop of the whole program, so it is the part that is not written in C: 2,304 lines of hand-written 8086 assembly, which keeps the 6510's registers in the 8086's own registers instead of in memory.
It implements every instruction the 6510 has, including the ones that were never documented -- the accidental combinations that games and demos used anyway -- and each one costs the number of clock cycles it really cost, down to the extra cycle a branch pays when it crosses a page boundary. Time inside the machine is counted in those cycles and nothing else, which is what lets the screen chip and the two timer chips agree with each other.
How it is checked
There is a standard test for a 6502 and it is a well-travelled one: Klaus Dormann's functional test, a 65,536-byte program that exercises every instruction and addressing mode and stops dead at a known address if any of it is wrong. It is loaded straight into the emulated machine's memory and run, and it reaches its success point.
Decimal-mode addition and subtraction get a test of their own, because the fixture for those is published only as source. All 262,144 combinations -- every value, every operand, carry both ways, both instructions -- are checked against a separate implementation of the rules written in Python. Twelve groups of checks in all, and each is shown to fail when the thing it covers is deliberately broken.
Where the 64KB lives
A program in os8088 gets just under 60KB for itself, so the Commodore's 64KB of memory cannot live inside it. It is a separate block the system hands out on request, and the ROMs are a second one. The program itself is 52,490 bytes of its allowance, which is why part of it -- the menu commands, the things that happen once when you pick them -- sits in a second file and is only read off the floppy when it is needed.
Sharing the machine
An emulator wants to run flat out forever. The rest of the system needs the processor back
to move the mouse, drop a menu and redraw a window. So the 6510 runs in slices: it
executes for a while, hands control back, and asks to be woken again. A C64 sitting at
READY. with nothing happening costs nothing at all until a key arrives.
Drawing the screen
Drawing all 1,000 characters of a C64 screen through the operating system's own text call would cost about a second on the machine this targets. So the screen is never redrawn. The emulator keeps a copy of the picture, and the processor emulator marks each 256-byte page of memory it writes to, so the redraw knows which parts of the screen can possibly have changed before it looks at any of them.
What is left is drawn as bands of pixels rather than as letters, and a band of 40 characters costs 7.5 milliseconds where the same 40 characters drawn as text cost 41.4. Scrolling is caught separately: when the whole picture has moved up by a whole number of rows -- which is what happens every time a line of BASIC output reaches the bottom -- the pixels already on the screen are shifted by the graphics hardware and only the newly uncovered rows are drawn.
Why a sixteen-colour machine is black and white
Not a shortcut. A measurement.
The C64 has sixteen colours and the PC this runs on can show them. The picture is still black and white, on every screen including VGA, and the reason is arithmetic.
The fast way to put pixels on a VGA screen in this system is to write them a run of one colour at a time. A C64 text screen is between 1,000 and 1,600 such runs, and each costs about 215 microseconds. That is 0.2 to 0.35 seconds for one band of the picture on the target machine, and 0.3 to 0.5 seconds for a whole frame even on a 386. A colour version was priced before it was written and it does not run.
So each of the sixteen colours is resolved to black or white by how bright it is -- and the brightness figures are the VIC-II's own, taken from the table VICE uses for a PAL machine. There are only nine distinct levels for the sixteen colours: blue and brown are the same brightness, so are red and dark grey, so are yellow and light green. Seven pairs vanish into each other, which is exactly what happens on a real C64 plugged into a black-and-white television.
What it does not do
Each of these is a decision with a reason, and the program says so where you would meet it.
Slower than a C64, on the machine it aims at
On a modern computer running the PC as an emulator, the emulated C64 runs at thousands of per cent of full speed. On the 4.77 MHz IBM PC this system is written for, it runs at a few per cent. That is stated rather than hidden: the status strip prints the speed the machine actually delivered, every time.
No 1541, no tape, no cartridge
There is no disk drive to LOAD from, so the ROM says so. Programs come in
through File > Smart attach instead: pick a .PRG file off the os8088 floppy
and it is put into memory and started, which is one of the ways VICE itself does it.
Nothing that beats the raster
The screen chip is serviced a scan line at a time, not a clock cycle at a time. Text, BASIC and most software of the era do not notice. Demos do: the tricks that change a colour part-way along a line or open the border need timing this does not have, and sprite collision registers read as zero.
One voice, on the PC speaker
The SID had three voices, filters and ring modulation, through a proper audio chip. A PC of this era has a speaker that plays one square wave. The first voice's pitch is sent to it, so a tune is recognisable and nothing more is claimed.
Try it yourself
In your browser in about thirty seconds, or on a floppy of its own.
-
The quickest way is the browser demo. It boots os8088 with the every-application floppy in the second drive, and the C64 is on it. Double-click the
Disk Bicon, then theC64folder, thenC64.O88. Give it a few seconds -- it reads 20KB of ROM off the floppy before it starts. -
Or take the disk.
c64.imgon the download page is a 1.44MB floppy with the emulator and the ROMs on it;c64360.imgis the 360KB version, the one a real IBM PC/XT reads. Either goes in the second drive in place of the software disk. Building from source,make c64diskmakes all three sizes. -
Type at the prompt. It is a Commodore 64, so BASIC is already running.
PRINT 2+2works. So does the maze one-liner above, andLIST, andRUN. The Run/Stop key is Esc. -
The keys worth knowing. Alt+D gives it the whole screen and takes it back. Alt+P pauses. Alt+W runs it as fast as it will go. Alt+F9 resets the machine and Alt+Q closes it. The arrow keys and Ctrl are joystick port 2, which means the arrow keys stop reaching the C64's own keyboard while you are playing something -- Scroll Lock switches that off again.
It is a window like any other. You can drag it, put another program in front of it, and come back. The C64 inside carries on running while you do.