interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / aienux / kernel / core / klog.om

153 lines · 2668 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/kernel/core/klog.om: Pure-Omega RAM Console Log (head + tail ring with drop marker).
3note Converted from upstream aienux native/kernel/core/klog.h and native/kernel/core/klog.c.
4
5const KLOG_HEAD_CAP ulong 4096
6const KLOG_TAIL_CAP ulong 1024
7
8note klog_meta: 0=head_len, 1=tail_put, 2=total, 3=head_cap, 4=tail_cap
9global klog_meta ulong 5
10global klog_head uchar 4096
11global klog_tail uchar 1024
12
13text klog_drop_pre "\n[klog: "
14text klog_drop_suf " bytes dropped]\n"
15
16fn klog_clear_caps hcap ulong tcap ulong to void
17hc = hcap
18tc = tcap
19if or eq hc 0 gt hc KLOG_HEAD_CAP
20hc = KLOG_HEAD_CAP
21end
22if or eq tc 0 gt tc KLOG_TAIL_CAP
23tc = KLOG_TAIL_CAP
24end
25put klog_meta 0 0
26put klog_meta 1 0
27put klog_meta 2 0
28put klog_meta 3 hc
29put klog_meta 4 tc
30end
31
32fn klog_clear to void
33klog_clear_caps KLOG_HEAD_CAP KLOG_TAIL_CAP
34end
35
36fn klog_put src view uchar * to void
37n = len src
38hc = get klog_meta 3
39tc = get klog_meta 4
40if eq hc 0
41klog_clear
42hc = KLOG_HEAD_CAP
43tc = KLOG_TAIL_CAP
44end
45put klog_meta 2 add get klog_meta 2 n
46hlen = get klog_meta 0
47tput = get klog_meta 1
48i = ulong 0
49while lt i n bound 65536
50ch = get src i
51if lt hlen hc
52put klog_head hlen ch
53hlen = add hlen 1
54else
55put klog_tail rem tput tc ch
56tput = add tput 1
57end
58i = add i 1
59end
60put klog_meta 0 hlen
61put klog_meta 1 tput
62end
63
64fn klog_total to ulong
65ret get klog_meta 2
66end
67
68fn klog_dropped to ulong
69tput = get klog_meta 1
70tc = get klog_meta 4
71if gt tput tc
72ret sub tput tc
73end
74ret 0
75end
76
77fn klog_write_dec dst edit uchar * pos ulong cap ulong val ulong to ulong
78digits = new uchar 24 0
79nd = ulong 0
80v = val
81if eq v 0
82put digits 0 48
83nd = ulong 1
84else
85while gt v 0 bound 24
86put digits nd uchar add 48 rem v 10
87v = div v 10
88nd = add nd 1
89end
90end
91w = pos
92i = nd
93while gt i 0 bound 24
94i = sub i 1
95if lt w cap
96put dst w get digits i
97w = add w 1
98end
99end
100ret w
101end
102
103fn klog_copy_text dst edit uchar * pos ulong cap ulong src view uchar * to ulong
104w = pos
105n = len src
106i = ulong 0
107while lt i n bound 64
108if ge w cap
109break
110end
111put dst w get src i
112w = add w 1
113i = add i 1
114end
115ret w
116end
117
118fn klog_read dst edit uchar * to ulong
119cap = len dst
120hlen = get klog_meta 0
121tput = get klog_meta 1
122tc = get klog_meta 4
123w = ulong 0
124i = ulong 0
125while lt i hlen bound 4096
126if ge w cap
127break
128end
129put dst w get klog_head i
130w = add w 1
131i = add i 1
132end
133kept = tput
134if gt kept tc
135kept = tc
136end
137if gt tput tc
138w = klog_copy_text dst w cap klog_drop_pre
139w = klog_write_dec dst w cap sub tput tc
140w = klog_copy_text dst w cap klog_drop_suf
141end
142start = sub tput kept
143j = start
144while lt j tput bound 1024
145if ge w cap
146break
147end
148put dst w get klog_tail rem j tc
149w = add w 1
150j = add j 1
151end
152ret w
153end