This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Several Binary-to-Decimal Libraries

Several Binary-to-Decimal Libraries
by on (#17790)
Several Binary-to-Decimal Libraries

I've put together a fairly extensive set of binary to decimal routines for the NES, each fairly thoroughly tested. There are three versions: tiny, standard, and fast. They all have a BSD-style license which has very few restrictions.

nes_bin2dec.zip

Their performance compares as follows:
Code:
bin2dec:        260 bytes
bin2dec_24bit       Best:  106, Average:  758, Worst:  960
bin2dec_16bit       Best:   77, Average:  356, Worst:  437
bin2dec_8bit        Best:   55, Average:   65, Worst:   76

yxa2dec:        591 bytes
yxa_to_8_digits     Best:  183, Average:  324, Worst:  430 (234%)

xa2dec:         283 bytes
 xa_to_5_digits     Best:   98, Average:  161, Worst:  208 (221%)
  a_to_3_digits     Best:   42, Average:   49, Worst:   56 (133%)

bin2dec_mini24: 133 bytes
bin2dec_24bit       Best: 1079, Average: 1237, Worst: 1368 (62%)

bin2dec_mini16:  75 bytes
bin2dec_16bit       Best:  542, Average:  570, Worst:  597 (62%)

The package includes demos that use each one and print the result on screen. There's also a thorough test for the 16-bit routines. They can be assembled directly with ca65 or NESASM. The fast version is based on the code Mozz posted. The standard version is based on Quietust's table version of Mozz's algorithm. The tiny version is based on Tepples' binary table code.

Post if you have any questions of problems using them.

by on (#17791)
You could eventually implement some pseudo math creating virtual "software" BCD-Numbers.

I imagine that you'd write your own bcdINC/bcdDEC/bcdADD/bcdSUB methods for this, which'd properly handle overflows from 9 to A or underflows from 0 to F.

One would need to see how much time worst case things like (pseudo):
bcdINC 99999
would consume :)

by on (#17794)
In the bin2dec demo, your code loads the output digits and ORs them with '0' as it prints them to the screen - this is not needed, since you can just do "LDA #'0' | JSR bin2dec_init" at the top instead (and, indeed, that was the entire point of having bin2dec_init take a parameter).

by on (#17796)
I wanted to keep the demos as clear and simple as possible. I also thought that the feature might confuse some of the intended audience. For those it wouldn't, the third paragraph in the bin2dec section of the readme should be sufficient.

by on (#18006)
Cybergoth wrote:
You could eventually implement some pseudo math creating virtual "software" BCD-Numbers.


I've been discussing this in my blog:
http://www.atariage.com/forums/index.php?automodule=blog&blogid=54&showentry=2045

A friend of mine came up with some pretty cool pseudo BCD implementation that will require less than 50 cycles for an addition of two 24-Bit pseudo BCD numbers. Determining a single digit for the display is then just a simple table lookup. All in all some 250 bytes required for this solution. :)
Re: Several Binary-to-Decimal Libraries
by on (#118875)
Does nes_bin2dec.zip exist anymore? Per this post it should be http://blargg.8bitalley.com/misc/nes_bin2dec.zip but that's 404.
Re: Several Binary-to-Decimal Libraries
by on (#118879)
tepples wrote:
Does nes_bin2dec.zip exist anymore? Per this post it should be http://blargg.8bitalley.com/misc/nes_bin2dec.zip but that's 404.

Yeah, I also asked that here: viewtopic.php?p=118867#p118867
Re: Several Binary-to-Decimal Libraries
by on (#118889)
Only one that comes up now is bin2dec.zip. Looks like it's probably the one.