# Been designing a computer and need to convert binary to BCD. How would I program an EEPROM for this task?

#### Nomking

Joined Apr 1, 2020
1
I've been designing a computer-based on the concept of the old Generation 2 computers (1401, PDP 1, etc). I'm designing the printer now and have come into the problem of splitting a 16-bit number into single-digit integers to be printed (the printer in question is similar to this concept https://hackaday.com/2013/04/11/automating-a-mechanical-typewriter/). I've read forums about using an EEPROM to do the task, but I don't have any coding experience. Could someone help me with how to create a lookup table?

Thanks!

#### crutschow

Joined Mar 14, 2008
25,367
Could someone help me with how to create a lookup table?
It's quite simple.
You just put the BCD number at the equivalent binary EEPROM address.
Thus for example, for 1000d you would have the BCD number 1 0000 0000 0000bcd at the binary address
11 1110 1000b.
Note that to store all 16 bit combination of binary-decimal conversions, you need a word length of 19 bits to store the max BCD value for 65,535d (110 0101 0101 0011 0101).

#### MrChips

Joined Oct 2, 2009
21,815
If this is a DIY computer construction project it would be simpler to leave the 16 bits in binary and display them in hexadecimal numbers.

#### atferrari

Joined Jan 6, 2004
4,086
If this is a DIY computer construction project it would be simpler to leave the 16 bits in binary and display them in hexadecimal numbers.
You would impress only geeks and even then...

#### MrAl

Joined Jun 17, 2014
7,792
I've been designing a computer-based on the concept of the old Generation 2 computers (1401, PDP 1, etc). I'm designing the printer now and have come into the problem of splitting a 16-bit number into single-digit integers to be printed (the printer in question is similar to this concept https://hackaday.com/2013/04/11/automating-a-mechanical-typewriter/). I've read forums about using an EEPROM to do the task, but I don't have any coding experience. Could someone help me with how to create a lookup table?

Thanks!
If you can spare the time there are simple algorithms you can use that only use minimal storage space.
I'd say it can be done within 512 bytes of RAM memory for both algorithm and stored results.
When the input is limited to just 16 bits the highest BCD number would be 65535 and here is one example of one way to do it...
Divide the number by 10000, the number that remains is the leftmost BCD digit which includes 0.
Multiply the number found by 10000 and subtract that from the number.
Divide the remainder by 1000, the number that remains is the next BCD digit.
Multiply the number found by 1000 and subtract that from the remainder from before.
Divide that remainder by 100, the number that remains is the next BCD digit.
Repeat that pattern until you get down to 1 and then the remainder is the last BCD digit.
This assumes you already have a divide routine and multiply routine which is often the case.
There are better algorithms though, this is the simplest in principle.

Here is the way it would progress although the numbers would be binary...
Say the number is 65432.
65432/10000=6 first digit on the left
6*10000=60000
65432-60000=5432
5432/1000=5 next digit
5432-5000=432
432/100=4 next digit
432-400=32
32/10=3 next digit
32-30=2 last digit on the right.
So we get 65432 in BCD even if the number was in binary to start with.

if you dont have a multiply and divide routine here is one way to emulate those operations...
65432-10000=>55432-10000=>45432-10000=>35432-10000=>25432-10000=>15432-10000=>5432-10000=>underflow
We now have an underflow condition so we note the number of times we were able to successfully subtract and that number is 6 and that is the first digit on the left.
Now that we have that '6', we do this:
10000+10000+10000+10000+10000+10000=60000
Now we subtract that 65432-60000=5432
and then we start subtracting 1000 over and over until we reach an underflow again.
The number of successful subtractions would have been 5 for the number 5432 so the next digit is 5.
Repeat that pattern until you get all 5 digits.
This only assumes you already have 16 bit binary add and subtract algorithms.

Last edited:

#### dl324

Joined Mar 30, 2015
11,456
I've read forums about using an EEPROM to do the task, but I don't have any coding experience. Could someone help me with how to create a lookup table?
Code for converting binary to BCD for 4 digits using an MC27C4002. Haven't thought about how to expand it to 16 bits.

#### BobTPH

Joined Jun 5, 2013
2,530
Do you really need it done in hardware?

Bob