在流程外的二维数组中填充一行(VHDL)

时间:2014-09-14 18:14:30

标签: arrays matrix vhdl

我有阵列:

type MATR is array(natural range 1 to N, natural range 1 to N) of natural;
signal m: MATR;

1)是否可以用过程外的某些值填充元素m(0,1),m(0,2)... m(0,N)?类似的东西:

m(1) <= (others => 2)

2)是否可以将1D数组(范围1到N)分配给2D数组的一行(也在处理之外)?

2 个答案:

答案 0 :(得分:1)

是的,你可以通过写一个像这样的程序来做到这一点:

procedure assign_row(signal slm : out T_SLM; slv : STD_LOGIC_VECTOR; constant RowIndex : NATURAL) is
  variable temp : STD_LOGIC_VECTOR(slm'high(2) downto slm'low(2));  -- Xilinx iSIM work-around, because 'range(2) evaluates to 'range(1); tested with ISE XST/iSim 14.2
begin
  temp := slv;
  for i in temp'range loop
    slm(RowIndex, i)  <= temp(i);
  end loop;
end procedure;

其中T_SLM是我的矩阵类型定义如下:

-- STD_LOGIC_MATRIXs
type T_SLM is array(NATURAL range <>, NATURAL range <>) of STD_LOGIC;
-- ATTENTION:
-- 1. you MUST initialize your matrix signal with 'Z' to get correct simulation results (iSim, vSim, ghdl/gtkwave)
--    Example: signal myMatrix : T_SLM(3 downto 0, 7 downto 0) := (others => (others => 'Z'));
-- 2. Xilinx iSIM work-around: DON'T use myMatrix'range(n) for n >= 2
--    because: myMatrix'range(2) returns always myMatrix'range(1); tested with ISE/iSim 14.2
-- USAGE NOTES:
--  dimension 1 => rows     - e.g. Words
--  dimension 2 => columns  - e.g. Bits/Bytes in a word

以下是使用此过程的示例:

architecture [...]
  signal myVector : STD_LOGIC_VECTOR(7 downto 0);
  signal myMatrix : T_SLM(3 downto 0, myVector'range) := (others => (others => 'Z'));
  [...]
begin
  [...]
  assign_row(myMatrix, myVector, 0);
  assign_row(myMatrix, (myVector'range => '0'), 1);
  assign_row(myMatrix, x"4A", 2);
  [...]
end;

此代码使用ISE XST和iSim(13.x,14.x),vSim和GHDL进行测试。由于ISE 13.x是当前版本,Xilinx表示范围错误不会在ISE 14.x中修复。

如果你需要反过来,这是我的函数get_row:

-- get a matrix row
function get_row(slm : T_SLM; RowIndex : NATURAL) return STD_LOGIC_VECTOR is
  variable slv : STD_LOGIC_VECTOR(slm'high(2) downto slm'low(2));       -- Xilinx iSim work-around, because 'range(2) = 'range(1); tested with ISE/iSim 14.2
begin
  for i in slv'range loop
    slv(i)  := slm(RowIndex, i);
  end loop;
  return slv;
end function;

如果要将NATURAL用作向量和矩阵的基本类型,则将STD_LOGIC与NATURAL交换。

答案 1 :(得分:0)

1)是否可以用过程外的某些值填充元素m(0,1),m(0,2)... m(0,N)?

2)是否可以将1D数组(范围1到N)分配给2D数组的一行(也在处理之外)?

是的,这是可能的。我只举一个加载新行的例子。请注意,此示例适用于N = 4,对于较大的数组,该方法可能不切实际。

另请注意,这只允许使用策略性索引。

entity foo is
    constant N: natural := 4;
end entity;

architecture fum of foo is
    type MATR is array(natural range 1 to N, natural range 1 to N) of natural;
    type col is array (natural range 1 to N) of natural;
    constant f:col := (1,2,3,4);
    signal m: MATR;

begin
    m <=  (1 => ( 1 => f(1),    2 => f(2),    3 => f(3),   4 => f(4)), 
           2 => ( 1 => m(2,1),  2 => m(2,2),  3 => m(2,3), 4 => m(2,4)),
           3 => ( 1 => m(3,1),  2 => m(3,2),  3 => m(3,3), 4 => m(3,4)),
           4 => ( 1 => m(4,1),  2 => m(4,2),  3 => m(4,3), 4 => m(4,4)));

end architecture;

(也许你的问题应该更加明确。为了你的目的,答案可能不切实际。)