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>
213 lines
5.3 KiB
C
213 lines
5.3 KiB
C
#include "phase.h"
|
|
#include <string.h>
|
|
|
|
static void clear_channel(TimerChannel *channel, int id)
|
|
{
|
|
memset(channel, 0, sizeof(*channel));
|
|
channel->id = id;
|
|
channel->mode = TIMER_COUNT;
|
|
}
|
|
|
|
static int channel_exists(const TimerBank *bank, int id)
|
|
{
|
|
if (!timer_id_valid(id)) {
|
|
return TIMER_RANGE;
|
|
}
|
|
if (!bank->channels[id].present) {
|
|
return TIMER_MISSING;
|
|
}
|
|
return TIMER_OK;
|
|
}
|
|
|
|
static int validate_configuration(int id, int64_t period,
|
|
int64_t phase, int64_t epoch, TimerMode mode)
|
|
{
|
|
if (!timer_id_valid(id)) {
|
|
return TIMER_RANGE;
|
|
}
|
|
if (!timer_period_valid(period)) {
|
|
return TIMER_RANGE;
|
|
}
|
|
if (!timer_coord_valid(phase)) {
|
|
return TIMER_RANGE;
|
|
}
|
|
if (!timer_coord_valid(epoch)) {
|
|
return TIMER_RANGE;
|
|
}
|
|
if (!timer_mode_valid(mode)) {
|
|
return TIMER_SYNTAX;
|
|
}
|
|
return TIMER_OK;
|
|
}
|
|
|
|
static void rearm_channel(const TimerBank *bank, TimerChannel *channel)
|
|
{
|
|
if (channel->enabled) {
|
|
channel->next = timer_next(bank->now, channel->epoch,
|
|
channel->phase, channel->period);
|
|
} else {
|
|
channel->next = 0;
|
|
}
|
|
}
|
|
|
|
void timer_bank_reset(TimerBank *bank, int64_t now)
|
|
{
|
|
bank->now = now;
|
|
for (int id = 0; id < TIMER_CHANNELS; ++id) {
|
|
clear_channel(&bank->channels[id], id);
|
|
}
|
|
}
|
|
|
|
int timer_bank_put(TimerBank *bank, int id, int64_t period,
|
|
int64_t phase, int64_t epoch, TimerMode mode)
|
|
{
|
|
int code = validate_configuration(id, period, phase, epoch, mode);
|
|
TimerChannel replacement;
|
|
if (code != TIMER_OK) {
|
|
return code;
|
|
}
|
|
clear_channel(&replacement, id);
|
|
replacement.present = 1;
|
|
replacement.enabled = 1;
|
|
replacement.period = period;
|
|
replacement.phase = timer_residue(phase, period);
|
|
replacement.epoch = epoch;
|
|
replacement.mode = mode;
|
|
rearm_channel(bank, &replacement);
|
|
bank->channels[id] = replacement;
|
|
return TIMER_OK;
|
|
}
|
|
|
|
int timer_bank_peek(const TimerBank *bank, int id, TimerChannel *out)
|
|
{
|
|
int code = channel_exists(bank, id);
|
|
if (code != TIMER_OK) {
|
|
return code;
|
|
}
|
|
*out = bank->channels[id];
|
|
return TIMER_OK;
|
|
}
|
|
|
|
int timer_bank_enable(TimerBank *bank, int id, int enabled)
|
|
{
|
|
int code;
|
|
if (enabled != 0 && enabled != 1) {
|
|
return TIMER_RANGE;
|
|
}
|
|
code = channel_exists(bank, id);
|
|
if (code != TIMER_OK) {
|
|
return code;
|
|
}
|
|
bank->channels[id].enabled = enabled;
|
|
rearm_channel(bank, &bank->channels[id]);
|
|
return TIMER_OK;
|
|
}
|
|
|
|
int timer_bank_shift(TimerBank *bank, int id, int64_t delta)
|
|
{
|
|
int code;
|
|
TimerChannel *channel;
|
|
if (!timer_coord_valid(delta)) {
|
|
return TIMER_RANGE;
|
|
}
|
|
code = channel_exists(bank, id);
|
|
if (code != TIMER_OK) {
|
|
return code;
|
|
}
|
|
channel = &bank->channels[id];
|
|
channel->phase = timer_residue(channel->phase + delta, channel->period);
|
|
rearm_channel(bank, channel);
|
|
return TIMER_OK;
|
|
}
|
|
|
|
int timer_bank_drop(TimerBank *bank, int id)
|
|
{
|
|
int code = channel_exists(bank, id);
|
|
if (code != TIMER_OK) {
|
|
return code;
|
|
}
|
|
clear_channel(&bank->channels[id], id);
|
|
return TIMER_OK;
|
|
}
|
|
|
|
static TimerEvent consume_channel(TimerChannel *channel, int64_t target)
|
|
{
|
|
TimerEvent event;
|
|
int64_t count = timer_due(channel->next, target, channel->period);
|
|
event.id = channel->id;
|
|
event.due = count;
|
|
event.first = channel->next;
|
|
event.last = channel->next + (count - 1) * channel->period;
|
|
event.skipped = 0;
|
|
if (channel->mode == TIMER_LATEST) {
|
|
event.last = event.first;
|
|
event.skipped = count - 1;
|
|
}
|
|
channel->next += count * channel->period;
|
|
channel->fired += count;
|
|
channel->skipped += event.skipped;
|
|
event.next = channel->next;
|
|
return event;
|
|
}
|
|
|
|
int timer_bank_tick(TimerBank *bank, int64_t target,
|
|
TimerEvent events[TIMER_CHANNELS], size_t *count)
|
|
{
|
|
size_t used = 0;
|
|
if (!timer_coord_valid(target)) {
|
|
return TIMER_RANGE;
|
|
}
|
|
if (target < bank->now) {
|
|
return TIMER_BACKWARD;
|
|
}
|
|
for (int id = 0; id < TIMER_CHANNELS; ++id) {
|
|
TimerChannel *channel = &bank->channels[id];
|
|
if (!channel->present) {
|
|
continue;
|
|
}
|
|
if (!channel->enabled) {
|
|
continue;
|
|
}
|
|
if (channel->next > target) {
|
|
continue;
|
|
}
|
|
events[used] = consume_channel(channel, target);
|
|
++used;
|
|
}
|
|
bank->now = target;
|
|
*count = used;
|
|
return TIMER_OK;
|
|
}
|
|
|
|
size_t timer_bank_list(const TimerBank *bank, TimerChannel out[TIMER_CHANNELS])
|
|
{
|
|
size_t used = 0;
|
|
for (int id = 0; id < TIMER_CHANNELS; ++id) {
|
|
if (bank->channels[id].present) {
|
|
out[used] = bank->channels[id];
|
|
++used;
|
|
}
|
|
}
|
|
return used;
|
|
}
|
|
|
|
TimerStats timer_bank_stats(const TimerBank *bank)
|
|
{
|
|
TimerStats stats;
|
|
memset(&stats, 0, sizeof(stats));
|
|
stats.now = bank->now;
|
|
for (int id = 0; id < TIMER_CHANNELS; ++id) {
|
|
const TimerChannel *channel = &bank->channels[id];
|
|
if (!channel->present) {
|
|
continue;
|
|
}
|
|
++stats.configured;
|
|
if (channel->enabled) {
|
|
++stats.enabled;
|
|
}
|
|
stats.fired += channel->fired;
|
|
stats.skipped += channel->skipped;
|
|
}
|
|
return stats;
|
|
}
|