Writing up your own emulator?

Announce new emulators, discuss which games run best under each emulator, and much much more.

Moderator: General Mods

mog123
Hazed
Posts: 62
Joined: Sat Apr 01, 2006 11:33 pm

Writing up your own emulator?

Post by mog123 »

Hey, I've been reading some docs about writing emulators, and I'm really getting into this stuff. Can someone tell me if I've understood it enough?

Let's say I want to make a 6502 cpu core, so I have to make different variables for an accumulator, registers X, Y, flag status etc. and I come down with this:

Code: Select all

while (cycles_remaining > 0)
{
   instruction = read_mem(PC);
   PC++;
   switch (instruction)
   {
      case 0xA9:   //LDA #NN
         reg_a = read_mem(PC);
         PC++;
         negative_flag = (reg_a & 0x80) != 0;
         zero_flag = reg_a == 0;
         cycles_remaining -= 3;
      
      case 0xAA:  //TAX
         reg_x = reg_a;
         cycles_remaining -= 2;
      
      case 0x8A:  //TXA
         reg_a = reg_x;
         cycles_remaining -= 2;
      
      case 0x8D:  //STA $nnnn
         address_low = read_mem(PC);
         PC++;
         address_high = read_mem(PC);
         PC++;
         address = address_low + address_high * 0x100;
         write_mem(address, reg_a);
         cycles_remaining -= 4;
         // more instructions etc.
   }
} 
Let's say the core is written, I have the memory map (How do I get about doing the memory map?), ppu (this will be just a set of functions and calls, right?), i/o etc done also.

Then I load the rom, Starts with PC=0, and just execute the functions according to the PC?

Another thing, what system would you think would be easy to emulate for a first try?

Thanks for all your replies
My general dev blog:
http://mog123x.blogspot.com
lordmissus
Ignorant Child
Posts: 326
Joined: Mon Apr 06, 2009 10:10 pm
Location: 1984

Post by lordmissus »

I know nothing about the 6502, but as a learning exercise I have been working on writing my own emulator* for an imaginary, non-existent, theoretical machine that I've devised, and your code looks fine (assuming it does indeed emulate the 6502).

*Written in C#. I can't share it though because I'm using it as a project in the Computer Programming course that I'm taking.
mudlord
has wat u liek
Posts: 559
Joined: Tue Sep 11, 2007 2:54 pm
Location: Banland.

Post by mudlord »

Don't forget to make it cycle accurate. Otherwise certain developers will flame you for it. I know from personal experience. :?
kode54
Zealot
Posts: 1140
Joined: Wed Jul 28, 2004 3:31 am
Contact:

Post by kode54 »

Though, depending on what sort of emulator you're writing, you can get away with instruction-level execution. You just need to keep a cycle counter in each component, then pass an offset cycle count to components you interact with.

For instance, say you have a read operation that actually touches the memory 2 cycles after the start of the instruction. So you call object.read(address, current_cycle + 2) and object emulates whatever it needs to up to the correct cycle count for the value you want to read. Rather than, say, executing a single cycle of each component at a time.

Even if the slave component does its own reading or writing, none of it matters if the master (CPU) can't see it yet. So you can freely execute the master until it touches an address owned by a slave component, for which the slave needs to catch up and potentially write to an address the CPU is reading. Or read from an address the CPU is writing, before the cycle where the write actually takes place.

Fun things to consider.

Oh yeah, and things may get complicated if your theoretical system is to have interrupts break in. Of course, they may not be a problem if you can know ahead of time what cycle they'll occur on.

More importantly, though, this sort of design may not be good for initial learning experience. Optimizing before mastering the basics, and such. I've never written a cycle accurate emulator, but I've worked with a few before.
mog123
Hazed
Posts: 62
Joined: Sat Apr 01, 2006 11:33 pm

Re: Writing up your own emulator?

Post by mog123 »

mog123 wrote: Let's say the core is written, I have the memory map (How do I get about doing the memory map?), ppu (this will be just a set of functions and calls, right?), i/o etc done also.

Then I load the rom, Starts with PC=0, and just execute the functions according to the PC?

Another thing, what system would you think would be easy to emulate for a first try?
Again, thanks for all the replies.
Now, please try to elaborate on the topics I quoted.
My general dev blog:
http://mog123x.blogspot.com
whicker
Trooper
Posts: 479
Joined: Sat Nov 27, 2004 4:33 am

Re: Writing up your own emulator?

Post by whicker »

mog123 wrote:
mog123 wrote: Let's say the core is written, I have the memory map (How do I get about doing the memory map?), ppu (this will be just a set of functions and calls, right?), i/o etc done also.

Then I load the rom, Starts with PC=0, and just execute the functions according to the PC?

Another thing, what system would you think would be easy to emulate for a first try?
Again, thanks for all the replies.
Now, please try to elaborate on the topics I quoted.
Read the datasheet for the CPU in question. Program Counter "is set initially, and by interrupts, from vectors at memory addresses FFFA through FFFF (hex). See IRQ# NMI# and #RESET below..."

ANY commercially available CPU has a manual for it. It has to have one because the same kind of things you are dealing with is faced by everyone.

Unfortunately any video game system has its quirks due to memory size limitations and a desire to limit component count. I don't know your abilities. Examine what you can do, what information you already have, and where you can get the information, when choosing a system. If I said emulate a VIC 20, you'd get mad at me because the games suck and the video output looks like ass.
mog123
Hazed
Posts: 62
Joined: Sat Apr 01, 2006 11:33 pm

Post by mog123 »

Would starting with a Z80 / Gameboy / Arcade games would be good practice?
My general dev blog:
http://mog123x.blogspot.com
whicker
Trooper
Posts: 479
Joined: Sat Nov 27, 2004 4:33 am

Post by whicker »

The Z80 is a difficult processor. But so many people have already written emulators for it. I say it's difficult because it tries to do a lot: refresh dram, access ports outside of the normal address range, handle 3 different interrupt modes, have a set of swappable shadow registers, and complicated instruction fetching.

The Gameboy GB-z80 is highly customized, poorly documented, and has quirks. Other than by looking at the existing emulators I don't see that helping much.
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

lordmister wrote:your code looks fine
It doesn't in the slightest.

BREAK BETWEEN YOUR SWITCH CASES, DUMMY !
Else they get executed in sequence.

PC is probably loaded with the reset vector at boot, $FFFC.
You also want to be sure PC will wrap on the correct boundary instead of blindly using ++.

NES is mapper, ppu and interrupt hell, btw. Not very nice for a first emu.
Last edited by grinvader on Wed Jul 29, 2009 6:08 am, edited 1 time in total.
皆黙って俺について来い!!

Code: Select all

<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Pantheon: Gideon Zhi | CaitSith2 | Nach | kode54
mog123
Hazed
Posts: 62
Joined: Sat Apr 01, 2006 11:33 pm

Post by mog123 »

A simple SNES emulator isn't a bit too deep water?
grinvader wrote:
lordmister wrote:your code looks fine
It doesn't in the slightest.

BREAK BETWEEN YOUR SWITCH CASES, DUMMY !
Yeah, sorry for that, just a quick write up on the spot.
My general dev blog:
http://mog123x.blogspot.com
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

mog123 wrote:A simple SNES emulator isn't a bit too deep water?
There's nothing simple about snes either, MX flavours for most opcodes, wrapping on various bounds for addressing modes, some more irq stuff, yadda yadda

Oh, btw, TAX and TXA set the NZ flags too.
You'll have to get better docs.
皆黙って俺について来い!!

Code: Select all

<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Pantheon: Gideon Zhi | CaitSith2 | Nach | kode54
Rashidi
Trooper
Posts: 515
Joined: Fri Aug 18, 2006 2:45 pm

Post by Rashidi »

it seems docs for ARM were easy to read & understood.
is there any other game consoles that use ARM chip other than GBA/NDS?
mudlord
has wat u liek
Posts: 559
Joined: Tue Sep 11, 2007 2:54 pm
Location: Banland.

Post by mudlord »

Woah, there is more than one chip based on the ARM architecture.

There is the 3D0, some part of the Dreamcast, etc...
Gil_Hamilton
Buzzkill Gil
Posts: 4294
Joined: Wed Jan 12, 2005 7:14 pm

Post by Gil_Hamilton »

Rashidi wrote:it seems docs for ARM were easy to read & understood.
is there any other game consoles that use ARM chip other than GBA/NDS?
The NGage is ARM-based. According to Wikipedia.
I.S.T.
Zealot
Posts: 1325
Joined: Tue Nov 27, 2007 7:03 am

Post by I.S.T. »

ARM cpus are used everywhere. I'm sure you can find official manuals and such rather easily.
mog123
Hazed
Posts: 62
Joined: Sat Apr 01, 2006 11:33 pm

Post by mog123 »

I think I'm gonna go with the SNES for starters, lot's of resource on these boards, and maybe I can get some help from byuu :roll:
My general dev blog:
http://mog123x.blogspot.com
mudlord
has wat u liek
Posts: 559
Joined: Tue Sep 11, 2007 2:54 pm
Location: Banland.

Post by mudlord »

mog123 wrote:I think I'm gonna go with the SNES for starters, lot's of resource on these boards, and maybe I can get some help from byuu :roll:
.................
All I can say is: Good luck and nice knowing you. It'll be best if you also side with byuu's forum instead of this once. People here tend to cause flamewars with anything to do with scene politics and emulation methods. In time, you will come to understand why that is so.
adventure_of_link
Locksmith of Hyrule
Posts: 3634
Joined: Sun Aug 08, 2004 7:49 am
Location: 255.255.255.255
Contact:

Post by adventure_of_link »

mudlord wrote:
mog123 wrote:I think I'm gonna go with the SNES for starters, lot's of resource on these boards, and maybe I can get some help from byuu :roll:
.................
All I can say is: Good luck and nice knowing you. It'll be best if you also side with byuu's forum instead of this once. People here tend to cause flamewars with anything to do with scene politics and emulation methods. In time, you will come to understand why that is so.
Yeah, really. Trying to have an open mind over linux sound systems got me labeled as a "troll." :?
<Nach> so why don't the two of you get your own room and leave us alone with this stupidity of yours?
NSRT here.
mudlord
has wat u liek
Posts: 559
Joined: Tue Sep 11, 2007 2:54 pm
Location: Banland.

Post by mudlord »

adventure_of_link wrote:
mudlord wrote:
mog123 wrote:I think I'm gonna go with the SNES for starters, lot's of resource on these boards, and maybe I can get some help from byuu :roll:
.................
All I can say is: Good luck and nice knowing you. It'll be best if you also side with byuu's forum instead of this once. People here tend to cause flamewars with anything to do with scene politics and emulation methods. In time, you will come to understand why that is so.
Yeah, really. Trying to have an open mind over linux sound systems got me labeled as a "troll." :?
Exactly. Its their way, or the wrong way (which is yours, for anything).
Like I got labeled by them a "troll", for thinking optimization matters and that certain emulation methods are not a big deal :/
Good luck reasoning with them. You have to be pretty much in their inner circle if you want to be treated right.
Gil_Hamilton
Buzzkill Gil
Posts: 4294
Joined: Wed Jan 12, 2005 7:14 pm

Post by Gil_Hamilton »

You got labeled a troll for the MANNER in which you shared your opinions, actually. There's a big difference.

Which leads into the truly spectacular hypocrisy of you ranting about "their way or the wrong way" attitudes.
Given most of the "sharings of opinion" that I saw were direct attacks on someone else doing things in a manner that was WRONG and NOT YOUR WAY, often at random, out of nowhere, and in largely unrelated threads...
mudlord
has wat u liek
Posts: 559
Joined: Tue Sep 11, 2007 2:54 pm
Location: Banland.

Post by mudlord »

Gil_Hamilton wrote:You got labeled a troll for the MANNER in which you shared your opinions, actually. There's a big difference.

Which leads into the truly spectacular hypocrisy of you ranting about "their way or the wrong way" attitudes.
Given most of the "sharings of opinion" that I saw were direct attacks on someone else doing things in a manner that was WRONG and NOT YOUR WAY, often at random, out of nowhere, and in largely unrelated threads...
Sorry, that person just got on my nerves. Other people are not as much as a issue. However, the point remains.

And this is not a rant. If it was a rant, I would be trolling again on certain people and how they do things. Am I doing that now? No. When I rant, I troll. And I am not trolling now.
funkyass
"God"
Posts: 1128
Joined: Tue Jul 27, 2004 11:24 pm

Post by funkyass »

AOL got labeled a troll for violations concerning Godwin's law.
Does [Kevin] Smith masturbate with steel wool too?

- Yes, but don’t change the subject.
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

mudlord wrote:It'll be best if you also side with byuu's forum instead of this once. People here tend to cause flamewars with anything to do with scene politics and emulation methods.
???

Oh yeah, we're totally flaming spiller, the JSNES dood.
Except not.
However, there are many clear differences between spiller and mog123 (I'll let you search for the corresponding threads), which will likely cause differences in how we'll react to their questions.
皆黙って俺について来い!!

Code: Select all

<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Pantheon: Gideon Zhi | CaitSith2 | Nach | kode54
mudlord
has wat u liek
Posts: 559
Joined: Tue Sep 11, 2007 2:54 pm
Location: Banland.

Post by mudlord »

...I hardly know that person, so im not flaming it >.<

I'm refering to mr obvious, no one else.
Gil_Hamilton
Buzzkill Gil
Posts: 4294
Joined: Wed Jan 12, 2005 7:14 pm

Post by Gil_Hamilton »

mudlord wrote: And this is not a rant. If it was a rant, I would be trolling again on certain people and how they do things. Am I doing that now? No. When I rant, I troll. And I am not trolling now.
Sorry. I have a bad tendency to misuse rant.
Post Reply