61 const std::string &command =
"") {
62 parseParams(paramsPath);
63 selectCommand(command, paramsPath);
64 scratchpadBo = run.get_ctrl_scratchpad_bo();
65 boMap = scratchpadBo.map<uint32_t *>();
66 if (scratchpadBo.size() < scratchpadSizeBytes)
67 throw std::runtime_error(
"ParameterScratchpad: BO size (" +
68 std::to_string(scratchpadBo.size()) +
69 ") < required scratchpad size (" +
70 std::to_string(scratchpadSizeBytes) +
")");
80 const std::string ¶msPath,
81 const std::string &command =
"")
83 parseParams(paramsPath);
84 selectCommand(command, paramsPath);
85 if (bufferSizeBytes < scratchpadSizeBytes)
86 throw std::runtime_error(
"ParameterScratchpad: buffer size (" +
87 std::to_string(bufferSizeBytes) +
88 ") < required scratchpad size (" +
89 std::to_string(scratchpadSizeBytes) +
")");
98 void writeBytes(
const std::string &name,
const void *data,
size_t len) {
100 std::memcpy(&bits, data, std::min(len,
sizeof(bits)));
113 void writeBits(
const std::string &name, uint32_t bits) {
114 if (unslotted.count(name))
115 throw std::runtime_error(
"ParameterScratchpad: no command reads "
117 name +
"', so it has no slot");
118 auto it = paramMap.find(name);
119 if (it == paramMap.end()) {
120 throw std::runtime_error(
"ParameterScratchpad: unknown parameter '" +
123 if (!members.count(name))
124 throw std::runtime_error(
"ParameterScratchpad: command '" + command +
125 "' does not read parameter '" + name +
126 "', whose slot another of its parameters may "
128 if (
auto multiple = offsetMultiples.find(name);
129 multiple != offsetMultiples.end() &&
130 static_cast<int32_t
>(bits) % multiple->second)
131 throw std::invalid_argument(
132 "ParameterScratchpad: value " +
133 std::to_string(
static_cast<int32_t
>(bits)) +
" of '" + name +
134 "' is not a multiple of " + std::to_string(multiple->second) +
135 " elements, which the DMA address it offsets needs to stay "
137 if (
auto range = ranges.find(name); range != ranges.end()) {
138 int32_t value =
static_cast<int32_t
>(bits);
139 if (value < range->second.first || value > range->second.second)
140 throw std::invalid_argument(
141 "ParameterScratchpad: value " + std::to_string(value) +
" of '" +
142 name +
"' is outside [" + std::to_string(range->second.first) +
143 ", " + std::to_string(range->second.second) +
"], " +
144 (repeatParams.count(name)
145 ?
"its task's largest repeat count"
146 :
"which keeps its DMA transfers within their buffers"));
147 values[name] = value;
149 uint8_t idx = it->second;
150 uint32_t encoded = bits;
151 if (coreParams.count(name)) {
155 }
else if (repeatParams.count(name)) {
156 encoded = bits << 16;
158 boMap[idx] = encoded;
169 template <
typename T>
170 void write(
const std::string &name, T value) {
171 static_assert(
sizeof(T) <= 4,
"Parameter values must be at most 32 bits");
173 std::memcpy(&bits, &value,
sizeof(T));
181 for (
const JointBound &bound : jointBounds) {
182 int64_t offset = values.at(bound.offset);
183 int64_t length = values.at(bound.length);
184 int64_t end = offset + bound.lengthStep * length;
186 throw std::invalid_argument(
187 "ParameterScratchpad: '" + bound.offset +
188 "' = " + std::to_string(offset) +
" plus " +
189 std::to_string(bound.lengthStep) +
" * '" + bound.length +
190 "' = " + std::to_string(length) +
" is " + std::to_string(end) +
191 ", above " + std::to_string(bound.max) +
192 ", so a DMA transfer using both runs past its buffer");
196#if TEST_UTILS_USE_XRT
201 scratchpadBo.sync(XCL_BO_SYNC_BO_TO_DEVICE);
206 std::vector<std::string>
names()
const {
207 return std::vector<std::string>(members.begin(), members.end());
211 uint32_t
read(
const std::string &name)
const {
212 if (unslotted.count(name))
213 throw std::runtime_error(
"ParameterScratchpad: no command reads "
215 name +
"', so it has no slot");
216 auto it = paramMap.find(name);
217 if (it == paramMap.end()) {
218 throw std::runtime_error(
"ParameterScratchpad: unknown parameter '" +
221 return boMap[it->second];
225#if TEST_UTILS_USE_XRT
226 xrt::bo scratchpadBo;
228 uint32_t *boMap =
nullptr;
229 size_t scratchpadSizeBytes = 0;
230 std::unordered_map<std::string, uint8_t> paramMap;
231 std::unordered_set<std::string> unslotted;
232 std::unordered_set<std::string> coreParams;
233 std::unordered_set<std::string> repeatParams;
234 std::unordered_map<std::string, std::pair<int32_t, int32_t>> ranges;
235 std::unordered_map<std::string, int32_t> values;
238 std::unordered_map<std::string, int32_t> offsetMultiples;
240 std::string length, offset;
241 int64_t lengthStep, max;
243 std::vector<JointBound> jointBounds;
245 std::unordered_map<std::string, std::unordered_set<std::string>> commands;
246 std::vector<std::string> commandNames;
248 std::unordered_set<std::string> members;
250 void selectCommand(
const std::string &name,
const std::string &path) {
253 if (commandNames.empty())
254 throw std::runtime_error(
"ParameterScratchpad: '" + path +
255 "' lists no command to hold parameters for");
256 if (commandNames.size() > 1) {
257 std::string available;
258 for (
const std::string &entry : commandNames)
259 available += (available.empty() ?
"" :
", ") + entry;
260 throw std::runtime_error(
261 "ParameterScratchpad: '" + path +
"' lists " +
262 std::to_string(commandNames.size()) +
263 " commands, whose parameters may share slots: name the one this "
264 "scratchpad is for, of " +
267 command = commandNames.front();
269 auto it = commands.find(command);
270 if (it == commands.end())
271 throw std::runtime_error(
"ParameterScratchpad: '" + path +
272 "' has no command '" + name +
"'");
273 members = it->second;
277 for (
size_t i = 0; i < scratchpadSizeBytes / 4; i++) {
282 void parseParams(
const std::string &path) {
283 std::ifstream file(path);
284 if (!file.is_open()) {
285 throw std::runtime_error(
"ParameterScratchpad: cannot open '" + path +
308 unsigned numParams = 0, numSlots = 0;
309 file >> numParams >> numSlots;
311 throw std::runtime_error(
312 "ParameterScratchpad: missing parameter and slot counts in '" + path +
314 scratchpadSizeBytes = numSlots * 4;
316 for (
unsigned i = 0; i < numParams; i++) {
317 std::string name, slot, type, kind, min, max;
318 file >> name >> slot >> type >> kind >> min >> max;
320 throw std::runtime_error(
"ParameterScratchpad: malformed entry " +
321 std::to_string(i) +
" in '" + path +
"'");
322 if (paramMap.count(name) || unslotted.count(name))
323 throw std::runtime_error(
324 "ParameterScratchpad: duplicate parameter name '" + name +
"'");
326 unslotted.insert(name);
328 unsigned long idx = 0;
330 idx = std::stoul(slot);
331 }
catch (
const std::logic_error &) {
332 throw std::runtime_error(
"ParameterScratchpad: malformed slot '" +
333 slot +
"' for parameter '" + name +
334 "' in '" + path +
"'");
337 throw std::runtime_error(
"ParameterScratchpad: state_table_idx " +
338 std::to_string(idx) +
" for '" + name +
339 "' is past the " + std::to_string(numSlots) +
340 " slots of '" + path +
"'");
341 paramMap[name] =
static_cast<uint8_t
>(idx);
343 if (kind !=
"core" && kind !=
"addr" && kind !=
"repeat") {
344 throw std::runtime_error(
"ParameterScratchpad: invalid kind '" + kind +
345 "' for parameter '" + name +
"'");
346 }
else if (kind ==
"core") {
347 coreParams.insert(name);
348 }
else if (kind ==
"repeat") {
349 repeatParams.insert(name);
353 ranges[name] = {std::stoi(min), std::stoi(max)};
354 }
catch (
const std::logic_error &) {
355 throw std::runtime_error(
"ParameterScratchpad: malformed range '" +
356 min +
" " + max +
"' for parameter '" +
357 name +
"' in '" + path +
"'");
363 unsigned numJointBounds = 0;
364 file >> numJointBounds;
366 throw std::runtime_error(
367 "ParameterScratchpad: missing joint bound count in '" + path +
"'");
368 for (
unsigned i = 0; i < numJointBounds; i++) {
370 file >> bound.length >> bound.offset >> bound.lengthStep >> bound.max;
372 throw std::runtime_error(
"ParameterScratchpad: malformed joint bound " +
373 std::to_string(i) +
" in '" + path +
"'");
374 for (
const std::string &name : {bound.length, bound.offset})
375 if (!ranges.count(name))
376 throw std::runtime_error(
"ParameterScratchpad: joint bound " +
377 std::to_string(i) +
" names '" + name +
378 "', which has no range, in '" + path +
"'");
379 jointBounds.push_back(bound);
382 unsigned numCommands = 0;
385 throw std::runtime_error(
386 "ParameterScratchpad: missing command count in '" + path +
"'");
387 for (
unsigned i = 0; i < numCommands; i++) {
389 unsigned numRead = 0;
390 file >> name >> numRead;
391 commandNames.push_back(name);
392 std::unordered_set<std::string> &reads = commands[name];
393 for (
unsigned j = 0; j < numRead; j++) {
396 if (file && !paramMap.count(param))
397 throw std::runtime_error(
"ParameterScratchpad: command '" + name +
398 "' names unknown parameter '" + param +
399 "' in '" + path +
"'");
403 throw std::runtime_error(
"ParameterScratchpad: malformed command " +
404 std::to_string(i) +
" in '" + path +
"'");
407 unsigned numMultiples = 0;
408 if (!(file >> numMultiples))
410 for (
unsigned i = 0; i < numMultiples; i++) {
412 int32_t multiple = 0;
413 file >> name >> multiple;
414 if (!file || multiple < 1)
415 throw std::runtime_error(
"ParameterScratchpad: malformed offset "
417 std::to_string(i) +
" in '" + path +
"'");
418 if (!paramMap.count(name))
419 throw std::runtime_error(
"ParameterScratchpad: offset multiple " +
423 name +
"' in '" + path +
"'");
424 offsetMultiples[name] = multiple;