Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] How to have an undefined range in function?

Status
Not open for further replies.

wtr

Full Member level 5
Joined
May 1, 2014
Messages
299
Helped
29
Reputation
58
Reaction score
25
Trophy points
1,308
Activity points
4,108
Hello, pleases look at the following.

Code VHDL - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
type byte_2d_matrix_t is array(natural range <>, natural range <>) of BYTE_T;
subtype  COL_RANGE is NATURAL range 0 TO 3;
subtype  ROW_RANGE is NATURAL range 0 TO 3;
 
    function slv2matrix2d (a : STD_LOGIC_VECTOR) return byte_2d_matrix_t is 
        -- sqr(a)
        variable b : byte_2d_matrix_t(ROW_RANGE, COL_RANGE);
        -- variable b : byte_2d_matrix_t(rr, col);
    begin
        for i in COL_RANGE loop
            for j in ROW_RANGE loop
                b(j, i) := a(
                                (BLK_WIDTH - (1+(i*DWORD_WIDTH) + (j*BYTE_WIDTH))) 
                                DOWNTO 
                                (BLK_WIDTH - ((i*DWORD_WIDTH) + ((j+1)*BYTE_WIDTH))) 
                            );
            end loop;
        end loop;
    
    return b;
    end function;



How can I facilitate the situation where I want to change this function to

function slv2matrix2d (a : STD_LOGIC_VECTOR, range_of_row, Range_of_col) return byte_2d_matrix_t

Whereby this means that BLK_WIDTH = a'length and DWORD_WIDTH=cellsize(byte)*numbers of col

I don't think range ascending or decending matters
Regards
 

Define all the parameters for the function for word matrix size:


Code VHDL - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function slv2matrix2d(a : in std_logic_vector; n_rows, n_cols : integer) return byte_2d_matrix_t is
  variable  ret : byte_2d_matrix_t(0 to n_rows-1, 0 to n_cols-1);
begin
  assert (a rem 8) = 0 and n_rows*n_cols*8 = a'length
    report "Specified paramters do name match input word length"
      severity failure;
 
  for i in 0 to n_rows-1 loop
    for j in 0 to n_cols-1 loop
      ret(i,j) = a( (i*n_cols + j)*8 + 7 downto (i*n_cols + j)*8);
    end loop;
  end loop
 
  return ret;
end function;



This way, it doesnt matter what the range of the output is, because this will already be known. the 0 to n_rows etc will just map onto whatever the output is:

Code:
signal my_byte2d : byte_2d_matrix_t(123 downto 77, 5 to 104732464);

my_byte2d <= slv2matrix2d( a, my_byte2d'length(1), my_byte2d'length(2));
 
  • Like
Reactions: wtr

    wtr

    Points: 2
    Helpful Answer Positive Rating
Tricky, sometimes I wonder if you wrote the language.
 

no problem. Please see the revised version below, that will work with all versions of a, whether to or downto (and any length of BYTE_T)


Code VHDL - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function slv2matrix2d(a : in std_logic_vector; n_rows, n_cols : integer) return byte_2d_matrix_t is
  variable  ret : byte_2d_matrix_t(0 to n_rows-1, 0 to n_cols-1);
  alias a_a : std_logic_vector(a'length-1 downto 0) is a;
  
begin
  assert (a_a'length rem BYTE_T'length) = 0 and n_rows*n_cols*BYTE_T'length = a'length
    report "Specified paramters do name match input word length"
      severity failure;
 
  for i in 0 to n_rows-1 loop
    for j in 0 to n_cols-1 loop
      ret(i,j) = a_a( (i*n_cols + j)*BYTE_T'length + 7 downto (i*n_cols + j)*BYTE_T'length);
    end loop;
  end loop
 
  return ret;
end function;

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top