80 lines
2.5 KiB
C

#ifndef LAB_H
#define LAB_H
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#define ARG_LIMIT 80
#define BYTE_LIMIT 64
#define BANKS 4
#define TRACE_LIMIT 64
struct command {
char operation;
int argument[ARG_LIMIT];
size_t count;
};
struct reply {
int result;
unsigned char bytes[BYTE_LIMIT];
size_t length;
};
struct fault {
int error;
unsigned remaining;
};
struct trace_entry {
const char *operation;
int a;
int b;
int c;
};
struct recorder {
struct trace_entry entries[TRACE_LIMIT];
size_t used;
unsigned long total;
int overflow;
};
struct backend {
int selected;
int value[BANKS][8];
unsigned char storage[BANKS][256];
struct fault faults[8];
struct recorder trace;
};
struct client {
struct backend *backend;
int selected;
int value[BANKS][8];
};
int command_read(FILE *stream, struct command *command);
int command_has(const struct command *command, size_t count);
int command_byte(const struct command *command, size_t index);
void reply_reset(struct reply *reply);
void reply_copy(struct reply *reply, const unsigned char *data, size_t length);
void reply_print(const struct reply *reply);
void trace_begin(struct recorder *recorder);
void trace_add(struct recorder *recorder, const char *operation, int a, int b, int c);
void trace_print(const struct recorder *recorder);
void fault_set(struct backend *backend, int index, int error, int count);
int fault_take(struct backend *backend, int index);
unsigned byte_sum(const unsigned char *bytes, size_t length);
int bytes_valid(const struct command *command, size_t start, size_t count);
void bytes_from_command(unsigned char *target, const struct command *command,
size_t start, size_t count);
void backend_init(struct backend *backend);
void backend_control(struct backend *backend, const struct command *command,
struct reply *reply);
void backend_snapshot(const struct backend *backend);
int device_call(struct backend *backend, int operation, int a, int b,
unsigned char *bytes, size_t length);
void client_init(struct client *client, struct backend *backend);
void client_apply(struct client *client, const struct command *command,
struct reply *reply);
void client_snapshot(const struct client *client);
int text_integer(const unsigned char *bytes, size_t length, int *value);
size_t integer_text(int value, unsigned char *bytes);
#endif