无符号逻辑、向量和加法 - 如何?

发布于 2024-11-01 16:30:40 字数 290 浏览 0 评论 0原文

我正在创建一个应该仅使用无符号数字的程序计数器。

我有 2 个 STD_LOGIC_VECTOR 和几个 STD_LOGIC。我需要做什么才能让他们只使用未签名的吗?目前我只有 IEEE 库; use IEEE.STD_LOGIC_1164.ALL;

我还需要在某些条件下将其中一个二进制向量加 1(正如您现在可能已经猜到的那样)。考虑到其中一个向量以 32 位输出,您能否解释一下如何执行此类操作(使用无符号并加一)。

我猜(我尝试过)Output <= Output + 1;不会的。哦,我正在使用一个过程。

I'm creating a program counter that is supposed to use only unsigned numbers.

I have 2 STD_LOGIC_VECTOR and a couple of STD_LOGIC. Is there anything I need to do so that they only use unsigned? At the moment I only have library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

I also need to increase one of the binary vectors by 1 under certain conditions (as you probably have guessed by now). Would you be so kind to explain how to perform such actions (using unsigned and adding up one) considering one of the vectors is output with 32 bits.

I'm guessing (I tried) Output <= Output + 1; won't do. Oh and I'm using a process.

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

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

发布评论

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

评论(1

阿楠 2024-11-08 16:30:40

简而言之,您可以将 ieee.numeric_std 包添加到您的架构中(library ieee; use ieee.numeric_std.all;),然后使用以下方法进行添加:

Output <= std_logic_vector(unsigned(Output) + 1);

将您的std_logic_vector 转换为无符号向量,递增它,最后将结果转换回 std_logic_vector。

请注意,如果 Output 是输出端口,则此操作将不起作用,因为您无法访问同一块内的输出端口的值。如果是这种情况,您需要添加一个新信号,然后在进程外部分配该信号的输出

如果您确实需要添加信号,则将该信号设置为与 std_logic_vector 不同的类型可能会更简单。例如,您可以使用整数或上面的无符号类型。例如:

architecture foo of bar is
    signal Output_int : integer range 0 to (2**Output'length)-1;
begin
    PR: process(clk, resetn)
    begin
        if resetn='0' then
            Output_int <= 0;
        elsif clk'event and clk='1' then
            Output_int <= Output_int + 1;
        end if;
    end process;

    Output <= std_logic_vector(to_unsigned(Output_int, Output'length));
end foo;

Output_int 声明为具有一系列有效值,以便工具能够确定整数的大小以及模拟的有效值范围。
Output_int 的声明中,Output'lengthOutput 向量的宽度(作为整数),“**”运算符用于求幂,因此该表达式的意思是“可以用与输出一样多的位来表示的所有无符号整数”。

例如,对于定义为 std_logic_vector(31 downto 0)OutputOutput'length 为 32. 232-1 是可以用无符号 32 位整数表示的最高值。因此,在示例情况下,范围 0 到 (2**Output'length)-1 解析为范围 0...4294967295 (232=4294967296) ,即可以用 32 位表示的完整无符号范围。

请注意,您需要手动添加任何回绕逻辑:当您达到最大值并尝试增加 1 时,VHDL 模拟器将产生错误,即使合成逻辑将干净地回绕到 0。

In brief, you can add the ieee.numeric_std package to your architecture (library ieee; use ieee.numeric_std.all;) and then do the addition using:

Output <= std_logic_vector(unsigned(Output) + 1);

to convert your std_logic_vector to an unsigned vector, increment it, and finally convert the result back to an std_logic_vector.

Note that if Output is an output port, this won't work because you can't access the value of an output port within the same block. If that is the case, you need to add a new signal and then assign Output from that signal, outside your process.

If you do need to add a signal, it might be simpler to make that signal a different type than std_logic_vector. For example, you could use an integer or the unsigned type above. For example:

architecture foo of bar is
    signal Output_int : integer range 0 to (2**Output'length)-1;
begin
    PR: process(clk, resetn)
    begin
        if resetn='0' then
            Output_int <= 0;
        elsif clk'event and clk='1' then
            Output_int <= Output_int + 1;
        end if;
    end process;

    Output <= std_logic_vector(to_unsigned(Output_int, Output'length));
end foo;

Output_int is declared with a range of valid values so that tools will be able to determine both the size of the integer as well as the range of valid values for simulation.
In the declaration of Output_int, Output'length is the width of the Output vector (as an integer), and the "**" operator is used for exponentiation, so the expression means "all unsigned integers that can be expressed with as many bits as Output has".

For example, for an Output defined as std_logic_vector(31 downto 0), Output'length is 32. 232-1 is the highest value that can be expressed with an unsigned 32-bit integer. Thus, in the example case, the range 0 to (2**Output'length)-1 resolves to the range 0...4294967295 (232=4294967296), i.e. the full unsigned range that can be expressed with 32 bits.

Note that you'll need to add any wrapping logic manually: VHDL simulators will produce an error when you've reached the maximum value and try to increment by one, even if the synthesized logic will cleanly wrap around to 0.

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