Complete batch 20261016-C-embedded-2: 45 verified GLM/Pi samples, independent regrades and archives; preserve difficulty misses and Salty blockage.
108 lines
2.6 KiB
C
108 lines
2.6 KiB
C
#include "lab.h"
|
|
|
|
static unsigned transfer_amount_valid(const driver_t *driver,
|
|
const completion_t *completion)
|
|
{
|
|
if (completion->amount == 0u) {
|
|
return 0u;
|
|
}
|
|
if (completion->amount > driver->chunk) {
|
|
return 0u;
|
|
}
|
|
return 1u;
|
|
}
|
|
|
|
unsigned completion_valid(const driver_t *driver,
|
|
const completion_t *completion)
|
|
{
|
|
if (completion->error != 0u) {
|
|
return 0u;
|
|
}
|
|
return transfer_amount_valid(driver, completion);
|
|
}
|
|
|
|
static void count_stale(driver_t *driver)
|
|
{
|
|
driver->counters.stale += 1u;
|
|
}
|
|
|
|
static void fail_completion(driver_t *driver)
|
|
{
|
|
reg_abort(driver->registers, driver->token);
|
|
token_advance(driver);
|
|
driver_finish(driver, 1u);
|
|
}
|
|
|
|
static void record_cache(driver_t *driver, unsigned amount)
|
|
{
|
|
reg_cache(driver->registers, driver->offset, amount);
|
|
}
|
|
|
|
static unsigned transfer_finished(const driver_t *driver)
|
|
{
|
|
return descriptor_remaining(driver) == 0u;
|
|
}
|
|
|
|
static void continue_or_finish(driver_t *driver)
|
|
{
|
|
if (transfer_finished(driver)) {
|
|
driver_finish(driver, 0u);
|
|
} else {
|
|
descriptor_program(driver);
|
|
}
|
|
}
|
|
|
|
static void successful_completion(driver_t *driver,
|
|
const completion_t *completion)
|
|
{
|
|
record_cache(driver, completion->amount);
|
|
descriptor_advance(driver, completion->amount);
|
|
continue_or_finish(driver);
|
|
}
|
|
|
|
completion_class completion_classify(const driver_t *driver,
|
|
const completion_t *completion)
|
|
{
|
|
if (!driver->active) {
|
|
return COMPLETION_IDLE;
|
|
}
|
|
if (!descriptor_matches(driver, completion->token)) {
|
|
return COMPLETION_STALE;
|
|
}
|
|
if (!completion_valid(driver, completion)) {
|
|
return COMPLETION_INVALID;
|
|
}
|
|
return COMPLETION_READY;
|
|
}
|
|
|
|
void completion_accept(driver_t *driver, const completion_t *completion)
|
|
{
|
|
completion_class classification = completion_classify(driver, completion);
|
|
switch (classification) {
|
|
case COMPLETION_IDLE:
|
|
count_stale(driver);
|
|
break;
|
|
case COMPLETION_STALE:
|
|
count_stale(driver);
|
|
break;
|
|
case COMPLETION_INVALID:
|
|
fail_completion(driver);
|
|
break;
|
|
case COMPLETION_READY:
|
|
successful_completion(driver, completion);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void driver_complete(driver_t *driver,
|
|
unsigned token,
|
|
unsigned amount,
|
|
unsigned error)
|
|
{
|
|
completion_t completion;
|
|
completion.token = token;
|
|
completion.amount = amount;
|
|
completion.error = error;
|
|
completion_accept(driver, &completion);
|
|
}
|