addTypedInOut<LogicType extends Logic> method
Registers a signal as an inOut to this Module and returns an inOut
port that can be consumed inside this Module. The type of the port will
be LogicType and constructed via Logic.clone, so it is required that
the source implements clone functionality that matches the type and
properly updates the Logic.name as well. A declared payload must retain
its runtime type and recursive shape, including any
PayloadShapeMetadata.
This is a good way to construct inOuts that have matching widths or
dimensions to their source signal, or to make a LogicStructure an
inOut. You can use this on a Logic, LogicArray,
TypedLogicArray, or LogicStructure. A TypedLogicArray retains both
its hardware element type and semantic value type.
The source must be or exclusively contain LogicNets. If source is a
Const (or is a LogicStructure that includes a Const), the
LogicType must be set to Logic, since Consts cannot be driven and
are not suitable as ports.
The return value is the same as what is returned by inOut and should
only be used within this Module. The provided source is accessible via
inOutSource.
Implementation
LogicType addTypedInOut<LogicType extends Logic>(
String name, LogicType source) {
_checkForSafePortName(name);
if (!source.isNet) {
throw PortTypeException(source, 'Typed inOuts must be nets.');
}
source = _validateType<LogicType>(source, isOutput: false, name: name);
final inOutPort = source.clone(name: name);
if (inOutPort is! LogicType || !sameTypedCloneShape(source, inOutPort)) {
throw PortTypeException.forIntendedName(
name, 'The `clone` method changed the declared type or format.');
}
if (inOutPort.name != name) {
throw PortTypeException.forIntendedName(name,
'The `clone` method for $source failed to update the signal name.');
}
if (!inOutPort.isNet) {
throw PortTypeException.forIntendedName(
name, 'The `clone` method must produce a net.');
}
_inOutDrivers.add(source);
// we need to properly detect all inout sources
if (source is LogicStructure) {
final sourceElems = TraverseableCollection<Logic>()..add(source);
for (var i = 0; i < sourceElems.length; i++) {
final sei = sourceElems[i];
_inOutDrivers.add(sei);
if (sei.parentStructure != null) {
sourceElems.add(sei.parentStructure!);
}
if (sei is LogicStructure) {
sourceElems.addAll(sei.elements);
}
}
}
inOutPort.gets(source);
if (inOutPort is LogicStructure) {
inOutPort.setAllParentModule(this);
} else {
inOutPort.parentModule = this;
}
_inOutDrivers.addAll(inOutPort.srcConnections);
_inOuts[name] = inOutPort;
_inOutSources[name] = source;
return inOutPort;
}