Swizzle constructor

Swizzle(
  1. List<Logic> signals, {
  2. String name = 'swizzle',
})

Constructs a Module which concatenates signals into one large out.

Implementation

Swizzle(List<Logic> signals, {super.name = 'swizzle'})
    : _isNet = signals.any((e) => e.isNet) {
  var outputWidth = 0;

  final inputCreator = _isNet ? addInOut : addInput;

  var idx = 0;
  for (final signal in signals.reversed) {
    //reverse so bit 0 is the last thing in the input list
    final inputName = Naming.unpreferredName('in${idx++}');
    _swizzleInputs.add(
      inputCreator(inputName, signal, width: signal.width),
    );
    outputWidth += signal.width;
  }

  if (_isNet) {
    out = LogicNet(name: _out, width: outputWidth, naming: Naming.unnamed);
    final internalOut = addInOut(_out, out, width: outputWidth);

    var idx = 0;
    for (final swizzleInput in _swizzleInputs) {
      internalOut.quietlyMergeSubsetTo(swizzleInput as LogicNet, start: idx);
      idx += swizzleInput.width;
    }
  } else {
    out = addOutput(_out, width: outputWidth);

    // so that you can't assign the output of a (Logic) swizzle
    out.makeUnassignable(
        reason:
            'The output of a (non-LogicNet) Swizzle ($this) is read-only.');

    _execute(); // for initial values
    for (final swizzleInput in _swizzleInputs) {
      swizzleInput.glitch.listen((args) {
        _execute();
      });
    }
  }
}