News for hackers. Stuff that matters.
Re:How do emulators work and how are they written? by videl on Saturday May 15, 2010 @04:56PM (#1350634)
It is really amazing. Kudos to you as I really still don't get it how to emulate hardware in software... you are a man!
Re:How do emulators work and how are they written? by palish on Saturday May 15, 2010 @06:09PM (#1350768)
Think of it this way. A program is just a set of bytes (which are numbers from 0 to 255) which describe what it should do. The goal of most programs is usually just to read in some data (again, just a set of bytes) which is stored in memory, then manipulate that data.

The easiest way to understand how to emulate hardware in software is to just make up your own simple assembly language. For example, let's say you want to have these operations:

"set <memory location> <value between 0 and 255>" -- sets the value at <memory location> to the specified value

"add <src memory location 1> <src memory location 2>" -- adds the numbers at the two memory locations, and stores the result into memory location 1

The point is to keep it ridiculously simple for now, so let's assume the memory locations we provide are also values between 0 and 255 -- effectively giving us 256 bytes of memory to work with.

So let's write a very simple program. It will perform 2 + 2.

  set 0 2
  set 1 2
  add 0 1
The result is now in memory location 0, and the result is "4" obviously.

So what might the program look like on disk? Well, we only have two instructions, so let's say the code for "set" is "0" and the code for "add" is 1". If we store the program in a file, then it might look like this:

  0 0 2 0 1 2 1 0 1
Now that we've defined an extremely simple assembly language, we can write a virtual machine for it as follows.

First, write a program which allocates 256 bytes of memory. This is our virtual machine's memory space.

- Read in the first byte of the program.

- If it's 0 ("set"), then read in two more bytes (the dst and value arguments for "set"). Set mem[dst] = value.

- If it's 1 ("add"), then read in two more bytes (the "dst" and "src" arguments for "add"). Set mem[dst] = mem[dst] + mem[src].

- If it's > 1, then abort (the program is invalid).

If this all made sense to you, then you should be a stone's throw away from being able to write a VM for more complicated instruction sets. Just find out what the instruction set can do, and then write a VM which allows the instructions to do those things.

Re:How do emulators work and how are they written? by zackattack on Sunday May 16, 2010 @03:37AM (#1351615)
I lack formal CS training: "0 0 2 0 1 2 1 0 1" is a sequence of 9 integers. Why would you allocate 256 bytes? Edifying post, thanks.
Re:How do emulators work and how are they written? by daeken on Sunday May 16, 2010 @03:46AM (#1351626)
Your addresses in this simple instruction set are 8-bit; 256 bytes gives you a full 8-bit memory space.
Re:How do emulators work and how are they written? by johnl87 on Sunday May 16, 2010 @02:18PM (#1352477)
Not every number is an integer. Could be a char or a short too. The sizes of those depends on the platform and if it's signed or unsigned.
Re:How do emulators work and how are they written? by amalcon on Saturday May 15, 2010 @06:25PM (#1350785)
My "Aha!" moment came during my foundations of CS class at uni. As such, I'd suggest reading up on the work of Alan Turing (Church is good too, but Turing's state-machine-based definitions are usually more approachable to a beginner).