μHAL
decoders.h
1#ifndef DECODERS_H
2#define DECODERS_H
3
4#include <cstdint>
5#include <cstdio>
6#include <memory>
7#include <optional>
8#include <string_view>
9#include <unordered_map>
10#include <utility>
11#include <variant>
12#include <vector>
13
14#include <sys/types.h>
15
16#include "sdb-defs.h"
17
18#define LNLS_VENDORID 0x1000000000001215
19
21class Printer;
22
23#define CONSTRUCTOR_REGS(type) regs_storage(new type()), regs(*regs_storage)
24
25namespace decoders {
29using data_type = std::variant<std::int32_t, double>;
30
31using data_key = std::pair<std::string_view, std::optional<unsigned>>;
32}
33
39 bool devinfo_is_set = false;
40
41 uint64_t vendor_id;
42 uint32_t device_id;
43 uint8_t major_version;
47 bool check_devinfo = false;
48
49 bool match_devinfo(const struct sdb_device_info &) const;
50
51protected:
52 size_t read_size;
53 void *read_dest;
54
55 void set_read_dest(auto &dest)
56 {
57 read_dest = &dest;
58 read_size = sizeof dest;
59 }
60
61 struct pcie_bars &bars;
62 struct sdb_device_info devinfo;
63 size_t addr;
64
67 RegisterDecoderBase(struct pcie_bars &, const struct sdb_device_info &);
68
70 virtual void read();
71
72public:
73 void check_devinfo_is_set() const;
74 virtual void set_devinfo(const struct sdb_device_info &);
75
76 const device_match_fn match_devinfo_lambda;
77};
78
80 decoders::data_type value;
81 size_t offset;
82 uint32_t mask;
83 unsigned fixed_point_pos = 0;
84 bool multibit;
85 bool is_signed;
86 bool is_fixed_point = false;
87};
88
98 bool is_boolean_value(const char *) const;
99 int32_t try_boolean_value(const char *, int32_t) const;
100
101 std::unique_ptr<RegisterDecoderPrivate> pvt;
102
103 template <class T>
104 void add_data_internal(const char *, decoders::data_key::second_type, T);
105
106 void rf_add_data_internal(
107 const char *, decoders::data_key::second_type, RegisterField);
108
109 size_t register2offset(uint32_t *);
110 uint32_t *offset2register(size_t, void *);
111
112 void write_internal(
113 const char *, std::optional<unsigned>, decoders::data_type, void *);
114
115protected:
118 std::optional<unsigned> number_of_channels;
119
120 std::unordered_map<std::string_view, Printer> printers;
121
122 RegisterDecoder(struct pcie_bars &, const struct sdb_device_info &,
123 std::unordered_map<std::string_view, Printer>);
124
126 void add_general(const char *, int32_t);
128 void add_general_double(const char *, double);
131 void add_channel(const char *, unsigned, int32_t);
134 void add_channel_double(const char *, unsigned, double);
135
139 RegisterField rf_get_bit(uint32_t &, uint32_t);
141 RegisterField rf_extract_value(uint32_t &, uint32_t, bool = false);
144 RegisterField rf_whole_register(uint32_t &value, bool is_signed = false)
145 {
146 return rf_extract_value(value, UINT32_MAX, is_signed);
147 }
150
152 inline void add_general(const char *name, RegisterField rf)
153 {
154 rf_add_data_internal(name, std::nullopt, rf);
155 }
157 inline void add_channel(const char *name, unsigned pos, RegisterField rf)
158 {
159 rf_add_data_internal(name, pos, rf);
160 }
161
164 virtual void read_monitors();
166 virtual void decode() = 0;
169 virtual void decode_monitors();
170
171public:
172 virtual ~RegisterDecoder();
175 void get_data(bool = false);
176 void binary_dump(FILE *) const;
177 virtual void print(FILE *, bool) const;
178
179 template <class T> T get_general_data(const char *name) const
180 {
181 return std::get<T>(get_generic_data(name));
182 }
183 template <class T>
184 T get_channel_data(const char *name, unsigned channel_index) const
185 {
186 return std::get<T>(get_generic_data(name, channel_index));
187 }
188
189 decoders::data_type get_generic_data(
190 const char *, decoders::data_key::second_type = std::nullopt) const;
191
192 std::optional<unsigned> channel;
193
194 inline void write_general(
195 const char *name, decoders::data_type value, void *dest)
196 {
197 write_internal(name, std::nullopt, value, dest);
198 }
199 inline void write_channel(
200 const char *name, unsigned pos, decoders::data_type value, void *dest)
201 {
202 write_internal(name, pos, value, dest);
203 }
204};
205
206#endif
Definition: printer.h:24
Definition: decoders.h:36
bool check_devinfo
Definition: decoders.h:47
RegisterDecoderBase(struct pcie_bars &, const struct sdb_device_info &)
Definition: decoderbase.cc:6
bool devinfo_is_set
Definition: decoders.h:39
virtual void read()
Definition: decoderbase.cc:36
Definition: decoders.h:97
virtual void decode_monitors()
Definition: decoders.cc:162
RegisterField rf_whole_register(uint32_t &value, bool is_signed=false)
Definition: decoders.h:144
void add_general(const char *name, RegisterField rf)
Definition: decoders.h:152
void add_general(const char *, int32_t)
Definition: decoders.cc:86
virtual void read_monitors()
Definition: decoders.cc:160
virtual void decode()=0
void get_data(bool=false)
Definition: decoders.cc:164
RegisterField rf_fixed2float(RegisterField, unsigned)
Definition: decoders.cc:143
RegisterField rf_extract_value(uint32_t &, uint32_t, bool=false)
Definition: decoders.cc:131
RegisterField rf_get_bit(uint32_t &, uint32_t)
Definition: decoders.cc:120
std::optional< unsigned > number_of_channels
Definition: decoders.h:118
void add_general_double(const char *, double)
Definition: decoders.cc:90
void add_channel_double(const char *, unsigned, double)
Definition: decoders.cc:99
void add_channel(const char *name, unsigned pos, RegisterField rf)
Definition: decoders.h:157
void add_channel(const char *, unsigned, int32_t)
Definition: decoders.cc:95
Definition: decoders.cc:27
Definition: decoders.h:79
Definition: pcie-defs.h:19
Definition: sdb-defs.h:7