SMB 3 spiked ceilings in castles bug

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
User avatar
miker00lz
Posts: 235
Joined: Thu Sep 23, 2010 7:28 pm

SMB 3 spiked ceilings in castles bug

Post by miker00lz » Fri Dec 27, 2013 11:42 pm

Well, I know there's something wrong with my mapper 4 implementation (probably IRQ counter related) that has to be causing this one. Any idea which aspect of it I should be looking at first that could be making this nastiness happen?

Image

:x

3gengames
Formerly 65024U
Posts: 2277
Joined: Sat Mar 27, 2010 12:57 pm

Re: SMB 3 spiked ceilings in castles bug

Post by 3gengames » Sat Dec 28, 2013 1:52 am

irq? sprite 0?

WedNESday
Posts: 1236
Joined: Thu Sep 15, 2005 9:23 am
Location: Berlin, Germany
Contact:

Re: SMB 3 spiked ceilings in castles bug

Post by WedNESday » Sat Dec 28, 2013 4:37 am

3gengames wrote:irq? sprite 0?
AFAIK there is no Sprite 0 timing in SMB3. It looks like IRQ to me alright.

How is the rest of the game? How are other Mapper 004 games? Does your emu pass the MMC3 tests?

User avatar
ulfalizer
Posts: 349
Joined: Fri Mar 08, 2013 9:55 pm
Location: Linköping, Sweden

Re: SMB 3 spiked ceilings in castles bug

Post by ulfalizer » Sat Dec 28, 2013 6:23 am

Are you setting the period counter to zero when $C001 is written? Do you reload it with the most recent value written to $C000 when it's clocked by the scanline counter while being zero?

I'd mark up lines where IRQs are being asserted and look at that along with a textual trace of MMC3 IRQs.

User avatar
Zepper
Formerly Fx3
Posts: 3220
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: SMB 3 spiked ceilings in castles bug

Post by Zepper » Sat Dec 28, 2013 9:37 am

Post your IRQ code from mapper 4 source/VBlank.

User avatar
miker00lz
Posts: 235
Joined: Thu Sep 23, 2010 7:28 pm

Re: SMB 3 spiked ceilings in castles bug

Post by miker00lz » Sat Dec 28, 2013 1:27 pm

ulfalizer wrote:Are you setting the period counter to zero when $C001 is written? Do you reload it with the most recent value written to $C000 when it's clocked by the scanline counter while being zero?

I'd mark up lines where IRQs are being asserted and look at that along with a textual trace of MMC3 IRQs.
AH! I was doing this on write to $C000/$C001:

Code: Select all

				if (addr & 1) map4->irqcounter = 0;
					else map4->irqlatch = value;

Changing it to this:

Code: Select all

				if (addr & 1) map4->irqcounter = map4->irqlatch;
					else map4->irqlatch = value;

Gives me this:

Image


Much better, but my mapper 4 implementation is still slightly off though. You can see how the first scanlines where the IRQs happen are shifted sideways a bit. Also, when vertically scrolling the screen (i.e. Mario flying or the spiked ceilings moving) everything under where the IRQ scanline was is a little unstable. It's tough to explain. If you want to see what I mean, here's the exe I just compiled:

http://rubbermallet.org/currentmoarnes.zip

Turn into raccoon Mario on the first level of SMB 3 and start flying up into the sky and watch the status bar as you do it to see what I mean!
BTW, there are four quicksave slots in MoarNES. F1 through F4 saves state, F5 through F8 loads them again.
Last edited by miker00lz on Sat Dec 28, 2013 1:58 pm, edited 1 time in total.

User avatar
miker00lz
Posts: 235
Joined: Thu Sep 23, 2010 7:28 pm

Re: SMB 3 spiked ceilings in castles bug

Post by miker00lz » Sat Dec 28, 2013 1:30 pm

WedNESday wrote:
3gengames wrote:irq? sprite 0?
AFAIK there is no Sprite 0 timing in SMB3. It looks like IRQ to me alright.

How is the rest of the game? How are other Mapper 004 games? Does your emu pass the MMC3 tests?
The game is still fully playable. I don't pass the MMC3 tests. I tried using them a while back, but I wasn't getting anywhere with them. Asking about things here usually helps more. :)

Most other mapper 4 games have odd glitches to some extent too. TMNT 2 for example:

Image

Looks like the wrong CHR bank is being used there or something.
Last edited by miker00lz on Sat Dec 28, 2013 1:39 pm, edited 1 time in total.

User avatar
miker00lz
Posts: 235
Joined: Thu Sep 23, 2010 7:28 pm

Re: SMB 3 spiked ceilings in castles bug

Post by miker00lz » Sat Dec 28, 2013 1:33 pm

Zepper wrote:Post your IRQ code from mapper 4 source/VBlank.
I do this at the start of each frame where I turn off the vblank flag for the first scanline:

Code: Select all

	if (cartridge.mapper == 4) map4->irqcounter = map4->irqlatch;

At the beginning of every scanline I run map4work() which is:

Code: Select all

void map4work() {
	if (cartridge.mapper == 4) {
		if (map4->irqenable && map4->irqcounter == 0) irq6502();
		if (map4->irqcounter == 0) map4->irqcounter = map4->irqlatch;
		if (PPU->bgvisible || PPU->sprvisible) map4irqdecrement();
	}
}

tepples
Posts: 22054
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: SMB 3 spiked ceilings in castles bug

Post by tepples » Sat Dec 28, 2013 2:07 pm

In most MMC3 games, your map4work should be done at the first sprite fetch (dot 261 of the line), not at the beginning of the line. (Actually, it should be done when the PPU address switches from $0xxx or $2xxx to $1xxx for the first time in each scanline.)

User avatar
ulfalizer
Posts: 349
Joined: Fri Mar 08, 2013 9:55 pm
Location: Linköping, Sweden

Re: SMB 3 spiked ceilings in castles bug

Post by ulfalizer » Sat Dec 28, 2013 2:46 pm

miker00lz wrote:
ulfalizer wrote:Are you setting the period counter to zero when $C001 is written? Do you reload it with the most recent value written to $C000 when it's clocked by the scanline counter while being zero?

I'd mark up lines where IRQs are being asserted and look at that along with a textual trace of MMC3 IRQs.
AH! I was doing this on write to $C000/$C001:

Code: Select all

				if (addr & 1) map4->irqcounter = 0;
					else map4->irqlatch = value;

Changing it to this:

Code: Select all

				if (addr & 1) map4->irqcounter = map4->irqlatch;
					else map4->irqlatch = value;
The old version before the change is what I have. irqcounter is only ever reloaded from irqlatch as the result of a clock from the scanline counter - never as a result of writing one of the MMC3 registers.

To be more specific, irqlatch is copied to irqcounter when you get a clock from the scanline counter and irqcounter equals zero.

The fact that your change "almost" fixes things suggests something might be wrong with your scanline counting or reloading logic. Normally the value of irqcounter would go 0, irqlatch, irqlatch - 1, ..., where the initial 0 is from the $C001 write and the decrements are from scanline clocks. In your version it'll go irqlatch, irqlatch - 1, irqlatch - 2, ... instead, which probably is why it's off by one. I would check that the 0-to-irqlatch transition on the first scanline clock is working as expected first.

User avatar
ulfalizer
Posts: 349
Joined: Fri Mar 08, 2013 9:55 pm
Location: Linköping, Sweden

Re: SMB 3 spiked ceilings in castles bug

Post by ulfalizer » Sat Dec 28, 2013 3:25 pm

miker00lz wrote:
Zepper wrote:Post your IRQ code from mapper 4 source/VBlank.
I do this at the start of each frame where I turn off the vblank flag for the first scanline:

Code: Select all

	if (cartridge.mapper == 4) map4->irqcounter = map4->irqlatch;
This shouldn't be needed. The MMC3 itself doesn't keep track of frames.

miker00lz wrote:At the beginning of every scanline I run map4work() which is:

Code: Select all

void map4work() {
	if (cartridge.mapper == 4) {
		if (map4->irqenable && map4->irqcounter == 0) irq6502();
		if (map4->irqcounter == 0) map4->irqcounter = map4->irqlatch;
		if (PPU->bgvisible || PPU->sprvisible) map4irqdecrement();
	}
}
With this approach you'd want everything to be conditional on PPU->bgvisible || PPU->sprvisible. That's required for the MMC3 to see scanlines, and the scanline clocks are what trigger other events. Also note that reloading and decrementing irqcounter is an either-or thing: You should never both reload and decrement it during the same clock.

User avatar
Dwedit
Posts: 4354
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: SMB 3 spiked ceilings in castles bug

Post by Dwedit » Sat Dec 28, 2013 9:21 pm

I tested out Slalom, and can see that the scrolling code is buggy. But MMC3 emulation is solid enough to run my Chu Chu Rocket game correctly. So weird to see Chu Chu Rocket running correctly, but other games getting completely screwed up.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!

Post Reply