forked from OSVVM/OSVVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NameStorePkg.vhd
436 lines (385 loc) · 15.8 KB
/
NameStorePkg.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
--
-- File Name: NameStorePkg.vhd
-- Design Unit Name: NameStorePkg
-- Revision: STANDARD VERSION
--
-- Maintainer: Jim Lewis email: [email protected]
-- Contributor(s):
-- Jim Lewis SynthWorks
--
--
-- Package Defines
-- Data structure for name.
--
-- Developed for:
-- SynthWorks Design Inc.
-- VHDL Training Classes
-- 11898 SW 128th Ave. Tigard, Or 97223
-- http://www.SynthWorks.com
--
-- Revision History:
-- Date Version Description
-- 07/2024 2024.07 Added IsInitialized
-- 05/2024 2024.05 Calls to singleton forgot to pass ParentID and Search parameter to internal Protected type calls
-- 10/2022 2022.10 Changed enum value PRIVATE to PRIVATE_NAME due to VHDL-2019 keyword conflict.
-- 02/2022 2022.02 Updated NewID for Updated NewID and Find with ParentID and Search.
-- Supports searching in CoveragePkg, ScoreboardGenericPkg, and MemoryPkg.
-- 06/2021 2021.06 Initial revision. Derrived from NamePkg.vhd
--
--
-- This file is part of OSVVM.
--
-- Copyright (c) 2021-2022 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.
--
use std.textio.all ;
use work.ResolutionPkg.all ;
use work.AlertLogPkg.all ;
package NameStorePkg is
type NameIDType is record
ID : integer_max ;
end record NameIDType ;
alias NameStoreIDType is NameIDType ;
type NameIDArrayType is array (integer range <>) of NameIDType ;
type NameSearchType is (PRIVATE_NAME, NAME, NAME_AND_PARENT, NAME_AND_PARENT_ELSE_PRIVATE) ;
constant ID_NOT_FOUND : NameIDType := (ID => -1) ;
constant NAME_ID_UNINITIALZED : NameIdType := (ID => integer'left) ;
------------------------------------------------------------
impure function NewID (
iName : String ;
ParentID : AlertLogIdType := ALERTLOG_BASE_ID ;
Search : NameSearchType := NAME
) return NameIDType ;
------------------------------------------------------------
impure function IsInitialized (ID : NameIDType) return boolean ;
------------------------------------------------------------
procedure Set (
ID : NameIDType ;
iName : String ;
ParentID : AlertLogIdType := ALERTLOG_BASE_ID ;
Search : NameSearchType := NAME
) ;
impure function Get (ID : NameIDType ; DefaultName : string := "") return string ;
------------------------------------------------------------
impure function Find (
iName : String ;
ParentID : AlertLogIdType := ALERTLOG_BASE_ID ;
Search : NameSearchType := NAME
) return NameIDType ;
impure function GetOpt (ID : NameIDType) return string ;
impure function IsSet (ID : NameIDType) return boolean ;
procedure Clear (ID : NameIDType) ; -- clear name
procedure Deallocate(ID : NameIDType) ; -- effectively alias to clear name
------------------------------------------------------------
-- Helper function for NewID in data structures
function ResolveSearch (
UniqueParent : boolean ;
Search : NameSearchType
) return NameSearchType ;
type NameStorePType is protected
------------------------------------------------------------
impure function NewID (
iName : String ;
ParentID : AlertLogIdType := ALERTLOG_BASE_ID ;
Search : NameSearchType := NAME
) return integer ;
------------------------------------------------------------
impure function IsInitialized (ID : NameIDType) return boolean ;
------------------------------------------------------------
procedure Set (
ID : integer ;
iName : String ;
ParentID : AlertLogIdType := ALERTLOG_BASE_ID ;
Search : NameSearchType := NAME
) ;
impure function Get (ID : integer ; DefaultName : string := "") return string ;
------------------------------------------------------------
impure function Find (
iName : String ;
ParentID : AlertLogIdType := ALERTLOG_BASE_ID ;
Search : NameSearchType := NAME
) return integer ;
impure function GetOpt (ID : integer) return string ;
impure function IsSet (ID : integer) return boolean ;
procedure Clear (ID : integer) ; -- clear name
procedure Deallocate(ID : integer) ; -- effectively alias to clear name
end protected NameStorePType ;
end package NameStorePkg ;
--- ///////////////////////////////////////////////////////////////////////////
--- ///////////////////////////////////////////////////////////////////////////
--- ///////////////////////////////////////////////////////////////////////////
package body NameStorePkg is
type NameStorePType is protected body
type NameItemRecType is record
Name : Line ;
ParentID : AlertLogIDType ;
Search : NameSearchType ;
end record NameItemRecType ;
type NameArrayType is array (integer range <>) of NameItemRecType ;
type NameArrayPtrType is access NameArrayType ;
-- type LineArrayType is array (integer range <>) of Line ;
-- type LineArrayPtrType is access LineArrayType ;
-- variable NameArrayPtr : LineArrayPtrType ;
variable NameArrayPtr : NameArrayPtrType ;
variable NumItems : integer := 0 ;
-- constant MIN_NUM_ITEMS : integer := 4 ; -- Temporarily small for testing
constant MIN_NUM_ITEMS : integer := 32 ; -- Min amount to resize array
------------------------------------------------------------
-- Package Local
function NormalizeArraySize( NewNumItems, MinNumItems : integer ) return integer is
------------------------------------------------------------
variable NormNumItems : integer ;
variable ModNumItems : integer ;
begin
NormNumItems := NewNumItems ;
ModNumItems := NewNumItems mod MinNumItems ;
if ModNumItems > 0 then
NormNumItems := NormNumItems + (MinNumItems - ModNumItems) ;
end if ;
return NormNumItems ;
end function NormalizeArraySize ;
------------------------------------------------------------
-- Package Local
procedure GrowNumberItems (
------------------------------------------------------------
variable ItemArrayPtr : InOut NameArrayPtrType ;
variable NumItems : InOut integer ;
constant GrowAmount : in integer ;
constant MinNumItems : in integer
) is
variable oldItemArrayPtr : NameArrayPtrType ;
--!!Xc constant NewNumItems : integer := NumItems + GrowAmount ;
--!!Xc constant NewSize : integer := NormalizeArraySize(NewNumItems, MinNumItems) ;
variable NewNumItems : integer ;
variable NewSize : integer ;
begin
NewNumItems := NumItems + GrowAmount ;
NewSize := NormalizeArraySize(NewNumItems, MinNumItems) ;
if ItemArrayPtr = NULL then
ItemArrayPtr := new NameArrayType(1 to NewSize) ;
elsif NewNumItems > ItemArrayPtr'length then
oldItemArrayPtr := ItemArrayPtr ;
ItemArrayPtr := new NameArrayType(1 to NewSize) ;
ItemArrayPtr.all(1 to NumItems) := oldItemArrayPtr.all(1 to NumItems) ;
deallocate(oldItemArrayPtr) ;
end if ;
NumItems := NewNumItems ;
end procedure GrowNumberItems ;
------------------------------------------------------------
impure function NewID (
------------------------------------------------------------
iName : String ;
ParentID : AlertLogIdType := ALERTLOG_BASE_ID ;
Search : NameSearchType := NAME
) return integer is
begin
GrowNumberItems(NameArrayPtr, NumItems, 1, MIN_NUM_ITEMS) ;
Set(NumItems, iName, ParentID, Search) ;
return NumItems ;
end function NewID ;
------------------------------------------------------------
impure function IsInitialized (ID : NameIDType) return boolean is
------------------------------------------------------------
begin
return ID /= NAME_ID_UNINITIALZED ;
end function IsInitialized ;
------------------------------------------------------------
procedure Set (
------------------------------------------------------------
ID : integer ;
iName : String ;
ParentID : AlertLogIdType := ALERTLOG_BASE_ID ;
Search : NameSearchType := NAME
) is
begin
deallocate(NameArrayPtr(ID).Name) ;
NameArrayPtr(ID).Name := new string'(iName) ;
NameArrayPtr(ID).Search := Search ;
NameArrayPtr(ID).ParentID := ParentID ;
end procedure Set ;
------------------------------------------------------------
impure function Get (ID : integer ; DefaultName : string := "") return string is
------------------------------------------------------------
begin
if NameArrayPtr(ID).Name = NULL then
return DefaultName ;
else
return NameArrayPtr(ID).Name.all ;
end if ;
end function Get ;
------------------------------------------------------------
-- Local
impure function FindName (iName : String) return integer is
------------------------------------------------------------
begin
for ID in 1 to NumItems loop
-- skip if private
next when NameArrayPtr(ID).Search = PRIVATE_NAME ;
-- find Name
if iName = NameArrayPtr(ID).Name.all then
return ID ;
end if ;
end loop ;
return ID_NOT_FOUND.ID ;
end function FindName ;
------------------------------------------------------------
-- Local
impure function FindNameAndParent (iName : String; ParentID : AlertLogIdType) return integer is
------------------------------------------------------------
begin
for ID in 1 to NumItems loop
-- skip if private
next when NameArrayPtr(ID).Search = PRIVATE_NAME ;
-- find Name and Parent
if iName = NameArrayPtr(ID).Name.all and (ParentID = NameArrayPtr(ID).ParentID or NameArrayPtr(ID).Search = NAME) then
return ID ;
end if ;
end loop ;
return ID_NOT_FOUND.ID ;
end function FindNameAndParent ;
------------------------------------------------------------
impure function Find (
------------------------------------------------------------
iName : String ;
ParentID : AlertLogIdType := ALERTLOG_BASE_ID ;
Search : NameSearchType := NAME
) return integer is
begin
case Search is
when PRIVATE_NAME => return ID_NOT_FOUND.ID ;
when NAME => return FindName(iName) ;
when others => return FindNameAndParent(iName, ParentID) ;
end case ;
end function Find ;
------------------------------------------------------------
impure function GetOpt (ID : integer) return string is
------------------------------------------------------------
begin
if NameArrayPtr(ID).Name = NULL then
return NUL & "" ;
else
return NameArrayPtr(ID).Name.all ;
end if ;
end function GetOpt ;
------------------------------------------------------------
impure function IsSet (ID : integer) return boolean is
------------------------------------------------------------
begin
return NameArrayPtr(ID).Name /= NULL ;
end function IsSet ;
------------------------------------------------------------
procedure Clear (ID : integer) is
------------------------------------------------------------
begin
deallocate(NameArrayPtr(ID).Name) ;
end procedure Clear ;
------------------------------------------------------------
procedure Deallocate(ID : integer) is
------------------------------------------------------------
begin
Clear(ID) ;
end procedure Deallocate ;
end protected body NameStorePType ;
-- /////////////////////////////////////////
-- /////////////////////////////////////////
-- Singleton Data Structure
-- /////////////////////////////////////////
-- /////////////////////////////////////////
shared variable NameStore : NameStorePType ;
------------------------------------------------------------
impure function NewID (
------------------------------------------------------------
iName : String ;
ParentID : AlertLogIdType := ALERTLOG_BASE_ID ;
Search : NameSearchType := NAME
) return NameIDType is
variable Result : NameIDType ;
begin
Result.ID := NameStore.NewID(iName, ParentID, Search) ;
return Result ;
end function NewID ;
------------------------------------------------------------
impure function IsInitialized (ID : NameIDType) return boolean is
------------------------------------------------------------
begin
return NameStore.IsInitialized(ID) ;
end function IsInitialized ;
------------------------------------------------------------
procedure Set (
------------------------------------------------------------
ID : NameIDType ;
iName : String ;
ParentID : AlertLogIdType := ALERTLOG_BASE_ID ;
Search : NameSearchType := NAME
) is
begin
NameStore.set(ID.ID, iName, ParentID, Search) ;
end procedure Set ;
------------------------------------------------------------
impure function Get (ID : NameIDType ; DefaultName : string := "") return string is
------------------------------------------------------------
begin
return NameStore.Get(ID.ID, DefaultName) ;
end function Get ;
------------------------------------------------------------
impure function Find (
------------------------------------------------------------
iName : String ;
ParentID : AlertLogIdType := ALERTLOG_BASE_ID ;
Search : NameSearchType := NAME
) return NameIDType is
begin
return NameIDType'(ID => NameStore.Find(iName, ParentID, Search)) ;
end function Find ;
------------------------------------------------------------
impure function GetOpt (ID : NameIDType) return string is
------------------------------------------------------------
begin
return NameStore.Get(ID.ID) ;
end function GetOpt ;
------------------------------------------------------------
impure function IsSet (ID : NameIDType) return boolean is
------------------------------------------------------------
begin
return NameStore.IsSet(ID.ID) ;
end function IsSet ;
------------------------------------------------------------
procedure Clear (ID : NameIDType) is
------------------------------------------------------------
begin
NameStore.Clear(ID.ID) ;
end procedure Clear ;
------------------------------------------------------------
procedure Deallocate(ID : NameIDType) is
------------------------------------------------------------
begin
NameStore.Clear(ID.ID) ;
end procedure Deallocate ;
------------------------------------------------------------
-- Helper function for NewID in data structures
function ResolveSearch (
------------------------------------------------------------
UniqueParent : boolean ;
Search : NameSearchType
) return NameSearchType is
variable result : NameSearchType ;
begin
if search = NAME_AND_PARENT_ELSE_PRIVATE then
result := NAME_AND_PARENT when UniqueParent else PRIVATE_NAME ;
else
result := Search ;
end if ;
return result ;
end function ResolveSearch ;
end package body NameStorePkg ;