Jump to content

Recommended Posts

Hi Guys,

 

I have been tracking down an issue in a game I am creating where the my scanline count was going wrong and it was because somehow the decimal flag was being set so a 'sbc' was not as I expected.

 

Currently my work around is doing a 'cld' just before this bit of code but I don't know how it gets set in the first place. I have looked for the obvious and I am not doing a 'sed' anywhere in my code.

 

Does anyone know more about this to advise me how this flag can get set in another way that might help me find the answer?

 

It would be good to set some kind of cognitional breakpoint in stella so I can just find the code that way but I don't know if its possible.

 

Thanks,

Mike

Share this post


Link to post
Share on other sites

Ok it looks like I managed to track it down myself. It seems stella 3.9.2 sometimes starts with the decimal flag set and if starts like this my game crashes. My old 3.8 build of stella did not have the same behaviour so is the new behaviour the same as the real hardware?

Share this post


Link to post
Share on other sites

General practice with 6502 startup is the computer OS does SEI / CLD / LDX #$FF / TXS before anything else.

Unsure what happens in reality - possibly the datasheet/specs documents have it listed.

 

In normal operation decimal mode should only occur in certain situations:

SED instruction, as a result of PLP instruction and the stack entry has it set, RTI instruction where flags has D mode set.

 

Unsure if any illegal instructions inadvertantly set D mode. Interrupt stuff can largely be ignored on 2600 since it doesn't use them.

 

Some programmers use PHP/PLP on 2600 to use 6502 flags so potential exists there for problems.

Share this post


Link to post
Share on other sites

The 2600 powers up in a random state. There's no built-in initialization routine of the CPU, TIA, and RIOT chips, so any initialization-- including setting or clearing the decimal flag-- must be done by the game cartridge.

 

Also, if the game cartridge uses bank switching, you can't be certain which bank will be active on power-up, so the bank that you want to use as the start-up bank should jump to the initial routine, but the other banks need to jump to a bit of code that switches to the desired start-up bank before jumping to the initial routine. This especially applies to bank-switching schemes that swap the entire 4K cartridge memory. If the bank-switching scheme has smaller banks, the uppermost bank area is typically frozen in place and can't be switched, but the program will still need to switch the other bank area(s) to the desired bank(s).

Share this post


Link to post
Share on other sites

Thanks for the info I didn't know the CPU could be in a random state too. I still find it wired that this was not simulated in Stella 3.8

Share this post


Link to post
Share on other sites

Well, the 6502 (and 6507) CPU does go through an initialization procedure at power-up, but I think it mainly has to do with loading the program counter with the address in the reset vector, and maybe clearing the interrupt lines (not the interrupt flags, but rather the interrupt signals) to ensure that the program starts from the reset vector rather than the NMI vector or IRQ vector. I don't think it clears or sets any of the processor flags, since setting or clearing the flags (or the more important ones, anyway) is one of the things that initialization routines usually do-- and there'd be no need to do that if the CPU's power-up procedure already took care of it.

Share this post


Link to post
Share on other sites

I do not think you need to do a CLD before each time the routine is run. That would possibly waste program space and certainly waste cycles.
A CLD at the very beginning of your game code should do it. A universal procedure when programming the Atari 2600. The D flag is undefined on power up.

 

***

Here is a good overview of the 6502 decimal mode. (Some of it may not apply to the 6507).

http://www.6502.org/tutorials/decimal_mode.html

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...