Page 1 of 1

EOR #$FF - 6502 Ponderables and Befuddlements

Posted: Mon Oct 16, 2023 5:42 pm
by JoJo1973
I can't believe nobody has still mentioned this excellent "poetry" book by David Youd!

https://archive.org/details/eor6502

For your edification and purification of spirit! :wink:

Re: EOR #$FF - 6502 Ponderables and Befuddlements

Posted: Mon Oct 16, 2023 6:52 pm
by BigEd
Welcome! I'm not so suprised, as it seems to have been self-published this last week.

However, it has some intriguing snippets, such as

Code: Select all

ASL
ADC #$80
ROL
ASL
ADC #$80
ROL
(I imagine many of us will immediately grasp some proportion of the snippets, and ponder over some others, depending on our experience and habits. The specific ponderables will be different for each of us.)

I think it's worth a look - thanks!

Re: EOR #$FF - 6502 Ponderables and Befuddlements

Posted: Sat Nov 01, 2025 4:26 pm
by chitselb
$1E has this:

Code: Select all

 SED
 CMP #$0A
 ADC #$30
 CLD


I am pretty sure (on the PET anyway) that this needs to be performed in between SEI / CLI so that when the jiffy clock IRQ strikes, we don't have a problem with the processor being in decimal mode.

Re: EOR #$FF - 6502 Ponderables and Befuddlements

Posted: Sat Nov 01, 2025 5:46 pm
by BigDumbDinosaur
chitselb wrote:
$1E has this:

Code: Select all

          SED
          CMP #$0A
          ADC #$30
          CLD


I am pretty sure (on the PET anyway) that this needs to be performed in between SEI / CLI so that when the jiffy clock IRQ strikes, we don't have a problem with the processor being in decimal mode.
That requirement applies to all versions of the NMOS 6502.  The 65C02 and 65C816 return to binary mode when responding to any interrupt.

Re: EOR #$FF - 6502 Ponderables and Befuddlements

Posted: Sat Nov 01, 2025 5:55 pm
by BigEd
A bit of an omission in the PET ROM - it became clear that an ISR should CLD if it needs to. One fix in the ISR, instead of fixes in every application that uses decimal mode. But then again, decimal mode is rather rarely used...

Re: EOR #$FF - 6502 Ponderables and Befuddlements

Posted: Sat Nov 01, 2025 8:25 pm
by BigDumbDinosaur
BigEd wrote:
A bit of an omission in the PET ROM - it became clear that an ISR should CLD if it needs to. One fix in the ISR, instead of fixes in every application that uses decimal mode. But then again, decimal mode is rather rarely used...

Actually, decimal arithmetic was used quite a bit in the applications for which the 6502 was originally intended.  I worked on several such things in the 1970s, and all used BCD numbers for tracking human-readable quantities.  I was a bit taken aback when I got my Commodore 64 and discovered that the interrupt service routine didn’t revert to binary arithmetic.

That said, there was no use of BCD arithmetic in any of the Commodore 8-bit machines until the C-128.  Working with the C-64’s and C-128’s TOD clocks in the foreground could get interesting, since they read out in BCD.

It’s likely the 6502 not flipping back to binary arithmetic was part of the cost-reduction process—a few fewer transistors to use up die space.  Or it could be in the heady rush to get product out the door, it was overlooked.  Alas, the one individual who would have known for sure is no longer with us.

Re: EOR #$FF - 6502 Ponderables and Befuddlements

Posted: Mon Nov 03, 2025 3:57 am
by 6502inside
That got a little tricky virtualizing the 6502 in 6o6 because guest code can execute a SED and transiently have decimal mode set before the VM records the flag and cleans the status register. On a system like the PET or C64 that doesn't compensate, this could lead to a race condition, so for the Incredible KIMplement I have the system Timer A IRQ revectored to ensure the decimal flag is clear (since many KIM-1 programs do arithmetic in decimal mode).