-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdevice.hh
More file actions
84 lines (70 loc) · 2.21 KB
/
device.hh
File metadata and controls
84 lines (70 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#pragma once
#ifndef XERXES_DEVICE_HH
#define XERXES_DEVICE_HH
#include "def.hh"
#include "ext/toml.hpp"
#include "simulation.hh"
#include "system.hh"
#include "topology.hh"
namespace xerxes {
// General device class for all devices in the simulation.
class Device {
protected:
Simulation *sim;
Topology *topology;
TopoID self;
std::string name_;
// Schedule one transit event.
void sched_transit(Tick tick);
void send_pkt_to(Packet pkt, TopoID dst) {
auto to = topology->next_node(self, dst);
if (to == nullptr)
return;
pkt.from = self;
to->send(pkt);
auto a = sim->system()->find_dev(to->id());
a->sched_transit(pkt.arrive);
}
void send_pkt(Packet pkt) { send_pkt_to(pkt, pkt.dst); }
// Receive a packet from the TopoNode's buffer.
Packet receive_pkt() {
auto pkt = Packet{};
topology->get_node(self)->receive(pkt);
return pkt;
}
void show_all_pkt() {
XerxesLogger::debug() << name() << " has packets: ";
topology->get_node(self)->show_all_pkt();
}
void log_transit_normal(const Packet &pkt) {
XerxesLogger::debug()
<< name() << " transit packet " << pkt.id << " from " << pkt.from
<< " to " << pkt.dst << " at " << pkt.arrive << std::endl;
}
public:
Device(Simulation *sim, std::string name = "default_name")
: sim(sim), self(sim->topology()->new_node()), name_(name) {
topology = sim->topology();
}
virtual ~Device() {}
std::string name() const { return name_ + "#" + std::to_string(self); }
TopoID id() const { return self; }
// Default transit, do nothing.
virtual void transit() {
auto pkt = receive_pkt();
XerxesLogger::warning()
<< "!DEFAULLT TRANSIT! " << name() << " received packet " << pkt.id
<< " from " << pkt.from << " to " << pkt.dst << " at " << pkt.arrive
<< std::endl;
if (pkt.dst == self) {
return;
}
send_pkt(pkt);
}
virtual void log_stats(std::ostream &os) {}
auto get_transit_func() {
return [this]() { transit(); };
}
};
} // namespace xerxes
#endif // XERXES_DEVICE_HH