interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / svc / allen_code.h

102 lines · 5339 bytes Raw Blame History
1/* allen_code.h -- Code from the Spark, the kernel side. A person asks Allen
2 * for code ("make me a calculator", "write a snake game in Python"); Allen
3 * says at once that he needs to code it and sends the request over the
4 * courier (svc/courier.h) to the Spark, where crates/allen-courier asks the
5 * language model with a coding prompt. The answer comes back as numbered
6 * pieces and a closing sentence:
7 *
8 * CODE <seq> <i>/<n> <bytes> <crc32> <lang> <piece>
9 * SAY <seq> 1/1 <Allen's sentence>
10 *
11 * Every piece but the last carries exactly CK_CODE_PIECE bytes of code (the
12 * piece is escaped on the wire: "\n" a newline, "\\" a backslash), so piece i
13 * lands at i * CK_CODE_PIECE whatever order the pieces come in. bytes is the
14 * whole length and crc32 (IEEE, 8 hex digits) the checksum of the whole; the
15 * kernel checks both before anything is shown. A missing piece or a bad
16 * checksum is asked for again once ("AGAIN <seq>": the Spark sends every
17 * piece and the sentence again); after that Allen says plainly that the code
18 * got lost on the way.
19 *
20 * Limits, stated: at most CK_CODE_MAX bytes and CK_CODE_LINES lines (the
21 * Spark cuts a longer answer at a line and says so), printable ASCII and
22 * newlines only (the Spark turns tabs into spaces and anything else into
23 * '?'). Pure and integer only: no I/O, host tested in
24 * svc/tests/allen_code_test.c (scripts/ck_allen_code_test.sh). */
25#ifndef AIENOS_ALLEN_CODE_H
26#define AIENOS_ALLEN_CODE_H
27#include <stddef.h>
28#include <stdint.h>
29
30#define CK_CODE_MAX 32768u /* bytes of code at most */
31#define CK_CODE_LINES 800u /* lines of code at most */
32#define CK_CODE_PIECE 128u /* code bytes per piece (escaped, at most 256 on the wire) */
33#define CK_CODE_PIECES (CK_CODE_MAX / CK_CODE_PIECE)
34#define CK_CODE_LANG 16u
35
36enum { /* where the pieces stand (ck_code_state) */
37 CK_CODE_NONE = 0, /* no piece has come */
38 CK_CODE_PART, /* some pieces, not all */
39 CK_CODE_WHOLE, /* every piece, length and checksum right */
40 CK_CODE_BAD, /* every piece, but the length or checksum is wrong */
41};
42
43typedef struct {
44 uint32_t seq; /* the job the pieces belong to */
45 uint32_t n; /* pieces announced (0: none yet) */
46 uint32_t bytes, crc; /* announced length and checksum */
47 uint32_t got; /* pieces taken */
48 uint32_t refused; /* lines that said CODE but did not fit (wrong job, malformed, past the limits) */
49 char lang[CK_CODE_LANG]; /* the language the Spark named ("python", "c"; "" when it named none) */
50 uint8_t have[CK_CODE_PIECES];
51 char text[CK_CODE_MAX + 1]; /* the code, NUL-terminated once whole */
52} ck_code_rx;
53
54/* Empties rx for job seq. */
55void ck_code_begin(ck_code_rx *rx, uint32_t seq);
56/* Takes one courier line. 1 when it is a CODE line for rx's job and was kept
57 * (a piece already held is kept again, no harm), 0 when the line is not a
58 * CODE line at all, -1 when it is a CODE line that does not fit (another
59 * job, a malformed head, a piece past the limits or of the wrong length, a
60 * head that disagrees with the first piece's): counted in rx->refused. */
61int ck_code_take(ck_code_rx *rx, const char *line);
62/* CK_CODE_NONE, _PART, _WHOLE or _BAD. Checks the checksum once all pieces are in. */
63int ck_code_state(ck_code_rx *rx);
64/* The first piece still missing (1-based), 0 when none is. */
65uint32_t ck_code_missing(const ck_code_rx *rx);
66/* Lines in a whole code text (a last line without a newline counts). */
67uint32_t ck_code_lines(const char *text, uint32_t bytes);
68/* CRC-32 (IEEE 802.3, reflected, as zlib's crc32) of n bytes. */
69uint32_t ck_code_crc32(const void *p, size_t n);
70
71/* ---- what the screen shows: rows of at most cols characters ---- */
72
73/* Breaks text into screen rows of at most cols characters: a line longer than
74 * cols goes on in the next row (cut at the last space in reach when there is
75 * one, else at cols). starts[i] is where row i begins in text, lens[i] its
76 * length, line_of[i] the 1-based line it belongs to (0 for a row that goes on
77 * from the one before). Returns the number of rows, at most max. */
78uint32_t ck_code_rows(const char *text, uint32_t bytes, uint32_t cols, uint32_t *starts, uint16_t *lens,
79 uint16_t *line_of, uint32_t max);
80/* The top row after a scroll: delta rows (negative up), kept within
81 * [0, rows - page] (0 when everything fits). */
82uint32_t ck_code_scroll(uint32_t top, int32_t delta, uint32_t rows, uint32_t page);
83
84/* ---- the wait for the Spark ---- */
85
86enum { /* what ck_code_wait_tick says should happen now */
87 CK_CODE_WAIT_NOTHING = 0,
88 CK_CODE_WAIT_SLOW, /* still nothing after CK_CODE_SLOW_MS: say once that the Spark is still at it */
89 CK_CODE_WAIT_GONE, /* silence for CK_CODE_GONE_MS: the Spark stopped answering; give the job up */
90};
91#define CK_CODE_SLOW_MS 20000u
92#define CK_CODE_GONE_MS 30000u
93typedef struct {
94 uint64_t asked_ms, heard_ms; /* when the job went, and the last word about it (WAIT, a piece, a part) */
95 int slow_said;
96} ck_code_wait;
97void ck_code_wait_begin(ck_code_wait *w, uint64_t now_ms);
98/* The Spark said something about the job (it is busy, or a piece came). */
99void ck_code_wait_heard(ck_code_wait *w, uint64_t now_ms);
100int ck_code_wait_tick(ck_code_wait *w, uint64_t now_ms);
101
102#endif