Page 1 of 2
New demo uploaded: Floating Point in 6502
Posted: Thu Jan 14, 2021 4:23 pm
by gavinhaslehurst
Floating Point in 6502
View File
**EDITED TO ADD: for all those who are new like me, exploring this world, please see the forum thread where some of the more experienced coders here have pointed out some really useful Kernal routines which take the pain out of this!!
Hi all! As I journey towards 6502 mastery (LOL), this demo explores floating point numbers and how they are stored and managed in binary. It borrows heavily from others' code to achieve what I was struggling to do from first principles, and I am grateful to all the YouTubers, bloggers and hobbyists out there who have kindly shared their work in this area.
This particular routine takes a binary floating point number stored in memory and displays it on the screen in a human-readable decimal format. It also dumps some of the memory addresses involved so you can have a look under the hood.
Some functions include:
jsr FLTTODEC Displays the floating point number stored in
MSB, NMSB, NLSB, LSB and
BEXP as a decimal number on screen (PETSCII string)
jsr print_mem_16 .word (addr)
Memory dump. Shows paired bytes at the address, looping for
MemDumpLen addresses (default=8)
If you click try it now, you can poke around in memory to change the starting parameters of the programme. For example, to change the most significant byte of the mantissa,
POKE $080E,XX and to change the binary exponent,
POKE $081E,XX then type
RUN again to see the results.
FLTTODEC was adapted by me for the specific hardware of the Commander X16 from Jeff Tranter's code, who in turn adapted it for CC65 from the original appearing in Compute! issues 9 and 11, 1981 by Marvin L. De Jong.
https://github.com/jefftranter/6502/blob/master/asm/wozfp/bcdfloat.s Jeff's Blog:
https://jefftranter.blogspot.com/
Submitter
Submitted
01/14/21
Category
New demo uploaded: Floating Point in 6502
Posted: Thu Jan 14, 2021 4:49 pm
by desertfish
Do I understand correctly that this program provides its own code and storage format for dealing with floating points?
Or does it use the same binary storage format as what the basic in rom does?
New demo uploaded: Floating Point in 6502
Posted: Thu Jan 14, 2021 10:16 pm
by rje
It looks like it is BCD-to-float conversion, based on work originally from Compute! magazine?
New demo uploaded: Floating Point in 6502
Posted: Fri Jan 15, 2021 6:36 am
by gavinhaslehurst
Correct RJE. Floats are stored in the usual way and converted using the Compute! code. I'm slowly working my way through the whole floating point maths thing, and this exercise is to help understand more how it all works in machine code. DesertFish has a good point though. I could add a line of basic to store a floating point number somewhere and see if the code works with it!
New demo uploaded: Floating Point in 6502
Posted: Fri Jan 15, 2021 9:17 am
by desertfish
You could also try to use the basic/kernal floating point routines such as FOUT $fe81 to print the floating point values
New demo uploaded: Floating Point in 6502
Posted: Fri Jan 15, 2021 9:50 am
by gavinhaslehurst
30 minutes ago, desertfish said:
You could also try to use the basic/kernal floating point routines such as FOUT $fe81 to print the floating point values
Oh man, this is why this forum is so useful! I'm really new to this stuff, and although it's been a great learning experience to literally work out floating point, 2s complement etc from scratch, finding out there is a kernal routine is really useful! I will search out some decent references for this and explore my options.
Still, definitely time well spent as I've really enjoyed getting under the hood of how some of this stuff actually works. Thanks again for your help!
New demo uploaded: Floating Point in 6502
Posted: Fri Jan 15, 2021 10:54 am
by desertfish
I went through (parts of) the same exploration when trying to figure out how to add floating point support to Prog8. Don't hesitate to ask if you have additional questions!
New demo uploaded: Floating Point in 6502
Posted: Sat Jan 16, 2021 3:12 pm
by gavinhaslehurst
On 1/15/2021 at 10:54 AM, desertfish said:
I went through (parts of) the same exploration when trying to figure out how to add floating point support to Prog8. Don't hesitate to ask if you have additional questions!
So I had a look at Kernal routines, and thought I'd start with FIN to convert a PETSCII string to a floating point number in memory. I used the original FIN address from C64 because I assumed cx16 Kernal is compatible? Is that right? I popped the address of my string in $7a and $7b and jsr'd to $bcf3. But unlike the CHROUT call we all know and love, it did not return me back to the calling routine. And I doing something stupid? Or making a wrong assumption here? Thanks for any help or advice for this noob!
New demo uploaded: Floating Point in 6502
Posted: Sat Jan 16, 2021 4:01 pm
by desertfish
Yeah, the floating point routines are on different addresses in the X16 rom. Also I think not all "internal" basic routines of the C64 basic rom can be used or are even available. I stuck with the exposed routines listed here:
https://github.com/commanderx16/x16-rom/blob/master/fplib/fplib.inc (
translated into Prog8 here with a little bit of description added for each routine)
Unfortunately FIN is listed as ";fin = $fe7b ; XXX TODO" and the routine is not implemented. So converting a string to a floating point value is "Left As An Exercise For The Reader" I think.....
I haven't had to do this myself so far--- all I ever needed was the user inputting an
integer . This can be converted to float using GIVAYF for instance. For floating point constant values in the program, the Prog8
compiler itself is doing the conversion to the 5-byte binary format so the program never sees the string...
New demo uploaded: Floating Point in 6502
Posted: Sat Jan 16, 2021 4:49 pm
by gavinhaslehurst
47 minutes ago, desertfish said:
Yeah, the floating point routines are on different addresses in the X16 rom. Also I think not all "internal" basic routines of the C64 basic rom can be used or are even available. I stuck with the exposed routines listed here:
https://github.com/commanderx16/x16-rom/blob/master/fplib/fplib.inc (
translated into Prog8 here with a little bit of description added for each routine)
Unfortunately FIN is listed as ";fin = $fe7b ; XXX TODO" and the routine is not implemented. So converting a string to a floating point value is "Left As An Exercise For The Reader" I think.....
I haven't had to do this myself so far--- all I ever needed was the user inputting an
integer . This can be converted to float using GIVAYF for instance. For floating point constant values in the program, the Prog8
compiler itself is doing the conversion to the 5-byte binary format so the program never sees the string...
Thanks so much. This is a really useful explanation! I'll let you know how I get on.