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

217 lines
5.6 KiB
C

#include "pin.h"
#include <stdlib.h>
#include <string.h>
struct simulated_pin {
struct pin_request plan;
struct pin_observation seen;
unsigned configured;
unsigned export_left;
unsigned ready_left;
unsigned direction_left;
unsigned value_left;
};
struct pin_bus {
struct simulated_pin lines[PIN_CAPACITY];
};
struct pin_bus *pin_bus_create(void)
{
return calloc(1, sizeof(struct pin_bus));
}
void pin_bus_destroy(struct pin_bus *bus)
{
free(bus);
}
static struct simulated_pin *find_pin(struct pin_bus *bus, unsigned pin)
{
if (bus == NULL || pin >= PIN_CAPACITY) {
return NULL;
}
if (!bus->lines[pin].configured) {
return NULL;
}
return &bus->lines[pin];
}
static int valid_request(const struct pin_request *request)
{
if (request->pin >= PIN_CAPACITY || request->value > 1) {
return 0;
}
if (request->ready_delay > 8 || request->export_busy > 8) {
return 0;
}
if (request->direction_busy > 8 || request->value_busy > 8) {
return 0;
}
return request->fatal_stage <= PIN_VALUE;
}
int pin_bus_configure(struct pin_bus *bus,
const struct pin_request *requests, size_t count)
{
unsigned occupied[PIN_CAPACITY] = {0};
size_t index;
if (bus == NULL || count > PIN_MAX_BATCH) {
return -1;
}
if (count != 0 && requests == NULL) {
return -1;
}
for (index = 0; index < count; ++index) {
unsigned pin = requests[index].pin;
if (!valid_request(&requests[index])) {
return -1;
}
if (occupied[pin]) {
return -1;
}
occupied[pin] = 1;
}
memset(bus, 0, sizeof(*bus));
for (index = 0; index < count; ++index) {
const struct pin_request *request = &requests[index];
struct simulated_pin *line = &bus->lines[request->pin];
line->plan = *request;
line->configured = 1;
line->export_left = request->export_busy;
line->ready_left = request->ready_delay;
line->direction_left = request->direction_busy;
line->value_left = request->value_busy;
}
return 0;
}
static int transient(unsigned *remaining)
{
if (*remaining == 0) {
return 0;
}
--*remaining;
return PIN_AGAIN;
}
static int illegal(struct simulated_pin *line)
{
++line->seen.illegal;
return PIN_PROTOCOL;
}
int pin_export(struct pin_bus *bus, unsigned pin)
{
struct simulated_pin *line = find_pin(bus, pin);
int rc;
if (line == NULL) {
return PIN_PROTOCOL;
}
++line->seen.exports;
if (line->seen.exported) {
return illegal(line);
}
if (line->plan.fatal_stage == PIN_EXPORT) {
return PIN_IO;
}
rc = transient(&line->export_left);
if (rc != 0) {
return rc;
}
line->seen.exported = 1;
return 0;
}
int pin_wait_ready(struct pin_bus *bus, unsigned pin)
{
struct simulated_pin *line = find_pin(bus, pin);
int rc;
if (line == NULL) {
return PIN_PROTOCOL;
}
++line->seen.waits;
if (!line->seen.exported || line->seen.ready) {
return illegal(line);
}
if (line->plan.fatal_stage == PIN_READY) {
return PIN_IO;
}
rc = transient(&line->ready_left);
if (rc != 0) {
return rc;
}
line->seen.ready = 1;
return 0;
}
int pin_set_output(struct pin_bus *bus, unsigned pin)
{
struct simulated_pin *line = find_pin(bus, pin);
int rc;
if (line == NULL) {
return PIN_PROTOCOL;
}
++line->seen.directions;
if (!line->seen.exported || !line->seen.ready || line->seen.output) {
return illegal(line);
}
if (line->plan.fatal_stage == PIN_DIRECTION) {
return PIN_IO;
}
rc = transient(&line->direction_left);
if (rc != 0) {
return rc;
}
line->seen.output = 1;
return 0;
}
int pin_set_value(struct pin_bus *bus, unsigned pin, unsigned value)
{
struct simulated_pin *line = find_pin(bus, pin);
int rc;
if (line == NULL) {
return PIN_PROTOCOL;
}
++line->seen.values;
if (!line->seen.output || !line->seen.ready || value > 1) {
return illegal(line);
}
if (line->plan.fatal_stage == PIN_VALUE) {
return PIN_IO;
}
rc = transient(&line->value_left);
if (rc != 0) {
return rc;
}
line->seen.value = value;
return 0;
}
int pin_release(struct pin_bus *bus, unsigned pin)
{
struct simulated_pin *line = find_pin(bus, pin);
if (line == NULL) {
return PIN_PROTOCOL;
}
++line->seen.releases;
if (!line->seen.exported) {
return illegal(line);
}
line->seen.exported = 0;
line->seen.ready = 0;
line->seen.output = 0;
line->seen.value = 0;
return 0;
}
int pin_observe(const struct pin_bus *bus, unsigned pin,
struct pin_observation *observation)
{
if (bus == NULL || pin >= PIN_CAPACITY || observation == NULL) {
return -1;
}
if (!bus->lines[pin].configured) {
return -1;
}
*observation = bus->lines[pin].seen;
return 0;
}
void pin_print_observation(FILE *stream, unsigned sequence, unsigned pin,
const struct pin_result *result,
const struct pin_observation *observation)
{
fprintf(stream, "P %u %u %d %u", sequence, pin, result->rc, result->stage);
fprintf(stream, " %u %u %u %u", observation->exported,
observation->ready, observation->output, observation->value);
fprintf(stream, " %u %u %u %u %u %u\n", observation->exports,
observation->waits, observation->directions, observation->values,
observation->releases, observation->illegal);
}