2

我正在用累加器编写一个 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):

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

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

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

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

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

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

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

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

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

** 错误: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。请告诉我怎么做?:(

4

2 回答 2

8

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 标准中定义的极少数类型之一)与 xor 一起使用。

最简单的解决方案是将实体中的 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;

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

于 2009-12-01T15:02:22.113 回答
0

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

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

确保您没有在不同版本的模拟器中使用不同版本的预编译头文件。

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

于 2010-06-05T13:59:45.460 回答