如何在 Verilog 中拆分两位数

发布于 2024-10-21 12:23:53 字数 54 浏览 3 评论 0原文

我需要将两位数分开,以便可以单独显示它们。问题是 mod 只适用于 2 的幂的数字。这怎么办?

I need to split a two-digit number up so that I can display them separately. The problem is that mod only works with numbers that are a power of 2. How can this be done?

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

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

发布评论

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

评论(4

逆光下的微笑 2024-10-28 12:23:53

来自 http://www.edaboard.com/thread112872.html

很多综合工具不支持
整数除法/模数/余数
除非计算是微不足道的,
例如除以 2 的幂。如果
那么你的价值不是2的幂
你可能不走运。

也许你可以使用另一种方法
例如构建自己的数学模块,
或使用您的数学核心
软件的IP库。

或者也许你可以近似
除法乘以
分数 1/K 而不是除以 K。

如果两个操作数都很小,你可以
从 ROM 查找中获取结果
表。

From http://www.edaboard.com/thread112872.html

Many synthesis tools don't support
integer division/modulus/remainder
unless the calculation is trivial,
such as division by a power of two. If
your value isn't a power of two, then
you are probably out of luck.

Maybe you can use another approach
such as building your own math module,
or using a math core from your
software's IP library.

Or maybe you can approximate the
division by multiplying by the
fraction 1/K instead of dividing by K.

If both operands are small, you could
fetch the result from a ROM lookup
table.

彼岸花ソ最美的依靠 2024-10-28 12:23:53

如果这是一个简单的递增值,您应该考虑二进制编码十进制计数器。每个数字需要 4 位,但这使得与 7 段显示器的连接变得更加容易。

//BCD counter - I did not test this
reg [3:0] digit_one, digit_two;    
always @(posedge clk)
  begin : led_digits
  if(reset)
    begin
    digit_one <= 0;
    digit_two <= 0;
    end
  else if(increment)
    begin
    //BCD values wrap at 9 
    if(digit_one == 4'd9)
      digit_one <= 0;
    else
      digit_one <= digit_one + 1;

    //Carry when previous digit wraps
    if(digit_one == 4'd9)
      begin
      if(digit_two == 4'd9)
        digit_two <= 0;
      else
        digit_two <= digit_two + 1;
      end
    end
  end

如果您需要显示一些任意的十进制值,那么它会变得更加复杂。 GuanoLoco 的解决方案应该适用于 2 位数字输出。存在更有效的算法,但实现并不那么简单。

If this is a simple incrementing value, you should consider Binary Coded Decimal counters. You need 4 bits per digit but it makes interfacing with 7-segment displays much easier.

//BCD counter - I did not test this
reg [3:0] digit_one, digit_two;    
always @(posedge clk)
  begin : led_digits
  if(reset)
    begin
    digit_one <= 0;
    digit_two <= 0;
    end
  else if(increment)
    begin
    //BCD values wrap at 9 
    if(digit_one == 4'd9)
      digit_one <= 0;
    else
      digit_one <= digit_one + 1;

    //Carry when previous digit wraps
    if(digit_one == 4'd9)
      begin
      if(digit_two == 4'd9)
        digit_two <= 0;
      else
        digit_two <= digit_two + 1;
      end
    end
  end

If you have some arbitrary decimal value you need to display, then it gets more complicated. GuanoLoco's solution should work for a 2 digit output. A more efficient algorithm exists but the implementation is not as straightforward.

离去的眼神 2024-10-28 12:23:53

一个简单的强力解决方案是使用 if-else 块将您的数字与 10 的倍数进行比较。小于您的数字的最大十倍数是“十”位,差值是“个”位。

if (number >= 90) begin
    tens <= 9;
    ones <= number - 90;
end else if ...

也就是说,这是不可扩展的,并且巨大的 if-else 块通常不是好的做法。

A simple brute force solution would be to use an if-else block to compare your number to multiples of 10. The largest multiple of ten that is smaller than your number is the "tens" digit, and the difference is the "ones" digit.

if (number >= 90) begin
    tens <= 9;
    ones <= number - 90;
end else if ...

That said, this isn't scalable, and giant if-else blocks are generally not good practice.

谁许谁一生繁华 2024-10-28 12:23:53

相当令人困惑的问题。当你说“显示”时,我立即想到$display,因此建模和模拟。出于建模目的,模数没有 2 的幂限制。

但即使您指的是综合,一般而言,模数仅适用于 2 的幂也是不正确的。但是,这样做可能不是一个好主意,因为它在硬件上会非常昂贵。

Rather confusinq question. When you say "display", I immediately think $display, hence modeling and simulation. There are no powers of 2 restrictions on modulo for modeling purposes.

But even if you mean synthesis, it is not true in general that modulo only works with powers of 2. However, doing this is probably not a very good idea, because it would be quite expensive in hardware.

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