65 lines
2.0 KiB
C

#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] = 100; }
}
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)length;
if (!result && operation == 1) {
unsigned rate = (unsigned)state[0];
size_t index;
for (index = 0; index < 4; index++) {
bytes[index] = (unsigned char)(rate >> (index * 8));
}
unsigned mode = (unsigned)state[1];
for (index = 0; index < 4; index++) {
bytes[index + 4] = (unsigned char)(mode >> (index * 8));
}
state[2]++;
} else if (!result && operation == 2) {
state[0] = a;
state[1] = b;
state[3]++;
} else if (!result && operation == 3) {
state[0] = 100;
state[1] = 0;
state[4]++;
}
trace_add(&backend->trace, "IOCTL", operation, a, result);
return result;
}