# TempleOS — Static Recompilation Static recompilation of **TempleOS** V5.03, the final release of the operating system Terry A. Davis (1969–2018) wrote, from the machine code on its ISO to native C. TempleOS runs as an ordinary program on Windows and Linux, with no emulator and no virtual machine. It is a complete 64-bit operating system written by one person: its kernel, its language (HolyC) and that language's compiler, its document format and editor, its window manager, 2D and 3D graphics, sound, games and the documentation for all of it were designed and written by Terry A. Davis over more than a decade. This project takes the system exactly as he shipped it and translates its machine code to C so that it can keep running on modern machines. Built on the [pcrecomp](https://github.com/sp00nznet/pcrecomp) toolchain, on its `temple` branch, and following its house style. TempleOS boots from a CD into 64-bit long mode, runs everything in ring 0 in one identity-mapped address space, and compiles most of itself from HolyC source while it boots, so the project has three parts: the kernel (`0000Kernel.BIN.C` on the disc), lifted by pcrecomp's `lift64_sys`, an x86-64 lifter for operating-system code (port I/O, interrupts, MSRs, descriptor tables, IRETQ, the x87 FPU), with the 16- and 32-bit start-up that talks to the BIOS reproduced in C; a small model of the PC in C (timer and interrupt controllers, the keyboard and mouse controller, the CMOS clock, VGA into an SDL2 window, an ATAPI CD-ROM that serves your ISO read-only, and an HPET); and the code made at run time (`Compiler.BIN`, and everything the HolyC compiler produces), captured when it first appears, lifted, and checked byte for byte before its lifted form is used. New code that was never captured stops the run with a message; nothing is ever interpreted or executed from guest memory. The details, with diagrams, are in [docs/architecture.md](docs/architecture.md). ![TempleOS V5.03 running lifted to C: the boot questions, a HolyC expression and Dir at the shell, then Squirt](docs/screenshots/hero.gif) ## Getting Started You need **TempleOS V5.03's `TempleOS.ISO`**, preserved at [templeos.org](https://templeos.org/) and in the [Internet Archive](https://archive.org/search?query=TempleOS). Nothing from TempleOS is in this repository or downloaded for you; everything is built on your machine from your ISO. ### The installer (Windows) 1. Download `TempleOS-Recomp-Installer-windows-x64.exe` from the **[latest release](https://github.com/sp00nznet/templeos/releases)**. 2. Open it and point it at the folder holding your `TempleOS.ISO`. It needs Python 3.10 or newer, CMake, and Visual Studio 2022 or its Build Tools with Desktop development with C++. 3. Build, and when it's done, **Play**. ### The setup scripts **Windows:** double-click `Setup.cmd`. It checks the tools (Python 3.10+ with `capstone`, CMake, Visual Studio 2022 with the C++ tools, offering to install Python and CMake with winget), clones pcrecomp's `temple` branch beside this folder when it is missing, gets SDL2 (through vcpkg when present, otherwise SDL's development package), asks for the ISO (`-Iso` takes its path or its folder), lifts, builds the capture cache (hidden, muted runs), and leaves `TempleOS.cmd` to start it. A rerun skips finished steps (`-Force` redoes them); the details go to `setup.log`. **Linux / macOS:** `./setup.sh /path/to/TempleOS.ISO`. It needs Python 3 with `capstone`, CMake, a C compiler and SDL2 (Debian/Ubuntu: `apt install libsdl2-dev`; macOS: `brew install sdl2`), and clones pcrecomp's `temple` branch when it is missing. macOS has not been built yet. ### By hand You need Python 3.10+ with `capstone` (`pip install capstone`), CMake, a C compiler and SDL2, and a checkout of pcrecomp's `temple` branch next to this folder (`../pcrecomp-temple`, or set `PCRECOMP`). python tools/gen.py TempleOS.ISO # lift into src/gen/ cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=/scripts/buildsystems/vcpkg.cmake python tools/capture.py TempleOS.ISO # lift, build, run hidden, repeat build/Release/templeos TempleOS.ISO # then run it ### Command line `--hidden` (no window), `--sound` (the PC speaker), `--shot out.png SECONDS`, `--frames DIR START EVERY` (numbered PNGs), `--quit SECONDS`, `--keys TEXT SECONDS` (types TEXT, `\n` is Enter; `mouse:DX,DY,BUTTONS` moves or clicks), `--trace`. `TEMPLE_TRACE_FN=` logs calls to one guest function. ## Status **v0.1.0, early.** The kernel boots, the HolyC compiler and the code it makes are lifted as they appear, and StartOS runs through to the Adam desktop, where commands typed at the shell run. Squirt, from the disc's `Demo/Games` folder, runs on Windows and on Linux. Code that has not been captured yet stops the run with a message. | Stage | State | |---|---| | ISO read (RedSea, TempleOS compression) | done | | Kernel lifted and booting: KMain, memory, interrupts, timer, drive detection | done - boot text on screen | | `Load("Compiler")`: the HolyC compiler, captured and lifted | done - verified byte for byte on Windows and Linux | | StartOS: the compiler's output, captured and lifted as it boots | done - about 1,100 distinct blocks | | Adam, the window manager, the shell | done - the desktop in 16 colors; commands typed at the shell run | | Keyboard, mouse, a demo or game from the disc | keyboard done; Squirt from Demo/Games runs; mouse packets scripted (`mouse:` events) | | Sound (PC speaker) | square wave with `--sound` (off by default, never in hidden runs) | | Builds | Windows (MSVC, Visual Studio 2022) and Linux (gcc, tested on Debian under WSL); macOS through the same CMake, not yet built | | Setup | `Setup.cmd` and the Windows installer (v0.1.0 release); `setup.sh` for Linux and macOS | The handoff log in [docs/survey.md](docs/survey.md) records each step and what it took, and [ROADMAP.md](ROADMAP.md) what comes next. ## Screenshots Real output of the recompiled system: | | | |---|---| | ![The Adam desktop of TempleOS V5.03, running lifted to C](docs/screenshots/desktop.png) | ![Squirt, from TempleOS's Demo/Games folder](docs/screenshots/squirt.png) | | The Adam desktop after StartOS: the boot questions answered from the keyboard, a HolyC expression (`F64 x=6*7;x/4;`) and `Dir;` run at the shell. Every instruction on screen is TempleOS's own code, translated ahead of time. | Squirt, one of the games that ship on the disc, compiled by TempleOS's own HolyC compiler at run time and running from its lifted output. | | ![The boot screen on Windows](docs/screenshots/first-light.png) | ![The boot screen on Linux](docs/screenshots/linux-first-light.png) | | The lifted kernel's boot screen on Windows (MSVC). | The same boot screen on Linux (gcc, Debian under WSL). | | ![Squirt on Linux](docs/screenshots/linux-squirt.png) | | | Squirt on Linux, from the capture cache made on Windows. | | ## Documentation - [docs/architecture.md](docs/architecture.md): how it works, with diagrams: the lift, booting without a BIOS, interrupts and task switches, code made at run time, the machine model, building - [docs/survey.md](docs/survey.md): the ISO, the boot path, what the kernel touches, where code is made at run time, the reasoning behind each choice, and the handoff log - [ROADMAP.md](ROADMAP.md), [CHANGELOG.md](CHANGELOG.md), [CONTRIBUTING.md](CONTRIBUTING.md) The code: | Path | What | |---|---| | `runtime/` | the host side: boot (`main.c`), CPU system calls and interrupts (`machine.c`), task switches (`tasks.c`), devices (`devices.c`), the window and PNG writer (`present.c`), dispatch and capture | | `tools/` | ISO readers (`redsea.py`, `tosz.py`, `extract.py`), `.BIN` modules (`tosbin.py`), function discovery, the generator (`gen.py`) and the capture loop (`capture.py`) | | `docs/` | the survey, the architecture, screenshots | | `src/gen/`, `work/` | generated from your ISO; not in the repository | ## License MIT for this repository's own code ([LICENSE](LICENSE)). TempleOS is the work of Terry A. Davis, who placed it in the public domain; its source is part of the disc itself. Everything that runs here is his code: this repository adds only the translation and a model of the PC it expects. None of TempleOS's files, and nothing generated from them, is in this repository; you supply the ISO ([NOTICE.md](NOTICE.md)).