Alex Rivera | Logout

How would I go about writing a Virtual Machine

Asked 2009-11-05T05:32:07.277
24

I'm interested in programming a virtual machine, nothing as fancy as virtualbox or vmware, but something that can emulate a simple architecture, be it cisc or risc, say the Zilog, SPARC, MIPS, or 80686 architecture models.

I suppose by doing this it would be relatively simple to make an emulator of the same kind, I'm just interested in using this for experience more than anything else (being my first C project, I'd rather do this in C than in anything else).

Edit
Report

1 Answer

4

A few thoughts:

  • The older instruction sets will be simpler, so they might be a good place to start.
  • Choose a risc architecture: decoding the instruction stream will be much easier.
  • Ignore things like interrupts, NMIs, etc.
  • There's always a lot of fiddly detail in accurately emulating booting. Instead, pick something very simple like starting execution at address zero, with all registers set to zero.
  • Real programs will need things like real hardware emulation as well, so don't do that.
  • You might want to extend the instruction set with a few special i/o instructions to read a number, write a character (or even a string), etc, so that you can write simple test programs that actually do very simple i/o.
  • Parsing an object file format like elf can be a lot of work all by itself. With tools like objdump, you can probably extract just the text section (ie, the instructions) as binary (at least ascii hex).
  • Start by writing a disassembler for whatever instruction set you want to emulate, or at least for the initial subset you'll shoot for. You'll need it for debugging anyway.
  • Find out how to get gas (gnu assembler) for your chosen instruction set working so you can produce known good object files and test programs.

Unless you have knowledge of other programming languages, and/or a reasonable understanding of assembler, this is a pretty challenging first C project. Never the less, good luck!

answered 2009-11-05T06:07:31.073

Your Answer