The Kernel’s Entry Point

Every Z80 system, upon power-on or reset, immediately begins executing the instruction at a fixed, low memory address. This is the boot sequence start, and it usually resides in Read-Only Memory (ROM).

Common Reset Vector: The Z80 automatically jumps to address 0000H (the Reset Vector). The instruction at this address must be a jump to the main ROM code.

Phase 1: Minimal Hardware Initialization

The first few instructions of the kernel must perform critical tasks to ensure the CPU can safely run code.

Essential Boot Instructions:

    ORG  0000H            ; The fixed reset address
    JP   MAIN_BOOT        ; Jump to the main code located further up in ROM

MAIN_BOOT:
    DI                    ; 1. Disable all interrupts immediately
    
    LD   SP, STACK_TOP    ; 2. Initialize the Stack Pointer (critical for CALLs)
    
    ; 3. Perform memory banking setup (Part 43)
    CALL INIT_MEMORY_BANKS 

    ; 4. Clear key hardware (e.g., set video controller to a known state)
    CALL INIT_VIDEO
    
    JP   KERNEL_MAIN      ; Jump to the main OS loop

Phase 2: Setting up the Kernel’s Space

The kernel needs its own dedicated space for variables and buffers. This includes setting up the Interrupt Vector Table (Part 48).

Initial Kernel Tasks:

  1. Clear Memory: Clear the main RAM area reserved for the operating system variables to ensure a clean start.
  2. Set Up Interrupts: Set the Interrupt Mode (e.g., IM 1′ or IM 2′) and the Interrupt Vector Register (`LD I, BASE_ADDR′).
  3. Enable: Finish by enabling interrupts (`EI′) once all handlers are stable.

The Kernel Main Loop

The Kernel Main Loop is the heart of the operating system. It is an infinite loop that constantly manages system tasks like polling the keyboard, checking the clock, and executing user programs.

The Main Loop (Simplified):

KERNEL_MAIN:
    CALL CHECK_KEYBOARD   ; Check for user input
    CALL RUN_TASKS        ; Run system background tasks (e.g., update clock)

    ; The HALT instruction pauses the CPU until the next interrupt arrives
    HALT                  ; Saves power and T-states
    
    JP   KERNEL_MAIN      ; Loop infinitely