Page 3 of 3

Re: processor detection

Posted: Mon Sep 11, 2017 11:48 pm
by chitselb
This might not be the best time to ask, but other than processor detection and maybe making calculators, is there any good purpose for the SED/CLD instructions? The PET ROM does CLD during initialization, and nothing I've ever seen turns it on without turning it back off again. But in tons of programs I've seen (admittedly mine too!) , there is this CLD in there somewhere near the beginning, an homage to the D flag

Re: processor detection

Posted: Tue Sep 12, 2017 12:51 am
by leepivonka
SED & CLD are a handy way to change D.
The D flag controls whether ADC & SBC work in binary or BCD.

If doing ADC on $99 and $01 with C clear:
If D is set, the result is $00
If D is clear, the result is $9A

Re: processor detection

Posted: Tue Sep 12, 2017 4:21 am
by White Flame
Yeah, you certainly want decimal mode off by default, and only turn it on when you need it. ADC and SBC are very widely used in assumption of full 8-bit mode to toy with leaving it on. Many IRQ routines also forget to CLD, so interrupting decimal mode code can be dangerous. Note that since the processor flags get effectively PHP/PLP'd in the IRQ/RTI mechanism, the routine doesn't have to worry about restoring the prior state of D manually.

Beyond the financial fixed point & high score calculations, there are also some clever routines that print a register in ASCII decimal digits that use decimal mode for the conversion (and turn it off when they're done ;) ).

Re: processor detection

Posted: Tue Sep 12, 2017 5:45 am
by GARTHWILSON
Note that the CMOS '02 (65c02) does clear the D flag upon reset or interrupt. The NMOS needs the CLD in the reset routine and ISRs though.

Re: processor detection

Posted: Tue Sep 12, 2017 7:59 am
by BitWise
I always set the I flag and clear D flag in my reset routines in case an application has executed JMP ($FFFC) to simulate a hardware reset.

Re: processor detection

Posted: Tue Sep 12, 2017 8:42 am
by BigEd
BitWise wrote:
I always set the I flag and clear D flag in my reset routines in case an application has executed JMP ($FFFC) to simulate a hardware reset.
An excellent safeguard!

Re: processor detection

Posted: Wed Sep 13, 2017 4:10 am
by BigDumbDinosaur
Elsewhere on this tie, there is a "universal" binary to ASCII conversion routine that uses decimal mode.