SystemC Library API Reference Manual
Reference documentation for the Simics SystemC Library.
 
Loading...
Searching...
No Matches
simics_adapter.h
Go to the documentation of this file.
1// -*- mode: C++; c-file-style: "virtutech-c++" -*-
2
3/*
4 © 2015 Intel Corporation
5
6 This software and the related documents are Intel copyrighted materials, and
7 your use of them is governed by the express license under which they were
8 provided to you ("License"). Unless the License provides otherwise, you may
9 not use, modify, copy, publish, distribute, disclose or transmit this software
10 or the related documents without Intel's prior written permission.
11
12 This software and the related documents are provided as is, with no express or
13 implied warranties, other than those that are expressly stated in the License.
14*/
15
16#ifndef SIMICS_SYSTEMC_IFACE_SIMICS_ADAPTER_H
17#define SIMICS_SYSTEMC_IFACE_SIMICS_ADAPTER_H
18
19#include <simics/iface/interface-info.h>
20#include <simics/devs/io-memory.h>
25
26#include <string>
27#include <vector>
28
29namespace simics {
30namespace systemc {
31namespace iface {
32
45template<typename TSimicsInterface>
46class SimicsAdapter : public simics::iface::InterfaceInfo,
47 public Registrant<SimicsAdapterInterface> {
48 public:
49 SimicsAdapter(const char *name, TSimicsInterface iface)
50 : name_(name),
51 iface_(iface),
52 conf_class_(NULL),
53 map_adapter_(true) {
54 }
55
56 // simics::iface::InterfaceInfo
57 std::string name() const override {
58 return name_;
59 }
60 const interface_t *cstruct() const override {
61 return &iface_;
62 }
63
64 void set_simics_class(conf_class_t *conf_class) override {
65 if (!conf_class_)
66 conf_class_ = conf_class;
67 }
68 conf_class_t *simics_class() const override {
69 return conf_class_;
70 }
71 void set_map_adapter(bool map) override {
72 map_adapter_ = map;
73 }
74 bool map_adapter() const override {
75 return map_adapter_;
76 }
77
78 protected:
79 template<typename TBase, typename TInterface>
80 static TInterface *adapterWithoutLocking(conf_object_t *obj) {
81 FATAL_ERROR_IF(!obj, "NULL object passed to Simics interface");
82 TBase *so = static_cast<TBase*>(SIM_object_data(obj));
83 TInterface *adapter = dynamic_cast<TInterface*>(so);
84 FATAL_ERROR_IF(adapter == NULL, "Unable to locate GasketAdapter");
85 return adapter;
86 }
87 // This version is mainly used by the tool_simics_adapter that the lock
88 // is not implemented on obj but on the provider/connection object.
89 template<typename TBase, typename TInterface>
90 static SimicsLock<TInterface> adapter(conf_object_t *obj,
91 conf_object_t *obj_lock) {
92 SimulationInterface *simulation = cached_simulation_;
93 if (obj_lock != cached_obj_lock_
94 || cached_obj_id_ != SIM_object_id(obj_lock)) {
95 ConfObject *lock_base =
96 static_cast<ConfObject*>(SIM_object_data(obj_lock));
97 simulation = dynamic_cast<SimulationInterface*>(lock_base);
98 if (simulation == nullptr) {
99 conf_object_t *parent = SIM_port_object_parent(obj_lock);
100 if (parent) {
101 // Check port's parent
102 lock_base = static_cast<ConfObject*>(
103 SIM_object_data(parent));
104 simulation = dynamic_cast<SimulationInterface*>(lock_base);
105 }
106 }
107 FATAL_ERROR_IF(simulation == NULL, "Unable to locate Simulation");
108 cached_obj_lock_ = obj_lock;
109 cached_obj_id_ = SIM_object_id(obj_lock);
110 cached_simulation_ = simulation;
111 }
112 return {simulation, adapterWithoutLocking<TBase, TInterface>(obj)};
113 }
114 template<typename TBase, typename TInterface>
115 static SimicsLock<TInterface> adapter(conf_object_t *obj) {
116 return adapter<TBase, TInterface>(obj, obj);
117 }
118 template<typename TBase, typename TInterface>
119 std::vector<std::string> descriptionBase(conf_object_t *obj,
120 DescriptionType type) {
121 if (SIM_object_class(obj) != conf_class_ )
122 return {};
123
124 TInterface *iface = adapterWithoutLocking<TBase, TInterface>(obj);
125 auto *adapter = dynamic_cast<DescriptionInterface<TInterface> *>(iface);
126 if (!adapter)
127 return {};
128
129 std::vector<std::string> description = adapter->description(type);
130 if (description.empty())
131 return {};
132
133 description.insert(description.begin(), name_);
134 description.insert(description.begin(), SIM_object_name(obj));
135 return description;
136 }
137
138 private:
139 std::string name_;
140 TSimicsInterface iface_;
141 conf_class_t *conf_class_;
142 bool map_adapter_;
143 // Under extreme scenarios (100M consecutive interface calls) the
144 // dynamic_cast within adapter() becomes a problem. By caching the
145 // result, this is avoided. This was highlighted (and verified) by VTune
146 static conf_object_t *cached_obj_lock_;
147 static std::string cached_obj_id_;
148 static SimulationInterface* cached_simulation_;
149};
150
151template<typename TSimicsInterface> conf_object_t*
152SimicsAdapter<TSimicsInterface>::cached_obj_lock_ = nullptr;
153template<typename TSimicsInterface> std::string
154SimicsAdapter<TSimicsInterface>::cached_obj_id_ = "";
155template<typename TSimicsInterface> SimulationInterface*
156SimicsAdapter<TSimicsInterface>::cached_simulation_ = nullptr;
157
160
171template <typename InterfaceSimicsAdapter>
172InterfaceSimicsAdapter& createAdapter() {
173 static InterfaceSimicsAdapter adapter;
174 return adapter;
175}
176
178
179} // namespace iface
180} // namespace systemc
181} // namespace simics
182
183#endif
Definition: description_interface.h:34
Definition: registry.h:88
Definition: simics_lock.h:27
Base class for mapping Simics interface to a C++ interface.
Definition: simics_adapter.h:47
static TInterface * adapterWithoutLocking(conf_object_t *obj)
Definition: simics_adapter.h:80
const interface_t * cstruct() const override
Definition: simics_adapter.h:60
void set_map_adapter(bool map) override
Definition: simics_adapter.h:71
conf_class_t * simics_class() const override
Definition: simics_adapter.h:68
static SimicsLock< TInterface > adapter(conf_object_t *obj)
Definition: simics_adapter.h:115
SimicsAdapter(const char *name, TSimicsInterface iface)
Definition: simics_adapter.h:49
std::vector< std::string > descriptionBase(conf_object_t *obj, DescriptionType type)
Definition: simics_adapter.h:119
static SimicsLock< TInterface > adapter(conf_object_t *obj, conf_object_t *obj_lock)
Definition: simics_adapter.h:90
void set_simics_class(conf_class_t *conf_class) override
Definition: simics_adapter.h:64
std::string name() const override
Definition: simics_adapter.h:57
bool map_adapter() const override
Definition: simics_adapter.h:74
Interface to the SystemC simulation.
Definition: simulation_interface.h:27
InterfaceSimicsAdapter & createAdapter()
Create a SimicsAdapter that can be registered with a ConfClass.
Definition: simics_adapter.h:172
DescriptionType
Definition: description_interface.h:25
Definition: pci_bus_interface.h:24