Verilog中算术运算结果的大小

发布于 2024-12-13 21:59:40 字数 1099 浏览 0 评论 0原文

我正在 Verilog 中制作一个签名比较器。代码如下:

module signedComparator(a0, a1, a2, b0, b1, b2, G, E, L);
input a0, a1, a2, b0, b1, b2;
output reg G, E, L;

always@(a0 or a1 or a2 or b0 or b1 or b2)
begin

    if(a2 == 0 && b2 == 0) //both a and b >= 0
    begin
        L <= {a1,a0} < {b1,b0};
        G <= {a1,a0} > {b1,b0};
        E <= {a1,a0} == {b1,b0}; 
    end
    else if(a2 == 1 && b2 == 0) //a negative, b >= 0
    begin
        L <= 1;
        G <= 0;
        E <= 0;
    end
    else if(a2 == 0 && b2 == 1) //a >= 0, b negative
    begin
        L <= 0;
        G <= 1;
        E <= 0;
    end
    else //both a and b negative
    begin
        L <= (~{a1,a0} + 1) > (~{b1,b0} + 1);
        G <= (~{a1,a0} + 1) < (~{b1,b0} + 1);
        E <= (~{a1,a0} + 1) == (~{b1,b0} + 1);
    end

end
endmodule

我想知道,向量相加时,中间结果的长度是多少?我担心最后一种情况 (L <= (~{a1,a0} + 1) > (~{b1,b0} + 1);)。 ~{a1,a0}加1时,比较结果是三位长度,还是{1,1} + 1 = {0,0}?是否有文档说明 verilog 中中间结果的数据类型是什么?由于我还不知道正确的术语,因此很难搜索。

I am making a signed comparator in Verilog. Here is the code:

module signedComparator(a0, a1, a2, b0, b1, b2, G, E, L);
input a0, a1, a2, b0, b1, b2;
output reg G, E, L;

always@(a0 or a1 or a2 or b0 or b1 or b2)
begin

    if(a2 == 0 && b2 == 0) //both a and b >= 0
    begin
        L <= {a1,a0} < {b1,b0};
        G <= {a1,a0} > {b1,b0};
        E <= {a1,a0} == {b1,b0}; 
    end
    else if(a2 == 1 && b2 == 0) //a negative, b >= 0
    begin
        L <= 1;
        G <= 0;
        E <= 0;
    end
    else if(a2 == 0 && b2 == 1) //a >= 0, b negative
    begin
        L <= 0;
        G <= 1;
        E <= 0;
    end
    else //both a and b negative
    begin
        L <= (~{a1,a0} + 1) > (~{b1,b0} + 1);
        G <= (~{a1,a0} + 1) < (~{b1,b0} + 1);
        E <= (~{a1,a0} + 1) == (~{b1,b0} + 1);
    end

end
endmodule

I am wondering, when adding vectors, what is the length of the intermediate result? I am concerned about the last case (L <= (~{a1,a0} + 1) > (~{b1,b0} + 1);). When adding 1 to ~{a1,a0}, is the result three bits in length for the comparison, or will {1,1} + 1 = {0,0}? Is there documentation somewhere for what the data type of intermediate results in verilog will be? This is hard to search for since I don't yet know the proper terminology.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来是傀儡 2024-12-20 21:59:40

我假设这是为了综合,并对您的代码有一些评论。您似乎使用各个位作为模块的输入,然后使用串联来创建向量。您可以通过将端口声明为有符号向量并直接进行比较来避免这种情况。

input signed [2:0] a,b;
...
if(a == b)
...
else if(a > b)
...
else
...

此外,您还使用非阻塞分配来建模组合逻辑。这些将在您发布的代码中工作,但实际上不应该以这种方式使用。它们通过时钟进程更好地建模同步逻辑。有一篇好论文总结了良好的综合编码风格。

我想知道,向量相加时,中间结果的长度是多少?

规范为此提供了一个表格,因为它取决于操作数和上下文。

  • 整数:无大小常量至少为 32 位
  • {a,b} : sizeof(a) + sizeof(b)
  • ~{a} : sizeof(a)
  • a + b : max(sizeof(a),sizeof(b) )

因此,您的比较操作数都将(至少)为 32 位。您可以通过在值前使用勾号来显式分配常量大小。

4'b1 // 0001 Binary 1
4'd1 // 0001 Decimal 1
4'd8 // 1000 Decimal 8
1'b1 //    1 Binary 1
'b1  // The same as 1, tick here only specifies dec/oct/bin format

是否有文档说明数据类型
verilog 的中间结果会是什么?

到目前为止,我找到的有关此类详细信息的最佳资源是规范本身,IEEE 1364。

I'm assuming this is for synthesis and have a few comments about your code. You seem to be using individual bits as inputs to the module and then using concatenation to make vectors later on. You can avoid this by declaring ports as signed vectors and doing a comparison directly.

input signed [2:0] a,b;
...
if(a == b)
...
else if(a > b)
...
else
...

Also, you are using non-blocking assignments to model combinational logic. These will work in the code you posted but really shouldn't be used in this manner. They work much better modeling synchronous logic via a clocked process. There's a good paper that summarizes a good coding style for synthesis.

I am wondering, when adding vectors, what is the length of the intermediate result?

The spec has a table for this as it depends on the operands and context.

  • An integer : Unsized constants are at least 32-bits
  • {a,b} : sizeof(a) + sizeof(b)
  • ~{a} : sizeof(a)
  • a + b : max(sizeof(a),sizeof(b))

Thus your comparison operands will both be (at least) 32-bits. You can explicitly assign a constant size by using a tick before the value.

4'b1 // 0001 Binary 1
4'd1 // 0001 Decimal 1
4'd8 // 1000 Decimal 8
1'b1 //    1 Binary 1
'b1  // The same as 1, tick here only specifies dec/oct/bin format

Is there documentation somewhere for what the data type of
intermediate results in verilog will be?

By far the best resource I've found for details like this is the spec itself, IEEE 1364.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文