forked from OSVVM/OSVVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryGenericPkg.vhd
1603 lines (1441 loc) · 62.9 KB
/
MemoryGenericPkg.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- File Name: MemoryGenericPkg.vhd
-- Design Unit Name: MemoryGenericPkg
-- Revision: STANDARD VERSION
--
-- Maintainer: Jim Lewis email: jim@synthworks.com
-- Contributor(s):
-- Jim Lewis email: jim@synthworks.com
--
-- Description
-- Package defines a protected type, MemoryPType, and methods
-- for efficiently implementing memory data structures
--
-- Developed for:
-- SynthWorks Design Inc.
-- VHDL Training Classes
-- 11898 SW 128th Ave. Tigard, Or 97223
-- http://www.SynthWorks.com
--
-- Revision History:
-- Date Version Description
-- 09/2024 2024.09 Updated reporting for integer'high and integer'low
-- 07/2024 2024.07 Throw Errors on Address > 41 and warnings if Address > 38
-- Added IsInitialized
-- 01/2023 2023.01 Updated address checks in MemRead and MemWrite
-- 11/2022 2022.11 Updated default search to PRIVATE_NAME
-- 08/2022 2022.08 Refactored and added generics for base type
-- 02/2022 2022.02 Updated NewID with ReportMode, Search, PrintParent. Supports searching for Memory models.
-- 06/2021 2021.06 Updated Data Structure, IDs for new use model, and Wrapper Subprograms
-- 01/2020 2020.01 Updated Licenses to Apache
-- 11/2016 2016.11 Refinement to MemRead to return value, X (if X), U (if not initialized)
-- 01/2016 2016.01 Update for buf.all(buf'left)
-- 06/2015 2015.06 Updated for Alerts, ...
-- ... ... Numerous revisions for VHDL Testbenches and Verification
-- 05/2005 0.1 Initial revision
--
--
-- This file is part of OSVVM.
--
-- Copyright (c) 2005 - 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 ;
library IEEE ;
use IEEE.std_logic_1164.all ;
use IEEE.numeric_std.all ;
use IEEE.numeric_std_unsigned.all ;
use IEEE.math_real.all ;
use work.TextUtilPkg.all ;
use work.TranscriptPkg.all ;
use work.AlertLogPkg.all ;
use work.NameStorePkg.all ;
use work.ResolutionPkg.all ;
-- Temporary workaround for MemoryBaseType
use work.MemorySupportPkg.MemoryBaseType ;
package MemoryGenericPkg is
generic (
-- type MemoryBaseType ;
function SizeMemoryBaseType(Size : integer) return integer ; -- is <> ;
function ToMemoryBaseType (A : std_logic_vector ; Size : integer) return MemoryBaseType ; -- is <> ;
function FromMemoryBaseType(A : MemoryBaseType ; Size : integer) return std_logic_vector ; -- is <> ;
function InitMemoryBaseType(Size : integer) return MemoryBaseType -- is <>
) ;
type MemoryIDType is record
ID : integer_max ;
end record MemoryIDType ;
constant MEMORY_ID_UNINITIALZED : MemoryIdType := (ID => integer'left) ;
type MemoryIDArrayType is array (integer range <>) of MemoryIDType ;
constant OSVVM_MEMORY_ALERTLOG_ID : AlertLogIDType := OSVVM_ALERTLOG_ID ;
------------------------------------------------------------
impure function NewID (
Name : String ;
AddrWidth : integer ;
DataWidth : integer ;
ParentID : AlertLogIDType := OSVVM_MEMORY_ALERTLOG_ID ;
ReportMode : AlertLogReportModeType := ENABLED ;
Search : NameSearchType := PRIVATE_NAME ;
PrintParent : AlertLogPrintParentType := PRINT_NAME_AND_PARENT
) return MemoryIDType ;
impure function IsInitialized (ID : MemoryIDType) return boolean ;
------------------------------------------------------------
procedure MemWrite (
ID : MemoryIDType ;
Addr : std_logic_vector ;
Data : std_logic_vector
) ;
alias Write is MemWrite [MemoryIDType, std_logic_vector, std_logic_vector ] ;
procedure MemRead (
ID : in MemoryIDType ;
Addr : in std_logic_vector ;
Data : out std_logic_vector
) ;
alias Read is MemRead [MemoryIDType, std_logic_vector, std_logic_vector ] ;
impure function MemRead (
ID : MemoryIDType ;
Addr : std_logic_vector
) return std_logic_vector ;
alias Read is MemRead [MemoryIDType, std_logic_vector return std_logic_vector ] ;
------------------------------------------------------------
procedure MemErase (ID : in MemoryIDType);
procedure deallocate (ID : in MemoryIDType) ;
procedure MemoryPkgDeallocate ;
------------------------------------------------------------
impure function GetAlertLogID (ID : in MemoryIDType) return AlertLogIDType ;
------------------------------------------------------------
procedure FileReadH ( -- Hexadecimal File Read
ID : MemoryIDType ;
FileName : string ;
StartAddr : std_logic_vector ;
EndAddr : std_logic_vector
) ;
procedure FileReadH (
ID : MemoryIDType ;
FileName : string ;
StartAddr : std_logic_vector
) ;
procedure FileReadH (
ID : MemoryIDType ;
FileName : string
) ;
------------------------------------------------------------
procedure FileReadB ( -- Binary File Read
ID : MemoryIDType ;
FileName : string ;
StartAddr : std_logic_vector ;
EndAddr : std_logic_vector
) ;
procedure FileReadB (
ID : MemoryIDType ;
FileName : string ;
StartAddr : std_logic_vector
) ;
procedure FileReadB (
ID : MemoryIDType ;
FileName : string
) ;
------------------------------------------------------------
procedure FileWriteH ( -- Hexadecimal File Write
ID : MemoryIDType ;
FileName : string ;
StartAddr : std_logic_vector ;
EndAddr : std_logic_vector
) ;
procedure FileWriteH (
ID : MemoryIDType ;
FileName : string ;
StartAddr : std_logic_vector
) ;
procedure FileWriteH (
ID : MemoryIDType ;
FileName : string
) ;
------------------------------------------------------------
procedure FileWriteB ( -- Binary File Write
ID : MemoryIDType ;
FileName : string ;
StartAddr : std_logic_vector ;
EndAddr : std_logic_vector
) ;
procedure FileWriteB (
ID : MemoryIDType ;
FileName : string ;
StartAddr : std_logic_vector
) ;
procedure FileWriteB (
ID : MemoryIDType ;
FileName : string
) ;
type MemoryPType is protected
------------------------------------------------------------
impure function NewID (
Name : String ;
AddrWidth : integer ;
DataWidth : integer ;
ParentID : AlertLogIDType := OSVVM_MEMORY_ALERTLOG_ID ;
ReportMode : AlertLogReportModeType := ENABLED ;
Search : NameSearchType := PRIVATE_NAME ;
PrintParent : AlertLogPrintParentType := PRINT_NAME_AND_PARENT
) return integer ;
impure function IsInitialized (ID : MemoryIDType) return boolean ;
------------------------------------------------------------
procedure MemWrite (
ID : integer ;
Addr : std_logic_vector ;
Data : std_logic_vector
) ;
procedure MemRead (
ID : in integer ;
Addr : in std_logic_vector ;
Data : out std_logic_vector
) ;
impure function MemRead (
ID : integer ;
Addr : std_logic_vector
) return std_logic_vector ;
------------------------------------------------------------
procedure MemErase (ID : integer) ;
impure function GetAlertLogID (ID : integer) return AlertLogIDType ;
------------------------------------------------------------
procedure FileReadH ( -- Hexadecimal File Read
ID : integer ;
FileName : string ;
StartAddr : std_logic_vector ;
EndAddr : std_logic_vector
) ;
procedure FileReadH (
ID : integer ;
FileName : string ;
StartAddr : std_logic_vector
) ;
procedure FileReadH (
ID : integer ;
FileName : string
) ;
------------------------------------------------------------
procedure FileReadB ( -- Binary File Read
ID : integer ;
FileName : string ;
StartAddr : std_logic_vector ;
EndAddr : std_logic_vector
) ;
procedure FileReadB (
ID : integer ;
FileName : string ;
StartAddr : std_logic_vector
) ;
procedure FileReadB (
ID : integer ;
FileName : string
) ;
------------------------------------------------------------
procedure FileWriteH ( -- Hexadecimal File Write
ID : integer ;
FileName : string ;
StartAddr : std_logic_vector ;
EndAddr : std_logic_vector
) ;
procedure FileWriteH (
ID : integer ;
FileName : string ;
StartAddr : std_logic_vector
) ;
procedure FileWriteH (
ID : integer ;
FileName : string
) ;
------------------------------------------------------------
procedure FileWriteB ( -- Binary File Write
ID : integer ;
FileName : string ;
StartAddr : std_logic_vector ;
EndAddr : std_logic_vector
) ;
procedure FileWriteB (
ID : integer ;
FileName : string ;
StartAddr : std_logic_vector
) ;
procedure FileWriteB (
ID : integer ;
FileName : string
) ;
------------------------------------------------------------
-- Destroys the entire data structure
-- Usage: At the end of the simulation to remove all
-- memory used by data structure.
-- Note, a normal simulator does this for you.
-- You only need this if the simulator is broken.
procedure deallocate (ID : integer) ;
procedure deallocate ;
------------------------------------------------------------
-- /////////////////////////////////////////
-- Historical Interface
-- In the new implementation, these use index 1.
-- These are for backward compatibility support
--
-- /////////////////////////////////////////
------------------------------------------------------------
procedure MemInit ( AddrWidth, DataWidth : in integer ) ;
------------------------------------------------------------
procedure MemWrite ( Addr, Data : in std_logic_vector ) ;
------------------------------------------------------------
procedure MemRead (
Addr : in std_logic_vector ;
Data : out std_logic_vector
) ;
impure function MemRead ( Addr : std_logic_vector ) return std_logic_vector ;
------------------------------------------------------------
procedure MemErase ;
------------------------------------------------------------
procedure SetAlertLogID (A : AlertLogIDType) ;
procedure SetAlertLogID (Name : string ; ParentID : AlertLogIDType := OSVVM_MEMORY_ALERTLOG_ID ; CreateHierarchy : Boolean := TRUE) ;
impure function GetAlertLogID return AlertLogIDType ;
------------------------------------------------------------
procedure FileReadH ( -- Hexadecimal File Read
FileName : string ;
StartAddr : std_logic_vector ;
EndAddr : std_logic_vector
) ;
procedure FileReadH (FileName : string ; StartAddr : std_logic_vector) ;
procedure FileReadH (FileName : string) ;
------------------------------------------------------------
procedure FileReadB ( -- Binary File Read
FileName : string ;
StartAddr : std_logic_vector ;
EndAddr : std_logic_vector
) ;
procedure FileReadB (FileName : string ; StartAddr : std_logic_vector) ;
procedure FileReadB (FileName : string) ;
------------------------------------------------------------
procedure FileWriteH ( -- Hexadecimal File Write
FileName : string ;
StartAddr : std_logic_vector ;
EndAddr : std_logic_vector
) ;
procedure FileWriteH (FileName : string ; StartAddr : std_logic_vector) ;
procedure FileWriteH (FileName : string) ;
------------------------------------------------------------
procedure FileWriteB ( -- Binary File Write
FileName : string ;
StartAddr : std_logic_vector ;
EndAddr : std_logic_vector
) ;
procedure FileWriteB (FileName : string ; StartAddr : std_logic_vector) ;
procedure FileWriteB (FileName : string) ;
end protected MemoryPType ;
end MemoryGenericPkg ;
package body MemoryGenericPkg is
constant BLOCK_WIDTH : integer := 10 ;
constant WARNING_AT_ADDRESS_WIDTH : integer := BLOCK_WIDTH + 24 ; -- 16 M Byte array of pointers
constant MAXIMUM_ADDRESS_WIDTH : integer := BLOCK_WIDTH + 30 ; -- 1 G Byte Array of pointers - Maximum size supported by type integer
type MemoryPType is protected body
type MemBlockType is array (integer range <>) of MemoryBaseType ;
type MemBlockPtrType is access MemBlockType ;
type MemArrayType is array (integer range <>) of MemBlockPtrType ;
type MemArrayPtrType is access MemArrayType ;
type FileFormatType is (BINARY, HEX) ;
type MemStructType is record
MemArrayPtr : MemArrayPtrType ;
AddrWidth : integer ;
DataWidth : natural ;
BlockWidth : natural ;
MemoryBaseTypeWidth : natural ;
AlertLogID : AlertLogIDType ;
end record MemStructType ;
-- New Structure
type ItemArrayType is array (integer range <>) of MemStructType ;
type ItemArrayPtrType is access ItemArrayType ;
variable Template : ItemArrayType(1 to 1) := (1 => (NULL, -1, 1, 0, 0, OSVVM_MEMORY_ALERTLOG_ID)) ; -- Work around for QS 2020.04 and 2021.02
variable MemStructPtr : ItemArrayPtrType := new ItemArrayType'(Template) ;
constant MIN_INDEX : integer := 1 ;
constant PT_ID : integer := MIN_INDEX ;
variable NumItems : integer := 0 ;
-- constant NUM_ITEMS_TO_ALLOCATE : integer := 4 ; -- Temporarily small for testing
constant NUM_ITEMS_TO_ALLOCATE : integer := 32 ; -- Min amount to resize array
variable LocalNameStore : NameStorePType ;
------------------------------------------------------------
-- Package Local
function NormalizeArraySize( NewNumItems, MinNumItems : integer ) return integer is
------------------------------------------------------------
variable NormNumItems : integer := NewNumItems ;
variable ModNumItems : integer := 0;
begin
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 ItemArrayPtrType ;
variable NumItems : InOut integer ;
constant GrowAmount : in integer ;
-- constant NewNumItems : in integer ;
-- constant CurNumItems : in integer ;
constant MinNumItems : in integer
) is
variable oldItemArrayPtr : ItemArrayPtrType ;
variable NewNumItems : integer ;
begin
NewNumItems := NumItems + GrowAmount ;
-- Array Allocated in declaration to have a single item, but no items (historical mode)
-- if ItemArrayPtr = NULL then
-- ItemArrayPtr := new ItemArrayType(1 to NormalizeArraySize(NewNumItems, MinNumItems)) ;
-- elsif NewNumItems > ItemArrayPtr'length then
if NewNumItems > ItemArrayPtr'length then
oldItemArrayPtr := ItemArrayPtr ;
ItemArrayPtr := new ItemArrayType(1 to NormalizeArraySize(NewNumItems, MinNumItems)) ;
ItemArrayPtr.all(1 to NumItems) := ItemArrayType'(oldItemArrayPtr.all(1 to NumItems)) ;
deallocate(oldItemArrayPtr) ;
end if ;
NumItems := NewNumItems ;
end procedure GrowNumberItems ;
------------------------------------------------------------
-- PT Local
function MaximumAddressWidth (AddrWidth : integer) return integer is
------------------------------------------------------------
begin
return minimum(AddrWidth, MAXIMUM_ADDRESS_WIDTH) ;
end function MaximumAddressWidth ;
------------------------------------------------------------
-- PT Local
procedure MemInit (ID : integer ; AddrWidth, DataWidth : integer ) is
------------------------------------------------------------
constant ADJ_BLOCK_WIDTH : integer := minimum(BLOCK_WIDTH, AddrWidth) ;
constant ADJ_ADDR_WDITH : integer := MaximumAddressWidth(AddrWidth) ;
constant ADJ_ARRAY_OF_POINTERS_WIDTH : integer := ADJ_ADDR_WDITH - ADJ_BLOCK_WIDTH ;
begin
if AddrWidth <= 0 then
Alert(MemStructPtr(ID).AlertLogID, "MemoryPkg.MemInit/NewID. AddrWidth = " & to_string(AddrWidth) & " must be > 0.", FAILURE) ;
return ;
end if ;
if AddrWidth >= WARNING_AT_ADDRESS_WIDTH then
-- Array of pointers > 64 M words
log(MemStructPtr(ID).AlertLogID, "MemoryPkg.NewID(MemInit): Requested AddrWidth = " & to_string(AddrWidth) & ".") ;
log(MemStructPtr(ID).AlertLogID, "MemoryPkg.NewID(MemInit): Internally this creates an array sized 2**" & to_string(ADJ_ARRAY_OF_POINTERS_WIDTH) & ".") ;
log(MemStructPtr(ID).AlertLogID, "MemoryPkg.NewID(MemInit): Memories this large may result in poor simulation performance.") ;
log(MemStructPtr(ID).AlertLogID, "MemoryPkg.NewID(MemInit): If you need a memory this large, be sure to file an issue on GitHub/OSVVM/OsvvmLibraries or osvvm.org.") ;
if AddrWidth > ADJ_ADDR_WDITH then
Alert(MemStructPtr(ID).AlertLogID, "MemoryPkg.NewID(MemInit): Requested AddrWidth = " & to_string(AddrWidth) &
" was truncated to " & to_string(ADJ_ADDR_WDITH) & ".", ERROR) ;
else
Alert(MemStructPtr(ID).AlertLogID, "MemoryPkg.NewID(MemInit): Requested AddrWidth = " & to_string(AddrWidth) & " is large and may slow simulation.", WARNING) ;
end if ;
end if ;
-- if DataWidth <= 0 or DataWidth > 31 then
-- Alert(MemStructPtr(ID).AlertLogID, "MemoryPkg.MemInit/NewID. DataWidth = " & to_string(DataWidth) & " must be > 0 and <= 31.", FAILURE) ;
if DataWidth <= 0 then
Alert(MemStructPtr(ID).AlertLogID, "MemoryPkg.MemInit/NewID. DataWidth = " & to_string(DataWidth) & " must be > 0 ", FAILURE) ;
return ;
end if ;
MemStructPtr(ID).AddrWidth := ADJ_ADDR_WDITH ;
MemStructPtr(ID).DataWidth := DataWidth ;
MemStructPtr(ID).MemoryBaseTypeWidth := SizeMemoryBaseType(DataWidth) ;
MemStructPtr(ID).BlockWidth := ADJ_BLOCK_WIDTH ;
if ADJ_ARRAY_OF_POINTERS_WIDTH < 31 then
MemStructPtr(ID).MemArrayPtr := new MemArrayType(0 to 2**(ADJ_ARRAY_OF_POINTERS_WIDTH)-1) ;
else
-- with 32 bit signed numbers, formulating 2**31-1 can be interesting.
MemStructPtr(ID).MemArrayPtr := new MemArrayType(0 to (2**30-1) + 2**30) ;
end if ;
end procedure MemInit ;
------------------------------------------------------------
impure function NewID (
------------------------------------------------------------
Name : String ;
AddrWidth : integer ;
DataWidth : integer ;
ParentID : AlertLogIDType := OSVVM_MEMORY_ALERTLOG_ID ;
ReportMode : AlertLogReportModeType := ENABLED ;
Search : NameSearchType := PRIVATE_NAME ;
PrintParent : AlertLogPrintParentType := PRINT_NAME_AND_PARENT
) return integer is
variable NameID : integer ;
variable ResolvedSearch : NameSearchType ;
variable ResolvedPrintParent : AlertLogPrintParentType ;
begin
ResolvedSearch := ResolveSearch (ParentID /= OSVVM_MEMORY_ALERTLOG_ID, Search) ;
ResolvedPrintParent := ResolvePrintParent(ParentID /= OSVVM_MEMORY_ALERTLOG_ID, PrintParent) ;
NameID := LocalNameStore.find(Name, ParentID, ResolvedSearch) ;
-- Share the memory if they match
if NameID /= ID_NOT_FOUND.ID then
if MemStructPtr(NameID).MemArrayPtr /= NULL then
-- Found ID and structure exists, does structure match?
AlertIf(MemStructPtr(NameID).AlertLogID, MaximumAddressWidth(AddrWidth) /= MemStructPtr(NameID).AddrWidth,
"NewID: AddrWidth: " & to_string(MaximumAddressWidth(AddrWidth)) & " /= Existing AddrWidth: " & to_string(MemStructPtr(NameID).AddrWidth), FAILURE);
AlertIf(MemStructPtr(NameID).AlertLogID, DataWidth /= MemStructPtr(NameID).DataWidth,
"NewID: DataWidth: " & to_string(DataWidth) & " /= Existing DataWidth: " & to_string(MemStructPtr(NameID).DataWidth), FAILURE);
-- NameStore IDs are issued sequentially and match MemoryID
else
-- Found ID and structure does not exist, Reconstruct Memory
MemInit(NameID, AddrWidth, DataWidth) ;
end if ;
return NameID ;
else
-- Add New Memory to Structure
GrowNumberItems(MemStructPtr, NumItems, GrowAmount => 1, MinNumItems => NUM_ITEMS_TO_ALLOCATE) ;
-- Create AlertLogID
MemStructPtr(NumItems).AlertLogID := NewID(Name, ParentID, ReportMode, ResolvedPrintParent, CreateHierarchy => FALSE) ;
-- Construct Memory, Reports agains AlertLogID
MemInit(NumItems, AddrWidth, DataWidth) ;
-- Add item to NameStore
NameID := LocalNameStore.NewID(Name, ParentID, ResolvedSearch) ;
-- Check NameStore Index vs MemoryIndex
AlertIfNotEqual(MemStructPtr(NumItems).AlertLogID, NameID, NumItems, "MemoryStore, Check Index of LocalNameStore matches MemoryID") ;
return NumItems ;
end if ;
end function NewID ;
------------------------------------------------------------
impure function IsInitialized (ID : MemoryIDType) return boolean is
------------------------------------------------------------
begin
return ID /= MEMORY_ID_UNINITIALZED ;
end function IsInitialized ;
------------------------------------------------------------
-- PT Local
impure function IdOutOfRange(
------------------------------------------------------------
constant ID : in integer ;
constant Name : in string
) return boolean is
begin
if ID < MIN_INDEX or ID > MemStructPtr'High then
if ID = integer'left then
Alert(OSVVM_MEMORY_ALERTLOG_ID, "MemoryPkg." & Name & " ID not initialized yet. " &
"Either a call to NewID or wait for 0 ns (to allow for signal update) is needed. ",
FAILURE ) ;
else
Alert(OSVVM_MEMORY_ALERTLOG_ID,
"MemoryPkg." & Name & " ID: " & to_string_max(ID) &
" is not in the range (" & to_string(MIN_INDEX) &
" to " & to_string(MemStructPtr'High) & ")",
FAILURE ) ;
end if ;
return TRUE ;
else
-- valid ID
return FALSE ;
end if;
end function IdOutOfRange ;
------------------------------------------------------------
-- Local
-- This is a temporary solution that works around GHDL issues
function InitMemoryBlockType(BlockWidth, BaseWidth : integer) return MemBlockType is
------------------------------------------------------------
-- This keeps MemoryBaseType from being a generic type
constant BaseU : MemoryBaseType(BaseWidth-1 downto 0) := InitMemoryBaseType(BaseWidth) ;
--!! GHDL Bug constant BaseU : MemoryBaseType := InitMemoryBaseType(BaseWidth) ;
begin
return MemBlockType'(0 to 2**BlockWidth-1 => BaseU) ;
end function InitMemoryBlockType ;
------------------------------------------------------------
procedure MemWrite (
------------------------------------------------------------
ID : integer ;
Addr : std_logic_vector ;
Data : std_logic_vector
) is
variable BlockWidth, AddrWidth : integer ;
variable MemoryBaseWidth : integer ;
-- constant BlockWidth : integer := MemStructPtr(ID).BlockWidth;
variable BlockAddr, WordAddr : integer ;
alias aAddr : std_logic_vector (Addr'length-1 downto 0) is Addr ;
-- subtype MemBlockSubType is MemBlockType(0 to 2**BlockWidth-1) ;
variable MemArrayPtr : MemArrayPtrType ;
begin
if IdOutOfRange(ID, "MemWrite") then
return ;
end if ;
AddrWidth := MemStructPtr(ID).AddrWidth ;
MemArrayPtr := MemStructPtr(ID).MemArrayPtr ;
-- Check Bounds of Address and if memory is initialized
if Addr'length > AddrWidth then
if (MemArrayPtr = NULL) then -- ONLY PT since if ID in range, then MemInit called
Alert(MemStructPtr(ID).AlertLogID, "MemoryPkg.MemWrite: Memory not initialized, Write Ignored.", FAILURE) ;
return ;
elsif aAddr(aAddr'left downto AddrWidth) /= 0 then
Alert(MemStructPtr(ID).AlertLogID, "MemoryPkg.MemWrite: Address value " & to_hxstring(Addr) & " goes beyond memory address width: " & to_string(MemStructPtr(ID).AddrWidth), FAILURE) ;
return ;
end if ;
end if ;
-- Check Bounds on Data
if Data'length /= MemStructPtr(ID).DataWidth then
Alert(MemStructPtr(ID).AlertLogID, "MemoryPkg.MemWrite: Data'length: " & to_string(Data'length) & " /= Memory Data Width: " & to_string(MemStructPtr(ID).DataWidth), FAILURE) ;
return ;
end if ;
if is_X( Addr ) then
Alert(MemStructPtr(ID).AlertLogID, "MemoryPkg.MemWrite: Address X, Write Ignored.") ;
return ;
end if ;
BlockWidth := MemStructPtr(ID).BlockWidth ;
-- Slice out upper address to form block address
if aAddr'high >= BlockWidth then
BlockAddr := to_integer(aAddr(aAddr'high downto BlockWidth)) ;
else
BlockAddr := 0 ;
end if ;
MemoryBaseWidth := MemStructPtr(ID).MemoryBaseTypeWidth ;
-- If empty, allocate a memory block
if (MemArrayPtr(BlockAddr) = NULL) then
MemArrayPtr(BlockAddr) := new
MemBlockType'(InitMemoryBlockType(BlockWidth, MemoryBaseWidth)) ;
-- Long term, we need the first one to allow transition of MemoryBaseType to a generic.
--!! GHDL Bug MemStructPtr(ID).MemArrayPtr(BlockAddr) := new
--!! GHDL Bug MemBlockType'(0 to 2**BlockWidth-1 => InitMemoryBaseType(MemoryBaseWidth) ) ;
-- MemStructPtr(ID).MemArrayPtr(BlockAddr) := new
-- MemBlockType(0 to 2**BlockWidth-1)(MemoryBaseWidth-1 downto 0) ;
--!! GHDL Bug MemStructPtr(ID).MemArrayPtr(BlockAddr).all := (0 to 2**BlockWidth-1 => InitMemoryBaseType(MemoryBaseWidth));
end if ;
-- Address of a word within a block
WordAddr := to_integer(aAddr(BlockWidth -1 downto 0)) ;
-- Write to BlockAddr, WordAddr
MemArrayPtr(BlockAddr)(WordAddr) := ToMemoryBaseType(Data, MemoryBaseWidth) ;
end procedure MemWrite ;
------------------------------------------------------------
procedure MemRead (
------------------------------------------------------------
ID : in integer ;
Addr : in std_logic_vector ;
Data : out std_logic_vector
) is
variable BlockWidth, AddrWidth : integer ;
variable BlockAddr, WordAddr : integer ;
alias aAddr : std_logic_vector (Addr'length-1 downto 0) is Addr ;
begin
Data := (Data'range => 'U') ; -- Error return value
if IdOutOfRange(ID, "MemRead") then
return ;
end if ;
AddrWidth := MemStructPtr(ID).AddrWidth ;
-- Check Bounds of Address and if memory is initialized
if Addr'length > AddrWidth then
if (MemStructPtr(ID).MemArrayPtr = NULL) then -- ONLY PT since if ID in range, then MemInit called
Alert(MemStructPtr(ID).AlertLogID, "MemoryPkg.MemRead: Memory not initialized. Returning U", FAILURE) ;
return ;
elsif aAddr(aAddr'left downto AddrWidth) /= 0 then
Alert(MemStructPtr(ID).AlertLogID, "MemoryPkg.MemRead: Address value " & to_hxstring(Addr) & " goes beyond memory address width: " & to_string(MemStructPtr(ID).AddrWidth), FAILURE) ;
return ;
end if ;
end if ;
-- Check Bounds on Data
if Data'length /= MemStructPtr(ID).DataWidth then
Alert(MemStructPtr(ID).AlertLogID, "MemoryPkg.MemRead: Data'length: " & to_string(Data'length) & " /= Memory Data Width: " & to_string(MemStructPtr(ID).DataWidth), FAILURE) ;
return ;
end if ;
-- If Addr X, data = X
if is_X( aAddr ) then
Data := (Data'range => 'X') ;
return ;
end if ;
BlockWidth := MemStructPtr(ID).BlockWidth ;
-- Slice out upper address to form block address
if aAddr'high >= BlockWidth then
BlockAddr := to_integer(aAddr(aAddr'high downto BlockWidth)) ;
else
BlockAddr := 0 ;
end if ;
-- Empty Block, return all U
if (MemStructPtr(ID).MemArrayPtr(BlockAddr) = NULL) then
Data := (Data'range => 'U') ;
return ;
end if ;
-- Address of a word within a block
WordAddr := to_integer(aAddr(BlockWidth -1 downto 0)) ;
Data := FromMemoryBaseType(MemStructPtr(ID).MemArrayPtr(BlockAddr)(WordAddr), Data'length) ;
end procedure MemRead ;
------------------------------------------------------------
impure function MemRead (
ID : integer ;
Addr : std_logic_vector
) return std_logic_vector is
------------------------------------------------------------
constant ID_CHECK_OK : boolean := IdOutOfRange(ID, "MemRead function") ;
constant DATA_WIDTH : integer := MemStructPtr(ID).DataWidth ;
variable Data : std_logic_vector(DATA_WIDTH-1 downto 0) ;
begin
MemRead(ID, Addr, Data) ;
return Data ;
end function MemRead ;
------------------------------------------------------------
procedure MemErase(ID : integer) is
-- Erase the memory, but not the array of pointers
------------------------------------------------------------
begin
if IdOutOfRange(ID, "MemErase") then
return ;
end if ;
for BlockAddr in MemStructPtr(ID).MemArrayPtr'range loop
if (MemStructPtr(ID).MemArrayPtr(BlockAddr) /= NULL) then
deallocate (MemStructPtr(ID).MemArrayPtr(BlockAddr)) ;
end if ;
end loop ;
end procedure ;
------------------------------------------------------------
impure function GetAlertLogID (ID : integer) return AlertLogIDType is
------------------------------------------------------------
begin
if IdOutOfRange(ID, "MemErase") then
return ALERTLOG_ID_NOT_FOUND ;
else
return MemStructPtr(ID).AlertLogID ;
end if ;
end function GetAlertLogID ;
------------------------------------------------------------
-- PT Local
procedure FileReadX (
-- Hexadecimal or Binary File Read
------------------------------------------------------------
ID : integer ;
FileName : string ;
DataFormat : FileFormatType ;
StartAddr : std_logic_vector ;
EndAddr : std_logic_vector
) is
constant ADDR_WIDTH : integer := MemStructPtr(ID).AddrWidth ;
constant DATA_WIDTH : integer := MemStructPtr(ID).DataWidth ;
-- constant TemplateRange : std_logic_vector := (ADDR_WIDTH-1 downto 0 => '0') ;
-- Format:
-- @hh..h -- Address in hex
-- hhh_XX_ZZ -- data values in hex - space delimited
-- "--" or "//" -- comments
file MemFile : text open READ_MODE is FileName ;
variable Addr : std_logic_vector(ADDR_WIDTH - 1 downto 0) ;
variable SmallAddr : std_logic_vector(ADDR_WIDTH - 1 downto 0) ;
variable BigAddr : std_logic_vector(ADDR_WIDTH - 1 downto 0) ;
variable Data : std_logic_vector(DATA_WIDTH - 1 downto 0) ;
variable LineNum : natural ;
variable ItemNum : natural ;
variable AddrInc : std_logic_vector(ADDR_WIDTH - 1 downto 0) ;
variable buf : line ;
variable ReadValid : boolean ;
variable Empty : boolean ;
variable MultiLineComment : boolean ;
variable NextChar : character ;
variable StrLen : integer ;
begin
MultiLineComment := FALSE ;
if StartAddr'length /= ADDR_WIDTH and EndAddr'length /= ADDR_WIDTH then
if (MemStructPtr(ID).MemArrayPtr = NULL) then
Alert(MemStructPtr(ID).AlertLogID, "MemoryPkg.FileReadX: Memory not initialized, FileRead Ignored.", FAILURE) ;
else
Alert(MemStructPtr(ID).AlertLogID, "MemoryPkg.FileReadX: Addr'length: " & to_string(Addr'length) & " /= Memory Address Width: " & to_string(ADDR_WIDTH), FAILURE) ;
end if ;
return ;
end if ;
Addr := StartAddr ;
LineNum := 0 ;
if StartAddr <= EndAddr then
SmallAddr := StartAddr ;
BigAddr := EndAddr ;
AddrInc := (ADDR_WIDTH -1 downto 0 => '0') + 1 ;
else
SmallAddr := EndAddr ;
BigAddr := StartAddr ;
AddrInc := (others => '1') ; -- -1
end if;
ReadLineLoop : while not EndFile(MemFile) loop
ReadLine(MemFile, buf) ;
LineNum := LineNum + 1 ;
ItemNum := 0 ;
ItemLoop : loop
EmptyOrCommentLine(buf, Empty, MultiLineComment) ;
exit ItemLoop when Empty ;
ItemNum := ItemNum + 1 ;
NextChar := buf.all(buf'left) ;
if (NextChar = '@') then
-- Get Address
read(buf, NextChar) ;
ReadHexToken(buf, Addr, StrLen) ;
exit ReadLineLoop when AlertIf(MemStructPtr(ID).AlertLogID, StrLen = 0, "MemoryPkg.FileReadX: Address length 0 on line: " & to_string(LineNum), FAILURE) ;
exit ItemLoop when AlertIf(MemStructPtr(ID).AlertLogID, Addr < SmallAddr,
"MemoryPkg.FileReadX: Address in file: " & to_hxstring(Addr) &
" < StartAddr: " & to_hxstring(StartAddr) & " on line: " & to_string(LineNum)) ;
exit ItemLoop when AlertIf(MemStructPtr(ID).AlertLogID, Addr > BigAddr,
"MemoryPkg.FileReadX: Address in file: " & to_hxstring(Addr) &
" > EndAddr: " & to_hxstring(BigAddr) & " on line: " & to_string(LineNum)) ;
elsif DataFormat = HEX and IsHexOrStdLogic(NextChar) then
-- Get Hex Data
ReadHexToken(buf, data, StrLen) ;
exit ReadLineLoop when AlertIfNot(MemStructPtr(ID).AlertLogID, StrLen > 0,
"MemoryPkg.FileReadH: Error while reading data on line: " & to_string(LineNum) &
" Item number: " & to_string(ItemNum), FAILURE) ;
log(MemStructPtr(ID).AlertLogID, "MemoryPkg.FileReadX: MemWrite(Addr => " & to_hxstring(Addr) & ", Data => " & to_hxstring(Data) & ")", DEBUG) ;
MemWrite(ID, Addr, data) ;
Addr := Addr + AddrInc ;
elsif DataFormat = BINARY and isstd_logic(NextChar) then
-- Get Binary Data
-- read(buf, data, ReadValid) ;
ReadBinaryToken(buf, data, StrLen) ;
-- exit ReadLineLoop when AlertIfNot(MemStructPtr(ID).AlertLogID, ReadValid,
exit ReadLineLoop when AlertIfNot(MemStructPtr(ID).AlertLogID, StrLen > 0,
"MemoryPkg.FileReadB: Error while reading data on line: " & to_string(LineNum) &
" Item number: " & to_string(ItemNum), FAILURE) ;
log(MemStructPtr(ID).AlertLogID, "MemoryPkg.FileReadX: MemWrite(Addr => " & to_hxstring(Addr) & ", Data => " & to_string(Data) & ")", DEBUG) ;
MemWrite(ID, Addr, data) ;
Addr := Addr + AddrInc ;
else
if NextChar = LF or NextChar = CR then
-- If LF or CR, silently skip the character (DOS file in Unix)
read(buf, NextChar) ;
else
-- invalid Text, issue warning and skip rest of line
Alert(MemStructPtr(ID).AlertLogID,
"MemoryPkg.FileReadX: Invalid text on line: " & to_string(LineNum) &
" Item: " & to_string(ItemNum) & ". Skipping text: " & StripCrLf(buf.all)) ;
exit ItemLoop ;
end if ;
end if ;
end loop ItemLoop ;
end loop ReadLineLoop ;
-- -- must read EndAddr-StartAddr number of words if both start and end specified
-- if (StartAddr /= 0 or (not EndAddr) /= 0) and (Addr /= EndAddr) then
-- Alert("MemoryPkg.FileReadH: insufficient data values", WARNING) ;
-- end if ;
file_close(MemFile) ;
end FileReadX ;
------------------------------------------------------------
procedure FileReadH (
-- Hexadecimal File Read
------------------------------------------------------------
ID : integer ;
FileName : string ;
StartAddr : std_logic_vector ;
EndAddr : std_logic_vector
) is
constant ID_CHECK_OK : boolean := IdOutOfRange(ID, "FileReadH") ;
begin
FileReadX(ID, FileName, HEX, StartAddr, EndAddr) ;
end FileReadH ;
------------------------------------------------------------
-- Hexadecimal File Read
procedure FileReadH (
------------------------------------------------------------
ID : integer ;
FileName : string ;
StartAddr : std_logic_vector
) is
constant ID_CHECK_OK : boolean := IdOutOfRange(ID, "FileReadH") ;
constant ADDR_WIDTH : integer := MemStructPtr(ID).AddrWidth ;
constant EndAddr : std_logic_vector := (ADDR_WIDTH - 1 downto 0 => '1') ;
begin
FileReadX(ID, FileName, HEX, StartAddr, EndAddr) ;
end FileReadH ;
------------------------------------------------------------
-- Hexadecimal File Read
procedure FileReadH (
------------------------------------------------------------
ID : integer ;
FileName : string
) is
constant ID_CHECK_OK : boolean := IdOutOfRange(ID, "FileReadH") ;
constant ADDR_WIDTH : integer := MemStructPtr(ID).AddrWidth ;
constant StartAddr : std_logic_vector := (ADDR_WIDTH - 1 downto 0 => '0') ;
constant EndAddr : std_logic_vector := (ADDR_WIDTH - 1 downto 0 => '1') ;
begin
FileReadX(ID, FileName, HEX, StartAddr, EndAddr) ;
end FileReadH ;
------------------------------------------------------------
-- Binary File Read
procedure FileReadB (
------------------------------------------------------------
ID : integer ;
FileName : string ;
StartAddr : std_logic_vector ;
EndAddr : std_logic_vector
) is
constant ID_CHECK_OK : boolean := IdOutOfRange(ID, "FileReadB") ;
begin
FileReadX(ID, FileName, BINARY, StartAddr, EndAddr) ;
end FileReadB ;
------------------------------------------------------------
-- Binary File Read
procedure FileReadB (
------------------------------------------------------------
ID : integer ;
FileName : string ;
StartAddr : std_logic_vector
) is
constant ID_CHECK_OK : boolean := IdOutOfRange(ID, "FileReadB") ;
constant ADDR_WIDTH : integer := MemStructPtr(ID).AddrWidth ;
constant EndAddr : std_logic_vector := (ADDR_WIDTH - 1 downto 0 => '1') ;
begin
FileReadX(ID, FileName, BINARY, StartAddr, EndAddr) ;
end FileReadB ;
------------------------------------------------------------
-- Binary File Read
procedure FileReadB (
------------------------------------------------------------
ID : integer ;
FileName : string
) is
constant ID_CHECK_OK : boolean := IdOutOfRange(ID, "FileReadB") ;
constant ADDR_WIDTH : integer := MemStructPtr(ID).AddrWidth ;
constant StartAddr : std_logic_vector := (ADDR_WIDTH - 1 downto 0 => '0') ;
constant EndAddr : std_logic_vector := (ADDR_WIDTH - 1 downto 0 => '1') ;
begin
FileReadX(ID, FileName, BINARY, StartAddr, EndAddr) ;
end FileReadB ;