Frage

Is there a shorthand way to set a row of values in a matrix? I'm looking for a function/procedure type of solution.

Just to clarify, by matrix I don't mean an array of array but rather a 2D array.

I've managed to read a specific row using:

function extract_row(matrix : matrix_type; row_index : natural) return row_type is
    variable res : row_type (matrix'range(2));
begin
    for i in res'range loop
        res(i) := matrix(row, i);
    end loop;
    return res;
end function;

And now I need a way to set a row in a fashion similar to how one can set a subarray in the array of arrays:

signal x : array_of_rows_type(range_a)(range_b);
signal y : row_type(range_b);

x(0) <= y;

I realise the shorthand isn't necessary and that one can work around it using loops and generates, but I have many places where I need to do this, and it's becoming increasingly difficult to keep legible code (and my sanity).

For those wondering, the reason why I'm using the matrix approach and not the array of arrays is because I need to reuse the type in multiple entities with different ranges.

Bonus points if the solution somehow allows me to use it in port mapping (although I realise this is impossible, unless I've misunderstood VHDL completely). i.e.:

port map (
    row_type_outport => row_insert_solution(matrix, row)
)
War es hilfreich?

Lösung

I seem to recognize the code for the extract_row function. Perhaps you could adapt the function replace_matrix_column that is given in the same package?

function replace_matrix_column(
    input_matrix: bit_matrix; 
    new_column: bit_vector; 
    column_index: integer
) return bit_matrix is
    variable output: bit_matrix(input_matrix'range(1), input_matrix'range(2));
begin
    for i in input_matrix'range(1) loop
        for j in input_matrix'range(2) loop
            if j = column_index then
                output(i, j) := new_column(i);
            else
                output(i, j) := input_matrix(i, j);
            end if;
        end loop;
    end loop;

    return output;
end;

Then you could call it like this:

x <= replace_matrix_row(x, y, 0);

Or, for better clarity:

x <= replace_matrix_row(input_matrix => x, new_row => y, row_index => 0);

Andere Tipps

I don't have your types, so I can't compile this to make sure it works, but this might help get you close.

function row_insert_solution(matrix : matrix_type; 
                             row_insert : row_type; 
                             row_index : natural
                             ) return matrix_type is

variable res : matrix_type := matrix;
begin
  for i in row_insert'range loop
    res(row_index, i) := row_insert(i);
  end loop;

  return res;
end function;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top