Jump to content

Recommended Posts

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/


 

  • Like 1

Share this post


Link to post
Share on other sites

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?

  • Like 1

Share this post


Link to post
Share on other sites

It looks like it is BCD-to-float conversion, based on work originally from Compute! magazine?

 

  • Like 1

Share this post


Link to post
Share on other sites

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!  

Share this post


Link to post
Share on other sites

You could also try to use the basic/kernal floating point routines such as FOUT $fe81 to print the floating point values

  • Thanks 1

Share this post


Link to post
Share on other sites
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! 

  • Like 1

Share this post


Link to post
Share on other sites

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!

  • Thanks 1

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

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...

 

 

Edited by desertfish
  • Thanks 1

Share this post


Link to post
Share on other sites
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.  

Share this post


Link to post
Share on other sites
18 hours 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...

 

 

Update: absolutely brilliant! Thanks for the pointers here. Managed to get my Julia Set demo working in assembler. Super chuffed! Finally feel like I've got my head round it. 

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Please review our Terms of Use