[futurebasic] Re: simple INCLUDE question

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : March 1998 : Group Archive : Group : All Groups

From: Paul Bruneau <paul_bruneau@...>
Date: Tue, 17 Mar 1998 10:58:11 -0500
Terence Jordan wrote:

> I've got some code right now that I'd like to separate from the MAIN into
> an INCLUDE (or two or more includes if necessary).
>
> The only problem I seem to be running into is that I have to SEGMENT these
> statements every once in a while, and the SEGMENT statement is not allowed
> in an include...
>

You put the segment instructions in between your include instructions (in the
Main file). This makes each include have its own 32k segment.

> Now, what else comes into play is that some of the later functions call the
> beginning functions. Separating this into separate INCL's, they never see
> each other; they never interact, right?

True, until you set them up to know where they are...see below

> Yes, this is for that 6502 emulator I just did. For example, separating the
> addressing modes from the instructions would probably have to happen, but
> then these would be in different includes...! Separating them produces an
> error saying that the functions don't exist...
>
> What can I do?

Well, it's kinda complicated until you've fixed bugs caused by doing it wrong
about 100 times (which won't take long!).

What you have to do is tell the calling function just where the heck is this
other function in memory.

So....

Make a global long that will hold the address of the function to be called:
dim gMyFunction&

Then from your Main, you have to call the following (which is typically the
last function in the include):

fn globalizeMyFunctions

You call this from Main because Main knows where everything is.

Here is what globalizeMyFunctions does. Remember that it's the last function
in the include.

Local FN globalizeMyFunctions
  gMyFunction& = @FN myFunction
end fn

This thing puts the address of myFunction into the long integer. Now that the
address of the function is stored, the last step is to tell EACH INCLUDE FILE
THAT NEEDS THAT FUNCTION where the thing is. Here's how you do it. Put this
line at the top of EACH INCLUDE that needs to call myFunction. (I put it after
the Globals statement) Remember, Main knows where everything is, and doesn't
need this:

DEF FN myFunction(var1%,var2&,varEtcEtcEtc$) USING gMyFunction&

**Note the paramater list is necessary here.

This says to that include: "Hey, when you want to know where myFunction is,
use the contents of gMyFunction& to find out!"

That should cover it. If ANY of the above are messed up, welcome home to
Macsbug! (If you're lucky)

This procedure took me hours to figure out, so you can count on it taking at
least half that ; ).

Good luck, and someone flame me if I got it wrong!

PB