SparseToDense constructor

SparseToDense(
  1. List<Logic> indices,
  2. List<Logic> valids, {
  3. required int outputWidth,
  4. String name = 'sparse_to_dense',
  5. bool reserveName = false,
  6. bool reserveDefinitionName = false,
  7. String? definitionName,
})

Creates a combinational scatter for indices and their valids.

Implementation

SparseToDense(
  List<Logic> indices,
  List<Logic> valids, {
  required this.outputWidth,
  super.name = 'sparse_to_dense',
  super.reserveName,
  super.reserveDefinitionName,
  String? definitionName,
})  : inputCount = indices.length,
      super(
        definitionName: definitionName ??
            'SparseToDense_N${indices.length}_I'
                '${indices.isEmpty ? 0 : indices.first.width}_O$outputWidth',
      ) {
  if (indices.isEmpty) {
    throw RohdHclException('SparseToDense requires at least one index.');
  }
  if (outputWidth <= 0) {
    throw RohdHclException('SparseToDense outputWidth must be positive.');
  }
  if (valids.length != indices.length) {
    throw RohdHclException('SparseToDense requires one valid per index.');
  }
  final width = indices.first.width;
  if (width <= 0 || indices.any((index) => index.width != width)) {
    throw RohdHclException(
        'SparseToDense indices must have equal, positive widths.');
  }
  if (valids.any((valid) => valid.width != 1)) {
    throw RohdHclException('SparseToDense valids must be single-bit.');
  }

  final terms = [
    for (var i = 0; i < inputCount; i++)
      (Const(1, width: outputWidth) <<
              addInput('index$i', indices[i], width: width)) &
          addInput('valid$i', valids[i]).replicate(outputWidth),
  ];
  final tree = ReductionTree<Logic, Logic>(
    terms,
    (inputs, {depth = 0, control, name = ''}) =>
        inputs.reduce((a, b) => a | b),
    definitionName: 'SparseToDenseOr_N${inputCount}_O$outputWidth',
  );
  addOutput('out', width: outputWidth) <= tree.out;
}