Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

matrix 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 write this code? Many thanks

22 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Dear Tricky,

    From the code you give above, the input will be a matrix std_logic_vector data type right? As I know the input will be like "0011".

    How if I want the input is a matrix with integer data type? It is I need to change the data type std_logic_vector to integer? Thanks for reply
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    it 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.