addInOut method

LogicNet addInOut(
  1. String name,
  2. Logic source, {
  3. int width = 1,
})

Registers a signal as an inOut to this Module and returns an inOut port that can be consumed.

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

LogicNet addInOut(String name, Logic source, {int width = 1}) {
  _checkForSafePortName(name);
  if (source.width != width) {
    throw PortWidthMismatchException(source, width);
  }

  _inOutDrivers.add(source);

  // we need to properly detect all inout sources, even for arrays
  if (source.isArrayMember || source is LogicArray) {
    final sourceElems = TraverseableCollection<Logic>()..add(source);
    for (var i = 0; i < sourceElems.length; i++) {
      final sei = sourceElems[i];
      _inOutDrivers.add(sei);

      if (sei.isArrayMember) {
        sourceElems.add(sei.parentStructure!);
      }

      if (sei is LogicArray) {
        sourceElems.addAll(sei.elements);
      }
    }
  }

  if (source is LogicStructure) {
    // need to also track the packed version if it's a structure, since the
    // signal that's actually getting connected to the port *is* the packed
    // one, not the original array/struct.
    _inOutDrivers.add(source.packed);
  }

  final inOutPort =
      LogicNet(name: name, width: width, naming: Naming.reserved)
        // ignore: invalid_use_of_protected_member
        ..parentModule = this
        ..gets(source);

  _inOuts[name] = inOutPort;

  _inOutSources[name] = source;

  return inOutPort;
}