addTypedInOut<LogicType extends Logic> method

LogicType addTypedInOut<LogicType extends Logic>(
  1. String name,
  2. LogicType source
)

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.

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, or LogicStructure.

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.');
  }

  // ignore: parameter_assignments
  source = _validateType<LogicType>(source, isOutput: false, name: name);

  _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);
      }
    }
  }

  final inOutPort = (source.clone(name: name) as LogicType)..gets(source);

  if (inOutPort.name != name) {
    throw PortTypeException.forIntendedName(name,
        'The `clone` method for $source failed to update the signal name.');
  }

  if (inOutPort is LogicStructure) {
    inOutPort.setAllParentModule(this);
  } else {
    inOutPort.parentModule = this;
  }

  _inOutDrivers.addAll(inOutPort.srcConnections);

  _inOuts[name] = inOutPort;

  _inOutSources[name] = source;

  return inOutPort;
}