Magic‐1 Runtime Initialization - retrotruestory/M1DEV GitHub Wiki

Based on the provided source files, here's the Magic-1 runtime initialization requirements:

Magic-1 Runtime Initialization

1. Register Initialization

From bcrt0.s:

_start:
    // Initialize system state 
    ld.16   a,0x0000
    copy    msw,a          // MSW = 0x0000 (interrupts off, system mode)
    
    // Setup kernel stack
    ld.16   a,stack_start  // stack_start = 0x8000
    copy    sp,a
    
    // Initialize data pointer 
    ld.16   a,dp_start    // dp_start = 0x0000
    copy    dp,a

2. Memory Map

0x0000-0x3FFF: ROM (16KB)
0x4000-0x7FFF: Device SRAM (16KB)
0x8000-0xF7FF: Main Memory
0xF800-0xFFFF: Device Page

Special Addresses:
- Stack: Starts at 0x8000
- Device Page: 0xF800-0xFFFF

3. Page Table Setup

// Initialize page tables
flags = PAGE_PRESENT | PAGE_SRAM | PAGE_NO_WAIT;
for (i=0; i<32; i++) {
    // Code pages
    write_code_pte(address, i | flags);
    // Data pages with write permission
    write_data_pte(address, i | (flags | PAGE_WRITEABLE));
    address += PAGE_SIZE;
}

4. Hardware State Requirements

  • MSW (Machine Status Word) = 0x0000
    • Bit 7: Paging disabled
    • Bit 6-4: Reserved
    • Bit 3: User mode disabled (kernel mode)
    • Bit 2-0: Interrupts disabled

5. Device Initialization Sequence

// Initialize devices in correct order
1. Disable all interrupts
2. Setup UART(s)
3. Initialize RTC
4. Setup memory controller
5. Initialize IDE/CF interface

6. Memory Layout Requirements

Text Segment: 
- Must be aligned on page boundary
- Read-only in user mode
- Executable

Data Segment:
- Must be aligned on page boundary  
- Read-write in both modes
- Non-executable

BSS Segment:
- Must be zeroed before use
- Follows data segment

7. Runtime State Validation

// Required state checks before program execution
if (N_BADMAG(hdr)) {
    // Verify valid Magic-1 executable
    error("Invalid magic number");
}

if (hdr.a_trsize || hdr.a_drsize) {
    // Check for relocations
    warning("Relocations present");
}

This initialization ensures proper operation of the Magic-1 system before transferring control to the main program.