想在模块中编写一个task便于其它模块调用,在task中要实现对模块中的寄存器变量的写操作,我编写的task没有输出,而是对模块中的寄存器变量直接赋值,编译和综合都可以通过,可是时序仿真时,该寄存器变量的输出为不定状态。请问在task中可以对全局变量操作吗?代码如下。
其中,bitstream_codelen,bitstream_bitbufs,bitstream_bitpos为全局变量,task要实现对其读写,这个程序是用来测试的,结果是bitstream_codelen和bitstream_bitpos输出均为x,这是哪的问题,是task不能对模块中的变量操作吗?小弟初学,求高手指点
module writetobitstream(value,len,bitstream_bitpos,bitstream_codelen,flag);
input [31:0] value,len;
output [31:0] bitstream_bitpos,bitstream_codelen;
output [1:0] flag;
reg [31:0] bitstream_codelen;
reg [31:0] bitstream_bitbufs[3:0];
reg [31:0] bitstream_bitpos;
reg [31:0] value_tmp,len_tmp;
reg [1:0] flag;
reg [4:0] num;
reg [31:0] bitpos_tmp,bitcodelen_tmp;
parameter
INITIAL = 0,
RUN = 1;
always @(value or flag)
begin
case(flag)
INITIAL:
begin
bitpos_tmp = 31;
bitcodelen_tmp = 0;
for(num = 0;num < 4;num = num + 1)
bitstream_bitbufs[num] = 0;
flag = RUN;
end
RUN:
begin
value_tmp = value;
len_tmp = len;
writebits(len_tmp,value_tmp);
bitstream_bitpos = bitpos_tmp;
bitstream_codelen = bitcodelen_tmp;
flag = RUN;
end
default:
flag = INITIAL;
endcase
end
task writebits;
input [31:0] code_length,code_value;
reg [31:0] left_bits;
if(code_length < (bitpos_tmp + 1))
begin
bitstream_bitbufs[bitcodelen_tmp] = bitstream_bitbufs[bitcodelen_tmp] | (code_value << (bitpos_tmp + 32'b1 - code_length));
bitpos_tmp = bitpos_tmp - code_length;
end
else
begin
left_bits = code_length - bitpos_tmp - 1;
bitstream_bitbufs[bitcodelen_tmp] = bitstream_bitbufs[bitcodelen_tmp] | (code_value >> left_bits);
bitcodelen_tmp = bitcodelen_tmp + 1;
bitstream_bitbufs[bitcodelen_tmp] = code_value << (32 - left_bits);
bitpos_tmp = 31 - left_bits;
end
endtask
endmodule
------解决方案--------------------------------------------------------
试了试如果改成非阻塞赋值的话,得到的输出为0,不是正确结果,求高手指点啊。。。