【问题标题】:Detecting the position of the 1 bit value in the less significative position VHDL检测低位 VHDL 中 1 位值的位置
【发布时间】:2015-11-16 18:53:11
【问题描述】:

我有一个 32 位的字。我需要使用 VHDL 创建一个新的 32 位字,其中仅包含第一个值为 1 的位,位于不太重要的位置。

例子:

输入:01000010 01001011 00000000 00100010
输出:00000000 00000000 00000000 00000010

【问题讨论】:

  • 一个 if then elsif... if 语句按优先级顺序测试输入位 只有一个测试条件评估为真。等效的条件信号赋值。一个 for 循环,内部有一个出口并封闭 if 语句测试循环常量输入位。这个想法是只有最高优先级的位被设置,其余的被重置。
  • 您尚未指定这是用于行为建模还是合成。
  • @user1155120 :可以合成行为代码。问题是他是否希望在行为建模或结构建模中使用它。
  • 所有 VHDL 模型都是行为的 - IEEE Std 1076-2008 14.2 设计层次结构的细化“设计层次结构的细化创建了由网络互连的过程的集合;然后,这个过程和网络的集合可以是执行以模拟设计的行为。”结构由内部或外部块语句叠加。但是,有些 VHDL 设计规范无法综合。

标签: vhdl


【解决方案1】:

这个想法是按顺序从最高优先级端到最低优先级端遍历输入,当遇到“1”时,在输出上生成一个值,对应位置为“1”,其余位置为“0”。找到第一个这样的匹配项后,您退出。

我想到了五种方法。使用 if 语句、条件信号赋值(或 -2008 中的条件变量赋值)、带有退出的循环语句、case 语句和选定的信号赋值(或 -2008 中的选定变量赋值)。

其中一些依赖于 VHDL 标准修订级别,不能保证对综合的支持。

最简单的表达是流程中显示的for循环:

library ieee;
use ieee.std_logic_1164.all;

entity priority_mapper is
    port (
        input:  in  std_logic_vector (0 to 31);
        output: out std_logic_vector (0 to 31)
    );
end entity;

architecture foe of priority_mapper is

begin
MAPIT:
    process (input)
    begin
        output <= (others => '0');
        for i in input'reverse_range loop 
            if input(i) = '1' then
                output(i) <= '1';
                exit;     
            end if;
        end loop;
    end process;
end architecture;

对于您的样本刺激:

input <= B"01000010_01001011_00000000_00100010";

它产生输出:

priority_mapper.vhdl:136:13:@10ns:(报告说明):输出 = 00000000 00000000 00000000 00000010

在测试台输出中放置空格有点复杂:

library ieee;
use ieee.std_logic_1164.all;

entity priority_mapper_tb is
end entity;

architecture tb of priority_mapper_tb is
    signal input:  std_logic_vector (0 to 31) := (others => '0');
    signal output: std_logic_vector (0 to 31) := (others => '0');
begin
DUT:
    entity work.priority_mapper (foe)
        port map (input, output);
STIMULUS:
    input <= B"01000010_01001011_00000000_00100010" after 10 ns;
RESPONSE:
    process (output)
        variable  output_string: string (1 to 35);
        variable j: integer range output_string'range;
    begin
        if now > 0 ns then
            j := 1;
            for i in output'range loop
                if i = 8 or i = 16 or i = 24 then
                    output_string(j) := ' ';
                    j :=  j + 1;
                end if;
                output_string(j) :=         
                            character'VALUE(std_ulogic'IMAGE(output(i)));
                if j < output_string'right then
                    j := j + 1;
                end if;
            end loop;
            report "output = " & output_string;
        end if;
    end process;
end architecture;

在字符串输入中接受空格的代码将非常复杂,将表示值的字符转换为 std_logic 数组值。你没有显示类型声明,所以看起来有点工作。输出空间很奇思妙想。

【讨论】:

    猜你喜欢
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 2016-12-30
    • 2020-02-14
    • 1970-01-01
    相关资源
    最近更新 更多