amos 9d633e9e67 Batch 15 (20261023-C-hwe-scale-15): 320 HWE-derived repair tasks
One user-space repair task per eligible HWE debug-diag / compile-deploy base
ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content
filter. Each ships a public spec with a check-to-basis table, at least two
defects with single-fix fixtures, a symptom-masking fixture and an alternative
correct implementation. Claude re-checked all 320: structure, the HWE quality
items and trace6 Docker calibration receipts against current digests
(320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120
ad-hoc family names; the next batch maps them to the canonical list.
The batch's 1,256-line progress log stays in its factory report and the
36-local branch batch15-hwe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 01:55:56 +08:00

191 lines
6.0 KiB
C++

#include "stream.hpp"
#include <algorithm>
namespace stream {
std::uint8_t pattern(std::uint32_t seed, std::size_t index) {
const std::uint32_t i = static_cast<std::uint32_t>(index);
return static_cast<std::uint8_t>(((seed ^ (i * 29u)) + i * i + 17u) & 255u);
}
std::uint32_t digest(const std::vector<std::uint8_t>& bytes) {
std::uint32_t value = 2166136261u;
for (std::uint8_t byte : bytes) {
value ^= byte;
value *= 16777619u;
}
return value;
}
Port::Port() = default;
Port::~Port() = default;
bool Port::fault() {
++errors_;
return false;
}
bool Port::range(const Region& region, std::size_t offset, std::size_t count) const {
return offset <= region.length && count <= region.length - offset;
}
bool Port::cpu_region(std::size_t slot) {
if (slot >= slot_count) {
return fault();
}
if (!regions_[slot].live || regions_[slot].owner != Owner::Cpu) {
return fault();
}
return true;
}
bool Port::map(std::size_t slot, std::size_t length, Direction direction,
std::uint32_t seed, std::uint64_t& generation) {
const unsigned d = static_cast<unsigned>(direction);
if (slot >= slot_count || length == 0 || length > capacity || d < 1 || d > 3) {
return fault();
}
Region& region = regions_[slot];
if (region.live) {
return fault();
}
region.live = true;
region.armed = false;
region.owner = Owner::Cpu;
region.direction = direction;
region.length = length;
++region.generation;
++region.counters.maps;
for (std::size_t i = 0; i < length; ++i) {
region.cpu[i] = pattern(seed, i);
region.device[i] = pattern(seed ^ 0xA5u, i);
}
generation = region.generation;
return true;
}
bool Port::patch(std::size_t slot, std::size_t offset, std::size_t count,
std::uint32_t seed) {
if (!cpu_region(slot)) {
return false;
}
Region& region = regions_[slot];
if (!range(region, offset, count)) {
return fault();
}
for (std::size_t i = offset; i < offset + count; ++i) {
region.cpu[i] = pattern(seed, i);
}
return true;
}
bool Port::read(std::size_t slot, std::size_t offset, std::size_t count,
std::vector<std::uint8_t>& result) {
if (!cpu_region(slot)) {
return false;
}
const Region& region = regions_[slot];
if (!range(region, offset, count)) {
return fault();
}
result.assign(region.cpu.begin() + offset, region.cpu.begin() + offset + count);
return true;
}
bool Port::for_device(std::size_t slot, Direction direction) {
if (!cpu_region(slot)) {
return false;
}
Region& region = regions_[slot];
if (region.direction != direction) {
return fault();
}
if (direction == Direction::ToDevice || direction == Direction::Both) {
std::copy_n(region.cpu.begin(), region.length, region.device.begin());
region.counters.to_bytes += region.length;
}
region.owner = Owner::Device;
region.armed = true;
++region.counters.prepares;
return true;
}
bool Port::transfer(std::size_t slot, std::size_t offset, std::size_t count,
std::uint32_t seed, Receipt& result) {
if (slot >= slot_count) {
return fault();
}
Region& region = regions_[slot];
if (!region.live || region.owner != Owner::Device || !region.armed ||
count == 0 || !range(region, offset, count)) {
return fault();
}
Receipt receipt;
receipt.ticket = next_ticket_++;
receipt.generation = region.generation;
receipt.offset = offset;
receipt.count = count;
std::vector<std::uint8_t> segment(region.device.begin() + offset,
region.device.begin() + offset + count);
if (region.direction != Direction::FromDevice) {
receipt.read_hash = digest(segment);
}
if (region.direction == Direction::FromDevice) {
for (std::size_t i = offset; i < offset + count; ++i) {
region.device[i] = pattern(seed, i);
}
} else if (region.direction == Direction::Both) {
for (std::size_t i = offset; i < offset + count; ++i) {
region.device[i] ^= pattern(seed, i);
}
}
if (region.direction != Direction::ToDevice) {
segment.assign(region.device.begin() + offset, region.device.begin() + offset + count);
receipt.write_hash = digest(segment);
}
region.armed = false;
++region.counters.transfers;
result = receipt;
return true;
}
bool Port::for_cpu(std::size_t slot, Direction direction) {
if (slot >= slot_count) {
return fault();
}
Region& region = regions_[slot];
if (!region.live || region.owner != Owner::Device || region.armed ||
region.direction != direction) {
return fault();
}
if (direction == Direction::FromDevice || direction == Direction::Both) {
std::copy_n(region.device.begin(), region.length, region.cpu.begin());
region.counters.from_bytes += region.length;
}
region.owner = Owner::Cpu;
++region.counters.reclaims;
return true;
}
bool Port::unmap(std::size_t slot) {
if (!cpu_region(slot)) {
return false;
}
Region& region = regions_[slot];
region.live = false;
region.armed = false;
region.owner = Owner::None;
region.length = 0;
++region.counters.unmaps;
return true;
}
Observation Port::observe(std::size_t slot) const {
Observation result;
if (slot >= slot_count) {
return result;
}
const Region& region = regions_[slot];
result.live = region.live;
result.owner = region.owner;
result.direction = region.direction;
result.length = region.length;
result.generation = region.generation;
result.counters = region.counters;
if (region.live) {
result.cpu.assign(region.cpu.begin(), region.cpu.begin() + region.length);
result.device.assign(region.device.begin(), region.device.begin() + region.length);
}
return result;
}
std::uint64_t Port::errors() const {
return errors_;
}
}