Subject: RE: [sv-cc] Another directC C mapping question
From: Michael McNamara (mac@verisity.com)
Date: Mon Feb 03 2003 - 10:33:11 PST
Francoise Martinolle writes:
 > In the case of a negative integer on the Verilog side, it would be
 > declared as a signed int but when it is passed to C it is
 > transformed into an unsigned int, therefore the value seen by the C
 > code would be unsigned.  If the C code needs to know it needs to be
 > interpreted as a signed value how it would know if we transform any
 > signed value to an unsigned value on the C side?
 > 
 > For example, if in Verilog I declare a variable c:
 > signed char c = -1;
 > extern myCfunc(c); //C function which takes c as a parameter
 > 
 > On the C side the parameter c is seen as unsigned char I believe.
 > 
 > void myCfunc(unsigned char c)
 > {
 >    int i;
 > 
 >    i = c;
 >    printf("c = %d\n", i);  //This will print 255 instead of -1
 > 
 > }
 > 
 > This is a general issue of how do we pass the signedness to the C side?
 > 
 > Francoise
This problem exists in general in C and other languages.  The bit
pattern sent over is 8'b1111_1111.  If the c rountine is coded to expect a
sign character, it will see this as -1.  If the c rountine is codes to
expect an unsigned character it will see this as 255.
Unless a complete declaration of the C function is presented to the
Verilog compiler:
signed char c = -1;
extern myCfunc(unsigned char c); //C function which expects unsigned char 
there is no way for either side to even know there is a problem.
Of course, even in this case, all one can do is issue a warning:
Warning: foo.v line 86: type mismatch, argumnet 1: passing signed char to routine expecting unsigned
Even then, unless the exact same external declaration is by both the C
compiler and the Verilog compiler, one still risks partial updates
hiding bugs:
% cat myCfunc.ext
extern myCfunc(signed char c); 
% cat myCfunc.c:
#include "/myProject/include/myCfinc.ext"
void myCfunc(unsigned char c) {
...
% cat myverilog.v
...
  signed char c = -1;
`include "/myProject/include/myCfinc.ext"
...
 
This archive was generated by hypermail 2b28 : Mon Feb 03 2003 - 10:33:45 PST