The Sparta Modeling Framework
Loading...
Searching...
No Matches
Consumer.cpp
1// <Consumer.cpp> -*- C++ -*-
2
3#include "Consumer.hpp"
4#include "MessageCategories.hpp"
6
7const char * Consumer::name = "consumer";
8
9Consumer::Consumer (sparta::TreeNode * node,
10 const ConsumerParameterSet * p) :
11 sparta::Unit(node, name),
12 num_producers_(p->num_producers),
13 consumer_log_(node, message_categories::INFO, "Consumer Info Messages")
14{
15 (void)p;
16
17 // Set up the producer go outports -- these are the ports used to
18 // signal the producers that this consumer is ready for it.
19 for(uint32_t i = 0; i < num_producers_; ++i) {
20 std::stringstream str;
21 str << "producer" << i << "_go_port";
22 producer_go_port_.emplace_back(new sparta::SignalOutPort(&unit_port_set_, str.str()));
23 }
24
25 // Register callback to receive data on the InPort
26 consumer_in_port_.
27 registerConsumerHandler(CREATE_SPARTA_HANDLER_WITH_DATA(Consumer, receiveData_, uint32_t));
28
29 // Get the ball rolling
30 sparta::StartupEvent(node, CREATE_SPARTA_HANDLER(Consumer, signalNextProducer_));
31
32}
33
34void Consumer::signalNextProducer_()
35{
36 // Tell the next producer to go
37 producer_go_port_[current_producer_]->send();
38 ++current_producer_;
39 if(current_producer_ == num_producers_) {
40 current_producer_ = 0;
41 }
42}
43
44void Consumer::receiveData_(const uint32_t & dat)
45{
46 sparta_assert(arrived_data_.isValid() == false,
47 "Somehow, data wasn't cleared in this consumer: " << getName());
48 arrived_data_ = dat;
49
50 // Schedule a consumption this cycle
51 ev_data_arrived_.schedule();
52
53 // Signal the next producer
54 signalNextProducer_();
55}
56
57void Consumer::dataArrived_()
58{
59 if(SPARTA_EXPECT_FALSE(consumer_log_)) {
60 consumer_log_ << "Got data '" << arrived_data_ << "' on cycle: " << getClock()->currentCycle();
61 }
62 ++num_consumed_;
63 arrived_data_.clearValid();
64}
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
#define SPARTA_EXPECT_FALSE(x)
A macro for hinting to the compiler a particular condition should be considered most likely false.
#define CREATE_SPARTA_HANDLER_WITH_DATA(clname, meth, dataT)
#define CREATE_SPARTA_HANDLER(clname, meth)
File that defines the StartupEvent class.
static const char * name
Name of this resource. Required by sparta::ResourceFactory.
Definition Consumer.hpp:19
Cycle currentCycle() const
Get the current cycle (uses current tick from the Scheduler)
Definition Clock.hpp:176
const Clock * getClock() const
std::string getName() const
SignalOutPort is used for transferring a simple signal to another module.
StartupEvent is a simple class for scheduling a starting event on the Scheduler. It does not support ...
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:205
void schedule()
Schedule this event with its pre-set delay using the pre-set Clock.
void clearValid()
Clear the validity of this object.
bool isValid() const
Is this value valid.
Macros for handling exponential backoff.