quick 6502 questions
Moderator: Moderators
quick 6502 questions
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?
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?
- never-obsolete
- Posts: 411
- Joined: Wed Sep 07, 2005 9:55 am
- Location: Phoenix, AZ
- Contact:
- never-obsolete
- Posts: 411
- Joined: Wed Sep 07, 2005 9:55 am
- Location: Phoenix, AZ
- Contact:
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.
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.
<rant>From a programmer's perspective, it's always a good idea never to assume anything.
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.
!? Nice and clear...dvdmth wrote:...and do something you don't need to do rather than not do something you need to.
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?
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.
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;
}
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)
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)
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.
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.