Introduction : A Machine Emulator

In this module we will be implementing a computer emulator. The final product will be a program which emulates a non-existent computer! It even comes with a built in "assembly language" program, which fills the screen up with coloured characters.

To compile this program, you will need the latest version of Pelles C, i.e. version 4.0 or later. It has just been released by Pelle himself over on his site.

A full version of the source code for this program can be downloaded. You need three separate files: main.c, which contains the main part of the program, including the "assembly language source" which runs on the emulated machine, emulate.h which contains the details of all the assembly language instructions that this machine understands and emulate.c which actually contains the implementation of all these assembly language instructions, in C.

To run this program, create a new project as usual in Pelles C and place the three files downloaded above, into the project directory that Pelles C has created. Now click "Project->Add Files to Project" in the menu at the top of the Pelles C IDE and select each of the three files, to add them to the project. Now the project should compile and run. When it runs, simply press a key and the screen will fill with coloured characters.

What is going on here, and what is an emulated machine anyhow? The program you are seeing run, after you press a key, is not written in C at all, but in an assembly language of my own invention. So how did I get assembly language into your C compiler? The answer is by writing a machine emulator in C, which emulates a machine which understands this assembly language. So, a machine emulator is precisely this: a program which emulates the operation of another machine. In this case the machine doesn't even exist, and the assembly language it understands is a completely new assembly language which is not used anywhere!

The first part of writing a machine emulator is of course to come up with a machine to emulate in the first place. In this case, I decided to emulate a "computer" with a 16 bit microprocessor (including its very own assembly language), 64k of RAM, and so far, graphics hardware, which supports coloured text output by writing to its video memory. I'll describe the machine in detail, then I'll describe how to program an emulator for it in C.

Let me mention that the possibilities for the code that I have listed are endless. With very little work, you could adapt it to make your own version of the machine, with a much enhanced feature set. You could add support for input from the keyboard (which would make it truly a computer emulator, since a computer by definition needs output, a central processing unit, and an input unit of some description). You could add file capabilities so that the machine has an emulated disk drive, and if you got really ambitious and knew what you were doing, you could add graphics and sound capabilities to your machine. I encourage you to play around, extend the concept, and write to me with your final products, so that I can have a look-see.

Ultimately, you could even write an assembler, a BASIC interpreter, a BIOS, an operating system and other kinds of programs for your fictitious machine. The FriedSpace website will eventually have something along these lines as a free product for download. Of course it will take time to develop, so don't expect it soon!