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>
358 lines
8.4 KiB
C
358 lines
8.4 KiB
C
#include "output.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <limits.h>
|
|
|
|
struct port_state {
|
|
int direction;
|
|
int value;
|
|
unsigned directions;
|
|
unsigned writes;
|
|
unsigned ignored;
|
|
unsigned replacements;
|
|
};
|
|
struct command {
|
|
char opcode;
|
|
int first;
|
|
int second;
|
|
int count;
|
|
};
|
|
struct token {
|
|
const char *begin;
|
|
const char *end;
|
|
};
|
|
static struct port_state ports[OUTPUT_PINS];
|
|
static uint64_t audit;
|
|
static unsigned calls;
|
|
static unsigned bad_calls;
|
|
|
|
static int pin_exists(int pin)
|
|
{
|
|
return pin >= 0 && pin < OUTPUT_PINS;
|
|
}
|
|
|
|
static void record_call(int operation, int pin, int argument)
|
|
{
|
|
uint64_t word = (uint64_t)(17 * operation + 13 * pin + 7 * argument + 1);
|
|
audit = (audit * 257 + word) % UINT64_C(1000000007);
|
|
calls += 1;
|
|
}
|
|
|
|
void port_direction(int pin)
|
|
{
|
|
if (!pin_exists(pin)) {
|
|
bad_calls += 1;
|
|
return;
|
|
}
|
|
record_call(1, pin, 1);
|
|
ports[pin].directions += 1;
|
|
ports[pin].direction = 1;
|
|
}
|
|
|
|
void port_value(int pin, int value)
|
|
{
|
|
if (!pin_exists(pin) || (value != 0 && value != 1)) {
|
|
bad_calls += 1;
|
|
return;
|
|
}
|
|
record_call(2, pin, value);
|
|
ports[pin].writes += 1;
|
|
if (ports[pin].direction == 0) {
|
|
ports[pin].ignored += 1;
|
|
return;
|
|
}
|
|
ports[pin].value = value;
|
|
}
|
|
|
|
void port_rebind(int pin, int value)
|
|
{
|
|
if (!pin_exists(pin) || (value != 0 && value != 1)) {
|
|
bad_calls += 1;
|
|
return;
|
|
}
|
|
record_call(3, pin, value);
|
|
ports[pin].replacements += 1;
|
|
ports[pin].direction = 0;
|
|
ports[pin].value = value;
|
|
}
|
|
|
|
static void initialize_ports(void)
|
|
{
|
|
int pin;
|
|
for (pin = 0; pin < OUTPUT_PINS; ++pin) {
|
|
ports[pin].direction = 0;
|
|
ports[pin].value = 0;
|
|
ports[pin].directions = 0;
|
|
ports[pin].writes = 0;
|
|
ports[pin].ignored = 0;
|
|
ports[pin].replacements = 0;
|
|
}
|
|
audit = 0;
|
|
calls = 0;
|
|
bad_calls = 0;
|
|
}
|
|
|
|
static int horizontal(int character)
|
|
{
|
|
return character == ' ' || character == '\t' || character == '\r';
|
|
}
|
|
|
|
static int next_token(const char **cursor, struct token *token)
|
|
{
|
|
const char *position = *cursor;
|
|
while (horizontal((unsigned char)*position)) {
|
|
position += 1;
|
|
}
|
|
if (*position == '\0' || *position == '\n') {
|
|
*cursor = position;
|
|
return 0;
|
|
}
|
|
token->begin = position;
|
|
while (*position != '\0' && *position != '\n' &&
|
|
!horizontal((unsigned char)*position)) {
|
|
position += 1;
|
|
}
|
|
token->end = position;
|
|
*cursor = position;
|
|
return 1;
|
|
}
|
|
|
|
static int decimal_token(struct token token, int *result)
|
|
{
|
|
const char *position = token.begin;
|
|
int negative = 0;
|
|
unsigned magnitude = 0;
|
|
unsigned limit;
|
|
if (position < token.end && (*position == '+' || *position == '-')) {
|
|
negative = *position == '-';
|
|
position += 1;
|
|
}
|
|
if (position == token.end) {
|
|
return 0;
|
|
}
|
|
limit = negative ? (unsigned)INT_MAX + 1U : (unsigned)INT_MAX;
|
|
while (position < token.end) {
|
|
unsigned digit;
|
|
if (*position < '0' || *position > '9') {
|
|
return 0;
|
|
}
|
|
digit = (unsigned)(*position - '0');
|
|
if (magnitude > (limit - digit) / 10U) {
|
|
return 0;
|
|
}
|
|
magnitude = magnitude * 10U + digit;
|
|
position += 1;
|
|
}
|
|
if (negative) {
|
|
*result = magnitude == (unsigned)INT_MAX + 1U ? INT_MIN : -(int)magnitude;
|
|
} else {
|
|
*result = (int)magnitude;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static int argument_count(char opcode)
|
|
{
|
|
switch (opcode) {
|
|
case 'S':
|
|
case 'M':
|
|
case 'R':
|
|
return 2;
|
|
case 'A':
|
|
case 'C':
|
|
case 'Q':
|
|
return 1;
|
|
default:
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
static int decode_line(const char *line, struct command *command)
|
|
{
|
|
const char *cursor = line;
|
|
struct token token;
|
|
int count;
|
|
int values[2] = {0, 0};
|
|
int index;
|
|
if (!next_token(&cursor, &token)) {
|
|
return 0;
|
|
}
|
|
if (token.end - token.begin != 1) {
|
|
return -1;
|
|
}
|
|
command->opcode = *token.begin;
|
|
count = argument_count(command->opcode);
|
|
if (count < 0) {
|
|
return -1;
|
|
}
|
|
for (index = 0; index < count; ++index) {
|
|
if (!next_token(&cursor, &token) || !decimal_token(token, &values[index])) {
|
|
return -1;
|
|
}
|
|
}
|
|
if (next_token(&cursor, &token)) {
|
|
return -1;
|
|
}
|
|
command->first = values[0];
|
|
command->second = values[1];
|
|
command->count = count;
|
|
return 1;
|
|
}
|
|
|
|
static int dispatch(struct output_control *control, const struct command *command)
|
|
{
|
|
struct output_view view;
|
|
switch (command->opcode) {
|
|
case 'S':
|
|
return output_stage(control, command->first, command->second);
|
|
case 'M':
|
|
return output_stage_mask(control, command->first, command->second);
|
|
case 'A':
|
|
return output_apply(control, command->first);
|
|
case 'C':
|
|
return output_cancel(control, command->first);
|
|
case 'R':
|
|
return output_rebind(control, command->first, command->second);
|
|
case 'Q':
|
|
return output_query(control, command->first, &view);
|
|
default:
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
static void print_control(const struct output_control *control)
|
|
{
|
|
printf(" %u %u %u", control->staged, control->committed, control->canceled);
|
|
}
|
|
|
|
static void print_port(int pin)
|
|
{
|
|
const struct port_state *port = &ports[pin];
|
|
printf(" %d %d %u %u %u %u", port->direction, port->value,
|
|
port->directions, port->writes, port->ignored, port->replacements);
|
|
}
|
|
|
|
static void print_view(const struct output_control *control, int pin)
|
|
{
|
|
struct output_view view = {-9, -9, -9};
|
|
int result = output_query(control, pin, &view);
|
|
printf(" %d %d %d %d", result, view.desired, view.pending, view.ready);
|
|
}
|
|
|
|
static void print_observation(unsigned sequence, int result,
|
|
const struct output_control *control)
|
|
{
|
|
int pin;
|
|
printf("%u %d %u %u %llu", sequence, result, calls, bad_calls,
|
|
(unsigned long long)audit);
|
|
print_control(control);
|
|
for (pin = 0; pin < OUTPUT_PINS; ++pin) {
|
|
print_view(control, pin);
|
|
print_port(pin);
|
|
}
|
|
putchar('\n');
|
|
}
|
|
|
|
static int append_character(char **line, size_t *capacity, size_t *length, int character)
|
|
{
|
|
char *replacement;
|
|
size_t next;
|
|
if (*length + 1 >= *capacity) {
|
|
next = *capacity == 0 ? 128 : *capacity * 2;
|
|
if (next < *capacity || next > (size_t)INT_MAX) {
|
|
return 0;
|
|
}
|
|
replacement = realloc(*line, next);
|
|
if (replacement == NULL) {
|
|
return 0;
|
|
}
|
|
*line = replacement;
|
|
*capacity = next;
|
|
}
|
|
(*line)[*length] = (char)character;
|
|
*length += 1;
|
|
return 1;
|
|
}
|
|
|
|
static int read_line(FILE *stream, char **line, size_t *capacity)
|
|
{
|
|
int character;
|
|
size_t length = 0;
|
|
while ((character = fgetc(stream)) != EOF) {
|
|
if (character == 0 || !append_character(line, capacity, &length, character)) {
|
|
return -1;
|
|
}
|
|
if (character == '\n') {
|
|
break;
|
|
}
|
|
}
|
|
if (ferror(stream)) {
|
|
return -1;
|
|
}
|
|
if (length == 0 && character == EOF) {
|
|
return 0;
|
|
}
|
|
if (!append_character(line, capacity, &length, 0)) {
|
|
return -1;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static int run_stream(FILE *stream)
|
|
{
|
|
struct output_control control;
|
|
char *line = NULL;
|
|
size_t capacity = 0;
|
|
unsigned sequence = 0;
|
|
int status = 0;
|
|
initialize_ports();
|
|
output_init(&control);
|
|
for (;;) {
|
|
struct command command;
|
|
int read_result = read_line(stream, &line, &capacity);
|
|
int decoded;
|
|
int result;
|
|
if (read_result == 0) {
|
|
break;
|
|
}
|
|
if (read_result < 0) {
|
|
status = 2;
|
|
break;
|
|
}
|
|
decoded = decode_line(line, &command);
|
|
if (decoded == 0) {
|
|
continue;
|
|
}
|
|
result = decoded < 0 ? -2 : dispatch(&control, &command);
|
|
sequence += 1;
|
|
print_observation(sequence, result, &control);
|
|
}
|
|
free(line);
|
|
return status;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
FILE *stream = stdin;
|
|
int result;
|
|
if (argc > 2) {
|
|
return 2;
|
|
}
|
|
if (argc == 2) {
|
|
stream = fopen(argv[1], "rb");
|
|
if (stream == NULL) {
|
|
return 2;
|
|
}
|
|
}
|
|
result = run_stream(stream);
|
|
if (stream != stdin && fclose(stream) != 0) {
|
|
result = 2;
|
|
}
|
|
if (ferror(stdout)) {
|
|
result = 2;
|
|
}
|
|
return result;
|
|
}
|