Thursday, September 16, 2010

NES emu progress

For those that don't know, I had started a NES emu project from scratch a while back, but then I got bored and stopped working on it in favor of some other projects.
This past week I have regained my interest in it and have made some progress.
After some hours of debugging, it now passes all normal opcode tests in Kevin Horton's nestest.nes rom.
Last week it failed most of those tests miserably (turns out I was just setting wrong flags in a few opcodes like BIT and PLP).

I currently have mappers 0(no-mapper),1,2,3,4,and 7 implemented. I think mapper #1 has a few bugs and mapper #4 needs to have its IRQ timer code redone later on to be more accurate (but its okay for now).
Mappers 2,3, and 7 are kind-of fun because they're so easy to implement. Each can be done in just a few lines of code.

For example, my Mapper #2 implementation is just:

class Mapper2: public Mapper {
public:
Mapper2() { Init(); }
virtual ~Mapper2() { Close(); }
void WritePROM(u16 addr, u8 value) {
clamp(value, ROM.romBanks, "Mapper2: PROM");
PROM_SLOT[0] = _PROM[value*2+0];
PROM_SLOT[1] = _PROM[value*2+1];
}
};


Something I've noticed when browsing through other NES emu's source-code, is that many of the emulators have horribly messy code. There is one popular emulator that uses OOP too much. When this happens it fucks up intellisense and makes browsing code a PITA. You right click on any method to go to the definition and intellisense finds ~50 possible matches and makes you chose manually which method is the one you're looking for. This always happens in projects that use too much OOP and is a very annoying problem.

Another popular emulator does the opposite; it doesn't use any OOP even though the code is now in c++ (I guess originally it was in C). Anyways the whole code is very C-like and abuses macros like crazy, this makes everything a mess. Its a common mistake to abuse macros; I did it when I started with c++, but any good coder knows that you should only use macros when there's no better way. Oh and the code uses 2-space tabbing which is ridiculous.

On the subject of macros, its more understandable to see macro usage in C code compared to C++ code. This is because C is very limited compared to C++, and there is a lot more stuff you can't do so you use macros to simulate these features. In C++ however you have templates and references which can help you remove the need for many macros. This is a big reason why C++ is better than C.
Many C-programmers move on to C++ and still code C-like because they don't know proper C++, that could be the case with the nes-emu I looked at. Anyways I suppose I'll save a C/C++ comparison rant for another blog post.

So back to talking about my emulator, cottonNES...
My emulator is no-where near ready for a release. Although it boots hundreds of games now that the big cpu bugs are fixed, it still has problems in many games (most-likely to do with PPU and timing issues...).

I would like to continue discussing nes emulation further here, but sadly I have an exam this week and need to study D:

I suppose this post will be boring without screen-shots :D



6 comments:

  1. It's good to see your project... I started one (JNesBR made in Java) but it's been a long time since I touch it! Keep coding cotton.

    ReplyDelete
  2. Nice job Leandro, I looked up your project and you got some nice debugging tools; i don't have anything implemented with my debugger lol.
    So far i just print out opcodes and reg-values to console. I have to implement real debugging tools at some point.

    I noticed though in my emu projects its usually easier to find bugs while reviewing code, than it is to analyze debugging output and find bugs that way. Although i've never wrote a powerful debugger so guess that could be why.
    On my work with pcsx2's vu recompilers for example, i printed html-assembly logs of vu programs and i couldn't actually step-through the opcodes because in a recompiler its much more difficult to make a step-through debugger.
    Although that kindof gives me the idea that maybe someday i should make a step-through debugger for microVU.

    ReplyDelete
  3. Can i look the source code ?
    owh it's open source :D , no ?
    I want to become emu developer like Shadow(pcsx2 former dev's)
    maybe this is a good start for me :D

    ReplyDelete
  4. Hi Rachman,
    The emu isn't open source yet because I'm still working on it, and I don't want to make it open source till I'm mostly done with it.
    If you have good programming experience you can probably jump into NES emulation, there are many other open-source NES emulators out there to get you started.

    If you're still not that experienced with programming and emulation, then a NES emulator probably is not the place to start.
    There is a simpler system called the Chip-8 which is probably a better place to start.
    Here is a thread to get you started if you want to try a Chip-8 emulator:
    http://forums.ngemu.com/web-development-programming/114578-chip8-thread.html
    In that thread i posted source-code to a chip-8 emulator i did, and also other people have posted their own chip-8 emulators.

    ReplyDelete
  5. Thanks for the input.
    Keep coding cotton. :D
    actualy i want write emulator for my final scription in my collage, but my friend and my teacher laugh at me.
    maybe someday i will prove it to them.

    ReplyDelete
  6. haha Rachman, yeah its always fun to prove people wrong like that.
    That should give you some extra motivation to work hard and accomplish the goal.
    Good luck!

    ReplyDelete