#include "flash.hpp" #include #include namespace flash { FlashBank::FlashBank() { reset(); } bool FlashBank::valid_id(Number id) { return id >= 0 && id < 4; } Result FlashBank::failure(Code code) { Result result; result.code = code; switch (code) { case Code::Range: result.error = "range"; break; case Code::Absent: result.error = "absent"; break; case Code::Exists: result.error = "exists"; break; case Code::Busy: result.error = "busy"; break; case Code::Locked: result.error = "locked"; break; case Code::Page: result.error = "page"; break; default: break; } return result; } Result FlashBank::success(std::vector fields) { Result result; result.fields = std::move(fields); return result; } void FlashBank::append_status(std::vector& fields, const Chip& chip) { fields.push_back(chip.exists ? 1 : 0); fields.push_back(chip.wel ? 1 : 0); fields.push_back(chip.busy); fields.push_back(chip.kind); fields.push_back(chip.address); fields.push_back(chip.count); fields.push_back(chip.value); fields.push_back(chip.accepted); fields.push_back(chip.completed); } Result FlashBank::reset() { chips_ = {}; return success(); } Result FlashBank::attach(Number id, Number fill) { if (!valid_id(id) || fill < 0 || fill > 255) { return failure(Code::Range); } Chip& chip = chips_[static_cast(id)]; if (chip.exists) { return failure(Code::Exists); } Chip fresh; fresh.exists = true; fresh.image.fill(static_cast(fill)); chip = fresh; return success({id, fill}); } Result FlashBank::detach(Number id) { if (!valid_id(id)) { return failure(Code::Range); } Chip& chip = chips_[static_cast(id)]; if (!chip.exists) { return failure(Code::Absent); } if (chip.busy != 0) { return failure(Code::Busy); } chip = Chip{}; return success({id}); } Result FlashBank::wren(Number id) { if (!valid_id(id)) { return failure(Code::Range); } Chip& chip = chips_[static_cast(id)]; if (!chip.exists) { return failure(Code::Absent); } if (chip.busy != 0) { return failure(Code::Busy); } chip.wel = true; return success({id}); } Result FlashBank::wrdi(Number id) { if (!valid_id(id)) { return failure(Code::Range); } Chip& chip = chips_[static_cast(id)]; if (!chip.exists) { return failure(Code::Absent); } if (chip.busy != 0) { return failure(Code::Busy); } chip.wel = false; return success({id}); } Result FlashBank::program(Number id, Number address, Number count, Number value) { if (!valid_id(id) || address < 0 || address > 127 || count < 1 || count > 16 || value < 0 || value > 255) { return failure(Code::Range); } Chip& chip = chips_[static_cast(id)]; if (!chip.exists) { return failure(Code::Absent); } if (chip.busy != 0) { return failure(Code::Busy); } if (!chip.wel) { return failure(Code::Locked); } if (address / 16 != (address + count - 1) / 16) { return failure(Code::Page); } chip.wel = false; chip.busy = 2; chip.kind = 1; chip.address = address; chip.count = count; chip.value = value; ++chip.accepted; return success({id, address, count, value}); } Result FlashBank::erase(Number id, Number sector) { if (!valid_id(id) || sector < 0 || sector > 3) { return failure(Code::Range); } Chip& chip = chips_[static_cast(id)]; if (!chip.exists) { return failure(Code::Absent); } if (chip.busy != 0) { return failure(Code::Busy); } if (!chip.wel) { return failure(Code::Locked); } chip.wel = false; chip.busy = 3; chip.kind = 2; chip.address = sector * 32; chip.count = 32; chip.value = 255; ++chip.accepted; return success({id, sector}); } void FlashBank::complete(Chip& chip) { const auto begin = static_cast(chip.address); const auto end = static_cast(chip.address + chip.count); for (auto offset = begin; offset < end; ++offset) { if (chip.kind == 1) { chip.image[offset] &= static_cast(chip.value); } else { chip.image[offset] = 255; } } chip.kind = 0; chip.address = 0; chip.count = 0; chip.value = 0; ++chip.completed; } Result FlashBank::advance(Number id, Number ticks) { if (!valid_id(id) || ticks < 0 || ticks > 8) { return failure(Code::Range); } Chip& chip = chips_[static_cast(id)]; if (!chip.exists) { return failure(Code::Absent); } const Number pending = chip.busy; if (pending != 0) { chip.busy = std::max(0, pending - ticks); if (chip.busy == 0) { complete(chip); } } return success({id, ticks, chip.busy}); } Result FlashBank::read(Number id, Number address, Number count) const { if (!valid_id(id) || address < 0 || address > 128 || count < 0 || count > 128 || address + count > 128) { return failure(Code::Range); } const Chip& chip = chips_[static_cast(id)]; if (!chip.exists) { return failure(Code::Absent); } if (chip.busy != 0) { return failure(Code::Busy); } Result result = success({id, address, count}); const auto first = chip.image.begin(); result.data.assign(first, first + count); return result; } Result FlashBank::word(Number id, Number address, Number width) const { if (!valid_id(id) || address < 0 || address > 127 || width < 1 || width > 4 || address + width > 128) { return failure(Code::Range); } const Chip& chip = chips_[static_cast(id)]; if (!chip.exists) { return failure(Code::Absent); } if (chip.busy != 0) { return failure(Code::Busy); } Number value = 0; for (Number index = 0; index < width; ++index) { const auto byte = chip.image[static_cast(address + index)]; value |= static_cast(byte) << (index * 8); } return success({id, address, width, value}); } Result FlashBank::status(Number id) const { if (!valid_id(id)) { return failure(Code::Range); } const Chip& chip = chips_[static_cast(id)]; if (!chip.exists) { return failure(Code::Absent); } Result result = success({id}); append_status(result.fields, chip); return result; } Result FlashBank::inspect() const { Result result; for (const Chip& chip : chips_) { append_status(result.fields, chip); result.data.insert(result.data.end(), chip.image.begin(), chip.image.end()); } return result; } }