blob: 92d881b387468d61620b95574897231b20546afd (
plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
--
-- create a mask with all length bit set
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
entity len_to_mask is
port
(
len: in unsigned;
mask: out std_logic_vector
);
end entity;
architecture len_to_mask_rtl of len_to_mask is
constant size: integer := mask'length;
begin
--
-- result in a mask such as:
-- mask(mask'length - 1 downto len) = '0';
-- mask(len - 1 downto 0) = '1';
process(len)
begin
for i in 2 to size loop
if i = to_integer(len) then
mask(size - 1 downto i) <= (others => '0');
mask(i - 1 downto 0) <= (others => '1');
end if;
end loop;
end process;
end len_to_mask_rtl;
--
-- lsb to msb conversion
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
entity lsb_to_msb is
port
(
en: in std_logic;
data_len: in unsigned;
lsb_data: in std_logic_vector;
msb_data: out std_logic_vector
);
end entity;
architecture lsb_to_msb_rtl of lsb_to_msb is
constant size: integer := lsb_data'length;
begin
--
-- lsb_data: uuuuLxxxxM
-- msb_data: uuuuMxxxxL
-- where u undefined, x 0 or 1
process(en, lsb_data)
begin
if (en = '1') then
for i in 1 to size loop
if i = to_integer(data_len) then
msb_data(msb_data'length - 1 downto i) <= (others => '0');
for j in 0 to (i - 1) loop
msb_data(j) <= lsb_data(i - 1 - j);
end loop;
exit ;
end if;
end loop;
else
msb_data <= lsb_data;
end if;
end process;
end lsb_to_msb_rtl;
--
-- binary to gray conversion
library ieee;
use ieee.std_logic_1164.all;
entity bin_to_gray is
port
(
-- enable conversion
en: in std_logic;
bin_data: in std_logic_vector;
gray_data: out std_logic_vector
);
end entity;
architecture bin_to_gray_rtl of bin_to_gray is
constant size: integer := bin_data'length;
begin
assert (bin_data'length = gray_data'length)
report "size differs" severity failure;
process(en, bin_data) is
begin
if en = '1' then
for j in 0 to (size - 2) loop
gray_data(j) <= bin_data(j) xor bin_data(j + 1);
end loop;
gray_data(size - 1) <= bin_data(size - 1);
else
gray_data <= bin_data;
end if;
end process;
end bin_to_gray_rtl;
--
-- gray to binary conversion
library ieee;
use ieee.std_logic_1164.all;
entity gray_to_bin is
port
(
-- enable conversion
en: in std_logic;
gray_data: in std_logic_vector;
bin_data: out std_logic_vector
);
end entity;
architecture gray_to_bin_rtl of gray_to_bin is
constant size: integer := bin_data'length;
signal tmp_data: std_logic_vector(bin_data'range);
begin
assert (bin_data'length = gray_data'length)
report "size differs" severity failure;
process(en, gray_data, tmp_data) is
begin
if (en = '1') then
for j in 0 to (size - 2) loop
tmp_data(j) <= gray_data(j) xor tmp_data(j + 1);
end loop;
tmp_data(size - 1) <= gray_data(size - 1);
bin_data <= tmp_data;
else
bin_data <= gray_data;
end if;
end process;
end gray_to_bin_rtl;
--
-- extend sign
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
entity extend_sign is
port
(
data_len: in unsigned;
data_in: in std_logic_vector;
data_out: out std_logic_vector;
len_mask: in std_logic_vector
);
end entity;
architecture extend_sign_rtl of extend_sign is
constant size: integer := data_in'length;
signal is_signed: std_logic;
begin
process(data_in, data_len)
begin
-- fixme: modelsim fails without this check
-- synthesis translate_off
is_signed <= '0';
if data_len > 0 then
-- synthesis translate_on
is_signed <= data_in(to_integer(data_len) - 1);
-- synthesis translate_off
end if;
-- synthesis translate_on
end process;
process(is_signed, data_in)
begin
if (is_signed = '1') then
data_out <= data_in or not len_mask;
else
data_out <= data_in;
end if;
end process;
end extend_sign_rtl;
|