Subject: [sv-cc] Re: Calling SV from C
From: Andrzej Litwiniuk (Andrzej.Litwiniuk@synopsys.com)
Date: Thu Feb 06 2003 - 09:08:36 PST
> >I suppose that the export declaration will only provide the function name
> >and the hierarchical path. The path will identify both the unique instance and
> >the unique function declaration. There is no need to repeat the
> >specification of formal arguments and function result.
>
> Francoise:
> I thought it would have been useful to include the prototype in the export
> declaration if
> SV compilers intend to generate automatically the extern C declaration. But
> I don't think it is possible as the function declaration can involve
> parameters which are defined by the
> module definition.
Right. But once the parameter resolution is completed, the meaning of all types
is known for the function and the instance specified in the export declaration.
Therefore the compiler will be able to generate automatically the extern C
declaration.
> > SV portion may look like:
> >...
> >export a.b.c.myfunc; // whatever is the current syntax ...
> >...
> > function bit [15:0] myfunc (input int address, output logic [31:0] r,
> > input T complex);
> >
> >the extern function declaration which will be used would be:
> >
> >extern svBitVec32 myfunc (int address, svLogicPackedArrRef r, const T
> >*complex);
> >
> > > r is passed by reference
> >Right, svLogicPackedArrRef is a reference (actually typedefed to void *);
> >note that the representation is implementation-dependent.
>
> Francoise: Why can't I use svLogicVec32* for r?
svLogicVec32 is the canonical representation.
Simulator is free to use any representation for packed arrays.
It assumes that an output argument is passed by reference and the reference
will actually point to data object in the representation used by the simulator.
svLogicPackedArrRef is such an opaque reference to the simulator's
representation.
User is responsible for allocating the data object to be passed (as an output
arg) to SV function.
Let's illustrate this on a simplified example, with void function and one
argument.
SV:
export a.b.c.myfunc;
...
function void myfunc (output logic [31:0] r); ...
...
C:
#include "svc.h"
extern void myfunc (svLogicPackedArrRef r); /* exported from SV */
/* output logic packed 32-bits */
solution 1, dynamic allocation:
...
svLogicVec32 my_r[sv_CANONICAL_SIZE(32)]; /* my array, canonical representation */
/* allocate memory for logic packed 32-bits in simulator's representation */
svLogicPackedArrRef r =
(svLogicPackedArrRef)malloc(svSizeOfLogicPackedArr(32));
myfunc(r);
/* canonical <-- actual */
svGetLogicVec32(my_r, r, 32);
/* will use only the canonical representation from now on */
free(r); /* don't need any more */
...
solution 2, static allocation, needs recompilation:
#include "svc_src.h" /* needed for sv_LOGIC_PACKED_ARRAY */
...
svLogicVec32 my_r[sv_CANONICAL_SIZE(32)]; /* my array, canonical representation */
/* static memory for logic packed 32-bits in simulator's representation */
sv_LOGIC_PACKED_ARRAY(32, r); /* simulator's representation */
myfunc(r);
/* canonical <-- actual */
svGetLogicVec32(my_r, r, 32);
/* will use only the canonical representation from now on */
...
> Francoise: How do I know in my C code how big is the svLogic*?
> Do I have to rely on the SV declaration?
It's your problem!
The following three solutions come to mind:
1. The size of the (packed) array passed over the interface is known
to the both sides of interface and hence both the SV programmer and
the C programmer know the same size; they rely on the SV declaration.
The size is hard-coded, see my examples of C code above.
(Well, actually I would have rather written something like this:
#define WIDTH_IN_SV 32
...
svLogicVec32 my_r[sv_CANONICAL_SIZE(WIDTH_IN_SV)];
...
svGetLogicVec32(my_r, r, WIDTH_IN_SV);
)
2. An additional argument may be added to the function.
Then the SV programmer will be responsible for passing the correct size.
3. An open array is used for passing a packed array.
In this case the implementation will take care of providing
the size of the actual argument. The C programmer will call array querying
functions to get the size (svLength, for example).
Trade-offs as often: maintainability, convenience, performance.
Andrzej
This archive was generated by hypermail 2b28 : Thu Feb 06 2003 - 09:09:13 PST