restorePortableSnapshot method

void restorePortableSnapshot(
  1. String image
)

Restores a complete portable raw storage image after validating it.

Images must have exactly the same register addresses and widths. This operation bypasses CSR write effects and does not restore execution state.

Implementation

void restorePortableSnapshot(String image) {
  final tokens = image.trim().split(RegExp(r'\s+'));
  final addresses = _registers.keys.toList()..sort();
  if (tokens.length != 2 + addresses.length * 3 ||
      tokens[0] != 'RHCL_CSR_STORAGE_V1' ||
      tokens[1] != '${addresses.length}') {
    throw const FormatException(
        'Invalid CSR storage image header or length.');
  }
  final values = <BigInt, LogicValue>{};
  for (var index = 0; index < addresses.length; index++) {
    final address = addresses[index];
    final width = _registers[address]!.config.width;
    final offset = 2 + index * 3;
    final bits = tokens[offset + 2];
    if (tokens[offset] != '$address' ||
        tokens[offset + 1] != '$width' ||
        bits.length != width ||
        !RegExp(r'^[01xX]+$').hasMatch(bits)) {
      throw const FormatException('Invalid CSR storage image entry.');
    }
    values[address] = LogicValue.ofString(bits.toLowerCase());
  }
  load(values);
}