forked from OSVVM/OSVVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResizePkg.vhd
242 lines (207 loc) · 10.1 KB
/
ResizePkg.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
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
240
241
242
--
-- File Name: ResizePkg.vhd
-- Design Unit Name: ResizePkg
-- Revision: STANDARD VERSION
--
-- Maintainer: Jim Lewis email: [email protected]
-- Contributor(s):
-- Jim Lewis email: [email protected]
--
-- Package Defines
-- Resizing for transaction records
--
-- Developed for:
-- SynthWorks Design Inc.
-- VHDL Training Classes
-- 11898 SW 128th Ave. Tigard, Or 97223
-- http://www.SynthWorks.com
--
-- Revision History:
-- Date Version Description
-- 03/2024 2024.03 Added AlertLogID to SafeResize
-- 06/2021 2021.06 Refactored from ResolutionPkg
--
--
-- This file is part of OSVVM.
--
-- Copyright (c) 2005 - 2024 by SynthWorks Design Inc.
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
library ieee ;
use ieee.std_logic_1164.all ;
use ieee.numeric_std.all ;
use work.AlertLogPkg.all ;
use work.ResolutionPkg.all ;
package ResizePkg is
------------------------------------------------------------
-- SafeResize
-- Convert and resize to/from std_logic_vector and std_logic_vector_max_c
-- Generate an error if the value changes on downsize (for unsigned, throw away a 1)
--
impure function SafeResize(ID : AlertLogIDType ; A : std_logic_vector; Size : natural) return std_logic_vector ;
impure function SafeResize(ID : AlertLogIDType ; A : std_logic_vector; Size : natural) return std_logic_vector_max_c ;
impure function SafeResize(ID : AlertLogIDType ; A : std_logic_vector_max_c; Size : natural) return std_logic_vector ;
impure function SafeResize(A : std_logic_vector; Size : natural) return std_logic_vector ;
impure function SafeResize(A : std_logic_vector; Size : natural) return std_logic_vector_max_c ;
impure function SafeResize(A : std_logic_vector_max_c; Size : natural) return std_logic_vector ;
------------------------------------------------------------
-- Extend (upsize) and Reduce (downsize)
function Extend(A: std_logic_vector; Size : natural) return std_logic_vector ;
function Reduce(A: std_logic_vector; Size : natural) return std_logic_vector ;
------------------------------------------------------------
-- Deprecated: ToTransaction/FromTransaction - Deprecated by SafeResize
function ToTransaction(A : std_logic_vector) return std_logic_vector_max_c ;
impure function ToTransaction(A : std_logic_vector ; Size : natural) return std_logic_vector_max_c ;
function ToTransaction(A : integer; Size : natural) return std_logic_vector_max_c ;
function FromTransaction (A: std_logic_vector_max_c) return std_logic_vector ;
impure function FromTransaction (A: std_logic_vector_max_c ; Size : natural) return std_logic_vector ;
function FromTransaction (A: std_logic_vector_max_c) return integer ;
------------------------------------------------------------
-- Deprecated: ToTransaction/FromTransaction - Provided for symmetry - Deprecated by SafeResize
function ToTransaction(A : std_logic_vector) return std_logic_vector ; -- Symmetry
impure function ToTransaction(A : std_logic_vector ; Size : natural) return std_logic_vector ;
function ToTransaction(A : integer; Size : natural) return std_logic_vector ;
function FromTransaction (A: std_logic_vector) return std_logic_vector ; -- Symmetry
impure function FromTransaction (A: std_logic_vector ; Size : natural) return std_logic_vector ;
function FromTransaction (A: std_logic_vector) return integer ;
end package ResizePkg ;
package body ResizePkg is
------------------------------------------------------------
-- SafeResize
-- Convert and resize to/from std_logic_vector and std_logic_vector_max_c
-- Generate an error if the value changes on downsize (for unsigned, throw away a 1)
--
------------------------------------------------------------
impure function LocalSafeResize(A : std_logic_vector; Size : natural; ID : AlertLogIDType := ALERT_DEFAULT_ID) return std_logic_vector is
------------------------------------------------------------
variable Result : std_logic_vector(Size-1 downto 0) := (others => '0') ;
alias aA : std_logic_vector(A'length-1 downto 0) is A ;
begin
if A'length <= Size then
-- Extend A with '0' (see initialization)
Result(A'length-1 downto 0) := aA ;
else
-- Reduce A and Error if any extra bits of A are a '1'
Result := aA(Size-1 downto 0) ;
AlertIf(ID, (OR aA(A'length-1 downto Size) = '1'),
"SafeResize: value changed on resize. Original value: " & to_hstring(A) &
", Resized value: " & to_hstring(Result) ) ;
end if ;
return Result ;
end function LocalSafeResize ;
------------------------------------------------------------
impure function SafeResize(ID : AlertLogIDType ; A : std_logic_vector; Size : natural) return std_logic_vector is
------------------------------------------------------------
begin
return LocalSafeResize(A, Size, ID) ;
end function SafeResize ;
------------------------------------------------------------
impure function SafeResize(ID : AlertLogIDType ; A : std_logic_vector; Size : natural) return std_logic_vector_max_c is
------------------------------------------------------------
begin
return std_logic_vector_max_c(LocalSafeResize(A, Size, ID)) ;
end function SafeResize ;
------------------------------------------------------------
impure function SafeResize(ID : AlertLogIDType ; A : std_logic_vector_max_c; Size : natural) return std_logic_vector is
------------------------------------------------------------
begin
return LocalSafeResize(std_logic_vector(A), Size, ID) ;
end function SafeResize ;
------------------------------------------------------------
impure function SafeResize(A : std_logic_vector; Size : natural) return std_logic_vector is
------------------------------------------------------------
begin
return LocalSafeResize(A, Size) ;
end function SafeResize ;
------------------------------------------------------------
impure function SafeResize(A : std_logic_vector; Size : natural) return std_logic_vector_max_c is
------------------------------------------------------------
begin
return std_logic_vector_max_c(LocalSafeResize(A, Size)) ;
end function SafeResize ;
------------------------------------------------------------
impure function SafeResize(A : std_logic_vector_max_c; Size : natural) return std_logic_vector is
------------------------------------------------------------
begin
return LocalSafeResize(std_logic_vector(A), Size) ;
end function SafeResize ;
------------------------------------------------------------
-- Extend (upsize) and Reduce (downsize)
function Extend(A: std_logic_vector; Size : natural) return std_logic_vector is
------------------------------------------------------------
variable extA : std_logic_vector(Size downto 1) := (others => '0') ;
begin
extA(A'length downto 1) := A ;
return extA ;
end function Extend ;
------------------------------------------------------------
function Reduce(A: std_logic_vector; Size : natural) return std_logic_vector is
------------------------------------------------------------
alias aA : std_logic_vector(A'length-1 downto 0) is A ;
begin
return aA(Size-1 downto 0) ;
end function Reduce ;
------------------------------------------------------------
-- Deprecated: ToTransaction/FromTransaction - Deprecated by SafeResize
function ToTransaction(A : std_logic_vector) return std_logic_vector_max_c is
begin
return std_logic_vector_max_c(A) ;
end function ToTransaction ;
impure function ToTransaction(A : std_logic_vector ; Size : natural) return std_logic_vector_max_c is
begin
return std_logic_vector_max_c(LocalSafeResize(A, Size)) ;
end function ToTransaction ;
function ToTransaction(A : integer; Size : natural) return std_logic_vector_max_c is
begin
return std_logic_vector_max_c(to_signed(A, Size)) ;
end function ToTransaction ;
function FromTransaction (A: std_logic_vector_max_c) return std_logic_vector is
begin
return std_logic_vector(A) ;
end function FromTransaction ;
impure function FromTransaction (A: std_logic_vector_max_c ; Size : natural) return std_logic_vector is
begin
return LocalSafeResize(std_logic_vector(A), Size) ;
end function FromTransaction ;
function FromTransaction (A: std_logic_vector_max_c) return integer is
begin
return to_integer(signed(A)) ;
end function FromTransaction ;
------------------------------------------------------------
-- Deprecated: ToTransaction/FromTransaction - Provided for symmetry - Deprecated by SafeResize
function ToTransaction(A : std_logic_vector) return std_logic_vector is
begin
return A ;
end function ToTransaction ;
impure function ToTransaction(A : std_logic_vector ; Size : natural) return std_logic_vector is
begin
return LocalSafeResize(A, Size) ;
end function ToTransaction ;
function ToTransaction(A : integer; Size : natural) return std_logic_vector is
begin
return std_logic_vector(to_signed(A, Size)) ;
end function ToTransaction ;
function FromTransaction (A: std_logic_vector) return std_logic_vector is
begin
return A ;
end function FromTransaction ;
impure function FromTransaction (A: std_logic_vector ; Size : natural) return std_logic_vector is
begin
return LocalSafeResize(A, Size) ;
end function FromTransaction ;
function FromTransaction (A: std_logic_vector) return integer is
begin
return to_integer(signed(A)) ;
end function FromTransaction ;
end package body ResizePkg ;