Skip to content
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
8 changes: 8 additions & 0 deletions lib/src/signals/logic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -962,8 +962,16 @@ class Logic {
/// The input [multiplier] cannot be negative or 0; an exception will be
/// thrown, otherwise.
///
/// If [multiplier] is 1, then this signal is returned directly, since
/// replicating once is a no-op and would otherwise generate a redundant
/// `1{...}` in the output SystemVerilog.
///
/// If [isNet], then the result will also be a net.
Logic replicate(int multiplier) {
if (multiplier == 1) {
return this;
}

if (isNet) {
// many SV simulators don't support replication of nets
return List.generate(multiplier, (i) => this).swizzle();
Expand Down
30 changes: 29 additions & 1 deletion test/replication_test.dart
Comment thread
mkorbel1 marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2023 Intel Corporation
// Copyright (C) 2023-2026 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
//
// replication_test.dart
Expand All @@ -21,6 +21,13 @@ class ReplicationOpModule extends Module {
}
}

class SignExtendModule extends Module {
SignExtendModule(Logic a, int newWidth) {
a = addInput('a', a, width: a.width);
addOutput('b', width: newWidth) <= a.signExtend(newWidth);
}
}

void main() {
group('Logic', () {
tearDown(Simulator.reset);
Expand Down Expand Up @@ -52,6 +59,27 @@ void main() {
], 1, originalWidth: 4);
});

test('multiply by 1 returns the original signal', () {
final a = Logic(width: 4);
expect(identical(a.replicate(1), a), isTrue);
});

test('multiply by 1 generates no replication in SystemVerilog', () async {
final mod = ReplicationOpModule(Logic(width: 4), 1);
Comment thread
mkorbel1 marked this conversation as resolved.
await mod.build();
final sv = mod.generateSynth();
expect(sv, contains('assign b = a;'));
expect(sv, isNot(contains('{1{')));
});

test('signExtend of a 1-bit signal by 1 generates no replication',
() async {
final mod = SignExtendModule(Logic(), 1);
await mod.build();
final sv = mod.generateSynth();
expect(sv, isNot(contains('{1{')));
});

test('multiply by 2 replicates the input signal twice', () async {
await replicateVectors([
Vector({'a': 0}, {'b': 0}),
Expand Down
Loading