1 # Mu: a human-scale computer
3 Mu is a minimal-dependency hobbyist computing stack (everything above the
6 Mu is not designed to operate in large clusters providing services for
7 millions of people. Mu is designed for _you_, to run one computer. (Or a few.)
8 Running the code you want to run, and nothing else.
10 Here's the Mu computer running [Conway's Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life).
13 git clone https://github.com/akkartik/mu
15 ./translate apps/life.mu # emit a bootable code.img
16 qemu-system-i386 code.img
19 <img alt='screenshot of Game of Life running on the Mu computer' src='html/life.png'>
21 ([Colorized sources.](http://akkartik.github.io/mu/html/apps/life.mu.html)
22 This is memory-safe code, and most statements map to a single instruction of
25 Rather than start from some syntax and introduce layers of translation to
26 implement it, Mu starts from the processor's instruction set and tries to get
27 to _some_ safe and clear syntax with as few layers of translation as possible.
28 The emphasis is on internal consistency at any point in time rather than
29 compatibility with the past. ([More details.](http://akkartik.name/akkartik-convivial-20200607.pdf))
31 Tests are a key mechanism here for creating a computer that others can make
32 their own. I want to encourage a style of active and interactive reading with
33 Mu. If something doesn't make sense, try changing it and see what tests break.
34 Any breaking change should cause a failure in some well-named test somewhere.
36 Currently Mu requires a 32-bit x86 processor. It supports a short list of
37 generic hardware, and can't do networking yet.
43 - [Reward curiosity.](http://akkartik.name/about)
44 - Easy to build, easy to run. [Minimal dependencies](https://news.ycombinator.com/item?id=16882140#16882555),
45 so that installation is always painless.
46 - All design decisions comprehensible to a single individual. (On demand.)
47 - All design decisions comprehensible without needing to talk to anyone.
48 (I always love talking to you, but I try hard to make myself redundant.)
49 - [A globally comprehensible _codebase_ rather than locally clean code.](http://akkartik.name/post/readable-bad)
50 - Clear error messages over expressive syntax.
52 - Thorough test coverage. If you break something you should immediately see
54 - Memory leaks over memory corruption.
55 - Teach the computer bottom-up.
57 Thorough test coverage in particular deserves some elaboration. It implies
58 that any manual test should be easy to turn into a reproducible automated
59 test. Mu has some unconventional methods for providing this guarantee. It
60 exposes testable interfaces for hardware using dependency injection so that
61 tests can run on -- and make assertions against -- fake hardware. It also
62 performs [automated white-box testing](http://akkartik.name/post/tracing-tests)
63 which enables robust tests for performance, concurrency, fault-tolerance, etc.
67 - Speed. Staying close to machine code should naturally keep Mu fast enough.
68 - Efficiency. Controlling the number of abstractions should naturally keep Mu
69 using far less than the gigabytes of memory modern computers have.
70 - Portability. Mu will run on any computer as long as it's x86. I will
71 enthusiastically contribute to support for other processors -- in separate
72 forks. Readers shouldn't have to think about processors they don't have.
73 - Compatibility. The goal is to get off mainstream stacks, not to perpetuate
74 them. Sometimes the right long-term solution is to [bump the major version number](http://akkartik.name/post/versioning).
75 - Syntax. Mu code is meant to be comprehended by [running, not just reading](http://akkartik.name/post/comprehension).
76 It will always be just a thin memory-safe veneer over machine code.
77 I don't know how to make higher-level notations both fast and
78 comprehensible, so they are likely to remain slow and comprehensible, useful
79 for prototyping but invariably needing to be rewritten in statements that
80 map 1:1 with machine code. The goal of a prototype should be a risk-free
81 rewrite, thanks to tests that capture all the details of lessons learned.
85 The Mu stack consists of:
86 - the Mu type-safe and memory-safe language;
87 - SubX, an unsafe notation for a subset of x86 machine code; and
88 - _bare_ SubX, a more rudimentary form of SubX without certain syntax sugar.
90 All Mu programs get translated through these layers into tiny zero-dependency
91 binaries that run natively. The translators for most levels are built out of
92 lower levels. The translator from Mu to SubX is written in SubX, and the
93 translator from SubX to bare SubX is built in bare SubX. There is also an
94 emulator for Mu's supported subset of x86, that's useful for [debugging SubX
95 programs](linux/subx_debugging.md).
97 Mu programs build natively either on Linux or on Windows using [WSL 2](https://docs.microsoft.com/en-us/windows/wsl/install-win10).
98 For Macs and other Unix-like systems, use the (much slower) emulator:
101 ./translate_emulated apps/ex2.mu # ~2 mins to emit code.img
104 Mu programs can be written for two very different environments:
106 * At the top-level, Mu programs emit a bootable image that runs without an OS
107 (under emulation; I haven't tested on native hardware yet). There's rudimentary
108 support for some core peripherals: a 1024x768 screen, a keyboard with some
109 key-combinations, a PS/2 mouse that must be polled, a slow ATA disk drive.
110 No hardware acceleration, no virtual memory, no process separation, no
111 multi-tasking, no network. Boot always runs all tests, and only gets to
112 `main` if all tests pass.
114 * The top-level is built using tools created under the `linux/` sub-directory.
115 This sub-directory contains an entirely separate set of libraries intended
116 for building programs that run with just a Linux kernel, reading from stdin
117 and writing to stdout. The Mu compiler is such a program, at `linux/mu.subx`.
118 Individual programs typically run tests if given a command-line argument
121 The largest program built in Mu today is its prototyping environment for
122 writing slow, interpreted programs in a Lisp-based high-level language.
124 <img alt='screenshot of the Mu shell' src='html/20210624-shell.png'>
126 (For more details, see [the `shell/` directory.](https://github.com/akkartik/mu/tree/main/shell#readme))
128 While I currently focus on programs without an OS, the `linux/` sub-directory
129 is fairly ergonomic. There's a couple of dozen example programs to try out
130 there. It is likely to be the option for a network stack in the foreseeable
131 future; I have no idea how to interact on the network without Linux.
135 The entire stack shares certain properties and conventions. Programs consist
136 of functions and functions consist of statements, each performing a single
137 operation. Operands to statements are always variables or constants. You can't
138 perform `a + b*c` in a single statement; you have to break it up into two.
139 Variables can live in memory or in registers. Registers must be explicitly
140 specified. There are some shared lexical rules. Comments always start with
141 '#'. Numbers are always written in hex. Many terms can have context-dependent
142 _metadata_ attached after '/'.
144 Here's an example program in Mu:
146 <img alt='ex2.mu' src='html/ex2.mu.png' width='400px'>
148 More resources on Mu:
150 * [Mu Syntax reference](mu.md)
152 * [Library reference.](vocabulary.md) Mu programs can transparently call
153 low-level functions written in SubX.
155 Here's an example program in SubX:
161 bb/copy-to-ebx 1/imm32
165 e8/call syscall_exit/disp32
168 More resources on SubX:
170 * [SubX syntax reference](subx.md)
172 * [Some starter exercises for learning SubX](https://github.com/akkartik/mu/pulls)
173 (labelled `hello`). Feel free to [ping me](mailto:ak@akkartik.com) with any
176 * The [list of x86 opcodes](subx_opcodes) supported in SubX: `linux/bootstrap/bootstrap help opcodes`.
178 * [Some tips for debugging SubX programs.](linux/subx_debugging.md)
182 Forks of Mu are encouraged. If you don't like something about this repo, feel
183 free to make a fork. If you show it to me, I'll link to it here. I might even
184 pull features upstream!
186 - [uCISC](https://github.com/grokthis/ucisc): a 16-bit processor being
187 designed from scratch by [Robert Butler](https://www.youtube.com/channel/UCh4OpfF7T7UtezGejRTLxCw)
188 and programmed with a SubX-like syntax.
189 - [subv](https://git.s-ol.nu/subv): experimental SubX-like syntax by [s-ol
190 bekic](https://mmm.s-ol.nu) for the RISC-V instruction set.
191 - [mu-x86\_64](https://git.sr.ht/~akkartik/mu-x86_64): experimental fork for
192 64-bit x86 in collaboration with [Max Bernstein](https://bernsteinbear.com).
193 It's brought up a few concrete open problems that I don't have good solutions
195 - [mu-normie](https://git.sr.ht/~akkartik/mu-normie): with a more standard
196 build system for the `linux/bootstrap/` directory that organizes the repo by
197 header files and compilation units. Stays in sync with this repo.
201 If you're still reading, here are some more things to check out:
203 - [How to get your text editor set up for Mu and SubX programs.](editor/editor.md)
205 - [Videos demonstrating Mu programs and features.](https://archive.org/details/@kartik_agaram)
207 - [A summary](mu_instructions) of how the Mu compiler translates statements
208 to SubX. Most Mu statements map to a single x86 instruction.
209 ([colorized version](http://akkartik.github.io/mu/html/mu_instructions.html))
211 - A prototype live-updating programming environment for a postfix language
212 that I might work on again one day:
216 ./translate tile/*.mu
220 - Previous prototypes: [mu0](https://github.com/akkartik/mu0), [mu1](https://github.com/akkartik/mu1).
224 Mu builds on many ideas that have come before, especially:
226 - [Peter Naur](http://akkartik.name/naur.pdf) for articulating the paramount
227 problem of programming: communicating a codebase to others;
228 - [Christopher Alexander](http://www.amazon.com/Notes-Synthesis-Form-Harvard-Paperbacks/dp/0674627512)
229 and [Richard Gabriel](https://www.dreamsongs.com/Files/PatternsOfSoftware.pdf) for
230 the intellectual tools for reasoning about the higher order design of a
232 - [David Parnas](http://www.cs.umd.edu/class/spring2003/cmsc838p/Design/criteria.pdf)
233 and others for highlighting the value of separating concerns and stepwise
235 - The folklore of debugging by print and the trace facility in many Lisp
237 - Automated tests for showing the value of developing programs inside an
240 On a more tactical level, this project has made progress in a series of bursts
241 as I discovered the following resources. In autobiographical order, with no
242 claims of completeness:
243 - [“Bootstrapping a compiler from nothing”](http://web.archive.org/web/20061108010907/http://www.rano.org/bcompiler.html) by Edmund Grumley-Evans.
244 - [StoneKnifeForth](https://github.com/kragen/stoneknifeforth) by [Kragen Sitaker](http://canonical.org/~kragen),
245 including [a tiny sketch of an ELF loader](https://github.com/kragen/stoneknifeforth/blob/master/386.c).
246 - [“Creating tiny ELF executables”](https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html) by Brian Raiter.
247 - [Single-page cheatsheet for the x86 ISA](https://net.cs.uni-bonn.de/fileadmin/user_upload/plohmann/x86_opcode_structure_and_instruction_overview.pdf)
248 by Daniel Plohmann ([cached local copy](https://github.com/akkartik/mu/blob/main/cheatsheet.pdf))
249 - [Minimal Linux Live](http://minimal.linux-bg.org) for teaching how to create
250 a bootable disk image using the syslinux bootloader.
251 - [“Writing a bootloader from scratch”](https://www.cs.bham.ac.uk/~exr/lectures/opsys/10_11/lectures/os-dev.pdf)
253 - Wikipedia on BIOS interfaces: [Int 10h](https://en.wikipedia.org/wiki/INT_10H), [Int 13h](https://en.wikipedia.org/wiki/INT_13H).
254 - [Some tips on programming bootloaders](https://stackoverflow.com/questions/43786251/int-13h-42h-doesnt-load-anything-in-bochs/43787939#43787939)
256 - [xv6, the port of Unix Version 6 to x86 processors](https://github.com/mit-pdos/xv6-public)
257 - Some tips on handling keyboard interrupts by [Alex Dzyoba](https://alex.dzyoba.com/blog/os-interrupts)
258 and [Michael Petch](https://stackoverflow.com/questions/37618111/keyboard-irq-within-an-x86-kernel).