aboutsummaryrefslogtreecommitdiffstats
path: root/testsuite/gna/issue467/AbstractMmPkg.vhd
blob: 2752a07c2a1d98c4f92c99d439566fbf597aa339 (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
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
-------------------------------------------------------------------------------
-- Title      : Abstract Memory-Mapped Interface
-- Project    : 
-------------------------------------------------------------------------------
-- File       : AbstractMmPkg.vhd
-- Author     : Rob Gaddi  <rgaddi@highlandtechnology.com>
-- Company    : Highland Technology, Inc.
-- Created    : 20-Nov-2017
-- Last update: 2017-11-25
-- Platform   : Simulation
-- Standard   : VHDL-2008
-------------------------------------------------------------------------------
-- Description: Support package for abstract memory-mapped interface BFMs.
-------------------------------------------------------------------------------
-- Revision History:
-------------------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

library osvvm;
use osvvm.AlertLogPkg.all;
use osvvm.TbUtilPkg.all;
use osvvm.ResolutionPkg.all;

package AbstractMmPkg is
	
	-----------------------------------------------------------------------
	--	Constants and Types
	-----------------------------------------------------------------------
	
	type AlertLogIDArrayType is array(integer range <>) of AlertLogIDType;
	function alert_resolver(ta: AlertLogIDArrayType) return AlertLogIDType;
	subtype ResolvedAlert is alert_resolver AlertLogIDType;
	
	-- Transaction types
	type TransactionType_unresolved is (
		NONE,
		SINGLE,
		
		LINEAR_BURST,
		CONSTANT_BURST,
		CYCLE_BURST,
		
		BURST_DATA,
		
		PARAM
	);
	type TransactionArrayType is array(integer range <>) of TransactionType_unresolved;
	function resolved(ta: TransactionArrayType) return TransactionType_unresolved;
	subtype TransactionType is resolved TransactionType_unresolved;
	
	type AbstractMmRecType is record
		writedata : std_logic_vector;
		readdata : std_logic_vector;
		address : unsigned;
		byteen : std_logic_vector;
		write : std_logic;
		burstlen : integer_max;
		trans : TransactionType;
		
		addressiswords : std_logic;
		alert : ResolvedAlert;
		
		rdy : std_logic;
		ack : std_logic;
	end record AbstractMmRecType;
	
	constant AMR_READ: std_logic := '0';
	constant AMR_WRITE: std_logic := '1';
	
	constant AMR_ADDRESS_BYTES : std_logic := '0';
	constant AMR_ADDRESS_WORDS : std_logic := '1';
	
	constant ALRT : AlertLogIDType := GetAlertLogID("AbstractMmPkg");
	
	-----------------------------------------------------------------------
	--	Driver Functions
	-----------------------------------------------------------------------
	
	--	AmrRead (single read)
	procedure AmrRead(
		data: out std_logic_vector;
		addr: in unsigned;
		byteen: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	);
	procedure AmrRead(
		data: out std_logic_vector;
		addr: in std_logic_vector;
		byteen: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	);
	procedure AmrRead(
		data: out std_logic_vector;
		addr: in natural;
		byteen: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	);
	procedure AmrRead(
		data: out std_logic_vector;
		addr: in unsigned;
		signal rec: inout AbstractMmRecType
	);
	procedure AmrRead(
		data: out std_logic_vector;
		addr: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	);
	procedure AmrRead(
		data: out std_logic_vector;
		addr: in natural;
		signal rec: inout AbstractMmRecType
	);
	
	--	AmrWrite (single write)
	procedure AmrWrite(
		data: in  std_logic_vector;
		addr: in unsigned;
		byteen: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	);
	procedure AmrWrite(
		data: in std_logic_vector;
		addr: in std_logic_vector;
		byteen: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	);
	procedure AmrWrite(
		data: in  std_logic_vector;
		addr: in natural;
		byteen: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	);
	procedure AmrWrite(
		data: in  std_logic_vector;
		addr: in unsigned;
		signal rec: inout AbstractMmRecType
	);
	procedure AmrWrite(
		data: in  std_logic_vector;
		addr: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	);
	procedure AmrWrite(
		data: in  std_logic_vector;
		addr: in natural;
		signal rec: inout AbstractMmRecType
	);

	--	AmrAssert (single assert)
	procedure AmrAssert(
		data: in  std_logic_vector;
		addr: in unsigned;
		byteen: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	);
	procedure AmrAssert(
		data: in std_logic_vector;
		addr: in std_logic_vector;
		byteen: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	);
	procedure AmrAssert(
		data: in  std_logic_vector;
		addr: in natural;
		byteen: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	);
	procedure AmrAssert(
		data: in  std_logic_vector;
		addr: in unsigned;
		signal rec: inout AbstractMmRecType
	);
	procedure AmrAssert(
		data: in  std_logic_vector;
		addr: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	);
	procedure AmrAssert(
		data: in  std_logic_vector;
		addr: in natural;
		signal rec: inout AbstractMmRecType
	);
	
	-----------------------------------------------------------------------
	--	Model Support Functions
	-----------------------------------------------------------------------
	
	procedure InterpretByteEnable(
		rec    : in AbstractMmRecType;
		width  : out natural;
		align  : out natural
	);
	
	function GetByteAddress(rec: in AbstractMmRecType; unalign: boolean := false) return unsigned;
	function GetWordAddress(rec: in AbstractMmRecType) return unsigned;
	
	-----------------------------------------------------------------------
	--	Utility Functions
	-----------------------------------------------------------------------
	
	--	Initialization
	procedure InitializeAmr(signal rec: out AbstractMmRecType);
--	function INIT_AMR(datalen, addrlen : positive) return AbstractMmRecType;
--	function INIT_AMR(datalen, addrlen, belen : positive) return AbstractMmRecType;
	
	--	Selecting word/byte addressing
	procedure SetAddressWords(signal rec: inout AbstractMmRecType);
	procedure SetAddressBytes(signal rec: inout AbstractMmRecType);
	
	--	Overriding the default alert
	procedure OverrideAlert(signal rec: inout AbstractMmRecType; alert: AlertLogIDType);
	
end package AbstractMmPkg;

package body AbstractMmPkg is
	
	procedure InitializeAmr(signal rec: out AbstractMmRecType) is
		variable local : AbstractMmRecType(
			writedata(rec.writedata'range),
			readdata(rec.readdata'range),
			address(rec.address'range),
			byteen(rec.byteen'range)
		);
		constant WD : std_logic_vector(rec.writedata'range) := (others => 'Z');
		constant RD : std_logic_vector(rec.readdata'range) := (others => 'Z');
		constant AD : unsigned(rec.address'range) := (others => 'Z');
		constant BE : std_logic_vector(rec.byteen'range) := (others => 'Z');
	begin
		local := (
			writedata => WD,
			readdata => RD,
			address => AD,
			byteen => BE,
			write => 'Z',
			burstlen => integer'left,
			trans => NONE,
			addressiswords => 'Z',
			alert => ALRT,
			rdy => 'Z',
			ack => 'Z'
		);
		rec <= local;
	end procedure InitializeAmr;

	--function INIT_AMR(
	--	datalen, addrlen : positive
	--) return AbstractMmRecType is
	--	constant belen : positive := datalen / 8;
	--begin
	--	return INIT_AMR(datalen, addrlen, belen);
	--end function INIT_AMR;
	
	--function INIT_AMR(
	--	datalen, addrlen, belen: positive
	--) return AbstractMmRecType is
	--begin
	--	return (
	--		writedata => (datalen downto 1 => 'Z'),
	--		readdata => (datalen downto 1 => 'Z'),
	--		address => (addrlen downto 1 => 'Z'),
	--		byteen => (belen downto 1 => 'Z'),
	--		write => 'Z',
	--		burstlen => integer'left,
	--		trans => NONE,
			
	--		addressiswords => 'Z',
	--		alert => ALRT,
			
	--		rdy => 'Z',
	--		ack => 'Z'
	--	);
	--end function INIT_AMR;
	
	procedure SetAddressWords(signal rec: inout AbstractMmRecType) is
	begin
		rec.addressiswords <= AMR_ADDRESS_WORDS;
	end procedure SetAddressWords;
	
	procedure SetAddressBytes(signal rec: inout AbstractMmRecType) is
	begin
		rec.addressiswords <= AMR_ADDRESS_BYTES;
	end procedure SetAddressBytes;
	
	-----------------------------------------------------------------------
	--	AmrRead
	-----------------------------------------------------------------------
	
	procedure AmrRead(
		data: out std_logic_vector;
		addr: in unsigned;
		byteen: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	) is
		constant WD : std_logic_vector(rec.writedata'range) := (others => 'X');
	begin
		rec.writedata <= WD;
		rec.address <= RESIZE(addr, rec.address'length);
		rec.byteen <= byteen;
		rec.write <= AMR_READ;
		rec.burstlen <= 1;
		rec.trans <= SINGLE;
		RequestTransaction(rec.rdy, rec.ack);
		data := rec.readdata;
	end procedure AmrRead;
	
	procedure AmrRead(
		data: out std_logic_vector;
		addr: in std_logic_vector;
		byteen: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	) is
	begin
		AmrRead(data, UNSIGNED(addr), byteen, rec);
	end procedure AmrRead;
	
	procedure AmrRead(
		data: out std_logic_vector;
		addr: in natural;
		byteen: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	) is
	begin
		AmrRead(data, TO_UNSIGNED(addr, rec.address'length), byteen, rec);
	end procedure AmrRead;
	
	procedure AmrRead(
		data: out std_logic_vector;
		addr: in unsigned;
		signal rec: inout AbstractMmRecType
	) is
		variable byteen : std_logic_vector(rec.byteen'range) := (others => '1');
	begin
		AmrRead(data, addr, byteen, rec);
	end procedure AmrRead;
	
	procedure AmrRead(
		data: out std_logic_vector;
		addr: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	) is
		variable byteen : std_logic_vector(rec.byteen'range) := (others => '1');
	begin
		AmrRead(data, UNSIGNED(addr), byteen, rec);
	end procedure AmrRead;
	
	procedure AmrRead(
		data: out std_logic_vector;
		addr: in natural;
		signal rec: inout AbstractMmRecType
	) is
		variable byteen : std_logic_vector(rec.byteen'range) := (others => '1');
	begin
		AmrRead(data, TO_UNSIGNED(addr, rec.address'length), byteen, rec);
	end procedure AmrRead;
	
	-----------------------------------------------------------------------
	--	AmrWrite (single write)
	-----------------------------------------------------------------------
	
	procedure AmrWrite(
		data: in std_logic_vector;
		addr: in unsigned;
		byteen: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	) is
	begin
		rec.writedata <= data;
		rec.address <= RESIZE(addr, rec.address'length);
		rec.byteen <= byteen;
		rec.write <= AMR_WRITE;
		rec.burstlen <= 1;
		rec.trans <= SINGLE;
		RequestTransaction(rec.rdy, rec.ack);
	end procedure AmrWrite;
	
	procedure AmrWrite(
		data: in std_logic_vector;
		addr: in std_logic_vector;
		byteen: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	) is begin
		AmrWrite(data, UNSIGNED(addr), byteen, rec);
	end procedure AmrWrite;
	
	procedure AmrWrite(
		data: in  std_logic_vector;
		addr: in natural;
		byteen: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	) is begin
		AmrWrite(data, TO_UNSIGNED(addr, rec.address'length), byteen, rec);
	end procedure AmrWrite;
	
	procedure AmrWrite(
		data: in  std_logic_vector;
		addr: in unsigned;
		signal rec: inout AbstractMmRecType
	) is 
		constant byteen : std_logic_vector(rec.byteen'range) := (others => '1');
	begin
		AmrWrite(data, addr, byteen, rec);
	end procedure AmrWrite;
	
	procedure AmrWrite(
		data: in  std_logic_vector;
		addr: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	) is
		constant byteen : std_logic_vector(rec.byteen'range) := (others => '1');
	begin
		AmrWrite(data, UNSIGNED(addr), byteen, rec);
	end procedure AmrWrite;
	
	procedure AmrWrite(
		data: in  std_logic_vector;
		addr: in natural;
		signal rec: inout AbstractMmRecType
	) is 
		constant byteen : std_logic_vector(rec.byteen'range) := (others => '1');
	begin
		AmrWrite(data, TO_UNSIGNED(addr, rec.address'length), byteen, rec);
	end procedure AmrWrite;

		-----------------------------------------------------------------------
	--	AmrAssert (single assert)
	-----------------------------------------------------------------------
	
	procedure AmrAssert(
		data: in std_logic_vector;
		addr: in unsigned;
		byteen: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	) is
		variable readdata : std_logic_vector(data'range);
	begin
		AmrRead(readdata, addr, byteen, rec);
		--AffirmIfEqual(rec.alert, readdata, data, "Assert @ 0x" & TO_HSTRING(addr));
	end procedure AmrAssert;
	
	procedure AmrAssert(
		data: in std_logic_vector;
		addr: in std_logic_vector;
		byteen: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	) is begin
		AmrAssert(data, UNSIGNED(addr), byteen, rec);
	end procedure AmrAssert;
	
	procedure AmrAssert(
		data: in  std_logic_vector;
		addr: in natural;
		byteen: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	) is begin
		AmrAssert(data, TO_UNSIGNED(addr, rec.address'length), byteen, rec);
	end procedure AmrAssert;
	
	procedure AmrAssert(
		data: in  std_logic_vector;
		addr: in unsigned;
		signal rec: inout AbstractMmRecType
	) is 
		constant byteen : std_logic_vector(rec.byteen'range) := (others => '1');
	begin
		AmrAssert(data, addr, byteen, rec);
	end procedure AmrAssert;
	
	procedure AmrAssert(
		data: in  std_logic_vector;
		addr: in std_logic_vector;
		signal rec: inout AbstractMmRecType
	) is
		constant byteen : std_logic_vector(rec.byteen'range) := (others => '1');
	begin
		AmrAssert(data, UNSIGNED(addr), byteen, rec);
	end procedure AmrAssert;
	
	procedure AmrAssert(
		data: in  std_logic_vector;
		addr: in natural;
		signal rec: inout AbstractMmRecType
	) is 
		constant byteen : std_logic_vector(rec.byteen'range) := (others => '1');
	begin
		AmrAssert(data, TO_UNSIGNED(addr, rec.address'length), byteen, rec);
	end procedure AmrAssert;

	-----------------------------------------------------------------------
	--	Utility Functions
	-----------------------------------------------------------------------

	-- Turn a number into the number of bits needed to represent it.
	function clog2(x : positive) return natural is
		variable y : natural := 1;
	begin
		for log in 0 to 255 loop
			if y >= x then
				return log;
			end if;
			y := y * 2;
		end loop;
		return natural'right;
	end function clog2;
	
	-- Allow only 1 entry to be other than NONE.
	function resolved(ta: TransactionArrayType) return TransactionType_unresolved is
		variable r : TransactionType_unresolved := NONE;
		variable t : TransactionType_unresolved;
	begin
		for idx in ta'range loop
			t := ta(idx);
			if t /= NONE then
				assert r = NONE
					report "Multiple non-NONE transaction types."
					severity failure;
				r := t;
			end if;
		end loop;
		return r;
	end function resolved;

	-- Allow up to 1 entry to be other than our local ALRT, in which
	-- case it wins.
	function alert_resolver(ta: AlertLogIDArrayType) return AlertLogIDType is
		variable r : AlertLogIDType := ALRT;
		variable t : AlertLogIDType;
	begin
		for idx in ta'range loop
			t := ta(idx);
			if (t /= ALRT) and (t >= ALERTLOG_BASE_ID) then
				assert r = ALRT
					report "Multiple alerts provided."
					severity failure;
				r := t;
			end if;
		end loop;
		return r;
	end function alert_resolver;
	
	procedure InterpretByteEnable(
		rec    : in AbstractMmRecType;
		width  : out natural;
		align  : out natural
	) is
		alias byteen : std_logic_vector(rec.byteen'range) is rec.byteen;
		alias LA : AlertLogIDType is rec.alert;
		variable first, last: integer;
		variable found : boolean := false;
	begin
		if (and byteen) = '1' then
			-- Try to provide fast resolution for the most common case.
			width := byteen'length;
			align := 0;
		else
			-- Alright, do it the hard way.  Scan for contiguous enables.
			for i in byteen'low to byteen'high loop
				if byteen(i) = '1' then
					found := true;
					first := i;
					exit;
				end if;
			end loop;
			
			if not found then
				-- No byte enables are set
				Alert(LA, "No byte enables set.", WARNING);
				width := 0;
				align := 0;
			else
				last := first;
				for i in first+1 to byteen'high loop
					if byteen(i) = '1' then
						last := i;
					else
						exit;
					end if;
				end loop;
				
				if last /= byteen'high then
					for i in last+1 to byteen'high loop
						if byteen(i) = '1' then
							Alert(LA, "Non-contiguous byte enables " & TO_STRING(byteen), WARNING);
							exit;
						end if;
					end loop;
				end if;
				
				width := last-first+1;
				align := first;
			end if;
		end if;
	end procedure InterpretByteEnable;
	
	function GetByteAddress(rec: in AbstractMmRecType; unalign: boolean := false) return unsigned is
		variable padding : unsigned(clog2(rec.byteen'length)-1 downto 0);
		variable alignment : integer := integer'left;
	begin
		case rec.addressiswords is
			when AMR_ADDRESS_BYTES =>
				return rec.address;
				
			when AMR_ADDRESS_WORDS =>
				if unalign then
					for i in rec.byteen'low to rec.byteen'high loop
						if rec.byteen(i) = '1' then
							alignment := i;
							exit;
						end if;
					end loop;
					if alignment /= integer'left then
						report "All bytes disabled." severity warning;
						alignment := 0;
					end if;
					padding := TO_UNSIGNED(alignment, padding'length);
				else
					padding := (others => '0');
				end if;
				return rec.address & PADDING;
				
			when others =>
				report "Byte/word addressing not defined." severity failure;
				return (rec.address'range => 'X');
		end case;
	end function GetByteAddress;
	
	function GetWordAddress(rec: in AbstractMmRecType) return unsigned is
		variable padding : unsigned(clog2(rec.byteen'length)-1 downto 0);
		variable alignment, width : integer;
	begin
		case rec.addressiswords is
			when AMR_ADDRESS_BYTES =>
				return rec.address(rec.address'high downto padding'length);
				
			when AMR_ADDRESS_WORDS =>
				return rec.address;
				
			when others =>
				report "Byte/word addressing not defined." severity failure;
				return (rec.address'range => 'X');
		end case;
	end function GetWordAddress;
	
	procedure OverrideAlert(signal rec: inout AbstractMmRecType; alert: AlertLogIDType) is
	begin
		rec.alert <= alert;
	end procedure OverrideAlert;
	
end package body AbstractMmPkg;