COOKIES

This site may be using cookies to melk you with your own data. I, ben0bi, am not the owner of this web service and I also do not maintain their servers. But the EU and the owner of this service think, that the user (me) has the responsibility to inform the consumer (you), that this website uses cookies. Again: I, ben0bi, NEVER use cookies. I am not responsible for the setup of this web service. I just present some information here and do not intend to spy on you for whatever reason ever. But (also again), I do not host this website nor do I maintain any servers related to this website nor do I benefit from using the cookies maintained from this service. I hereby give the responsibility for using cookies on blogspot back to the owners of blogspot.

Donnerstag, 26. Februar 2015

How to write an emulator. (Part 1)

05.2015: I stopped working on this but I leave it online. Maybe I will continue that later.
05.2017: I started again working on this but I will do it with JavaScript now. This post totally needs an update for that approach but I will leave it as is and make a new post ( in german). So you can refer to this one for...stuff you may need.


I don't know. But I will write down, step by step, all what I do to accomplish it. (For my brains, can't hold all that. ;) )

This one is not for beginners in programming. Also, I will only cover how to make an old game console emulator. It's not about emulating a certain CPU - the CPU emulator will be included from third parties.

I am making a tutorial series about that. In this post we do some research. Actual work will be done in the next posts.

A friend of mine once wrote a Sega Master System Emulator. I did a graphical refresh for it.



Here is the Download (Windows exe and dll):
SMSEmu.zip on Mirror 1 (OneDrive)
SMSEmu.zip on Mirror 2 (homeserver) (direct download, maybe with left click/Save as..)

Since I lost the all the code and since I found out that WinPhone is an awesome platform to program with, I decided to do my own emulator and maybe a whole emulator pack. It could be fun.

First I want to re-do the SMS (Sega Master System), mostly because my friend has done it and...because I've got some ROMs.

Here are the SMS ROMs. Please ONLY use them for testing your emulator. DO NOT SHIP THEM with it. That would be illegal. I uploaded them only because they are relatively hard to find and you will need some ROMs at a certain point for further developing.

So, here's the ROM download:
SMSroms.zip on Mirror 1 (OneDrive)
SMSroms.zip on Mirror 2 (homeserver) (direct download)

Get The Basics

This is where I started. If you are new to the topic, have a look at it.

http://fms.komkon.org/EMUL8/HOWTO.html

But the links all are a little bit outdated. This tutorial came out when the internet just started, and all the newsgroups and stuff are mostly not actual and hip. ;)

Ok then, lets write that all down.
I want to do it with C#.
I HAVE to do it with C# if I want to port that to WinPhone.
(Actually, for using DirectX I would have to use C++. I hope, I find another solution.)

To develop WinPhone-Apps, you need something like this:
http://www.microsoft.com/de-ch/download/details.aspx?id=44914
(That's MS Visual Studio Express 2013 with Update 4 - WinPhone SDK should be included here.)

But there is a better version, Visual Studio Community. I don't know if that works with WinPhone, so I use the one recommended by Microsoft.

Some Theoretical Thoughts

I want to do a multi-purpose multi-platform emulator. It should be possible to emulate several consoles with one program and it should be possible to port the code to allmost any platform.

Best case scenario would be that you load any ROM and the application decides by filename and/or file signature, what emulator to create and start. You could put any supported ROM into your ROM folder and have to decide absolutely nothing (on the mobile phone, PC version needs an input configuration ;) ) and could just play the game - no matter if it's a Sega or Nintendo, MAME game and what console it actual is.

My platform decision is WindowsPhone, but I want to write the code as flexible as possible to include it in Unity3D or other Engines/Platforms. (With Unity3D, almost any platform can be reached, from PC/Mac/Linux over Android/iPhone/WP? up to *real* consoles like PS3/PS4/XBoxXXX/Wii. So, doing the emulator with Unity would be a great way to reach almost every people.)

Problem with Unity is that RTT (Render-To-Texture) is only possible with a paid version of Unity. You could not do RTT just for fun here. And that's what we need to render the old 2D-images on to a scaled texture which fits the whole screen.

[Edit] I am trying it with Unity, though. Maybe there is a possibility to, in any way, draw on the texture.

[Edit 2]: As of 2019, it is now possible to use RTT in Unity Free. I managed to do it with another project. It's not even hard to do. But I don't know exactly what I did. Just to note...RTT works with Unity Free now. So, Go for it.

But for the emulator, this is not a problem at all. The emulator just returns an array of pixels, which the graphics engine must use "outside" of the emulator. You can use just any graphics engine which can render pixels to a texture or directly to screen. If you want, you can try it with the Windows-Desktop-GUI-API. I will do it with the WinPhone API using Direct3D [MonoGame] and also try it with Unity.

Sound seems to be the most easy thing to emulate first from an old console [the SEGA Master System]. According to the tutorial ^ there was an SMS-emulator written in one Day - the only thing it could do was playing the sound from the ROM. So, I think, emulating the sound engine is more easy than the graphics itself.

Get The Facts


If you want to emulate something, you need to know about it.
I want to do an SMS emulator first. Friend gave me this links for it and mentioned the first (top) document, which he also used.

http://www.smspower.org/Development/Documents

This document will give more explanation than the other ones:
http://www.smspower.org/uploads/Development/richard.txt

I need to learn all I can about it...

Get The CPU

Most old - really old - consoles use the Z80-CPU as main CPU. That's the case for GameBoys, SEGA Master System, SEGA GameGear and I don't know what else. So, we will firstly use the Z80 CPU and maybe add some other CPUs later.

I did a search for a C# Z80 CPU Emulator and found this one:

https://code.google.com/p/cs80/source/checkout

Thank gods, it exists. We do not have to do it ourselves. Contributions go to: [Unknown]
It's released with the New BSD License.

I downloaded it using SlikSvn: https://sliksvn.com/download/

Friend told me we need only the Z80.cs file, all other stuff is (PC-?)hardware related addendum.



*(See below) I switched to Linux and try to do it now with Linux stuff. Please be patient.

More in the next parts,
[Not Done*]Basic Unity3D Setup
or
Basic Winphone Setup (with MonoGame).

You can skip that part if you want to use another Framework/Engine/Platform than WinPhone and Monogame. What you have to accomplish is to render pixels on the screen. I want to render them on an image (which could be stretched) and then show the whole image on the screen.

After that, we will Build Your Framework which is the interface between the engine and the emulators itself.