connectCosimulation static method

Future<void> connectCosimulation(
  1. CosimConfig cosimConfig
)

Starts the SystemVerilog simulation and connects it to the ROHD simulator.

Implementation

static Future<void> connectCosimulation(CosimConfig cosimConfig) async {
  _enableLogger = cosimConfig.enableLogging;

  final connection = await cosimConfig.connect();
  _socket = connection.socket;

  // catch errors if the socket shuts down
  // ignore: avoid_types_on_closure_parameters
  unawaited(_socket!.done.catchError((Object error) {
    logger?.info('Encountered error upon socket completion, '
        'shutting down cosim: $error');
    endCosim();
  }));

  _socket!.listen(
    (event) {
      _receive(utf8.decode(event));
    },
    onDone: () {
      logger?.info('Received "done" message over socket, shutting down...');
      endCosim();
    },
    cancelOnError: true,
    // ignore: avoid_types_on_closure_parameters
    onError: (Object err, StackTrace stackTrace) {
      logger?.info(
          'Encountered error while listening to socket, ending cosim: $err');
      logger?.info('> Stack trace: \n $stackTrace');
      endCosim();
    },
  );

  await _setupPortHandshakes(
    onEnd: connection.disconnect,
    throwOnUnexpectedEnd: cosimConfig.throwOnUnexpectedEnd,
  );

  Simulator.registerEndOfSimulationAction(() async {
    logger?.finer('Simulation ended... closing everything down.');

    try {
      await connection.disconnect();

      _send('END');

      await _socket!.flush().catchError(_socketErrorHandler);

      // leave some time for the END to get there
      await Future<void>.delayed(const Duration(seconds: 1));

      await _socket!.close().catchError(_socketErrorHandler);
      logger?.finer('Closed!');

      _socket!.destroy();
      _socket = null;

      logger?.finer('All done!');
    } on Exception {
      logger?.warning('Failed to gracefully close cosim connection.');
    }
  });
}