CsrModel constructor

CsrModel({
  1. required CsrModelDescription description,
  2. required Logic clk,
  3. required Logic reset,
  4. required DataPortInterface<Logic>? frontRead,
  5. required DataPortInterface<Logic>? frontWrite,
  6. required Map<BigInt, CsrBackdoorInterface> hardware,
})

Constructs the software state and attaches one shared clock handler.

hardware contains only selected, already connected hardware interfaces.

Implementation

CsrModel({
  required this.description,
  required Logic clk,
  required Logic reset,
  required DataPortInterface? frontRead,
  required DataPortInterface? frontWrite,
  required Map<BigInt, CsrBackdoorInterface> hardware,
})  : _clk = clk,
      _reset = reset,
      _frontRead = frontRead,
      _frontWrite = frontWrite {
  if (clk.width != 1 ||
      reset.width != 1 ||
      frontRead?.addrWidth != description.frontRead?.addrWidth ||
      frontRead?.dataWidth != description.frontRead?.dataWidth ||
      frontWrite?.addrWidth != description.frontWrite?.addrWidth ||
      frontWrite?.dataWidth != description.frontWrite?.dataWidth ||
      hardware.length != description.hardware.length ||
      hardware.keys
          .any((address) => !description.hardware.containsKey(address))) {
    throw ArgumentError('Runtime ports do not match the model description.');
  }
  for (final entry in hardware.entries) {
    final ports = description.hardware[entry.key]!;
    if (entry.value.hasRead != ports.hasRead ||
        entry.value.hasWrite != ports.hasWrite ||
        entry.value.config.width !=
            description.registers[entry.key]!.config.width) {
      throw ArgumentError(
          'Hardware view does not match the model description.');
    }
  }
  final largestAddress =
      description.registers.keys.reduce((a, b) => a > b ? a : b);
  final stores = <int, SparseMemoryStorage>{};
  for (final entry in description.registers.entries) {
    final width = entry.value.config.width;
    final storage = stores.putIfAbsent(
        width,
        () => SparseMemoryStorage(
            addrWidth: max(1, largestAddress.bitLength), dataWidth: width));
    _registers[entry.key] = _CsrModelRegister(entry.value, storage);
  }
  _names.addAll(description.names);
  _reads.addAll(description.reads.map((address, target) =>
      MapEntry(address, (_registers[target.$1.address]!, target.$2))));
  _writes.addAll(description.writes.map((address, target) =>
      MapEntry(address, (_registers[target.$1.address]!, target.$2))));
  _hardware.addAll(hardware);
  for (final entry in hardware.entries) {
    if (!entry.value.hasRead) {
      continue;
    }
    final register = _registers[entry.key]!;
    final commands = register.config.fields
        .where((field) => field.access == CsrFieldAccess.writeOnly)
        .toList(growable: false);
    _hardwareReads[entry.key] =
        (output: entry.value.rdData!, commands: commands);
    if (register.config.isFrontdoorWritable && commands.isNotEmpty) {
      _commandHardware.add(entry.key);
    }
  }
  for (final signal in [
    _reset,
    if (frontWrite != null) ...[
      frontWrite.en,
      frontWrite.addr,
      frontWrite.data,
    ],
    for (final interface in hardware.values)
      if (interface.hasWrite) ...[interface.wrEn!, interface.wrData!],
  ]) {
    _sampled[signal] = signal.value;
    _cancelGlitches.add(signal.glitch.listen((_) {
      if (Simulator.phase == SimulatorPhase.clkStable) {
        _deferred.add(signal);
      } else {
        _sampled[signal] = signal.value;
        if (Simulator.phase != SimulatorPhase.outOfTick) {
          _racing.add(signal);
        }
      }
    }).cancel);
  }
  _cancelGlitches.add(_clk.glitch.listen((event) {
    _oldClock ??= event.previousValue;
    _clockChanged = true;
  }).cancel);
  if (description.asyncReset) {
    _cancelGlitches.add(_reset.glitch.listen((event) {
      _oldReset ??= event.previousValue;
      _resetChanged = true;
    }).cancel);
  }
  _subscriptions.add(Simulator.clkStable.listen((_) => _tick()));
  _subscriptions.add(Simulator.postTick.listen((_) {
    for (final signal in _deferred) {
      _sampled[signal] = signal.value;
    }
    _deferred.clear();
    _racing.clear();
    for (final address in _deferredHardware) {
      _setHardwareActive(address, _hardware[address]!.wrEn!);
    }
    _deferredHardware.clear();
  }));
  if (frontRead != null) {
    _cancelGlitches
        .add(frontRead.addr.glitch.listen((_) => _driveRead()).cancel);
  }
  if (frontWrite != null && _commandHardware.isNotEmpty) {
    for (final signal in [frontWrite.en, frontWrite.addr, frontWrite.data]) {
      _cancelGlitches.add(signal.glitch
          .listen((_) => _driveHardware(_commandHardware))
          .cancel);
    }
  }
  for (final entry in hardware.entries) {
    final enable = entry.value.wrEn;
    if (enable != null) {
      _hardwareEnables[enable] = entry.key;
      void update() {
        if (Simulator.phase == SimulatorPhase.clkStable) {
          _deferredHardware.add(entry.key);
        } else {
          _setHardwareActive(entry.key, enable);
        }
      }

      update();
      _cancelGlitches.add(enable.glitch.listen((_) => update()).cancel);
    }
  }
  Simulator.registerEndOfSimulationAction(dispose);
  _driveRead();
  _driveHardware();
}