vcs_sv_variable_init_issue

记录在vcs编译sv变量遇到的一个问题。

vcs version: 2018.09-SP2-4_Full64

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
program sv();
typedef struct{
logic [31:0] v;
}x_t;
x_t q[$];

function haha();
x_t x;
x.v = 1;
q.push_back(x);
begin
x_t y = q.pop_front();
$display("%0d",y.v); // 打印出来居然是x
end
endfunction

initial begin
haha();
end
endprogram

这段代码打印出来居然是x

把结构体换成类,也是同样的问题。

如果把代码换成这样,就不会有问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
program sv();
typedef struct{
logic [31:0] v;
}x_t;
x_t q[$];

function haha();
x_t x;
x.v = 1;
q.push_back(x);
begin
x_t y;
y = q.pop_front();
$display("%0d",y.v); // 打印出来是1了
end
endfunction

initial begin
haha();
end
endprogram

或者把function放到类里面,也不会有问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
program sv();
class top;
typedef struct{
logic [31:0] v;
}x_t;
x_t q[$];

function haha();
x_t x;
x.v = 1;
q.push_back(x);
begin
x_t y = q.pop_front();
$display("%0d",y.v); // 打印出来是1了
end
endfunction
endclass

initial begin
top t = new();
t.haha();
end
endprogram

奇怪的现象,不知道是不是因为vcs的原因。

———- 20200727 update ————–

应该是因为在program里面,所有成员都是static的,导致x_t y = q.pop_front()在很早就调用了,是在push_back之前调用的,所以结果是x。

在program后面加automatic之后结果就是正确的了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
program automatic sv();
typedef struct{
logic [31:0] v;
}x_t;
x_t q[$];

function haha();
x_t x;
x.v = 1;
q.push_back(x);
begin
x_t y = q.pop_front();
$display("%0d",y.v); // 打印出来居然是x
end
endfunction

initial begin
haha();
end
endprogram