【问题标题】:8 bit comparator from 4 bit comparator - undefined outputs4 位比较器的 8 位比较器 - 未定义的输出
【发布时间】:2023-03-24 05:50:02
【问题描述】:

我有以下代码可以正确比较数字等于或大于彼此时的数字。但是,它不会产生小于输出。它总是以未定义的形式弹出。

我在这里错过了什么?

module FourBitComparator (input [3:0]a, 
                          input [3:0]b, 
                          output eq, 
                          output gt, 
                          output lt);
    assign eq = a == b;
    assign gt = a > b;
    assign lt = a < b;

endmodule


module EightBitComparator(input [7:0]a,
                          input [7:0]b,
                          output eq,
                          output gt,
                          output lt);
    wire [3:0]a1;
    wire [3:0]a2;
    wire [3:0]b1;
    wire [3:0]b2;
    assign a1 = {a[3:0]};
    assign a2 = {a[7:4]};
    assign b1 = {b[3:0]};
    assign b2 = {b[7:4]};
    FourBitComparator BC_2( a2, b2, eq, gt, lt);
    FourBitComparator BC_1( a1, b1, eq, gt, lt);
endmodule

测试台

module EightBitCompTB;

    // Variables
    reg [7:0] a, b;
    wire eq, gt, lt;

    // Call comaparator
    EightBitComparator BC_1(a, b, eq, gt, lt);

    // Test inputs
    initial begin
        $monitor("%d a=%b, b=%b, eq=%b, gt=%b, lt=%b",
                 $time,
                 a, b, eq, gt, lt);
        #10 
        a = 15;
        b = 15;
        #10 
        a = 255;
        b = 0;
        #10
        a = 74;
        b = 80;
        #10
        a = 65;
        b = 50;
    end

endmodule

【问题讨论】:

    标签: undefined verilog comparator test-bench


    【解决方案1】:

    您存在争用:两个驱动程序正在驱动相同的信号。在EightBitComparator 中,两个FourBitComparator lt 输出驱动相同的lt 信号。当 BC_1.lt=0 和 BC_2.lt=1 时,或反之亦然,您会得到一个 x(未知)。一个好的调试工具可以为您检测到这种情况。

    gt也是如此。

    你需要重新设计你的逻辑。

    为什么不能简单化?

    module EightBitComparator(input [7:0]a,
                              input [7:0]b,
                              output eq,
                              output gt,
                              output lt);
        assign eq = a == b;
        assign gt = a > b;
        assign lt = a < b;
    endmodule
    

    【讨论】:

      【解决方案2】:

      这是一个很好的解决方案:

      module Comparator8Bit(
                      input[7:0] a,
                      input[7:0] b,
                      output eq,
                      output lt,
                      output gt);
      
              wire[3:0] a1, a2, b1, b2;
              wire eq1, eq2, lt1, lt2, gt1, gt2;
      
              assign a1 = {a[3:0]};
              assign a2 = {a[7:4]};
              assign b1 = {b[3:0]};
              assign b2 = {b[7:4]};
      
              Comparator4Bit BC_1(a1, b1, eq1, lt1, gt1); 
              Comparator4Bit BC_2(a2, b2, eq2, lt2, gt2);
      
              assign eq = (eq1 & eq2);
              assign lt = (lt2 | (lt1 & eq2));
              assign gt = (~lt & ~eq);
      
      endmodule
      
      module Comparator4Bit(
                      input[3:0] a,
                      input[3:0] b,
                      output eq,
                      output lt,
                      output gt);
      
              assign eq = a == b;
              assign lt = a < b;
              assign gt = a > b;
      
      endmodule
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-25
        • 2013-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-02
        • 2013-11-04
        相关资源
        最近更新 更多