2014-10-10 46 views
0

我制作了两种形式的数据模式,并希望以错误计数的形式进行比较.....当两种模式不相等时,错误计数应该很高。 ...我做了包括测试台的代码,但是当我运行行为总结时,错误计数只在值0高而不在值1 .....我期望它在0和1都很高.. ..please帮我在这,因为我是新使用VerilogVerilog中两种不同模式的错误计数

这里是代码

`timescale 1ns/1ps 

module pattern(
clk, 
start, 
rst,enter code here 
clear, 
data_in1, 
data_in2, 
error 
); 

input [1:0] data_in1; 
input [1:0] data_in2; 

input clk; 
input start; 
input rst; 
input clear; 

output [1:0] error; 
reg [1:0] comp_out; 
reg [1:0] i = 0; 

assign error = comp_out; 

[email protected](posedge clk) 
begin 
comp_out = 0; 
if(rst) 
    comp_out = 0; 
    else 
    begin 
     for(i = 0; i < 2; i = i + 1) 
     begin  
      if(data_in1[i] != data_in2[i]) 
       comp_out <= comp_out + 1; 
     end 
    end 
end 

endmodule 

here is the test bench for the above code 
`timescale 1ns/1ps 

module tb_pattern(); 
// inputs 
reg clk; 
reg rst; 
reg [1:0] data_in1; 
reg [1:0] data_in2; 
wire [1:0] error; 
//outputs 
//wire [15:0] count; 

//instantiate the unit under test (UUT) 
pattern uut (
    // .count(count), 
    .clk(clk), 
    .start(start), 
    .rst(rst), 
    .clear(clear), 
    .data_in1(data_in1), 
    .data_in2(data_in2), 
    .error(error) 

); 
initial begin 
     clk = 1'b0; 
     rst = 1'b1; 
     repeat(4) #10 clk = ~clk; 
     rst = 1'b0; 
     forever #10 clk = ~clk; // generate a clock 
    end 
initial begin 
    //initialize inputs 
    clk = 0; 
    //rst = 1; 
    data_in1 = 2'b00; 
    data_in2 = 2'b01; 
    #100 
    data_in1 = 2'b11; 
    data_in2 = 2'b00; 
    #100 
    $finish; 
    end 
    //force rest after delay 
    //#20 rst = 0; 
    //#25 rst = 1; 


endmodule 
+0

'error'是一个2位信号。什么意思是“高”? – toolic 2014-10-10 21:04:51

回答

2

在for循环中递增时,您需要使用阻塞赋值(=),但是在分配触发器时,应该使用非阻塞赋值(<=)。当您需要使用for循环分配触发器时,最好将组合功能和同步功能拆分为独立的总是功能块。

... 
reg [1:0] comp_out, next_comb_out; 

always @* begin : comb 
    next_comp_out = 0; 
    for (i = 0; i < 2; i = i + 1) begin 
    if (data_in1[i] != data_in2[i]) begin 
     next_comp_out = next_comp_out + 1; 
    end 
    end 
end 

always @(posedge clk) begin : dff 
    if (rst) begin 
    comb_out <= 1'b0; 
    end 
    else begin 
    comb_out <= next_comp_out; 
    end 
end 
... 
2
begin 
    for(i = 0; i < 2; i = i + 1) 
    begin  
     if(data_in1[i] != data_in2[i]) 
      comp_out <= comp_out + 1; 
    end 
end 

此for循环不WO rk你认为它的方式。因为这是一个非阻塞赋值,所以实际上只应用循环的最后一次迭代。所以只有最后一点实际上在这里进行比较。

如果你的数据不匹配的两个位,则循环解开的东西,看起来像这样:

comp_out <= comp_out + 1; 
comp_out <= comp_out + 1; 

因为这是非阻塞,方程的RHS都在同一时间进行评估,留给你:

comp_out <= 0 + 1; 
comp_out <= 0 + 1; 

因此,即使你试图用这个作为一个柜台,只有最后一行生效,你得到的“1”不匹配计数,无论有多少位不匹配。

尝试使用阻塞语句(=)代替comp_out分配。