Hi,
Can someone help to solve a issue that keeping multiple individual copies for static variable in C through DPI-C calls?
Basically, for static variable is stored in the statically allocated memory, any method try to modify the copy will reflect in the memory. Is there a way to allow user to maintain multiple copies for static variable?
In the following code, how to keep static variable - miles as is, and function runner1 modify one copy, function runner2 modify another copy? The intention is trying to reuse the C code as much as possible(not to change is the best), and solve this issue.
Here is the code:
In C side:
test.h
static int miles;
extern int get_miles();
test.c
#include "test.h"
int get_miles() {
miles = miles + 1;
return miles;
}
user.c
#include <stdio.h>
#include "test.h"
extern void runner1();
extern void runner2();
void runner1() {
int i;
for(i=0;i<5;i++) {
printf("runner1 runs %0d miles\n", get_miles());
}
}
void runner2() {
int j;
for(j=0;j<5;j++) {
printf("runner2 runs %0d miles\n", get_miles());
}
}
In SV side:
module tb;
import "DPI-C" context function void runner1();
import "DPI-C" context function void runner2();
initial begin
fork
runner1();
runner2();
join
$finish();
end
endmodule