nesdev.com
http://forums.nesdev.com/

First steps in writing an emulator
http://forums.nesdev.com/viewtopic.php?f=3&t=10558
Page 3 of 6

Author:  janzdott [ Thu Oct 10, 2013 2:07 pm ]
Post subject:  Re: First steps in writing an emulator

^^I know that home made games use them, but no official games do, right? (EDIT: I totally missed the part where you said commercial games do... I didn't know that.) I'm just starting out with official mapper 0 games like Super Mario Bros 1. I'll implement unofficial opcodes once my emulator is actually up and running. Is Super Mario Bros 1 an alright ROM to start with? My CPU runs the title screen just fine as far as I can tell. I know enough OpenGL, so I can create a window and get the PPU working with just the title screen first. At least, that's what I was planning on doing. Here's what I think my next steps are...

- Start a PPU class with registers
- Create memory callbacks from 2000 to 3FFF to emulate mirroring, and redirect reads/writes to PPU registers
- Create PPU memory class (inherits from my generic memory class)
- Create memory callbacks for PPU class for mirroring
- Start working on the PPU

So far, I have very little knowledge of how the PPU works. I've only been focusing on the CPU so far. I'll go back to reading once I get to that point. Hopefully I can get something to display on the screen soon. I only started working on this 6 days ago, so I think I'm making good progress :D

Oh, and one question... For each CPU cycle, the PPU and APU cycle 3 times, right?

Author:  lidnariq [ Thu Oct 10, 2013 2:28 pm ]
Post subject:  Re: First steps in writing an emulator

Conventional wisdom is that SMB1 is actually one of the hardest "simple" games to get working. Starting off with something simpler, like Donkey Kong, might be preferable.

(See also: nesdevwiki:Tricky-to-emulate games and nesdevwiki:Game bugs )

Author:  janzdott [ Thu Oct 10, 2013 2:35 pm ]
Post subject:  Re: First steps in writing an emulator

Oh I didn't know that. I'll start with Donkey Kong then. What are the best resources for writing the PPU?

Author:  tokumaru [ Thu Oct 10, 2013 3:25 pm ]
Post subject:  Re: First steps in writing an emulator

janzdott wrote:
So far, I have very little knowledge of how the PPU works.

The PPU, just like the CPU, is constantly running, but instead of fetching and executing instructions it repeats the process of generating a video signal, which consists of: 20 scanlines of VBlank (70 for PAL), 1 pre-render scanline, 240 scanlines of picture, and finally, 1 dummy scanline (51 for Dendy). You just repeat this over and over, modifying the rendering parameters as the registers are written to.

Quote:
Oh, and one question... For each CPU cycle, the PPU and APU cycle 3 times, right?

The APU, being part of the CPU chip, probably runs at the same rate as the CPU, but someone else will have to confirm that, since my knowledge of the APU is limited.

As for the CPU/PPU ratio, yes, on NTSC each CPU cycle corresponds to 3 PPU cycles. But I wouldn't hardcode it this way, since the ratio for PAL is different (1 CPU cycle = 3.2 PPU cycles).

BTW, you can check NTSC/PAL/DENDY differences here. If you plan on supporting all 3 systems, you shouldn't hardcode any of the variable parameters.

Quote:
Oh I didn't know that. I'll start with Donkey Kong then. What are the best resources for writing the PPU?

I'd say our wiki has the most up to date information, but it's all in the format of reference documents, not guides of any sort. This looks like a good overview of what happens every frame.

Author:  janzdott [ Thu Oct 10, 2013 3:56 pm ]
Post subject:  Re: First steps in writing an emulator

tokumaru wrote:
janzdott wrote:
So far, I have very little knowledge of how the PPU works.

The PPU, just like the CPU, is constantly running, but instead of fetching and executing instructions it repeats the process of generating a video signal, which consists of: 20 scanlines of VBlank (70 for PAL), 1 pre-render scanline, 240 scanlines of picture, and finally, 1 dummy scanline (51 for Dendy). You just repeat this over and over, modifying the rendering parameters as the registers are written to.


That was actually very helpful. I've been looking, and I've been unable to find a simple description of what the PPU actually does each cycle. That gave me a better idea than any of the things I've read so far. I'm guessing it doesn't do a whole scanline each cycle?

Author:  tepples [ Thu Oct 10, 2013 5:12 pm ]
Post subject:  Re: First steps in writing an emulator

The PPU counts time in "dots", and it takes 341 dots (with some exceptions) to render one scanline.

Author:  janzdott [ Thu Oct 10, 2013 11:12 pm ]
Post subject:  Re: First steps in writing an emulator

I created a ppu class with registers, and a ppuMemory class. I have the character bank loaded into the ppu memory. Now I'm a little lost. I know about pattern tables and tiles and nametables. But I don't understand where the nametables come from. I'm guessing the cpu passes them to the ppu through 2000-2007? If that's right, how does the ppu read them into it's memory? All the information I can find discusses patters, tiles, nametables, sprites, palettes, etc. But I can't find anything that actually says "This is how the ppu works... This is what it does each cycle..." :?

Edit: I'm starting to understand it. I'm going to try loading nametables and outputting them as text to the console. I'll check back in when I get that done.

Author:  tokumaru [ Fri Oct 11, 2013 12:33 am ]
Post subject:  Re: First steps in writing an emulator

janzdott wrote:
But I don't understand where the nametables come from.

The name tables are usually stored in a 2KB RAM chip inside the NES (this can be changed though: carts can and do disable those 2KB and instead use their own memory for name tables, which can even be ROM! - so don't hardcode the name table memory to 2KB and don't assume it's always RAM), which the game program fills through $2006/$2007. Name tables are constantly changed as games run.

Quote:
If that's right, how does the ppu read them into it's memory?

When the CPU uses $2006/$2007 to send data to the PPU, this data is stored into the PPU memory. During rendering, the PPU will gather information from its own memory to generate the pixels that form the final picture.

Quote:
All the information I can find discusses patters, tiles, nametables, sprites, palettes, etc. But I can't find anything that actually says "This is how the ppu works... This is what it does each cycle..." :?

The wiki page I linked to has a very helpful diagram that indicates what happens each cycle. It doesn't get into details though, for that you have to check the rest of the page, and some other pages in the wiki. The info is all there, you just have to put it all together (otherwise writing an emulator would be no challenge at all!).

Author:  janzdott [ Fri Oct 11, 2013 2:52 pm ]
Post subject:  Re: First steps in writing an emulator

tokumaru wrote:
The wiki page I linked to has a very helpful diagram that indicates what happens each cycle. It doesn't get into details though, for that you have to check the rest of the page, and some other pages in the wiki. The info is all there, you just have to put it all together (otherwise writing an emulator would be no challenge at all!).


I've been looking at all the wiki pages. I usually have about 20 browser tabs open at a time, where most of them are the wiki! The problem was, there's SO much information that it makes it very difficult to just find the basics to get things started. If I'm looking for basic information, I have to read through tons of advanced stuff that just ends up confusing me. I don't mean to complain about the wiki at all. Its great. It was just a little overwhelming for someone who doesn't know any of this stuff.

I was having a really hard time understanding the PPU basics. But Ulfalizer and jero32 at #nesdev explained a lot of the basics to me. I was able to get my PPU registers and vlbank working enough so the games will start writing nametable data to the registers. I should be okay for a while. Now that I have something to build off of, I can hopefully get things to work. I was just clueless about where to start, and I needed some clarification.

I'm glad everyone here and at #nesdev is so helpful. I would have still been stuck on my CPU if you guys weren't here to explain things to me. If I get my emulator up and running, I'd like to write a really in-depth explanation of how to write one and where to start, in hopes that it would help out noobs like me :D

Author:  tepples [ Fri Oct 11, 2013 4:44 pm ]
Post subject:  Re: First steps in writing an emulator

janzdott wrote:
Ulfalizer and jero32 at #nesdev explained a lot of the basics to me. I was able to get my PPU registers and vlbank working enough so the games will start writing nametable data to the registers.

Would it have helped to describe a simplified behavior to mock the PPU in a new emulator to the point where Donkey Kong will start to boot?

Author:  janzdott [ Fri Oct 11, 2013 8:21 pm ]
Post subject:  Re: First steps in writing an emulator

Yeah, for people who don't know much about this stuff, it would be nice if there was a place that gave you a simple outline of what you need to implement to get you started and then you can worry about the details on your own. At least that's the way I learn, but everybody learns differently. I couldn't read through the wiki pages and discern the difference between minute details and important features. That's why I'd like to write a really detailed explanation for other newcomers when my emulator's working.

But they explained how the 2006-2007 registers work, and how the vblank flag works, and that's all I needed to get started. My games started writing to the registers after that, so I could start on my rendering code. Currently my PPU manages to get the correct nametable and pattern addresses, get the current tile number, read the pattern bytes, and come up with color index for each pixel of the background, not including attribute color because I'm not sure how that works yet. Next, I'm adding the color palette and a gui. I'm usually pretty good at figuring this stuff out once I get started. It's getting started that's the hard part haha. But hopefully I'll get some pixels on the screen tonight! Do you think I should use OpenGL or SDL? I know how to do this kind of thing easily in OpenGL, but I heard SDL handles input and audio as well, but I've never used it.

Edit: I decided to try out SDL. I gotta say, I'm liking it. It's very easy to use. It's kind of a pain to use OpenGL for this kind of thing. I would have had to create a texture and display it on a quad. With SDL, it's a lot easier and only a few lines.

Author:  janzdott [ Sat Oct 12, 2013 12:16 am ]
Post subject:  Re: First steps in writing an emulator

Well, my first PPU render was pretty disappointing. I don't think I see donkey kong in there.... Lol I'll see if I can find what the problem is.

Image

Edit: I'm on the right track. It looks like I'm not reading the right pixels from the pattern tiles

Image

Edit #2: It took a LOT of debugging and a pretty long time. I was doing almost everything correctly, there were just 2 very small and very stupid mistakes. Here's my first real screenshot! Does anyone know why the Donkey Kong letters are messed up?

Image

Author:  tepples [ Sat Oct 12, 2013 3:56 am ]
Post subject:  Re: First steps in writing an emulator

Just a guess, but are you emulating the +32 increment mode in PPUCTRL ($2000)? I imagine that it uses +32 to make vertical columns of blocks in the title.

Author:  janzdott [ Sat Oct 12, 2013 5:09 am ]
Post subject:  Re: First steps in writing an emulator

Yep. Everything else displays correctly, besides sprites because I'm not touching those yet. It even shows the little demo screen and Donkey Kong moves when he throws the barrels. Idk what it is :?

Oh, and there was a question I wanted to ask about the address register. Does it always keep track of the upper and lower bytes, or can something reset it?

Author:  Dwedit [ Sat Oct 12, 2013 11:13 am ]
Post subject:  Re: First steps in writing an emulator

Reading ppu status (2002) will reset the high-byte, low-byte latch.

Page 3 of 6 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/