Using a C library from BlitzMax ...

BlitzMax Forums/BlitzMax Programming/Using a C library from BlitzMax ...

mic_pringle(Posted 2010) [#1]
Hi,

I would like to use a C library from BlitzMax. I can pretty much figure out everything I need to do except one thing. The library I'm wanting to use requires the end user to code the implementation of a couple of functions themselves. That is, the function signatures are defined in the header file, but the implementation is left up to the end user because they are designed to do bespoke things dependant on the project they are being used in.

For example, a couple of the function signatures look like this
void WrZ80(register word Addr,register byte Value);
byte RdZ80(register word Addr);
My issue is that I have two questions around the implementation of these functions ...

1 - Can I implement these functions in C, but have the implementation actually call a BlitzMax function I define, and if so how ?

2 - Can I define these functions directly in BlitzMax and if so, how ?

Thanks

Mic


Otus(Posted 2010) [#2]
1. To call a Blitz function from C, you must add bb_ as prefix to the declaration in C source. E.g:
Function Blah:Int()
Becomes
int bb_Blah();

2. If they need to have predefined names, I don't think that is possible.


Brucey(Posted 2010) [#3]
If they need to have predefined names, I don't think that is possible.

Then simply wrap a call to the bmx function from an appropriately named C function.


Otus(Posted 2010) [#4]
Which is #1 :)


mic_pringle(Posted 2010) [#5]
@Otus,

Thanks for the response.

So if I've understood correctly, I could define the function in C as required, but then define in BlitzMax a function, and call that as the implementation as long as it was prefixed with bb_ ?

i.e
In C

void WrZ80(register word Addr,register byte Value)
{
    bb_WriteZ80();
}

In BlitzMax

Function WriteZ80:Int()
    'Actual implementation goes here
End Function

If this is correct, then I have one final question. How do I pass the variables that are passed into the original C function to the BlitzMax one ? (as you can see I've left them out above, but will need to pass them in the actual implementation)

Thanks

-Mic


Brucey(Posted 2010) [#6]
some possible C :
extern int bb_RdZ80(int addr);

byte RdZ80(register word Addr) {
    return (byte)bb_RdZ80((int)Addr);
}


Be wary of passing bytes around.


Otus(Posted 2010) [#7]
Yes, that is correct. Something like this should work. BMX:
Function WriteZ80(addr:Int, value:Int)
    'Actual implementation goes here
End Function

And in C:
void bb_WriteZ80(int addr, int value);

void WrZ80(register word Addr,register byte Value)
{
    bb_WriteZ80((int)Addr, (int)Value);
}

Note: I'm not 100% sure the arguments cast OK, but you should get it by trial and error if not otherwise.

Edit: Brucey beat me to it. You can pass bytes, shorts etc. around, if you want to, but using Ints where possible is simplest.


mic_pringle(Posted 2010) [#8]
Hi Brucey,

Thanks for this - would be so kind as to show the BlitzMax version of the above that returns the byte ?

Also, what should I be wary of ?

The utilmate functionality of the two functions above is to read or write a single byte from an array in BlitzMax, passing it to or receiving it from C.


mic_pringle(Posted 2010) [#9]
@Otus

Thanks again, that has made it much clearer.