Modular Computers the manual GitHub

Booting your own code

The smallest operating system there is: it writes a line on the screen, waits five seconds, and turns the computer off. Small as it is, the computer runs nothing else: no mcos, no shell.

What you need

The whole thing

Your OS is one file, /init.lua, on a disk of its own:

-- /init.lua: the smallest operating system. The EEPROM's bootloader found this file on a disk,
-- ran it, and passed it the disk's address. It writes to the screen, waits a few seconds, and
-- turns the computer off.
local disk = ...  -- the boot disk's address (sata:0, say): what the bootloader passes

local SECONDS = 5 -- how long the message stays before the computer turns off

-- "console" is every screen on the bus at once. A terminal needs \r\n to start a new line.
board.invoke("console", "write", "Hello from my own OS, booted from " .. disk .. "\r\n")

-- Waiting for an interrupt from no device is a sleep: nothing wakes it until the time is up.
board.waitForInterrupt(SECONDS, "nothing")
board.poweroff()

Download os-hello.lua

The steps:

  1. Craft the second disk alone in the crafting grid: it comes out blank (no mcos, no files).
  2. Put it in the computer’s second SATA socket, and power on. mcos boots from the first disk and shows the new one at /mnt/sata/1.
  3. Write the file: edit /mnt/sata/1/init.lua, type the program in, Ctrl+O to save, Ctrl+X to leave.
  4. Turn the computer off, take the mcos disk out, and put your disk in the first SATA socket.
  5. Power on. You see Hello from my own OS, booted from sata:0, and five seconds later the computer turns off.

Step by step

1. A blank disk

A disk crafted at the table comes with mcos on it. Crafted again alone, it comes out blank: an empty filesystem (hfs), no files at all. That’s what you want, so nothing of mcos is left to confuse you. (A disk that still has mcos would work too: your /init.lua would replace its kernel, and the rest of its files would just sit there.)

2. Write /init.lua

With your disk in the second SATA socket, mcos mounts it at /mnt/sata/1. Pick one way to put the file there:

Check it’s there: cat /mnt/sata/1/init.lua.

3. Boot it

Turn the computer off. Take the mcos disk out, and move your disk to the first SATA socket. Power on.

The EEPROM’s bootloader looks at the disks in bus order, finds /init.lua on yours, and runs it. You see:

Hello from my own OS, booted from sata:0

Five seconds later, the computer turns off.

To go back to mcos, put the mcos disk back in the first socket. To keep both, leave mcos first and your disk second while you work on your OS: the bootloader boots the first disk that has /init.lua, so the order of the sockets decides which OS starts.

What each line does

When it goes wrong

Next: Reading the keyboard.