2 * Copyright (c) 2019 The Regents of the University of California
3 * Copyright (c) 2018-2019 ARM Limited
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are
17 * met: redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer;
19 * redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution;
22 * neither the name of the copyright holders nor the names of its
23 * contributors may be used to endorse or promote products derived from
24 * this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * Authors: Nikos Nikoleris
42 #include <gtest/gtest.h>
46 #include "base/addr_range.hh"
47 #include "base/bitfield.hh"
49 TEST(AddrRangeTest
, ValidRange
)
52 EXPECT_FALSE(r
.valid());
56 * This following tests check the behavior of AddrRange when initialized with
57 * a start and end address. The expected behavior is that the first address
58 * within the range will be the start address, and the last address in the
59 * range will be the (end - 1) address.
61 TEST(AddrRangeTest
, EmptyRange
)
63 AddrRange
r(0x0, 0x0);
66 * Empty ranges are valid.
68 EXPECT_TRUE(r
.valid());
69 EXPECT_EQ(0x0, r
.start());
70 EXPECT_EQ(0x0, r
.end());
71 EXPECT_EQ(0, r
.size());
74 * With no masks, granularity equals the size of the range.
76 EXPECT_EQ(0, r
.granularity());
79 * With no masks, "interleaved()" returns false.
81 EXPECT_FALSE(r
.interleaved());
84 * With no masks, "stripes()" returns ULL(1).
86 EXPECT_EQ(ULL(1), r
.stripes());
87 EXPECT_EQ("[0:0]", r
.to_string());
90 TEST(AddrRangeTest
, RangeSizeOfOne
)
92 AddrRange
r(0x0, 0x1);
93 EXPECT_TRUE(r
.valid());
94 EXPECT_EQ(0x0, r
.start());
95 EXPECT_EQ(0x1, r
.end());
96 EXPECT_EQ(1, r
.size());
97 EXPECT_EQ(1, r
.granularity());
98 EXPECT_FALSE(r
.interleaved());
99 EXPECT_EQ(ULL(1), r
.stripes());
100 EXPECT_EQ("[0:0x1]", r
.to_string());
103 TEST(AddrRangeTest
, Range16Bit
)
105 AddrRange
r(0xF000, 0xFFFF);
106 EXPECT_TRUE(r
.valid());
107 EXPECT_EQ(0xF000, r
.start());
108 EXPECT_EQ(0xFFFF, r
.end());
109 EXPECT_EQ(0x0FFF, r
.size());
110 EXPECT_EQ(0x0FFF, r
.granularity());
111 EXPECT_FALSE(r
.interleaved());
112 EXPECT_EQ(ULL(1), r
.stripes());
113 EXPECT_EQ("[0xf000:0xffff]", r
.to_string());
116 TEST(AddrRangeTest
, InvalidRange
)
118 AddrRange
r(0x1, 0x0);
119 EXPECT_FALSE(r
.valid());
122 TEST(AddrRangeTest
, LessThan
)
125 * The less-than override is a bit unintuitive and does not have a
126 * corresponding greater than. It compares the AddrRange.start() values.
127 * If they are equal, the "intlvMatch" values are compared. This is
128 * zero when AddRange is initialized with a just a start and end address.
130 AddrRange
r1(0xF000, 0xFFFF);
131 AddrRange
r2(0xF001, 0xFFFF);
132 AddrRange
r3(0xF000, 0xFFFF);
134 EXPECT_TRUE(r1
< r2
);
135 EXPECT_FALSE(r2
< r1
);
136 EXPECT_FALSE(r1
< r3
);
137 EXPECT_FALSE(r3
< r1
);
140 TEST(AddrRangeTest
, EqualToNotEqualTo
)
142 AddrRange
r1(0x1234, 0x5678);
143 AddrRange
r2(0x1234, 0x5678);
144 AddrRange
r3(0x1234, 0x5679);
146 EXPECT_TRUE(r1
== r2
);
147 EXPECT_FALSE(r1
== r3
);
148 EXPECT_FALSE(r1
!= r2
);
149 EXPECT_TRUE(r1
!= r3
);
151 EXPECT_TRUE(r2
== r1
);
152 EXPECT_FALSE(r3
== r1
);
153 EXPECT_FALSE(r2
!= r1
);
154 EXPECT_TRUE(r3
!= r1
);
157 TEST(AddrRangeTest
, MergesWith
)
160 * AddrRange.mergesWith will return true if the start, end, and masks
163 AddrRange
r1(0x10, 0x1F);
164 AddrRange
r2(0x10, 0x1F);
166 EXPECT_TRUE(r1
.mergesWith(r2
));
167 EXPECT_TRUE(r2
.mergesWith(r1
));
170 TEST(AddrRangeTest
, DoesNotMergeWith
)
172 AddrRange
r1(0x10, 0x1E);
173 AddrRange
r2(0x10, 0x1F);
175 EXPECT_FALSE(r1
.mergesWith(r2
));
176 EXPECT_FALSE(r2
.mergesWith(r1
));
179 TEST(AddrRangeTest
, IntersectsCompleteOverlap
)
181 AddrRange
r1(0x21, 0x30);
182 AddrRange
r2(0x21, 0x30);
184 EXPECT_TRUE(r1
.intersects(r2
));
185 EXPECT_TRUE(r2
.intersects(r1
));
188 TEST(AddrRangeTest
, IntersectsAddressWithin
)
190 AddrRange
r1(0x0, 0xF);
191 AddrRange
r2(0x1, 0xE);
193 EXPECT_TRUE(r1
.intersects(r2
));
194 EXPECT_TRUE(r2
.intersects(r1
));
197 TEST(AddrRangeTest
, IntersectsPartialOverlap
)
199 AddrRange
r1(0x0F0, 0x0FF);
200 AddrRange
r2(0x0F5, 0xF00);
202 EXPECT_TRUE(r1
.intersects(r2
));
203 EXPECT_TRUE(r2
.intersects(r1
));
206 TEST(AddrRangeTest
, IntersectsNoOverlap
)
208 AddrRange
r1(0x00, 0x10);
209 AddrRange
r2(0x11, 0xFF);
211 EXPECT_FALSE(r1
.intersects(r2
));
212 EXPECT_FALSE(r2
.intersects(r1
));
215 TEST(AddrRangeTest
, IntersectsFirstLastAddressOverlap
)
217 AddrRange
r1(0x0, 0xF);
218 AddrRange
r2(0xF, 0xF0);
221 * The "end address" is not in the range. Therefore, if
222 * r1.end() == r2.start(), the ranges do not intersect.
224 EXPECT_FALSE(r1
.intersects(r2
));
225 EXPECT_FALSE(r2
.intersects(r1
));
228 TEST(AddrRangeTest
, isSubsetCompleteOverlap
)
230 AddrRange
r1(0x10, 0x20);
231 AddrRange
r2(0x10, 0x20);
233 EXPECT_TRUE(r1
.isSubset(r2
));
234 EXPECT_TRUE(r2
.isSubset(r1
));
237 TEST(AddrRangeTest
, isSubsetNoOverlap
)
239 AddrRange
r1(0x10, 0x20);
240 AddrRange
r2(0x20, 0x22);
242 EXPECT_FALSE(r1
.isSubset(r2
));
243 EXPECT_FALSE(r2
.isSubset(r1
));
246 TEST(AddrRangeTest
, isSubsetTrueSubset
)
248 AddrRange
r1(0x10, 0x20);
249 AddrRange
r2(0x15, 0x17);
251 EXPECT_TRUE(r2
.isSubset(r1
));
252 EXPECT_FALSE(r1
.isSubset(r2
));
255 TEST(AddrRangeTest
, isSubsetPartialSubset
)
257 AddrRange
r1(0x20, 0x30);
258 AddrRange
r2(0x26, 0xF0);
260 EXPECT_FALSE(r1
.isSubset(r2
));
261 EXPECT_FALSE(r2
.isSubset(r1
));
264 TEST(AddrRangeTest
, isSubsetInterleavedCompleteOverlap
)
266 AddrRange
r1(0x00, 0x100, {0x40}, 0);
267 AddrRange
r2(0x00, 0x40);
269 EXPECT_TRUE(r2
.isSubset(r1
));
272 TEST(AddrRangeTest
, isSubsetInterleavedNoOverlap
)
274 AddrRange
r1(0x00, 0x100, {0x40}, 1);
275 AddrRange
r2(0x00, 0x40);
277 EXPECT_FALSE(r2
.isSubset(r1
));
280 TEST(AddrRangeTest
, isSubsetInterleavedPartialOverlap
)
282 AddrRange
r1(0x00, 0x100, {0x40}, 0);
283 AddrRange
r2(0x10, 0x50);
285 EXPECT_FALSE(r2
.isSubset(r1
));
288 TEST(AddrRangeTest
, Contains
)
290 AddrRange
r(0xF0, 0xF5);
292 EXPECT_FALSE(r
.contains(0xEF));
293 EXPECT_TRUE(r
.contains(0xF0));
294 EXPECT_TRUE(r
.contains(0xF1));
295 EXPECT_TRUE(r
.contains(0xF2));
296 EXPECT_TRUE(r
.contains(0xF3));
297 EXPECT_TRUE(r
.contains(0xF4));
298 EXPECT_FALSE(r
.contains(0xF5));
299 EXPECT_FALSE(r
.contains(0xF6));
302 TEST(AddrRangeTest
, ContainsInAnEmptyRange
)
304 AddrRange
r(0x1, 0x1);
306 EXPECT_FALSE(r
.contains(0x1));
309 TEST(AddrRangeTest
, RemoveIntlvBits
)
311 AddrRange
r(0x01, 0x10);
314 * When there are no masks, AddrRange.removeIntlBits just returns the
318 a
= r
.removeIntlvBits(a
);
322 TEST(AddrRangeTest
, addIntlvBits
)
324 AddrRange
r(0x01, 0x10);
327 * As with AddrRange.removeIntlBits, when there are no masks,
328 * AddrRange.addIntlvBits just returns the address parameter.
331 a
= r
.addIntlvBits(a
);
335 TEST(AddrRangeTest
, OffsetInRange
)
337 AddrRange
r(0x01, 0xF0);
338 EXPECT_EQ(0x04, r
.getOffset(0x5));
341 TEST(AddrRangeTest
, OffsetOutOfRangeAfter
)
344 * If the address is less than the range, MaxAddr is returned.
346 AddrRange
r(0x01, 0xF0);
347 EXPECT_EQ(MaxAddr
, r
.getOffset(0xF0));
350 TEST(AddrRangeTest
, OffsetOutOfRangeBefore
)
352 AddrRange
r(0x05, 0xF0);
353 EXPECT_EQ(MaxAddr
, r
.getOffset(0x04));
357 * The following tests check the behavior of AddrRange when initialized with
358 * a start and end address, as well as masks to distinguish interleaving bits.
360 TEST(AddrRangeTest
, LsbInterleavingMask
)
364 std::vector
<Addr
> masks
;
366 * The address is in range if the LSB is set, i.e. is the value is odd.
369 uint8_t intlv_match
= 1;
371 AddrRange
r(start
, end
, masks
, intlv_match
);
372 EXPECT_TRUE(r
.valid());
373 EXPECT_EQ(start
, r
.start());
374 EXPECT_EQ(end
, r
.end());
376 * With interleaving, it's assumed the size is equal to
377 * start - end >> [number of masks].
379 EXPECT_EQ(0x7F, r
.size());
381 * The Granularity, the size of regions created by the interleaving bits,
382 * which, in this case, is one.
384 EXPECT_EQ(1, r
.granularity());
385 EXPECT_TRUE(r
.interleaved());
386 EXPECT_EQ(ULL(2), r
.stripes());
387 EXPECT_EQ("[0:0xff] a[0]^\b=1", r
.to_string());
390 TEST(AddrRangeTest
, TwoInterleavingMasks
)
394 std::vector
<Addr
> masks
;
396 * There are two marks, the two LSBs.
399 masks
.push_back((1 << 1));
400 uint8_t intlv_match
= (1 << 1) | 1;
402 AddrRange
r(start
, end
, masks
, intlv_match
);
403 EXPECT_TRUE(r
.valid());
404 EXPECT_EQ(start
, r
.start());
405 EXPECT_EQ(end
, r
.end());
407 EXPECT_EQ(0x3FFF, r
.size());
408 EXPECT_TRUE(r
.interleaved());
409 EXPECT_EQ(ULL(4), r
.stripes());
410 EXPECT_EQ("[0:0xffff] a[0]^\b=1 a[1]^\b=1", r
.to_string());
413 TEST(AddrRangeTest
, ComplexInterleavingMasks
)
417 std::vector
<Addr
> masks
;
418 masks
.push_back((1 << 1) | 1);
419 masks
.push_back((ULL(1) << 63) | (ULL(1) << 62));
420 uint8_t intlv_match
= 0;
422 AddrRange
r(start
, end
, masks
, intlv_match
);
423 EXPECT_TRUE(r
.valid());
424 EXPECT_EQ(start
, r
.start());
425 EXPECT_EQ(end
, r
.end());
427 EXPECT_EQ(0x3FFF, r
.size());
428 EXPECT_TRUE(r
.interleaved());
429 EXPECT_EQ(ULL(4), r
.stripes());
430 EXPECT_EQ("[0:0xffff] a[0]^a[1]^\b=0 a[62]^a[63]^\b=0", r
.to_string());
433 TEST(AddrRangeTest
, InterleavingAddressesMergesWith
)
435 Addr start1
= 0x0000;
437 std::vector
<Addr
> masks
;
438 masks
.push_back((1 << 29) | (1 << 20) | (1 << 10) | 1);
439 masks
.push_back((1 << 2));
440 uint8_t intlv_match1
= 0;
441 AddrRange
r1(start1
, end1
, masks
, intlv_match1
);
443 Addr start2
= 0x0000;
445 uint8_t intlv_match2
= 1; // intlv_match may differ.
446 AddrRange
r2(start2
, end2
, masks
, intlv_match2
);
448 EXPECT_TRUE(r1
.mergesWith(r2
));
449 EXPECT_TRUE(r2
.mergesWith(r1
));
452 TEST(AddrRangeTest
, InterleavingAddressesDoNotMergeWith
)
454 Addr start1
= 0x0000;
456 std::vector
<Addr
> masks1
;
457 masks1
.push_back((1 << 29) | (1 << 20) | (1 << 10) | 1);
458 masks1
.push_back((1 << 2));
459 uint8_t intlv_match1
= 0;
460 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
462 Addr start2
= 0x0000;
464 std::vector
<Addr
> masks2
;
465 masks2
.push_back((1 << 29) | (1 << 20) | (1 << 10) | 1);
466 masks2
.push_back((1 << 3)); // Different mask here.
467 uint8_t intlv_match2
= 1; // intlv_match may differ.
468 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
470 EXPECT_FALSE(r1
.mergesWith(r2
));
471 EXPECT_FALSE(r2
.mergesWith(r1
));
474 TEST(AddrRangeTest
, InterleavingAddressesDoNotIntersect
)
477 * Range 1: all the odd addresses between 0x0000 and 0xFFFF.
479 Addr start1
= 0x0000;
481 std::vector
<Addr
> masks1
;
483 uint8_t intlv_match1
= 1;
484 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
487 * Range 2: all the even addresses between 0x0000 and 0xFFFF. These
488 * addresses should thereby not intersect.
490 Addr start2
= 0x0000;
492 std::vector
<Addr
> masks2
;
494 uint8_t intv_match2
= 0;
495 AddrRange
r2(start2
, end2
, masks2
, intv_match2
);
497 EXPECT_FALSE(r1
.intersects(r2
));
498 EXPECT_FALSE(r2
.intersects(r1
));
501 TEST(AddrRangeTest
, InterleavingAddressesIntersectsViaMerging
)
503 Addr start1
= 0x0000;
505 std::vector
<Addr
> masks1
;
506 masks1
.push_back((1 << 29) | (1 << 20) | (1 << 10) | 1);
507 masks1
.push_back((1 << 2));
508 uint8_t intlv_match1
= 0;
509 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
511 Addr start2
= 0x0000;
513 std::vector
<Addr
> masks2
;
514 masks2
.push_back((1 << 29) | (1 << 20) | (1 << 10) | 1);
515 masks2
.push_back((1 << 2));
516 uint8_t intlv_match2
= 0;
517 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
519 EXPECT_TRUE(r1
.intersects(r2
));
520 EXPECT_TRUE(r2
.intersects(r1
));
523 TEST(AddrRangeTest
, InterleavingAddressesDoesNotIntersectViaMerging
)
525 Addr start1
= 0x0000;
527 std::vector
<Addr
> masks1
;
528 masks1
.push_back((1 << 29) | (1 << 20) | (1 << 10) | 1);
529 masks1
.push_back((1 << 2));
530 uint8_t intlv_match1
= 0;
531 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
533 Addr start2
= 0x0000;
535 std::vector
<Addr
> masks2
;
536 masks2
.push_back((1 << 29) | (1 << 20) | (1 << 10) | 1);
537 masks2
.push_back((1 << 2));
539 * These addresses can merge, but their intlv_match values differ. They
540 * therefore do not intersect.
542 uint8_t intlv_match2
= 1;
543 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
545 EXPECT_FALSE(r1
.intersects(r2
));
546 EXPECT_FALSE(r2
.intersects(r1
));
550 * The following tests were created to test more complex cases where
551 * interleaving addresses may intersect. However, the "intersects" function
552 * does not cover all cases (a "Cannot test intersection..." exception will
553 * be thrown outside of very simple checks to see if an intersection occurs).
554 * The tests below accurately test whether two ranges intersect but, for now,
555 * code has yet to be implemented to utilize these tests. They are therefore
556 * disabled, but may be enabled at a later date if/when the "intersects"
557 * function is enhanced.
559 TEST(AddrRangeTest
, DISABLED_InterleavingAddressesIntersect
)
562 * Range 1: all the odd addresses between 0x0000 and 0xFFFF.
564 Addr start1
= 0x0000;
566 std::vector
<Addr
> masks1
;
568 uint8_t intlv_match1
= 0;
569 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
572 * Range 2: all the addresses divisible by 4 between 0x0000 and
573 * 0xFFFF. These addresses should thereby intersect.
575 Addr start2
= 0x0000;
577 std::vector
<Addr
> masks2
;
578 masks2
.push_back(1 << 2);
579 uint8_t intlv_match2
= 1;
580 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
582 EXPECT_TRUE(r1
.intersects(r2
));
583 EXPECT_TRUE(r2
.intersects(r1
));
586 TEST(AddrRangeTest
, DISABLED_InterleavingAddressesIntersectsOnOneByteAddress
)
589 * Range: all the odd addresses between 0x0000 and 0xFFFF.
593 std::vector
<Addr
> masks
;
595 uint8_t intlv_match
= 1;
596 AddrRange
r1(start
, end
, masks
, intlv_match
);
598 AddrRange
r2(0x0000, 0x0001);
600 EXPECT_FALSE(r1
.intersects(r2
));
601 EXPECT_FALSE(r2
.intersects(r1
));
605 DISABLED_InterleavingAddressesDoesNotIntersectOnOneByteAddress
)
608 * Range: all the odd addresses between 0x0000 and 0xFFFF.
612 std::vector
<Addr
> masks
;
614 uint8_t intlv_match
= 1;
615 AddrRange
r1(start
, end
, masks
, intlv_match
);
617 AddrRange
r2(0x0001, 0x0002);
619 EXPECT_TRUE(r1
.intersects(r2
));
620 EXPECT_TRUE(r2
.intersects(r1
));
625 * The following three tests were created to test the addr_range.isSubset
626 * function for Interleaving address ranges. However, for now, this
627 * functionality has not been implemented. These tests are therefore disabled.
629 TEST(AddrRangeTest
, DISABLED_InterleavingAddressIsSubset
)
631 // Range 1: all the even addresses between 0x0000 and 0xFFFF.
632 Addr start1
= 0x0000;
634 std::vector
<Addr
> masks1
;
636 uint8_t intlv_match1
= 0;
637 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
639 // Range 2: all the even addresses between 0xF000 and 0x0FFF, this is
640 // a subset of Range 1.
641 Addr start2
= 0xF000;
643 std::vector
<Addr
> masks2
;
645 uint8_t intlv_match2
= 0;
646 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
648 EXPECT_TRUE(r1
.isSubset(r2
));
649 EXPECT_TRUE(r2
.isSubset(r1
));
652 TEST(AddrRangeTest
, DISABLED_InterleavingAddressIsNotSubset
)
654 //Range 1: all the even addresses between 0x0000 and 0xFFFF.
655 Addr start1
= 0x0000;
657 std::vector
<Addr
> masks1
;
659 uint8_t intlv_match1
= 0;
660 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
663 // Range 2: all the odd addresses between 0xF000 and 0x0FFF, this is
664 //a subset of Range 1.
665 Addr start2
= 0xF000;
667 std::vector
<Addr
> masks2
;
669 uint8_t intlv_match2
= 1;
670 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
672 EXPECT_FALSE(r1
.isSubset(r2
));
673 EXPECT_FALSE(r2
.isSubset(r1
));
676 TEST(AddrRangeTest
, DISABLED_InterleavingAddressContains
)
679 * Range: all the address between 0x0 and 0xFF which have both the 1st
680 * and 5th bits 1, or both are 0
684 std::vector
<Addr
> masks
;
685 masks
.push_back((1 << 4) | 1);
686 uint8_t intlv_match
= 0;
687 AddrRange
r(start
, end
, masks
, intlv_match
);
689 for (Addr addr
= start
; addr
< end
; addr
++) {
690 if (((addr
& 1) && ((1 << 4) & addr
)) || // addr[0] && addr[4]
691 (!(addr
& 1) && !((1 << 4) & addr
))) { //!addr[0] && !addr[4]
692 EXPECT_TRUE(r
.contains(addr
));
694 EXPECT_FALSE(r
.contains(addr
));
699 TEST(AddrRangeTest
, InterleavingAddressAddRemoveInterlvBits
)
701 Addr start
= 0x00000;
703 std::vector
<Addr
> masks
;
705 uint8_t intlv_match
= 1;
706 AddrRange
r(start
, end
, masks
, intlv_match
);
709 Addr output
= r
.removeIntlvBits(input
);
712 * The removeIntlvBits function removes the LSB from each mask from the
713 * input address. For example, two masks:
716 * with an input address of:
719 * we would remove bit at position 0, and at position 2, resulting in:
722 * In this test there is is one mask, with a LSB at position 0.
723 * Therefore, removing the interleaving bits is equivilant to bitshifting
724 * the input to the right.
726 EXPECT_EQ(input
>> 1, output
);
729 * The addIntlvBits function will re-insert bits at the removed locations
731 EXPECT_EQ(input
, r
.addIntlvBits(output
));
734 TEST(AddrRangeTest
, InterleavingAddressAddRemoveInterlvBitsTwoMasks
)
736 Addr start
= 0x00000;
738 std::vector
<Addr
> masks
;
739 masks
.push_back((1 << 3) | (1 << 2) | (1 << 1) | 1);
740 masks
.push_back((1 << 11) | (1 << 10) | (1 << 9) | (1 << 8));
741 uint8_t intlv_match
= 1;
742 AddrRange
r(start
, end
, masks
, intlv_match
);
744 Addr input
= (1 << 9) | (1 << 8) | 1;
746 * (1 << 8) and 1 are interleaving bits to be removed.
748 Addr output
= r
.removeIntlvBits(input
);
751 * The bit, formally at position 9, is now at 7.
753 EXPECT_EQ((1 << 7), output
);
756 * Re-adding the interleaving.
758 EXPECT_EQ(input
, r
.addIntlvBits(output
));
761 TEST(AddrRangeTest
, AddRemoveInterleavBitsAcrossRange
)
764 * This purpose of this test is to ensure that removing then adding
765 * interleaving bits has no net effect.
767 * addr_range.addIntlvBits(add_range.removeIntlvBits(an_address)) should
768 * always return an_address.
770 Addr start
= 0x00000;
772 std::vector
<Addr
> masks
;
773 masks
.push_back(1 << 2);
774 masks
.push_back(1 << 3);
775 masks
.push_back(1 << 16);
776 masks
.push_back(1 << 30);
777 uint8_t intlv_match
= 0xF;
778 AddrRange
r(start
, end
, masks
, intlv_match
);
780 for (Addr i
= 0; i
< 0xFFF; i
++) {
781 Addr removedBits
= r
.removeIntlvBits(i
);
783 * As intlv_match = 0xF, all the interleaved bits should be set.
785 EXPECT_EQ(i
| (1 << 2) | (1 << 3) | (1 << 16) | (1 << 30),
786 r
.addIntlvBits(removedBits
));
790 TEST(AddrRangeTest
, InterleavingAddressesGetOffset
)
794 std::vector
<Addr
> masks
;
795 masks
.push_back((1 << 4) | (1 << 2));
796 uint8_t intlv_match
= 0;
797 AddrRange
r(start
, end
, masks
, intlv_match
);
799 Addr value
= ((1 << 10) | (1 << 9) | (1 << 8) | (1 << 2) | (1 << 1) | 1);
800 Addr value_interleaving_bits_removed
=
801 ((1 << 9) | (1 << 8) | (1 << 7) | (1 << 1) | 1);
803 Addr expected_output
= value_interleaving_bits_removed
- start
;
805 EXPECT_EQ(expected_output
, r
.getOffset(value
));
808 TEST(AddrRangeTest
, InterleavingLessThanStartEquals
)
810 Addr start1
= 0x0000FFFF;
811 Addr end1
= 0xFFFF0000;
812 std::vector
<Addr
> masks1
;
813 masks1
.push_back((1 << 4) | (1 << 2));
814 uint8_t intlv_match1
= 0;
815 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
817 Addr start2
= 0x0000FFFF;
818 Addr end2
= 0x000F0000;
819 std::vector
<Addr
> masks2
;
820 masks2
.push_back((1 << 4) | (1 << 2));
821 masks2
.push_back((1 << 10));
822 uint8_t intlv_match2
= 2;
823 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
826 * When The start addresses are equal, the intlv_match values are
829 EXPECT_TRUE(r1
< r2
);
830 EXPECT_FALSE(r2
< r1
);
833 TEST(AddrRangeTest
, InterleavingLessThanStartNotEquals
)
835 Addr start1
= 0x0000FFFF;
836 Addr end1
= 0xFFFF0000;
837 std::vector
<Addr
> masks1
;
838 masks1
.push_back((1 << 4) | (1 << 2));
839 uint8_t intlv_match1
= 0;
840 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
842 Addr start2
= 0x0000FFFE;
843 Addr end2
= 0x000F0000;
844 std::vector
<Addr
> masks2
;
845 masks2
.push_back((1 << 4) | (1 << 2));
846 masks2
.push_back((1 << 10));
847 uint8_t intlv_match2
= 2;
848 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
850 EXPECT_TRUE(r2
< r1
);
851 EXPECT_FALSE(r1
< r2
);
854 TEST(AddrRangeTest
, InterleavingEqualTo
)
856 Addr start1
= 0x0000FFFF;
857 Addr end1
= 0xFFFF0000;
858 std::vector
<Addr
> masks1
;
859 masks1
.push_back((1 << 4) | (1 << 2));
860 uint8_t intlv_match1
= 0;
861 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
863 Addr start2
= 0x0000FFFF;
864 Addr end2
= 0xFFFF0000;
865 std::vector
<Addr
> masks2
;
866 masks2
.push_back((1 << 4) | (1 << 2));
867 uint8_t intlv_match2
= 0;
868 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
870 EXPECT_TRUE(r1
== r2
);
873 TEST(AddrRangeTest
, InterleavingNotEqualTo
)
875 Addr start1
= 0x0000FFFF;
876 Addr end1
= 0xFFFF0000;
877 std::vector
<Addr
> masks1
;
878 masks1
.push_back((1 << 4) | (1 << 2));
879 uint8_t intlv_match1
= 0;
880 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
882 Addr start2
= 0x0000FFFF;
883 Addr end2
= 0xFFFF0000;
884 std::vector
<Addr
> masks2
;
885 masks2
.push_back((1 << 4) | (1 << 2));
886 masks2
.push_back((1 << 10));
887 uint8_t intlv_match2
= 2;
888 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
891 * These ranges are not equal due to having different masks.
893 EXPECT_FALSE(r1
== r2
);
897 * The AddrRange(std::vector<AddrRange>) constructor "merges" the interleaving
898 * address ranges. It should be noted that this constructor simply checks that
899 * these interleaving addresses can be merged then creates a new address from
900 * the start and end addresses of the first address range in the vector.
902 TEST(AddrRangeTest
, MergingInterleavingAddressRanges
)
904 Addr start1
= 0x0000;
906 std::vector
<Addr
> masks1
;
907 masks1
.push_back((1 << 4) | (1 << 2));
908 uint8_t intlv_match1
= 0;
909 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
911 Addr start2
= 0x0000;
913 std::vector
<Addr
> masks2
;
914 masks2
.push_back((1 << 4) | (1 << 2));
915 uint8_t intlv_match2
= 1;
916 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
918 std::vector
<AddrRange
> to_merge
;
919 to_merge
.push_back(r1
);
920 to_merge
.push_back(r2
);
922 AddrRange
output(to_merge
);
924 EXPECT_EQ(0x0000, output
.start());
925 EXPECT_EQ(0xFFFF, output
.end());
926 EXPECT_FALSE(output
.interleaved());
929 TEST(AddrRangeTest
, MergingInterleavingAddressRangesOneRange
)
932 * In the case where there is just one range in the vector, the merged
933 * address range is equal to that range.
937 std::vector
<Addr
> masks
;
938 masks
.push_back((1 << 4) | (1 << 2));
939 uint8_t intlv_match
= 0;
940 AddrRange
r(start
, end
, masks
, intlv_match
);
942 std::vector
<AddrRange
> to_merge
;
943 to_merge
.push_back(r
);
945 AddrRange
output(to_merge
);
947 EXPECT_EQ(r
, output
);
951 * The following tests verify the soundness of the "legacy constructor",
952 * AddrRange(Addr, Addr, uint8_t, uint8_t, uint8_t, uint8_t).
954 * The address is assumed to contain two ranges; the interleaving bits, and
955 * the xor bits. The first two arguments of this constructor specify the
956 * start and end addresses. The third argument specifies the MSB of the
957 * interleaving bits. The fourth argument specifies the MSB of the xor bits.
958 * The firth argument specifies the size (in bits) of the xor and interleaving
959 * bits. These cannot overlap. The sixth argument specifies the value the
960 * XORing of the xor and interleaving bits should equal to be considered in
963 * This constructor does a lot of complex translation to migrate this
964 * constructor to the masks/intlv_match format.
966 TEST(AddrRangeTest
, LegacyConstructorNoInterleaving
)
969 * This constructor should create a range with no interleaving.
971 AddrRange
range(0x0000, 0xFFFF, 0, 0, 0 ,0);
972 AddrRange
expected(0x0000, 0xFFFF);
974 EXPECT_EQ(expected
, range
);
977 TEST(AddrRangeTest
, LegacyConstructorOneBitMask
)
980 * In this test, the LSB of the address determines whether an address is
981 * in range. If even, it's in range, if not, it's out of range. the XOR
982 * bit range is not used.
984 AddrRange
range(0x00000000, 0xFFFFFFFF, 0, 0, 1, 0);
986 std::vector
<Addr
> masks
;
988 AddrRange
expected(0x00000000, 0xFFFFFFFF, masks
, 0);
990 EXPECT_TRUE(expected
== range
);
993 TEST(AddrRangeTest
, LegacyConstructorTwoBitMask
)
996 * In this test, the two LSBs of the address determines whether an address
997 * is in range. If the two are set, the address is in range. The XOR bit
1000 AddrRange
range(0x00000000, 0xFFFFFFFF, 1, 0, 2, 3);
1002 std::vector
<Addr
> masks
;
1004 masks
.push_back((1 << 1));
1005 AddrRange
expected(0x00000000, 0xFFFFFFFF, masks
, 3);
1007 EXPECT_TRUE(expected
== range
);
1010 TEST(AddrRangeTest
, LegacyConstructorTwoBitMaskWithXOR
)
1013 * In this test, the two LSBs of the address determine wether an address
1014 * is in range. They are XORed to the 10th and 11th bits in the address.
1015 * If XORed value is equal to 3, then the address is in range.
1018 AddrRange
range(0x00000000, 0xFFFFFFFF, 1, 11, 2, 3);
1021 * The easiest way to ensure this range is correct is to iterate throguh
1022 * the address range and ensure the correct set of addresses are contained
1025 * We start with the xor_mask: a mask to select the 10th and 11th bits.
1027 Addr xor_mask
= (1 << 11) | (1 << 10);
1028 for (Addr i
= 0; i
< 0x0000FFFF; i
++) {
1030 Addr xor_value
= (xor_mask
& i
) >> 10;
1031 /* If the XOR of xor_bits and the intlv bits (the 0th and 1st bits) is
1032 * equal to intlv_match (3, i.e., the 0th and 1st bit is set),then the
1033 * address is within range.
1035 if (((xor_value
^ i
) & 3) == 3) {
1036 EXPECT_TRUE(range
.contains(i
));
1038 EXPECT_FALSE(range
.contains(i
));
1044 * addr_range.hh contains some convenience constructors. The following tests
1045 * verify they construct AddrRange correctly.
1047 TEST(AddrRangeTest
, RangeExConstruction
)
1049 AddrRange r
= RangeEx(0x6, 0xE);
1050 EXPECT_EQ(0x6, r
.start());
1051 EXPECT_EQ(0xE, r
.end());
1054 TEST(AddrRangeTest
, RangeInConstruction
)
1056 AddrRange r
= RangeIn(0x6, 0xE);
1057 EXPECT_EQ(0x6, r
.start());
1058 EXPECT_EQ(0xF, r
.end());
1061 TEST(AddrRangeTest
, RangeSizeConstruction
){
1062 AddrRange r
= RangeSize(0x5, 5);
1063 EXPECT_EQ(0x5, r
.start());
1064 EXPECT_EQ(0xA, r
.end());