generateSystemC method

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

Emits a standalone SystemC definition using className.

Implementation

@override
String generateSystemC({
  required String className,
  SystemCModelOptions options = const SystemCModelOptions(),
}) {
  if (!Sanitizer.isSanitary(className)) {
    throw ArgumentError.value(className, 'className', 'Invalid identifier.');
  }
  final d = description;
  if (options.portType == SystemCModelPortType.twoState) {
    final core = '${className}__RohdHclSharedStorageQueueCore';
    return generateTwoStateSystemCAdapter(
      className: className,
      coreClassName: core,
      coreSource: generateSystemC(className: core),
      inputPorts: d.inputPorts,
      outputPorts: d.outputPorts,
      options: options,
      qualifiedBy: const {'popIndex': 'popValid', 'popData': 'popValid'},
    );
  }
  final n = d.numChannels;
  final e = d.entryCount;
  String type(int width) =>
      width == 1 ? 'sc_dt::sc_logic' : 'sc_dt::sc_lv<$width>';
  String make(int width, String value) => width == 1
      ? 'sc_dt::sc_logic(bool(($value)&1))'
      : 'sc_dt::sc_lv<$width>(std::uint64_t($value))';
  String unknown(int width) => '${type(width)}(sc_dt::SC_LOGIC_X)';
  String value(int width, String signal) =>
      width == 1 ? '($signal==sc_dt::SC_LOGIC_1)' : '$signal.to_uint64()';
  final enableBit = n == 1 ? 'enables' : 'enables[i]';
  final occupiedBit = n == 1 ? 'occupied' : 'occupied[i]';
  final maxOccupancy = 1 << d.indexWidth;
  return '''
#pragma once
#include <systemc>
#include <array>
#include <cstdint>

SC_MODULE($className) {
using bit=sc_dt::sc_logic;
using word=${type(d.dataWidth)};
sc_core::sc_in<bit> clk{"clk"}, reset{"reset"}, push{"push"}, pop{"pop"};
sc_core::sc_in<${type(d.channelWidth)}> pushChannel{"pushChannel"};
sc_core::sc_in<word> pushData{"pushData"};
sc_core::sc_in<${type(n)}> channelEnable{"channelEnable"};
sc_core::sc_out<${type(d.indexWidth)}> pushIndex{"pushIndex"}, popIndex{"popIndex"};
sc_core::sc_out<${type(d.channelWidth)}> popChannel{"popChannel"};
sc_core::sc_out<word> popData{"popData"};
sc_core::sc_out<bit> popValid{"popValid"};
sc_core::sc_out<${type(n)}> occupiedChannels{"occupiedChannels"};
${d.generateFull ? 'sc_core::sc_out<bit> full{"full"};' : ''}
${d.generateEmpty ? 'sc_core::sc_out<bit> empty{"empty"};' : ''}
bool known_state=false;
std::array<std::uint64_t,$e> free_words{};
std::uint64_t free_write=0, free_read=0;
bool free_full=false;
std::array<std::uint64_t,$e> next_pointers{};
std::array<word,$e> payloads;
std::uint64_t mask_start=0;
std::array<std::uint64_t,$n> occupancy{}, first_index{}, last_index{};
bit old_clock{sc_dt::SC_LOGIC_X};
sc_core::sc_event changed;

static std::uint64_t increment(std::uint64_t pointer) {
  return pointer==$e-1 ? 0 : pointer+1;
}
bool free_empty() const { return free_write==free_read && !free_full; }
// Whether the enables are known; grant is -1 when nothing is arbitable.
bool arbitrate(int& grant) const {
  grant=-1;
  auto enables=channelEnable.read();
  if(!enables.is_01()) return false;
  for(int k=0;k<$n;++k) {
    int i=int((mask_start+k)%$n);
    if(occupancy[i]!=0 && $enableBit==sc_dt::SC_LOGIC_1) { grant=i; break; }
  }
  return true;
}
void reset_state() {
  for(int i=0;i<$e;++i) {
    free_words[i]=i; next_pointers[i]=0; payloads[i]=word(0);
  }
  free_write=0; free_read=0; free_full=true; mask_start=0;
  occupancy.fill(0); first_index.fill(0); last_index.fill(0);
  known_state=true;
}
bool commit() {
  bit push_bit=push.read(), pop_bit=pop.read();
  if(!push_bit.is_01() || !pop_bit.is_01()) return false;
  int grant;
  if(!arbitrate(grant)) return false;
  bool pushing=push_bit==sc_dt::SC_LOGIC_1, popping=pop_bit==sc_dt::SC_LOGIC_1;
  if(popping && grant<0) return false;
  int channel=-1;
  if(pushing) {
    auto requested=pushChannel.read();
    if(!requested.is_01()) return false;
    std::uint64_t index=${value(d.channelWidth, 'requested')};
    if(index>=$n) return false;
    channel=int(index);
  }
  std::uint64_t push_entry=free_words[free_read];
  std::uint64_t pop_entry=popping ? first_index[grant] : 0;
  std::uint64_t replacement=
      popping && pop_entry<$e ? next_pointers[pop_entry] : 0;
  std::array<std::uint64_t,$n> next_first;
  for(int i=0;i<$n;++i) {
    bool p=i==channel, q=popping && i==grant;
    if(occupancy[i]==0 && p) next_first[i]=push_entry;
    else if(p && q && occupancy[i]==1) next_first[i]=push_entry;
    else if(q && occupancy[i]>=2) next_first[i]=replacement;
    else next_first[i]=first_index[i];
  }
  if(pushing && free_empty())
    SC_REPORT_ERROR("$className", "pushed while no entries were free");
  if(popping && free_full && !pushing)
    SC_REPORT_ERROR("$className",
        "released an entry while every entry was free");
  if(channel>=0) {
    if(occupancy[channel]!=0 && last_index[channel]<$e)
      next_pointers[last_index[channel]]=push_entry;
    if(push_entry<$e) payloads[push_entry]=pushData.read();
  }
  bool next_full=(free_full && popping==pushing) ||
      (free_read==increment(free_write) && popping && !pushing);
  if(popping) {
    free_words[free_write]=pop_entry; free_write=increment(free_write);
  }
  if(pushing) free_read=increment(free_read);
  free_full=next_full;
  if(grant>=0) mask_start=grant+1;
  for(int i=0;i<$n;++i) {
    bool p=i==channel, q=popping && i==grant;
    std::uint64_t count=occupancy[i];
    if(p && !q) count=count==$maxOccupancy ? 0 : count+1;
    if(q && !p) count=count==0 ? $maxOccupancy : count-1;
    occupancy[i]=count;
    first_index[i]=next_first[i];
    if(p) last_index[i]=push_entry;
  }
  return true;
}
void tick() {
  bit clock=clk.read(), previous=old_clock; old_clock=clock;
  if(!clock.is_01() || !previous.is_01()) known_state=false;
  else if(previous==sc_dt::SC_LOGIC_0 && clock==sc_dt::SC_LOGIC_1) {
    bit rst=reset.read();
    if(!rst.is_01()) known_state=false;
    else if(rst==sc_dt::SC_LOGIC_1) reset_state();
    else if(known_state && !commit()) known_state=false;
  } else return;
  changed.notify(sc_core::SC_ZERO_TIME);
}
void drive() {
  if(!known_state) {
    pushIndex.write(${unknown(d.indexWidth)});
    popIndex.write(${unknown(d.indexWidth)});
    popChannel.write(${unknown(d.channelWidth)});
    popData.write(${unknown(d.dataWidth)});
    popValid.write(bit(sc_dt::SC_LOGIC_X));
    occupiedChannels.write(${unknown(n)});
    ${d.generateFull ? 'full.write(bit(sc_dt::SC_LOGIC_X));' : ''}
    ${d.generateEmpty ? 'empty.write(bit(sc_dt::SC_LOGIC_X));' : ''}
    return;
  }
  int grant;
  bool grant_known=arbitrate(grant);
  bool granted=grant_known && grant>=0;
  popValid.write(grant_known ? bit(grant>=0) : bit(sc_dt::SC_LOGIC_X));
  popChannel.write(grant_known ? ${make(d.channelWidth, 'grant<0 ? 0 : grant')}
      : ${unknown(d.channelWidth)});
  popIndex.write(granted ? ${make(d.indexWidth, 'first_index[grant]')}
      : ${unknown(d.indexWidth)});
  bit pop_bit=pop.read();
  if(pop_bit==sc_dt::SC_LOGIC_0) popData.write(word(0));
  else if(pop_bit==sc_dt::SC_LOGIC_1 && granted)
    popData.write(first_index[grant]<$e ? payloads[first_index[grant]] : word(0));
  else popData.write(${unknown(d.dataWidth)});
  pushIndex.write(${make(d.indexWidth, 'free_words[free_read]')});
  ${type(n)} occupied(0);
  bool any=false;
  for(int i=0;i<$n;++i) {
    $occupiedBit=bit(occupancy[i]!=0); any=any || occupancy[i]!=0;
  }
  occupiedChannels.write(occupied);
  ${d.generateFull ? 'full.write(bit(free_empty()));' : ''}
  ${d.generateEmpty ? 'empty.write(bit(!any));' : ''}
}
SC_CTOR($className) {
  for(auto& payload : payloads) payload=word(0);
  SC_METHOD(tick); sensitive << clk; dont_initialize();
  SC_METHOD(drive); sensitive << changed << pop << channelEnable;
}
};
''';
}