Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique case with multiple match behavior #403

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions lib/src/modules/conditional.dart
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,16 @@ abstract class Conditional {
/// This is used for [Combinational.ssa].
Map<Logic, Logic> _processSsa(Map<Logic, Logic> currentMappings,
{required int context});

/// Drives X to all receivers.
void _driveX(Set<Logic> drivenSignals) {
for (final receiver in receivers) {
receiverOutput(receiver).put(LogicValue.x);
if (!drivenSignals.contains(receiver) || receiver.value.isValid) {
drivenSignals.add(receiver);
}
}
}
}

/// Represents a group of [Conditional]s to be executed.
Expand Down Expand Up @@ -957,12 +967,7 @@ class Case extends Conditional {

if (!expression.value.isValid) {
// if expression has X or Z, then propogate X's!
for (final receiver in receivers) {
receiverOutput(receiver).put(LogicValue.x);
if (!drivenSignals.contains(receiver) || receiver.value.isValid) {
drivenSignals.add(receiver);
}
}
_driveX(drivenSignals);
return;
}

Expand All @@ -975,9 +980,8 @@ class Case extends Conditional {
conditional.execute(drivenSignals, guard);
}
if (foundMatch != null && conditionalType == ConditionalType.unique) {
throw Exception('Unique case statement had multiple matching cases.'
' Original: "$foundMatch".'
' Duplicate: "$item".');
_driveX(drivenSignals);
return;
}

foundMatch = item;
Expand All @@ -996,8 +1000,8 @@ class Case extends Conditional {
} else if (foundMatch == null &&
(conditionalType == ConditionalType.unique ||
conditionalType == ConditionalType.priority)) {
throw Exception('$conditionalType case statement had no matching case,'
' and type was $conditionalType.');
_driveX(drivenSignals);
return;
}
}

Expand Down
34 changes: 34 additions & 0 deletions test/conditionals_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,28 @@ class CaseModule extends Module {
}
}

class UniqueCase extends Module {
UniqueCase(Logic a, Logic b) : super(name: 'UniqueCase') {
a = addInput('a', a);
b = addInput('b', b);
final c = addOutput('c');
final d = addOutput('d');
Combinational([
Case(
Const(1),
[
CaseItem(a, [c < 1, d < 0]),
CaseItem(b, [c < 1, d < 0]),
],
defaultItem: [
c < 0,
d < 1,
],
conditionalType: ConditionalType.unique),
]);
}
}

enum SeqCondModuleType { caseNormal, caseZ, ifNormal }

class SeqCondModule extends Module {
Expand Down Expand Up @@ -568,6 +590,18 @@ void main() {
expect(simResult, equals(true));
});

test('Unique case', () async {
final mod = UniqueCase(Logic(), Logic());
await mod.build();
final vectors = [
Vector({'a': 0, 'b': 0}, {'c': 0, 'd': 1}),
Vector({'a': 0, 'b': 1}, {'c': 1, 'd': 0}),
Vector({'a': 1, 'b': 0}, {'c': 1, 'd': 0}),
Vector({'a': 1, 'b': 1}, {'c': LogicValue.x, 'd': LogicValue.x}),
];
await SimCompare.checkFunctionalVector(mod, vectors);
});

test('conditional ff', () async {
final mod = SequentialModule(Logic(), Logic(), Logic(width: 8));
await mod.build();
Expand Down