ChimeraTK-DeviceAccess 03.26.00
Loading...
Searching...
No Matches
NumericAddressedRegisterCatalogue.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
5
7#include "Exception.h"
8#include "MapFileParser.h"
9#include "NumericAddress.h"
10#include "predicates.h"
11
12#include <algorithm>
13#include <cmath>
14#include <stdexcept>
15#include <utility>
16
17namespace ChimeraTK {
18
19 /********************************************************************************************************************/
20
22 uint64_t address_, uint32_t nBytes_, uint64_t bar_, uint32_t width_, int32_t nFractionalBits_, bool signedFlag_,
23 Access dataAccess_, Type dataType_, std::vector<size_t> interruptId_,
24 std::optional<DoubleBufferInfo> doubleBufferInfo_)
25 : pathName(pathName_), nElements(nElements_), elementPitchBits(nElements_ > 0 ? nBytes_ / nElements_ * 8 : 0),
26 bar(bar_), address(address_), registerAccess(dataAccess_), interruptId(std::move(interruptId_)),
27 doubleBuffer(std::move(doubleBufferInfo_)),
28 channels({{0, dataType_, width_, nFractionalBits_, signedFlag_,
29 nElements_ > 0 ? ChimeraTK::DataType("int" + std::to_string(elementPitchBits)) :
31 assert(channels.size() == 1);
32
33 // make sure . and / is treated as similar as possible
34 pathName.setAltSeparator(".");
35
36 // consistency checks
37 if(nBytes_ > 0 && nElements_ > 0) {
38 if(nBytes_ % nElements_ != 0) {
39 // nBytes_ must be divisible by nElements_
40 throw logic_error("Number of bytes is not a multiple of number of elements for register " + pathName +
41 ". Check your map file!");
42 }
43 }
44
45 computeDataDescriptor();
46 }
47
48 /********************************************************************************************************************/
49
51 uint64_t address_, uint32_t nElements_, uint32_t elementPitchBits_, std::vector<ChannelInfo> channelInfo_,
52 Access dataAccess_, std::vector<size_t> interruptId_, std::optional<DoubleBufferInfo> doubleBufferInfo_)
53 : pathName(pathName_), nElements(nElements_), elementPitchBits(elementPitchBits_), bar(bar_), address(address_),
54 registerAccess(dataAccess_), interruptId(std::move(interruptId_)), doubleBuffer(std::move(doubleBufferInfo_)),
55 channels(std::move(channelInfo_)) {
56 assert(!channels.empty());
57
58 // make sure . and / is treated as similar as possible
60
62 }
63
64 /********************************************************************************************************************/
65
67 // Determine DataDescriptor. If there are multiple channels, use the "biggest" data type.
68 Type dataType = Type::VOID;
69 uint32_t width = 0;
70 int32_t nFractionalBits = 0;
71 bool signedFlag = false;
72 for(auto& c : channels) {
73 if(int(c.dataType) > int(dataType)) dataType = c.dataType;
74 if(c.width + c.nFractionalBits + c.signedFlag > width + nFractionalBits + signedFlag) {
75 width = c.width;
76 nFractionalBits = c.nFractionalBits;
77 signedFlag = c.signedFlag;
78 }
79 }
80
81 // set raw data type
82 DataType rawDataInfo{DataType::none};
83 if(channels.size() == 1) {
84 if(elementPitchBits == 0) {
85 rawDataInfo = DataType::Void;
86 }
87 else if(elementPitchBits == 8) {
88 rawDataInfo = DataType::int8;
89 }
90 else if(elementPitchBits == 16) {
91 rawDataInfo = DataType::int16;
92 }
93 else if(elementPitchBits == 32) {
94 rawDataInfo = DataType::int32;
95 }
96 else if(elementPitchBits == 64) {
97 rawDataInfo = DataType::int64;
98 }
99 else {
100 if(dataType != Type::ASCII) {
102 "Unsupported raw size: " + std::to_string(elementPitchBits) + " bits in register " + pathName);
103 }
104 }
105 }
106
107 // set "cooked" data type
108 if(dataType == Type::IEEE754) {
109 if(width == 32) {
110 // Largest possible number +- 3e38, smallest possible number 1e-45
111 // However, the actual precision is only 23+1 bit, which is < 1e9 relevant
112 // digits. Hence, we don't have to add the 3e38 and the 1e45, but just add
113 // the leading 0. comma and sign to the largest 45 digits
115 false, // isIntegral
116 true, // isSigned
117 3 + 45, // nDigits
118 45, // nFractionalDigits
119 rawDataInfo); // we have integer in the transport layer, or none if multiplexed
120 }
121 else if(width == 64) {
122 // smallest possible 5e-324, largest 2e308
124 false, // isIntegral
125 true, // isSigned
126 3 + 325, // nDigits
127 325, // nFractionalDigits
128 rawDataInfo);
129 }
130 else {
131 throw logic_error("Wrong data width for data type IEEE754 for register " + pathName + ". Check your map file!");
132 }
133 }
134 else if(dataType == Type::FIXED_POINT) {
135 if(width > 1) { // numeric type
136
137 if(nFractionalBits > 0) {
138 auto nDigits = static_cast<size_t>(
139 std::ceil(std::log10(std::pow(2, width))) + (signedFlag ? 1 : 0) + (nFractionalBits != 0 ? 1 : 0));
140 size_t nFractionalDigits = std::ceil(std::log10(std::pow(2, nFractionalBits)));
141
143 false, // isIntegral
144 signedFlag, // isSigned
145 nDigits, nFractionalDigits, rawDataInfo);
146 }
147 else {
148 auto nDigits =
149 static_cast<size_t>(std::ceil(std::log10(std::pow(2, width + nFractionalBits))) + (signedFlag ? 1 : 0));
150
152 true, // isIntegral
153 signedFlag, // isSigned
154 nDigits, 0, rawDataInfo);
155 }
156 }
157 else if(width == 1) { // boolean
159 }
160 else { // width == 0 -> nodata
162 }
163 }
164 else if(dataType == Type::ASCII) {
166 }
167 else if(dataType == Type::VOID) {
169 }
170 }
171
172 /********************************************************************************************************************/
173
175 return (address == rhs.address) && (bar == rhs.bar) && (nElements == rhs.nElements) && (channels == rhs.channels) &&
176 (pathName == rhs.pathName) && (elementPitchBits == rhs.elementPitchBits) &&
178 (interruptId == rhs.interruptId);
179 }
180
181 /********************************************************************************************************************/
182
186
187 /********************************************************************************************************************/
188
190 return interruptId;
191 }
192
193 /********************************************************************************************************************/
194
199
200 /********************************************************************************************************************/
201
205
206 /********************************************************************************************************************/
207
209 return !operator==(rhs);
210 }
211
212 /********************************************************************************************************************/
213 /********************************************************************************************************************/
214
216 const RegisterPath& registerPathName) const {
217 auto path = registerPathName;
218 path.setAltSeparator(".");
219
220 if(path.startsWith(numeric_address::BAR())) {
221 // special treatment for numeric addresses
222 auto components = path.getComponents();
223 if(components.size() != 3) {
224 throw ChimeraTK::logic_error("Illegal numeric address: '" + (path) + "'");
225 }
226 auto bar = std::stoi(components[1]);
227 size_t pos = components[2].find_first_of('*');
228 auto address = std::stoi(components[2].substr(0, pos));
229 size_t nBytes;
230 if(pos != std::string::npos) {
231 nBytes = std::stoi(components[2].substr(pos + 1));
232 }
233 else {
234 nBytes = sizeof(int32_t);
235 }
236 auto nElements = nBytes / sizeof(int32_t);
237 if(nBytes == 0 || nBytes % sizeof(int32_t) != 0) {
238 throw ChimeraTK::logic_error("Illegal numeric address: '" + (path) + "'");
239 }
240 return NumericAddressedRegisterInfo(path, nElements, address, nBytes, bar);
241 }
242 if(path.startsWith("!")) {
243 auto canonicalInterrupt = _canonicalInterrupts.find(path);
244 if(canonicalInterrupt == _canonicalInterrupts.end()) {
245 throw ChimeraTK::logic_error("Illegal canonical interrupt path: '" + (path) + "'");
246 }
247 return NumericAddressedRegisterInfo(path, 0, 0, 0, 0, 0, 0, false,
249 canonicalInterrupt->second);
250 }
252 }
253
254 /********************************************************************************************************************/
255
256 [[nodiscard]] bool NumericAddressedRegisterCatalogue::hasRegister(const RegisterPath& registerPathName) const {
257 if(registerPathName.startsWith(numeric_address::BAR())) {
259 return true;
260 }
261 if(_canonicalInterrupts.find(registerPathName) != _canonicalInterrupts.end()) {
262 return true;
263 }
264 return BackendRegisterCatalogue::hasRegister(registerPathName);
265 }
266
267 /********************************************************************************************************************/
268
269 const std::set<std::vector<size_t>>& NumericAddressedRegisterCatalogue::getListOfInterrupts() const {
270 return _listOfInterrupts;
271 }
272
273 /********************************************************************************************************************/
274
277 _listOfInterrupts.insert(registerInfo.interruptId);
278 RegisterPath canonicalName = "!" + std::to_string(registerInfo.interruptId.front());
279 std::vector<size_t> canonicalID = {registerInfo.interruptId.front()};
280 _canonicalInterrupts[canonicalName] = canonicalID;
281 for(auto subId = ++registerInfo.interruptId.begin(); subId != registerInfo.interruptId.end(); ++subId) {
282 canonicalName += ":" + std::to_string(*subId);
283 canonicalID.push_back(*subId);
284 _canonicalInterrupts[canonicalName] = canonicalID;
285 }
286 }
288 }
289
290 /********************************************************************************************************************/
291
292 std::unique_ptr<BackendRegisterCatalogueBase> NumericAddressedRegisterCatalogue::clone() const {
293 std::unique_ptr<BackendRegisterCatalogueBase> c = std::make_unique<NumericAddressedRegisterCatalogue>();
294 auto* casted_c = dynamic_cast<NumericAddressedRegisterCatalogue*>(c.get());
295 fillFromThis(casted_c);
296 return c;
297 }
298
299 /********************************************************************************************************************/
300
303 target->_listOfInterrupts = _listOfInterrupts;
304 target->_canonicalInterrupts = _canonicalInterrupts;
305 target->_dataConsistencyRealms = _dataConsistencyRealms;
306 }
307
308 /********************************************************************************************************************/
309
311 const RegisterPath& registerPath, const std::string& realmName) {
312 _dataConsistencyRealms[registerPath] = realmName;
313 }
314
315 /********************************************************************************************************************/
316
317 std::shared_ptr<async::DataConsistencyRealm> NumericAddressedRegisterCatalogue::getDataConsistencyRealm(
318 const std::vector<size_t>& qualifiedAsyncDomainId) const {
319 if(qualifiedAsyncDomainId.empty()) {
320 return {};
321 }
322
323 // iterate _dataConsistencyRealms and check if the registerPath matches the qualifiedAsyncDomainId
324 for(auto const& [registerPath, realmName] : _dataConsistencyRealms) {
325 if(getBackendRegister(registerPath).getQualifiedAsyncId() == qualifiedAsyncDomainId) {
327 return store.getRealm(realmName);
328 }
329 }
330 return {};
331 }
332
333 /********************************************************************************************************************/
334
336 const std::vector<size_t>& qualifiedAsyncDomainId) const {
337 if(qualifiedAsyncDomainId.empty()) {
338 return {};
339 }
340
341 // iterate _dataConsistencyRealms and check if the registerPath matches the qualifiedAsyncDomainId
342 for(auto const& [registerPath, realmName] : _dataConsistencyRealms) {
343 if(getBackendRegister(registerPath).getQualifiedAsyncId() == qualifiedAsyncDomainId) {
344 return registerPath;
345 }
346 }
347 return {};
348 }
349
350 /********************************************************************************************************************/
351
352} // namespace ChimeraTK
void addRegister(const BackendRegisterInfo &registerInfo)
Add register information to the catalogue.
void fillFromThis(BackendRegisterCatalogue< BackendRegisterInfo > *target) const
Helper function for clone functions.
virtual BackendRegisterInfo getBackendRegister(const RegisterPath &registerPathName) const
Note: Override this function if backend has "hidden" registers which are not added to the map and hen...
bool hasRegister(const RegisterPath &registerPathName) const override
Check if register with the given path name exists.
unsigned int getNumberOfDimensions() const
Return number of dimensions of this register.
Class describing the actual payload data format of a register in an abstract manner.
A class to describe which of the supported data types is used.
@ int16
Signed 16 bit integer.
@ int32
Signed 32 bit integer.
@ none
The data type/concept does not exist, e.g. there is no raw transfer (do not confuse with Void)
@ int64
Signed 64 bit integer.
@ int8
Signed 8 bit integer.
void addDataConsistencyRealm(const RegisterPath &registerPath, const std::string &realmName)
const std::set< std::vector< size_t > > & getListOfInterrupts() const
bool hasRegister(const RegisterPath &registerPathName) const override
Check if register with the given path name exists.
void addRegister(const NumericAddressedRegisterInfo &registerInfo)
void fillFromThis(NumericAddressedRegisterCatalogue *target) const
NumericAddressedRegisterInfo getBackendRegister(const RegisterPath &registerPathName) const override
Note: Override this function if backend has "hidden" registers which are not added to the map and hen...
std::set< std::vector< size_t > > _listOfInterrupts
set of interrupt IDs.
std::map< RegisterPath, std::vector< size_t > > _canonicalInterrupts
A canonical interrupt path consists of an exclamation mark, followed by a numeric interrupt and a col...
std::unique_ptr< BackendRegisterCatalogueBase > clone() const override
Create deep copy of the catalogue.
std::shared_ptr< async::DataConsistencyRealm > getDataConsistencyRealm(const std::vector< size_t > &qualifiedAsyncDomainId) const override
Return DataConsistencyRealm for the given qualified AsyncDomainId.
RegisterPath getDataConsistencyKeyRegisterPath(const std::vector< size_t > &qualifiedAsyncDomainId) const override
Return RegisterPath for the register containing the DataConsistencyKey value for the given qualified ...
std::map< RegisterPath, std::string > _dataConsistencyRealms
Map of data consistency key register paths to realm names.
uint32_t nElements
Number of elements in register.
std::vector< ChannelInfo > channels
Define per-channel information (bit interpretation etc.), 1D/scalars have exactly one entry.
Access
Enum describing the access mode of the register:
uint64_t bar
Upper part of the address (name originally from PCIe, meaning now generalised)
uint32_t elementPitchBits
Distance in bits (!) between two elements (of the same channel)
bool operator==(const ChimeraTK::NumericAddressedRegisterInfo &rhs) const
bool operator!=(const ChimeraTK::NumericAddressedRegisterInfo &rhs) const
std::vector< size_t > getQualifiedAsyncId() const override
Return the fully qualified async::SubDomain ID.
uint64_t address
Lower part of the address relative to BAR, in bytes.
Access registerAccess
Data access direction: Read, write, read and write or interrupt.
NumericAddressedRegisterInfo(RegisterPath const &pathName_={}, uint32_t nElements_=0, uint64_t address_=0, uint32_t nBytes_=0, uint64_t bar_=0, uint32_t width_=32, int32_t nFractionalBits_=0, bool signedFlag_=true, Access dataAccess_=Access::READ_WRITE, Type dataType_=Type::FIXED_POINT, std::vector< size_t > interruptId_={}, std::optional< DoubleBufferInfo > doubleBuffer_=std::nullopt)
Constructor to set all data members for scalar/1D registers.
Class to store a register path name.
bool startsWith(const RegisterPath &compare) const
check if the register path starts with the given path
void setAltSeparator(const std::string &altSeparator)
set alternative separator.
static DataConsistencyRealmStore & getInstance()
Exception thrown when a logic error has occured.
Definition Exception.h:51
RegisterPath BAR()
The numeric_address::BAR() function can be used to directly access registers by numeric addresses,...
STL namespace.
std::string to_string(const std::string &v)
uint32_t width
Number of significant bits in the register.