-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmatrix_pkg.vhd
144 lines (110 loc) · 4.21 KB
/
matrix_pkg.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
package matrix_pkg is
type matrix is array(natural range <>, natural range <>) of std_logic;
-- assign row
function mrst(slm : matrix) return matrix;
-- assign row
---function mset(slm : matrix; slv : std_logic_vector; rowindex : natural) return matrix;
procedure mset(signal slm : out matrix; slv : std_logic_vector; rowindex : natural);
procedure mset_no_rng(signal slm : out matrix; slv : std_logic_vector; rowindex : natural);
-- get std logic vector from matrix row
function mget(slm : matrix; rowindex : natural) return std_logic_vector;
-- flatten matrix to std logic vector
function mflat(slm : matrix) return std_logic_vector;
-- inflate std logic vector to matrix
function minfl(slm : matrix; slv : std_logic_vector ) return matrix;
-- inflate std logic vector to m x n matrix
function minflmn(slv : std_logic_vector; col_len : natural; row_len : natural) return matrix;
end matrix_pkg;
package body matrix_pkg is
function mrst(slm : matrix) return matrix is
variable res : matrix(slm'length(1)-1 downto 0, slm'length(2)-1 downto 0);
variable row, col : natural := 0;
constant row_len : natural := slm'length(2);
constant col_len : natural := slm'length(1);
begin
for row in 0 to col_len-1 loop
for col in 0 to row_len-1 loop
res(row, col) := '0';
end loop;
end loop;
return res;
end function;
procedure mset(signal slm : out matrix; slv : std_logic_vector; rowindex : natural) is
variable i : natural := 0;
begin
for i in slv'range loop
slm(rowindex, i) <= slv(i);
end loop;
end procedure;
procedure mset_no_rng(signal slm : out matrix; slv : std_logic_vector; rowindex : natural) is
variable i : natural := 0;
variable j : natural := 0;
begin
j := 0;
for i in slv'range loop
slm(rowindex, j) <= slv(i);
j := j+1;
end loop;
end procedure;
-- set matrix row
-- function mset(slm : matrix; slv : std_logic_vector; rowindex : natural) return matrix is
-- variable i : natural := 0;
-- variable res : matrix(slm'length(1)-1 downto 0, slm'length(2)-1 downto 0);
-- begin
-- res := slm;
-- for i in slv'range loop
-- res(rowindex, i) := slv(i);
-- end loop;
-- return res;
-- end function;
-- get matrix row
function mget(slm : matrix; rowindex : natural) return std_logic_vector is
variable i : natural := 0;
variable slv : std_logic_vector(slm'high(2) downto 0);
begin
for i in slv'range loop
slv(i) := slm(rowindex, i);
end loop;
return slv;
end function;
-- flatten matrix to std logic vector
function mflat(slm : matrix) return std_logic_vector is
constant row_len : natural := slm'length(2);
constant col_len : natural := slm'length(1);
variable res : std_logic_vector(col_len*row_len-1 downto 0);
begin
for row in 0 to col_len-1 loop
for col in 0 to row_len-1 loop
res(row*row_len+col) := slm(row, col);
end loop;
end loop;
return res;
end function;
-- inflate std logic vector to matrix
function minfl(slm : matrix; slv : std_logic_vector) return matrix is
constant row_len : natural := slm'length(2);
constant col_len : natural := slm'length(1);
variable res : matrix(slm'length(1)-1 downto 0, slm'length(2)-1 downto 0);
begin
for row in 0 to col_len-1 loop
for col in 0 to row_len-1 loop
res(row, col) := slv(row*row_len+col);
end loop;
end loop;
return res;
end function;
-- -- inflate std logic vector to m*n matrix
function minflmn(slv : std_logic_vector; col_len : natural; row_len : natural) return matrix is
variable res : matrix(col_len-1 downto 0, row_len-1 downto 0);
begin
for row in 0 to col_len-1 loop
for col in 0 to row_len-1 loop
res(row, col) := slv(slv'length-1 - (row*row_len+col));
end loop;
end loop;
return res;
end function;
end matrix_pkg;