#include "lab.h" void backend_init(struct backend *backend) { size_t bank; size_t index; memset(backend, 0, sizeof(*backend)); for (bank = 0; bank < BANKS; bank++) { for (index = 0; index < 256; index++) { backend->storage[bank][index] = (unsigned char)((index * 17 + bank * 29) & 255); } } for (size_t bank = 0; bank < BANKS; bank++) { backend->value[bank][0] = -1; } } void backend_control(struct backend *backend, const struct command *command, struct reply *reply) { int bank = backend->selected; if (command->count != 3 || command->argument[0] < 0 || command->argument[0] >= 8) { reply->result = -EINVAL; return; } backend->value[bank][command->argument[0]] = command->argument[1]; if (command->argument[2] >= 0) { size_t index; for (index = 0; index < 256; index++) { backend->storage[bank][index] = (unsigned char) ((index * 17 + command->argument[2]) & 255); } } } int device_call(struct backend *backend, int operation, int a, int b, unsigned char *bytes, size_t length) { int *state = backend->value[backend->selected]; int result = fault_take(backend, operation - 1); (void)b; (void)length; if (!result && operation == 1) { state[0] = a; state[1]++; } else if (!result && operation == 2) { unsigned value = (unsigned)state[0]; size_t index; for (index = 0; index < 4; index++) { bytes[index] = (unsigned char)(value >> (8 * index)); } state[2]++; } trace_add(&backend->trace, "ATTR", operation, a, result); return result; }