Batch 21 (20261027-A-bos-redo-21) rebuilt 40 of the 53 rejected Bos-qiao environments: hidden assertions that ran inside the candidate's process now run in a trusted parent that reads observations from a low-UID worker; gold bytes are unchanged. 9 were excluded for scope (ROS2, oversize, security core), 3 are blocked because the source requires an exec bit the platform does not freeze, 1 lacks requests/bs4 in trace6. Batch 18 (20261026-C-hwe-codegen-18) adds its last 34 of 134 tasks; the first 100 landed in 2f32624d. Claude reviewed 74/74 USABLE against current digests, canonical families, no ID or source overlap with main, plain public docs. Only the new task directories are taken; the stale pre-batch-20 copies of concur-release-capsule and resource-temp-link on the batch 21 branch and both branches' progress notes stay as main has them. Difficulty is unmeasured. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
252 lines
6.9 KiB
C++
252 lines
6.9 KiB
C++
#include "flash.hpp"
|
|
#include <algorithm>
|
|
#include <utility>
|
|
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<Number> fields) {
|
|
Result result;
|
|
result.fields = std::move(fields);
|
|
return result;
|
|
}
|
|
void FlashBank::append_status(std::vector<Number>& 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<std::size_t>(id)];
|
|
if (chip.exists) {
|
|
return failure(Code::Exists);
|
|
}
|
|
Chip fresh;
|
|
fresh.exists = true;
|
|
fresh.image.fill(static_cast<std::uint8_t>(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<std::size_t>(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<std::size_t>(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<std::size_t>(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<std::size_t>(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.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<std::size_t>(id)];
|
|
if (!chip.exists) {
|
|
return failure(Code::Absent);
|
|
}
|
|
if (chip.busy != 0) {
|
|
return failure(Code::Busy);
|
|
}
|
|
if (!chip.wel) {
|
|
return failure(Code::Locked);
|
|
}
|
|
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<std::size_t>(chip.address);
|
|
const auto end = static_cast<std::size_t>(chip.address + chip.count);
|
|
for (auto offset = begin; offset < end; ++offset) {
|
|
if (chip.kind == 1) {
|
|
chip.image[offset] &= static_cast<std::uint8_t>(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<std::size_t>(id)];
|
|
if (!chip.exists) {
|
|
return failure(Code::Absent);
|
|
}
|
|
const Number pending = chip.busy;
|
|
if (pending != 0) {
|
|
chip.busy = std::max<Number>(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<std::size_t>(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() + address;
|
|
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<std::size_t>(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<std::size_t>(address + index)];
|
|
value |= static_cast<Number>(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<std::size_t>(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;
|
|
}
|
|
}
|