来自 4 位比较器的 8 位比较器 - 未定义的输出

8 bit comparator from 4 bit comparator - undefined outputs

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

我在这里错过了什么?

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

您有争议:两个驱动器驱动相同的信号。在 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

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

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