0 votes
by Roger Levy (120 points)


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



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 (700 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]

IMHO the layout reduces line noise.