如何添加矢量的位,同时将值保存在矢量信号中?我使用谷歌翻译**



我想知道std_logic_vector中"1"的数量是奇数还是偶数。为此,我尝试使用"if"语句和计数器,但我没有得到预期的结果。

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.std_logic_unsigned.all;
entity prueba2 is
port (
entrada: in std_logic_vector(0 to 10)
);
end prueba2;
architecture Behavioral of prueba2 is
signal bit1: std_logic_vector(0 to 3):= (others => '0');
begin
prueba: process(entrada)
--variable suma: unsigned(0 to 2):= (others => '0');
begin
for i in 0 to 10 loop
if (entrada(i)= '1') then
bit1 <= bit1+1;
end if;
end loop;
end process;
end Behavioral;

我没有在语法中遇到错误,但是例如,通过使用输入我在模拟中作为输出收到的向量"1111111111",使用ISE设计套件,值"0001"而不是"1010"。感谢您的更正和代码建议。

参见IEEE Std 1076-2008 10.5.2.2执行简单的赋值语句,14.7.3.4 仿真周期和 14.7.3.4 信号更新。

信号不会立即更新。当前仿真周期尚未完成,直到对事件敏感的每个进程都已执行并可能计划信号更新并随后暂停。仿真时间提前到安排下一次信号更新的时间。信号在仿真周期开始时更新,然后由于信号事件而恢复任何暂停的进程。

进程暂停和恢复执行等待语句(请参阅 10.2 等待语句(。具有敏感度列表的进程语句 (11.3( 具有隐式等待语句作为最后一个语句,其敏感度列表具有进程敏感度列表的内容。

信号 bit1 在循环执行时不会更新,没有隐式或显式的等待语句导致进程挂起。

要解决此问题,请使用变量:

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.std_logic_unsigned.all;  -- arithmetic for std_logic_vector
entity prueba2 is
port (
entrada: in std_logic_vector(0 to 10) := (others => '1')
);
end entity prueba2;
architecture behavioral of prueba2 is
signal bit1: std_logic_vector(0 to 3):= (others => '0');
function to_string (inp: std_logic_vector) return string is
variable image_str: string (1 to inp'length);
alias input_str:  std_logic_vector (1 to inp'length) is inp;
begin
for i in input_str'range loop
image_str(i) := character'VALUE(std_ulogic'IMAGE(input_str(i)));
end loop;
return image_str;
end function;
begin
prueba: 
process (entrada)  -- sensitivity list implies wait as final statement
-- variable suma: unsigned(0 to 2):= (others => '0');
variable suma: std_logic_vector(0 to 3); 
begin
suma := (others => '0'); -- start at 0 every time
for i in entrada'range loop
if entrada(i) = '1' then
-- bit1 <= bit1+1 -- signals don't update until the beginning
-- of the next simulation cycle (at the soonest)
suma := suma + 1; -- variables assignment is immediate
end if;
end loop;
report "bit1 will be assigned " & to_string(suma); 
bit1 <= suma; -- both the same length processes communicate with signals
-- wait on entrada;  -- implicit wait statement, process suspends
end process;
end architecture behavioral;

您可以看到该变量与 bit1 具有相同的长度。这是必需的,您输入的 entrada 的长度为 11,这需要一个 4 位值来累积"1"的数量。

有一些用于测试的装饰。某些模拟器将允许您使用端口模拟顶级,只要输入具有默认值或可以强制输入。在这里,恩特拉达提供所有"1"。to_string功能在标准的修订版-2008中提供,此处也提供该功能以与早期修订版兼容。报告语句告诉我们将分配给 bit1 的 suma 值。

分析并阐述(编译(设计单元后,运行:

ghdl -a --ieee=synopsys -fexplicit prueba2.vhdl
ghdl -e --ieee=synopsys -fexplicit prueba2
ghdl -r prueba2
prueba2.vhdl:37:9:@0ms:(report note): bit1 will be assigned 1011

循环已成功计数所有"1",bit1 将安排具有相同模拟时间(此处为 0 毫秒(的更新。仿真器将执行后续仿真周期(增量周期(,然后不在任何投影输出波形(队列(中计划任何进一步的计划信号更新,仿真时间将提前到 TIME'HIGH,仿真将结束。

当模拟开始时,每个进程在初始化后至少执行一次。bit1 上的事件将导致信号值更新,但独立示例中没有对 bit1 敏感的进程,仿真将停止。

可以删除输入 entrada、函数to_string和 report 语句上的默认值,它们用于测试,未提供测试平台。

最新更新