Csr constructor

Csr(
  1. CsrInstanceConfig config
)

Factory constructor for Csr.

Because LogicStructure requires a List upon construction, the factory method assists in creating the List upfront before the LogicStructure constructor is called.

Implementation

factory Csr(
  CsrInstanceConfig config,
) {
  final fields = <Logic>[];
  final rsvds = <int>[];
  var currIdx = 0;
  var rsvdCount = 0;

  // semantically a register with no fields means that
  // there is one read/write field that is the entire register
  if (config.fields.isEmpty) {
    fields.add(Logic(name: '${config.name}_data', width: config.width));
  }
  // there is at least one field explicitly defined so
  // process them individually
  else {
    for (final field in config.fields) {
      if (field.start > currIdx) {
        fields.add(Logic(
            name: '${config.name}_rsvd_$rsvdCount',
            width: field.start - currIdx));
        rsvds.add(fields.length - 1);
        rsvdCount++;
      }
      fields.add(
          Logic(name: '${config.name}_${field.name}', width: field.width));
      currIdx = field.start + field.width;
    }
    if (currIdx < config.width) {
      fields.add(Logic(
          name: '${config.name}_rsvd_$rsvdCount',
          width: config.width - currIdx));
      rsvds.add(fields.length - 1);
    }
  }
  return Csr._(
    config: config,
    rsvdIndices: rsvds,
    fields: fields,
  );
}