# nibblebits/peachos architecture

> Architecture diagram of the GitHub repository nibblebits/peachos, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nibblebits/peachos
- Repository: https://github.com/nibblebits/peachos

## Overview

##### Project type and purpose
- This is a teaching-oriented, from‐scratch, monolithic operating system kernel with userland support (a small shell and standard library).  It covers the full boot process (real mode → protected mode), low-level drivers (disk, keyboard), filesystem (FAT16 + VFS), memory management (paging + heap), interrupt handling, ELF loader, and a simple multitasking kernel with a syscall interface for userland programs.

##### File structure
- Top‐level build artifacts and scripts: Makefile, build.sh, build/, bin/, programs/, src/
- `src/boot`, `src/gdt`, `src/idt`, `src/loader`, `src/memory`, `src/task`, `src/isr80h`, etc. → kernel subsystems
- `src/fs` + `src/disk` → storage and filesystem layers
- `programs/` → userland applications (blank, shell) and the accompanying embedded standard library
- Configuration and linker scripts (`src/linker.ld`, `programs/*/linker.ld`) define memory layout for kernel and user programs.

##### README for additional insights
- Course is structured in “Real Mode” and “Protected Mode” sections, so the bootloader and protected‐mode kernel are two distinct stages.
- Kernel features: FAT16 implementation, virtual filesystem inspired by Linux, ELF loader, process/task management, etc.

##### Detail is essential
- Every major .c/.asm directory as its own block in the kernel core
- Build inputs and outputs (linker scripts → binary images in bin/)

## Diagram (Mermaid)

```mermaid
graph TB
    %% Hardware Layer
    subgraph "Hardware / Emulator"
        BIOS["BIOS / QEMU / Bochs"]:::hardware
    end

    %% Bootloader Layer
    subgraph "Bootloader (Real Mode)"
        Bootloader["Bootloader"]:::boot
    end

    BIOS -->|loads first 512B| Bootloader
    click Bootloader "https://github.com/nibblebits/peachos/blob/master/src/boot/boot.asm"

    %% Kernel Core
    subgraph "Kernel Core (Protected Mode)"
        direction TB

        subgraph "Entry & Configuration"
            KASM["kernel.asm"]:::kernel
            KCFG["kernel.c / kernel.h / config.h"]:::kernel
            LINK["linker.ld"]:::kernel
        end
        click KASM "https://github.com/nibblebits/peachos/blob/master/src/kernel.asm"
        click KCFG "https://github.com/nibblebits/peachos/blob/master/src/kernel.c"
        click LINK "https://github.com/nibblebits/peachos/blob/master/src/linker.ld"

        subgraph "CPU Initialization"
            GDT["GDT (gdt.asm / gdt.c / gdt.h)"]:::kernel
            IDT["IDT & PIC Remap (idt.asm / idt.c / idt.h)"]:::kernel
        end
        click GDT "https://github.com/nibblebits/peachos/blob/master/src/gdt/gdt.c"
        click IDT "https://github.com/nibblebits/peachos/blob/master/src/idt/idt.c"

        subgraph "Memory Manager"
            PAGING["Paging (paging.asm / paging.c / paging.h)"]:::kernel
            HEAP["Heap (heap.c / heap.h / kheap.c / kheap.h)"]:::kernel
            MEM["Memory (memory.c / memory.h)"]:::kernel
        end
        click PAGING "https://github.com/nibblebits/peachos/blob/master/src/memory/paging/paging.c"
        click HEAP "https://github.com/nibblebits/peachos/blob/master/src/memory/heap/heap.c"
        click MEM "https://github.com/nibblebits/peachos/blob/master/src/memory/memory.c"

        subgraph "Interrupts & Syscalls"
            ISR80H["isr80h.c / isr80h.h"]:::kernel
            ISRHELP["Handlers (heap/io/misc/process)"]:::kernel
        end
        click ISR80H "https://github.com/nibblebits/peachos/blob/master/src/isr80h/isr80h.c"
        click ISRHELP "https://github.com/nibblebits/peachos/blob/master/src/isr80h/heap.c"

        subgraph "Device Drivers"
            DISK["Disk & Streamer (disk.c / streamer.c)"]:::kernel
            KEY["Keyboard (keyboard.c / classic.c)"]:::kernel
            IO["I/O Primitives (io.asm / io.h)"]:::kernel
        end
        click DISK "https://github.com/nibblebits/peachos/blob/master/src/disk/disk.c"
        click KEY "https://github.com/nibblebits/peachos/blob/master/src/keyboard/keyboard.c"
        click IO "https://github.com/nibblebits/peachos/blob/master/src/io/io.asm"

        subgraph "Filesystem Layer"
            VFS["VFS (file.c / pparser.c)"]:::kernel
            FAT16["FAT16 Driver (fat16.c / fat16.h)"]:::kernel
            DiskVol[("FAT16 Volume")]:::cylinder
        end
        click VFS "https://github.com/nibblebits/peachos/blob/master/src/fs/file.c"
        click FAT16 "https://github.com/nibblebits/peachos/blob/master/src/fs/fat/fat16.c"

        subgraph "ELF Loader"
            ELF["ELF Formats (elf.c / elf.h)"]:::kernel
            ELFLOAD["ELF Loader (elfloader.c / elfloader.h)"]:::kernel
        end
        click ELF "https://github.com/nibblebits/peachos/blob/master/src/loader/formats/elf.c"
        click ELFLOAD "https://github.com/nibblebits/peachos/blob/master/src/loader/formats/elfloader.c"

        subgraph "Process / Task Manager"
            TASKCORE["task.c / task.h / task.asm"]:::kernel
            PROCESS["process.c / process.h"]:::kernel
            TSS["tss.asm / tss.h"]:::kernel
        end
        click TASKCORE "https://github.com/nibblebits/peachos/blob/master/src/task/task.c"
        click PROCESS "https://github.com/nibblebits/peachos/blob/master/src/task/process.c"
        click TSS "https://github.com/nibblebits/peachos/blob/master/src/task/tss.asm"

        subgraph "Kernel Utilities"
            STRUTIL["string.c / string.h"]:::kernel
            STATUS["status.h"]:::kernel
        end
        click STRUTIL "https://github.com/nibblebits/peachos/blob/master/src/string/string.c"
        click STATUS "https://github.com/nibblebits/peachos/blob/master/src/status.h"
    end

    Bootloader -->|"jumps to"| KASM
    KCFG --> GDT
    GDT --> IDT
    IDT --> MEM
    MEM --> PAGING
    PAGING --> HEAP
    HEAP --> TASKCORE
    TASKCORE --> PROCESS
    PROCESS --> TSS
    ISR80H --> DISK
    DISK --> VFS
    VFS --> FAT16
    ELFLOAD --> TASKCORE

    %% Userland
    subgraph "Userland"
        direction TB
        STD["Standard Library"]:::user
        SHELL["Shell"]:::user
        BLANK["Blank Program"]:::user
    end
    click STD "https://github.com/nibblebits/peachos/blob/master/programs/stdlib/src/peachos.asm"
    click SHELL "https://github.com/nibblebits/peachos/blob/master/programs/shell/src/shell.c"
    click BLANK "https://github.com/nibblebits/peachos/blob/master/programs/blank/blank.c"

    ELFLOAD -->|loads| SHELL
    SHELL -->|syscall| ISR80H
    BLANK -->|syscall| ISR80H

    classDef hardware fill:#dddddd,stroke:#333;
    classDef boot fill:#cce5ff,stroke:#004085;
    classDef kernel fill:#d4edda,stroke:#155724;
    classDef user fill:#fff3cd,stroke:#856404;
    classDef cylinder shape: cylinder,fill:#f8d7da,stroke:#721c24;
```
