omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / aienux / net / wire / omtk.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/net/wire/omtk.om: OMTK v1 binary wire frame codec and validator for Pi 5 <-> Spark. |
| 3 | note Library unit: no fn main, no Linux syscalls, no heap. Prefix omk / OMK. |
| 4 | note All multi-byte integers are unsigned little-endian. Fixed 16-byte header: |
| 5 | note 0..3 u32 magic = 0x4B544D4F ("OMTK" LE = 1263816015) |
| 6 | note 4 u8 ver = 1 |
| 7 | note 5 u8 op = 1..9 (PING,PONG,RESET_KV,PREFILL,DECODE,TOKEN,DONE,CANCEL,ERR) |
| 8 | note 6..7 u16 flags = bit0 F_EOS (1), bit1 F_GREEDY (2); bits 2..15 must be 0 |
| 9 | note 8..11 u32 req_id = nonzero for PREFILL/DECODE/TOKEN/CANCEL; 0 allowed for PING/PONG/RESET_KV/DONE/ERR |
| 10 | note 12..15 u32 payload_len = 0..16384 (OMK_MAXPAY) |
| 11 | |
| 12 | const OMK_HDR ulong 16 |
| 13 | const OMK_MAXPAY ulong 16384 |
| 14 | const OMK_MAXFRM ulong 16400 |
| 15 | const OMK_MAXTOK ulong 4096 |
| 16 | const OMK_MAGIC ulong 1263816015 |
| 17 | const OMK_VER ulong 1 |
| 18 | |
| 19 | note Opcodes |
| 20 | const OMK_PING ulong 1 |
| 21 | const OMK_PONG ulong 2 |
| 22 | const OMK_RESET ulong 3 |
| 23 | const OMK_PREFILL ulong 4 |
| 24 | const OMK_DECODE ulong 5 |
| 25 | const OMK_TOKEN ulong 6 |
| 26 | const OMK_DONE ulong 7 |
| 27 | const OMK_CANCEL ulong 8 |
| 28 | const OMK_ERR ulong 9 |
| 29 | |
| 30 | note Flags (only bits 0..1 valid) |
| 31 | const OMK_FEOS ulong 1 |
| 32 | const OMK_FGREEDY ulong 2 |
| 33 | const OMK_FMASK ulong 3 |
| 34 | |
| 35 | note DONE reasons |
| 36 | const OMK_RMAX ulong 0 |
| 37 | const OMK_REOS ulong 1 |
| 38 | const OMK_RCANCEL ulong 2 |
| 39 | const OMK_RPREF ulong 3 |
| 40 | const OMK_RRESET ulong 4 |
| 41 | |
| 42 | note Error codes |
| 43 | const OMK_OK ulong 0 |
| 44 | const OMK_EBADMAG ulong 1 |
| 45 | const OMK_EBADVER ulong 2 |
| 46 | const OMK_EBADOP ulong 3 |
| 47 | const OMK_EBADFLG ulong 4 |
| 48 | const OMK_ETOOBIG ulong 5 |
| 49 | const OMK_EBADLEN ulong 6 |
| 50 | const OMK_EBADREQ ulong 7 |
| 51 | const OMK_EBADTOK ulong 8 |
| 52 | const OMK_ERANGE ulong 9 |
| 53 | const OMK_EBUSY ulong 10 |
| 54 | const OMK_EUNSUP ulong 11 |
| 55 | const OMK_EGSTAGE ulong 12 |
| 56 | const OMK_EGKICK ulong 13 |
| 57 | const OMK_EGTMO ulong 14 |
| 58 | const OMK_EGHALT ulong 15 |
| 59 | const OMK_ESEM ulong 16 |
| 60 | const OMK_EGVER ulong 17 |
| 61 | const OMK_EFAULT ulong 18 |
| 62 | |
| 63 | note Parsed header fields (omkhdr): 0 magic, 1 ver, 2 op, 3 flags, 4 req_id, 5 payload_len |
| 64 | global omkhdr ulong 8 |
| 65 | note Scratch staging buffer for encoding/decoding single frames (16 B hdr + up to 16384 B payload) |
| 66 | global omktx uchar 16400 |
| 67 | global omktxn ulong 1 |
| 68 | |
| 69 | fn omk_r16 buf view uchar * off ulong to ulong |
| 70 | a = ulong get buf off |
| 71 | b = ulong get buf add off 1 |
| 72 | ret or a shl b 8 |
| 73 | end |
| 74 | |
| 75 | fn omk_r32 buf view uchar * off ulong to ulong |
| 76 | a = ulong get buf off |
| 77 | b = ulong get buf add off 1 |
| 78 | c = ulong get buf add off 2 |
| 79 | d = ulong get buf add off 3 |
| 80 | ret or or or a shl b 8 shl c 16 shl d 24 |
| 81 | end |
| 82 | |
| 83 | fn omk_w16 buf edit uchar * off ulong v ulong to void |
| 84 | put buf off uchar and v 255 |
| 85 | put buf add off 1 uchar and shr v 8 255 |
| 86 | end |
| 87 | |
| 88 | fn omk_w32 buf edit uchar * off ulong v ulong to void |
| 89 | put buf off uchar and v 255 |
| 90 | put buf add off 1 uchar and shr v 8 255 |
| 91 | put buf add off 2 uchar and shr v 16 255 |
| 92 | put buf add off 3 uchar and shr v 24 255 |
| 93 | end |
| 94 | |
| 95 | note Global-buffer LE helpers (for omktx and rx accumulators without passing globals) |
| 96 | fn omk_txw16 off ulong v ulong to void |
| 97 | put omktx off uchar and v 255 |
| 98 | put omktx add off 1 uchar and shr v 8 255 |
| 99 | end |
| 100 | |
| 101 | fn omk_txw32 off ulong v ulong to void |
| 102 | put omktx off uchar and v 255 |
| 103 | put omktx add off 1 uchar and shr v 8 255 |
| 104 | put omktx add off 2 uchar and shr v 16 255 |
| 105 | put omktx add off 3 uchar and shr v 24 255 |
| 106 | end |
| 107 | |
| 108 | note Write 16-byte OMTK v1 header into omktx[0..16); sets omktxn = 16 + plen. |
| 109 | fn omk_mkhdr op ulong flags ulong req_id ulong plen ulong to ulong |
| 110 | if gt plen OMK_MAXPAY |
| 111 | ret OMK_ETOOBIG |
| 112 | end |
| 113 | omk_txw32 0 OMK_MAGIC |
| 114 | put omktx 4 uchar OMK_VER |
| 115 | put omktx 5 uchar and op 255 |
| 116 | omk_txw16 6 and flags 65535 |
| 117 | omk_txw32 8 req_id |
| 118 | omk_txw32 12 plen |
| 119 | put omktxn 0 add OMK_HDR plen |
| 120 | ret OMK_OK |
| 121 | end |
| 122 | |
| 123 | note Validate header fields already unpacked into omkhdr[0..5]. Returns OMK_OK or OMK_E*. |
| 124 | fn omk_chk_hdr to ulong |
| 125 | mag = get omkhdr 0 |
| 126 | if ne mag OMK_MAGIC |
| 127 | ret OMK_EBADMAG |
| 128 | end |
| 129 | ver = get omkhdr 1 |
| 130 | if ne ver OMK_VER |
| 131 | ret OMK_EBADVER |
| 132 | end |
| 133 | op = get omkhdr 2 |
| 134 | if or lt op OMK_PING gt op OMK_ERR |
| 135 | ret OMK_EBADOP |
| 136 | end |
| 137 | flg = get omkhdr 3 |
| 138 | if ne and flg not OMK_FMASK 0 |
| 139 | ret OMK_EBADFLG |
| 140 | end |
| 141 | req = get omkhdr 4 |
| 142 | plen = get omkhdr 5 |
| 143 | if gt plen OMK_MAXPAY |
| 144 | ret OMK_ETOOBIG |
| 145 | end |
| 146 | if eq op OMK_PING |
| 147 | if ne plen 0 |
| 148 | ret OMK_EBADLEN |
| 149 | end |
| 150 | ret OMK_OK |
| 151 | end |
| 152 | if eq op OMK_PONG |
| 153 | if and ne plen 0 ne plen 16 |
| 154 | ret OMK_EBADLEN |
| 155 | end |
| 156 | ret OMK_OK |
| 157 | end |
| 158 | if eq op OMK_RESET |
| 159 | if ne plen 4 |
| 160 | ret OMK_EBADLEN |
| 161 | end |
| 162 | ret OMK_OK |
| 163 | end |
| 164 | if eq op OMK_PREFILL |
| 165 | if eq req 0 |
| 166 | ret OMK_EBADREQ |
| 167 | end |
| 168 | if or lt plen 4 ne rem plen 4 0 |
| 169 | ret OMK_EBADLEN |
| 170 | end |
| 171 | ret OMK_OK |
| 172 | end |
| 173 | if eq op OMK_DECODE |
| 174 | if eq req 0 |
| 175 | ret OMK_EBADREQ |
| 176 | end |
| 177 | if ne plen 16 |
| 178 | ret OMK_EBADLEN |
| 179 | end |
| 180 | ret OMK_OK |
| 181 | end |
| 182 | if eq op OMK_TOKEN |
| 183 | if eq req 0 |
| 184 | ret OMK_EBADREQ |
| 185 | end |
| 186 | if ne plen 8 |
| 187 | ret OMK_EBADLEN |
| 188 | end |
| 189 | ret OMK_OK |
| 190 | end |
| 191 | if eq op OMK_DONE |
| 192 | if ne plen 8 |
| 193 | ret OMK_EBADLEN |
| 194 | end |
| 195 | ret OMK_OK |
| 196 | end |
| 197 | if eq op OMK_CANCEL |
| 198 | if eq req 0 |
| 199 | ret OMK_EBADREQ |
| 200 | end |
| 201 | if ne plen 0 |
| 202 | ret OMK_EBADLEN |
| 203 | end |
| 204 | ret OMK_OK |
| 205 | end |
| 206 | if eq op OMK_ERR |
| 207 | if ne plen 8 |
| 208 | ret OMK_EBADLEN |
| 209 | end |
| 210 | ret OMK_OK |
| 211 | end |
| 212 | ret OMK_EBADOP |
| 213 | end |
| 214 | |
| 215 | note Parse 16-byte header from a view buffer at offset off into omkhdr[0..5] and validate. |
| 216 | fn omk_parse_hdr buf view uchar * off ulong to ulong |
| 217 | if gt off len buf |
| 218 | ret OMK_EBADLEN |
| 219 | end |
| 220 | if lt sub len buf off OMK_HDR |
| 221 | ret OMK_EBADLEN |
| 222 | end |
| 223 | put omkhdr 0 omk_r32 buf off |
| 224 | put omkhdr 1 ulong get buf add off 4 |
| 225 | put omkhdr 2 ulong get buf add off 5 |
| 226 | put omkhdr 3 omk_r16 buf add off 6 |
| 227 | put omkhdr 4 omk_r32 buf add off 8 |
| 228 | put omkhdr 5 omk_r32 buf add off 12 |
| 229 | ret omk_chk_hdr |
| 230 | end |
| 231 | |
| 232 | note Encode standard Spark -> Pi replies into omktx[0..omktxn). |
| 233 | fn omk_enc_pong req_id ulong st ulong kv ulong mctx ulong gerr ulong to ulong |
| 234 | rc = omk_mkhdr OMK_PONG 0 req_id 16 |
| 235 | if ne rc OMK_OK |
| 236 | ret rc |
| 237 | end |
| 238 | omk_txw32 16 st |
| 239 | omk_txw32 20 kv |
| 240 | omk_txw32 24 mctx |
| 241 | omk_txw32 28 gerr |
| 242 | ret OMK_OK |
| 243 | end |
| 244 | |
| 245 | fn omk_enc_token req_id ulong flags ulong seq ulong tok ulong to ulong |
| 246 | rc = omk_mkhdr OMK_TOKEN flags req_id 8 |
| 247 | if ne rc OMK_OK |
| 248 | ret rc |
| 249 | end |
| 250 | omk_txw32 16 seq |
| 251 | omk_txw32 20 tok |
| 252 | ret OMK_OK |
| 253 | end |
| 254 | |
| 255 | fn omk_enc_done req_id ulong flags ulong kv ulong reason ulong to ulong |
| 256 | rc = omk_mkhdr OMK_DONE flags req_id 8 |
| 257 | if ne rc OMK_OK |
| 258 | ret rc |
| 259 | end |
| 260 | omk_txw32 16 kv |
| 261 | omk_txw32 20 reason |
| 262 | ret OMK_OK |
| 263 | end |
| 264 | |
| 265 | fn omk_enc_err req_id ulong ecode ulong detail ulong to ulong |
| 266 | rc = omk_mkhdr OMK_ERR 0 req_id 8 |
| 267 | if ne rc OMK_OK |
| 268 | ret rc |
| 269 | end |
| 270 | omk_txw32 16 ecode |
| 271 | omk_txw32 20 detail |
| 272 | ret OMK_OK |
| 273 | end |