Handcrafted CPU Expert: Assembler Language for Unix-like System

Handcrafted CPU Expert: Assembler Language for Unix-like System

Hardware Design:

GR8CPU is a fully functional computer built on a breadboard. The first version, GR8CPU Rev2, has over 600 wires, 74 integrated circuits, and 79 LEDs. It is an 8-bit architecture, designed with simplicity and processing power in mind. The CPU can access 256 bytes of RAM and a fully functional Tic-Tac-Toe game has already been written for it.

Handcrafted CPU Expert: Assembler Language for Unix-like System

So far, the second GR8CPU Rev3 is being constructed. There is a lot of interesting work to be done: with 65536 bytes of memory, improved algorithms, and a design for higher speed, this CPU is the most complex breadboard CPU ever built.

Handcrafted CPU Expert: Assembler Language for Unix-like System

Initial results:

Handcrafted CPU Expert: Assembler Language for Unix-like System

RAM module completed:

Handcrafted CPU Expert: Assembler Language for Unix-like System

Designing the ALU:

Handcrafted CPU Expert: Assembler Language for Unix-like System

Completed ALU design:

Handcrafted CPU Expert: Assembler Language for Unix-like System

Video card design:

Handcrafted CPU Expert: Assembler Language for Unix-like System

Completed video output circuit:

Handcrafted CPU Expert: Assembler Language for Unix-like System
Handcrafted CPU Expert: Assembler Language for Unix-like System

Unix-like System Design

GR8NIX is an operating system inspired by Unix, which is a simple multi-user, multi-tasking operating system released in the 1970s. Modern operating systems based on Unix ideas include Linux, MacOS, and Android. GR8CPU Rev3 is written in assembly language, with a kernel implemented in 2000 lines of assembly, totaling 5KB of code:

(1) Multi-threading support for 32 concurrent threads.

(2) Theoretically unlimited number of running programs.

(3) Dynamic memory allocation, with a current maximum size of 8 KB.

(4) True program execution is position-independent (dynamic application loading).

However, GR8NIX is not perfect; due to hardware limitations, GR8NIX cannot:

(1) Protect memory from process interference.

(2) Recover from attempts to run invalid instructions.

(3) Reliably prevent memory leaks after process termination.

After implementing dynamic memory management, work began on creating a file system to load files from disk:

Handcrafted CPU Expert: Assembler Language for Unix-like System

In the video demonstration, you can see GR8NIX come to life, successfully loading and running a Shell program, which in turn loads and runs any other programs you desire.

First, Exec is responsible for loading and running programs. Exec performs some integrity checks first: Does the file exist? Is it a program file? Is it valid? Next, Exec finds the length of the executable file by checking each entry and adding its offset to its length. The length found by Exec is the maximum calculated length.

After this, Exec calls thread_launch, a method used to prepare for starting threads. Exec completes this by adding some raw data to the process: user ID, pointer to allocated memory, command line being run, working directory, etc.

Handcrafted CPU Expert: Assembler Language for Unix-like System

Author: Eric2013

Source: 安富莱电子 (ID: armfly_com)

Leave a Comment