125 lines
3.0 KiB
C
125 lines
3.0 KiB
C
#include "domain.h"
|
|
#include <string.h>
|
|
|
|
static int shape(const Command *command, size_t count)
|
|
{
|
|
return wire_arguments(command, count, count);
|
|
}
|
|
|
|
static int range(int64_t value, int low, int high)
|
|
{
|
|
return value >= low && value <= high;
|
|
}
|
|
|
|
void domain_init(Domain *domain)
|
|
{
|
|
memset(domain, 0, sizeof(*domain));
|
|
}
|
|
|
|
static void adapter_write(ControlLane *lane, uint32_t command)
|
|
{
|
|
lane->pending_value = command;
|
|
lane->pending = 1;
|
|
lane->writes++;
|
|
}
|
|
|
|
static void adapter_complete(ControlLane *lane)
|
|
{
|
|
lane->drains++;
|
|
if (lane->pending) {
|
|
lane->committed = lane->pending_value;
|
|
lane->pending = 0;
|
|
lane->jobs++;
|
|
}
|
|
}
|
|
|
|
static uint32_t adapter_read(ControlLane *lane)
|
|
{
|
|
lane->reads++;
|
|
return lane->committed | ((uint32_t)lane->status << 16);
|
|
}
|
|
|
|
static int deliver(ControlLane *lane, uint32_t command)
|
|
{
|
|
uint32_t observed;
|
|
adapter_write(lane, command);
|
|
adapter_complete(lane);
|
|
observed = adapter_read(lane);
|
|
return (observed & UINT32_C(65535)) == command ? 0 : -1;
|
|
}
|
|
|
|
static void submit(Domain *domain, int index, uint32_t command, Observation *out)
|
|
{
|
|
int result = deliver(&domain->lane[index], command);
|
|
if (result == 0) {
|
|
domain->accepted++;
|
|
} else {
|
|
domain->failed++;
|
|
}
|
|
observe_event(out, "SUBMIT", index, command, result);
|
|
}
|
|
|
|
static void set_status(Domain *domain, int index, unsigned status)
|
|
{
|
|
domain->lane[index].status = status;
|
|
}
|
|
|
|
static void inspect(Domain *domain, int index, Observation *out)
|
|
{
|
|
uint32_t value = adapter_read(&domain->lane[index]);
|
|
observe_event(out, "READ", index, value, 0);
|
|
}
|
|
|
|
int domain_command(Domain *domain, const Command *command, Observation *out)
|
|
{
|
|
int64_t a = wire_number(command, 0);
|
|
int64_t b = wire_number(command, 1);
|
|
if (!range(a, 0, CONTROL_LANES - 1)) {
|
|
return 0;
|
|
}
|
|
switch (command->name) {
|
|
case 'C':
|
|
if (!shape(command, 2) || !range(b, 0, 65535)) {
|
|
return 0;
|
|
}
|
|
submit(domain, (int)a, (uint32_t)b, out);
|
|
return 1;
|
|
case 'H':
|
|
if (!shape(command, 2) || !range(b, 0, 255)) {
|
|
return 0;
|
|
}
|
|
set_status(domain, (int)a, (unsigned)b);
|
|
return 1;
|
|
case 'I':
|
|
if (!shape(command, 1)) {
|
|
return 0;
|
|
}
|
|
inspect(domain, (int)a, out);
|
|
return 1;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
static void lane_snapshot(const ControlLane *lane, Observation *out)
|
|
{
|
|
observe_value(out, lane->committed);
|
|
observe_value(out, lane->pending_value);
|
|
observe_value(out, lane->pending);
|
|
observe_value(out, lane->status);
|
|
observe_value(out, lane->writes);
|
|
observe_value(out, lane->drains);
|
|
observe_value(out, lane->reads);
|
|
observe_value(out, lane->jobs);
|
|
}
|
|
|
|
void domain_snapshot(const Domain *domain, Observation *out)
|
|
{
|
|
int index;
|
|
observe_value(out, domain->accepted);
|
|
observe_value(out, domain->failed);
|
|
for (index = 0; index < CONTROL_LANES; index++) {
|
|
lane_snapshot(&domain->lane[index], out);
|
|
}
|
|
}
|