interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / public / alpha / wizard.om

4894 lines · 84223 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note sha256.om: vendored from aien-dev/omega-mv 3364a1dfd0ca50c1b82bf19ba34932d8bc10a7a9 om/sha256/sha256-om2.om
3note (source sha256 dbf002dd6aa36585632b9d01d98ac3f0bd2fa0f0475082940de53638847994bb). Derived mechanically: every "export fn" became "fn", because a
4note program-mode unit (one with fn main) refuses export (OMEGESE 9.13, error 9). build.sh re-derives
5note this file from the pinned source and refuses any other difference.
6note SPDX-License-Identifier: Apache-2.0
7note sha256-om2.om: FIPS 180-4 SHA-256 in OM-2 (spec/omegese-om2.md, #747/#748, ledger L838).
8note The four entries are the API of src/sha256.h, exported through omega-bridge ABI v2 as
9note omega2_sha256_init/update/final/hash; the bridge's generated omega_api.h maps the C names.
10note
11note Invariants:
12note - All hashing state is the caller's 104-byte context (layout shactx below, equal to sha256_ctx
13note on LP64 AArch64). Nothing else persists between calls, so a byte copy of a live context is an
14note equivalent context (erratum E3).
15note - count is the number of bytes absorbed since init, modulo 2^64. buf[0 .. count mod 64) holds the
16note pending bytes of the current block; the rest of buf is unspecified. state is the chaining value
17note after the count div 64 whole blocks.
18note - final reads the context and writes only the digest, so the context is unchanged by it. The
19note contract promises less (after final the context is unspecified, memory safety only).
20note - Every branch and every loop count depends only on lengths, never on message bytes (section 8).
21note - Indices into the unsized input view are checked at run time (trap 3); indices into sized
22note arrays inside unrolled bodies are constants and checked when compiled.
23note bridge c-type shactx sha256_ctx
24note bridge c-define SHA256_DIGEST_SIZE 32
25note bridge c-define SHA256_BLOCK_SIZE 64
26om2
27
28layout shactx 104
29field state uint 8 at 0 little
30field count ulong at 32 little
31field buf uchar 64 at 40 little
32end
33
34note K[0..63]: the first 32 bits of the fractional parts of the cube roots of the first 64 primes.
35note Copied mechanically from seed/qualification/om2/fixtures/sha256-k.dec (tool-computed, never typed).
36fn kinit k edit uint 64 to void
37put k 0 0x428a2f98
38put k 1 0x71374491
39put k 2 0xb5c0fbcf
40put k 3 0xe9b5dba5
41put k 4 0x3956c25b
42put k 5 0x59f111f1
43put k 6 0x923f82a4
44put k 7 0xab1c5ed5
45put k 8 0xd807aa98
46put k 9 0x12835b01
47put k 10 0x243185be
48put k 11 0x550c7dc3
49put k 12 0x72be5d74
50put k 13 0x80deb1fe
51put k 14 0x9bdc06a7
52put k 15 0xc19bf174
53put k 16 0xe49b69c1
54put k 17 0xefbe4786
55put k 18 0x0fc19dc6
56put k 19 0x240ca1cc
57put k 20 0x2de92c6f
58put k 21 0x4a7484aa
59put k 22 0x5cb0a9dc
60put k 23 0x76f988da
61put k 24 0x983e5152
62put k 25 0xa831c66d
63put k 26 0xb00327c8
64put k 27 0xbf597fc7
65put k 28 0xc6e00bf3
66put k 29 0xd5a79147
67put k 30 0x06ca6351
68put k 31 0x14292967
69put k 32 0x27b70a85
70put k 33 0x2e1b2138
71put k 34 0x4d2c6dfc
72put k 35 0x53380d13
73put k 36 0x650a7354
74put k 37 0x766a0abb
75put k 38 0x81c2c92e
76put k 39 0x92722c85
77put k 40 0xa2bfe8a1
78put k 41 0xa81a664b
79put k 42 0xc24b8b70
80put k 43 0xc76c51a3
81put k 44 0xd192e819
82put k 45 0xd6990624
83put k 46 0xf40e3585
84put k 47 0x106aa070
85put k 48 0x19a4c116
86put k 49 0x1e376c08
87put k 50 0x2748774c
88put k 51 0x34b0bcb5
89put k 52 0x391c0cb3
90put k 53 0x4ed8aa4a
91put k 54 0x5b9cca4f
92put k 55 0x682e6ff3
93put k 56 0x748f82ee
94put k 57 0x78a5636f
95put k 58 0x84c87814
96put k 59 0x8cc70208
97put k 60 0x90befffa
98put k 61 0xa4506ceb
99put k 62 0xbef9a3f7
100put k 63 0xc67178f2
101end
102
103note H(0): the first 32 bits of the fractional parts of the square roots of the first 8 primes
104note (fixtures/sha256-iv.dec).
105fn iv s edit uint 8 to void
106put s 0 0x6a09e667
107put s 1 0xbb67ae85
108put s 2 0x3c6ef372
109put s 3 0xa54ff53a
110put s 4 0x510e527f
111put s 5 0x9b05688c
112put s 6 0x1f83d9ab
113put s 7 0x5be0cd19
114end
115
116note Message schedule of the block m[o .. o+64). w is fresh: its incoming contents are never read.
117note The compiler proves that every get of w reads an element written earlier in this function
118note (error 27 otherwise): w[0..16) are the block's big-endian words, w[16..64) the FIPS recurrence.
119fn sched w edit uint 64 fresh m view uchar * o ulong to void
120q = ulong 0
121x = uint 0
122y = uint 0
123for i ulong 16 unroll
124q = add o mul i 4
125x = uint get m q
126x = or shl x 8 uint get m add q 1
127x = or shl x 8 uint get m add q 2
128put w i or shl x 8 uint get m add q 3
129end
130for i ulong 48 unroll
131x = get w add i 1
132y = get w add i 14
133x = add add get w i get w add i 9 xor xor rotr x 7 rotr x 18 shr x 3
134put w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10
135end
136end
137
138note The 64 rounds on the chaining value s. One round body, emitted 64 times by unroll; i is a constant
139note in each copy, so get kt i and get w i need no run-time check. uint arithmetic wraps mod 2^32.
140note Ch(e,f,g) = (e and f) xor (not e and g) is written g xor (e and (f xor g)); Maj(a,b,c) =
141note (a and b) xor (a and c) xor (b and c) is written (a and b) or (c and (a or b)). Both are bitwise
142note identities. h first becomes T1, then T1 + T2, which is the next a; the names then shift by one.
143note The sums are built a term per line and each Sigma in two lines through t: 12 names are live, and
144note omc hands out 16 registers with no spilling (error 12 under compiler-s1/s2 for longer lines).
145fn rounds s edit uint 8 w view uint 64 kt view uint 64 to void
146a = get s 0
147b = get s 1
148c = get s 2
149d = get s 3
150e = get s 4
151f = get s 5
152g = get s 6
153h = get s 7
154t = uint 0
155for i ulong 64 unroll
156h = add h get kt i
157h = add h get w i
158t = xor rotr e 6 rotr e 11
159t = xor t rotr e 25
160h = add h t
161h = add h xor g and e xor f g
162d = add d h
163t = xor rotr a 2 rotr a 13
164t = xor t rotr a 22
165h = add h t
166t = and c or a b
167h = add h or and a b t
168t = h
169h = g
170g = f
171f = e
172e = d
173d = c
174c = b
175b = a
176a = t
177end
178put s 0 add get s 0 a
179put s 1 add get s 1 b
180put s 2 add get s 2 c
181put s 3 add get s 3 d
182put s 4 add get s 4 e
183put s 5 add get s 5 f
184put s 6 add get s 6 g
185put s 7 add get s 7 h
186end
187
188note One block m[o .. o+64) into s. sched and rounds stay calls: inlined, their names would share
189note one 16-register frame with this function's parameters (no spilling, error 12).
190fn compress s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * o ulong to void
191sched w m o
192rounds s w kt
193end
194
195note The whole blocks of m (len m is a multiple of 64 and at most 2^25, so under 2^19 passes).
196fn run s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * to void
197for j ulong shr len m 6
198compress s w kt m shl j 6
199end
200end
201
202note The whole blocks of m (len m a multiple of 64). A rolled loop traps 4 after 1000000 passes
203note (OMEGESE.md section 2; omc s_for), so m is cut into runs of 2^25 bytes (2^19 blocks):
204note len m = top * 2^44 + mid * 2^25 + rest, and no loop passes 2^20 times. top reaches 1000000
205note only above 1.7e19 bytes, which no AArch64 address space holds.
206fn blocks s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * to void
207q = ulong 0
208for a ulong shr len m 44
209for b ulong 0x80000
210run s w kt part m q 0x2000000
211q = add q 0x2000000
212end
213end
214for b ulong and shr len m 25 0x7ffff
215run s w kt part m q 0x2000000
216q = add q 0x2000000
217end
218run s w kt part m q sub len m q
219end
220
221note Pad the k pending bytes already in p (k < 64; p was all zero past them): 0x80, zeros, and the
222note 64-bit big-endian bit length of the n-byte message in the last 8 bytes of one or two blocks.
223fn finish s edit uint 8 w edit uint 64 kt view uint 64 p edit uchar 128 k ulong n ulong to void
224put p k 0x80
225e = ulong 64
226if ge k 56
227e = 128
228end
229bits = shl n 3
230for i ulong 8 unroll
231put p sub e add i 1 uchar and shr bits mul i 8 0xff
232end
233compress s w kt view p 0
234if eq e 128
235compress s w kt view p 64
236end
237end
238
239fn sha256_init c edit shactx to void
240s = new uint 8 0
241iv s
242for i ulong 8 unroll
243put c state i get s i
244end
245put c count 0
246end
247
248note k bytes of d from o into the context's buffer at bi (bi + k <= 64). A function of its own: inside
249note absorb the copy loop needs more than the 16 registers (error 12 under compiler-s4).
250fn tobuf c edit shactx bi ulong d view uchar * o ulong k ulong to void
251for i ulong k
252put c buf add bi i get d add o i
253end
254end
255
256note Whole blocks are hashed straight from d; the bytes that complete a pending block go through blk.
257fn absorb c edit shactx d view uchar * bi ulong to void
258n = len d
259s = new uint 8 0
260w = new uint 64 0
261kt = new uint 64 0
262blk = new uchar 64 0
263kinit kt
264for i ulong 8 unroll
265put s i get c state i
266end
267off = ulong 0
268if gt bi 0
269off = sub 64 bi
270for i ulong bi
271put blk i get c buf i
272end
273for i ulong off
274put blk add bi i get d i
275end
276compress s w kt view blk 0
277end
278nb = shr sub n off 6
279blocks s w kt part d off shl nb 6
280off = add off shl nb 6
281tobuf c 0 d off sub n off
282for i ulong 8 unroll
283put c state i get s i
284end
285end
286
287note Any length, including 0 and >= 2^32. lt n sub 64 bi cannot wrap (bi < 64); add bi n could.
288fn sha256_update c edit shactx d view uchar * to void
289n = len d
290cnt = get c count
291bi = and cnt 63
292put c count add cnt n
293if lt n sub 64 bi
294tobuf c bi d 0 n
295ret
296end
297absorb c d bi
298end
299
300fn sha256_final c edit shactx o out uchar 32 to void
301s = new uint 8 0
302w = new uint 64 0
303kt = new uint 64 0
304p = new uchar 128 0
305v = ulong 0
306kinit kt
307for i ulong 8 unroll
308put s i get c state i
309end
310cnt = get c count
311k = and cnt 63
312for i ulong k
313put p i get c buf i
314end
315finish s w kt p k cnt
316for i ulong 8 unroll
317v = ulong get s i
318for j ulong 4 unroll
319put o add mul i 4 j uchar and shr v sub 24 mul j 8 0xff
320end
321end
322end
323
324note Equal to init, update(d), final on a fresh context (section 7), with the state in locals. o may
325note overlap d: the binding copies o out only after the call returns status 0.
326fn sha256_hash d view uchar * o out uchar 32 to void
327s = new uint 8 0
328w = new uint 64 0
329kt = new uint 64 0
330p = new uchar 128 0
331v = ulong 0
332kinit kt
333iv s
334n = len d
335nb = shr n 6
336blocks s w kt part d 0 shl nb 6
337off = shl nb 6
338k = sub n off
339for i ulong k
340put p i get d add off i
341end
342finish s w kt p k n
343for i ulong 8 unroll
344v = ulong get s i
345for j ulong 4 unroll
346put o add mul i 4 j uchar and shr v sub 24 mul j 8 0xff
347end
348end
349end
350note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
351note lib.om: the output buffer, numbers, ANSI escapes, paths, files, environment and child
352note processes shared by every screen of the AienOS wizard. Linux arm64 only; every kernel result is
353note a count or minus the Linux errno and is checked where it is used.
354
355global ob uchar 262144
356global obn ulong 1
357global pb uchar 4096
358global pbn ulong 1
359global rb uchar 524288
360global rbn ulong 1
361global hb uchar 1024
362global hbn ulong 1
363global rt uchar 1024
364global rtn ulong 1
365global pr uchar 512
366global prn ulong 1
367global ib uchar 4096
368global ibn ulong 1
369global eb uchar 4096
370global pk uchar 4096
371global pkn ulong 1
372global sb uchar 128
373global ws uchar 8
374global sn int 64
375text tslash "/"
376text t_yes "yes"
377text tusrbin "/usr/bin/"
378text tbin "/bin/"
379
380note Append t to the output buffer. A frame never comes near the 256 KiB cap; past it the
381note bytes are dropped rather than trapping, and the next flush starts clean.
382fn emit t view uchar * to void
383n = get obn 0
384if gt add n len t len ob
385ret
386end
387for i ulong len t
388put ob add n i get t i
389end
390put obn 0 add n len t
391end
392
393fn putb b uchar to void
394n = get obn 0
395if lt n len ob
396put ob n b
397put obn 0 add n 1
398end
399end
400
401note Decimal digits of v, most significant first.
402fn num v ulong to void
403d = new uchar 20 0
404k = ulong 0
405x = v
406while true bound 21
407put d k uchar add rem x 10 48
408k = add k 1
409x = div x 10
410if eq x 0
411break
412end
413end
414for i ulong k
415putb get d sub sub k 1 i
416end
417end
418
419fn snum v long to void
420if lt v 0
421putb 45
422num ulong neg v
423ret
424end
425num ulong v
426end
427
428fn numw v ulong w ulong to void
429x = v
430k = ulong 1
431while ge x 10 bound 21
432x = div x 10
433k = add k 1
434end
435while lt k w bound 64
436putb 32
437k = add k 1
438end
439num v
440end
441
442note Write the whole buffer to fd 1 with a partial-write loop, then empty it.
443fn flush to long
444n = get obn 0
445o = ulong 0
446while lt o n bound 262145
447w = write 1 ob o sub n o
448if le w 0
449put obn 0 0
450ret w
451end
452o = add o ulong w
453end
454put obn 0 0
455ret long n
456end
457
458fn csi to void
459putb 27
460putb 91
461end
462
463fn mv r ulong c ulong to void
464csi
465num r
466putb 59
467num c
468putb 72
469end
470
471fn fg r ulong g ulong b ulong to void
472csi
473putb 51
474putb 56
475putb 59
476putb 50
477putb 59
478num r
479putb 59
480num g
481putb 59
482num b
483putb 109
484end
485
486fn bg r ulong g ulong b ulong to void
487csi
488putb 52
489putb 56
490putb 59
491putb 50
492putb 59
493num r
494putb 59
495num g
496putb 59
497num b
498putb 109
499end
500
501fn sgr0 to void
502csi
503putb 48
504putb 109
505end
506
507fn bold to void
508csi
509putb 49
510putb 109
511end
512
513note Erase from the cursor to the end of the line.
514fn eol to void
515csi
516putb 75
517end
518
519fn cls to void
520csi
521putb 50
522putb 74
523end
524
525note Cursor home with no parameters: every animation frame starts with it.
526fn home to void
527csi
528putb 72
529end
530
531note Alternate screen and hidden cursor on (true) or the user's screen and cursor back (false).
532fn scrn on bool to void
533csi
534putb 63
535num 1049
536if on
537putb 104
538csi
539putb 63
540num 25
541putb 108
542else
543putb 108
544csi
545putb 63
546num 25
547putb 104
548end
549end
550
551note The 64-entry sine table, sin(2 pi k / 64) scaled by 1000, computed with Wolfram:
552note Table[Round[1000*Sin[2 Pi k/64]], {k, 0, 63}]. Only the first quadrant is stored by hand;
553note the rest follows from sin(pi - x) = sin x and sin(x + pi) = -sin x.
554fn sinit to void
555q = new int 17 0
556put q 1 98
557put q 2 195
558put q 3 290
559put q 4 383
560put q 5 471
561put q 6 556
562put q 7 634
563put q 8 707
564put q 9 773
565put q 10 831
566put q 11 882
567put q 12 924
568put q 13 957
569put q 14 981
570put q 15 995
571put q 16 1000
572for k ulong 17
573put sn k get q k
574put sn sub 32 k get q k
575end
576for k ulong 32
577put sn add k 32 neg get sn k
578end
579end
580
581fn isin k ulong to long
582ret long get sn and k 63
583end
584
585fn icos k ulong to long
586ret long get sn and add k 16 63
587end
588
589note Paths: pb is built piece by piece and handed to the host words with its length pbn.
590fn pclr to void
591put pbn 0 0
592end
593
594fn pstr t view uchar * to void
595n = get pbn 0
596if gt add n len t len pb
597trap 3
598end
599for i ulong len t
600put pb add n i get t i
601end
602put pbn 0 add n len t
603end
604
605fn pbyte b uchar to void
606n = get pbn 0
607put pb n b
608put pbn 0 add n 1
609end
610
611fn pnum v ulong to void
612d = new uchar 20 0
613k = ulong 0
614x = v
615while true bound 21
616put d k uchar add rem x 10 48
617k = add k 1
618x = div x 10
619if eq x 0
620break
621end
622end
623for i ulong k
624pbyte get d sub sub k 1 i
625end
626end
627
628fn phome to void
629for i ulong get hbn 0
630pbyte get hb i
631end
632end
633
634fn proot to void
635for i ulong get rtn 0
636pbyte get rt i
637end
638end
639
640note A probe path: the test-only prefix AIENOS_PROBE_ROOT (empty in normal use) then t.
641fn pprobe t view uchar * to void
642pclr
643for i ulong get prn 0
644pbyte get pr i
645end
646pstr t
647end
648
649note Append rb[o .. o+n) to the path.
650fn prb o ulong n ulong to void
651for i ulong n
652pbyte get rb add o i
653end
654end
655
656fn pdrop k ulong to void
657put pbn 0 sub get pbn 0 k
658end
659
660note Stat pb into sb. Returns 0 or minus errno.
661fn pstat to long
662ret stat pb 0 get pbn 0 sb 0 128
663end
664
665note File type bits of the last stat: 4 directory, 2 character device, 8 regular, 10 link.
666fn stype to ulong
667m = or or uint get sb 16 shl uint get sb 17 8 shl uint get sb 18 16
668ret ulong shr and m 61440 12
669end
670
671fn ssize to ulong
672x = ulong 0
673for i ulong 8
674x = or x shl ulong get sb add 48 i mul i 8
675end
676ret x
677end
678
679fn sino to ulong
680x = ulong 0
681for i ulong 8
682x = or x shl ulong get sb add 8 i mul i 8
683end
684ret x
685end
686
687fn pexist to bool
688ret eq pstat 0
689end
690
691note Read the whole file named by pb into rb (at most len rb bytes). Returns the count or -errno.
692fn slurp to long
693put rbn 0 0
694fd = fopen pb 0 get pbn 0 524288 0
695if lt fd 0
696ret fd
697end
698n = ulong 0
699while lt n len rb bound 524289
700k = read int fd rb n sub len rb n
701if lt k 0
702c = fclose int fd
703ret k
704end
705if eq k 0
706break
707end
708n = add n ulong k
709end
710c = fclose int fd
711put rbn 0 n
712ret long n
713end
714
715note Write rb[0 .. n) to the file named by pb, created or truncated, mode 0644.
716fn spill n ulong to long
717fd = fopen pb 0 get pbn 0 525889 420
718if lt fd 0
719ret fd
720end
721o = ulong 0
722while lt o n bound 524289
723k = write int fd rb o sub n o
724if le k 0
725c = fclose int fd
726ret -5
727end
728o = add o ulong k
729end
730c = fclose int fd
731ret 0
732end
733
734note Does rb start, at offset o, with the bytes of t?
735fn rbat o ulong t view uchar * to bool
736if gt add o len t get rbn 0
737ret false
738end
739for i ulong len t
740if ne get rb add o i get t i
741ret false
742end
743end
744ret true
745end
746
747note First offset at or after o where t occurs in rb, or the length of rb's content when absent.
748fn rbfind o ulong t view uchar * to ulong
749n = get rbn 0
750i = o
751while lt i n bound 524289
752if rbat i t
753ret i
754end
755i = add i 1
756end
757ret n
758end
759
760note Does t occur inside rb[o .. e)?
761fn rbin o ulong e ulong t view uchar * to bool
762if lt e len t
763ret false
764end
765i = o
766while le add i len t e bound 524289
767if rbat i t
768ret true
769end
770i = add i 1
771end
772ret false
773end
774
775note Decimal number in rb at o; stops at the first non-digit.
776fn rbnum o ulong to ulong
777x = ulong 0
778i = o
779while lt i get rbn 0 bound 21
780c = get rb i
781if or lt c 48 gt c 57
782break
783end
784x = add mul x 10 ulong sub c 48
785i = add i 1
786end
787ret x
788end
789
790note Copy the environment entry KEY=value whose key is k into eb; returns the value length, or
791note -1 when the key is absent. The value starts at eb[len k + 1].
792fn envget k view uchar * to long
793c = envcnt
794for i ulong ulong c
795n = envread i eb 0 len eb
796if gt n long len k
797m = true
798for j ulong len k
799if ne get eb j get k j
800m = false
801end
802end
803if and m eq get eb len k 61
804ret sub n add long len k 1
805end
806end
807end
808ret -1
809end
810
811note Read one line from fd 0 into ib (without its newline). Returns its length, or -1 at end of input.
812fn rline to long
813n = ulong 0
814put ibn 0 0
815while lt n sub len ib 1 bound 4096
816k = read 0 ib n 1
817if le k 0
818if eq n 0
819ret -1
820end
821break
822end
823if eq get ib n 10
824break
825end
826n = add n 1
827end
828put ibn 0 n
829ret long n
830end
831
832fn ibis t view uchar * to bool
833if ne get ibn 0 len t
834ret false
835end
836for i ulong len t
837if ne get ib i get t i
838ret false
839end
840end
841ret true
842end
843
844note Packed argument lists for execprog: a little-endian u16 count, then u16 length + bytes records.
845fn pkclr to void
846put pk 0 0
847put pk 1 0
848put pkn 0 2
849end
850
851fn pkrec n ulong to ulong
852c = add or uint get pk 0 shl uint get pk 1 8 1
853put pk 0 uchar and c 255
854put pk 1 uchar shr c 8
855o = get pkn 0
856put pk o uchar and n 255
857put pk add o 1 uchar shr n 8
858put pkn 0 add add o 2 n
859ret add o 2
860end
861
862fn pkstr t view uchar * to void
863o = pkrec len t
864for i ulong len t
865put pk add o i get t i
866end
867end
868
869note The current path pb as one argument.
870fn pkpath to void
871o = pkrec get pbn 0
872for i ulong get pbn 0
873put pk add o i get pb i
874end
875end
876
877fn pknum v ulong to void
878pclr
879pnum v
880pkpath
881end
882
883note Exit status from the 4-byte wait status in ws: the exit code, or 128 + signal.
884fn wcode to long
885s = uint get ws 0
886if ne and s 127 0
887ret long add and s 127 128
888end
889ret long uint get ws 1
890end
891
892note Run the packed program in pk with fd 1 and 2 sent to the file named by pb (appended) or, when
893note pb is empty, inherited. Returns the exit status or a negative errno from fork.
894fn runpk to long
895lfd = long -1
896if gt get pbn 0 0
897lfd = fopen pb 0 get pbn 0 525377 420
898if lt lfd 0
899ret lfd
900end
901end
902pid = fork
903if lt pid 0
904ret pid
905end
906if eq pid 0
907if ge lfd 0
908d = dupfd int lfd 1 0
909d = dupfd int lfd 2 0
910end
911r = execprog pk 0 get pkn 0
912exit 127
913end
914if ge lfd 0
915c = fclose int lfd
916end
917r = waitpid int pid ws 0 4 0
918if ne r pid
919ret -10
920end
921ret wcode
922end
923
924note Run pk with its stdout captured into rb (at most len rb bytes); stdin and stderr are inherited.
925note Returns the exit status; rbn holds the captured length.
926fn runcap to long
927put rbn 0 0
928r = pipefds ws 0 8 524288
929if ne r 0
930ret r
931end
932rd = or or uint get ws 0 shl uint get ws 1 8 shl uint get ws 2 16
933wr = or or uint get ws 4 shl uint get ws 5 8 shl uint get ws 6 16
934pid = fork
935if lt pid 0
936ret pid
937end
938if eq pid 0
939d = dupfd int wr 1 0
940r = execprog pk 0 get pkn 0
941exit 127
942end
943c = fclose int wr
944n = ulong 0
945while lt n len rb bound 524289
946k = read int rd rb n sub len rb n
947if le k 0
948break
949end
950n = add n ulong k
951end
952c = fclose int rd
953put rbn 0 n
954r = waitpid int pid ws 0 4 0
955if ne r pid
956ret -10
957end
958ret wcode
959end
960
961note Find a host helper by name in /usr/bin then /bin; leaves its path in pb. False when absent.
962fn findbin t view uchar * to bool
963pclr
964pstr tusrbin
965pstr t
966if eq faccess pb 0 get pbn 0 1 0
967ret true
968end
969pclr
970pstr tbin
971pstr t
972ret eq faccess pb 0 get pbn 0 1 0
973end
974note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
975note hole.om: the animated black hole. Each terminal cell shows two square pixels with the upper
976note half block (U+2580): the foreground colour is the upper pixel, the background the lower one.
977note No floating point and no trigonometry beyond the 64-entry sine table: the angle around the
978note hole is the integer "diamond angle" (0..255 for one turn), and every radius is compared squared.
979
980global gm long 12
981global tw ulong 1
982global th ulong 1
983global tick ulong 1
984text lb0 "libcuda.so"
985text lb1 "cuBLAS"
986text lb2 "nvcc"
987text lb3 "CUDA toolkit"
988text lb4 "libcudart"
989text lb5 "cuDNN"
990text lb6 "NCCL"
991text lb7 "TensorRT"
992text tblk "â–€"
993
994note gm: 0 cx, 1 cy (pixels), 2 R, 3 horizon^2, 4 photon ring^2, 5 disk inner^2, 6 disk outer^2,
995note 7 glow^2, 8 scene rows, 9 horizon radius, 10 lensed arc^2.
996fn hgeom rows ulong to void
997cx = long div get tw 0 2
998cy = long rows
999r = cx
1000if lt cy r
1001r = cy
1002end
1003r = div mul r 95 100
1004rh = div mul r 44 100
1005put gm 0 cx
1006put gm 1 cy
1007put gm 2 r
1008put gm 3 mul rh rh
1009rp = add rh add div r 16 1
1010put gm 4 mul rp rp
1011di = div mul rh 135 100
1012put gm 5 mul di di
1013ro = div mul r 26 10
1014lim = div mul cx 92 100
1015if gt ro lim
1016ro = lim
1017end
1018put gm 6 mul ro ro
1019gl = div mul rp 160 100
1020put gm 7 mul gl gl
1021put gm 8 long rows
1022put gm 9 rh
1023lz = div mul rh 150 100
1024put gm 10 mul lz lz
1025end
1026
1027note Diamond angle of (x, y): 0..255 for one counter-clockwise turn, monotone in the true angle.
1028fn dang x long y long to ulong
1029ax = x
1030if lt ax 0
1031ax = neg ax
1032end
1033ay = y
1034if lt ay 0
1035ay = neg ay
1036end
1037s = add ax ay
1038if eq s 0
1039ret 0
1040end
1041if ge y 0
1042if ge x 0
1043ret ulong div mul 64 ay s
1044end
1045ret ulong add 64 div mul 64 ax s
1046end
1047if lt x 0
1048ret ulong add 128 div mul 64 ay s
1049end
1050ret ulong add 192 div mul 64 ax s
1051end
1052
1053fn pack r long g long b long to ulong
1054ret or or shl ulong r 16 shl ulong g 8 ulong b
1055end
1056
1057note Scale a packed colour by m/1000 and clamp each channel to 255.
1058fn scale c ulong m long to ulong
1059r = div mul long and shr c 16 255 m 1000
1060g = div mul long and shr c 8 255 m 1000
1061b = div mul long and c 255 m 1000
1062if gt r 255
1063r = 255
1064end
1065if gt g 255
1066g = 255
1067end
1068if gt b 255
1069b = 255
1070end
1071if lt r 0
1072r = 0
1073end
1074if lt g 0
1075g = 0
1076end
1077if lt b 0
1078b = 0
1079end
1080ret pack r g b
1081end
1082
1083note One channel of a mix: (x u + y t) / 256.
1084fn mixc x long y long u long t long to long
1085ret div add mul x u mul y t 256
1086end
1087
1088note Mix two packed colours: t/256 of b over a.
1089fn mix a ulong b ulong t long to ulong
1090u = sub 256 t
1091r = mixc long and shr a 16 255 long and shr b 16 255 u t
1092g = mixc long and shr a 8 255 long and shr b 8 255 u t
1093c = mixc long and a 255 long and b 255 u t
1094ret pack r g c
1095end
1096
1097note Accretion disk at (dx, dyd) with dd2 = dx^2 + dyd^2: hot white at the inner edge through orange
1098note to violet at the rim, three spiral arms that wind tighter inward, the approaching side brighter.
1099fn dcol t long to ulong
1100if ge t 128
1101ret mix pack 255 132 36 pack 110 28 120 mul sub t 128 2
1102end
1103ret mix pack 255 240 210 pack 255 132 36 mul t 2
1104end
1105
1106fn dlum dx long dyd long tw2 long to long
1107p = add div dang dx dyd 4 ulong tw2
1108p = add p mul get tick 0 2
1109ret add 640 div mul 360 isin mul p 3 1000
1110end
1111
1112fn diskc dx long dyd long dd2 long to ulong
1113in2 = get gm 5
1114t = div mul sub dd2 in2 256 add sub get gm 6 in2 1
1115c = dcol t
1116i = dlum dx dyd div mul in2 40 add dd2 1
1117dop = sub 1000 div mul dx 350 add get gm 2 1
1118ret scale c div mul i dop 1000
1119end
1120
1121fn shash x ulong y ulong to ulong
1122h = xor mul x 374761393 mul y 668265263
1123h = mul xor h shr h 13 1274126177
1124ret and shr xor h shr h 16 7 2047
1125end
1126
1127fn star x ulong y ulong to ulong
1128h = shash x y
1129if ge h 12
1130ret pack 3 3 10
1131end
1132v = long add 70 mul h 15
1133if eq and shr add get tick 0 mul h 5 3 1 1
1134v = div mul v 2 3
1135end
1136ret pack v v add v 25
1137end
1138
1139note The far side of the disk, bent over and under the shadow by the hole's gravity.
1140fn lensc dx long dy long d2 long to ulong
1141rp2 = get gm 4
1142t = div mul sub d2 rp2 256 add sub get gm 10 rp2 1
1143c = dcol t
1144ret scale c dlum dx mul dy 4 64
1145end
1146
1147note Faint orange halo just outside the photon ring, fading into the stars.
1148fn glow x ulong y ulong d2 long to ulong
1149g7 = get gm 7
1150t = div mul sub g7 d2 200 sub g7 get gm 4
1151ret mix star x y pack 160 60 20 t
1152end
1153
1154note Colour of pixel (x, y) of the scene.
1155fn px x ulong y ulong to ulong
1156dx = sub long x get gm 0
1157dy = sub long y get gm 1
1158d2 = add mul dx dx mul dy dy
1159dyd = mul dy 4
1160dd2 = add mul dx dx mul dyd dyd
1161indisk = and ge dd2 get gm 5 le dd2 get gm 6
1162if and indisk ge dy 0
1163ret diskc dx dyd dd2
1164end
1165if lt d2 get gm 3
1166ret 0
1167end
1168if lt d2 get gm 4
1169f = add 820 div mul 180 isin add div dang dx dy 4 get tick 0 1000
1170ret scale pack 255 226 170 f
1171end
1172if lt d2 get gm 10
1173ret lensc dx dy d2
1174end
1175if indisk
1176ret diskc dx dyd dd2
1177end
1178if lt d2 get gm 7
1179ret glow x y d2
1180end
1181ret star x y
1182end
1183
1184fn hpix c ulong top bool to void
1185r = and shr c 16 255
1186g = and shr c 8 255
1187b = and c 255
1188if top
1189fg r g b
1190else
1191bg r g b
1192end
1193end
1194
1195note Draw the scene rows 1..rows at full terminal width into the output buffer.
1196fn hdraw rows ulong to void
1197lf = ulong 16777216
1198lb = ulong 16777216
1199for row ulong rows
1200mv add row 1 1
1201lf = 16777216
1202lb = 16777216
1203for x ulong get tw 0
1204a = px x mul row 2
1205b = px x add mul row 2 1
1206if ne a lf
1207hpix a true
1208lf = a
1209end
1210if ne b lb
1211hpix b false
1212lb = b
1213end
1214emit tblk
1215end
1216sgr0
1217end
1218end
1219
1220fn lemit i ulong o ulong k ulong to void
1221if eq i 0
1222emit part lb0 o k
1223end
1224if eq i 1
1225emit part lb1 o k
1226end
1227if eq i 2
1228emit part lb2 o k
1229end
1230if eq i 3
1231emit part lb3 o k
1232end
1233if eq i 4
1234emit part lb4 o k
1235end
1236if eq i 5
1237emit part lb5 o k
1238end
1239if eq i 6
1240emit part lb6 o k
1241end
1242if eq i 7
1243emit part lb7 o k
1244end
1245end
1246
1247fn llen i ulong to ulong
1248if eq i 0
1249ret len lb0
1250end
1251if eq i 1
1252ret len lb1
1253end
1254if eq i 2
1255ret len lb2
1256end
1257if eq i 3
1258ret len lb3
1259end
1260if eq i 4
1261ret len lb4
1262end
1263if eq i 5
1264ret len lb5
1265end
1266if eq i 6
1267ret len lb6
1268end
1269ret len lb7
1270end
1271
1272note Label i of the NVIDIA user-space stack spirals inward along the disk plane. It starts green
1273note at the edge of the picture, turns red as it falls, loses letters at the horizon and is gone.
1274fn lfrm i ulong to ulong
1275ret rem add get tick 0 mul i 15 120
1276end
1277
1278fn lrad i ulong to long
1279f = lfrm i
1280ret div mul mul get gm 2 3 long sub 120 f 240
1281end
1282
1283fn lang i ulong to ulong
1284f = lfrm i
1285ret add add mul i 8 div mul f 3 2 div mul f f 90
1286end
1287
1288fn lpos i ulong to void
1289r = lrad i
1290a = lang i
1291x = add get gm 0 div mul r icos a 1000
1292y = add get gm 1 div mul mul r isin a 2 5000
1293rh = get gm 9
1294if or le r rh lt y 0
1295ret
1296end
1297lshow i r x y
1298end
1299
1300fn lshow i ulong r long x long y long to void
1301rh = get gm 9
1302n = llen i
1303k = n
1304if lt r mul rh 2
1305k = ulong div mul long n sub r rh rh
1306end
1307if eq k 0
1308ret
1309end
1310row = add ulong div y 2 1
1311if gt row ulong get gm 8
1312ret
1313end
1314col = sub x long div k 2
1315if or lt col 1 gt add ulong col k get tw 0
1316ret
1317end
1318lpaint i sub n k k lfade r row ulong col
1319end
1320
1321fn lfade r long to long
1322d = mul sub r get gm 9 256
1323t = div d add mul get gm 2 2 1
1324if gt t 256
1325ret 256
1326end
1327ret t
1328end
1329
1330fn lpaint i ulong d ulong k ulong t long row ulong col ulong to void
1331c = mix pack 255 70 30 pack 118 185 0 t
1332mv row col
1333fg and shr c 16 255 and shr c 8 255 and c 255
1334bg 0 0 0
1335bold
1336lemit i div d 2 k
1337sgr0
1338end
1339
1340fn hlabels to void
1341for i ulong 8
1342lpos i
1343end
1344end
1345
1346note One complete frame of the scene: picture, then labels over it.
1347fn hframe rows ulong to void
1348hgeom rows
1349hdraw rows
1350hlabels
1351put tick 0 add get tick 0 1
1352end
1353note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
1354note doctor.om: what this machine is, read only. Nothing here writes a file or loads a library.
1355note Every absolute path goes through pprobe, so a test can point the doctor at a fake tree with
1356note AIENOS_PROBE_ROOT; in normal use that prefix is empty.
1357
1358const NCHK ulong 8
1359global dk ulong 8
1360global dt uchar 1536
1361global dtn ulong 8
1362global dci ulong 1
1363global db uchar 32768
1364global dv ulong 8
1365text c_arch "/proc/sys/kernel/arch"
1366text c_a64 "aarch64"
1367text c_pci "/sys/bus/pci/devices"
1368text c_vend "/vendor"
1369text c_dev "/device"
1370text c_nvv "0x10de"
1371text c_gb10 "0x2e12"
1372text c_dctl "/dev/nvidiactl"
1373text c_dn0 "/dev/nvidia0"
1374text c_duvm "/dev/nvidia-uvm"
1375text c_pdrv "/proc/driver/nvidia/version"
1376text c_mver "/sys/module/nvidia/version"
1377text c_good "580.173.02"
1378text c_mem "/proc/meminfo"
1379text c_mtot "MemTotal:"
1380text c_mav "MemAvailable:"
1381text c_mmap "Mapped:"
1382text c_lock "/tmp/aien-gb10.lock"
1383text c_plk "/proc/locks"
1384text c_cuda "/usr/local/cuda"
1385text c_lcu "/usr/lib/aarch64-linux-gnu/libcuda.so.1"
1386text c_smi "/usr/bin/nvidia-smi"
1387text t_gb " GB"
1388text t_nl "\n"
1389text n_arch "CPU"
1390text n_gpu "GPU"
1391text n_drv "Driver"
1392text n_mem "Memory"
1393text n_disk "Disk"
1394text n_inst "AienOS"
1395text n_busy "GPU lock"
1396text n_nv "NVIDIA"
1397text d_a1 "arm64 (aarch64), Linux"
1398text d_a2 "not arm64: this alpha needs Linux arm64"
1399text d_g1 "NVIDIA GB10 found (PCI 10de:2e12), device nodes present"
1400text d_g2 "no NVIDIA GB10 found: this alpha runs only on a DGX Spark (GB10)"
1401text d_g3 "GB10 found but /dev/nvidiactl, /dev/nvidia0 or /dev/nvidia-uvm is missing"
1402text d_d1 "kernel driver "
1403text d_d2 " (the version this alpha was built and tested against)"
1404text d_d3 " found; this alpha only knows 580.173.02, so it stops here"
1405text d_d4 "no NVIDIA kernel module loaded (/sys/module/nvidia/version missing)"
1406text d_m1 " GB memory, "
1407text d_m2 " GB free for a model"
1408text d_m3 "; another model is using memory, so the first chat waits until it is free"
1409text d_m4 "; this alpha needs a 128 GB GB10"
1410text h_df "df"
1411text h_b1 "-B1"
1412text h_av "--output=avail"
1413text d_k1 " free in your home"
1414text d_k2 "; the model needs "
1415text d_k3 "unknown free space (df not found); the model needs "
1416text d_i1 "not installed yet"
1417text d_i2 "installed: "
1418text d_i3 ", model verified"
1419text d_i4 ", model not downloaded yet"
1420text t_cur "/.local/share/aienos/current"
1421text d_b1 "the GB10 lock (/tmp/aien-gb10.lock) is not held right now"
1422text d_b2 "busy: process "
1423text d_b3 " holds the GB10 lock, so I start no second model"
1424text d_n1 "found "
1425text d_n2 "libcuda"
1426text d_n3 "the CUDA toolkit"
1427text d_n4 "nvidia-smi"
1428text d_n5 ": they stay installed and untouched, and AienOS never loads them"
1429text d_nc ", "
1430text d_n6 "no NVIDIA user-space libraries found; AienOS does not need them"
1431text k_ok " ok "
1432text k_wn " note "
1433text k_no " no "
1434text k_in " info "
1435
1436note Detail line of check i, built with dstr/dnum/drb.
1437fn dsel i ulong to void
1438put dci 0 i
1439put dtn i 0
1440end
1441
1442fn dstr t view uchar * to void
1443i = get dci 0
1444n = get dtn i
1445for j ulong len t
1446if lt add n j 190
1447put dt add mul i 192 add n j get t j
1448end
1449end
1450m = add n len t
1451if gt m 190
1452m = 190
1453end
1454put dtn i m
1455end
1456
1457fn dbyte b uchar to void
1458i = get dci 0
1459n = get dtn i
1460if lt n 190
1461put dt add mul i 192 n b
1462put dtn i add n 1
1463end
1464end
1465
1466fn dnum v ulong to void
1467d = new uchar 20 0
1468k = ulong 0
1469x = v
1470while true bound 21
1471put d k uchar add rem x 10 48
1472k = add k 1
1473x = div x 10
1474if eq x 0
1475break
1476end
1477end
1478for j ulong k
1479dbyte get d sub sub k 1 j
1480end
1481end
1482
1483note Bytes of rb[o ..) up to the first newline or n bytes.
1484fn drb o ulong n ulong to void
1485for j ulong n
1486if ge add o j get rbn 0
1487ret
1488end
1489c = get rb add o j
1490if eq c 10
1491ret
1492end
1493dbyte c
1494end
1495end
1496
1497note Size in GB, rounded to one decimal, from a byte count (1 GB = 10^9 bytes).
1498fn dgb v ulong to void
1499r = add v 50000000
1500dnum div r 1000000000
1501dbyte 46
1502dnum div rem r 1000000000 100000000
1503dstr t_gb
1504end
1505
1506fn verdict i ulong k ulong to void
1507put dk i k
1508end
1509
1510note 0 CPU architecture. The binary itself is aarch64 and static, so running at all proves the ISA;
1511note the kernel's own word is shown when it has one.
1512fn ckarch to void
1513dsel 0
1514pprobe c_arch
1515n = slurp
1516if lt n 0
1517verdict 0 0
1518dstr d_a1
1519ret
1520end
1521if rbat 0 c_a64
1522verdict 0 0
1523dstr d_a1
1524ret
1525end
1526verdict 0 2
1527dstr d_a2
1528end
1529
1530note Is pci device directory entry rb-name a GB10? Builds devices/NAME/vendor and /device.
1531fn pcidev o ulong n ulong to bool
1532pprobe c_pci
1533pbyte 47
1534for j ulong n
1535pbyte get db add o j
1536end
1537k = get pbn 0
1538pstr c_vend
1539r = slurp
1540if or lt r 0 not rbat 0 c_nvv
1541ret false
1542end
1543put pbn 0 k
1544pstr c_dev
1545r = slurp
1546if lt r 0
1547ret false
1548end
1549ret rbat 0 c_gb10
1550end
1551
1552fn dbu16 o ulong to ulong
1553lo = ulong get db o
1554hi = ulong get db add o 1
1555ret or lo shl hi 8
1556end
1557
1558note Length of the zero-terminated name at db[o].
1559fn dname o ulong to ulong
1560k = ulong 0
1561while lt add o k len db bound 300
1562if eq get db add o k 0
1563ret k
1564end
1565k = add k 1
1566end
1567ret k
1568end
1569
1570note Count GB10 devices (PCI 10de:2e12) under /sys/bus/pci/devices.
1571fn gbcount to ulong
1572pprobe c_pci
1573fd = fopen pb 0 get pbn 0 540672 0
1574if lt fd 0
1575ret 0
1576end
1577found = ulong 0
1578while true bound 4096
1579n = getdnts int fd db 0 len db
1580if le n 0
1581break
1582end
1583o = ulong 0
1584while lt o ulong n bound 32768
1585rl = dbu16 add o 16
1586nl = dname add o 19
1587if and gt nl 0 ne get db add o 19 46
1588if pcidev add o 19 nl
1589found = add found 1
1590end
1591end
1592if eq rl 0
1593break
1594end
1595o = add o rl
1596end
1597end
1598c = fclose int fd
1599ret found
1600end
1601
1602fn chrdev t view uchar * to bool
1603pprobe t
1604if ne pstat 0
1605ret false
1606end
1607ret eq stype 2
1608end
1609
1610fn ckgpu to void
1611dsel 1
1612g = gbcount
1613put dv 1 g
1614if eq g 0
1615verdict 1 2
1616dstr d_g2
1617ret
1618end
1619ok = and and chrdev c_dctl chrdev c_dn0 chrdev c_duvm
1620pprobe c_pdrv
1621if or not ok not pexist
1622verdict 1 2
1623dstr d_g3
1624ret
1625end
1626verdict 1 0
1627dstr d_g1
1628end
1629
1630fn ckdrv to void
1631dsel 2
1632pprobe c_mver
1633n = slurp
1634if lt n 0
1635verdict 2 2
1636dstr d_d4
1637ret
1638end
1639dstr d_d1
1640drb 0 40
1641if rbat 0 c_good
1642verdict 2 0
1643dstr d_d2
1644ret
1645end
1646verdict 2 2
1647dstr d_d3
1648end
1649
1650note The kB value after key k in /proc/meminfo (rb), 0 when absent.
1651fn memkb k view uchar * to ulong
1652o = rbfind 0 k
1653if ge o get rbn 0
1654ret 0
1655end
1656o = add o len k
1657while eq get rb o 32 bound 64
1658o = add o 1
1659end
1660ret rbnum o
1661end
1662
1663fn ckmem to void
1664dsel 3
1665pprobe c_mem
1666n = slurp
1667if lt n 0
1668verdict 3 2
1669ret
1670end
1671tot = memkb c_mtot
1672av = memkb c_mav
1673mp = memkb c_mmap
1674fr = ulong 0
1675if gt av mp
1676fr = sub av mp
1677end
1678put dv 3 fr
1679dgbk tot
1680dstr d_m1
1681dgbk fr
1682dstr d_m2
1683if lt tot 67108864
1684verdict 3 2
1685dstr d_m4
1686ret
1687end
1688note 35B load need (Core qwen36_batch at 59b05a71, live load log): 37254 MiB model + 8 rows x 1982.85 MiB
1689note + 8192 MiB kept free = 61309 MiB = 62780416 kB (Wolfram: Ceiling[37254+8*1982.85+8192]*1024).
1690if lt fr 62780416
1691verdict 3 1
1692dstr d_m3
1693ret
1694end
1695verdict 3 0
1696end
1697
1698fn dgbk kb ulong to void
1699dnum div mul kb 1024 1000000000
1700end
1701
1702note Free bytes where the model will live, read with the host's df (Omega has no statfs word yet).
1703fn dfree to ulong
1704if not findbin h_df
1705ret 0
1706end
1707pkclr
1708pkpath
1709pkstr h_b1
1710pkstr h_av
1711pclr
1712phome
1713pkpath
1714r = runcap
1715if ne r 0
1716ret 0
1717end
1718o = rbfind 0 t_nl
1719ret rbnum add o 1
1720end
1721
1722fn ckdisk need ulong to void
1723dsel 4
1724f = dfree
1725put dv 4 f
1726if eq f 0
1727verdict 4 1
1728dstr d_k3
1729dgb need
1730ret
1731end
1732dgb f
1733dstr d_k1
1734if lt f need
1735verdict 4 2
1736dstr d_k2
1737dgb need
1738ret
1739end
1740verdict 4 0
1741end
1742
1743fn ckinst to void
1744dsel 5
1745verdict 5 3
1746pclr
1747phome
1748pstr t_cur
1749n = readlnk pb 0 get pbn 0 rb 0 256
1750if le n 0
1751dstr d_i1
1752ret
1753end
1754put rbn 0 ulong n
1755dstr d_i2
1756s = ulong 0
1757for j ulong ulong n
1758if eq get rb j 47
1759s = add j 1
1760end
1761end
1762drb s sub ulong n s
1763if mdone
1764dstr d_i3
1765else
1766dstr d_i4
1767end
1768end
1769
1770note Is the GB10 lock file held? Its inode is looked up in /proc/locks. Returns the holder pid or 0.
1771fn lockpid to ulong
1772pprobe c_lock
1773if ne pstat 0
1774ret 0
1775end
1776ino = sino
1777pprobe c_plk
1778n = slurp
1779if lt n 0
1780ret 0
1781end
1782o = ulong 0
1783while lt o get rbn 0 bound 524289
1784e = rbfind o t_nl
1785p = lkline o e ino
1786if gt p 0
1787ret p
1788end
1789o = add e 1
1790end
1791ret 0
1792end
1793
1794note One /proc/locks line in rb[o..e): "1: POSIX ADVISORY WRITE PID MAJ:MIN:INODE START END".
1795note Returns PID when INODE equals ino.
1796fn lkline o ulong e ulong ino ulong to ulong
1797f = ulong 0
1798i = o
1799pid = ulong 0
1800while lt i e bound 4096
1801if eq get rb i 32
1802while and lt i e eq get rb i 32 bound 4096
1803i = add i 1
1804end
1805f = add f 1
1806if eq f 4
1807pid = rbnum i
1808end
1809if eq f 5
1810ret lkino i e ino pid
1811end
1812else
1813i = add i 1
1814end
1815end
1816ret 0
1817end
1818
1819fn lkino i ulong e ulong ino ulong pid ulong to ulong
1820j = i
1821c = ulong 0
1822while and lt j e lt c 2 bound 64
1823if eq get rb j 58
1824c = add c 1
1825end
1826j = add j 1
1827end
1828if eq rbnum j ino
1829ret pid
1830end
1831ret 0
1832end
1833
1834fn ckbusy to void
1835dsel 6
1836p = lockpid
1837put dv 6 p
1838if eq p 0
1839verdict 6 0
1840dstr d_b1
1841ret
1842end
1843verdict 6 1
1844dstr d_b2
1845dnum p
1846dstr d_b3
1847end
1848
1849fn cknv to void
1850dsel 7
1851verdict 7 3
1852k = ulong 0
1853pprobe c_lcu
1854a = pexist
1855pprobe c_cuda
1856b = pexist
1857pprobe c_smi
1858c = pexist
1859if not or or a b c
1860dstr d_n6
1861ret
1862end
1863dstr d_n1
1864if a
1865dstr d_n2
1866end
1867if and a or b c
1868dstr d_nc
1869end
1870if b
1871dstr d_n3
1872end
1873if and b c
1874dstr d_nc
1875end
1876if c
1877dstr d_n4
1878end
1879dstr d_n5
1880end
1881
1882note Run every check. Returns true when the machine can take this alpha (no check failed).
1883fn doctor need ulong to bool
1884ckarch
1885ckgpu
1886ckdrv
1887ckmem
1888ckdisk need
1889ckinst
1890ckbusy
1891cknv
1892ok = true
1893for i ulong NCHK
1894if eq get dk i 2
1895ok = false
1896end
1897end
1898ret ok
1899end
1900
1901fn cname i ulong to void
1902if eq i 0
1903emit n_arch
1904end
1905if eq i 1
1906emit n_gpu
1907end
1908if eq i 2
1909emit n_drv
1910end
1911if eq i 3
1912emit n_mem
1913end
1914if eq i 4
1915emit n_disk
1916end
1917if eq i 5
1918emit n_inst
1919end
1920if eq i 6
1921emit n_busy
1922end
1923if eq i 7
1924emit n_nv
1925end
1926end
1927
1928fn ctag k ulong colour bool to void
1929if colour
1930if eq k 0
1931fg 120 220 120
1932end
1933if eq k 1
1934fg 250 200 80
1935end
1936if eq k 2
1937fg 255 90 80
1938end
1939if eq k 3
1940fg 140 170 255
1941end
1942end
1943if eq k 0
1944emit k_ok
1945end
1946if eq k 1
1947emit k_wn
1948end
1949if eq k 2
1950emit k_no
1951end
1952if eq k 3
1953emit k_in
1954end
1955if colour
1956sgr0
1957end
1958end
1959
1960note One check as one line: [tag] name padded to 8, then the detail.
1961fn cline i ulong colour bool to void
1962putb 91
1963ctag get dk i colour
1964putb 93
1965putb 32
1966cname i
1967n = cnlen i
1968while lt n 9 bound 16
1969putb 32
1970n = add n 1
1971end
1972dline i
1973end
1974
1975fn dline i ulong to void
1976for j ulong get dtn i
1977putb get dt add mul i 192 j
1978end
1979end
1980
1981fn cnlen i ulong to ulong
1982if eq i 0
1983ret len n_arch
1984end
1985if eq i 1
1986ret len n_gpu
1987end
1988if eq i 2
1989ret len n_drv
1990end
1991if eq i 3
1992ret len n_mem
1993end
1994if eq i 4
1995ret len n_disk
1996end
1997if eq i 5
1998ret len n_inst
1999end
2000if eq i 6
2001ret len n_busy
2002end
2003ret len n_nv
2004end
2005note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2006note net.om: a small HTTP/1.0 client for the engine on 127.0.0.1, written with the native socket
2007note words, and the JSON string escaping it needs. No TLS: it only ever talks to the loopback door.
2008note Chat replies are read as a server-sent event stream and shown token by token.
2009
2010global qb uchar 65536
2011global qbn ulong 1
2012global hs uchar 262144
2013global hsn ulong 1
2014global ab uchar 65536
2015global abn ulong 1
2016global nt ulong 4
2017text j_open "{\"stream\":true,\"max_tokens\":1024,\"messages\":["
2018text j_close "]}"
2019text j_user "{\"role\":\"user\",\"content\":\""
2020text j_asst "{\"role\":\"assistant\",\"content\":\""
2021text j_end "\"}"
2022text j_comma ","
2023text h_post "POST /v1/chat/completions HTTP/1.0"
2024text h_host "Host: 127.0.0.1"
2025text h_ctype "Content-Type: application/json"
2026text h_clen "Content-Length: "
2027text h_get1 "GET "
2028text h_get2 " HTTP/1.0"
2029text h_data "data: "
2030text h_done "[DONE]"
2031text h_cont "\"content\":\""
2032text h_health "/health"
2033
2034fn qstr t view uchar * to void
2035n = get qbn 0
2036if gt add n len t len qb
2037trap 3
2038end
2039for i ulong len t
2040put qb add n i get t i
2041end
2042put qbn 0 add n len t
2043end
2044
2045fn qcrlf to void
2046qbyte 13
2047qbyte 10
2048end
2049
2050fn qline t view uchar * to void
2051qstr t
2052qcrlf
2053end
2054
2055note Offset of the blank line that ends the headers in rb, or the content length when absent.
2056fn hend to ulong
2057e4 = new uchar 4 13
2058put e4 1 10
2059put e4 3 10
2060ret rbfind 0 e4
2061end
2062
2063fn qbyte b uchar to void
2064n = get qbn 0
2065put qb n b
2066put qbn 0 add n 1
2067end
2068
2069fn qnum v ulong to void
2070d = new uchar 20 0
2071k = ulong 0
2072x = v
2073while true bound 21
2074put d k uchar add rem x 10 48
2075k = add k 1
2076x = div x 10
2077if eq x 0
2078break
2079end
2080end
2081for i ulong k
2082qbyte get d sub sub k 1 i
2083end
2084end
2085
2086note Append one byte to the conversation history hs (JSON already escaped).
2087fn hbyte b uchar to void
2088n = get hsn 0
2089if lt n len hs
2090put hs n b
2091put hsn 0 add n 1
2092end
2093end
2094
2095fn hstr t view uchar * to void
2096for i ulong len t
2097hbyte get t i
2098end
2099end
2100
2101fn hexd v ulong to uchar
2102if lt v 10
2103ret uchar add v 48
2104end
2105ret uchar add v 87
2106end
2107
2108note The user's line ib, escaped as a JSON string body, appended to the history.
2109fn hesc to void
2110for i ulong get ibn 0
2111c = get ib i
2112if or eq c 34 eq c 92
2113hbyte 92
2114hbyte c
2115else
2116if lt c 32
2117hbyte 92
2118hbyte 117
2119hbyte 48
2120hbyte 48
2121hbyte hexd ulong shr c 4
2122hbyte hexd ulong and c 15
2123else
2124hbyte c
2125end
2126end
2127end
2128end
2129
2130note A loopback TCP connection to port p. Returns the descriptor or -errno.
2131fn tcp p ulong to long
2132a = new uchar 16 0
2133put a 0 2
2134put a 2 uchar shr p 8
2135put a 3 uchar and p 255
2136put a 4 127
2137put a 7 1
2138fd = socknew 2 524289 0
2139if lt fd 0
2140ret fd
2141end
2142r = connect int fd a 0 16
2143if ne r 0
2144c = fclose int fd
2145ret r
2146end
2147ret fd
2148end
2149
2150fn sendq fd int to long
2151n = get qbn 0
2152o = ulong 0
2153while lt o n bound 65537
2154k = send fd qb o sub n o 0
2155if le k 0
2156ret -5
2157end
2158o = add o ulong k
2159end
2160ret 0
2161end
2162
2163note GET path on 127.0.0.1:p. Returns the HTTP status (200 ..), or a negative error.
2164fn hget p ulong t view uchar * to long
2165fd = tcp p
2166if lt fd 0
2167ret fd
2168end
2169put qbn 0 0
2170qstr h_get1
2171qstr t
2172qline h_get2
2173qline h_host
2174qcrlf
2175r = sendq int fd
2176if ne r 0
2177c = fclose int fd
2178ret r
2179end
2180put rbn 0 0
2181k = waitio int fd 1 5000000000
2182if le k 0
2183c = fclose int fd
2184ret -110
2185end
2186k = recv int fd rb 0 64 0
2187c = fclose int fd
2188if lt k 12
2189ret -71
2190end
2191put rbn 0 ulong k
2192ret long rbnum 9
2193end
2194
2195fn alive p ulong to bool
2196ret eq hget p h_health 200
2197end
2198
2199note Decode one \uXXXX escape's four hex digits at rb[o].
2200fn hex4 o ulong to ulong
2201v = ulong 0
2202for i ulong 4
2203c = ulong get rb add o i
2204d = ulong 0
2205if and ge c 48 le c 57
2206d = sub c 48
2207end
2208if and ge c 97 le c 102
2209d = sub c 87
2210end
2211if and ge c 65 le c 70
2212d = sub c 55
2213end
2214v = or shl v 4 d
2215end
2216ret v
2217end
2218
2219note UTF-8 of code point v to the output buffer.
2220fn utf8 v ulong to void
2221if lt v 128
2222putb uchar v
2223ret
2224end
2225if lt v 2048
2226putb uchar or 192 shr v 6
2227putb uchar or 128 and v 63
2228ret
2229end
2230if lt v 65536
2231putb uchar or 224 shr v 12
2232putb uchar or 128 and shr v 6 63
2233putb uchar or 128 and v 63
2234ret
2235end
2236putb uchar or 240 shr v 18
2237putb uchar or 128 and shr v 12 63
2238putb uchar or 128 and shr v 6 63
2239putb uchar or 128 and v 63
2240end
2241
2242note Print the JSON string body starting at rb[o] up to its closing quote, decoding escapes, and keep
2243note the raw escaped bytes in ab for the history. Returns the offset after the closing quote.
2244fn jshow o ulong e ulong to ulong
2245i = o
2246while lt i e bound 524289
2247c = get rb i
2248if eq c 34
2249ret add i 1
2250end
2251abyte c
2252if ne c 92
2253putb c
2254i = add i 1
2255else
2256i = jesc add i 1 e
2257end
2258end
2259ret i
2260end
2261
2262fn abyte c uchar to void
2263n = get abn 0
2264if lt n len ab
2265put ab n c
2266put abn 0 add n 1
2267end
2268end
2269
2270note One escape after a backslash at rb[i]; returns the offset after it.
2271fn jesc i ulong e ulong to ulong
2272if ge i e
2273ret i
2274end
2275c = get rb i
2276abyte c
2277if eq c 110
2278putb 10
2279ret add i 1
2280end
2281if eq c 116
2282putb 9
2283ret add i 1
2284end
2285if eq c 114
2286ret add i 1
2287end
2288if eq c 117
2289if le add i 5 e
2290for k ulong 4
2291abyte get rb add add i 1 k
2292end
2293v = hex4 add i 1
2294if and ge v 55296 lt v 56320
2295ret jsur i e v
2296end
2297utf8 v
2298ret add i 5
2299end
2300ret e
2301end
2302putb c
2303ret add i 1
2304end
2305
2306note A high surrogate at rb[i-1..i+5) followed by \uDC00..\uDFFF.
2307fn jsur i ulong e ulong v ulong to ulong
2308if and le add i 11 e eq get rb add i 5 92
2309for k ulong 6
2310abyte get rb add add i 5 k
2311end
2312lo = hex4 add i 7
2313utf8 add 65536 add shl sub v 55296 10 sub lo 56320
2314ret add i 11
2315end
2316utf8 65533
2317ret add i 5
2318end
2319
2320note Handle the complete event lines in rb[o .. e): print every content delta. Returns the offset of
2321note the first incomplete line, or the length of rb's content plus one after [DONE].
2322fn events o ulong to ulong
2323i = o
2324while lt i get rbn 0 bound 524289
2325nl = rbfind i t_nl
2326if ge nl get rbn 0
2327ret i
2328end
2329if rbat i h_data
2330if rbat add i len h_data h_done
2331ret add get rbn 0 1
2332end
2333c = rbfind i h_cont
2334if lt c nl
2335if eq get nt 0 0
2336put nt 1 ulong sub monons long get nt 2
2337end
2338put nt 0 add get nt 0 1
2339x = jshow add c len h_cont nl
2340w = flush
2341end
2342end
2343i = add nl 1
2344end
2345ret i
2346end
2347
2348note Send the history as one chat request and stream the answer to the screen. The answer's escaped
2349note text is kept in ab for the history. Returns 0, a negative error, or the HTTP status when not 200.
2350fn chatreq p ulong to long
2351body = add add len j_open get hsn 0 len j_close
2352put qbn 0 0
2353qline h_post
2354qline h_host
2355qline h_ctype
2356qstr h_clen
2357qnum body
2358qcrlf
2359qcrlf
2360if gt add get qbn 0 body len qb
2361ret -7
2362end
2363qstr j_open
2364for i ulong get hsn 0
2365qbyte get hs i
2366end
2367qstr j_close
2368fd = tcp p
2369if lt fd 0
2370ret fd
2371end
2372r = sendq int fd
2373if ne r 0
2374c = fclose int fd
2375ret r
2376end
2377put abn 0 0
2378put nt 0 0
2379put nt 2 ulong monons
2380r = chatrx int fd
2381c = fclose int fd
2382ret r
2383end
2384
2385note Receive the reply: status line, headers, then events until [DONE] or the server closes.
2386fn chatrx fd int to long
2387put rbn 0 0
2388hb2 = ulong 0
2389st = long 0
2390o = ulong 0
2391while lt get rbn 0 len rb bound 524289
2392k = waitio fd 1 600000000000
2393if le k 0
2394ret -110
2395end
2396k = recv fd rb get rbn 0 sub len rb get rbn 0 0
2397if le k 0
2398break
2399end
2400put rbn 0 add get rbn 0 ulong k
2401if eq hb2 0
2402e = hend
2403if lt e get rbn 0
2404hb2 = add e 4
2405st = long rbnum 9
2406o = hb2
2407if ne st 200
2408ret st
2409end
2410end
2411end
2412if gt hb2 0
2413o = events o
2414if gt o get rbn 0
2415ret 0
2416end
2417end
2418end
2419if eq hb2 0
2420ret -71
2421end
2422ret 0
2423end
2424note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2425note steps.om: the screens of the wizard (welcome, doctor, plan, consent, install, model, first chat,
2426note done), the panel under the black hole, the progress bar, file hashing with the Omega SHA-256 unit,
2427note the model download and the engine's start and stop.
2428note
2429note Panel markup: byte 1 gold, 2 plain, 3 green, 4 red, 5 dim, 6 bright. A line ends at byte 10.
2430
2431global ui ulong 12
2432global pt uchar 8192
2433global ptn ulong 1
2434global pp uchar 256
2435global ppn ulong 1
2436global mf uchar 65536
2437global mfn ulong 1
2438global mnm uchar 128
2439global mnn ulong 1
2440global dg uchar 32
2441global bs uchar 1024
2442global bsn ulong 1
2443global pidb uchar 32
2444global ppt uchar 4096
2445global pptn ulong 1
2446text s_title " Sovereign Intelligence alpha "
2447text s_step " step "
2448text s_of " of 8 "
2449text s_n0 "welcome"
2450text s_n1 "doctor"
2451text s_n2 "plan"
2452text s_n3 "your yes"
2453text s_n4 "install"
2454text s_n5 "model"
2455text s_n6 "first chat"
2456text s_n7 "done"
2457text s_omega " written in Omega (.om)"
2458text t_tbase "~/.local/share/aienos"
2459text t_files " files"
2460text t_slash " / "
2461text m_name "name "
2462text m_url "url "
2463text m_bytes "bytes "
2464text m_files "files "
2465text t_share "/share/aienos/model.manifest"
2466text t_base "/.local/share/aienos"
2467text t_models "/models/"
2468text t_vfy "/.aienos-verified"
2469text t_sums "/SHA256SUMS"
2470text i_l1 "Checking every file of this release with SHA-256."
2471text i_l2 "The hash runs in Omega (the sha256.om unit shipped beside me)."
2472text i_ok " files match SHA256SUMS."
2473text i_bad "A file does not match SHA256SUMS: "
2474text i_bad2 "This copy is damaged or was changed. Install again with the curl command."
2475text i_cut "Meanwhile the user-space stack falls into the hole: libcuda, cuBLAS, nvcc."
2476text i_cut2 "That is the picture only. On disk those stay installed; AienOS just never loads them."
2477text i_md1 "Created "
2478text i_md2 " for the model."
2479text t_mkfail "I could not create the model folder in ~/.local/share/aienos."
2480text t_wait "working, please wait"
2481text t_next "Press Enter to continue"
2482text t_close "Press Enter to close"
2483text h_curl "curl"
2484text c_f "-fL"
2485text c_s "-sS"
2486text c_r "--retry"
2487text c_5 "5"
2488text c_c "-C"
2489text c_dash "-"
2490text c_o "-o"
2491text c_ua "--user-agent"
2492text c_uav "aienos-wizard/alpha"
2493text t_part ".part"
2494text t_curlog "/download.log"
2495text m_have "The model is already here and verified. Nothing to download."
2496text m_l1 "Downloading "
2497text m_l2 "from Hugging Face: "
2498text m_l3 " in "
2499text m_l4 " files."
2500text m_l5 "Every file is checked against its SHA-256 before it is used."
2501text m_l6 "A cut connection resumes where it stopped. q is not read here; Ctrl+C stops."
2502text m_fail "The download or a hash check failed. Nothing broken was kept."
2503text m_fail2 "Run aienos again to resume. The log is ~/.local/share/aienos/download.log."
2504text m_done "Every model file is downloaded and matches its SHA-256."
2505text t_dlocal "/.local"
2506text t_dshare "/share"
2507
2508note ui: 0 animate, 1 scene rows, 2 step, 3 last frame ns, 4 bytes done, 5 bytes total,
2509note 6 bar shown, 7 files done, 8 files total, 9 engine pid, 10 frames since step entry.
2510
2511fn pclear to void
2512put ptn 0 0
2513end
2514
2515fn pl t view uchar * to void
2516n = get ptn 0
2517for i ulong len t
2518if lt add n i len pt
2519put pt add n i get t i
2520end
2521end
2522m = add n len t
2523if gt m len pt
2524m = len pt
2525end
2526put ptn 0 m
2527end
2528
2529fn pc b uchar to void
2530n = get ptn 0
2531if lt n len pt
2532put pt n b
2533put ptn 0 add n 1
2534end
2535end
2536
2537fn pln to void
2538pc 10
2539end
2540
2541fn plnum v ulong to void
2542d = new uchar 20 0
2543k = ulong 0
2544x = v
2545while true bound 21
2546put d k uchar add rem x 10 48
2547k = add k 1
2548x = div x 10
2549if eq x 0
2550break
2551end
2552end
2553for i ulong k
2554pc get d sub sub k 1 i
2555end
2556end
2557
2558note Gigabytes (10^9 bytes) rounded to one decimal.
2559fn plgb v ulong to void
2560r = add v 50000000
2561plnum div r 1000000000
2562pc 46
2563plnum div rem r 1000000000 100000000
2564pl t_gb
2565end
2566
2567note The base folder ~/.local/share/aienos as text in the panel, spelled with a tilde.
2568fn plbase to void
2569pl t_tbase
2570end
2571
2572fn prompt t view uchar * to void
2573put ppn 0 0
2574for i ulong len t
2575if lt i len pp
2576put pp i get t i
2577end
2578end
2579put ppn 0 len t
2580end
2581
2582fn mark b uchar to void
2583if eq b 1
2584fg 255 196 90
2585end
2586if eq b 2
2587sgr0
2588end
2589if eq b 3
2590fg 120 220 120
2591end
2592if eq b 4
2593fg 255 100 90
2594end
2595if eq b 5
2596fg 140 140 165
2597end
2598if eq b 6
2599bold
2600fg 245 245 255
2601end
2602end
2603
2604fn sname i ulong to void
2605if eq i 0
2606emit s_n0
2607end
2608if eq i 1
2609emit s_n1
2610end
2611if eq i 2
2612emit s_n2
2613end
2614if eq i 3
2615emit s_n3
2616end
2617if eq i 4
2618emit s_n4
2619end
2620if eq i 5
2621emit s_n5
2622end
2623if eq i 6
2624emit s_n6
2625end
2626if eq i 7
2627emit s_n7
2628end
2629end
2630
2631fn header row ulong to void
2632mv row 1
2633bg 18 16 28
2634fg 255 196 90
2635bold
2636emit s_title
2637sgr0
2638bg 18 16 28
2639fg 200 200 220
2640emit s_step
2641num add get ui 2 1
2642emit s_of
2643sname get ui 2
2644fg 120 120 150
2645emit s_omega
2646eol
2647sgr0
2648end
2649
2650note Draw the panel lines from row r0 on, at most rows lines, each cleared to the end of the line.
2651note Put one panel byte at column col (counted from 3); bytes past the terminal's width are
2652note dropped so a long line never wraps into the next one. Returns the new column.
2653fn pput b uchar col ulong to ulong
2654if ge col sub get tw 0 1
2655ret col
2656end
2657putb b
2658if eq and b 192 128
2659ret col
2660end
2661ret add col 1
2662end
2663
2664fn pdraw r0 ulong rows ulong colour bool to void
2665row = r0
2666col = ulong 3
2667i = ulong 0
2668n = get ptn 0
2669mv row 3
2670eol
2671while lt i n bound 8193
2672b = get pt i
2673if eq b 10
2674row = add row 1
2675if ge row add r0 rows
2676ret
2677end
2678if colour
2679sgr0
2680end
2681mv row 1
2682eol
2683mv row 3
2684col = 3
2685else
2686if lt b 7
2687if colour
2688mark b
2689end
2690else
2691col = pput b col
2692end
2693end
2694i = add i 1
2695end
2696if colour
2697sgr0
2698end
2699row = add row 1
2700while lt row add r0 rows bound 256
2701mv row 1
2702eol
2703row = add row 1
2704end
2705end
2706
2707note The progress bar: done/total bytes as a gold bar, the percentage and the file count.
2708fn pbar row ulong to void
2709mv row 3
2710eol
2711if eq get ui 6 0
2712ret
2713end
2714w = ulong 40
2715tot = get ui 5
2716if eq tot 0
2717tot = 1
2718end
2719dn = get ui 4
2720if gt dn tot
2721dn = tot
2722end
2723f = div mul dn w tot
2724fg 255 196 90
2725for i ulong w
2726if lt i f
2727bg 255 170 60
2728else
2729bg 50 44 70
2730end
2731putb 32
2732end
2733sgr0
2734putb 32
2735numw div mul dn 100 tot 3
2736putb 37
2737putb 32
2738putb 32
2739num get ui 7
2740emit t_slash
2741num get ui 8
2742emit t_files
2743end
2744
2745fn pprompt row ulong colour bool to void
2746mv row 3
2747eol
2748if colour
2749fg 255 196 90
2750bold
2751end
2752putb 62
2753putb 32
2754for i ulong get ppn 0
2755putb get pp i
2756end
2757if colour
2758sgr0
2759end
2760end
2761
2762note One animation frame: the scene, the header, the panel, the bar and the prompt.
2763fn frame to void
2764if eq get ui 0 0
2765ret
2766end
2767sh = get ui 1
2768t = get th 0
2769home
2770hframe sh
2771header add sh 1
2772pdraw add sh 2 sub sub t sh 4 true
2773pbar sub t 1
2774pprompt t true
2775w = flush
2776put ui 3 ulong monons
2777put ui 10 add get ui 10 1
2778end
2779
2780note Draw a frame only when 80 ms have passed since the last one; used inside long loops.
2781fn tickui to void
2782if eq get ui 0 0
2783ret
2784end
2785if gt sub ulong monons get ui 3 80000000
2786frame
2787end
2788end
2789
2790note Plain mode (no terminal or too small): print the panel once, without colour or animation.
2791fn plainout to void
2792putb 10
2793emit s_title
2794emit s_step
2795num add get ui 2 1
2796emit s_of
2797sname get ui 2
2798putb 10
2799for i ulong get ptn 0
2800b = get pt i
2801if or ge b 7 eq b 10
2802putb b
2803end
2804end
2805putb 10
2806if gt get ui 6 0
2807pbarp
2808end
2809w = flush
2810end
2811
2812fn pbarp to void
2813tot = get ui 5
2814if eq tot 0
2815tot = 1
2816end
2817num div mul get ui 4 100 tot
2818putb 37
2819putb 32
2820num get ui 7
2821emit t_slash
2822num get ui 8
2823emit t_files
2824putb 10
2825end
2826
2827fn enter i ulong to void
2828put ui 2 i
2829put ui 6 0
2830put ui 10 0
2831if eq get ui 0 1
2832cls
2833end
2834end
2835
2836note Show the step: a frame, or in plain mode the panel text.
2837fn show to void
2838if eq get ui 0 1
2839frame
2840else
2841plainout
2842end
2843end
2844
2845note Wait for one line of input while the hole keeps turning. Returns its length or -1 at end of input.
2846fn ask to long
2847if eq get ui 0 1
2848while true bound 999999
2849frame
2850k = waitio 0 1 80000000
2851if ne k 0
2852break
2853end
2854end
2855else
2856for i ulong get ppn 0
2857putb get pp i
2858end
2859putb 58
2860putb 32
2861w = flush
2862end
2863ret rline
2864end
2865
2866note Keep animating for about ms milliseconds (plain mode returns at once).
2867fn hold ms ulong to void
2868if eq get ui 0 0
2869ret
2870end
2871st = ulong monons
2872while lt sub ulong monons st mul ms 1000000 bound 100000
2873frame
2874s = sleepns 60000000
2875end
2876end
2877
2878note ---------- SHA-256 of a file, with the Omega unit in sha256.om ----------
2879
2880note Hash the file named by pb into dg, adding its bytes to ui 4 as it goes. Returns the size or -errno.
2881fn fhash to long
2882c = new uchar 104 0
2883buf = new uchar 1048576 0
2884o = new uchar 32 0
2885fd = fopen pb 0 get pbn 0 524288 0
2886if lt fd 0
2887ret fd
2888end
2889sha256_init c
2890tot = ulong 0
2891while true bound 999999
2892k = read int fd buf 0 len buf
2893if lt k 0
2894x = fclose int fd
2895ret k
2896end
2897if eq k 0
2898break
2899end
2900sha256_update c part buf 0 ulong k
2901tot = add tot ulong k
2902put ui 4 add get ui 4 ulong k
2903tickui
2904end
2905x = fclose int fd
2906sha256_final c o
2907for i ulong 32
2908put dg i get o i
2909end
2910ret long tot
2911end
2912
2913note Does dg equal the 64 lowercase hex digits at mf[o]?
2914fn dgis o ulong to bool
2915for i ulong 32
2916b = get dg i
2917if ne get mf add o mul i 2 hexd ulong shr b 4
2918ret false
2919end
2920if ne get mf add add o mul i 2 1 hexd ulong and b 15
2921ret false
2922end
2923end
2924ret true
2925end
2926
2927note ---------- manifests ----------
2928
2929note Load the file named by pb into mf. Returns its length or -errno.
2930fn mload to long
2931n = slurp
2932if lt n 0
2933ret n
2934end
2935if gt get rbn 0 len mf
2936ret -27
2937end
2938for i ulong get rbn 0
2939put mf i get rb i
2940end
2941put mfn 0 get rbn 0
2942ret n
2943end
2944
2945fn mfat o ulong t view uchar * to bool
2946if gt add o len t get mfn 0
2947ret false
2948end
2949for i ulong len t
2950if ne get mf add o i get t i
2951ret false
2952end
2953end
2954ret true
2955end
2956
2957fn mfnl o ulong to ulong
2958i = o
2959while lt i get mfn 0 bound 65537
2960if eq get mf i 10
2961ret i
2962end
2963i = add i 1
2964end
2965ret get mfn 0
2966end
2967
2968fn mfnum o ulong to ulong
2969x = ulong 0
2970i = o
2971while lt i get mfn 0 bound 21
2972c = get mf i
2973if or lt c 48 gt c 57
2974ret x
2975end
2976x = add mul x 10 ulong sub c 48
2977i = add i 1
2978end
2979ret x
2980end
2981
2982note Model manifest lines: "name N", "url U", "bytes B", "files F", then "SHA256 SIZE PATH" per file.
2983
2984note Offset of the value after key t at the start of a manifest line, or the manifest length.
2985fn mkey t view uchar * to ulong
2986o = ulong 0
2987while lt o get mfn 0 bound 65537
2988if mfat o t
2989ret add o len t
2990end
2991o = add mfnl o 1
2992end
2993ret get mfn 0
2994end
2995
2996fn mopen to bool
2997pclr
2998proot
2999pstr t_share
3000n = mload
3001if lt n 0
3002ret false
3003end
3004o = mkey m_name
3005e = mfnl o
3006if ge o e
3007ret false
3008end
3009put mnn 0 0
3010for i ulong sub e o
3011if lt i len mnm
3012put mnm i get mf add o i
3013put mnn 0 add i 1
3014end
3015end
3016ret true
3017end
3018
3019fn mbytes to ulong
3020ret mfnum mkey m_bytes
3021end
3022
3023fn mfiles to ulong
3024ret mfnum mkey m_files
3025end
3026
3027fn pbase to void
3028pclr
3029phome
3030pstr t_base
3031end
3032
3033note pb = ~/.local/share/aienos/models/NAME
3034fn pmodel to void
3035pbase
3036pstr t_models
3037for i ulong get mnn 0
3038pbyte get mnm i
3039end
3040end
3041
3042fn mdone to bool
3043if not mopen
3044ret false
3045end
3046pmodel
3047pstr t_vfy
3048ret pexist
3049end
3050
3051note ---------- install step: verify this release ----------
3052
3053note Verify root/SHA256SUMS ("HEX PATH" lines). Returns the number of files checked, or the
3054note negative offset+1 of the first bad line.
3055fn sums to long
3056pclr
3057proot
3058pstr t_sums
3059n = mload
3060if lt n 0
3061ret -1
3062end
3063put ui 4 0
3064put ui 5 sumbytes
3065put ui 6 1
3066put ui 7 0
3067put ui 8 sumcount
3068o = ulong 0
3069k = long 0
3070while lt o get mfn 0 bound 65537
3071e = mfnl o
3072if ishex o e
3073r = sumline o e
3074if not r
3075ret neg long add o 1
3076end
3077k = add k 1
3078put ui 7 ulong k
3079end
3080o = add e 1
3081end
3082ret k
3083end
3084
3085fn sumline o ulong e ulong to bool
3086pclr
3087proot
3088pbyte 47
3089for i ulong sub sub e o 66
3090pbyte get mf add add o 66 i
3091end
3092r = fhash
3093if lt r 0
3094ret false
3095end
3096ret dgis o
3097end
3098
3099fn sumcount to ulong
3100k = ulong 0
3101o = ulong 0
3102while lt o get mfn 0 bound 65537
3103e = mfnl o
3104if ishex o e
3105k = add k 1
3106end
3107o = add e 1
3108end
3109ret k
3110end
3111
3112note Total size of the files listed in SHA256SUMS (for the progress bar).
3113fn sumbytes to ulong
3114t = ulong 0
3115o = ulong 0
3116while lt o get mfn 0 bound 65537
3117e = mfnl o
3118if ishex o e
3119pclr
3120proot
3121pbyte 47
3122for i ulong sub sub e o 66
3123pbyte get mf add add o 66 i
3124end
3125if eq pstat 0
3126t = add t ssize
3127end
3128end
3129o = add e 1
3130end
3131ret t
3132end
3133
3134fn mkdirs to bool
3135pclr
3136phome
3137pstr t_dlocal
3138r = makedir pb 0 get pbn 0 493
3139pstr t_dshare
3140r = makedir pb 0 get pbn 0 493
3141pbase
3142r = makedir pb 0 get pbn 0 448
3143pmodel
3144pdrop add get mnn 0 1
3145r = makedir pb 0 get pbn 0 448
3146pmodel
3147r = makedir pb 0 get pbn 0 448
3148ret or eq r 0 eq r -17
3149end
3150
3151fn stinst to bool
3152enter 4
3153pclear
3154pl i_l1
3155pln
3156pl i_l2
3157pln
3158pln
3159pc 5
3160pl i_cut
3161pln
3162pl i_cut2
3163pc 2
3164prompt t_wait
3165show
3166k = sums
3167put ui 6 0
3168pclear
3169if lt k 0
3170pc 4
3171pl i_bad
3172pc 2
3173pln
3174pl i_bad2
3175prompt t_close
3176show
3177r = ask
3178ret false
3179end
3180pc 3
3181plnum ulong k
3182pl i_ok
3183pc 2
3184pln
3185if not mkdirs
3186pc 4
3187pl t_mkfail
3188pc 2
3189prompt t_close
3190show
3191r = ask
3192ret false
3193end
3194pl i_md1
3195plbase
3196pl t_models
3197for i ulong get mnn 0
3198pc get mnm i
3199end
3200pl i_md2
3201pln
3202pln
3203pc 5
3204pl i_cut
3205pln
3206pl i_cut2
3207pc 2
3208prompt t_next
3209hold 1500
3210show
3211r = ask
3212ret ge r 0
3213end
3214
3215note ---------- model download ----------
3216
3217note Per file line at mf[o..e): "SHA SIZE PATH". Puts the path start in ui 11 and returns the size.
3218fn fline o ulong e ulong to ulong
3219s = add o 65
3220sz = mfnum s
3221i = s
3222while and lt i e ne get mf i 32 bound 32
3223i = add i 1
3224end
3225put ui 11 add i 1
3226ret sz
3227end
3228
3229note pb = model folder / PATH (from mf[ui 11 .. e)).
3230fn pmfile e ulong to void
3231pmodel
3232pbyte 47
3233for i ulong sub e get ui 11
3234pbyte get mf add get ui 11 i
3235end
3236end
3237
3238note Run curl for URL base + PATH into pb + ".part", animating while it runs. Returns curl's status.
3239fn fetch e ulong sz ulong to long
3240pkclr
3241if not findbin h_curl
3242ret -2
3243end
3244pkpath
3245pkstr c_f
3246pkstr c_s
3247pkstr c_r
3248pkstr c_5
3249pkstr c_ua
3250pkstr c_uav
3251pkstr c_c
3252pkstr c_dash
3253pkstr c_o
3254pmfile e
3255pstr t_part
3256savep
3257pkpath
3258pkurl e
3259pbase
3260pstr t_curlog
3261ret runbg sz
3262end
3263
3264note The URL: the manifest's url value, then PATH.
3265fn pkurl e ulong to void
3266u = mkey m_url
3267ue = mfnl u
3268pclr
3269for i ulong sub ue u
3270pbyte get mf add u i
3271end
3272for i ulong sub e get ui 11
3273pbyte get mf add get ui 11 i
3274end
3275pkpath
3276end
3277
3278note Like runpk, but animates and follows the .part file's size while the child runs.
3279fn runbg sz ulong to long
3280lfd = fopen pb 0 get pbn 0 525377 384
3281if lt lfd 0
3282ret lfd
3283end
3284pid = fork
3285if lt pid 0
3286ret pid
3287end
3288if eq pid 0
3289d = dupfd int lfd 1 0
3290d = dupfd int lfd 2 0
3291r = execprog pk 0 get pkn 0
3292exit 127
3293end
3294c = fclose int lfd
3295base = get ui 4
3296while true bound 999999
3297r = waitpid int pid ws 0 4 1
3298if eq r pid
3299ret wcode
3300end
3301if lt r 0
3302ret r
3303end
3304put ui 4 add base partsz
3305frame
3306s = sleepns 200000000
3307end
3308ret -4
3309end
3310
3311note Size of the .part file being written (its path was saved in ppt by fetch).
3312fn partsz to ulong
3313r = stat ppt 0 get pptn 0 sb 0 128
3314if ne r 0
3315ret 0
3316end
3317ret ssize
3318end
3319
3320fn savep to void
3321for i ulong get pbn 0
3322put ppt i get pb i
3323end
3324put pptn 0 get pbn 0
3325end
3326
3327fn mstep to bool
3328enter 5
3329if not mopen
3330ret false
3331end
3332if mdone
3333pclear
3334pc 3
3335pl m_have
3336pc 2
3337pln
3338prompt t_next
3339show
3340r = ask
3341ret ge r 0
3342end
3343pclear
3344pl m_l1
3345pc 6
3346for i ulong get mnn 0
3347pc get mnm i
3348end
3349pc 2
3350pln
3351pl m_l2
3352plgb mbytes
3353pl m_l3
3354plnum mfiles
3355pl m_l4
3356pln
3357pl m_l5
3358pln
3359pln
3360pc 5
3361pl m_l6
3362pc 2
3363prompt t_wait
3364put ui 4 0
3365put ui 5 mbytes
3366put ui 6 1
3367put ui 7 0
3368put ui 8 mfiles
3369show
3370r = mall
3371put ui 6 0
3372if not r
3373pclear
3374pc 4
3375pl m_fail
3376pc 2
3377pln
3378pl m_fail2
3379prompt t_close
3380show
3381x = ask
3382ret false
3383end
3384pmodel
3385pstr t_vfy
3386put rb 0 111
3387put rb 1 107
3388put rb 2 10
3389x = spill 3
3390pclear
3391pc 3
3392pl m_done
3393pc 2
3394pln
3395prompt t_next
3396show
3397x = ask
3398ret ge x 0
3399end
3400
3401note Is mf[o..e) a file line: 64 lowercase hex digits, then a space?
3402fn ishex o ulong e ulong to bool
3403if le sub e o 66
3404ret false
3405end
3406for i ulong 64
3407c = get mf add o i
3408if not or and ge c 48 le c 57 and ge c 97 le c 102
3409ret false
3410end
3411end
3412ret eq get mf add o 64 32
3413end
3414
3415note Fetch and verify every file of the model manifest.
3416fn mall to bool
3417o = ulong 0
3418while lt o get mfn 0 bound 65537
3419e = mfnl o
3420if ishex o e
3421if not mfile o e
3422ret false
3423end
3424put ui 7 add get ui 7 1
3425end
3426o = add e 1
3427end
3428ret true
3429end
3430
3431note One model file: keep it if it is there and hashes right, else download to .part, check the
3432note size and hash, then rename it into place. A file that fails its hash is deleted.
3433fn mfile o ulong e ulong to bool
3434sz = fline o e
3435pmfile e
3436if eq pstat 0
3437if eq ssize sz
3438r = fhash
3439if and ge r 0 dgis o
3440ret true
3441end
3442put ui 4 sub get ui 4 sz
3443end
3444r = unlink pb 0 get pbn 0
3445end
3446ret mget o e sz
3447end
3448
3449fn mget o ulong e ulong sz ulong to bool
3450for tries ulong 2
3451pmfile e
3452pstr t_part
3453st = get ui 4
3454if eq pstat 0
3455if gt ssize sz
3456r = unlink pb 0 get pbn 0
3457end
3458end
3459c = fetch e sz
3460put ui 4 st
3461pmfile e
3462pstr t_part
3463if and eq c 0 eq pstat 0
3464if eq ssize sz
3465h = fhash
3466if dgis o
3467ret mplace e
3468end
3469end
3470end
3471pmfile e
3472pstr t_part
3473r = unlink pb 0 get pbn 0
3474end
3475ret false
3476end
3477
3478note Rename model/PATH.part to model/PATH with renameat2 (packed list of the two paths).
3479fn mplace e ulong to bool
3480pkclr
3481pmfile e
3482pstr t_part
3483pkpath
3484pmfile e
3485pkpath
3486r = renames pk 0 get pkn 0 0
3487ret eq r 0
3488end
3489note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3490note engine.om: start, watch and stop the engine (aien-serve-http, Core ship/motor-l387, engine omega) on
3491note 127.0.0.1, check from /proc that it loaded no CUDA library, the first-chat screen, the done
3492note screen, the uninstaller and the rollback.
3493
3494const PORT ulong 18100
3495text e_bin "/bin/aien-serve-http"
3496text e_eng "--engine"
3497text e_omega "omega"
3498text e_mdir "--model-dir"
3499text e_port "--port"
3500text e_ctx "--max-context"
3501text e_ctxv "49152"
3502text e_cap "--max-tokens-cap"
3503text e_capv "16384"
3504text e_thk "--thinking"
3505text e_off "off"
3506text e_slot "--batch-slots"
3507text e_slotv "8"
3508text e_log "/engine.log"
3509text e_pid "/engine.pid"
3510text e_null "/dev/null"
3511text e_proc "/proc/"
3512text e_exe "/exe"
3513text e_maps "/maps"
3514text e_name "aien-serve-http"
3515text e_cu1 "libcuda"
3516text e_cu2 "libcublas"
3517text e_cu3 "libcudart"
3518text e_cu4 "libnvidia-"
3519text f_skip1 "I am not starting the engine now: "
3520text f_mem "another model is using this GB10's memory."
3521text f_lock "another process holds the GB10 lock."
3522text f_skip2 "Your model is installed. When the GPU is free, run: aienos chat"
3523text f_load1 "Starting the engine (Core ship/motor-l387 59b05a71) and loading the 35B onto the GB10."
3524text f_load2 "The first load reads about 37.5 GB, so it can take a few minutes."
3525text f_fail1 "The engine stopped before it was ready. Its log: ~/.local/share/aienos/engine.log"
3526text f_q "Say hello in one short sentence, and tell me what you are."
3527text f_you "you "
3528text f_ai "model "
3529text f_ttf "first token after "
3530text f_ms " ms"
3531text f_cuda "CUDA libraries in the engine's memory map (/proc/"
3532text f_cuda2 "/maps): "
3533text f_run "The engine keeps running on 127.0.0.1:18100. Stop it with: aienos stop"
3534text d_l1 "Your local model is ready."
3535text d_l0 "Your model is installed and verified. Start it with aienos chat when the GPU is free."
3536text d_l2 " aienos chat talk to it in this terminal"
3537text d_l3 " aienos doctor check this machine again"
3538text d_l4 " aienos stop stop the engine"
3539text d_l5 " aienos source where my .om source lives"
3540text d_l6 " aienos uninstall remove everything AienOS put on this machine"
3541text d_l7 "OpenAI-style API: http://127.0.0.1:18100/v1/chat/completions"
3542text u_list "/installed-files"
3543text u_sure "This removes AienOS from this machine. Type yes to remove it"
3544text u_keep "--keep-model"
3545text u_yes "--yes"
3546text u_done "AienOS is removed. NVIDIA's driver and CUDA were never touched."
3547text u_kept "The model was kept in ~/.local/share/aienos/models."
3548text u_no "Nothing was removed."
3549text u_stop "The engine did not stop; nothing was removed."
3550text u_dot ".."
3551text u_mods "models"
3552text r_rel "/releases"
3553text r_cur "/current"
3554text r_new "/current.new"
3555text r_pre "releases/"
3556text r_none "There is no other release to roll back to."
3557text r_ok "current now points to "
3558
3559note The packed argument list of the live 35B server's command (same flags), on PORT.
3560fn eargs to void
3561pkclr
3562pclr
3563proot
3564pstr e_bin
3565pkpath
3566pkstr e_eng
3567pkstr e_omega
3568pkstr e_mdir
3569pmodel
3570pkpath
3571pkstr e_port
3572pknum PORT
3573pkstr e_ctx
3574pkstr e_ctxv
3575pkstr e_cap
3576pkstr e_capv
3577pkstr e_thk
3578pkstr e_off
3579pkstr e_slot
3580pkstr e_slotv
3581end
3582
3583note Start the engine in its own session, output appended to engine.log, pid in engine.pid.
3584fn estart to long
3585eargs
3586pbase
3587pstr e_log
3588lfd = fopen pb 0 get pbn 0 525377 384
3589if lt lfd 0
3590ret lfd
3591end
3592nfd = fopen e_null 0 len e_null 524288 0
3593pid = fork
3594if lt pid 0
3595ret pid
3596end
3597if eq pid 0
3598s = setsid
3599d = dupfd int nfd 0 0
3600d = dupfd int lfd 1 0
3601d = dupfd int lfd 2 0
3602r = execprog pk 0 get pkn 0
3603exit 127
3604end
3605c = fclose int lfd
3606c = fclose int nfd
3607put ui 9 ulong pid
3608put rbn 0 0
3609pclr
3610pnum ulong pid
3611for i ulong get pbn 0
3612put rb i get pb i
3613end
3614put rb get pbn 0 10
3615k = add get pbn 0 1
3616pbase
3617pstr e_pid
3618r = spill k
3619ret pid
3620end
3621
3622note The pid in engine.pid when that process is still aien-serve-http, else 0.
3623fn epid to ulong
3624pbase
3625pstr e_pid
3626n = slurp
3627if le n 0
3628ret 0
3629end
3630p = rbnum 0
3631if eq p 0
3632ret 0
3633end
3634pclr
3635pstr e_proc
3636pnum p
3637pstr e_exe
3638n = readlnk pb 0 get pbn 0 rb 0 4096
3639if le n 0
3640ret 0
3641end
3642put rbn 0 ulong n
3643if lt ulong n len e_name
3644ret 0
3645end
3646if rbat sub ulong n len e_name e_name
3647ret p
3648end
3649ret 0
3650end
3651
3652note Stop the engine: SIGTERM, then wait up to 120 s for it to go. Returns true when it is gone.
3653fn estop to bool
3654p = epid
3655if eq p 0
3656ret true
3657end
3658r = killpid int p 15
3659for i ulong 1200
3660x = waitpid int p ws 0 4 1
3661if eq epid 0
3662pbase
3663pstr e_pid
3664x = unlink pb 0 get pbn 0
3665ret true
3666end
3667s = sleepns 100000000
3668end
3669ret false
3670end
3671
3672note Count the CUDA user-space libraries in /proc/PID/maps (libcuda, libcublas, libcudart,
3673note libnvidia-*). Returns the count, or -1 when the maps could not be read.
3674fn ecuda p ulong to long
3675pclr
3676pstr e_proc
3677pnum p
3678pstr e_maps
3679n = slurp
3680if lt n 0
3681ret -1
3682end
3683k = long 0
3684o = ulong 0
3685while lt o get rbn 0 bound 524289
3686e = rbfind o t_nl
3687if culine o e
3688k = add k 1
3689end
3690o = add e 1
3691end
3692ret k
3693end
3694
3695fn culine o ulong e ulong to bool
3696a = or rbin o e e_cu1 rbin o e e_cu2
3697b = or rbin o e e_cu3 rbin o e e_cu4
3698ret or a b
3699end
3700
3701note Wait for the engine's /health while the hole turns. Gives up after limit seconds or when the
3702note process exits. Returns true when it answers.
3703fn ewait limit ulong to bool
3704st = ulong monons
3705p = get ui 9
3706while lt sub ulong monons st mul limit 1000000000 bound 999999
3707if alive PORT
3708ret true
3709end
3710x = waitpid int p ws 0 4 1
3711if eq x long p
3712ret false
3713end
3714put ui 4 div sub ulong monons st 1000000000
3715for k ulong 20
3716frame
3717s = sleepns 100000000
3718end
3719end
3720ret false
3721end
3722
3723fn ckskip to bool
3724if eq get dk 6 1
3725pl f_skip1
3726pl f_lock
3727ret true
3728end
3729if eq get dk 3 1
3730pl f_skip1
3731pl f_mem
3732ret true
3733end
3734ret false
3735end
3736
3737note The first-chat step. Returns false only when the engine failed to come up.
3738fn stchat to bool
3739enter 6
3740pclear
3741put ui 11 0
3742if ckskip
3743pln
3744pln
3745pl f_skip2
3746prompt t_next
3747show
3748r = ask
3749ret true
3750end
3751if not alive PORT
3752pl f_load1
3753pln
3754pl f_load2
3755prompt t_wait
3756put ui 6 0
3757show
3758p = estart
3759if lt p 0
3760ret false
3761end
3762if not ewait 2400
3763pclear
3764pc 4
3765pl f_fail1
3766pc 2
3767prompt t_close
3768show
3769r = ask
3770ret false
3771end
3772end
3773put ui 11 1
3774firstq
3775ret true
3776end
3777
3778note Ask the first question and stream the answer under the picture, then show the evidence lines.
3779fn firstq to void
3780put hsn 0 0
3781hstr j_user
3782put ibn 0 len f_q
3783for i ulong len f_q
3784put ib i get f_q i
3785end
3786hesc
3787hstr j_end
3788pclear
3789pc 5
3790pl f_you
3791pc 2
3792pl f_q
3793pln
3794pc 1
3795pl f_ai
3796pc 2
3797prompt t_wait
3798show
3799if eq get ui 0 1
3800mv add get ui 1 4 10
3801end
3802r = chatreq PORT
3803w = flush
3804ftail
3805end
3806
3807fn ftail to void
3808pclear
3809pc 3
3810pl f_ttf
3811plnum div get nt 1 1000000
3812pl f_ms
3813pc 2
3814pln
3815p = epid
3816if gt p 0
3817pl f_cuda
3818plnum p
3819pl f_cuda2
3820c = ecuda p
3821if eq c 0
3822pc 3
3823end
3824plnum ulong c
3825pc 2
3826pln
3827end
3828pl f_run
3829prompt t_next
3830if eq get ui 0 1
3831pdraw sub get th 0 4 3 true
3832pprompt get th 0 true
3833x = flush
3834else
3835plainout
3836end
3837r = rline
3838end
3839
3840fn stdone ready bool to void
3841enter 7
3842pclear
3843pc 3
3844if ready
3845pl d_l1
3846else
3847pl d_l0
3848end
3849pc 2
3850pln
3851pln
3852pl d_l2
3853pln
3854pl d_l3
3855pln
3856pl d_l4
3857pln
3858pl d_l5
3859pln
3860pl d_l6
3861pln
3862pln
3863pc 5
3864pl d_l7
3865pc 2
3866prompt t_close
3867show
3868r = ask
3869end
3870
3871note ---------- uninstall ----------
3872
3873note Is pb inside the home folder: HOME + "/" + something, with no ".." component?
3874fn inhome to bool
3875h = get hbn 0
3876if le get pbn 0 add h 1
3877ret false
3878end
3879if le h 1
3880ret false
3881end
3882for i ulong h
3883if ne get pb i get hb i
3884ret false
3885end
3886end
3887if ne get pb h 47
3888ret false
3889end
3890for i ulong sub get pbn 0 1
3891if and eq get pb i 46 eq get pb add i 1 46
3892ret false
3893end
3894end
3895ret true
3896end
3897
3898note Unlink every path in installed-files that lies inside the home folder.
3899fn ulist to void
3900pbase
3901pstr u_list
3902n = mload
3903if lt n 0
3904ret
3905end
3906o = ulong 0
3907while lt o get mfn 0 bound 65537
3908e = mfnl o
3909pclr
3910for i ulong sub e o
3911pbyte get mf add o i
3912end
3913if inhome
3914r = unlink pb 0 get pbn 0
3915end
3916o = add e 1
3917end
3918end
3919
3920note Remove the tree at pb (not following links). depth guards the recursion; keep skips the
3921note models folder at the top level.
3922fn rmt depth ulong keep bool to void
3923d = new uchar 8192 0
3924if gt depth 32
3925ret
3926end
3927for pass ulong 3
3928fd = fopen pb 0 get pbn 0 573440 0
3929if lt fd 0
3930r = unlink pb 0 get pbn 0
3931ret
3932end
3933while true bound 100000
3934n = getdnts int fd d 0 8192
3935if le n 0
3936break
3937end
3938o = ulong 0
3939while lt o ulong n bound 8192
3940o = rment d o depth keep
3941end
3942end
3943c = fclose int fd
3944r = rmdir pb 0 get pbn 0
3945if or eq r 0 eq r -2
3946ret
3947end
3948end
3949end
3950
3951note One directory entry at d[o]: remove it (recursing into directories). Returns the next offset.
3952fn rment d view uchar 8192 o ulong depth ulong keep bool to ulong
3953rl = or ulong get d add o 16 shl ulong get d add o 17 8
3954if eq rl 0
3955ret 8192
3956end
3957k = ulong 0
3958while ne get d add add o 19 k 0 bound 256
3959k = add k 1
3960end
3961if isdot d add o 19 k
3962ret add o rl
3963end
3964if and and keep eq depth 0 isname d add o 19 k
3965ret add o rl
3966end
3967sv = get pbn 0
3968pbyte 47
3969for i ulong k
3970pbyte get d add add o 19 i
3971end
3972if eq get d add o 18 4
3973rmt add depth 1 keep
3974else
3975r = unlink pb 0 get pbn 0
3976end
3977put pbn 0 sv
3978ret add o rl
3979end
3980
3981fn isdot d view uchar 8192 o ulong k ulong to bool
3982if ne get d o 46
3983ret false
3984end
3985if eq k 1
3986ret true
3987end
3988ret and eq k 2 eq get d add o 1 46
3989end
3990
3991fn isname d view uchar 8192 o ulong k ulong to bool
3992if ne k len u_mods
3993ret false
3994end
3995for i ulong k
3996if ne get d add o i get u_mods i
3997ret false
3998end
3999end
4000ret true
4001end
4002
4003note aienos uninstall [--keep-model] [--yes]
4004fn uninst keep bool yes bool to int
4005if not yes
4006emit u_sure
4007putb 58
4008putb 32
4009w = flush
4010n = rline
4011if not ibis t_yes
4012emit u_no
4013putb 10
4014w = flush
4015ret 0
4016end
4017end
4018if not estop
4019emit u_stop
4020putb 10
4021w = flush
4022ret 1
4023end
4024ulist
4025pbase
4026rmt 0 keep
4027emit u_done
4028putb 10
4029if keep
4030emit u_kept
4031putb 10
4032end
4033w = flush
4034ret 0
4035end
4036
4037note ---------- rollback ----------
4038
4039note aienos rollback: point current at the newest other release in releases/.
4040fn rollbk to int
4041best = new uchar 256 0
4042pbase
4043pstr r_cur
4044n = readlnk pb 0 get pbn 0 rb 0 512
4045put rbn 0 0
4046if gt n 0
4047put rbn 0 ulong n
4048end
4049bn = rpick best
4050if eq bn 0
4051emit r_none
4052putb 10
4053w = flush
4054ret 1
4055end
4056ret rswap best bn
4057end
4058
4059note Pick the greatest release name that is not the current target (rb holds the link text).
4060fn rpick best edit uchar 256 to ulong
4061d = new uchar 8192 0
4062bn = ulong 0
4063pbase
4064pstr r_rel
4065fd = fopen pb 0 get pbn 0 540672 0
4066if lt fd 0
4067ret 0
4068end
4069n = getdnts int fd d 0 8192
4070c = fclose int fd
4071if le n 0
4072ret 0
4073end
4074o = ulong 0
4075while lt o ulong n bound 8192
4076rl = or ulong get d add o 16 shl ulong get d add o 17 8
4077if eq rl 0
4078break
4079end
4080bn = rcand d add o 19 best bn
4081o = add o rl
4082end
4083ret bn
4084end
4085
4086note Consider entry name d[o..]: keep it in best when it is greater than best and not current.
4087fn rcand d view uchar 8192 o ulong best edit uchar 256 bn ulong to ulong
4088k = ulong 0
4089while and ne get d add o k 0 lt k 200 bound 256
4090k = add k 1
4091end
4092if eq get d o 46
4093ret bn
4094end
4095if and eq get rbn 0 add len r_pre k rbat len r_pre part d o k
4096ret bn
4097end
4098if not gtname d o k best bn
4099ret bn
4100end
4101for i ulong k
4102put best i get d add o i
4103end
4104ret k
4105end
4106
4107fn gtname d view uchar 8192 o ulong k ulong best view uchar 256 bn ulong to bool
4108for i ulong k
4109if ge i bn
4110ret true
4111end
4112a = get d add o i
4113b = get best i
4114if ne a b
4115ret gt a b
4116end
4117end
4118ret false
4119end
4120
4121fn rswap best view uchar 256 bn ulong to int
4122pkclr
4123pclr
4124pstr r_pre
4125for i ulong bn
4126pbyte get best i
4127end
4128pkpath
4129pbase
4130pstr r_new
4131x = unlink pb 0 get pbn 0
4132pkpath
4133r = symlnk pk 0 get pkn 0
4134if ne r 0
4135ret 1
4136end
4137pkclr
4138pbase
4139pstr r_new
4140pkpath
4141pbase
4142pstr r_cur
4143pkpath
4144r = renames pk 0 get pkn 0 0
4145if ne r 0
4146ret 1
4147end
4148emit r_ok
4149emit r_pre
4150for i ulong bn
4151putb get best i
4152end
4153putb 10
4154w = flush
4155ret 0
4156end
4157note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4158note main.om: the aienos command. With no argument (or "wizard") it runs the full-screen wizard;
4159note "doctor", "chat", "start", "stop", "verify", "source", "about", "uninstall" and "rollback" are the
4160note small commands beside it. Build: concatenate sha256.om lib.om hole.om doctor.om net.om steps.om
4161note engine.om main.om, in that order, and compile the result with the pinned omc (see build.sh).
4162
4163text v_tag "alpha-20261026.2"
4164text a_l1 "Sovereign Intelligence alpha-20261026.2: the installer and the aienos command"
4165text a_l2 "written in Omega (.om), compiled by omc sha256 befb8199db8c4bca188a494dfaac2a6f0ee5c36fcea11188c75666c55fce8a1d"
4166text a_l3 " (aien-dev/omega-mv 3364a1dfd0ca50c1b82bf19ba34932d8bc10a7a9, seed/out/omc)"
4167text a_l4 "engine: aien-serve-http sha256 19173f9ec3b609fe79af644f84741d8808b92ea8eb3ecec4bc945af6f22b3aeb,"
4168text a_l5 " the build that serves the Spark's 35B: aien-dev/aien-sovereign-core-mv branch ship/motor-l387"
4169text a_l6 " commit 59b05a71a33e4a2f1324db2f2b6407ee82b8c2c3 (a development build, not a Core release tag)"
4170text a_l7 " evidence: Core evidence/l387-motor-ship (merge train 20261022T0244Z-bfc41659, timing runs)"
4171text a_l8 "GPU path: the engine's statically linked libomega_gpu.a (omega ship/motor-l387 6de999da) talks to"
4172text a_l9 " /dev/nvidiactl, /dev/nvidia0 and /dev/nvidia-uvm. There is no separate CUDA++ runtime."
4173text a_l10 "model: Qwen/Qwen3.6-35B-A3B-FP8 revision 95a723d08a9490559dae23d0cff1d9466213d989 (37.5 GB)"
4174text a_l11 " This is the only model offered. A 27B is to come; it is not offered in this release."
4175text a_l12 "evidence: this engine binary serving the 35B on a GB10 EXERCISED (it is the live server's build);"
4176text a_l13 " this installer's GB10 install and first token, and the 3x physical qualification: NOT_RUN."
4177text a_l14 "signature: Ed25519 alpha release key ff949504b427680039d53c33c85c448546b57d590eb1ecee3bb2ed6c2eaaf54a"
4178text a_l15 " (an alpha key; it is rotated to a ceremony key before beta)"
4179text s_l1 "My Omega source ships with this release:"
4180text s_l2 "/share/aienos/wizard/"
4181text s_l3 " sha256.om lib.om hole.om doctor.om net.om steps.om engine.om main.om"
4182text s_l4 "/share/aienos/aienos.om"
4183text s_l5 " the same files concatenated in build order: one unit, one compile"
4184text s_l6 "Also online: https://www.aienos.com/alpha/ and https://github.com/aien-dev/sovereign-intelligence"
4185text w_l1 "Welcome. I am Drake, and this is Sovereign Intelligence, the AienOS alpha."
4186text w_l2 "This installer is written in Omega, my own language. It sets up a local model on"
4187text w_l3 "your DGX Spark that runs on my own engine: no CUDA toolkit, no libcuda, no cuBLAS."
4188text w_l4 "NVIDIA's kernel driver stays. Everything else NVIDIA ships stays installed, unused."
4189text w_l5 "Next: doctor, plan, your yes, install, model, first chat. Nothing changes before your yes."
4190text w_l6 "Type about for versions and evidence, source for my .om source, q to quit."
4191text w_go "Press Enter to run the doctor"
4192text x_l1 "This alpha runs on Linux arm64 with an NVIDIA GB10 (DGX Spark) only."
4193text x_l2 "Here is what is coming: https://www.aienos.com/alpha/"
4194text x_l3 "I changed nothing. The files the installer unpacked go away with: aienos uninstall"
4195text p_l1 "Here is exactly what changes, all inside your home folder:"
4196text p_l2 "/releases/"
4197text p_l3 " this release, already unpacked and checked"
4198text p_l4 "/models/"
4199text p_l5 "~/.local/bin/aienos, the menu entry and the desktop icon (already there)"
4200text p_l6 "/engine.log the engine's log, when it runs"
4201text p_l7 "Not touched: NVIDIA's driver, the CUDA toolkit, libcuda, cuBLAS, /usr, /etc. No sudo."
4202text p_l8 "Undo everything at any time: aienos uninstall"
4203text p_l9 " the model: "
4204text p_ok "Press Enter for the last question"
4205text c_l1 "Type yes to begin. Anything else stops here, and nothing changes."
4206text c_q "yes or no"
4207text c_stop "Stopped. Nothing was changed."
4208text c_fail "Stopped. Run aienos again once the problem above is fixed; it picks up where it left off."
4209text t_q "q"
4210text t_about "about"
4211text t_source "source"
4212text t_probe "AIENOS_PROBE_ROOT"
4213text t_home "HOME"
4214text t_plain "--plain"
4215text t_exe "/proc/self/exe"
4216text h_stty "stty"
4217text h_size "size"
4218text k_wiz "wizard"
4219text k_doc "doctor"
4220text k_chat "chat"
4221text k_start "start"
4222text k_stop "stop"
4223text k_vfy "verify"
4224text k_unin "uninstall"
4225text k_roll "rollback"
4226text k_help "usage: aienos [wizard [--plain] | doctor | chat | start | stop | verify | source | about | uninstall [--keep-model] [--yes] | rollback]"
4227text n_nohome "HOME is not set; I cannot tell where to install."
4228text n_badrt "I cannot find my own folder (/proc/self/exe)."
4229text n_vok " files match SHA256SUMS"
4230text n_vbad "a file does not match SHA256SUMS"
4231text n_start "engine started, pid "
4232text n_ready "engine ready on http://127.0.0.1:18100/v1"
4233text n_nostrt "engine did not become ready; see ~/.local/share/aienos/engine.log"
4234text n_stopd "engine stopped"
4235text n_chat1 "aienos chat: type a message and press Enter. /bye leaves."
4236text n_bye "/bye"
4237text n_you "you> "
4238text n_ai "model> "
4239text n_err "request failed: "
4240text t_sp " "
4241
4242note argument i into ib; returns its length or -1.
4243fn arg i ulong to long
4244if ge i ulong argcnt
4245ret -1
4246end
4247n = argread i ib 0 len ib
4248if lt n 0
4249ret -1
4250end
4251put ibn 0 ulong n
4252ret n
4253end
4254
4255note HOME, the probe prefix and this release's folder (the parent of bin/aienos).
4256fn setup to bool
4257sinit
4258n = envget t_home
4259if le n 0
4260ret false
4261end
4262for i ulong ulong n
4263put hb i get eb add len t_home add i 1
4264end
4265put hbn 0 ulong n
4266n = envget t_probe
4267if gt n 0
4268for i ulong ulong n
4269put pr i get eb add len t_probe add i 1
4270end
4271put prn 0 ulong n
4272end
4273ret selfdir
4274end
4275
4276fn selfdir to bool
4277n = readlnk t_exe 0 len t_exe rt 0 len rt
4278if or le n 0 ge n long len rt
4279ret false
4280end
4281k = ulong n
4282s = ulong 0
4283for pass ulong 2
4284while and gt k 0 ne get rt sub k 1 47 bound 1024
4285k = sub k 1
4286end
4287if gt k 0
4288k = sub k 1
4289end
4290end
4291put rtn 0 k
4292ret gt k 0
4293end
4294
4295note Terminal: animate when stdin and stdout are character devices and stty reports at least
4296note 64 columns and 22 rows; the picture takes what the 12-line panel leaves. The size comes from the host's stty (Omega has no window-size word yet).
4297fn term plain bool to void
4298put ui 0 0
4299put tw 0 80
4300put th 0 24
4301if plain
4302ret
4303end
4304r = fstat 0 sb 0 128
4305if or ne r 0 ne stype 2
4306ret
4307end
4308r = fstat 1 sb 0 128
4309if or ne r 0 ne stype 2
4310ret
4311end
4312if not findbin h_stty
4313ret
4314end
4315pkclr
4316pkpath
4317pkstr h_size
4318r = runcap
4319if ne r 0
4320ret
4321end
4322rows = rbnum 0
4323sp = rbfind 0 t_sp
4324cols = rbnum add sp 1
4325if or lt cols 64 lt rows 22
4326ret
4327end
4328if gt cols 400
4329cols = 400
4330end
4331put tw 0 cols
4332put th 0 rows
4333note The panel under the picture keeps 12 lines: the plan and the doctor must fit whole.
4334sh = ulong 6
4335if gt rows 22
4336sh = sub rows 16
4337end
4338if gt sh 30
4339sh = 30
4340end
4341put ui 1 sh
4342put ui 0 1
4343end
4344
4345fn plines to void
4346pl w_l1
4347pln
4348pln
4349pl w_l2
4350pln
4351pl w_l3
4352pln
4353pl w_l4
4354pln
4355pln
4356pl w_l5
4357pln
4358pc 5
4359pl w_l6
4360pc 2
4361end
4362
4363fn about to void
4364pl a_l1
4365pln
4366pl a_l2
4367pln
4368pl a_l3
4369pln
4370pl a_l4
4371pln
4372pl a_l5
4373pln
4374pl a_l6
4375pln
4376pl a_l7
4377pln
4378pl a_l8
4379pln
4380pl a_l9
4381pln
4382pl a_l10
4383pln
4384pl a_l11
4385pln
4386pl a_l12
4387pln
4388pl a_l13
4389pln
4390pl a_l14
4391pln
4392pl a_l15
4393end
4394
4395fn source to void
4396pl s_l1
4397pln
4398pc 6
4399plroot
4400pl s_l2
4401pc 2
4402pln
4403pl s_l3
4404pln
4405pc 6
4406plroot
4407pl s_l4
4408pc 2
4409pln
4410pl s_l5
4411pln
4412pln
4413pl s_l6
4414end
4415
4416fn plroot to void
4417for i ulong get rtn 0
4418pc get rt i
4419end
4420end
4421
4422note Welcome; loops on about and source. Returns false to quit.
4423fn stwel to bool
4424while true bound 1000
4425enter 0
4426pclear
4427plines
4428prompt w_go
4429show
4430r = ask
4431if lt r 0
4432ret false
4433end
4434if ibis t_q
4435ret false
4436end
4437if ibis t_about
4438sub1 true
4439end
4440if ibis t_source
4441sub1 false
4442end
4443if not or ibis t_about ibis t_source
4444ret true
4445end
4446end
4447ret false
4448end
4449
4450fn sub1 isab bool to void
4451pclear
4452if isab
4453about
4454else
4455source
4456end
4457prompt t_next
4458show
4459r = ask
4460end
4461
4462note Doctor screen. Returns true when the machine can take this alpha.
4463fn stdoc to bool
4464enter 1
4465pclear
4466need = mneed
4467ok = doctor need
4468for i ulong NCHK
4469dpanel i
4470end
4471if not ok
4472pln
4473pc 4
4474pl x_l1
4475pc 2
4476pln
4477pl x_l2
4478pln
4479pl x_l3
4480prompt t_close
4481show
4482r = ask
4483ret false
4484end
4485prompt t_next
4486show
4487r = ask
4488ret ge r 0
4489end
4490
4491note Bytes the model still needs on disk: none when verified, else its size plus 1 GB.
4492fn mneed to ulong
4493if not mopen
4494ret 0
4495end
4496if mdone
4497ret 0
4498end
4499ret add mbytes 1000000000
4500end
4501
4502fn dpanel i ulong to void
4503k = get dk i
4504if eq k 0
4505pc 3
4506pl k_ok
4507end
4508if eq k 1
4509pc 1
4510pl k_wn
4511end
4512if eq k 2
4513pc 4
4514pl k_no
4515end
4516if eq k 3
4517pc 5
4518pl k_in
4519end
4520pc 2
4521pc 32
4522n = get ptn 0
4523dnm i
4524while lt sub get ptn 0 n 9 bound 16
4525pc 32
4526end
4527for j ulong get dtn i
4528pc get dt add mul i 192 j
4529end
4530pln
4531end
4532
4533fn dnm i ulong to void
4534if eq i 0
4535pl n_arch
4536end
4537if eq i 1
4538pl n_gpu
4539end
4540if eq i 2
4541pl n_drv
4542end
4543if eq i 3
4544pl n_mem
4545end
4546if eq i 4
4547pl n_disk
4548end
4549if eq i 5
4550pl n_inst
4551end
4552if eq i 6
4553pl n_busy
4554end
4555if eq i 7
4556pl n_nv
4557end
4558end
4559
4560fn stplan to bool
4561enter 2
4562pclear
4563pl p_l1
4564pln
4565pln
4566pc 6
4567plbase
4568pl p_l2
4569pl v_tag
4570pc 2
4571pl p_l3
4572pln
4573pc 6
4574plbase
4575pl p_l4
4576for i ulong get mnn 0
4577pc get mnm i
4578end
4579pc 2
4580pln
4581pl p_l9
4582plgb mbytes
4583pl m_l3
4584plnum mfiles
4585pl m_l4
4586pln
4587pc 6
4588plbase
4589pc 2
4590pl p_l6
4591pln
4592pl p_l5
4593pln
4594pln
4595pl p_l7
4596pln
4597pc 1
4598pl p_l8
4599pc 2
4600prompt p_ok
4601show
4602r = ask
4603ret ge r 0
4604end
4605
4606fn styes to bool
4607enter 3
4608pclear
4609pc 6
4610pl c_l1
4611pc 2
4612prompt c_q
4613show
4614r = ask
4615ret and ge r 0 ibis t_yes
4616end
4617
4618fn bye t view uchar * to void
4619if eq get ui 0 1
4620scrn false
4621end
4622sgr0
4623putb 10
4624emit t
4625putb 10
4626w = flush
4627end
4628
4629note The full wizard. Exit 0 for a finished run and for a friendly refusal; 1 when a step failed.
4630fn wizard plain bool to int
4631term plain
4632if not mopen
4633ret 3
4634end
4635if eq get ui 0 1
4636scrn true
4637w = flush
4638end
4639if not stwel
4640bye c_stop
4641ret 0
4642end
4643if not stdoc
4644bye x_l3
4645ret 0
4646end
4647if not stplan
4648bye c_stop
4649ret 0
4650end
4651if not styes
4652bye c_stop
4653ret 0
4654end
4655if not stinst
4656bye c_fail
4657ret 1
4658end
4659if not mstep
4660bye c_fail
4661ret 1
4662end
4663if not stchat
4664bye c_fail
4665ret 1
4666end
4667stdone eq get ui 11 1
4668if eq get ui 11 1
4669bye d_l1
4670else
4671bye d_l0
4672end
4673ret 0
4674end
4675
4676fn istty fd int to bool
4677r = fstat fd sb 0 128
4678ret and eq r 0 eq stype 2
4679end
4680
4681fn cdoctor to int
4682need = mneed
4683ok = doctor need
4684col = istty 1
4685for i ulong NCHK
4686cline i col
4687putb 10
4688end
4689w = flush
4690if ok
4691ret 0
4692end
4693emit x_l1
4694putb 10
4695emit x_l2
4696putb 10
4697w = flush
4698ret 1
4699end
4700
4701fn cverify to int
4702x = mopen
4703k = sums
4704if lt k 0
4705emit n_vbad
4706putb 10
4707w = flush
4708ret 1
4709end
4710num ulong k
4711emit n_vok
4712putb 10
4713w = flush
4714ret 0
4715end
4716
4717note Start the engine when the doctor allows it and wait until it answers.
4718fn cstart to int
4719x = mopen
4720if alive PORT
4721emit n_ready
4722putb 10
4723w = flush
4724ret 0
4725end
4726ok = doctor 0
4727pclear
4728if ckskip
4729for i ulong get ptn 0
4730putb get pt i
4731end
4732putb 10
4733w = flush
4734ret 2
4735end
4736p = estart
4737if lt p 0
4738ret 1
4739end
4740emit n_start
4741num ulong p
4742putb 10
4743w = flush
4744if not ewait 2400
4745emit n_nostrt
4746putb 10
4747w = flush
4748ret 1
4749end
4750emit n_ready
4751putb 10
4752w = flush
4753ret 0
4754end
4755
4756fn cstop to int
4757if estop
4758emit n_stopd
4759putb 10
4760w = flush
4761ret 0
4762end
4763ret 1
4764end
4765
4766fn cchat to int
4767r = cstart
4768if ne r 0
4769ret r
4770end
4771emit n_chat1
4772putb 10
4773put hsn 0 0
4774while true bound 100000
4775emit n_you
4776w = flush
4777n = rline
4778if or lt n 0 ibis n_bye
4779ret 0
4780end
4781if gt get hsn 0 0
4782hstr j_comma
4783end
4784hstr j_user
4785hesc
4786hstr j_end
4787emit n_ai
4788w = flush
4789q = chatreq PORT
4790putb 10
4791w = flush
4792if ne q 0
4793emit n_err
4794snum q
4795putb 10
4796w = flush
4797else
4798hstr j_comma
4799hstr j_asst
4800for i ulong get abn 0
4801hbyte get ab i
4802end
4803hstr j_end
4804end
4805end
4806ret 0
4807end
4808
4809fn cprint to int
4810for i ulong get ptn 0
4811b = get pt i
4812if or ge b 7 eq b 10
4813putb b
4814end
4815end
4816putb 10
4817w = flush
4818ret 0
4819end
4820
4821fn iskey t view uchar * to bool
4822ret ibis t
4823end
4824
4825fn main to int
4826if not setup
4827emit n_nohome
4828putb 10
4829w = flush
4830ret 2
4831end
4832if lt arg 1 0
4833ret wizard false
4834end
4835if iskey k_wiz
4836plain = false
4837if ge arg 2 0
4838plain = ibis t_plain
4839end
4840ret wizard plain
4841end
4842if iskey k_doc
4843ret cdoctor
4844end
4845if iskey k_chat
4846ret cchat
4847end
4848if iskey k_start
4849ret cstart
4850end
4851if iskey k_stop
4852ret cstop
4853end
4854if iskey k_vfy
4855ret cverify
4856end
4857if iskey t_source
4858pclear
4859source
4860ret cprint
4861end
4862if iskey t_about
4863pclear
4864about
4865ret cprint
4866end
4867if iskey k_unin
4868ret cunin
4869end
4870if iskey k_roll
4871ret rollbk
4872end
4873emit k_help
4874putb 10
4875w = flush
4876ret 2
4877end
4878
4879fn cunin to int
4880keep = false
4881yes = false
4882for i ulong 2
4883if ge arg add i 2 0
4884if ibis u_keep
4885keep = true
4886end
4887if ibis u_yes
4888yes = true
4889end
4890end
4891end
4892x = mopen
4893ret uninst keep yes
4894end