在system Verilog中function automatic logic [15:0] CalcCrcDAT(input [15:0] crc, input inbit); return {crc[14:0],crc[15]^inbit} ^ {3'b0,crc[15]^inbit,6'b0,crc[15]^inbit,5'b0}; endfunction task cmd_53_wr_dat_gen; input [9:0] num; reg [7:0] wr_data; reg dat_part_select; integer k; reg [15:0] crc [3:0]; begin @(negedge clk); dat_en <= 1'b1; dat_d <= 4'h0; wr_data<= 8'd1; dat_part_select <= 1'b0; for(int j = 0; j < 4; j++) crc[j] <= CalcCrcDAT(crc[j],1'b0); for (k=0;k<num * 2;k=k+1) begin @(negedge clk); dat_en <= 1'b1; dat_part_select <= ~dat_part_select; if(dat_part_select) begin dat_d <= wr_data[7:4]; for(int j = 0; j < 4; j++) crc[j] <= CalcCrcDAT(crc[j],wr_data[j+4]); end else begin dat_d <= wr_data[3:0]; wr_data <= wr_data + 1'b1; for(int j = 0; j < 4; j++) crc[j] <= CalcCrcDAT(crc[j],wr_data[j+4]); end end @(negedge clk); dat_en <= 1'b1; dat_d <= 4'hf; @(negedge clk); dat_en <= 1'b0; dat_d <= 4'hf; end endtask中为什么crc一直为不定态
时间: 2024-04-02 09:36:18 浏览: 94
根据代码,可以看到在task `cmd_53_wr_dat_gen` 中,`crc` 是一个数组,但是在初始化时并没有对其进行赋值。因此,最初 `crc` 数组的值是不确定的。
在代码的循环中,使用了 `CalcCrcDAT` 函数对 `crc` 进行更新,但是由于 `crc` 最初的值不确定,因此在更新时可能会出现错误,导致 `crc` 的值一直是不确定的。
为了解决这个问题,可以在 `cmd_53_wr_dat_gen` 中对 `crc` 数组进行初始化,或者在声明 `crc` 时给定初始值。
相关问题
system Verilog中 代码reg dat_en = 0; reg [3:0] dat_d ; reg [15:0] crc_cal [3:0]; initial begin for(int j = 0; j < 4; j++) crc_cal[j] <= 16'd0; end function automatic logic [15:0] CalcCrcDAT(input [15:0] crc, input inbit); return {crc[14:0],crc[15]^inbit} ^ {3'b0,crc[15]^inbit,6'b0,crc[15]^inbit,5'b0}; endfunction task cmd_53_wr_dat_gen; input [9:0] num; reg [7:0] wr_data; reg dat_part_select; integer k; begin /* @(negedge clk); for(int j = 0; j < 4; j++) crc_cal[j] <= 16'd0; */ @(negedge clk); dat_en <= 1'b1; dat_d <= 4'h0; wr_data<= 8'd1; dat_part_select <= 1'b0; for(int j = 0; j < 4; j++) crc_cal[j] <= CalcCrcDAT(crc_cal[j],1'b0); for (k=0;k<num * 2;k=k+1) begin @(negedge clk); dat_en <= 1'b1; dat_part_select <= ~dat_part_select; if(dat_part_select) begin dat_d <= wr_data[7:4]; for(int j = 0; j < 4; j++) crc_cal[j] <= CalcCrcDAT(crc_cal[j],wr_data[j+4]); end else begin dat_d <= wr_data[3:0]; wr_data <= wr_data + 1'b1; for(int j = 0; j < 4; j++) crc_cal[j] <= CalcCrcDAT(crc_cal[j],wr_data[j+4]); end end @(negedge clk); dat_en <= 1'b1; dat_d <= 4'hf; @(negedge clk); dat_en <= 1'b0; dat_d <= 4'hf; end endtask中为什么crc一直为为0
在这段代码中,`crc_cal` 是一个数组,并且在初始时,使用了如下代码将其所有元素初始化为 0:
```
initial begin
for(int j = 0; j < 4; j++)
crc_cal[j] <= 16'd0;
end
```
在 `cmd_53_wr_dat_gen` 的循环中,使用了 `CalcCrcDAT` 函数对 `crc_cal` 数组进行更新,因此在每次循环后,`crc_cal` 数组应该会被更新为新的值。
如果 `crc_cal` 数组一直为 0,那么可能是因为在循环中,没有正确调用 `CalcCrcDAT` 函数来更新数组的值。你可以检查一下代码中 `CalcCrcDAT` 函数的实现是否正确,并确保在循环中正确调用了该函数。此外,你也可以在代码中增加一些调试语句,例如打印出 `crc_cal` 数组的值,来帮助你定位问题所在。
//XW_crc_p.v `pragma protect begin module DW_crc_p( data_in, crc_in, crc_ok, crc_out ); parameter integer data_width = 16; parameter integer poly_size = 16; parameter integer crc_cfg = 7; parameter integer bit_order = 3; parameter integer poly_coef0 = 4129; parameter integer poly_coef1 = 0; parameter integer poly_coef2 = 0; parameter integer poly_coef3 = 0; input [data_width-1:0] data_in; input [poly_size-1:0] crc_in; output crc_ok; output [poly_size-1:0] crc_out; `define DW_max_data_crc_1 (data_width>poly_size?data_width:poly_size) wire [poly_size-1:0] crc_in_inv; wire [poly_size-1:0] crc_reg; wire [poly_size-1:0] crc_out_inv; wire [poly_size-1:0] crc_chk_crc_in; reg [poly_size-1:0] crc_inv_alt; reg [poly_size-1:0] crc_polynomial; `include "bit_order_crc_function.inc" `include "bit_order_data_function.inc" `include "calculate_crc_w_in_function.inc" `include "calculate_crc_function.inc" `include "calculate_crc_crc_function.inc" generate //begin genvar bit_idx; reg [63:0] crc_polynomial64; reg [15:0] coef0; reg [15:0] coef1; reg [15:0] coef2; reg [15:0] coef3; assign coef0= poly_coef0; assign coef0= poly_coef1; assign coef0= poly_coef2; assign coef0= poly_coef3; assign crc_polynomial64 = {coef3, coef2, coef1, coef0}; assign crc_pollynomial = crc_polynomial64[poly_size-1:0]; case(crc_cfg/2) 0: assign crc_inv_alt = {poly_size{1'b0}}; 1: for(bit_idx = 0; bit_idx<poly_sizel bit_idx=bit_idx+1) assign crc_inv_alt[bit_idx] = (bit_idx % 2)? 1'b0:1'b1; 2: for(bit_idx=0; bit_idx<poly_size; bit_idx=bit_idx+1) assign crc_inv_alt[bit_idx] = (bit_idx % 2)?1'b1:1'b0; 3: assign crc_inv_alt = { poly_size{1'b1}}; endcase endgenerate assign crc_in_inv = bit_order_crc(crc_in) ^ crc_inv_alt; assign crc_reg = calculate_crc(bit_order_data(data_in)); assign crc_out_inv = crc_reg; assign crc_out = bit_order_crc(crc_out_inv)^ crc_inv_alt; assign crc_chk_crc_in = calculate_crc_crc(crc_reg, crc_in_inv); assign crc_ok = (crc_chk_crc_in ==0); `undef DW_max_data_crc_1 endmodule `pragma protect end can you write a testbench for this piece of CRC verilog code so that this verilog file and the testbench can be used togerther by vcs to verify the correctness of this verilog file?
Sure, here's a basic testbench for the `DW_crc_p` module:
```
`timescale 1ns/1ps
module testbench;
// Inputs
reg [15:0] data_in;
reg [15:0] crc_in;
// Outputs
wire crc_ok;
wire [15:0] crc_out;
// Instantiate the DUT
DW_crc_p dut (
.data_in(data_in),
.crc_in(crc_in),
.crc_ok(crc_ok),
.crc_out(crc_out)
);
initial begin
// Initialize inputs
data_in = 16'h1234;
crc_in = 16'h5678;
// Wait for a few cycles
#10;
// Change inputs
data_in = 16'h5678;
crc_in = 16'h1234;
// Wait for a few cycles
#10;
// Add more test cases as needed
// Check outputs
$display("crc_ok = %b, crc_out = %h", crc_ok, crc_out);
end
endmodule
```
This testbench simply initializes the `data_in` and `crc_in` inputs, waits for a few cycles, changes the inputs, waits for a few more cycles, and then checks the `crc_ok` and `crc_out` outputs using the `$display` system task. You can add more test cases as needed by changing the input values and checking the output values. To run the testbench with the `DW_crc_p` module, you can use the following command:
```
vcs -debug testbench.v DW_crc_p.v
```
This will compile both the testbench and the `DW_crc_p` module, and then run the simulation. You can view the simulation results using the `dve` GUI or by examining the output in the terminal window.
阅读全文