Forum Index | FAQ | New User | Login | Search

Make a New PostPrevious ThreadView All ThreadsNext Thread*Show in Threaded Mode


SubjectEmulator Writing Reply to this message
Posted bycaseh
Posted on10/03/05 12:31 PM



Hi,

I'm new to this area of emulation and was hoping someone could clear some things up for me.

How exactly do I go about writing an emulator, I understand I need to first emulate the chips of the system I will be emulating. I'm not gonna be stupid so have opted for something along the lines of the Genesis. All the chips for this have already been emulated to my knowledge so what exactly comes next?

Advice on what steps I should follow to construct an emulator would be cool, also advice on what languages I should be looking to use. A reasonable time frame for the project would be good also and a level of difficulty. I'm looking to do it as a 3rd year project in University and have 9 months to complete a large project.

Thanks




SubjectRe: Emulator Writing new Reply to this message
Posted byBart T.
Posted on10/04/05 00:05 AM



You will find the Genesis to be difficult. If you are doing this for school, you will probably be expected to write all the code yourself, so the fact that the Genesis is already emulated will have little effect on lightening your load. The Genesis has 2 different CPUs, a fairly complicated video processor, and a non-trivial sound system.

I'd recommend using C or C++ (if you really must) and trying something easy first like the Sega Master System, NES, or a simple arcade game like Space Invaders. There are lots of emulation how-to's out there, I suggest taking a look at the "Arcade Emulation How-To" if you can still find it. I think even Marat Fayzullin had some good basic info on his page.


> Hi,
>
> I'm new to this area of emulation and was hoping someone could clear some things
> up for me.
>
> How exactly do I go about writing an emulator, I understand I need to first
> emulate the chips of the system I will be emulating. I'm not gonna be stupid so
> have opted for something along the lines of the Genesis. All the chips for this
> have already been emulated to my knowledge so what exactly comes next?
>
> Advice on what steps I should follow to construct an emulator would be cool,
> also advice on what languages I should be looking to use. A reasonable time
> frame for the project would be good also and a level of difficulty. I'm looking
> to do it as a 3rd year project in University and have 9 months to complete a
> large project.
>
> Thanks
>



SubjectRe: Emulator Writing new Reply to this message
Posted byHyde
Posted on10/04/05 01:26 PM



> I'd recommend using C or C++ (if you really must) and trying something easy
> first like the Sega Master System, NES...

NES emulation is easy? Sure, it's easy to come up with something as inaccurate as Nesticle; if that's what you are aiming for, then hey.




SubjectRe: Emulator Writing new Reply to this message
Posted byBart T.
Posted on10/04/05 10:39 PM



> > I'd recommend using C or C++ (if you really must) and trying something easy
> > first like the Sega Master System, NES...
>
> NES emulation is easy? Sure, it's easy to come up with something as inaccurate
> as Nesticle; if that's what you are aiming for, then hey.

Writing An NES emulator from scratch that runs a few games will be much easier than a Genesis emulator for a college project. Starting with the Genesis is a bit much.




SubjectRe: Emulator Writing new Reply to this message
Posted byerikduijs
Posted on10/05/05 05:23 AM



> Hi,
>
> I'm new to this area of emulation and was hoping someone could clear some things
> up for me.
>
> How exactly do I go about writing an emulator, I understand I need to first
> emulate the chips of the system I will be emulating. I'm not gonna be stupid so
> have opted for something along the lines of the Genesis. All the chips for this
> have already been emulated to my knowledge so what exactly comes next?
>
> Advice on what steps I should follow to construct an emulator would be cool,
> also advice on what languages I should be looking to use. A reasonable time
> frame for the project would be good also and a level of difficulty. I'm looking
> to do it as a 3rd year project in University and have 9 months to complete a
> large project.
>
> Thanks
>

This might be a useful link:
http://people.ac.upc.edu/vmoya/emulation.html

I would suggest to look at sources from a few open-source emulators like MAME or Final Burn.

If you are going to re-use existing CPU emulators and sound emulators as components for your own project, Genesis could be a good choice for the final project if you like a challange. Maybe a NES or SMS emulator is a little bit more realistic though if it's your first emulation project.

I'd first start with something simpler though. After you finished a space invaders emulator, you'll have a general understanding of how emulators work and it will have you on your way on something like Genesis much easier. A space invaders emulator should take a small amount of time in comparison.

As for choice of language, C or C++ is probably the best bet if you're already familiar with them. Most emulator development is done with those languages, so you'll find it's easier to reuse existing CPU and sound chip emulators.

I myself use java, which saves me from some C related headaches, but introduces a few new ones too:
* if I want to reuse components which are not availble in java, I have to either port them or write them new from scratch
* in emulators, being able to go a little bit more low level can really help sometimes.




SubjectRe: Emulator Writing new Reply to this message
Posted byJan_Klaassen
Posted on10/07/05 05:56 PM



> Hi,
>
> I'm new to this area of emulation and was hoping someone could clear some things
> up for me.
>
> How exactly do I go about writing an emulator, I understand I need to first
> emulate the chips of the system I will be emulating. I'm not gonna be stupid so
> have opted for something along the lines of the Genesis. All the chips for this
> have already been emulated to my knowledge so what exactly comes next?
>
> Advice on what steps I should follow to construct an emulator would be cool,
> also advice on what languages I should be looking to use. A reasonable time
> frame for the project would be good also and a level of difficulty. I'm looking
> to do it as a 3rd year project in University and have 9 months to complete a
> large project.

It depends on which parts you want to write yourself. If you want to write everything yourself, forget it. The 68000, the VDP, and the YM2612 are each quite complex.

I would suggest you look for a system that's based on a 6502 CPU and well documented. The NES would probably qualify, as would some old home computers from the early 80s (though probably not the C64 if accuracy is your goal). You could write all of the code yourself.




SubjectRe: Emulator Writing new Reply to this message
Posted bycaseh
Posted on10/14/05 07:37 PM



> > > I'd recommend using C or C++ (if you really must) and trying something easy
> > > first like the Sega Master System, NES...
> >
> > NES emulation is easy? Sure, it's easy to come up with something as inaccurate
> > as Nesticle; if that's what you are aiming for, then hey.
>
> Writing An NES emulator from scratch that runs a few games will be much easier
> than a Genesis emulator for a college project. Starting with the Genesis is a
> bit much.
>

Thank for all the feedback guys, I have decided on writing a NES emu as a result, but my lecturer says I should not start it from scratch as my project contains a detailed report of 20,000 words and focusing on the coding will impact on my write up of the project.

So the next thing I'm asking is what should I focus on. At the moment Im thinking there is no point in emulating the 6502 core,i'll be re-inventing the wheel right? :) So I will be using Marat Fayzullin's M6502 CPU core which is written in C which for me is a bonus. What I really need to know is what else is required from this point to construct a fairly basic nes emu and what sort of work load it involves. Please try and understand that my programming skills are not that advanced (furthest I've gone with C is writing a command line text editor) but I learn pretty quickly as long as its not overly complex.

Thanks!





SubjectRe: Emulator Writing new Reply to this message
Posted byJan_Klaassen
Posted on10/15/05 09:51 AM



> > > > I'd recommend using C or C++ (if you really must) and trying something
> easy
> > > > first like the Sega Master System, NES...
> > >
> > > NES emulation is easy? Sure, it's easy to come up with something as
> inaccurate
> > > as Nesticle; if that's what you are aiming for, then hey.
> >
> > Writing An NES emulator from scratch that runs a few games will be much easier
> > than a Genesis emulator for a college project. Starting with the Genesis is a
> > bit much.
> >
>
> Thank for all the feedback guys, I have decided on writing a NES emu as a
> result, but my lecturer says I should not start it from scratch as my project
> contains a detailed report of 20,000 words and focusing on the coding will
> impact on my write up of the project.
>
> So the next thing I'm asking is what should I focus on. At the moment Im
> thinking there is no point in emulating the 6502 core,i'll be re-inventing the
> wheel right? :) So I will be using Marat Fayzullin's M6502 CPU core which is
> written in C which for me is a bonus. What I really need to know is what else
> is required from this point to construct a fairly basic nes emu and what sort of
> work load it involves. Please try and understand that my programming skills are
> not that advanced (furthest I've gone with C is writing a command line text
> editor) but I learn pretty quickly as long as its not overly complex.

Well, the recommendation for simple hardware is partly so you can write everything yourself without it taking up all your time, e.g. the 6502 is a fairly simple CPU, and the NES generally is a fairly simple computer. That said, you should gather any information and documentation you can find for the NES and its components so you can gain an understanding of the way it works. The documentation will specify memory maps, hardware registers, I/O ports, that sort of thing. You also need to use inputs and audio/video on the PC you're using as a host, so you need to understand how that works too. If you're not familiar with any APIs you'd like to use, I'd suggest SDL.

When you build your emulator, start by hooking up the CPU and memory. Let the CPU run or step through the instructions, trace what it's doing or log accesses of hardware that's not implemented yet, and (crudely if needed) emulate enough of the hardware bits using to get it to continue running. Once the emulated program is basically running, you can add correct emulation of the graphics and sound.

Again, you need to understand how the system works at the lowest level, you will be dealing with assembly language and hardware registers of the emulated system.




SubjectRe: Emulator Writing new Reply to this message
Posted byStainless
Posted on01/12/06 11:36 AM



I have written several emulators from scratch, the process I go through is this.

1) Analyse target machine and create coding budget

Look at the machine you want to run the emulator on and allocate run time to each section of the code. This may seem a strange starting point but it saves you a lot of grief later on. For example, I wrote a spectrum emulator, I knew from day one it would have to run on everything from a low end GSM handset up to a state of the art PC, so I coded the emulator to run in three modes. Hyper realistic, realistic, and speed.

In hyper realistic I emulated hblank, vblank, realtime video update, everything. In realistic mode I updated the display every scanline, in speed mode I just updated the display once per frame (20ms).

In order to do this I had to have a cpu that could run for a set number of T states.

2) Work out what is going to be the most difficult part of the emulator

The hardest part of the speccy emulator was all the undocumented opcodes, so this didn't really impact the design at all. The gameboy emulator was a different matter, all the different memory maps, and the paging system makes a simple thing like reading a byte from memory a slow process. The gameboy emulator had to run on low end devices, so I had to come up with a fast way of doing this before I started the project.

3) write a test harness.

the test harness is going to be where you do all your testing, so starting with this cements all the features you are going to need in the finished system.

4) write a dissassembler, and imbed into the harness.

Why do I start with the disassembler? By writing this first you get to know the structure of the cpu very well. I also write in such a way that I can use the disassembler as a template for the cpu core itself.

5) Write the cpu core and TEST it

Use macros for every memory access, then if you want to re-use the core for a different memory system later you only have to change two macros

---------
the next stages can be swapped around depending on what you are trying to emulate
---------

6) write game loading routines
7) write basic display handler
8) code hooks for sound (don't attempt to write the sound yet)
9) write input device handling

-------------------------------
at this stage you should be able to run some games from within the test harness, and when they crash (which they will!!!) you will be glad you have invested the time in it.
-------------------------------
10) write run time harness
11) write sound handlers
12) run lots of games


some things to watch out for.

a) stack overflow
on some devices (Speccy for one) the stack wrapping around was legal and in fact used by some coders. The danger comes if you are using a 32 bit value for the stack pointer instead of wrapping around from 0xffff to 0x0000 you would end up at 0x10000

b) undocumented opcodes
frequently used by old-school coders



anyway hope this helps





SubjectRe: Emulator Writing new Reply to this message
Posted bycodingsama
Posted on05/25/06 10:16 AM



Speaking as an NES emulator author, the most complex thing about writing an NES emulator is Memory Mappers. When you add mappers in to your emu, this is where you experience complexity.

So don't worry about them. Just support games like Baloon Fight, Mario, etc. that use "mapper 0": no mapper.

Zophar.net has enough info on the NES (under 'technical docs' section) to write your emulator. I'll give you a few steps:

I use C++, personally, as it's an elegant way of representing a machine, but you can use the same process in C if you want.

1) Create an NES class or object of some form. It should contain the PPU(picture processing unit), the system memory, the CPU, and the joystick input units.

2) READ UP ON the nes! nes.txt and nestech.txt from zophar.net are indespensible

3) Code up a simple routine to load the ROMs (since you won't be worrying about MMCs, you don't need a complex one, and you can just have a flat 64k array of memory to work with)

4) Now find something to get input and output with. I'd reccommend SDL or Allegro. Allegro.cc has info on Allegro, and SDL, well, google it. Allegro is great for starting out, but its timers kind of suck.

5) Program the memory reads/writes. Small task, but make sure to pay attention to the documentation!

6) Most of the work you'll do now is just writing the PPU.
It draws the pictures to the screen. Joysticks are a bit of a pain, but in the end, very easy and short.

And then...all of the sudden...you (hopefully) have a working NES emulator!

Unfortunately, programming the PPU is rather complex compared to the rest. Lots and lots of bit shifts and pointer maths. You can get it wrong very easy and have very little hope of debugging it. Just be very careful and make sure you understand the docs.


Previous ThreadView All ThreadsNext Thread*Show in Threaded Mode