Jump to content

Preventing a method from triggering more than once in a given cycle


betabandido

Recommended Posts

I'm developing a performance simulator for a hardware unit. The hardware runs at 1GHz and therefore I define a clock cycle as:

sc_time clk(1, SC_NS)

In a specific module requests arrive to the input port, triggering a method that places them into a request buffer. At some point requests that are ready for processing are moved out from the request buffer and placed into another buffer. The overall design would look like:

void Receive() {
  auto x = input.read();
  buffer_.push_back(x);
  process_buffer_event_.notify(clk);
}

void ProcessBuffer() {
  for (auto it = begin(buffer_); it != end(buffer_); ++it) {
    if (process_entry(*it)) { // returns true if a request was successfully moved to the second buffer
      buffer_.erase(it);
      break; // only one request can be processed per cycle
    }
  }

  if (!buffer_.empty())
    process_buffer_event_.notify(clk);
}

So, ProcessBuffer will trigger either when a new request arrives or when a request is processed and there are still requests in the buffer. However, there is another case when ProcessBuffer should trigger. In the module there are some resources that can be either in use or idle. Whenever a resource becomes idle, ProcessBuffer must immediately trigger (without waiting until the next cycle):

void ReleaseResources() {
  bool any_released = false;
  for (auto& r : resources_)
    any_released = r->try_to_release() || any_released;

  if (any_released)
    process_buffer_event_.notify(SC_ZERO_TIME);
}

However, because of this piece of code, ProcessBuffer now may trigger twice in a given cycle (in the same nanosecond). I suspect it is related to using SC_ZERO_TIME, but I'm not really sure. This creates an issue since now two requests might be moved into the second buffer. I suppose I could guard against executing ProcessBuffer twice by doing something like:

void ProcessBuffer() {
  if (sc_time_stamp() == last_time_processed)
    return;
  
  last_time_processed = sc_time_stamp();

  for (auto it = begin(buffer_); it != end(buffer_); ++it) {
    if (process_entry(*it)) {
      buffer_.erase(it);
      break; // only one request can be processed per cycle
    }
  }

  if (!buffer_.empty())
    process_buffer_event_.notify(clk);
}

But I was just wondering whether there is some mechanism in SystemC to properly prevent a method from triggering more than once.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...