6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat May 04, 2024 10:42 am

All times are UTC




Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Thu Nov 09, 2017 8:52 pm 
Offline

Joined: Sat Sep 16, 2017 12:19 pm
Posts: 64
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.


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 09, 2017 9:17 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10793
Location: England
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!


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 09, 2017 9:22 pm 
Offline

Joined: Sat Sep 16, 2017 12:19 pm
Posts: 64
BigEd wrote:
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!


Thanks!


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 09, 2017 9:33 pm 
Offline

Joined: Sat Sep 16, 2017 12:19 pm
Posts: 64
BigEd wrote:
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!


Also, what's the highest number you can store with Decimal mode in 8 bit?


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 09, 2017 9:58 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10793
Location: England
What do you think? And why?


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 10, 2017 2:16 am 
Offline

Joined: Tue Jul 24, 2012 2:27 am
Posts: 672
LBSC wrote:
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.

_________________
WFDis Interactive 6502 Disassembler
AcheronVM: A Reconfigurable 16-bit Virtual CPU for the 6502 Microprocessor


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 10, 2017 7:03 am 
Offline

Joined: Sat Sep 16, 2017 12:19 pm
Posts: 64
White Flame wrote:
LBSC wrote:
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?


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 10, 2017 7:16 am 
Offline
User avatar

Joined: Fri Nov 09, 2012 5:54 pm
Posts: 1392
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. :)


Last edited by ttlworks on Fri Nov 10, 2017 9:06 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 10, 2017 7:24 am 
Offline

Joined: Sat Sep 16, 2017 12:19 pm
Posts: 64
ttlworks wrote:
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.


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 10, 2017 7:39 am 
Offline
User avatar

Joined: Fri Nov 09, 2012 5:54 pm
Posts: 1392
LBSC wrote:
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.


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 10, 2017 7:50 am 
Offline

Joined: Sat Sep 16, 2017 12:19 pm
Posts: 64
ttlworks wrote:
LBSC wrote:
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!


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 10, 2017 10:54 am 
Offline

Joined: Wed Oct 06, 2010 9:05 am
Posts: 95
Location: Palma, Spain
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.


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 10, 2017 4:31 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1928
Location: Sacramento, CA, USA
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.


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 10, 2017 4:41 pm 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3350
Location: Ontario, Canada
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.

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 10, 2017 5:03 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10793
Location: England
(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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron