Question about fixed point FIXED type in SGL (after more than ten years)


I'm currently trying to compile the saturn C code of a game (floupix) that I wrote for the Saturn many years ago, this time for more up-to-date architectures.

I want to reuse the code rather than rewrite it for the fine-tweaked physics of the game to be exactly the same as the original.

Most of the code use the SGL (Saturn Graphic Library), for everything, making it very easy to just replace almost all SGL missing parts (mainly display text at a given position and display sprites at given position, scale and rotation, and wait for the screen sync)

Now my problem is that all the SGL API uses a decimal number representation called FIXED.

FIXED is a signed 32 bits fixed point representation (1-8 in SGL developer's manual). The SGL provide math functions like slMulFX, slDivFX, slSquartFX etc. to handle this fixed point format.

So I need to replace theses functions by their exact equivalent for the physics to fully match the original.

My question, for those who read up to this point and would be expert in this precise field : how are the FIXED math operations implemented in SGL, or maybe, on the Saturn hardware ?

Hi vreuzon,

You can take a look at this library on Github: ijacquez/libfixmath. While it does not mention Sega Saturn specifically the author's other projects are Sega Saturn related.

If that fails you can try reverse engineering the functions yourself. Ghidra (an open source RE tool) comes with a free decompiler. It doesn't come with SH-2 support but there is a module for it: VGKintsugi/Ghidra-SegaSaturn-Processor.
I make use of that library. It's better incorporated here.

It's the same idea as FIXED. Except, it's fix16_t.

The idea is that the 16 lower bits are meant as the fractional part of the value, while the upper 16-bits are the integral part. This means that you can make use of non-integer (floats) values without actually using floats.

Here is a tutorial.
Hi all, and thanks for your kind replies. Very nice to see you're still around.

> ijacquez/libfixmath.

Perfect ! The fixed point lib slinga and mrkotfw mentioned is the way to go.

In the meantime, I used a pair of handmade FIXED to double / double to FIXED functions, doing SGL math calculations on doubles and converting back to FIXEDs, resulting in physics being already very close to the original.

What took most of the time was to figure out why my deadly simple custom slDivFX(a,b) was returning wrong results, only to finally remember that the original SGL function was returning b/a and not a/b ! Cool moment when all the garbage of sprites on the screen turned into something meaningful and playable.

Slinga (unforgettable author of "Slinga's 12 Players Snake"), i just read your excellent test report of an early version of floupix today (it was in the source code directory) and remembered where some good ideas in floupix where coming from.

Vbt, I think I can confess I saw today that I did add a GPL license to part of your cd and background image loading lib in the saturn version of floupix source code. Hope you'll forgive me!

I'll post the latest floupix/SDL code somewhere sometime if someone is interested.

Have a nice day.
Last edited: