validate method

void validate()

Method to validate the configuration of a single register.

Must check that its fields are mutually valid.

Implementation

void validate() {
  final ranges = <List<int>>[];
  final issues = <String>[];
  for (final field in fields) {
    // check the field on its own for issues
    try {
      field.validate();
    } on Exception catch (e) {
      issues.add(e.toString());
    }

    // check to ensure that the field doesn't overlap with any other field
    // overlap can occur on name or on bit placement
    for (var i = 0; i < ranges.length; i++) {
      // check against all other names
      if (field.name == fields[i].name) {
        issues.add('Field ${field.name} is duplicated.');
      }
      // check field start to see if it falls within another field
      else if (field.start >= ranges[i][0] && field.start <= ranges[i][1]) {
        issues.add(
            'Field ${field.name} overlaps with field ${fields[i].name}.');
      }
      // check field end to see if it falls within another field
      else if (field.start + field.width - 1 >= ranges[i][0] &&
          field.start + field.width - 1 <= ranges[i][1]) {
        issues.add(
            'Field ${field.name} overlaps with field ${fields[i].name}.');
      }
    }
    ranges.add([field.start, field.start + field.width - 1]);
  }
  if (issues.isNotEmpty) {
    throw CsrValidationException(issues.join('\n'));
  }
}