Skip to content

Commit 6474134

Browse files
authored
Fix bug in LogicStructure.getRange calculations (#499)
1 parent d29d920 commit 6474134

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

lib/src/signals/logic_structure.dart

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,17 @@ class LogicStructure implements Logic {
179179
final elementStart = index;
180180
final elementEnd = index + element.width;
181181

182-
final elementInRange = ((elementStart >= modifiedStartIndex) &&
183-
(elementStart < modifiedEndIndex)) ||
184-
((elementEnd > modifiedStartIndex) &&
185-
(elementEnd <= modifiedEndIndex));
182+
// if the element is even partially within the range, then include it
183+
// OR, if it is wholly contained within the range, include it
184+
final elementInRange =
185+
// end is within the element
186+
(modifiedEndIndex > elementStart && modifiedEndIndex < elementEnd) ||
187+
// start is within the element
188+
(modifiedStartIndex >= elementStart &&
189+
modifiedStartIndex < elementEnd) ||
190+
//element is fully contained
191+
(modifiedEndIndex >= elementEnd &&
192+
modifiedStartIndex <= elementStart);
186193

187194
if (elementInRange) {
188195
// figure out the subset of `element` that needs to be included

test/logic_array_test.dart

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2023 Intel Corporation
1+
// Copyright (C) 2023-2024 Intel Corporation
22
// SPDX-License-Identifier: BSD-3-Clause
33
//
44
// logic_array_test.dart
@@ -579,6 +579,70 @@ void main() {
579579
});
580580
});
581581

582+
group('access logicarray', () {
583+
test('slice one bit of 1d array', () async {
584+
final la = LogicArray([3], 8);
585+
final slice = la.slice(9, 9);
586+
expect(slice.width, 1);
587+
la.elements[1].put(bin('00000010'));
588+
expect(slice.value.toInt(), 1);
589+
});
590+
591+
test('slice 2 bits of one element of 1d array', () async {
592+
final la = LogicArray([3], 8);
593+
final slice = la.slice(10, 9);
594+
expect(slice.width, 2);
595+
la.elements[1].put(bin('00000110'));
596+
expect(slice.value.toInt(), bin('11'));
597+
});
598+
599+
test('slice 2 bits spanning two elements of 1d array', () async {
600+
final la = LogicArray([3], 8);
601+
final slice = la.slice(8, 7);
602+
expect(slice.width, 2);
603+
la.elements[1].put(1, fill: true);
604+
la.elements[0].put(0, fill: true);
605+
expect(slice.value.toInt(), bin('10'));
606+
});
607+
608+
test('slice 2 bits spanning 2 arrays of 2d array', () async {
609+
final la = LogicArray([3, 2], 8);
610+
final slice = la.slice(16, 15);
611+
expect(slice.width, 2);
612+
la.elements[1].elements[0].put(1, fill: true);
613+
la.elements[0].elements[1].put(0, fill: true);
614+
expect(slice.value.toInt(), bin('10'));
615+
});
616+
617+
test('slice more than one element of array', () async {
618+
final la = LogicArray([3], 8);
619+
final slice = la.slice(19, 4);
620+
expect(slice.width, 16);
621+
la.elements[2].put(LogicValue.x);
622+
la.elements[1].put(0);
623+
la.elements[0].put(1, fill: true);
624+
expect(slice.value, LogicValue.of('xxxx000000001111'));
625+
});
626+
627+
test('slice more than one element of array at the edges', () async {
628+
final la = LogicArray([3], 8);
629+
final slice = la.slice(16, 7);
630+
expect(slice.width, 10);
631+
la.elements[2].put(LogicValue.x);
632+
la.elements[1].put(0);
633+
la.elements[0].put(1, fill: true);
634+
expect(slice.value, LogicValue.of('x000000001'));
635+
});
636+
637+
test('slice exactly one element of array', () async {
638+
final la = LogicArray([3], 8);
639+
final slice = la.slice(15, 8);
640+
expect(slice.width, 8);
641+
la.elements[1].put(1, fill: true);
642+
expect(slice.value, LogicValue.of('11111111'));
643+
});
644+
});
645+
582646
group('logicarray passthrough', () {
583647
Future<void> testArrayPassthrough(SimpleLAPassthrough mod,
584648
{bool checkNoSwizzle = true,

0 commit comments

Comments
 (0)