How do I learn to write a console emulator? [duplicate]
Asked Answered
F

3

7

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?

Frangible answered 21/6, 2012 at 18:15 Comment(2)
I have a collection of simulators github.com/dwelch67 What you need to learn first is assembly language for the target cpu, then work on a disassembler that disassembles in execution order, at some point you can just start turning that into the instruction set simulator. Then you have to make emulators for all the peripherals. So you will need good docs for the system you are emulating.Thurman
Another approach is a static binary translator, SBT. dwelch.com/ipod/asteroids.htm is one I did for the ipod (classic) I have a lot more work on that one since that release, not published anywhere though. A good tutorial on the subject. gtoal.com/sbt you can also do dynamic translation as well, translate on the fly rather than static. I wouldnt try dynamic until you can do static.Thurman
S
7

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.

Singular answered 21/6, 2012 at 18:20 Comment(2)
One thing that I have always wondered is where exactly one finds the specs for a particular system. Some systems, like you mentioned, are very well documented, but what about a project like Dolphin Emu? Does Nintendo really release their console's architecture out for everyone to see?Weaver
Lots and lots of hard work and reverse engineering. Sometimes starting from e.g. pinouts on the chips, sometimes going as far as sticking probes on the actual hardware while the system is operating.Singular
T
2

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.

Thoughtful answered 21/6, 2012 at 18:22 Comment(0)
R
2

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/

Rembrandt answered 21/6, 2012 at 18:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.