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 / tools / pi-drive-27b / pi_drive_omtk.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note tools/pi-drive-27b/pi_drive_omtk.om: Pure seeded Omega (OM-2) Pi 5 client for OMTK v1 binary |
| 3 | note token-ID wire protocol (Piece 6). Zero HTTP, zero JSON, zero C/Python. |
| 4 | note Build on Pi 5 / AArch64 Linux: |
| 5 | note { printf 'om2\n'; cat aienux/net/wire/omtk.om tools/pi-drive-27b/pi_drive_omtk.om; } | ./seed/out/omc > pi_drive_omtk |
| 6 | note Usage: |
| 7 | note ./pi_drive_omtk <ipv4> <port> ping |
| 8 | note ./pi_drive_omtk <ipv4> <port> reset <keep_pos> |
| 9 | note ./pi_drive_omtk <ipv4> <port> infer <max_tokens> <eos_id> <tok0> [tok1 ...] |
| 10 | |
| 11 | global cl_arg uchar 128 |
| 12 | global cl_sa uchar 16 |
| 13 | global cl_rx uchar 16400 |
| 14 | global cl_num uchar 32 |
| 15 | |
| 16 | text Mpong "PONG state=" |
| 17 | text Mkv " kv=" |
| 18 | text Mmctx " max_ctx=" |
| 19 | text Mgerr " gpu_err=" |
| 20 | text Mtok "TOKEN " |
| 21 | text Mdone "DONE kv=" |
| 22 | text Mreas " reason=" |
| 23 | text Merr "ERR code=" |
| 24 | text Mdet " detail=" |
| 25 | text Msp " " |
| 26 | text Mnl "\n" |
| 27 | text Cping "ping" |
| 28 | text Creset "reset" |
| 29 | text Cinfer "infer" |
| 30 | |
| 31 | fn cl_put_u v ulong to void |
| 32 | i = ulong 32 |
| 33 | while true bound 21 |
| 34 | i = sub i 1 |
| 35 | put cl_num i uchar add 48 rem v 10 |
| 36 | v = div v 10 |
| 37 | if eq v 0 |
| 38 | break |
| 39 | end |
| 40 | end |
| 41 | k = write 1 cl_num i sub 32 i |
| 42 | end |
| 43 | |
| 44 | fn cl_arg_len idx ulong to ulong |
| 45 | if ge long idx argcnt |
| 46 | ret 0 |
| 47 | end |
| 48 | k = argread idx cl_arg 0 127 |
| 49 | if lt k 0 |
| 50 | ret 0 |
| 51 | end |
| 52 | ret ulong k |
| 53 | end |
| 54 | |
| 55 | fn cl_parse_u n ulong to ulong |
| 56 | if or eq n 0 gt n 10 |
| 57 | exit 2 |
| 58 | end |
| 59 | v = ulong 0 |
| 60 | i = ulong 0 |
| 61 | while lt i n bound 10 |
| 62 | c = ulong get cl_arg i |
| 63 | if or lt c 48 gt c 57 |
| 64 | exit 2 |
| 65 | end |
| 66 | v = add mul v 10 sub c 48 |
| 67 | if gt v 4294967295 |
| 68 | exit 2 |
| 69 | end |
| 70 | i = add i 1 |
| 71 | end |
| 72 | ret v |
| 73 | end |
| 74 | |
| 75 | note Parse dotted IPv4 string in cl_arg[0..n) into big-endian u32. |
| 76 | fn cl_parse_ip n ulong to ulong |
| 77 | if or eq n 0 gt n 15 |
| 78 | exit 2 |
| 79 | end |
| 80 | v = ulong 0 |
| 81 | o = ulong 0 |
| 82 | d = ulong 0 |
| 83 | dots = ulong 0 |
| 84 | i = ulong 0 |
| 85 | while lt i n bound 15 |
| 86 | c = ulong get cl_arg i |
| 87 | if eq c 46 |
| 88 | if or eq d 0 or gt o 255 ge dots 3 |
| 89 | exit 2 |
| 90 | end |
| 91 | v = add shl v 8 o |
| 92 | o = ulong 0 |
| 93 | d = ulong 0 |
| 94 | dots = add dots 1 |
| 95 | else |
| 96 | if or lt c 48 gt c 57 |
| 97 | exit 2 |
| 98 | end |
| 99 | if ge d 3 |
| 100 | exit 2 |
| 101 | end |
| 102 | o = add mul o 10 sub c 48 |
| 103 | d = add d 1 |
| 104 | end |
| 105 | i = add i 1 |
| 106 | end |
| 107 | if or ne dots 3 or eq d 0 gt o 255 |
| 108 | exit 2 |
| 109 | end |
| 110 | ret add shl v 8 o |
| 111 | end |
| 112 | |
| 113 | note Dial IPv4:port via AF_INET SOCK_STREAM. Returns fd >= 0 or < 0. |
| 114 | fn cl_dial ip ulong port ulong to long |
| 115 | fd = socknew 2 1 0 |
| 116 | if lt fd 0 |
| 117 | ret fd |
| 118 | end |
| 119 | for i ulong 16 |
| 120 | put cl_sa i 0 |
| 121 | end |
| 122 | put cl_sa 0 2 |
| 123 | put cl_sa 1 0 |
| 124 | put cl_sa 2 uchar and shr port 8 255 |
| 125 | put cl_sa 3 uchar and port 255 |
| 126 | put cl_sa 4 uchar and shr ip 24 255 |
| 127 | put cl_sa 5 uchar and shr ip 16 255 |
| 128 | put cl_sa 6 uchar and shr ip 8 255 |
| 129 | put cl_sa 7 uchar and ip 255 |
| 130 | rc = connect int fd cl_sa 0 16 |
| 131 | if lt rc 0 |
| 132 | c = fclose int fd |
| 133 | ret rc |
| 134 | end |
| 135 | ret fd |
| 136 | end |
| 137 | |
| 138 | note Send omktx[0..n) completely over fd. |
| 139 | fn cl_send_all fd int n ulong to long |
| 140 | off = ulong 0 |
| 141 | while lt off n bound 16400 |
| 142 | k = send fd omktx off sub n off 0 |
| 143 | if le k 0 |
| 144 | ret -1 |
| 145 | end |
| 146 | off = add off ulong k |
| 147 | end |
| 148 | ret 0 |
| 149 | end |
| 150 | |
| 151 | note Receive exactly n bytes into cl_rx[off..off+n). |
| 152 | fn cl_recv_exact fd int off ulong n ulong to long |
| 153 | got = ulong 0 |
| 154 | while lt got n bound 16400 |
| 155 | k = recv fd cl_rx add off got sub n got 0 |
| 156 | if le k 0 |
| 157 | ret -1 |
| 158 | end |
| 159 | got = add got ulong k |
| 160 | end |
| 161 | ret 0 |
| 162 | end |
| 163 | |
| 164 | fn cl_rx_r16 off ulong to ulong |
| 165 | a = ulong get cl_rx off |
| 166 | b = ulong get cl_rx add off 1 |
| 167 | ret or a shl b 8 |
| 168 | end |
| 169 | |
| 170 | fn cl_rx_r32 off ulong to ulong |
| 171 | a = ulong get cl_rx off |
| 172 | b = ulong get cl_rx add off 1 |
| 173 | c = ulong get cl_rx add off 2 |
| 174 | d = ulong get cl_rx add off 3 |
| 175 | ret or or or a shl b 8 shl c 16 shl d 24 |
| 176 | end |
| 177 | |
| 178 | note Receive and validate one OMTK v1 frame into cl_rx; header fields placed in omkhdr[0..5]. |
| 179 | fn cl_recv_frame fd int to long |
| 180 | rc = cl_recv_exact fd 0 OMK_HDR |
| 181 | if lt rc 0 |
| 182 | ret -1 |
| 183 | end |
| 184 | put omkhdr 0 cl_rx_r32 0 |
| 185 | put omkhdr 1 ulong get cl_rx 4 |
| 186 | put omkhdr 2 ulong get cl_rx 5 |
| 187 | put omkhdr 3 cl_rx_r16 6 |
| 188 | put omkhdr 4 cl_rx_r32 8 |
| 189 | put omkhdr 5 cl_rx_r32 12 |
| 190 | vrc = omk_chk_hdr |
| 191 | if ne vrc OMK_OK |
| 192 | ret -2 |
| 193 | end |
| 194 | plen = get omkhdr 5 |
| 195 | if gt plen 0 |
| 196 | rc = cl_recv_exact fd OMK_HDR plen |
| 197 | if lt rc 0 |
| 198 | ret -1 |
| 199 | end |
| 200 | else |
| 201 | for z ulong 16 |
| 202 | put cl_rx add OMK_HDR z 0 |
| 203 | end |
| 204 | end |
| 205 | ret 0 |
| 206 | end |
| 207 | |
| 208 | fn cl_print_err to void |
| 209 | k = write 1 Merr 0 len Merr |
| 210 | cl_put_u cl_rx_r32 16 |
| 211 | k = write 1 Mdet 0 len Mdet |
| 212 | cl_put_u cl_rx_r32 20 |
| 213 | k = write 1 Mnl 0 len Mnl |
| 214 | end |
| 215 | |
| 216 | fn cl_print_done to void |
| 217 | k = write 1 Mdone 0 len Mdone |
| 218 | cl_put_u cl_rx_r32 16 |
| 219 | k = write 1 Mreas 0 len Mreas |
| 220 | cl_put_u cl_rx_r32 20 |
| 221 | k = write 1 Mnl 0 len Mnl |
| 222 | end |
| 223 | |
| 224 | fn cl_cmd_ping fd int to int |
| 225 | rc = omk_mkhdr OMK_PING 0 0 0 |
| 226 | if ne rc OMK_OK |
| 227 | ret 2 |
| 228 | end |
| 229 | if lt cl_send_all fd get omktxn 0 0 |
| 230 | ret 1 |
| 231 | end |
| 232 | if lt cl_recv_frame fd 0 |
| 233 | ret 1 |
| 234 | end |
| 235 | op = get omkhdr 2 |
| 236 | if eq op OMK_ERR |
| 237 | cl_print_err |
| 238 | ret 2 |
| 239 | end |
| 240 | if ne op OMK_PONG |
| 241 | ret 2 |
| 242 | end |
| 243 | k = write 1 Mpong 0 len Mpong |
| 244 | cl_put_u cl_rx_r32 16 |
| 245 | k = write 1 Mkv 0 len Mkv |
| 246 | cl_put_u cl_rx_r32 20 |
| 247 | k = write 1 Mmctx 0 len Mmctx |
| 248 | cl_put_u cl_rx_r32 24 |
| 249 | k = write 1 Mgerr 0 len Mgerr |
| 250 | cl_put_u cl_rx_r32 28 |
| 251 | k = write 1 Mnl 0 len Mnl |
| 252 | ret 0 |
| 253 | end |
| 254 | |
| 255 | fn cl_cmd_reset fd int to int |
| 256 | if ne argcnt 5 |
| 257 | ret 2 |
| 258 | end |
| 259 | keep = cl_parse_u cl_arg_len 4 |
| 260 | rc = omk_mkhdr OMK_RESET 0 0 4 |
| 261 | if ne rc OMK_OK |
| 262 | ret 2 |
| 263 | end |
| 264 | omk_txw32 16 keep |
| 265 | if lt cl_send_all fd get omktxn 0 0 |
| 266 | ret 1 |
| 267 | end |
| 268 | if lt cl_recv_frame fd 0 |
| 269 | ret 1 |
| 270 | end |
| 271 | op = get omkhdr 2 |
| 272 | if eq op OMK_ERR |
| 273 | cl_print_err |
| 274 | ret 2 |
| 275 | end |
| 276 | if ne op OMK_DONE |
| 277 | ret 2 |
| 278 | end |
| 279 | cl_print_done |
| 280 | ret 0 |
| 281 | end |
| 282 | |
| 283 | fn cl_cmd_infer fd int to int |
| 284 | ac = ulong argcnt |
| 285 | if lt ac 7 |
| 286 | ret 2 |
| 287 | end |
| 288 | mtok = cl_parse_u cl_arg_len 4 |
| 289 | eos = cl_parse_u cl_arg_len 5 |
| 290 | ntok = sub ac 6 |
| 291 | if gt ntok OMK_MAXTOK |
| 292 | ret 2 |
| 293 | end |
| 294 | rc = omk_mkhdr OMK_PREFILL 0 1 mul ntok 4 |
| 295 | if ne rc OMK_OK |
| 296 | ret 2 |
| 297 | end |
| 298 | i = ulong 0 |
| 299 | while lt i ntok bound 4096 |
| 300 | tok = cl_parse_u cl_arg_len add 6 i |
| 301 | omk_txw32 add OMK_HDR mul i 4 tok |
| 302 | i = add i 1 |
| 303 | end |
| 304 | if lt cl_send_all fd get omktxn 0 0 |
| 305 | ret 1 |
| 306 | end |
| 307 | if lt cl_recv_frame fd 0 |
| 308 | ret 1 |
| 309 | end |
| 310 | op = get omkhdr 2 |
| 311 | if eq op OMK_ERR |
| 312 | cl_print_err |
| 313 | ret 2 |
| 314 | end |
| 315 | if ne op OMK_DONE |
| 316 | ret 2 |
| 317 | end |
| 318 | rc = omk_mkhdr OMK_DECODE OMK_FGREEDY 2 16 |
| 319 | if ne rc OMK_OK |
| 320 | ret 2 |
| 321 | end |
| 322 | omk_txw32 16 mtok |
| 323 | omk_txw32 20 eos |
| 324 | omk_txw32 24 0 |
| 325 | omk_txw32 28 0 |
| 326 | if lt cl_send_all fd get omktxn 0 0 |
| 327 | ret 1 |
| 328 | end |
| 329 | step = ulong 0 |
| 330 | while lt step 16385 bound 16385 |
| 331 | if lt cl_recv_frame fd 0 |
| 332 | ret 1 |
| 333 | end |
| 334 | op = get omkhdr 2 |
| 335 | if eq op OMK_TOKEN |
| 336 | k = write 1 Mtok 0 len Mtok |
| 337 | cl_put_u cl_rx_r32 16 |
| 338 | k = write 1 Msp 0 len Msp |
| 339 | cl_put_u cl_rx_r32 20 |
| 340 | k = write 1 Mnl 0 len Mnl |
| 341 | else |
| 342 | if eq op OMK_DONE |
| 343 | cl_print_done |
| 344 | ret 0 |
| 345 | else |
| 346 | if eq op OMK_ERR |
| 347 | cl_print_err |
| 348 | ret 2 |
| 349 | else |
| 350 | ret 2 |
| 351 | end |
| 352 | end |
| 353 | end |
| 354 | step = add step 1 |
| 355 | end |
| 356 | ret 2 |
| 357 | end |
| 358 | |
| 359 | fn cl_eq_str n ulong pat view uchar * to bool |
| 360 | if ne n len pat |
| 361 | ret false |
| 362 | end |
| 363 | i = ulong 0 |
| 364 | while lt i n bound 16 |
| 365 | if ne get cl_arg i get pat i |
| 366 | ret false |
| 367 | end |
| 368 | i = add i 1 |
| 369 | end |
| 370 | ret true |
| 371 | end |
| 372 | |
| 373 | fn main to int |
| 374 | if lt argcnt 4 |
| 375 | ret 2 |
| 376 | end |
| 377 | ip = cl_parse_ip cl_arg_len 1 |
| 378 | port = cl_parse_u cl_arg_len 2 |
| 379 | if or eq port 0 gt port 65535 |
| 380 | ret 2 |
| 381 | end |
| 382 | cn = cl_arg_len 3 |
| 383 | if eq cn 0 |
| 384 | ret 2 |
| 385 | end |
| 386 | kind = ulong 0 |
| 387 | if cl_eq_str cn Cping |
| 388 | kind = 1 |
| 389 | else |
| 390 | if cl_eq_str cn Creset |
| 391 | kind = 2 |
| 392 | else |
| 393 | if cl_eq_str cn Cinfer |
| 394 | kind = 3 |
| 395 | else |
| 396 | ret 2 |
| 397 | end |
| 398 | end |
| 399 | end |
| 400 | fd = cl_dial ip port |
| 401 | if lt fd 0 |
| 402 | ret 1 |
| 403 | end |
| 404 | res = int 2 |
| 405 | if eq kind 1 |
| 406 | res = cl_cmd_ping int fd |
| 407 | else |
| 408 | if eq kind 2 |
| 409 | res = cl_cmd_reset int fd |
| 410 | else |
| 411 | if eq kind 3 |
| 412 | res = cl_cmd_infer int fd |
| 413 | end |
| 414 | end |
| 415 | end |
| 416 | c = fclose int fd |
| 417 | ret res |
| 418 | end |