{ "Makefile": "CC = gcc\nCFLAGS = -std=c11 -O2 -Wall -Wextra -pedantic\nSOURCES = window.c store.c instruction.c console.c observe.c driver.c\ncode-window: $(SOURCES) window.h console.h\n\t$(CC) $(CFLAGS) $(SOURCES) -o $@\n.PHONY: clean\nclean:\n\trm -f code-window\n", "README.md": "# Code Window workbench\n\nThis is a deterministic offline deployment workbench for a small integer instruction language. It has no executable memory or machine instructions. The operator can install and edit four independent code windows, then execute their published views.\n\nBuild with `make` (C11 gcc); replay `./code-window < examples/field.txt`. `make clean` removes only this project's binary. Build and run need no downloads, devices, privileges or clock. The console continues after rejected commands and exits 0 at ordinary EOF; diagnostics on stderr are permitted.\n\nThe public delivery contract is all clauses in `docs/window.md`, `docs/instructions.md` and `docs/console.md`. Public headers describe the implementation boundary. Only `window.c` is submitted and graded; other files are build and observation support. Extra files made during development are ignored by final grading.\n", "console.c": "#include \"console.h\"\n#include \n#include \n\nLineStatus console_line(FILE *stream, char *buffer, size_t capacity)\n{\n size_t used = 0;\n int over = 0;\n int ch;\n while ((ch = fgetc(stream)) != EOF && ch != '\\n') {\n if (used + 1 < capacity) {\n buffer[used++] = (char)ch;\n } else {\n over = 1;\n }\n }\n if (ferror(stream)) {\n return LINE_IO;\n }\n if (ch == EOF && used == 0 && !over) {\n return LINE_END;\n }\n if (used && buffer[used - 1] == '\\r') {\n --used;\n }\n buffer[used] = '\\0';\n return over ? LINE_LONG : LINE_TEXT;\n}\n\nint console_ignored(const char *line)\n{\n while (*line && isspace((unsigned char)*line)) {\n ++line;\n }\n return *line == '\\0' || *line == '#';\n}\n\nstatic size_t tokenize(char *line, char **tokens, size_t capacity)\n{\n size_t count = 0;\n char *cursor = line;\n while (*cursor) {\n while (*cursor && isspace((unsigned char)*cursor)) {\n ++cursor;\n }\n if (!*cursor) {\n break;\n }\n if (count == capacity) {\n return capacity + 1;\n }\n tokens[count++] = cursor;\n while (*cursor && !isspace((unsigned char)*cursor)) {\n ++cursor;\n }\n if (*cursor) {\n *cursor++ = '\\0';\n }\n }\n return count;\n}\n\nstatic int decimal(const char *token, uint64_t *result)\n{\n uint64_t value = 0;\n if (!*token) {\n return 0;\n }\n for (const unsigned char *p = (const unsigned char *)token; *p; ++p) {\n if (*p < '0' || *p > '9') {\n return 0;\n }\n unsigned digit = *p - '0';\n if (value > (UINT64_MAX - digit) / 10u) {\n return 0;\n }\n value = value * 10u + digit;\n }\n *result = value;\n return 1;\n}\n\nstatic int bounded(const char *token, uint64_t maximum, uint64_t *result)\n{\n return decimal(token, result) && *result <= maximum;\n}\n\nstatic int parse_words(char **tokens, size_t start, Command *command)\n{\n for (unsigned i = 0; i < command->count; ++i) {\n uint64_t value;\n if (!bounded(tokens[start + i], UINT16_MAX, &value)) {\n return 0;\n }\n command->words[i] = (uint16_t)value;\n }\n return 1;\n}\n\nCommand console_parse(char *line)\n{\n Command command = {0};\n char *tokens[CW_TOKEN_LIMIT];\n size_t count = tokenize(line, tokens, CW_TOKEN_LIMIT);\n if (count == 0 || count > CW_TOKEN_LIMIT || strlen(tokens[0]) != 1) {\n return command;\n }\n char kind = tokens[0][0];\n if (kind == 'R') {\n if (count == 1) {\n command.kind = CMD_RESET;\n }\n return command;\n }\n if (count < 2) {\n return command;\n }\n uint64_t value;\n if (!bounded(tokens[1], CW_SLOTS - 1, &value)) {\n return command;\n }\n command.slot = (unsigned)value;\n if (kind == 'D' && count == 3) {\n if (decimal(tokens[2], &command.expected)) {\n command.kind = CMD_DROP;\n }\n } else if (kind == 'E' && count == 3) {\n if (bounded(tokens[2], UINT32_MAX, &value)) {\n command.input = (uint32_t)value;\n command.kind = CMD_EXECUTE;\n }\n } else if (kind == 'I' && count >= 4) {\n if (bounded(tokens[2], CW_CAPACITY, &value) && value > 0) {\n command.count = (unsigned)value;\n if (count == 3 + value && parse_words(tokens, 3, &command)) {\n command.kind = CMD_INSTALL;\n }\n }\n } else if (kind == 'P' && count >= 6) {\n if (!decimal(tokens[2], &command.expected)) {\n return command;\n }\n if (!bounded(tokens[3], CW_CAPACITY - 1, &value)) {\n return command;\n }\n command.first = (unsigned)value;\n if (bounded(tokens[4], CW_CAPACITY, &value) && value > 0) {\n command.count = (unsigned)value;\n if (count == 5 + value && parse_words(tokens, 5, &command)) {\n command.kind = CMD_PATCH;\n }\n }\n }\n return command;\n}\n", "console.h": "#ifndef CODE_CONSOLE_H\n#define CODE_CONSOLE_H\n#include \"window.h\"\n#include \n#define CW_LINE_LIMIT 1023u\n#define CW_TOKEN_LIMIT 32u\ntypedef enum {\n CMD_BAD,\n CMD_RESET,\n CMD_INSTALL,\n CMD_PATCH,\n CMD_DROP,\n CMD_EXECUTE\n} CommandKind;\ntypedef struct {\n CommandKind kind;\n unsigned slot;\n unsigned first;\n unsigned count;\n uint64_t expected;\n uint32_t input;\n uint16_t words[CW_CAPACITY];\n} Command;\ntypedef enum {\n LINE_END = 0,\n LINE_TEXT = 1,\n LINE_LONG = 2,\n LINE_IO = 3\n} LineStatus;\nLineStatus console_line(FILE *stream, char *buffer, size_t capacity);\nint console_ignored(const char *line);\nCommand console_parse(char *line);\nCodeResult dispatch(CodeWorkbench *work, const Command *command);\n#endif\n", "docs/console.md": "# Console protocol and observations\n\nC1. Input is ASCII, one command per line, maximum 1023 bytes excluding LF; optional CR before LF is accepted. Blank lines and lines whose first non-space character is # are ignored without consuming a sequence number. Whitespace separates tokens. Tokens containing # are not comments. EOF after an unterminated final line is accepted. Longer lines are rejected once and drained through LF.\nC2. Integers are decimal digits only, with no sign, prefix or suffix. A token's numeric value must be representable as uint64. Leading zeros are accepted. Commands and arities are exact: R; I slot count followed by exactly count words; P slot expected first count followed by exactly count words; D slot expected; E slot input. Unknown commands and any invalid syntax return -4. Slot=0..3, count=1..12, first=0..11, word=0..65535, input=0..4294967295; expected may be any uint64. No extra tokens are allowed.\nC3. Every nonignored line consumes one sequence number starting at 1. R resets state and counters including rejected and returns zeros; sequence numbering does not reset. Every other command with nonzero rc increments rejected once. Commands are processed in order. Normal EOF returns process exit code 0, even if commands were rejected.\nC4. Each processed line emits O sequence rc value retired, then S sequence serial installs patches drops executions writes sync_calls sync_words rejected. Then for slots 0..3 it emits B sequence slot active length generation, D sequence slot followed by that active slot's data words, and X sequence slot followed by that active slot's execution words. Inactive D/X lines have no words. Numbers are decimal and fields separated by single ASCII spaces. One LF ends each observation. No other stdout is emitted; stderr diagnostics are allowed.\nC5. The observation driver reports current public metadata and valid-range word arrays without repair, assertions or expected results. The supported build is `make`, using gcc C11 and the provided support sources; `./code-window < examples/field.txt` replays the one-slot field report. Other allowed streams include all slots, repeated installs/drops, nonzero patch offsets, any instruction operands in range, optimistic conflicts and invalid program edits. A submitted file may use C11 standard library facilities and the public interface, and must build with the supplied sources.\n", "docs/instructions.md": "# Offline integer instruction language\n\nL1. A word is an unsigned 16-bit integer. The high byte is opcode and the low byte is operand. A valid whole program has 1..12 words; exactly its last word is HALT (opcode 0, operand 0), and no earlier word is HALT. Other opcode/operand pairs must satisfy the table below. The program is a bounded straight-line sequence, with no jumps, memory access or external calls.\nL2. Arithmetic uses a uint32 accumulator initialized to the command's input. All arithmetic wraps modulo 2^32. Signed operand s is b when b<128, otherwise b-256. One instruction is retired for every inspected word, including HALT. A valid run stops after its final HALT.\nL3. Opcode 1 ADD accepts any byte b and computes accumulator+s. Opcode 2 XOR accepts any byte and computes accumulator XOR b. Opcode 3 MUL accepts any byte and computes accumulator*b. Opcode 4 ROT accepts b=0..31 and rotates the uint32 accumulator left b bits (b=0 leaves it unchanged). Opcode 5 SET accepts any byte and sets accumulator to signed s reduced modulo 2^32. Opcode 6 AND accepts any byte and computes accumulator AND b. Opcodes 7..255 are invalid; HALT with nonzero operand is invalid.\nL4. The interpreter accepts only a valid whole program, but reports raw faults deterministically: scan in order, rejecting any invalid opcode/operand, early HALT or a non-HALT last word at the first such word. Return -5, value=0 and retired including that inspected word. Active length is always 1..12. This reports a stale or incomplete execution view without invoking undefined behavior.\n", "docs/window.md": "# Window publication contract\n\nW1. There are four slots numbered 0..3. Each contains independent data and execution word arrays of capacity 12, plus active, length and generation metadata. Reset sets all arrays, metadata and counters to zero. Only array elements 0..length-1 of active slots are observable contractual content; unused tails are unspecified.\nW2. A publication is a successful install, patch or drop. Each publication increments that slot's generation by one and the global serial by one. Generations belong to slots; activity in a different slot cannot invalidate a slot's expected generation. Reset starts a new workbench, with generations and serial zero.\nW3. Install `I slot count words...` accepts 1..12 words forming a valid whole program as defined in instructions.md. It may replace an active slot. Before publication it writes exactly count data words at offset 0 and synchronizes exactly the half-open range [0,count) once. It then publishes active=1, length=count. Install does not require an expected generation.\nW4. Patch `P slot expected first count words...` accepts 1..12 replacement words. Inactive slot returns -2. Otherwise an expected generation differing from this slot's generation returns -1. These checks precede range/program checks. For an active matching slot, first+count must be <=length and the resulting whole program must be valid, or return -3. Successful patch changes only that data slice, synchronizes exactly [first,first+count) once, then publishes the same active length. Counted writes equal count. Existing data outside the slice is preserved.\nW5. Drop `D slot expected` returns -2 for inactive slots, then -1 for mismatching slot generation. Success publishes active=0,length=0, increments the drop count and performs no data writes or synchronization. Per-slot generation survives drop. A later install reuses the slot and increments its surviving generation.\nW6. Any declined publication operation has no slot, serial, store-counter or interpreter-counter effects. Inactive execution and invalid input syntax also have no such effects; execution-view faults follow W7. The console rejected counter does increment. This includes invalid whole programs, optimistic conflicts and inactive operations. Invalid input syntax never enters a window operation.\nW7. Execute `E slot input` returns -2 on inactive slots. Otherwise it evaluates the execution view, never the data view, with the interpreter specified in instructions.md. An active execution increments executions once. It changes no slot metadata, word array, publication or synchronization counters. Value and retired are raw interpreter observations. Normal execution returns 0; malformed execution-view programs return -5 with value 0 and retired equal to instructions consumed before the first invalid word, including that word. Such malformed views can expose publication faults.\nW8. Store adapter calls are observable. `store_write` writes an in-bounds slice and adds its count to writes. `store_sync` copies the half-open data slice to the execution slice, adds one to sync_calls and its width to sync_words; it does not publish. `store_publish` changes only metadata, increments generation/serial and the appropriate installs/patches/drops counter. Invalid adapter calls return -6 and have no effects. Window operations must fulfill W3-W7 without extra adapter operations.\nW9. A successful mutating command returns rc=0,value=0,retired=0. Declined commands return the specified rc and zero value/retired. All counters and generations are unsigned 64-bit; tested streams stay below overflow. The API receives only syntactically valid bounded fields from the console, but whole-program validity and active/range/generation decisions are window responsibilities.\n", "driver.c": "#include \"console.h\"\n#include \n\nCodeResult dispatch(CodeWorkbench *work, const Command *command)\n{\n CodeResult result = {CW_SYNTAX, 0, 0};\n switch (command->kind) {\n case CMD_RESET:\n store_reset(work);\n result.rc = CW_OK;\n break;\n case CMD_INSTALL:\n result = window_install(work, command->slot, command->words,\n command->count);\n break;\n case CMD_PATCH:\n result = window_patch(work, command->slot, command->expected,\n command->first, command->words, command->count);\n break;\n case CMD_DROP:\n result = window_drop(work, command->slot, command->expected);\n break;\n case CMD_EXECUTE:\n result = window_execute(work, command->slot, command->input);\n break;\n default:\n break;\n }\n if (result.rc != CW_OK) {\n work->counts.rejected += 1;\n }\n return result;\n}\n\nint main(void)\n{\n CodeWorkbench work;\n char line[CW_LINE_LIMIT + 1];\n uint64_t sequence = 0;\n store_reset(&work);\n for (;;) {\n LineStatus status = console_line(stdin, line, sizeof(line));\n if (status == LINE_END) {\n return 0;\n }\n if (status == LINE_IO) {\n fputs(\"input read failed\\n\", stderr);\n return 2;\n }\n if (status == LINE_TEXT && console_ignored(line)) {\n continue;\n }\n ++sequence;\n CodeResult result;\n if (status == LINE_LONG) {\n result.rc = CW_SYNTAX;\n result.value = 0;\n result.retired = 0;\n work.counts.rejected += 1;\n } else {\n Command command = console_parse(line);\n result = dispatch(&work, &command);\n }\n observe_result(sequence, result);\n observe_state(sequence, &work);\n }\n}\n", "examples/field.txt": "# one-slot operator replay\nR\nI 0 4 257 770 1031 0\nE 0 11\nP 0 1 1 1 771\nE 0 11\n", "instruction.c": "#include \"window.h\"\n\nstatic unsigned opcode(uint16_t word)\n{\n return word >> 8;\n}\n\nstatic unsigned operand(uint16_t word)\n{\n return word & 255u;\n}\n\nint instruction_valid(uint16_t word, int last)\n{\n unsigned op = opcode(word);\n unsigned byte = operand(word);\n if (last) {\n return op == 0 && byte == 0;\n }\n if (op == 0 || op > 6) {\n return 0;\n }\n if (op == 4 && byte > 31) {\n return 0;\n }\n return 1;\n}\n\nint program_valid(const uint16_t *words, unsigned count)\n{\n if (words == NULL || count == 0 || count > CW_CAPACITY) {\n return 0;\n }\n for (unsigned index = 0; index < count; ++index) {\n if (!instruction_valid(words[index], index + 1 == count)) {\n return 0;\n }\n }\n return 1;\n}\n\nstatic uint32_t signed_operand(unsigned byte)\n{\n if (byte < 128) {\n return byte;\n }\n return UINT32_MAX - (255u - byte);\n}\n\nstatic uint32_t rotate_left(uint32_t value, unsigned bits)\n{\n if (bits == 0) {\n return value;\n }\n return (value << bits) | (value >> (32u - bits));\n}\n\nstatic uint32_t apply_instruction(uint32_t accumulator, uint16_t word)\n{\n unsigned byte = operand(word);\n switch (opcode(word)) {\n case 1:\n return accumulator + signed_operand(byte);\n case 2:\n return accumulator ^ byte;\n case 3:\n return accumulator * byte;\n case 4:\n return rotate_left(accumulator, byte);\n case 5:\n return signed_operand(byte);\n case 6:\n return accumulator & byte;\n default:\n return accumulator;\n }\n}\n\nCodeResult interpret(const uint16_t *words, unsigned count, uint32_t input)\n{\n CodeResult result = {CW_OK, input, 0};\n if (words == NULL || count == 0 || count > CW_CAPACITY) {\n result.rc = CW_EXECUTION;\n result.value = 0;\n return result;\n }\n for (unsigned index = 0; index < count; ++index) {\n uint16_t word = words[index];\n result.retired += 1;\n if (!instruction_valid(word, index + 1 == count)) {\n result.rc = CW_EXECUTION;\n result.value = 0;\n return result;\n }\n if (opcode(word) != 0) {\n result.value = apply_instruction(result.value, word);\n }\n }\n return result;\n}\n", "observe.c": "#include \"window.h\"\n#include \n#include \n\nvoid observe_result(uint64_t sequence, CodeResult result)\n{\n printf(\"O %\" PRIu64 \" %d %\" PRIu32 \" %u\\n\",\n sequence, result.rc, result.value, result.retired);\n}\n\nstatic void observe_counters(uint64_t sequence, const CodeCounters *c)\n{\n printf(\"S %\" PRIu64 \" %\" PRIu64 \" %\" PRIu64 \" %\" PRIu64,\n sequence, c->serial, c->installs, c->patches);\n printf(\" %\" PRIu64 \" %\" PRIu64 \" %\" PRIu64,\n c->drops, c->executions, c->writes);\n printf(\" %\" PRIu64 \" %\" PRIu64 \" %\" PRIu64 \"\\n\",\n c->sync_calls, c->sync_words, c->rejected);\n}\n\nstatic void observe_slot(uint64_t sequence, unsigned index, const CodeSlot *slot)\n{\n printf(\"B %\" PRIu64 \" %u %u %u %\" PRIu64 \"\\n\",\n sequence, index, slot->active, slot->length, slot->generation);\n printf(\"D %\" PRIu64 \" %u\", sequence, index);\n if (slot->active) {\n for (unsigned word = 0; word < slot->length && word < CW_CAPACITY; ++word) {\n printf(\" %u\", (unsigned)slot->data[word]);\n }\n }\n putchar('\\n');\n printf(\"X %\" PRIu64 \" %u\", sequence, index);\n if (slot->active) {\n for (unsigned word = 0; word < slot->length && word < CW_CAPACITY; ++word) {\n printf(\" %u\", (unsigned)slot->execution[word]);\n }\n }\n putchar('\\n');\n}\n\nvoid observe_state(uint64_t sequence, const CodeWorkbench *work)\n{\n observe_counters(sequence, &work->counts);\n for (unsigned slot = 0; slot < CW_SLOTS; ++slot) {\n observe_slot(sequence, slot, &work->slots[slot]);\n }\n}\n", "store.c": "#include \"window.h\"\n#include \n\nstatic int slot_index(unsigned slot)\n{\n return slot < CW_SLOTS;\n}\n\nstatic int span_valid(unsigned first, unsigned count)\n{\n if (first > CW_CAPACITY) {\n return 0;\n }\n if (count > CW_CAPACITY - first) {\n return 0;\n }\n return 1;\n}\n\nvoid store_reset(CodeWorkbench *work)\n{\n memset(work, 0, sizeof(*work));\n}\n\nint store_write(CodeWorkbench *work, unsigned slot, unsigned first,\n const uint16_t *words, unsigned count)\n{\n if (!slot_index(slot)) {\n return CW_ADAPTER;\n }\n if (words == NULL || count == 0 || !span_valid(first, count)) {\n return CW_ADAPTER;\n }\n CodeSlot *target = &work->slots[slot];\n for (unsigned index = 0; index < count; ++index) {\n target->data[first + index] = words[index];\n }\n work->counts.writes += count;\n return CW_OK;\n}\n\nint store_sync(CodeWorkbench *work, unsigned slot, unsigned first,\n unsigned end)\n{\n if (!slot_index(slot)) {\n return CW_ADAPTER;\n }\n if (end <= first || end > CW_CAPACITY) {\n return CW_ADAPTER;\n }\n CodeSlot *target = &work->slots[slot];\n for (unsigned index = first; index < end; ++index) {\n target->execution[index] = target->data[index];\n }\n work->counts.sync_calls += 1;\n work->counts.sync_words += end - first;\n return CW_OK;\n}\n\nstatic int publication_valid(Publication kind, unsigned length)\n{\n if (kind == CW_DROP) {\n return length == 0;\n }\n if (kind != CW_INSTALL && kind != CW_PATCH) {\n return 0;\n }\n return length > 0 && length <= CW_CAPACITY;\n}\n\nint store_publish(CodeWorkbench *work, unsigned slot, unsigned length,\n Publication kind)\n{\n if (!slot_index(slot) || !publication_valid(kind, length)) {\n return CW_ADAPTER;\n }\n CodeSlot *target = &work->slots[slot];\n target->active = kind != CW_DROP;\n target->length = length;\n target->generation += 1;\n work->counts.serial += 1;\n switch (kind) {\n case CW_INSTALL:\n work->counts.installs += 1;\n break;\n case CW_PATCH:\n work->counts.patches += 1;\n break;\n case CW_DROP:\n work->counts.drops += 1;\n break;\n default:\n return CW_ADAPTER;\n }\n return CW_OK;\n}\n", "window.h": "#ifndef CODE_WINDOW_H\n#define CODE_WINDOW_H\n#include \n#include \n#define CW_SLOTS 4u\n#define CW_CAPACITY 12u\n#define CW_OK 0\n#define CW_CONFLICT (-1)\n#define CW_INACTIVE (-2)\n#define CW_PROGRAM (-3)\n#define CW_SYNTAX (-4)\n#define CW_EXECUTION (-5)\n#define CW_ADAPTER (-6)\ntypedef struct {\n uint16_t data[CW_CAPACITY];\n uint16_t execution[CW_CAPACITY];\n unsigned active;\n unsigned length;\n uint64_t generation;\n} CodeSlot;\ntypedef struct {\n uint64_t serial;\n uint64_t installs;\n uint64_t patches;\n uint64_t drops;\n uint64_t executions;\n uint64_t writes;\n uint64_t sync_calls;\n uint64_t sync_words;\n uint64_t rejected;\n} CodeCounters;\ntypedef struct {\n CodeSlot slots[CW_SLOTS];\n CodeCounters counts;\n} CodeWorkbench;\ntypedef struct {\n int rc;\n uint32_t value;\n unsigned retired;\n} CodeResult;\ntypedef enum {\n CW_INSTALL = 0,\n CW_PATCH = 1,\n CW_DROP = 2\n} Publication;\nvoid store_reset(CodeWorkbench *work);\nint store_write(CodeWorkbench *work, unsigned slot, unsigned first,\n const uint16_t *words, unsigned count);\nint store_sync(CodeWorkbench *work, unsigned slot, unsigned first,\n unsigned end);\nint store_publish(CodeWorkbench *work, unsigned slot, unsigned length,\n Publication kind);\nint instruction_valid(uint16_t word, int last);\nint program_valid(const uint16_t *words, unsigned count);\nCodeResult interpret(const uint16_t *words, unsigned count, uint32_t input);\nCodeResult window_install(CodeWorkbench *work, unsigned slot,\n const uint16_t *words, unsigned count);\nCodeResult window_patch(CodeWorkbench *work, unsigned slot, uint64_t expected,\n unsigned first, const uint16_t *words, unsigned count);\nCodeResult window_drop(CodeWorkbench *work, unsigned slot, uint64_t expected);\nCodeResult window_execute(CodeWorkbench *work, unsigned slot, uint32_t input);\nvoid observe_result(uint64_t sequence, CodeResult result);\nvoid observe_state(uint64_t sequence, const CodeWorkbench *work);\n#endif\n" }