VHDL - std_logic_vector 问题

发布于 2024-08-13 06:05:25 字数 2432 浏览 2 评论 0原文

我正在用累加器编写一个 4 位二进制加法器:

library ieee;
use ieee.std_logic_1164.all;

entity binadder is
    port(n,clk,sh:in bit;
        x,y:inout std_logic_vector(3 downto 0);
        co:inout bit;
        done:out bit);
end binadder;

architecture binadder of binadder is
    signal state: integer range 0 to 3;
    signal sum,cin:bit;
begin
    sum<= (x(0) xor y(0)) xor cin;
    co<= (x(0) and y(0)) or (y(0) and cin) or (x(0) and cin);

    process
    begin
        wait until clk='0';
        case state is
            when 0=>
                if(n='1') then
                    state<=1;
                end if;
            when 1|2|3=>
                if(sh='1') then
                    x<= sum & x(3 downto 1);
                    y<= y(0) & y(3 downto 1);
                    cin<=co;
                end if;
                if(state=3) then
                    state<=0;
                end if;
        end case;
    end process;

    done<='1' when state=3 else '0';
end binadder;

输出:

--编译binadder的架构binadder

** 错误:C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(15):

中缀运算符没有可行的条目 “异或”。

** 错误:C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(15):

解析中缀表达式时出现类型错误 “xor”作为 std.standard.bit 类型。

** 错误:C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(16):

中缀运算符没有可行的条目 “和”。

** 错误:C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(16):

右操作数中的错误表达式 中缀表达式“或”。

** 错误:C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(16):

中缀运算符没有可行的条目 “和”。

** 错误:C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(16):

左操作数中的错误表达式 中缀表达式“或”。

** 错误:C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(16):

右操作数中的错误表达式 中缀表达式“或”。

** 错误:C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(16):

解析中缀表达式时出现类型错误 “或”作为 std.standard.bit 类型。

** 错误:C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(28):

中缀运算符没有可行的条目 “&”。

** 错误:C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(28):

解析中缀表达式时出现类型错误 “&”作为类型 ieee.std_logic_1164.std_logic_vector。

** 错误:C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(39):

VHDL编译器退出

我相信我没有正确处理 std_logic_vector。请告诉我怎么做? :(

i'm coding a 4-bit binary adder with accumulator:

library ieee;
use ieee.std_logic_1164.all;

entity binadder is
    port(n,clk,sh:in bit;
        x,y:inout std_logic_vector(3 downto 0);
        co:inout bit;
        done:out bit);
end binadder;

architecture binadder of binadder is
    signal state: integer range 0 to 3;
    signal sum,cin:bit;
begin
    sum<= (x(0) xor y(0)) xor cin;
    co<= (x(0) and y(0)) or (y(0) and cin) or (x(0) and cin);

    process
    begin
        wait until clk='0';
        case state is
            when 0=>
                if(n='1') then
                    state<=1;
                end if;
            when 1|2|3=>
                if(sh='1') then
                    x<= sum & x(3 downto 1);
                    y<= y(0) & y(3 downto 1);
                    cin<=co;
                end if;
                if(state=3) then
                    state<=0;
                end if;
        end case;
    end process;

    done<='1' when state=3 else '0';
end binadder;

The output :

-- Compiling architecture binadder of binadder

** Error: C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(15):

No feasible entries for infix operator
"xor".

** Error: C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(15):

Type error resolving infix expression
"xor" as type std.standard.bit.

** Error: C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(16):

No feasible entries for infix operator
"and".

** Error: C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(16):

Bad expression in right operand of
infix expression "or".

** Error: C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(16):

No feasible entries for infix operator
"and".

** Error: C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(16):

Bad expression in left operand of
infix expression "or".

** Error: C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(16):

Bad expression in right operand of
infix expression "or".

** Error: C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(16):

Type error resolving infix expression
"or" as type std.standard.bit.

** Error: C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(28):

No feasible entries for infix operator
"&".

** Error: C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(28):

Type error resolving infix expression
"&" as type
ieee.std_logic_1164.std_logic_vector.

** Error: C:/Modeltech_pe_edu_6.5a/examples/binadder.vhdl(39):

VHDL Compiler exiting

I believe i'm not handling std_logic_vector's correctly. Please tell me how? :(

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

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

发布评论

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

评论(2

記柔刀 2024-08-20 06:05:25

VHDL 的特点之一是基本语言本身提供的功能非常少。其中大部分是通过使用包提供的。代码的第二行是一个示例(使用 ieee.std_logic_1164.all)。这意味着您正在使用所有 std_logic_1164 包。请参阅此处了解此包的定义。

当您编写代码时,您通常希望将信号存储在 std_logic 或 std_logic_vector 中。这有两个原因。首先,std_logic 还可以表示“0”或“1”以外的值。例如,它还可以表示“Z”或“X”。第二个是模拟器(例如您正在使用的 modelsim)经过优化,可以使用 std_logic 运行得更快。

作为一般约定,最好始终将实体的输入和输出设为 std_logic 或 std_logic_vector。

您遇到的具体问题是您正在将类型位(这是 VHDL 标准中定义的极少数类型之一)与异或一起使用。

最简单的解决方案是将实体中的 co 输出更改为 std_logic 类型,并将 sum 和 cin 的声明更改为 std_logic 类型。

entity binadder is
    port(n,clk,sh:in bit;
         x,y:inout std_logic_vector(3 downto 0);
         co:inout std_logic;
         done:out bit);
end binadder;

    signal sum,cin:std_logic;

进一步的评论是,除非您有充分的理由这样做,否则将端口设置为 inout 通常是不好的做法,因为这会消除语言中内置的一些严格的类型检查。最好的解决方案是在实体本身内创建一个信号并将该信号直接分配给输出。

entity binadder is
    port(n,clk,sh:in bit;
         x,y:inout std_logic_vector(3 downto 0);
         co:out std_logic;
         done:out bit);
end binadder;

    signal co_int:std_logic;
 begin
    co_int<= (x(0) and y(0)) or (y(0) and cin) or (x(0) and cin);
    co <= co_int;

最后一个评论是,一旦状态的值为 1,它怎么会变成 2 或 3?

One of the features of VHDL is that very little functionality is provided in the base language itself. Most of it is provided by using packages. The second line of your code is an example of this (use ieee.std_logic_1164.all). This means that you are using all of the std_logic_1164 package. See here for what this package defines.

When you write code, you generally want to store your signals in either std_logic or std_logic_vector. There are two reasons for this. The first is that a std_logic can also represent values other than '0' or '1'. It can also represent 'Z' or 'X' for example. The second is that the simulators (such as modelsim that you are using) are optimised to run faster with std_logic.

As a general convention, it is good practice to always make the inputs and outputs from your entity a std_logic or std_logic_vector.

The specific problem you are having is that you are using the type bit (which is one of the very few types defined in the VHDL standard) with xor.

The simplest solution is to change the co output in your entity to be of type std_logic and to change the declaration for sum and cin to be of type std_logic.

entity binadder is
    port(n,clk,sh:in bit;
         x,y:inout std_logic_vector(3 downto 0);
         co:inout std_logic;
         done:out bit);
end binadder;

    signal sum,cin:std_logic;

A further comment is that it is generally bad practice to make your ports inout unless you have a very good reason to do so as this removes some of the strict type checking that is built into the language. The best solution is to create a signal within the entity itself and assign the signal directly to the output.

entity binadder is
    port(n,clk,sh:in bit;
         x,y:inout std_logic_vector(3 downto 0);
         co:out std_logic;
         done:out bit);
end binadder;

    signal co_int:std_logic;
 begin
    co_int<= (x(0) and y(0)) or (y(0) and cin) or (x(0) and cin);
    co <= co_int;

One final comment is that once the value of state is 1, how will it ever become 2 or 3?

悲欢浪云 2024-08-20 06:05:25

查看您的逻辑-物理库映射。

检查物理库是否确实已转储包。

确保您没有将不同版本的预编译头与不同版本的模拟器一起使用。

如果没有任何效果,只需制作 ieee 的本地副本,将 std_logic_1164 包编译到其中,移动到工作库,然后编译您的设计。这必须起作用。

Take a look into your logical-physical library mappings.

Check that the physical library actually has the packages dumped.

Make sure you are not using a different version of pre-compiled header with a different version of the simulator.

If nothing works, just make a local copy of ieee, compile the std_logic_1164 packages into it, move to work library and then compile your design. This has to work.

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