HOW TO WRITE AN EMULATOR 

Emulators are great learning tools. This tutorial teaches you how to program in C, and how computers work.

The way computers work is relatively simple: The CPU has registers, which are small storage units for values that don't need or aren't big enough to go into RAM (main memory). The Intel 80x86 processor family has many registers, including AX (AH and AL), BX (BH and BL), CX (CH and CL), and DX (DH and DL) for real mode (aka DOS mode). In protected mode, you have EAX, EBX, ECX and EDX.

The MOS Technology 6502 has only three usable registers: X, Y, and ACC (Accumulator). The accumulator is meant to store intermediate results, such as the result of a simple addition equation, like 51+37. The accumulator would then have the value of 88.

Let's start the program code. Open your IDE or favourite plain text editor. Add the text in monospace below to your file (ONLY WORKS IN WINDOWS):

#include <stdio.h>
#define LDX 0x01
#define LDY 0x02
#define IOX 0x10
#define HLT 0xFF
unsigned char opcode;
unsigned int ip=0, x=0, y=0, acc=0;
unsigned char memory = {LDX,0x01,LDY,0x06,IOX,HLT,'M','y',' ','e','m','u','l','a','t','o','r',' ','w','o','r','k','s','!','\0'};
void ldx(unsigned int xdata)
{
  x=xdata;
}
void ldy(unsigned int ydata)
{
  y=ydata;
}
void iox()
{
  switch(x)
  {
    case 0x01:
      printf("%s",memory+y);
      break;
  }
}
int main(void)
{
  int CPUIsRunning = 1;
  float memkb=sizeof(memory)/1024;
  printf("My Emulator 1.0\n%d bytes (%f KB) of memory.\n\n",sizeof(memory),(memkb));
  while(CPUIsRunning==1)
  {
    switch(memory)
    {
      case 0x01:
        ldx(memory);
        ip+=2;
        break;
      case 0x02:
        ldy(memory);
        ip+=2;
        break;
      case 0x10:
        iox();
        ip+=1;
        break;
      case 0xFF:
        printf("\n\nEmulator halted by opcode 0xFF (instruction HLT).\n");
        system("pause");
        CPUIsRunning=0;
        break;
      default:
        printf("\n\nEmulator halted by illegal opcode.\n");
        system("pause");
        CPUIsRunning=0;
        break;
    }
  }
  return 0;
}

If you added the code right, It should come out something like this when you run the program:

My Emulator 1.0
256 bytes (0.250000 KB) of memory.

My emulator works!

Emulator halted by opcode 0xFF (instruction HLT).
Press any key to continue . . .

That's it! One emulator, less than twenty minutes. It's a pretty crude and rudimentary emulator. Here's a list of basic features you could add, and a hint in italics on how you can implement them:

  • ROM protection (const)
  • Keyboard input (stdin)
  • Integer mathematics (*,/,+,-)
  • Bigger memory ()

I'm making an emulator based off of this tutorial I've written here, and it has all of the features I've listed above.

Where will you take your emulator? Command-Line Interfaces? GUIs? SDL? Sky's the limit.


By the way, the links that look aren't links. They're parts of code. The HTML codes for the brakets don't want to work.

Log in or register to write something here or to contact authors.