0 votes
by Roger Levy (220 points)

Hi!

I'm working on a series of game dev tutorials for VFX.

The code relies on a well-known gaming library, Allegro 5.

The (not show-stopping but still significant) challenge is that when using ndp387.fth, we're limited to 8 floats, but the Allegro API has many functions that take 8 or more floats and the FFI doesn't support hfp387.fth.

I talked with Stephen and he guided me to Extern.fth, where I made the following change:

: CopyFLOAT \ ^ai --

\ Compile code to copy FTOS to the frame as a float or double

  [ FPSystem 1 = [if]   \ external float stack

    ] [a    \ pop float from external float stack into the FPU

      mov   eax, FSP-OFFSET [esi]   \ get FP stack pointer

      fld   fword 0 [eax]           \ push FNOS to FPU

      lea   eax, FPCELL [eax]       \ update stack pointer

      mov   FSP-OFFSET [esi], eax

    a] [a] [            

  [then] ]

  case  dup ai.size @

    4 of  >r  [a fstp float a]      r> ai.FrameOff @ [a [esp] a] [a]  endof

    8 of  >r  [a fstp double a]     r> ai.FrameOff @ [a [esp] a] [a]  endof

    10 of >r  [a fstp extended a]   r> ai.FrameOff @ [a [esp] a] [a]  endof

       err_CGsize throw

  endcase

;

I've verified that the extra code - copy-pasted directly from the definition of >FPU in hfp387.fth - is generated in calls to foreign functions taking floats.  But I'm getting an access violation.

The repository is here -> https://github.com/RogerLevy/vfxland

The modified Extern.fth is included.  The test is in test7_tilemap_hfp.f.

Any help is appreciated!

(Btw, an official way to format code might be a good addition to the forums)

1 Answer

0 votes
by Stephen Pelc (4.2k points)

One of the confusing parts of the HFP package is that FTOS is cached in ST0. Thus the frame copy has to be done before FTOS is reloaded from the external stack. So the [IF] block goes after the frame copy. 

There's a copy of the MPE code layout standard in my book Programming Forth available from the books page. I could reformat the chapter in PDF or HTML. On this topic, note that [IF] and [THEN] are immediate in MPE land. This means that the conditionals can be reduced to:

[ FPsystem 1 = ] [if]
...
[then]

IMHO the layout reduces line noise.

by Roger Levy (220 points)
I can't believe I didn't see this reply until now.  I don't think I got a notification.  

Anyway I was referring more to formatting code with monospace font etc in posts rather than coding conventions.  I had to "hack" it.  

Winter's almost here, so I suppose I'll have some extra time to take another stab at this.
by Stephen Pelc (4.2k points)
Posts here can be formatted using Markdown. Some responses (but not this one) give you a menu of formatting tools.

The notification system is not fully functional. Our apologies, we will fix it, but between the CI system and 64-bit conversions we are just overloaded.
by Roger Levy (220 points)
Well, resurrecting this thread after 9 months.  

I think the best way to go now is to support passing floats to *some* foreign functions using the data stack.  And add a word for copying a float to the datastack (without conversion), if there isn't one already - as a single or double.  This would preserve the performance benefits of the fp stack while providing a way to call these complicated functions.
...