6502 Decimal Mode

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
Bah
Posts: 2
Joined: Tue Nov 01, 2022 12:30 am

6502 Decimal Mode

Post by Bah »

First off, I'm fully aware that the 6502's decimal mode is not supported in NES, and that my question would be better suited for 6502.org.
However, after several futile attempts of trying to get the activation email and not finding any info on who to contact (other than 'board administrator' with no specific contact info), I thought I'd try my luck here, as there is a high probability of someone knowing the answer.

So, I'm writing a 6502 emulator (just the processor), and I'm trying to understand how exactly the decimal mode works. I KNOW that when the flag is set, the processor considers each digit of a byte as a decimal number if the digit is in the range of 0-9, and if either digit is in the range of A-F, it is considered an invalid BCD number. All the examples I've seen are when the BCD number is valid, but what happens when it is an INVALID BCD?
for example, what would the result be in the following case?

Code: Select all

SED
LDA #$20
CLC
ADC #$1A
Will the least significant digit wrap over (effectively becoming #$10 in BCD), will it be treated as a binary digit (the whole byte effectively becoming #$20 in BCD) or will the whole byte be treated as a binary number?

Also, if anyone could give me any info on who to contact regarding my problems on registration to 6502.org I would thankful.
User avatar
dougeff
Posts: 3078
Joined: Fri May 08, 2015 7:17 pm

Re: 6502 Decimal Mode

Post by dougeff »

I believe that, with the D flag set, invalid values A-F are wrapped upward towards the next valid value.

So...
01 to 09, keep the same.
0A -> 10
0B -> 11
0C -> 12
0D -> 13
0E -> 14
0F -> 15

so that tens digit will then be added to the existing tens digit, then the upper digit will be evaluated like...

00 to 90, keep the same
A0 -> 00
B0 -> 10
C0 -> 20
D0 -> 30
E0 -> 40
F0 -> 50


In your example of adding 1a... the 1a will be treated the same as 20 (a = 10, add 10 to 10 = 20)
20 + 20, the result after adding will be A = 40



Pseudo code to write an emulator...
if((value & 0x0f) > 9)
{
value = value + 6;
}
if(value > 0x9f)
{
value = value + 0x60;
}
value = value & 0xff; // clamp to 2 hex digits


edit...
so decimal mode only affects the result of adding and subtracting, and you would handle each digit separately

result = A + B + carry

result low = (A & 0x0f) + (B & 0x0f) + carry
if result low > 9 then add 6
result = (A & 0xf0) + (B & 0xf0) + result low
if result > 0x9f then add 0x60

I think that's right
nesdoug.com -- blog/tutorial on programming for the NES
User avatar
dougeff
Posts: 3078
Joined: Fri May 08, 2015 7:17 pm

Re: 6502 Decimal Mode

Post by dougeff »

See "add8" and "sub8" and how it handles the decimal flag

https://github.com/SourMesen/Mesen-S/bl ... ructions.h
nesdoug.com -- blog/tutorial on programming for the NES
Bah
Posts: 2
Joined: Tue Nov 01, 2022 12:30 am

Re: 6502 Decimal Mode

Post by Bah »

Yes, after realizing I could do some quick tests on visual 6502, it seems you are right.
Thanks a bunch, this helps a lot.
unregistered
Posts: 1318
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Re: 6502 Decimal Mode

Post by unregistered »

Bah wrote: Tue Nov 01, 2022 1:53 am However, after several futile attempts of trying to get the activation email
Are you using a VPN? Sometimes my VPN ip address is considered bad by some sites. I was just thinking that maybe your VPN ip address was awarded a bad badge by 6502.org, and so maybe that’s why you weren’t sent an activation email.


In this case, it may be helpful to disable or pause your VPN while you register for 6502.org. Then, after receiving and using the activation email, feel free to re-enable your VPN.

Note: The USPS never has enjoyed my VPN ip address, but while VPN is disabled, I’m free to track my packages. :)


Disabling/enabling a VPN only changes your ip address. So, just do that whenever you are having problems accessing a needed site. Remember that countless people may have used your current VPN ip address, and one of them may have been malicious toward the site you are trying to access.
Post Reply