restorePortableSnapshot method

void restorePortableSnapshot(
  1. String text
)

Validates and restores a portable execution image atomically.

Implementation

void restorePortableSnapshot(String text) {
  final tokens = text.trim().split(RegExp(r'\s+'));
  if (tokens.length < 6 ||
      tokens[0] != 'RHCL_SRAM_V1' ||
      tokens[1] != '$addrWidth' ||
      tokens[2] != '$dataWidth' ||
      tokens[3] != '$entryCount') {
    throw const FormatException('Invalid SRAM state header.');
  }
  final count = int.tryParse(tokens[4]);
  if (!RegExp(r'^(0|[1-9][0-9]*)$').hasMatch(tokens[4]) ||
      count == null ||
      count < 0 ||
      count > entryCount ||
      tokens.length != 6 + 2 * count) {
    throw const FormatException('Invalid SRAM state entry count.');
  }
  LogicValue value(String bits, {required bool allowZ}) {
    if (bits.length != dataWidth ||
        !(allowZ ? RegExp(r'^[01xXzZ]+$') : RegExp(r'^[01xX]+$'))
            .hasMatch(bits)) {
      throw const FormatException('Invalid SRAM state value.');
    }
    return LogicValue.ofString(bits.toLowerCase());
  }

  final output = value(tokens[5], allowZ: true);
  final entries = <BigInt, LogicValue>{};
  for (var index = 0; index < count; index++) {
    final bits = tokens[6 + 2 * index];
    if (bits.length != addrWidth || !RegExp(r'^[01]+$').hasMatch(bits)) {
      throw const FormatException('Invalid SRAM state address.');
    }
    final address = BigInt.parse(bits, radix: 2);
    if (address >= BigInt.from(entryCount) || entries.containsKey(address)) {
      throw const FormatException('Out-of-range or duplicate SRAM address.');
    }
    entries[address] = value(tokens[7 + 2 * index], allowZ: false);
  }
  restore(InferredSramImage._(
      entryCount,
      addrWidth,
      dataWidth,
      [
        cloneTypedPayload(wrPorts.single.data),
        cloneTypedPayload(rdPorts.single.data)
      ],
      MemoryStorageImage.wide(entries),
      output,
      _resetSeen));
}