Advertisement

I'm writing an NES emulator

Started by August 16, 2009 01:04 AM
34 comments, last by essial 15 years, 2 months ago
**edit** You can follow my progress at http://projectemulate.blogspot.com/ now. **end edit** I have finally decided to write an NES emulator. I've always been fascinated by emulators, and I think I am to the point skill-wise to actually be able to pull it off. I'm well aware that there are countless NES emulators out there, but again, I want to write one both for fun, and to be able to say that I did :) I'm on day 3 now and here's where I'm at: Obviously thing's aren't quite right yet, but I'm getting there. Currently, most games crash (I'm testing games that don't require a mapper, I'll add mapper support once I get everything else working). Input appears to be working correctly, and of course no sound yet. I'm doing this in C++ and am using an array of function pointers to call the opcodes efficiently without going through a giant if/else or switch statement (conditional jumps destroy CPU caching). If you guys want I'll post updates as they come; otherwise I wont say anything else about it -- just wanted to let you guys know! [Edited by - essial on August 16, 2009 11:06:32 PM]
Looks pretty cool...
Advertisement
Nice job.

It doesn't matter if there are a billion of the exact same program if you are enjoying writing it, and are learning from it.
That's neato. Can you elaborate more on the process? I never done anything like an EMU or virtual machine and would actually be curious to how one works? Are you basically just taking all the instructions in the ROM file and just writing C++ equivalents using stuff like SDL, while also creating a "fake" CPU / RAM / etc as a virtual environment for it? Or is it more involved than that?
Comrade, Listen! The Glorious Commonwealth's first Airship has been compromised! Who is the saboteur? Who can be saved? Uncover what the passengers are hiding and write the grisly conclusion of its final hours in an open-ended, player-driven adventure. Dziekujemy! -- Karaski: What Goes Up...
Quote: Original post by Koobazaur
That's neato. Can you elaborate more on the process? I never done anything like an EMU or virtual machine and would actually be curious to how one works? Are you basically just taking all the instructions in the ROM file and just writing C++ equivalents using stuff like SDL, while also creating a "fake" CPU / RAM / etc as a virtual environment for it? Or is it more involved than that?


It's a bit more involved. If I am successful at making this emulator I plan on releasing full docs on how I did it (along with what I believe to be the CORRECT NES specs, as some are wrong), and possibly even a few videos.

But basically at the heart of the system is the processor; Since the processor in the US nintendo is called 2A03 (a derivative of the 6502), I created a class called "CNES2A03". Once I did that, it was a matter of having function pointers for memory read/writes (in the real world this would be sent to the address bus and such), handlers for the opcodes, a set of flags, and some very important functions such as "processOpcode()" and "generateNMI()". Of course it gets a lot more complicated once you get into the video rendering (as you can tell by my shots :p).

But as I said, if I can make this thing work, I'll release a nice fresh and modern doc explaining everything accurately.
Respect.
Advertisement
Yeah right now I am fighting an issue that seems to be related to a stack overflow (meaning, when RTS is called, the wrong address is poped off the stack. It's pretty obvious when seen through my CPU trace:

[#$F4FA] $18 <- CLC (Clear Carry Flag)
[#$F4FB] $F0 <- BEQ (Branch if Equal)
[#$F4FD] $38 <- STC (Set Carry Flag)
[#$F4FE] $66 <- ROR (Rotate Right)
[#$F500] $66 <- ROR
[#$F502] $66 <- ROR
[#$F504] $66 <- ROR
[#$F506] $66 <- ROR
[#$F508] $66 <- ROR
[#$F50A] $66 <- ROR
[#$F50C] $66 <- ROR
[#$F50E] $60 <- RTS (Return from subroutine)
[#$0FE5] $0 <- BRK (Break - This address should not be executed at)
[#$0FE6] $0 <- BRK (Break)
[#$0FE7] $0 <- BRK (Break)

So hopefully I can figure this one out and get mario and donkeykong running.

On the other hand, I finally have nestress.nes running (nes stress test rom) without crashing:

You should probably blog this. I'd definitely like to see the progress on this project as it unfolds. Then again, posting here makes it easier for you to get feedback. Best of luck!
If enough people actually want me to blog about it then I will, I just figured there wouldn't be too much interest as this is not exactly something new :)
Well if I disable my kill-switches (invalid ops, etc) mario looks like it's doing ~something~..


But anyway, I'm off to run some errands but I'll be back later :)

This topic is closed to new replies.

Advertisement