首页 > 解决方案 > 如何以通用方式编写此代码(使用可以在每个项目中递增的计数器的状态机)

问题描述

我正在尝试以通用方式编写此代码。有没有办法用通用表达式替换数字。(1,2,3..max) “max”将在每个项目和项目期间发生变化。每次有新变量时,我都会在代码末尾添加一行,自己更改数字。

我试着写 max, max -1, max-2 max-3 .. 但是当 max 是通用的时这是不行的。

 case state_counter is
                when max-6 => block_output  <= variable_a;
                when max-5 => block_output  <= variable_b
                when max-4 => block_output  <= variable_c;
                when max-3 => block_output  <= variable_d;
                .....
                when max-1  => block_output  <= something;
                when max    => block_output  <= something_else;
 case state_counter is
                when 1 => block_output  <= variable_a;
                when 2 => block_output  <= variable_b
                when 3 => block_output  <= variable_c;
                when 4 => block_output  <= variable_d;
                .....
                when max-1  => block_output  <= something;
                when max    => block_output  <= something_else;

标签: vhdl

解决方案


有点变通,但是您可以将所有变量放入一个数组中,然后从该数组中选择变量吗?您必须将变量放入数组中。我没有尝试编译它,但它应该看起来像这样。

假设输出是一个 slv(7 downto 0) 并且你有通用的 MAX 来设置大小

architecture A1 of example is
 type t_output_array is array (INTEGER range <>) of std_logic_vector(7 downto 0);
 signal output_array : t_output_array(1 to MAX);
 signal I : integer range 0 to MAX; -- To select in the array
begin
 process( CLK )
  begin
   if Rising_edge( CLK ) then
      block_output  <= output_array(I);
    end if;
  end process;
end architecture A1;

另一种方法是以某种方式使用生成语句。您可以尝试使用它来查看是否可以创建一个从设定范围内列出数字和列表的过程。

G1: for I in 0 to N generate
  -- Component instances and/or process
end generate G1;

推荐阅读