1#include "loopback/LoopbackIP.h"
19 {
AppID(
"loopback_inst", 0),
AppID(
"loopback_tohw")}, lastLookup);
21 throw std::runtime_error(
"No loopback_tohw port found");
23 {
AppID(
"loopback_inst", 0),
AppID(
"loopback_fromhw")}, lastLookup);
25 throw std::runtime_error(
"No loopback_fromhw port found");
32 uint8_t sendVal = 0x5a;
37 if (recvMsg.
getSize() !=
sizeof(uint8_t))
38 throw std::runtime_error(
"Unexpected loopback recv size");
39 uint8_t got = *recvMsg.
as<uint8_t>();
41 throw std::runtime_error(
"Loopback byte mismatch");
43 std::cout <<
"loopback i8 ok: 0x" << std::hex << (int)got << std::dec <<
"\n";
50 throw std::runtime_error(
"No structFunc port found");
54 throw std::runtime_error(
"structFunc not a FuncService::Function");
57 esi_system::ArgStruct arg{};
59 arg.b =
static_cast<int8_t
>(-7);
62 const auto *res = resMsg.
as<esi_system::ResultStruct>();
64 int8_t expectedX =
static_cast<int8_t
>(arg.b + 1);
65 if (res->x != expectedX || res->y != arg.b)
66 throw std::runtime_error(
"Struct func result mismatch");
68 std::cout <<
"struct func ok: b=" << (int)arg.b <<
" x=" << (
int)res->x
69 <<
" y=" << (int)res->y <<
"\n";
76 throw std::runtime_error(
"No oddStructFunc port found");
80 throw std::runtime_error(
"oddStructFunc not a FuncService::Function");
83 esi_system::OddStruct arg{};
85 arg.b =
static_cast<int8_t
>(-17);
87 arg.inner.q =
static_cast<int8_t
>(-7);
92 const auto *res = resMsg.
as<esi_system::OddStruct>();
94 uint16_t expectA =
static_cast<uint16_t
>(arg.a + 1);
95 int8_t expectB =
static_cast<int8_t
>(arg.b - 3);
96 uint8_t expectP =
static_cast<uint8_t
>(arg.inner.p + 5);
97 int8_t expectQ =
static_cast<int8_t
>(arg.inner.q + 2);
98 uint8_t expectR0 =
static_cast<uint8_t
>(arg.inner.r[0] + 1);
99 uint8_t expectR1 =
static_cast<uint8_t
>(arg.inner.r[1] + 2);
100 if (res->a != expectA || res->b != expectB || res->inner.p != expectP ||
101 res->inner.q != expectQ || res->inner.r[0] != expectR0 ||
102 res->inner.r[1] != expectR1)
103 throw std::runtime_error(
"Odd struct func result mismatch");
105 std::cout <<
"odd struct func ok: a=" << res->a <<
" b=" << (int)res->b
106 <<
" p=" << (
int)res->inner.p <<
" q=" << (int)res->inner.q
107 <<
" r0=" << (
int)res->inner.r[0] <<
" r1=" << (int)res->inner.r[1]
115 throw std::runtime_error(
"No arrayFunc port found");
119 throw std::runtime_error(
"arrayFunc not a FuncService::Function");
122 int8_t argArray[1] = {
static_cast<int8_t
>(-3)};
124 func->call(
MessageData(
reinterpret_cast<const uint8_t *
>(argArray),
128 const auto *res = resMsg.
as<esi_system::ResultArray>();
129 int8_t a = (*res)[0];
130 int8_t b = (*res)[1];
131 int8_t expect0 = argArray[0];
132 int8_t expect1 =
static_cast<int8_t
>(argArray[0] + 1);
134 bool ok = (a == expect0 && b == expect1) || (a == expect1 && b == expect0);
136 throw std::runtime_error(
"Array func result mismatch");
145 std::cout <<
"array func ok: " << (int)low <<
" " << (
int)high <<
"\n";
179 std::vector<SerialCoordData>
coords;
184 std::vector<SerialCoordData> &
coords)
195 return {
reinterpret_cast<const uint8_t *
>(&
header),
sizeof(
header)};
197 return {
reinterpret_cast<const uint8_t *
>(
coords.data()),
200 return {
reinterpret_cast<const uint8_t *
>(&
footer),
sizeof(
footer)};
202 throw std::out_of_range(
"SerialCoordInput: invalid segment index");
223 size_t numCoords = 100;
224 uint32_t xTrans = 10, yTrans = 20;
227 std::mt19937 rng(0xDEADBEEF);
228 std::uniform_int_distribution<uint32_t> dist(0, 1000000);
229 std::vector<Coord> inputCoords;
230 inputCoords.reserve(numCoords);
231 for (uint32_t i = 0; i < numCoords; ++i)
232 inputCoords.push_back({dist(rng), dist(rng)});
236 throw std::runtime_error(
"Serial coord translate test: no "
237 "'coord_translator_serial' child found");
239 auto &ports = child->second->getPorts();
240 auto portIter = ports.find(
AppID(
"translate_coords_serial"));
241 if (portIter == ports.end())
242 throw std::runtime_error(
243 "Serial coord translate test: no 'translate_coords_serial' port found");
252 std::vector<SerialCoordData> coords;
253 for (
auto &c : inputCoords)
254 coords.emplace_back(c.x, c.y);
258 (void)translateCoords.
call(batch).get();
261int main(
int argc,
const char *argv[]) {
263 cli.description(
"Loopback cosim test using generated ESI headers.");
264 if (
int rc = cli.
esiParse(argc, argv))
266 if (!cli.get_help_ptr()->empty())
273 Manifest manifest(ctxt, info.getJsonManifest());
274 Accelerator *accel = manifest.buildAccelerator(*conn);
275 conn->getServiceThread()->addPoll(*accel);
277 std::cout <<
"depth: 0x" << std::hex << esi_system::LoopbackIP::depth
287 }
catch (std::exception &e) {
Abstract class representing a connection to an accelerator.
Top level accelerator class.
Services provide connections to 'bundles' – collections of named, unidirectional communication channe...
T * getAs() const
Cast this Bundle port to a subclass which is actually useful.
ReadChannelPort & getRawRead(const std::string &name) const
WriteChannelPort & getRawWrite(const std::string &name) const
Get access to the raw byte streams of a channel.
Common options and code for ESI runtime tools.
Context & getContext()
Get the context.
AcceleratorConnection * connect()
Connect to the accelerator using the specified backend and connection.
int esiParse(int argc, const char **argv)
Run the parser.
AcceleratorConnections, Accelerators, and Manifests must all share a context.
BundlePort * resolvePort(const AppIDPath &path, AppIDPath &lastLookup) const
Attempt to resolve a path to a port.
const std::map< AppID, Instance * > & getChildren() const
Access the module's children by ID.
virtual void error(const std::string &subsystem, const std::string &msg, const std::map< std::string, std::any > *details=nullptr)
Report an error.
Class to parse a manifest.
A logical chunk of data representing serialized data.
const T * as() const
Cast to a type.
size_t getSize() const
Get the size of the data in bytes.
static MessageData from(T &t)
Cast from a type to its raw bytes.
A ChannelPort which reads data from the accelerator.
virtual void connect(std::function< bool(MessageData)> callback, const ConnectOptions &options={})
virtual void read(MessageData &outData)
Specify a buffer to read into.
Abstract multi-segment message.
std::future< ResultT > call(const ArgT &arg)
A ChannelPort which sends data to the accelerator.
void write(const MessageData &data)
A very basic blocking write API.
virtual void connect(const ConnectOptions &options={}) override
Set up a connection to the accelerator.
A function call which gets attached to a service port.
Information about the Accelerator system.
static void runOddStructFunc(Accelerator *accel)
int main(int argc, const char *argv[])
static void serialCoordTranslateTest(Accelerator *accel)
static void runLoopbackI8(Accelerator *accel)
static void runStructFunc(Accelerator *accel)
static void runArrayFunc(Accelerator *accel)
Test the CoordTranslator module using message translation.
SerialCoordData(uint32_t x, uint32_t y)
A contiguous, non-owning view of bytes within a SegmentedMessageData.
SerialCoordOutputData data
SerialCoordOutputHeader header