68k signed division problems (DIVS instruction)
Posted: Fri Feb 28, 2014 6:10 pm
Hello everyone. I haven't been active here for.. a long time. I just started working on a small Sega CD program (Yes, this is the Genesis forum but the problem I'm about to talk about has to do with the main M68K processor and nothing involving SCD hardware)
So I've created a simple system for showing animated sprites I have an array-like structure with each entry containing fixed point position,velocity and acceleration values. (lowest 6 bits are the fractional part)
I have this sprite that acts like a bouncing ball. It has a constant positive vertical acceleration value so it accelerates downwards (unless A,B,C are being pressed, in which case it accelerates upwards).
So, to make this sprite bounce I set the velocity this way every time the sprite touches a lower limit and then I change its sign:
VerticalAcc=VerticalAcc *4/3
In 68K asm and assuming d3 is the vertical acceleration (again, a 16 bit value whose lower 6 bits correspond to the fractional part)
While stepping with Gens debugger I noticed that the signed division of a negative value produces an invalid result
Let's say d3 is -7 ($FFF9 in two's complement)
doing
for some reason, stores into the d3 register $00017FFC (highest two bytes are the remainder and lowest two bytes are the quotient), which is funny because DIVU yields the same result.
According to the 68k reference the result should be $FFFFFFFD (remainder -1,quotient -3)
So I've created a simple system for showing animated sprites I have an array-like structure with each entry containing fixed point position,velocity and acceleration values. (lowest 6 bits are the fractional part)
I have this sprite that acts like a bouncing ball. It has a constant positive vertical acceleration value so it accelerates downwards (unless A,B,C are being pressed, in which case it accelerates upwards).
So, to make this sprite bounce I set the velocity this way every time the sprite touches a lower limit and then I change its sign:
VerticalAcc=VerticalAcc *4/3
In 68K asm and assuming d3 is the vertical acceleration (again, a 16 bit value whose lower 6 bits correspond to the fractional part)
Code: Select all
divs.w #4,d3
muls.w #3,d3
neg d3
Let's say d3 is -7 ($FFF9 in two's complement)
doing
Code: Select all
move.w #-7,d3
divs.w #2,d3
According to the 68k reference the result should be $FFFFFFFD (remainder -1,quotient -3)
So yeah that's pretty much it? What could it be? Both Gens and Fusion produce the same result.. I couldn't test it on real hardware.For DIVS, the sign of the remainder is always the same
as the sign of the dividend (unless the remainder is zero).