Skip to content

Commit d22fdd2

Browse files
authored
Fix bug where one-bit struct output ports improperly slice (#629)
1 parent f66a290 commit d22fdd2

4 files changed

Lines changed: 63 additions & 2 deletions

File tree

lib/src/synthesizers/systemverilog/systemverilog_synthesis_result.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class SystemVerilogSynthesisResult extends SynthesisResult {
153153
' bidirectional net connections.');
154154

155155
var sliceString = '';
156-
if (assignment is PartialSynthAssignment) {
156+
if (assignment is PartialSynthAssignment && assignment.width > 1) {
157157
sliceString = assignment.dstUpperIndex == assignment.dstLowerIndex
158158
? '[${assignment.dstUpperIndex}]'
159159
: '[${assignment.dstUpperIndex}:${assignment.dstLowerIndex}]';

lib/src/synthesizers/utilities/synth_assignment.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class SynthAssignment {
4747
assert(_checkWidths(), 'Signal width mismatch');
4848
}
4949

50+
/// The width of the assignment (of both the [src] and [dst]).
51+
int get width => dst.width;
52+
5053
@override
5154
String toString() => '$dst <= $src';
5255
}

test/bus_test.dart

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2021-2023 Intel Corporation
1+
// Copyright (C) 2021-2025 Intel Corporation
22
// SPDX-License-Identifier: BSD-3-Clause
33
//
44
// bus_test.dart
@@ -211,11 +211,35 @@ class ConstBusModule extends Module {
211211
}
212212
}
213213

214+
class SingleBitBusSubsetMod extends Module {
215+
SingleBitBusSubsetMod(Logic oneBit) {
216+
oneBit = addInput('oneBit', oneBit);
217+
218+
addOutput('result') <= BusSubset(oneBit, 0, 0).subset;
219+
}
220+
}
221+
214222
void main() {
215223
tearDown(() async {
216224
await Simulator.reset();
217225
});
218226

227+
test('single-bit bus subset', () async {
228+
final mod = SingleBitBusSubsetMod(Logic());
229+
await mod.build();
230+
231+
final sv = mod.generateSynth();
232+
expect(sv, contains('assign result = oneBit'));
233+
234+
final vectors = [
235+
Vector({'oneBit': 0}, {'result': 0}),
236+
Vector({'oneBit': 1}, {'result': 1}),
237+
];
238+
239+
await SimCompare.checkFunctionalVector(mod, vectors);
240+
SimCompare.checkIverilogVector(mod, vectors);
241+
});
242+
219243
group('functional', () {
220244
test('NotGate bus', () async {
221245
final a = Logic(width: 8);

test/typed_port_test.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ class SimpleStructModule extends Module {
5555
}
5656
}
5757

58+
class OneBitStruct extends LogicStructure {
59+
OneBitStruct({super.name = 'obs'}) : super([Logic(name: 'oneBit')]);
60+
61+
@override
62+
OneBitStruct clone({String? name}) => OneBitStruct(name: name ?? this.name);
63+
}
64+
65+
class ModuleWithOneBitStructPort extends Module {
66+
ModuleWithOneBitStructPort(OneBitStruct inStruct) {
67+
inStruct = addTypedInput('inStruct', inStruct);
68+
final outStruct = addTypedOutput('outStruct', inStruct.clone);
69+
70+
outStruct.elements.first.gets(inStruct.elements.first);
71+
}
72+
}
73+
5874
class SimpleStructModuleContainer extends Module {
5975
SimpleStructModuleContainer(Logic a1, Logic a2,
6076
{super.name = 'simple_struct_mod_container', bool asNet = false}) {
@@ -408,4 +424,22 @@ void main() {
408424
});
409425
}
410426
});
427+
428+
test('single bit struct port partial assignment', () async {
429+
final mod = ModuleWithOneBitStructPort(OneBitStruct());
430+
await mod.build();
431+
432+
final sv = mod.generateSynth();
433+
434+
// no slicing on single-bit signals
435+
expect(sv, contains('assign outStruct = outStruct_oneBit'));
436+
437+
final vectors = [
438+
Vector({'inStruct': 0}, {'outStruct': 0}),
439+
Vector({'inStruct': 1}, {'outStruct': 1}),
440+
];
441+
442+
await SimCompare.checkFunctionalVector(mod, vectors);
443+
SimCompare.checkIverilogVector(mod, vectors);
444+
});
411445
}

0 commit comments

Comments
 (0)