dumpMemString method

String dumpMemString({
  1. int radix = 16,
  2. int bitsPerAddress = 8,
})

Dumps the contents of memory to a verilog-compliant hex file.

The address will increment each bitsPerAddress bits of data between address @ annotations. Data is output according to the provided radix, which must be a positive power of 2 up to 16.

Implementation

String dumpMemString({int radix = 16, int bitsPerAddress = 8}) {
  if (radix <= 0 || (radix & (radix - 1) != 0) || radix > 16) {
    throw RohdHclException('Radix must be a positive power of 2 (max 16).');
  }

  if (isEmpty) {
    return '';
  }

  final bitsPerChar = log2Ceil(radix);

  final addrs = addresses.sorted();

  final memString = StringBuffer();

  LogicValue? currentAddr;

  for (final addr in addrs) {
    if (currentAddr != addr) {
      memString.writeln('@${addr.toInt().toRadixString(16)}');
      currentAddr = addr;
    }

    final data = getData(addr)!;
    memString.writeln(data
        .toInt()
        .toRadixString(radix)
        .padLeft(data.width ~/ bitsPerChar, '0'));
    currentAddr = currentAddr! +
        LogicValue.ofInt(dataWidth ~/ bitsPerAddress, addrWidth);
  }

  return memString.toString();
}