interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tools / pi-drive-27b / pi_drive_omtk.om

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