Page 1 of 2
Implementation of the decimal mode.
Posted: Thu Nov 09, 2017 8:52 pm
by LBSC
This is (in my opinion) the hardest thing ever. I only know how to do Binary to BCD conversion, using the massive Double Dabble decoder, but the 6502 seems to be very small, same for the 4004 and both CPUs have decimal mode. Please help me with the implementation and theory.
Re: Implementation of the decimal mode.
Posted: Thu Nov 09, 2017 9:17 pm
by BigEd
I think this has been covered - the gate level design is in the patent. See the links in Ken Shirriff's article, for example:
http://www.righto.com/2013/08/reverse-e ... cimal.html
If you find other posts in discussions here, please link to them!
Re: Implementation of the decimal mode.
Posted: Thu Nov 09, 2017 9:22 pm
by LBSC
Re: Implementation of the decimal mode.
Posted: Thu Nov 09, 2017 9:33 pm
by LBSC
Also, what's the highest number you can store with Decimal mode in 8 bit?
Re: Implementation of the decimal mode.
Posted: Thu Nov 09, 2017 9:58 pm
by BigEd
What do you think? And why?
Re: Implementation of the decimal mode.
Posted: Fri Nov 10, 2017 2:16 am
by White Flame
I only know how to do Binary to BCD conversion, using the massive Double Dabble decoder, but the 6502 seems to be very small, same for the 4004 and both CPUs have decimal mode.
The 6502 doesn't do any 8-bit binary to decimal conversion. It only performs addition/subtraction with numbers already in BCD form, so that's just additional fixup logic on the normal adder, which is enabled in Decimal mode.
Re: Implementation of the decimal mode.
Posted: Fri Nov 10, 2017 7:03 am
by LBSC
I only know how to do Binary to BCD conversion, using the massive Double Dabble decoder, but the 6502 seems to be very small, same for the 4004 and both CPUs have decimal mode.
The 6502 doesn't do any 8-bit binary to decimal conversion. It only performs addition/subtraction with numbers already in BCD form, so that's just additional
fixup logic on the normal adder, which is enabled in Decimal mode.
Oh, so the highest number is 99 or does it get an Hex input and converts it to BCD? Also, how do you do subtraction? Do I have to subtract 6 for the correction or something?
Re: Implementation of the decimal mode.
Posted: Fri Nov 10, 2017 7:16 am
by ttlworks
Please start reading about 6502 decimal mode
here.
To make it short:
Imagine to have two 4 Bit ALUs instead of one 8 Bit ALU.
There is some circuitry at the output of a 4 Bit ALU, checking if the result from a BCD ADD\SUB isn't decimal.
If the result isn't decimal, the circuitry at the ALU output corrects the result... and the carry output.
Also, it might be helpful to take a look at some
BCD test code.
Another idea would be to dig into the C source code of VICE (which emulates the NMOS 6502 on a PC).
And yes, BCD mode on the 6502 is a bit tricky...
Edit: Just to be fair, I felt a need to mention:
NMOS 6502 flag evaluation in BCD mode for NVZ checks the output of the ALU before it enters the correction circuitry. (So NVZ isn't valid.)
65C02 flag evaluation in BCD mode for NVZ checks the outputs of the correction circuitry. (So NVZ is valid.)
Take care.

Re: Implementation of the decimal mode.
Posted: Fri Nov 10, 2017 7:24 am
by LBSC
Please start reading about 6502 decimal mode
here.
To make it short:
Imagine to have two 4 Bit ALUs instead of one 8 Bit ALU.
There is some circuitry at the output of a 4 Bit ALU, checking if the result from a BCD ADD\SUB isn't decimal.
If the result isn't decimal, the circuitry at the ALU output corrects the result... and the carry output.
Also, it might be helpful to take a look at some
BCD test code.
Another idea would be to dig into the C source code of VICE (which emulates the NMOS 6502 on a PC).
And yes, BCD mode on the 6502 is a bit tricky...
Thanks! Now I know how to build a BCD adder/subtracter but, what''s the highest number you can store with 8 bit BCD? I think it was 99 but I'm not sure.
Re: Implementation of the decimal mode.
Posted: Fri Nov 10, 2017 7:39 am
by ttlworks
what's the highest number you can store with 8 bit BCD? I think it was 99 but I'm not sure.
For 8 Bit BCD, the highest valid number would be $99 (representing decimal 99).
https://en.wikipedia.org/wiki/Binary-coded_decimal
Point is, that BCD (binary coded decimal) uses "chunks" of 4 Bits.
Each of the 4 Bit "chunks" only can be in the range 0..9.
A..F in a 4 Bit "chunk" won't be a "valid" number for BCD.
Re: Implementation of the decimal mode.
Posted: Fri Nov 10, 2017 7:50 am
by LBSC
what's the highest number you can store with 8 bit BCD? I think it was 99 but I'm not sure.
For 8 Bit BCD, the highest valid number would be $99 (representing decimal 99).
https://en.wikipedia.org/wiki/Binary-coded_decimal
Point is, that BCD (binary coded decimal) uses "chunks" of 4 Bits.
Each of the 4 Bit "chunks" only can be in the range 0..9.
A..F in a 4 Bit "chunk" won't be a "valid" number for BCD.
Oh, thanks!
Re: Implementation of the decimal mode.
Posted: Fri Nov 10, 2017 10:54 am
by RichTW
Also consider that, unlike binary addition and subtraction, decimal mode addition and subtraction are operations which work differently.
The best reference I've found is
Bruce Clark's article which also precisely documents the flag results, and the differences between NMOS and CMOS 6502 handling of decimal mode.
Re: Implementation of the decimal mode.
Posted: Fri Nov 10, 2017 4:31 pm
by barrym95838
As far as I know, you can handle SBC with legal BCD values by taking the nine's complement of the operand and falling through to a BCD ADC. What I don't know is whether or not using this method will provide results similar to the actual hardware for illegal BCD values.
Mike B.
Re: Implementation of the decimal mode.
Posted: Fri Nov 10, 2017 4:41 pm
by Dr Jefyll
Wow, lots of helpful comments. I'll keep mine short. In case it isn't already obvious, sometimes normal addition and BCD addition are the same thing. Eg: 1 + 1 = 2, either way.
Written in binary, that involves the following 4-bit chunks: 0001 + 0001 = 0010. That's valid binary addition and it also satisfies the rules of BCD addition.
The interesting part happens when the addition produces a sum which exceeds 1001 aka "nine". Nine is the biggest chunk BCD can express, so a fixup (which I won't describe) is required. But there's no fixup needed if the sum is nine or less.
Re: Implementation of the decimal mode.
Posted: Fri Nov 10, 2017 5:03 pm
by BigEd
(Slightly OT, but referring back to the original post and the possible slight confusion between Decimal Mode, which changes ADC and SBC behaviour, and the different problem of converting between binary and BCD,
here's Wikipedia on that Double Dabble conversion method.)
Bit of a diversion: I think one of the thresholds in understanding computers is when you realise that bit patterns are just bit patterns, and those are all the values there are, and they only have meaning according to what you do with them. For example, 10011001 could be the Ö character, or the ™ character, or it could be the number 99, or it could be the number 153, or it could be the number -103, or it could represent the positions of 8 toggle switches, or mean 'light green' ... and for any of those numbers, of course the number might be a time, an angle, a volume, a distance, an amount of money, and so on.