Mikes360 Posted January 8, 2014 Share Posted January 8, 2014 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 Quote Link to comment Share on other sites More sharing options...
Mikes360 Posted January 8, 2014 Author Share Posted January 8, 2014 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? Quote Link to comment Share on other sites More sharing options...
Rybags Posted January 9, 2014 Share Posted January 9, 2014 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. Quote Link to comment Share on other sites More sharing options...
SeaGtGruff Posted January 9, 2014 Share Posted January 9, 2014 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). Quote Link to comment Share on other sites More sharing options...
Mikes360 Posted January 9, 2014 Author Share Posted January 9, 2014 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 Quote Link to comment Share on other sites More sharing options...
SeaGtGruff Posted January 9, 2014 Share Posted January 9, 2014 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. Quote Link to comment Share on other sites More sharing options...
jdrose Posted June 22, 2014 Share Posted June 22, 2014 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.