#include "model.hpp" #include namespace clockmodel { Reply Bus::configure(Arg id, Arg clock, Arg reset, Arg enable, Arg data) { if (!valid_id(id)) { return error("ID"); } if (!valid_word(clock)) { return error("WORD"); } if (!valid_word(reset)) { return error("WORD"); } if (!valid_word(enable)) { return error("WORD"); } if (!valid_word(data)) { return error("WORD"); } if ((clock & 1) == 0) { return error("CLOCK"); } if ((reset & 1) == 0) { return error("RESET"); } if ((enable & 1) == 0) { return error("ENABLE"); } const auto index = static_cast(id); Device trial = devices_[index]; std::array plan{{ {1, 0}, {0, static_cast(clock)}, {1, static_cast(reset)}, {2, static_cast(enable)}, {3, static_cast(data)} }}; for (const Event& event : plan) { std::string status = apply(trial, event.reg, event.value); if (status != "OK") { return error(status); } } devices_[index] = std::move(trial); return success(); } Reply Bus::burst(Arg id, const std::vector& values) { if (!valid_id(id)) { return error("ID"); } if (values.size() > 16) { return error("COUNT"); } for (Arg value : values) { if (!valid_word(value)) { return error("WORD"); } } if (values.empty()) { return success(); } const auto index = static_cast(id); const Device& current = devices_[index]; std::string permission = data_permission(current); if (permission != "OK") { return error(permission); } const std::size_t available = 16 - current.fifo.size(); if (values.size() > available) { return error("FULL"); } Device trial = current; for (Arg value : values) { const auto word = static_cast(value); std::string status = apply(trial, 3, word); if (status != "OK") { return error(status); } } devices_[index] = std::move(trial); return success(); } Reply Bus::drain(Arg id, Arg count) { if (!valid_id(id)) { return error("ID"); } if (!valid_count(count, 16)) { return error("COUNT"); } if (count == 0) { return success(); } const auto index = static_cast(id); const auto length = static_cast(count); const Device& current = devices_[index]; std::string permission = data_permission(current); if (permission != "OK") { return error(permission); } if (length > current.fifo.size()) { return error("SHORT"); } Device trial = current; Reply reply = success(); reply.values.reserve(length); for (std::size_t offset = 0; offset < length; ++offset) { reply.values.push_back(trial.fifo.front()); trial.fifo.pop_front(); } devices_[index] = std::move(trial); return reply; } }