#include "poll_model.hpp" #include #include #include #include static std::vector decode(const std::string &word) { std::vector result; if (word == "-") return result; for (std::size_t i = 0; i < word.size(); i += 2) result.push_back(static_cast(std::stoul(word.substr(i, 2), nullptr, 16))); return result; } static std::string encode(const std::vector &data) { if (data.empty()) return "-"; std::ostringstream output; output << std::hex << std::setfill('0'); for (auto value : data) output << std::setw(2) << static_cast(value); return output.str(); } int main() { ps::Model banks[2]; int selected = 0; std::string command; while (std::cin >> command) { auto &model = banks[selected]; int a, b, c, nonce; unsigned mask; std::uint64_t token; std::string word; if (command == "U") { std::cin >> selected; std::cout << "U " << selected; } else if (command == "Z") { model.reset(); std::cout << "Z 0"; } else if (command == "C") { std::cin >> a >> b >> c; std::cout << "C " << model.create(a, b, c); } else if (command == "T") { std::cin >> a >> b >> c; std::cout << "T " << model.transfer(a, b, c); } else if (command == "D") { std::cin >> a; std::cout << "D " << model.destroy(a); } else if (command == "F") { std::cin >> a >> word; auto bytes = decode(word); std::cout << "F " << model.feed(a, bytes); } else if (command == "G" || command == "E") { std::cin >> a >> b; int status = command == "G" ? model.grant(a, b) : model.spend(a, b); std::cout << command << ' ' << status; } else if (command == "L") { std::cin >> a >> mask; std::cout << "L " << model.latch(a, mask); } else if (command == "A") { std::cin >> a; std::cout << "A " << model.acknowledge(a); } else if (command == "R") { std::cin >> a >> b >> c >> word; auto output = decode(word); int status = model.read(a, b, c, output); std::cout << "R " << status << ' ' << encode(output); } else if (command == "Q") { std::cin >> a >> nonce; ps::DeviceView view; view.capacity = view.queued = view.credits = nonce; view.flags = static_cast(nonce); view.generation = static_cast(nonce); view.data = {0x55, 0xaa}; int status = model.inspect(a, view); std::cout << "Q " << status << ' ' << view.capacity << ' ' << view.queued << ' ' << view.credits << ' ' << view.flags << ' ' << view.generation << ' ' << encode(view.data); } else if (command == "B") { std::cin >> a >> b >> mask >> token; int status = model.begin(a, b, mask, token); std::cout << "B " << status << ' ' << token; } else if (command == "S") { std::cin >> a >> token >> mask; int status = model.step(a, token, mask); std::cout << "S " << status << ' ' << mask; } else if (command == "X" || command == "I" || command == "V") { std::cin >> a >> token; int status = command == "X" ? model.cancel(a, token) : command == "I" ? model.interrupt(a, token) : model.release(a, token); std::cout << command << ' ' << status; } else if (command == "W") { std::cin >> a >> nonce; ps::WatchView view; view.token = view.seen = view.pending = view.wakes = static_cast(nonce); view.device = view.phase = view.registered = nonce; view.requested = view.ready = static_cast(nonce); int status = model.watch(a, view); std::cout << "W " << status << ' ' << view.token << ' ' << view.device << ' ' << view.requested << ' ' << view.phase << ' ' << view.registered << ' ' << view.ready << ' ' << view.seen << ' ' << view.pending << ' ' << view.wakes; } else if (command == "J") { std::cin >> token >> a >> nonce; ps::Record record; record.sequence = record.token = record.generation = static_cast(nonce); record.kind = record.device = record.actor = nonce; std::vector output{record}; std::uint64_t next = static_cast(nonce); int status = model.journal(token, a, output, next); std::cout << "J " << status << ' ' << next << ' ' << output.size(); for (const auto &entry : output) std::cout << ' ' << entry.sequence << ' ' << entry.kind << ' ' << entry.device << ' ' << entry.actor << ' ' << entry.token << ' ' << entry.generation; } std::cout << '\n'; } return 0; }