Possible Duplicate:
How do emulators work and how are they written?
I'd like to try writing a basic gameboy emulator, or maybe even NES. I know the basics of C and I'm fairly good at Java, so I know the necessary basics of programming. What I don't know though, is how people process all the data into a C program and create an emulator out of it. I know I should learn from source, but it's kind of hard to see a bunch of lines of code without knowing why they're there and what they're supposed to do. Where am I supposed to start if I wanted to learn how to write such an emulator?
I've searched the internet, but I've only found unclear tutorials that contain too many errors to figure out by myself. Where am I supposed to start?
You don't. You emulate the HARDWARE. You don't have to "process" the program data at all, you would need to write code that mimics the CPU, graphics hardware, input devices, etc.
A good first step would probably be to write a Z-Machine emulator, which while not a console it was actually the first widespread "emulator". It was used for all the infocom text adventures (zork, etc). Since it's a text oriented game format, there isn't much to emulate in terms of graphics or sound, the only input device is the keyboard, and you don't have to worry about execution speed/sync.
It's very well documented here: http://www.gnelson.demon.co.uk/zspec/preface.html
It's actually a project I mean to undertake myself one of these days, just have never quite found the time.
You probably start by collecting information about the hardware you want to emulate. The CPU is probably the single biggest piece, and the most easily available, so that's a reasonable starting point.
A CPU emulator basically reads bytes of a program, decodes individual instructions, and executes them. You typically allocate a big array to represent the device's memory, and some variables to represent the CPU's registers. You simulate execution of instructions by doing operations one at a time on those "registers"/memory locations.
You'll also have to find information about the I/O on the device you're trying to emulate -- e.g., ports or memory locations that represent input from particular buttons, which addresses represent the screen, etc. On a newer device (but generally not most of the older ones) you'll have a separate graphics processor you need to emulate as well. You'll not only need to emulate that processor itself, but also how its connected to the main CPU.
A console emulator is a really big project, and I predict it won't be much fun for most of it; you have to get a lot of stuff right before any games will even start to run.
It might be more fun to find an existing open-source project that is trying to emulate a console, and see if you can find any ways to improve it.
But if you are just looking for a very educational project, I have another suggestion: write your own Scheme interpreter, or write your own FORTH interpreter. Both of these are minimal, elegant languages (and both with non-mainstream syntax rules!). You can write your own Scheme or FORTH from scratch and be running programs in a matter of days.
These are not toys, and writing them will be educational.
Just imagine: getting a copy of SICP[1] and running the programs in a system you wrote yourself! Or getting a copy of a FORTH book and doing the problems on a system you wrote yourself!
If you are interested in these projects, Google search for "write a Scheme interpreter" and/or "write a FORTH interpreter".
P.S. When I was first getting started with computers, I spent a lot of time hacking around in FORTH, and it taught me a lot. The lessons I learned from that have helped me through the years.
[1] http://sicpebook.wordpress.com/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With