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

207 lines
5.7 KiB
C

#include "tx.h"
#include <string.h>
static int id_valid(int id)
{
return id >= 1 && id <= 65535;
}
static int find_slot(const struct tx_engine *e, int id)
{
for (int i = 0; i < TX_SLOTS; ++i) {
if (e->slots[i].state && e->slots[i].id == id)
return i;
}
return -1;
}
static int free_slot(const struct tx_engine *e)
{
for (int i = 0; i < TX_SLOTS; ++i) {
if (!e->slots[i].state)
return i;
}
return -1;
}
static size_t count_covered(const struct tx_slot *s)
{
size_t count = 0;
for (size_t i = 0; i < s->length; ++i) {
if (s->covered[i])
++count;
}
return count;
}
static int lookup_draft(struct tx_engine *e, int id, int *slot)
{
if (!id_valid(id))
return TX_INVALID;
*slot = find_slot(e, id);
if (*slot < 0)
return TX_ABSENT;
if (e->slots[*slot].state != TX_DRAFT)
return TX_BUSY;
return TX_OK;
}
static int write_range(const struct tx_slot *s, int offset, int length)
{
if (offset < 0 || offset >= TX_CAPACITY || length < 1 ||
length > TX_CAPACITY || length > TX_CAPACITY - offset)
return TX_INVALID;
if ((size_t)offset + (size_t)length > s->length)
return TX_INVALID;
return TX_OK;
}
static void cover(struct tx_slot *s, int offset, int length)
{
for (int i = offset; i < offset + length; ++i)
s->covered[i] = 1;
}
static int basic_range_valid(int offset, int length)
{
return offset >= 0 && offset < TX_CAPACITY && length >= 1 &&
length <= TX_CAPACITY && length <= TX_CAPACITY - offset;
}
int tx_init(struct tx_engine *e, int line)
{
if (line != 16 && line != 32 && line != 64)
return TX_INVALID;
memset(e, 0, sizeof(*e));
e->next_order = 1;
return port_init(&e->port, line);
}
int tx_begin(struct tx_engine *e, int id, int length)
{
if (!id_valid(id) || length < 1 || length > TX_CAPACITY)
return TX_INVALID;
if (find_slot(e, id) >= 0)
return TX_BUSY;
int index = free_slot(e);
if (index < 0)
return TX_BUSY;
struct tx_slot *s = &e->slots[index];
memset(s, 0, sizeof(*s));
s->state = TX_DRAFT;
s->id = id;
s->length = (size_t)length;
++e->totals.begins;
return TX_OK;
}
int tx_write(struct tx_engine *e, int id, int offset,
const uint8_t *data, int length)
{
if (!id_valid(id) || !basic_range_valid(offset, length) || !data)
return TX_INVALID;
int index = find_slot(e, id);
if (index < 0)
return TX_ABSENT;
if (write_range(&e->slots[index], offset, length) != TX_OK)
return TX_INVALID;
if (e->slots[index].state != TX_DRAFT)
return TX_BUSY;
int rc = port_write(&e->port, index, (size_t)offset, data, (size_t)length);
if (rc != TX_OK)
return rc;
cover(&e->slots[index], offset, length);
++e->totals.writes;
return TX_OK;
}
int tx_fill(struct tx_engine *e, int id, int offset, int length, int value)
{
if (!id_valid(id) || !basic_range_valid(offset, length) || value < 0 || value > 255)
return TX_INVALID;
int index = find_slot(e, id);
if (index < 0)
return TX_ABSENT;
if (write_range(&e->slots[index], offset, length) != TX_OK)
return TX_INVALID;
if (e->slots[index].state != TX_DRAFT)
return TX_BUSY;
int rc = port_fill(&e->port, index, (size_t)offset, (size_t)length, (uint8_t)value);
if (rc != TX_OK)
return rc;
cover(&e->slots[index], offset, length);
++e->totals.writes;
return TX_OK;
}
int tx_send(struct tx_engine *e, int id)
{
int index;
int rc = lookup_draft(e, id, &index);
if (rc != TX_OK)
return rc;
struct tx_slot *s = &e->slots[index];
if (count_covered(s) != s->length)
return TX_INCOMPLETE;
port_fill(&e->port, index, 0, s->length, 0xa5);
rc = port_clean(&e->port, index, 0, s->length);
if (rc != TX_OK)
return rc;
rc = port_queue(&e->port, index, id, s->length);
if (rc != TX_OK)
return rc;
s->state = TX_PENDING;
s->order = e->next_order++;
++e->totals.submits;
return TX_OK;
}
int tx_complete(struct tx_engine *e, int id, int status,
struct tx_completion *out)
{
out->id = 0;
out->length = 0;
if (!id_valid(id) || (status != 0 && status != 1))
return TX_INVALID;
int index = find_slot(e, id);
if (index < 0)
return TX_ABSENT;
if (e->slots[index].state != TX_PENDING)
return TX_BUSY;
int rc = port_complete(&e->port, id, status, out);
if (rc != TX_OK)
return rc;
if (status == 0) {
++e->totals.completed;
e->totals.bytes += out->length;
} else {
++e->totals.failures;
}
int released = index;
memset(&e->slots[released], 0, sizeof(e->slots[released]));
return TX_OK;
}
int tx_cancel(struct tx_engine *e, int id)
{
int index;
int rc = lookup_draft(e, id, &index);
if (rc != TX_OK)
return rc;
memset(&e->slots[index], 0, sizeof(e->slots[index]));
++e->totals.cancelled;
return TX_OK;
}
int tx_inspect(const struct tx_engine *e, int id, struct tx_frame *out)
{
memset(out, 0, sizeof(*out));
if (!id_valid(id))
return TX_INVALID;
int index = find_slot(e, id);
if (index < 0)
return TX_ABSENT;
const struct tx_slot *s = &e->slots[index];
out->state = s->state;
out->slot = index;
out->length = s->length;
out->covered = count_covered(s);
return TX_OK;
}
void tx_statistics(const struct tx_engine *e, struct tx_stats *out)
{
*out = e->totals;
out->drafts = 0;
out->pending = 0;
for (int i = 0; i < TX_SLOTS; ++i) {
if (e->slots[i].state == TX_DRAFT)
++out->drafts;
if (e->slots[i].state == TX_PENDING)
++out->pending;
}
}