quick 6502 questions

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

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

quick 6502 questions

Post by WedNESday »

BRK, IRQ and NMI are 7 cycles long. What about RESET?

Why is the D flag cleared at the start of every ROM (78, D8, A9...) even though there is no decimal mode, and ADC/SBC are not affected?
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Post by tepples »

Perhaps the CLD is left over from a debugging system that's based around a real 6502 chip.
User avatar
never-obsolete
Posts: 411
Joined: Wed Sep 07, 2005 9:55 am
Location: Phoenix, AZ
Contact:

Post by never-obsolete »

I always thought is was to set the flag to a known state, err thats what i do it for anyways.
User avatar
Disch
Posts: 1848
Joined: Wed Nov 10, 2004 6:47 pm

Post by Disch »

Yeah, but the flag itself doesn't have any impact at all (aside from what's pushed to the stack on PHP/Interrupt), so it doesn't matter if its a fixed state or not.
User avatar
never-obsolete
Posts: 411
Joined: Wed Sep 07, 2005 9:55 am
Location: Phoenix, AZ
Contact:

Post by never-obsolete »

For the nes thats true, but the people who wrote the nes games we all love probably wrote software before, for other systems using a 65xx processor and possibly out of habit set it to a known value. Even though it doesn't matter. That would be my best guess.
dvdmth
Posts: 354
Joined: Wed Mar 22, 2006 8:00 am

Post by dvdmth »

From a programmer's perspective, it's always a good idea never to assume anything. I read somewhere that a couple of GameBoy games did not work 100% correctly in the GBC because they assumed certain things to be true (and were true for the B&W system but not the GBC). If Nintendo had done something similar with the NES (making an improved system that was backwards compatible), what would have happened if the new system supported decimal mode but developers assumed it would never occur?

There are a number of things that games do (on powerup and during gameplay) that are ultimately redundant. It is best to err on the side of caution and do something you don't need to do rather than not do something you need to.
User avatar
baisoku
Posts: 121
Joined: Thu Nov 11, 2004 5:30 am
Location: San Francisco, CA
Contact:

Post by baisoku »

it's also extremely likely that it was a Nintendo software submission requirement, based on the principles already discussed here.
...patience...
User avatar
blargg
Posts: 3715
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Post by blargg »

From a programmer's perspective, it's always a good idea never to assume anything.
<rant>
How can you code a program without making assumptions about the machine you're programming for? It is a good thing to avoid relying on particular hardware behavior if you can easily avoid it (as was probably the case in the GBC example), but avoiding all assumptions would lead to very complex programs and more bugs.

I do see value in ensuring the D flag has a definite value at all times, because it reduces the chance of some errant code behaving differently than it does when you're developing. The value of the D flag affects the byte pushed on the stack whenever the status flags are saved, and some erroneous code could be examining it. It could also be helpful if you wanted to run your code on a NES-like machine that did have a decimal mode (i.e. had a full 6502 processor).

I don't like the general advice to "program defensively" because it leads to all sorts of complex arrangements to avoid problems the programmer dreams up. I consider it far better to document clearly what a routine expects and then make no attempt to handle situations outside these expectations. If you give routines reasonable expectations, the caller will be able to easily meet them. What this leads to is a layered system where the unpredictability of the outside world is progressively filtered out from the inner layers, without a huge burden on any one layer.
User avatar
Bregalad
Posts: 8056
Joined: Fri Nov 12, 2004 2:49 pm
Location: Divonne-les-bains, France

Post by Bregalad »

I think the SNES actually has a D flag, doesn't is ?
Anyway, I think wasting one single byte to clear the D flag is really unsignificant, so it is better to do it to get proper 6502 code.
Useless, lumbering half-wits don't scare us.
WedNESday
Posts: 1284
Joined: Thu Sep 15, 2005 9:23 am
Location: Berlin, Germany
Contact:

Post by WedNESday »

dvdmth wrote:...and do something you don't need to do rather than not do something you need to.
!? Nice and clear...

Thanks for the replies. blargg has proved a theory of mine already about games the do not use RTI but instead manipulate the stack by hand. Now that would affect some games. BTW how long is RESET?
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Post by tepples »

Reset is however long you keep the button pushed in.
WedNESday
Posts: 1284
Joined: Thu Sep 15, 2005 9:23 am
Location: Berlin, Germany
Contact:

Post by WedNESday »

Can anybody shed any more light on this? Let's imagine that the button was held down for the shortest period of time possible.

Here is my RESET procedure.

Code: Select all

inline void RESET()
{
	CPU.IF = 0x04;
	CPU.PC = CPU.Memory[0xFFFC] + (CPU.Memory[0xFFFD] << 8);
	CPU.CC += 7;
}
User avatar
Disch
Posts: 1848
Joined: Wed Nov 10, 2004 6:47 pm

Post by Disch »

Pressing the Reset button may reset the PPU in ways too... and if that's the case the PPU might re-align itself to a certain time within the frame, making the number of cycles the CPU reset takes moot, since it would need to be re-aligned to whever the PPU reset to.

Note: Above is completely speculatory.

That said... when my emu performs a soft reset, I reset my timestamps to the same time I do for a hard reset (10 scanlines before VBlank ends). With NMIs disabled, the I flag set, and $2002.7 high.

Also, SP is reduced by 3 on reset (though nothing is pushed to the stack)
User avatar
Memblers
Site Admin
Posts: 4044
Joined: Mon Sep 20, 2004 6:04 am
Location: Indianapolis
Contact:

Post by Memblers »

I'd imagine it's the same length as IRQ and NMI. I'd like to hear about a case where it could make a difference, heheh.

I don't think the top-loading NES resets the PPU. Holding reset often you can see any mid-frame bankswitching stop and the sprites glitch. On the front-loader the screen goes black.
Post Reply