AddressedMemoryImage<DataType extends Object>.fromJson constructor
Decodes an addressed-memory image.
Implementation
factory AddressedMemoryImage.fromJson(
Map<String, Object?> json,
DataType Function(Object? encoded) decodeData,
) {
if (json['kind'] != kind || json['version'] != version) {
throw const FormatException(
'Expected $kind state image version $version.');
}
final depth = json['depth'];
final dataWidth = json['dataWidth'];
final encodedEntries = json['entries'];
if (depth is! int || depth <= 0) {
throw const FormatException('depth must be a positive integer.');
}
if (dataWidth is! int || dataWidth <= 0) {
throw const FormatException('dataWidth must be a positive integer.');
}
if (encodedEntries is! List<Object?>) {
throw const FormatException('entries must be a list.');
}
final entries = <int, DataType>{};
for (final encodedEntry in encodedEntries) {
if (encodedEntry is! Map<String, Object?>) {
throw const FormatException('Each memory entry must be an object.');
}
final address = encodedEntry['address'];
if (address is! int || address < 0 || address >= depth) {
throw FormatException(
'Memory entry address must be between 0 and ${depth - 1}.');
}
if (entries.containsKey(address)) {
throw FormatException('Duplicate memory entry address $address.');
}
entries[address] = decodeData(encodedEntry['data']);
}
return AddressedMemoryImage(
depth: depth,
dataWidth: dataWidth,
entries: entries,
);
}