An operating system for Tetris?

HalOS Reflection

An operating system for Tetris?

HalOS running a Tetris clone via emulation

Background

Some time back YouTube suggested a video with the title I made an entire OS that only runs Tetris. The video was posted on April 18th, 2021. This caught my attention because this was what I did for my senior capstone project while in college. I did my presentation 20+ years ago.

This experience really opens my mind on just how different the world is today. Knowledge is everywhere to be found. For perspective here are some key moments in the history of the internet that are relevant for the context of my project. I did my work in 2001/2002.

  • Windows XP was released in 2001
  • First video card with a programmable pixel shader was released in 2001
  • Firefox was released in 2002
  • Git was created in 2005
  • Youtube was founded in 2005
  • Github was founded in 2008
  • Stack Overflow was founded in 2008

I am absolutely jealous of the amazing resources that are available today.

Project HalOS

After trying to figure out exactly what to do for a capstone project I finally decided to write an operating system from scratch. I loved my classes in the subject matter and really enjoyed interacting with hardware at that intimate level.

The project is named after Hal the computer from A Space Odyssey.

My goal was not to build Tetris, or any game for that matter. I had perceived games as an ideal candidate as it would be the most performant way to run PC games (i.e. consoles). In the lead up to assembling my presentation I realized that I needed something people would understand. Notably my parents would be at the presentation and I needed something for them to relate to. While inserting my 3.5 inch floppy and watching the computer boot was still mesmerizing for me, it would fall flat otherwise.

So to show off the system I had the following items in the demo:

  • A simple counter in the upper left corner of the screen that would increment every second.
  • The current temperature in the upper right corner. This was getting its data form an RS-232 port from another students project.
  • Tetris in the middle of the screen.

I needed to show true multitasking and this seemed to have gone over well.

Features

Runtime

Before .NET and Java we already had a portable runtime. The C runtime. For my Operating System I wanted to be able to support the standard C runtime library so I implemented a handful of key items. Here is an example of using the HalOS API and the C runtime.

Halos API

void Task1(void){
    ConsoleWrite(hStdout,Hello World!, Press any key to continue.\n);
    KeyBoardGetCharacter(hStdin);
    TaskClose();
}

C Runtime

void Task1(void){
    printf(Hello World!, Press any key to continue.\n);
    getchar();
}

Both of these are valid programs for HalOS. Here are some details on what was available in the Operating System

Here is a slide from my final presentation covering whats available

HalOS Running Tetris

Bit Rot?

In a world of new programming languages, frameworks, etc. we forget the unsung hero’s that keep our C/C++ code bases still working 20 years later. I was able to take code not touched in 20 years and run make and it all still worked. I was then able to install bochs and run the image. Even the bochs config file I had created 20 years ago still worked. An if you guessed it, yes, even the doxygen config file still worked.

After having to deal with a Python 2 –> 3 migration not long ago, I can’t help but think that we are doing something wrong in modern software.

Reflection

Lessons learned

The biggest lesson that I learned through this process was that I accidentally merged three major projects in to one unknowingly. I was building the following:

  • Boot Loader
  • Kernel
  • C Runtime
    • No one is going to want to write directly to your API!

In the end the Boot Loader took about 75% of the project’s time. In hindsight I should have probably used something like LILO/GRUB to really focus on the Operating System portion of the project.

Style

Its interesting to see how things can influence your approach to software. Lets talk about my influences and how it shaped my approach to this project. For context, I was in high school in the early 1990’s and was a user of Turbo Pascal and Turbo C++. Had books on all kinds of subjects. I eventually saved up the money to head down to Babbages and purchase Borland C++ 4.0 (22 3.5 floppy disks!). I was really getting in to Windows development. The new Borland compiler also included the Win32 extensions! I even contemplated purchasing and MSDN subscription.

For instance you might think this is something out of the Win32 SDK

PUBLIC DWORD ConsoleWrite(HANDLE hConsole,char *pszText){
  CONSOLE *con = (CONSOLE*)hConsole;
  BOOL bWrapScreen = TRUE;
  BOOL bIsControl  = TRUE;
  WORD wBufferPosition;

Even With Comments I don’t remember

Assembly language is an amazing thing. But the reality is if you are not doing it everyday, you will quickly forget the sequence of actions needed to invoke a CPU instruction. Even with every line commented, I can’t remember what I was doing here. For instance this bit of code …

;; ---------------------------------------------------------------
;; to make life easy, relocate the bios data, we might want to
;; look at it later
;; ---------------------------------------------------------------
mov ax,Final_Bios   ; load the address we want to load
mov es,ax           ; set the es segment to point to the address
xor di,di           ; clear out destination index
mov ax,Initial_Bios ; load the address of where the bios currently lives
mov ds,ax           ; set the ds segment to point to the old address
xor si,si           ; clear out the source index
mov cx,256          ; the bios data area is 256 bytes long
rep movsb           ; rep byte string function

Resources (2001/2002)

This is an outline of the resources I used on the project.

  • Books
    • Operating Systems Design and Implementation - Andrew Tandembaum
    • Modern Operating Systems - Andrew Tandembaum
    • Operating Systems Internals and Design Principles - William Stallings
    • MMURTL - A Real-Time Multitasking Kernel - Richard Burgess
    • The Indispensable PC Hardware Book 4th Edition - Hans-Peter Messmer
    • Intel IA-32 Manual
  • Newsgroups
  • Blogs!
    • Most don’t exist anymore, but they were all from very intelligent individuals. I wish I could remember them all.
  • Source Code
    • I really prided myself on trying to write this on my own, but I will admit I peeked at various sources to learn. Minux, Linux 0.01, and MMURTL were the main sources I looked at while at the same time never copying any code.