Jump to content
IGNORED

software floating point / fixed point on 2600


walaber

Recommended Posts

Has anyone tried using software floating-point routines on the 2600? For my motorcycle game I'm thinking about different ways to handle the physics. I can always do something simplified and custom using just integers / some 16-bit math, but if I could get some floating point routines working it would be possible to make the physics pretty realistic.

 

I see there are some 6502 floating point routines here:

http://codebase64.org/doku.php?id=base:floating_point_routines_for_the_6502

 

(along with some other interesting links at the parent page: http://codebase64.org/doku.php?id=base:6502_6510_maths )

 

Has anyone tried these routines on 2600? I assume they consume a lot of cycles... but if I'm only simulating a few point masses it might be acceptible.

 

otherwise does anyone have any recommendations for doing either floating point or fixed point (signed) math on 6502 assembly? In order to do a "proper" physics simulation I'd need the ability to add, subtract, multiply, divide, and sqrt.

 

Other than the link above I can't seem to find any ready-to-use code for this kind of math.

Link to comment
Share on other sites

Thanks SpiceWare, I've already seen that, and I understand the basics of fixed-point math, but can't seem to find actual code for all the math I need. every example I can find shows the extremely simple examples and then says something like "of course remember with twos-complement you have to change this a bit..." etc.

 

I'm really surprised there isn't a link anywhere with basic routines for signed fixed-point 8.8 math with subroutines for ADD, SUB, MUL, DIV, SQRT...

Link to comment
Share on other sites

6502.org has a lot of math routines in their repository. The Batari Basic also has some 8.8 fixed point math routines. I don't think Fred will mind me posting those here as long as it is known it is from the BB kernel.

; Fixed point math routines - created by AtariAge member djmips
; some changes by Fred Quimby

;assignment from 8.8 to 4.4

Assign88to44:

      ; A(4.4) = A,X(8.

       stx temp1
       rol temp1
       asl
       rol temp1
       asl
       rol temp1
       asl
       rol temp1
       asl
       rts

;assignment from 4.4 to 8.8
;

Assign44to88:

      ; A,X(8. = A(4.4)

       sta temp1
       lda #0
       asl temp1
       sbc #0   ;
       eor #$ff ; do sign extend
       rol
       asl temp1
       rol
       asl temp1
       rol
       asl temp1
       rol
       ldx temp1
       rts

 ifconst bankswitch
Assign88to44bs:

      ; A(4.4) = A,X(8.

       stx temp1
       rol temp1
       asl
       rol temp1
       asl
       rol temp1
       asl
       rol temp1
       asl
       RETURN

;assignment from 4.4 to 8.8
;

Assign44to88bs:

      ; A,X(8. = A(4.4)

       sta temp1
       lda #0
       asl temp1
       sbc #0   ;
       eor #$ff ; do sign extend
       rol
       asl temp1
       rol
       asl temp1
       rol
       asl temp1
       rol
       ldx temp1
       RETURN
 endif

;
;Addition/subtraction asm procedures:

;add/sub 8.8 to/from 4.4

Add88to44:

      ; A(4.4) = A,X(8. + Y(4.4)

       jsr Assign88to44
       sty temp1
       clc
       adc temp1
       rts

Sub88from44:

      ; A(4.4) = A,X(8. - Y(4.4)

       jsr Assign88to44
       sty temp1
       sec
       sbc temp1
       rts


Add44to88:

      ; A,X(8. = A,X(8. + Y(4.4)

       sta temp2
       stx temp3
       tya
       jsr Assign44to88
       clc
       sta temp1
       txa
       adc temp3
       tax
       lda temp1
       adc temp2
       rts


Sub44from88:

      ; A,X(8. = A,X(8. - Y(4.4)

       sta temp2
       stx temp3
       tya
       jsr Assign44to88
       sec
       sta temp1
       lda temp3
       stx temp3
       sbc temp3
       tax
       lda temp2
       sbc temp1
       rts

Link to comment
Share on other sites

You can accomplish plenty using variations of the fractional method as previously linked.

 

A problem with FP on 6502 and especially 2600 when doing so using BCD - 6 bytes to represent 10 significant digits + mantissa. OK, you could reduce that to 4 bytes representing 6 digits which would be plenty.

But when doing calculations you're dealing with usually 2 numbers coming in and 1 coming out (usually you'd overwrite one of the incoming operands).

So, the amount of Ram required can ramp up.

 

Also, with BCD you generally need a buffer area so that numbers can be expanded into long form that the exponent in the mantissa represents before calculations can take place.

 

e.g. if doing an add on 0.00001 and 100,000 there you need to have leading zeroes and trailing zeros on the operands and depending on what exponent you limit your numbers to, it can add up to a fair amount of Ram.

Link to comment
Share on other sites

I used floating point numbers for score in Bell Hopper (if you get far enough that is :) ). IIRC they needed 6 bytes for mantissa (12 BCD characters) and one byte for the exponent. It only supported addition and doubling (adding the accumulator to itself).

 

But what everyone else said: fixed point is most likely the way to go unless you need to do something *really* special.

Link to comment
Share on other sites

I don't know if this will be useful, but if you have need to do some unsigned integer division then I made some routines to do specific integers. You can find a copy here:

 

http://forums.nesdev.com/viewtopic.php?f=2&t=11336

 

Also at the end of that thread is a 16 bit divide by 10. There is also some stuff in my blog.

Link to comment
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.
Note: Your post will require moderator approval before it will be visible.

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.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...