https://github.com/akkartik/mu/blob/main/shell/main.mu
1
2
3
4 fn main screen: (addr screen), keyboard: (addr keyboard), data-disk: (addr disk) {
5 var globals-storage: global-table
6 var globals/edi: (addr global-table) <- address globals-storage
7 initialize-globals globals
8 var sandbox-storage: sandbox
9 var sandbox/esi: (addr sandbox) <- address sandbox-storage
10 initialize-sandbox sandbox, 1/with-screen
11 load-state data-disk, sandbox, globals
12 $main:loop: {
13
14
15
16 render-globals screen, globals
17 render-sandbox screen, sandbox, 0x55/sandbox-left-margin, 0/sandbox-top-margin, 0x80/screen-width, 0x2f/screen-height-without-menu
18 {
19 var key/eax: byte <- read-key keyboard
20 compare key, 0
21 loop-if-=
22
23 {
24 compare key, 0x12/ctrl-r
25 break-if-!=
26 var tmp/eax: (addr handle cell) <- copy 0
27 var nil: (handle cell)
28 tmp <- address nil
29 allocate-pair tmp
30
31
32 var real-keyboard: (handle cell)
33 tmp <- address real-keyboard
34 allocate-keyboard tmp
35
36 var args: (handle cell)
37 tmp <- address args
38 new-pair tmp, real-keyboard, nil
39
40 var real-screen: (handle cell)
41 tmp <- address real-screen
42 allocate-screen tmp
43
44 tmp <- address args
45 new-pair tmp, real-screen, *tmp
46
47 var main: (handle cell)
48 tmp <- address main
49 new-symbol tmp, "main"
50
51 tmp <- address args
52 new-pair tmp, main, *tmp
53
54 clear-screen screen
55 set-cursor-position screen, 0, 0
56
57 var out: (handle cell)
58 var out-ah/ecx: (addr handle cell) <- address out
59 evaluate tmp, out-ah, nil, globals, 0/trace, 0/no-fake-screen, 0/no-fake-keyboard, 0/call-number
60 {
61 var tmp/eax: byte <- read-key keyboard
62 compare tmp, 0
63 loop-if-=
64 }
65
66 loop $main:loop
67 }
68
69 edit-sandbox sandbox, key, globals, data-disk, screen, 1/tweak-real-screen
70 }
71 loop
72 }
73 }
74
75
76 fn load-state data-disk: (addr disk), _sandbox: (addr sandbox), globals: (addr global-table) {
77 var sandbox/eax: (addr sandbox) <- copy _sandbox
78 var data-ah/eax: (addr handle gap-buffer) <- get sandbox, data
79 var _data/eax: (addr gap-buffer) <- lookup *data-ah
80 var data/esi: (addr gap-buffer) <- copy _data
81
82 var s-storage: (stream byte 0x1000)
83 var s/ebx: (addr stream byte) <- address s-storage
84 load-sectors data-disk, 0/lba, 8/sectors, s
85
86
87 load-gap-buffer-from-stream data, s
88 clear-stream s
89
90 var initial-root-storage: (handle cell)
91 var initial-root/ecx: (addr handle cell) <- address initial-root-storage
92 read-cell data, initial-root, 0/no-trace
93 clear-gap-buffer data
94
95 {
96 var initial-root-addr/eax: (addr cell) <- lookup *initial-root
97 compare initial-root-addr, 0
98 break-if-!=
99 return
100 }
101
102 var globals-literal-storage: (handle cell)
103 var globals-literal-ah/eax: (addr handle cell) <- address globals-literal-storage
104 new-symbol globals-literal-ah, "globals"
105 var globals-literal/eax: (addr cell) <- lookup *globals-literal-ah
106 var globals-cell-storage: (handle cell)
107 var globals-cell-ah/edx: (addr handle cell) <- address globals-cell-storage
108 lookup-symbol globals-literal, globals-cell-ah, *initial-root, 0/no-globals, 0/no-trace, 0/no-screen, 0/no-keyboard
109 var globals-cell/eax: (addr cell) <- lookup *globals-cell-ah
110 {
111 compare globals-cell, 0
112 break-if-=
113 load-globals globals-cell-ah, globals
114 }
115
116 var sandbox-literal-storage: (handle cell)
117 var sandbox-literal-ah/eax: (addr handle cell) <- address sandbox-literal-storage
118 new-symbol sandbox-literal-ah, "sandbox"
119 var sandbox-literal/eax: (addr cell) <- lookup *sandbox-literal-ah
120 var sandbox-cell-storage: (handle cell)
121 var sandbox-cell-ah/edx: (addr handle cell) <- address sandbox-cell-storage
122 lookup-symbol sandbox-literal, sandbox-cell-ah, *initial-root, 0/no-globals, 0/no-trace, 0/no-screen, 0/no-keyboard
123 var sandbox-cell/eax: (addr cell) <- lookup *sandbox-cell-ah
124 {
125 compare sandbox-cell, 0
126 break-if-=
127
128 print-cell sandbox-cell-ah, s, 0/no-trace
129
130 load-gap-buffer-from-stream data, s
131 }
132 }
133
134
135
136
137
138 fn store-state data-disk: (addr disk), sandbox: (addr sandbox), globals: (addr global-table) {
139 compare data-disk, 0/no-disk
140 {
141 break-if-!=
142 return
143 }
144 var stream-storage: (stream byte 0x1000)
145 var stream/edi: (addr stream byte) <- address stream-storage
146 write stream, "(\n"
147 write-globals stream, globals
148 write-sandbox stream, sandbox
149 write stream, ")\n"
150 store-sectors data-disk, 0/lba, 8/sectors, stream
151 }