generateSystemC method

String generateSystemC({
  1. required String className,
  2. SystemCModelOptions options = const SystemCModelOptions(),
})

Generates a combinational-lookup, synchronous-update SystemC module.

Internal state is four-state; two-state ports convert at the boundary according to SystemCModelOptions.unknownOutputs.

Implementation

String generateSystemC({
  required String className,
  SystemCModelOptions options = const SystemCModelOptions(),
}) {
  if (!Sanitizer.isSanitary(className)) {
    throw ArgumentError.value(className, 'className', 'Invalid identifier.');
  }
  final io = _SystemCPorts(className, options);
  final twoState = io.twoState;
  final emit = io.emit;
  const clockCheck = '''
  bit clock=clk.read(), previous=old_clock;
  old_clock=clock;
  if(!known(previous) || !known(clock)) {
    clear(sc_dt::SC_LOGIC_X);
    changed.notify(sc_core::SC_ZERO_TIME);
    return;
  }
  if(previous!=sc_dt::SC_LOGIC_0 || clock!=sc_dt::SC_LOGIC_1) return;''';
  final source = StringBuffer()
    ..writeln('#pragma once')
    ..writeln('#include <systemc>')
    ..writeln('SC_MODULE($className) {')
    ..writeln('  using bit=sc_dt::sc_logic;')
    ..writeln('  using key=sc_dt::sc_lv<$keyWidth>;');
  for (final (ports, direction) in [
    (inputPorts, 'in'),
    (outputPorts, 'out')
  ]) {
    for (final port in ports.entries) {
      source.writeln('  sc_core::sc_$direction<${io.type(port.value)}> '
          '${port.key}{"${port.key}"};');
    }
  }
  source.writeln('''
key keys[$entryCount];
bit valid[$entryCount];
bit old_clock=sc_dt::SC_LOGIC_X;
bool reset_seen=false;
sc_core::sc_event changed;
static sc_dt::sc_lv<1> lv(const bit& value) {
  sc_dt::sc_lv<1> result; result[0]=value; return result;
}
template<int W> static sc_dt::sc_lv<W> lv(const sc_dt::sc_lv<W>& value) {
  return value;
}
static bool known(bit x) {
  return x==sc_dt::SC_LOGIC_0 || x==sc_dt::SC_LOGIC_1;
}
static bit normalize(bit x) {
  return x==sc_dt::SC_LOGIC_Z ? bit(sc_dt::SC_LOGIC_X) : x;
}
static bit equal(bit a, bit b) {
  return known(a) && known(b) ? bit(a==b) : bit(sc_dt::SC_LOGIC_X);
}
template<int W> static bit matches_index(const sc_dt::sc_lv<W>& addr, int index) {
  bit result=sc_dt::SC_LOGIC_1;
  for(int i=0;i<W;++i)
    result=result & equal(addr[i],bit(i<31 && ((index>>i)&1)));
  return result;
}
void clear(bit value) {
  for(int i=0;i<$entryCount;++i) { keys[i]=key(value); valid[i]=value; }
}
void tick() {
  ${twoState ? '' : clockCheck}
  bit rst=${io.scalar('reset')};
  if(rst==sc_dt::SC_LOGIC_1) { clear(sc_dt::SC_LOGIC_0); reset_seen=true; }
  else if(rst!=sc_dt::SC_LOGIC_0) clear(sc_dt::SC_LOGIC_X);
  else {
    for(int i=0;i<$entryCount;++i) {''');
  for (var u = 0; u < updateIndexWidths.length; u++) {
    source.writeln('''
      { bit selected=${io.scalar('update_en_$u')} & matches_index(${io.vector('update_index_$u', updateIndexWidths[u])},i);
        if(selected==sc_dt::SC_LOGIC_1) {
          key value=${io.vector('update_key_$u', keyWidth)};
          for(int b=0;b<$keyWidth;++b) value[b]=normalize(value[b]);
          keys[i]=value; valid[i]=normalize(${io.scalar('update_entryValid_$u')});
        } else if(selected!=sc_dt::SC_LOGIC_0) {
          keys[i]=key(sc_dt::SC_LOGIC_X); valid[i]=sc_dt::SC_LOGIC_X;
        }
      }''');
  }
  source.writeln('''
    }
  }
  changed.notify(sc_core::SC_ZERO_TIME);
}
void drive() {''');
  for (var j = 0; j < lookupIndexWidths.length; j++) {
    final w = lookupIndexWidths[j];
    source.writeln('''
  { key query=${io.vector('lookup_key_$j', keyWidth)}; int index=0; bool hit=false;
    for(int i=0;i<$entryCount && !hit;++i) {
      if(valid[i]!=sc_dt::SC_LOGIC_1) continue;
      bool same=true;
      for(int b=0;b<$keyWidth;++b)
        if(bit(keys[i][b])!=bit(query[b])) same=false;
      if(same) { index=i; hit=true; }
    }
    ${emit('lookup_index_$j', 'sc_dt::sc_lv<$w>(index)', w)}
    ${emit('lookup_hit_$j', 'lv(bit(hit))', 1)}
  }''');
  }
  if (trackValidity) {
    source.writeln('''
  { bool settled=true; unsigned count=0;
    for(int i=0;i<$entryCount;++i) {
      if(!known(valid[i])) settled=false;
      else if(valid[i]==sc_dt::SC_LOGIC_1) ++count;
    }
    sc_dt::sc_lv<$countWidth> total(sc_dt::SC_LOGIC_X);
    bit all=sc_dt::SC_LOGIC_X, none=sc_dt::SC_LOGIC_X;
    if(settled) {
      total=sc_dt::sc_lv<$countWidth>(count);
      all=bit(count==${entryCount}u); none=bit(count==0u);
    }
    ${emit('valid_count', 'total', countWidth)}
    ${emit('full', 'lv(all)', 1)}
    ${emit('empty', 'lv(none)', 1)}
  }''');
  }
  source
    ..writeln('  }')
    ..writeln('  SC_CTOR($className) {')
    ..writeln('    clear(sc_dt::SC_LOGIC_X);')
    ..writeln('    SC_METHOD(tick); sensitive << '
        '${twoState ? 'clk.pos()' : 'clk'}; dont_initialize();')
    ..write('    SC_METHOD(drive); sensitive << changed');
  for (var j = 0; j < lookupIndexWidths.length; j++) {
    source.write(' << lookup_key_$j');
  }
  source
    ..writeln(';')
    ..writeln('  }')
    ..writeln('};');
  return source.toString();
}