#include "eeprom.hpp" #include namespace eeprom { bool Bus::valid_id(Number id) { return id >= 0 && id <= 3; } bool Bus::valid_bytes(const std::vector& values, std::size_t limit) { if (values.size() > limit) { return false; } for (Number value : values) { if (value < 0 || value > 255) { return false; } } return true; } Bus::Device Bus::state(Number id) const { return devices_.at(id); } void Bus::record(Number id, char operation, Number first, Number second, const std::vector& data) { Device& device = devices_.at(id); if (operation == 'S') { device.cursor = first; return; } if (operation == 'W') { device.pending = data; device.pending_start = device.cursor; device.busy = device.cycle; device.accepted += 1; return; } if (operation == 'P') { device.polls += 1; if (device.pending.empty()) { return; } if (device.busy > 0) { device.busy -= 1; return; } Number base = device.pending_start / 8 * 8; for (std::size_t i = 0; i < device.pending.size(); ++i) { Number offset = (device.pending_start % 8 + static_cast(i)) % 8; device.bytes[static_cast(base + offset)] = device.pending[i]; } device.cursor = (device.pending_start + static_cast(device.pending.size())) % 256; device.pending.clear(); device.pending_start = 0; device.committed += 1; return; } if (operation == 'R') { device.cursor = (device.cursor + first) % 256; } (void)second; } Reply Bus::mount(Number id, Number fill, Number cycle) { if (!valid_id(id)) { return {Code::RANGE, 0, {}}; } if (fill < 0 || fill > 255) { return {Code::RANGE, 0, {}}; } if (cycle < 0 || cycle > 4) { return {Code::RANGE, 0, {}}; } if (devices_.count(id) != 0) { return {Code::EXISTS, 0, {}}; } Device device; device.cycle = cycle; device.bytes.assign(256, fill); (void)device; return {}; } Reply Bus::unmount(Number id) { if (!valid_id(id)) { return {Code::RANGE, 0, {}}; } if (devices_.count(id) == 0) { return {Code::ABSENT, 0, {}}; } if (!state(id).pending.empty()) { return {Code::BUSY, 0, {}}; } devices_.erase(id); return {}; } Reply Bus::seek(Number id, Number address) { if (!valid_id(id)) { return {Code::RANGE, 0, {}}; } if (address < 0 || address > 255) { return {Code::RANGE, 0, {}}; } if (devices_.count(id) == 0) { return {Code::ABSENT, 0, {}}; } if (!state(id).pending.empty()) { return {Code::BUSY, 0, {}}; } record(id, 'S', address, 0, {}); return {}; } Reply Bus::page(Number id, const std::vector& values) { if (!valid_id(id)) { return {Code::RANGE, 0, {}}; } if (!valid_bytes(values, 8)) { return {Code::RANGE, 0, {}}; } if (devices_.count(id) == 0) { return {Code::ABSENT, 0, {}}; } if (!state(id).pending.empty()) { return {Code::BUSY, 0, {}}; } if (!values.empty()) { record(id, 'W', 0, 0, values); } return {}; } Reply Bus::poll(Number id) { if (!valid_id(id)) { return {Code::RANGE, 0, {}}; } if (devices_.count(id) == 0) { return {Code::ABSENT, 0, {}}; } Device before = state(id); record(id, 'P', 0, 0, {}); if (!before.pending.empty() && before.busy > 0) { return {Code::BUSY, 0, {}}; } return {Code::OK, static_cast(before.pending.size()), {}}; } Reply Bus::read(Number id, Number count) { if (!valid_id(id)) { return {Code::RANGE, 0, {}}; } if (count < 0 || count > 256) { return {Code::RANGE, 0, {}}; } if (devices_.count(id) == 0) { return {Code::ABSENT, 0, {}}; } Device before = state(id); if (!before.pending.empty()) { return {Code::BUSY, 0, {}}; } Reply reply; reply.completed = count; for (Number i = 0; i < count; ++i) { std::size_t index = static_cast((before.cursor + i) % 256); reply.data.push_back(before.bytes[index]); } record(id, 'R', count, 0, {}); return reply; } Snapshot Bus::inspect(Number id) const { Snapshot snapshot; if (!valid_id(id)) { snapshot.code = Code::RANGE; return snapshot; } if (devices_.count(id) == 0) { snapshot.code = Code::ABSENT; return snapshot; } Device device = state(id); snapshot.cursor = device.cursor; snapshot.busy = device.busy; snapshot.accepted = device.accepted; snapshot.committed = device.committed; snapshot.polls = device.polls; snapshot.pending_start = device.pending_start; snapshot.pending = device.pending; snapshot.bytes = device.bytes; return snapshot; } }