Forum Discussion
Altera_Forum
Honored Contributor
15 years agomatrix array in vhdl
Dear all, I have 4x4 matrix and I want to read this matrix row by row on each clock cycle. For example, row1 is read in cycle1, row2 is read in cycle2 and so on. Can anyone give idea how to w...
Altera_Forum
Honored Contributor
15 years agoit depends. If you have an array of arrays, its going to be much easier:
type row_t is array(0 to 3) of std_logic_vector(7 downto 0);
type matrix_t is array(0 to 3) of row_t;
signal matrix : matrix_t;
signal temp_row : row_t;
signal count : unsigned(1 downto 0) := "00";
begin
process(clk)
begin
if rising_edge(clk) then
temp_row <= matrix( to_integer(count) );
count <= count + 1;
end if;
end process;
If you're using a 2d, matrix type, things get a little more complicated as you have to use a function to extract the row, but it should generate the same logic.
type row_t is array(0 to 3) of std_logic_vector(7 downto 0);
type matrix_t is array(0 to 3, 0 to 3) of std_logic_vector(7 downto 0);;
signal matrix : matrix_t;
signal temp_row : row_t;
signal count : unsigned(1 downto 0) := "00";
function extract_row( m : matrix_t; row : integer) return row_t is
variable ret : row_t;
begin
for i in row_t'range loop
ret(i) := m(row, i);
end loop;
return ret;
end function;
begin
process(clk)
begin
if rising_edge(clk) then
temp_row <= extract_row( matrix, to_integer(count) );
count <= count + 1;
end if;
end process;
NB: Matrix could be an input to your entity.