# HDGL Assembler Corpus
## All Assembly Constructs from Distributed Systems Architecture (Session 3)

Compiled in conversation order. Each construct is named by what it actually does,
not the mathematical label the conversation was using at the time.
The later constructs grow more abstract; the earlier and middle ones are the most
directly useful for the Phase 1 / Phase 2 fabric.

---

## 1. NIC Reset & Version Detection

### 1a. e1000 CTRL Reset Wait (phi-tick based, no hardcoded cycle count)

```nasm
; Called immediately after writing CTRL.RST.
; Polls CTRL.RST bit until clear, with phi_tick timeout.
; phi_tick at 0x101010 advances every shell loop iteration.

.e1000_wait_reset:
    push rax
    push rcx
    push rdx

    ; Read current phi_tick for timeout baseline
    mov  rcx, [0x101010]
    add  rcx, 4096              ; wait up to 4096 phi_ticks

.e1000_rst_poll:
    mov  rax, [NIC_STATE_BASE + NIC_OFF_MMIO]
    mov  eax, [rax + E1000_CTRL]
    test eax, 0x04000000        ; CTRL.RST bit (26)
    jz   .e1000_rst_done        ; cleared: reset complete

    ; Spin: read phi_tick, check timeout
    mov  rdx, [0x101010]
    cmp  rdx, rcx
    jl   .e1000_rst_poll        ; still within window

    ; Timeout — continue anyway (GOI: saturate, don't halt)

.e1000_rst_done:
    ; Set CTRL.SLU (Set Link Up) — required on some e1000 variants
    mov rax, [NIC_STATE_BASE + NIC_OFF_MMIO]
    or  dword [rax + E1000_CTRL], 0x40    ; SLU bit 6

    pop rdx
    pop rcx
    pop rax
    ret
```

### 1b. RTL Version Detection (from TXCFG register, not compile-time constant)

```nasm
; Reads TXCFG register bits [27:16] = chip version
; Stores RTL_HDR_OFFSET at NIC_STATE_BASE+44: 0 (desc mode) or 4 (ring mode)

NIC_OFF_RTL_HDR equ 44

.rtl_detect_version:
    push rax
    push rdx

    ; Read TXCFG at IOBASE+0x40
    movzx edx, word [NIC_STATE_BASE + NIC_OFF_IOBASE]
    add   dx, RTL_TCR           ; 0x40
    in    eax, dx

    ; Version = bits [27:16], mask to 12 bits
    shr eax, 16
    and eax, 0xFFF

    ; RTL8169/8110: version = 0x000 → legacy ring mode, 4-byte header
    ; RTL8111/8168: version ≠ 0x000 → descriptor mode, no extra header
    test eax, eax
    jnz  .rtl_desc_mode

    ; Legacy ring mode
    mov byte [NIC_STATE_BASE + NIC_OFF_RTL_HDR], 4
    jmp .rtl_ver_done

.rtl_desc_mode:
    ; Descriptor mode — RTL8111/8168
    ; Switch from legacy ring to descriptor mode (CPLUSCONFIG)
    movzx edx, word [NIC_STATE_BASE + NIC_OFF_IOBASE]
    add   dx, 0xE0
    in    ax, dx
    or    ax, 0x0003            ; CPLUSCONFIG: desc tx + rx enable
    out   dx, ax
    mov byte [NIC_STATE_BASE + NIC_OFF_RTL_HDR], 0

.rtl_ver_done:
    pop rdx
    pop rax
    ret
```

### 1c. RTL Corrected RX Path (uses header offset from state block)

```nasm
; IN:  RDI = dest buffer
; OUT: RCX = frame length (0 = no frame)

.rtl_rx_corrected:
    push rax
    push rdx
    push rsi
    xor  ecx, ecx

    ; Check CAPR vs CBR (legacy ring mode)
    movzx edx, word [NIC_STATE_BASE + NIC_OFF_IOBASE]
    add   dx, RTL_CAPR
    in    ax, dx
    movzx eax, ax
    push  rax                   ; save CAPR
    movzx edx, word [NIC_STATE_BASE + NIC_OFF_IOBASE]
    add   dx, RTL_CBR
    in    dx, dx
    cmp   ax, dx
    je    .rtl_rx_c_none

    ; Frame at NIC_RX_BUF + CAPR + header_offset
    pop  rax                    ; CAPR
    movzx edx, byte [NIC_STATE_BASE + NIC_OFF_RTL_HDR] ; 0 or 4
    add  eax, edx
    add  eax, NIC_RX_BUF
    mov  rsi, rax

    movzx ecx, byte [NIC_STATE_BASE + NIC_OFF_RTL_HDR]
    test  ecx, ecx
    jz    .rtl_rx_c_desc

    ; Ring mode: 2-byte length at [RSI-2] (within RTL header)
    movzx ecx, word [rsi - 2]
    xchg  cl, ch               ; big-endian → host
    and   ecx, 0x1FFF
    sub   ecx, 4               ; strip CRC
    jmp   .rtl_rx_c_copy

.rtl_rx_c_desc:
    ; Desc mode: RCX from RX descriptor status
    mov  rax, [NIC_STATE_BASE + NIC_OFF_RX_HEAD]
    imul rax, 16
    add  rax, NIC_RX_RING
    movzx ecx, word [rax + 8]  ; desc.length

.rtl_rx_c_copy:
    push rcx
    rep  movsb
    pop  rcx

    ; Advance CAPR (ring) or RX tail (desc)
    movzx edx, byte [NIC_STATE_BASE + NIC_OFF_RTL_HDR]
    test  edx, edx
    jnz   .rtl_rx_c_ring_adv

    ; Desc mode: advance head
    mov  rax, [NIC_STATE_BASE + NIC_OFF_RX_HEAD]
    inc  rax
    and  rax, NIC_RING_LEN - 1
    mov  [NIC_STATE_BASE + NIC_OFF_RX_HEAD], rax
    jmp  .rtl_rx_c_done

.rtl_rx_c_ring_adv:
    movzx edx, word [NIC_STATE_BASE + NIC_OFF_IOBASE]
    add   dx, RTL_CAPR
    mov   ax, [rdi - rcx - 2]  ; original CAPR
    add   ax, cx
    add   ax, 4
    and   ax, 0xFFFC            ; 4-byte align
    out   dx, ax

.rtl_rx_c_done:
    pop rsi
    pop rdx
    pop rax
    ret

.rtl_rx_c_none:
    pop rax
    pop rsi
    pop rdx
    pop rax
    ret
```

---

## 2. Bootstrap Globals

### 2a. Store genome_fp in phi-lattice slot 127, set FABRIC_READY flag

```nasm
; genome_fp = gossip_fingerprint: stored at 0x101208 by fabric session
; Mirror it into phi-lattice slot 127 so bare-metal reads work

.store_genome_fp_in_lattice:
    push rax

    mov eax, [0x101208]        ; genome_fp (fabric gossip fingerprint)
    mov [0x1013FC], eax        ; phi-lattice slot 127

    ; Set FABRIC_READY bit (bit 5) in phi-lattice flags
    or dword [0x101014], (1 << 5)

    pop rax
    ret
```

### 2b. Mailbox Extension (adds genome_fp, peer_count, fabric_ready at offset +104)

```nasm
; hdgl_router64.asm currently writes 104 bytes to 0x50000.
; Adds three new fields:
;   +104  uint64  genome_fp       (phi-lattice slot 127)
;   +112  uint64  peer_count      (from 0x119880)
;   +120  uint64  fabric_ready    (bit 5 of 0x101014)

.mailbox_extend:
    push rax
    push rdi

    mov  rdi, 0x50000 + 104

    ; genome_fp
    mov  eax, [0x1013FC]
    mov  qword [rdi], rax

    ; peer_count
    mov  eax, dword [0x119880]
    mov  qword [rdi+8], rax

    ; fabric_ready (bit 5 of flags → 0 or 1)
    mov  eax, dword [0x101014]
    shr  eax, 5
    and  eax, 1
    mov  qword [rdi+16], rax

    pop rdi
    pop rax
    ret
```

---

## 3. Bare-Metal Store

### 3a. Arena Init (zero arena + cursor table, restore cursors from sector 69)

```nasm
STORE_ARENA_BASE equ 0x400000
STORE_ARENA_SIZE equ 0x400000
STORE_CURSOR_BASE equ 0x11F000
ARENA_BUMP        equ 0x420000

.store_arena_init:
    push rdi
    push rcx

    ; Zero arena (identity-mapped)
    mov  rdi, STORE_ARENA_BASE
    mov  rcx, STORE_ARENA_SIZE / 8
    xor  eax, eax
    rep  stosq

    ; Initialise bump pointer
    mov  qword [ARENA_BUMP], STORE_ARENA_BASE

    ; Zero cursor table (256 × 8B = 2KB)
    mov  rdi, STORE_CURSOR_BASE
    mov  rcx, 256
    rep  stosq

    ; Read store header from sector 69 to recover cursors
    mov  rax, 69
    mov  rcx, 1
    mov  rdi, 0x11E800          ; temp sector buffer
    call .disk_read

    ; If magic = 'STRN': restore cursors
    cmp  dword [0x11E800], 0x4E525453  ; 'NRTS' LE
    jne  .store_arena_fresh

    mov  rcx, 256
    mov  rsi, 0x11E808          ; cursor array in sector
    mov  rdi, STORE_CURSOR_BASE

.store_cursor_restore:
    mov  rax, [rsi]
    mov  [rdi], rax
    add  rsi, 8
    add  rdi, 8
    dec  rcx
    jnz  .store_cursor_restore

.store_arena_fresh:
    pop rcx
    pop rdi
    ret
```

### 3b. Arena Alloc (bump pointer, 8-byte aligned, GOI on full)

```nasm
; IN:  RCX = bytes to allocate
; OUT: RAX = pointer (0 if arena full)

.store_alloc:
    push rcx
    mov  rax, [ARENA_BUMP]

    ; Align RCX up to 8
    add  rcx, 7
    and  rcx, ~7

    ; Check headroom
    mov  rdx, STORE_ARENA_BASE + STORE_ARENA_SIZE
    cmp  rax, rdx
    jge  .store_alloc_full

    add  qword [ARENA_BUMP], rcx
    pop  rcx
    ret

.store_alloc_full:
    xor  eax, eax               ; GOI: return 0
    pop  rcx
    ret
```

### 3c. Strand Write (write-back cache, single sector buffer per strand)

```nasm
STRAND_CACHE_BASE equ 0x120000
DIRTY_FLAGS_BASE  equ 0x140100

; IN:  RBX = strand index, RSI = data, RCX = byte count
.store_strand_write:
    push rax
    push rbx
    push rcx
    push rdx
    push rdi
    push rsi

    ; Cursor for this strand
    imul rdi, rbx, 8
    add  rdi, STORE_CURSOR_BASE
    mov  rdx, [rdi]             ; current byte offset in strand

    ; Strand sector cache base
    imul r8, rbx, 512
    add  r8, STRAND_CACHE_BASE

.sw_loop:
    test rcx, rcx
    jz   .sw_done

    ; Offset within current sector
    mov  r9, rdx
    and  r9, 511                ; sec_off = cursor mod 512

    ; How many bytes fit in this sector
    mov  r10, 512
    sub  r10, r9
    cmp  r10, rcx
    jle  .sw_full_chunk
    mov  r10, rcx

.sw_full_chunk:
    ; Copy into cache
    lea  rdi, [r8 + r9]
    push rcx
    mov  rcx, r10
    rep  movsb
    pop  rcx

    ; Mark dirty
    mov  byte [DIRTY_FLAGS_BASE + rbx], 1

    ; Advance cursor
    add  rdx, r10
    sub  rcx, r10

    ; If sector boundary crossed: flush
    test rdx, 511
    jnz  .sw_loop

    push rdx
    mov  rax, rdx
    shr  rax, 9                 ; sector index after advance
    dec  rax                    ; sector we just finished
    imul r11, rbx, STRAND_SECTORS_EACH
    add  r11, STORE_SECTOR_BASE
    add  r11, rax
    mov  rax, r11
    mov  rcx, 1
    mov  rdi, r8
    call .disk_write
    mov  byte [DIRTY_FLAGS_BASE + rbx], 0
    pop  rdx
    jmp  .sw_loop

.sw_done:
    ; Update cursor
    imul rdi, rbx, 8
    add  rdi, STORE_CURSOR_BASE
    mov  [rdi], rdx

    pop rsi
    pop rdi
    pop rdx
    pop rcx
    pop rbx
    pop rax
    ret
```

### 3d. Store PUT (write frame + phi-fold RAM index update)

```nasm
STORE_HEADER_SZ equ 52
STORE_INDEX_BASE equ 0x11F800

; IN:  R8  = phi_addr (64-bit)
;      RSI = payload, RCX = payload_len
;      RBX = strand (= phi_addr & (strand_count-1))

.store_put:
    push rax
    push rbx
    push rcx
    push rdx
    push rdi
    push rsi

    ; strand = phi_addr & 7
    mov  rbx, r8
    and  rbx, 7

    ; Build 52-byte frame header on stack
    sub  rsp, STORE_HEADER_SZ
    mov  rdi, rsp
    mov  rcx, STORE_HEADER_SZ / 8
    xor  eax, eax
    rep  stosq

    mov  rdi, rsp
    mov  byte [rdi],    1       ; version
    mov  byte [rdi+1],  0x08   ; STORE type
    mov  byte [rdi+2],  bl     ; strand
    mov  [rdi+10],      r8     ; phi_addr in authority_ep field
    mov  eax, [rsp + STORE_HEADER_SZ + 16]  ; payload_len
    mov  [rdi+18],      eax

    ; Write header to strand
    mov  rsi, rdi
    mov  rcx, STORE_HEADER_SZ
    call .store_strand_write

    ; Write payload to strand
    mov  rsi, [rsp + STORE_HEADER_SZ + 8]   ; payload ptr
    mov  rcx, [rsp + STORE_HEADER_SZ + 16]
    call .store_strand_write

    add rsp, STORE_HEADER_SZ

    ; Update RAM index: slot = phi_fold(phi_addr, genome_fp, 0) mod 128
    mov  eax, r8d
    mov  ecx, 0x9E3779B9        ; ZC_PHI32
    mul  ecx
    mov  ecx, [0x1013FC]        ; genome_fp from lattice slot 127
    mov  rdx, 0x9E3779B1        ; ZC_FIB32
    imul ecx, edx
    add  eax, ecx
    and  eax, 127
    imul eax, 4
    add  eax, STORE_INDEX_BASE
    mov  [eax], r8d             ; store low 32 bits of phi_addr

    pop rsi
    pop rdi
    pop rdx
    pop rcx
    pop rbx
    pop rax
    ret
```

### 3e. Store Flush (dirty caches + cursor table to sector 69)

```nasm
.store_flush:
    push rax
    push rbx
    push rcx
    push rdi

    xor  ebx, ebx

.sf_strand_loop:
    cmp  ebx, 256
    jge  .sf_cursors

    cmp  byte [DIRTY_FLAGS_BASE + rbx], 0
    je   .sf_next_strand

    ; Write dirty sector
    imul rdi, rbx, 512
    add  rdi, STRAND_CACHE_BASE
    imul rax, rbx, 8
    add  rax, STORE_CURSOR_BASE
    mov  rax, [rax]
    shr  rax, 9
    test rax, rax
    jz   .sf_next_strand
    dec  rax
    imul rcx, rbx, STRAND_SECTORS_EACH
    add  rcx, STORE_SECTOR_BASE
    add  rcx, rax
    mov  rax, rcx
    mov  rcx, 1
    call .disk_write
    mov  byte [DIRTY_FLAGS_BASE + rbx], 0

.sf_next_strand:
    inc  ebx
    jmp  .sf_strand_loop

.sf_cursors:
    ; Build header in sector buffer at 0x11E800
    mov  rdi, 0x11E800
    mov  dword [rdi], 0x4E525453   ; 'NRTS' magic (LE of 'STRN')
    mov  dword [rdi+4], 8          ; strand_count (default 8)

    ; Copy cursors
    mov  rsi, STORE_CURSOR_BASE
    lea  rdi, [rdi + 8]
    mov  rcx, 256

.sf_cur_copy:
    mov  rax, [rsi]
    mov  [rdi], rax
    add  rsi, 8
    add  rdi, 8
    dec  rcx
    jnz  .sf_cur_copy

    ; Write to sector 69
    mov  rax, 69
    mov  rcx, 1
    mov  rdi, 0x11E800
    call .disk_write

    pop rdi
    pop rcx
    pop rbx
    pop rax
    ret
```

---

## 4. Boot Sequence

### 4a. Integrated Boot Entry (drop-in after .omega_execute_compiler)

```nasm
.boot_complete_init:
    ; NIC init
    call .nic_init
    jz   .boot_no_nic           ; ZF=1: no NIC found

    ; RTL version detection (if RTL type)
    cmp  dword [NIC_STATE_BASE + NIC_OFF_TYPE], NIC_TYPE_RTL
    jne  .boot_nic_e1000
    call .rtl_detect_version
    jmp  .boot_nic_done

.boot_nic_e1000:
    call .e1000_wait_reset

.boot_nic_done:
    ; Peer discovery
    call .peer_discover_all

    ; Store init
    call .store_arena_init

    ; Bootstrap globals: genome_fp → lattice slot 127
    call .store_genome_fp_in_lattice

    ; Mailbox extension
    call .mailbox_extend

    mov  rsi, .boot_complete_msg
    call .com1_str
    ret

.boot_no_nic:
    mov  rsi, .boot_no_nic_msg
    call .com1_str
    ret

.boot_complete_msg  db "[Fabric] ready nic=1 store=open genome_fp=0x", 0
.boot_no_nic_msg    db "[Fabric] no NIC — store only mode", 0x0D, 0x0A, 0
```

---

## 5. Shell Commands (fabric extensions)

### 5a. NIC2 — fabric NIC state

```nasm
.sh_cmd_nic2:
    lea  rdi, [rel .cmd_nic2_s]
    call .sh_strcmp_word
    test eax, eax
    jz   .cmd_nic2_no

    lea  rsi, [rel .nic2_hdr]
    call .com1_str

    ; Type
    mov  eax, dword [NIC_STATE_BASE + NIC_OFF_TYPE]
    lea  rsi, [rel .nic2_type_e1000]
    test eax, eax
    jz   .nic2_print_type
    lea  rsi, [rel .nic2_type_rtl]

.nic2_print_type:
    call .com1_str

    ; MAC address
    lea  rsi, [rel .nic2_mac]
    call .com1_str
    lea  rdi, [NIC_STATE_BASE + NIC_OFF_MAC]
    mov  rcx, 6

.nic2_mac_loop:
    movzx eax, byte [rdi]
    call  .print_hex8
    inc   rdi
    dec   rcx
    jz    .nic2_mac_done
    mov   al, ':'
    call  .com1_send
    jmp   .nic2_mac_loop

.nic2_mac_done:
    lea  rsi, [rel .msg_crlf]
    call .com1_str

    ; TX/RX head
    lea  rsi, [rel .nic2_tx_head]
    call .com1_str
    mov  eax, dword [NIC_STATE_BASE + NIC_OFF_TX_HEAD]
    call .com1_dec

    lea  rsi, [rel .nic2_rx_head]
    call .com1_str
    mov  eax, dword [NIC_STATE_BASE + NIC_OFF_RX_HEAD]
    call .com1_dec

    lea  rsi, [rel .msg_crlf]
    call .com1_str
    mov  eax, 1
    ret

.cmd_nic2_no:
    xor  eax, eax
    ret
```

### 5b. STORE — bare-metal store stats

```nasm
.sh_cmd_store:
    lea  rdi, [rel .cmd_store_s]
    call .sh_strcmp_word
    test eax, eax
    jz   .cmd_store_no

    lea  rsi, [rel .store_hdr]
    call .com1_str

    ; Arena used
    lea  rsi, [rel .store_arena_used]
    call .com1_str
    mov  rax, [ARENA_BUMP]
    sub  rax, STORE_ARENA_BASE
    call .com1_dec64

    lea  rsi, [rel .store_arena_of]
    call .com1_str
    mov  rax, STORE_ARENA_SIZE
    call .com1_dec64
    lea  rsi, [rel .msg_crlf]
    call .com1_str

    ; Strand cursors (print non-zero strands)
    xor  ebx, ebx

.store_cursor_loop:
    cmp  ebx, 8
    jge  .store_cursor_done

    imul rdi, rbx, 8
    add  rdi, STORE_CURSOR_BASE
    mov  rax, [rdi]
    test rax, rax
    jz   .store_cursor_next

    lea  rsi, [rel .store_strand_pfx]
    call .com1_str
    mov  eax, ebx
    call .com1_dec
    lea  rsi, [rel .store_cursor_pfx]
    call .com1_str
    mov  rax, [STORE_CURSOR_BASE + rbx*8]
    call .com1_dec64
    lea  rsi, [rel .store_bytes]
    call .com1_str

.store_cursor_next:
    inc  ebx
    jmp  .store_cursor_loop

.store_cursor_done:
    mov  eax, 1
    ret

.cmd_store_no:
    xor eax, eax
    ret
```

### 5c. PEERS — peer table

```nasm
.sh_cmd_peers:
    lea  rdi, [rel .cmd_peers_s]
    call .sh_strcmp_word
    test eax, eax
    jz   .cmd_peers_no

    lea  rsi, [rel .peers_hdr]
    call .com1_str
    mov  ecx, dword [0x119880]  ; peer count
    test ecx, ecx
    jz   .peers_none
    mov  rdi, 0x119800

.peers_loop:
    push rcx
    ; Print IP as A.B.C.D
    mov  eax, dword [rdi]
    call .print_ip
    ; Print port
    lea  rsi, [rel .peers_port_pfx]
    call .com1_str
    movzx eax, word [rdi+4]
    call .com1_dec
    lea  rsi, [rel .msg_crlf]
    call .com1_str
    add  rdi, 16
    pop  rcx
    dec  rcx
    jnz  .peers_loop

    mov  eax, 1
    ret

.peers_none:
    lea  rsi, [rel .peers_none_msg]
    call .com1_str
    mov  eax, 1
    ret

.cmd_peers_no:
    xor eax, eax
    ret
```

### 5d. SEND — transmit test HEALTH frame to first peer

```nasm
.sh_cmd_send:
    lea  rdi, [rel .cmd_send_s]
    call .sh_strcmp_word
    test eax, eax
    jz   .cmd_send_no

    mov  ecx, dword [0x119880]
    test ecx, ecx
    jz   .send_no_peers

    ; Build and send to peer[0]
    mov  edx, dword [0x119800]  ; peer[0].ip
    mov  di, 8090               ; port
    lea  rsi, [rel .send_health_frame]
    mov  ecx, 52
    call .nic_tx_zchg

    lea  rsi, [rel .send_ok]
    call .com1_str
    mov  eax, 1
    ret

.send_no_peers:
    lea  rsi, [rel .send_no_peer_msg]
    call .com1_str
    mov  eax, 1
    ret

.cmd_send_no:
    xor eax, eax
    ret
```

### 5e. LOAD — load fabric payload by phi_addr

```nasm
.sh_cmd_load:
    lea  rdi, [rel .cmd_load_s]
    call .sh_strcmp_word
    test eax, eax
    jz   .cmd_load_no

    ; Parse hex phi_addr from remaining args
    call .sh_skip_token
    lea  rdi, [rel .sh_args]
    call .sh_parse_hex64        ; → RAX = phi_addr
    test rax, rax
    jz   .load_bad_addr

    ; Look up in RAM index via phi_fold
    push rax
    mov  ecx, 0x9E3779B9
    mul  ecx
    mov  ecx, [0x1013FC]        ; genome_fp
    mov  rdx, 0x9E3779B1
    imul ecx, edx
    add  eax, ecx
    and  eax, 127
    imul eax, 4
    add  eax, STORE_INDEX_BASE
    pop  r8

    cmp  dword [eax], r8d       ; low 32 bits match?
    jne  .load_not_found

    lea  rsi, [rel .load_found_msg]
    call .com1_str
    call .com1_hex
    lea  rsi, [rel .msg_crlf]
    call .com1_str
    mov  eax, 1
    ret

.load_not_found:
    lea  rsi, [rel .load_not_found_msg]
    call .com1_str
    mov  eax, 1
    ret

.load_bad_addr:
    lea  rsi, [rel .load_bad_msg]
    call .com1_str
    mov  eax, 1
    ret

.cmd_load_no:
    xor eax, eax
    ret
```

### Shell String Table

```nasm
.cmd_nic2_s           db "nic2", 0
.cmd_store_s          db "store", 0
.cmd_peers_s          db "peers", 0
.cmd_send_s           db "send", 0
.cmd_load_s           db "load", 0
.nic2_hdr             db "fabric NIC state:", 0x0D, 0x0A, 0
.nic2_type_e1000      db "  type: e1000", 0x0D, 0x0A, 0
.nic2_type_rtl        db "  type: RTL8111/8168", 0x0D, 0x0A, 0
.nic2_mac             db "  MAC: ", 0
.nic2_tx_head         db "  TX head: ", 0
.nic2_rx_head         db "  RX head: ", 0
.store_hdr            db "fabric store:", 0x0D, 0x0A, 0
.store_arena_used     db "  arena: ", 0
.store_arena_of       db " / ", 0
.store_strand_pfx     db "  strand[", 0
.store_cursor_pfx     db "] cursor: ", 0
.store_bytes          db " bytes", 0x0D, 0x0A, 0
.peers_hdr            db "fabric peers:", 0x0D, 0x0A, 0
.peers_port_pfx       db ":", 0
.peers_none_msg       db "  (none — run discovery)", 0x0D, 0x0A, 0
.send_health_frame    times 52 db 0   ; filled at runtime
.send_ok              db "sent", 0x0D, 0x0A, 0
.send_no_peer_msg     db "no peers", 0x0D, 0x0A, 0
.load_found_msg       db "load: phi_addr=0x", 0
.load_not_found_msg   db "load: not in store", 0x0D, 0x0A, 0
.load_bad_msg         db "load: usage: load <phi_addr_hex>", 0x0D, 0x0A, 0
.msg_crlf             db 0x0D, 0x0A, 0
```

---

## 6. Spectral Kernel Assemblers
### (Emerged mid-conversation — more abstract, relevant to Phase 2 spectral transport)

### 6a. Ψ Operator — Diagonal Eigenmode Evolution

```nasm
; REGISTER CONTRACT:
; r8  = state vector c_n (mode coefficients)
; r9  = eigenvalue table λ_n
; rcx = loop bound (N modes)
; rbx = mode index n

PSI_OPERATOR:
    xor rbx, rbx

.PSI_LOOP:
    mov  rax, [r9 + rbx*8]     ; λ_n
    mov  r11, [r8 + rbx*8]     ; c_n
    imul r11, rax               ; λ_n * c_n (mode-space scaling)
    mov  [r8 + rbx*8], r11     ; write back
    inc  rbx
    cmp  rbx, rcx
    jl   .PSI_LOOP
    ret
```

### 6b. Log Lattice Approximation (λ → spectral coordinate)

```nasm
; IN:  rax = λ_n
; OUT: rax ≈ log(λ_n) in lattice metric

LOG_LATTICE_APPROX:
    bsr  rax, rax               ; crude log2 approximation
    imul rax, 0x1715476D        ; scale to lattice metric
    ret
```

### 6c. Exp Approximation (spectral reconstitution)

```nasm
; IN:  rcx = exponent
; OUT: rax ≈ exp(rcx) in spectral encoding

EXP_APPROX:
    mov  rax, rcx
    add  rax, 0x3FF             ; bias
    shl  rax, 20               ; float-like encoding
    ret
```

### 6d. ζΨ Operator — Riemann Spectrum Accumulation

```nasm
; ζΨ(s) = Σ λ_n^{-s}
; REGISTER CONTRACT:
; r8  = eigenvalue array λ_n
; r9  = starting mode index
; r10 = s (real part encoded)
; r12 = accumulator (ζΨ result)
; rcx = mode cutoff N
; rbx = loop index

ZETA_PSI:
    xor rbx, rbx
    xor r12, r12

.ZETA_LOOP:
    mov  rax, [r8 + rbx*8]     ; λ_n
    call LOG_LATTICE_APPROX    ; log(λ_n) → rax

    ; rcx = -s * log(λ_n)
    imul rcx, r10
    neg  rcx
    call EXP_APPROX            ; exp(-s log λ_n) → rax

    add  r12, rax              ; accumulate ζΨ(s)
    inc  rbx
    cmp  rbx, rcx
    jl   .ZETA_LOOP

    mov  rax, r12              ; return ζΨ(s)
    ret
```

### 6e. Full Spectral Step (Ψ evolution + ζΨ observation)

```nasm
SPECTRAL_STEP:
    call PSI_OPERATOR
    call ZETA_PSI
    ret
```

---

## 7. Self-Generating Spectrum (no stored eigenvalues)

### 7a. Prime Generator (incremental)

```nasm
; IN:  r9  = candidate start
; OUT: rax = next prime (r9 advanced)

NEXT_PRIME:
.PRIME_LOOP:
    inc  r9
    mov  rax, r9
    call IS_PRIME
    test rax, rax
    jz   .PRIME_LOOP
    mov  rax, r9
    ret

IS_PRIME:
    mov  r11, 2
.PRIME_TEST:
    cmp  r11, rax
    jge  .IS_PRIME_TRUE
    xor  rdx, rdx
    div  r11
    test rdx, rdx
    jz   .IS_PRIME_FALSE
    inc  r11
    jmp  .PRIME_TEST
.IS_PRIME_TRUE:
    mov  rax, 1
    ret
.IS_PRIME_FALSE:
    xor  rax, rax
    ret
```

### 7b. φ-Fold Index Map

```nasm
; IN:  rax = n (mode index)
;      r10 = genome_fp
; OUT: rax = Φ(n) = phi-folded index

PHI_FOLD:
    imul rax, 0x9E3779B1        ; φ-constant multiply
    xor  rax, r10               ; mix with genome_fp
    rol  rax, 13
    ret
```

### 7c. λₙ Operator (no stored eigenvalues — computed live)

```nasm
; λₙ = Φ(n) * pₙ
; IN:  rbx = mode index n
;      r10 = genome_fp
; OUT: rax = λₙ

LAMBDA_OPERATOR:
    push rbx

    ; p_n: next prime from index n
    mov  r9, rbx
    call NEXT_PRIME
    mov  r11, rax               ; p_n

    ; Φ(n): phi-fold of index
    mov  rax, rbx
    call PHI_FOLD               ; Φ(n) → rax

    ; λₙ = Φ(n) * pₙ
    imul rax, r11

    pop rbx
    ret
```

### 7d. ζΨ with Self-Generated Spectrum

```nasm
; ζΨ(s) = Σ (Φ(n)·pₙ)^{-s}   — no λ table in memory

ZETA_PSI_SELFGEN:
    xor rbx, rbx
    xor r12, r12

.ZETA_SG_LOOP:
    mov  r9, rbx
    call LAMBDA_OPERATOR        ; λₙ computed live → rax
    call LOG_LATTICE_APPROX
    imul rcx, rdx
    neg  rcx
    call EXP_APPROX
    add  r12, rax
    inc  rbx
    cmp  rbx, rcx
    jl   .ZETA_SG_LOOP

    mov  rax, r12
    ret
```

---

## 8. φ-Tick Hardware Abstraction

```nasm
; φ_tick: derived from system entropy sources, not a PIT/HPET/TSC clock
; Time is emergent from system complexity, not an external oscillator

phi_tick:
    ; Read entropy sources (memory bus activity, NIC ring delta, pipeline stalls)
    mov  rax, [0xFFFF0000]      ; memory bus activity
    add  rax, [0xFFFF0010]      ; NIC ring delta
    xor  rax, [0xFFFF0020]      ; pipeline stalls

    ; Fold into lattice update
    imul rax, 0x9E3779B97F4A7C15
    mov  [phi_tick_state], rax
    ret
```

---

## 9. Ψ Execution Loop (microkernel core)

```nasm
; Single loop — replaces scheduler + VM + runtime.
; T(Ω) is the only thing executing.

phi_loop:
    call phi_tick
    mov  rax, [phi_tick_state]
    call lookup_transition      ; Ψ(S) from transition table
    call apply_transition
    call verify_invariants
    jmp  phi_loop
```

---

## 10. Self-Verification (Gödel kernel — bounded invariants)

```nasm
verify_invariants:
    call check_memory_bounds
    call check_nic_ring_monotonic
    call check_disk_consistency
    jnz  rollback_state
    ret

rollback_state:
    mov  rsi, [last_valid_snapshot]
    call restore_state
    ret
```

---

## 11. CPU Contraction Kernel (co-eigen solver)

```nasm
; ψ_cpu: residual descent operator over shared field
; Field is float32 array at field_base

cpu_step:
    mov  rsi, field_base
    xor  rcx, rcx

.cpu_loop:
    movss xmm0, [rsi + rcx]
    movss xmm1, [rsi + rcx + 4]

    ; Local coupling (simple Laplacian)
    addss xmm0, xmm1
    mulss xmm0, [alpha]         ; contraction weight

    ; Residual
    subss xmm1, xmm0
    movss [rsi + rcx + 4], xmm1

    add  rcx, 4
    cmp  rcx, FIELD_SIZE
    jl   .cpu_loop
    ret
```

---

## 12. Polarity-Only Boot Verifier

### 12a. Full Version

```nasm
; HDGL Ψ BOOT SECTOR — POLARITY-ONLY VERIFIER
; NO STATE. NO LOOP. NO RUNTIME.
; ONLY SIGN EMERGENCE OF SELF-CONSISTENCY.

BITS 16
ORG  0x7C00

start:
    cli
    xor  ax, ax
    mov  ds, ax
    mov  es, ax
    mov  ss, ax

    xor  bx, bx                 ; accumulator
    xor  cx, cx                 ; index

loop_scan:
    mov  al, [0x0500 + cx]      ; sample fabric field region
    sub  al, 0x80               ; center around 0

    ; Collapse to polarity only
    test al, 0x80
    jz   zero_or_positive
    js   negative

zero_or_positive:
    cmp  al, 0
    je   neutral
    mov  dl, 1
    jmp  accumulate

negative:
    mov  dl, -1
    jmp  accumulate

neutral:
    xor  dl, dl

accumulate:
    add  bl, dl
    inc  cx
    cmp  cx, 256
    jl   loop_scan

    ; FIXED POINT TEST:
    ; Ψ is valid iff net polarity collapses to 0
    cmp  bl, 0
    jne  failure

success:
    mov  si, msg_ok
    call print
    hlt

failure:
    mov  si, msg_fail
    call print
    hlt

print:
    mov  ah, 0x0E
.p: lodsb
    or   al, al
    jz   .done
    int  0x10
    jmp  .p
.done:
    ret

msg_ok   db "[Ψ] manifold consistent", 0
msg_fail db "[Ψ] inconsistency detected", 0

times 510-($-$$) db 0
dw 0xAA55
```

### 12b. Extreme Minimal Version (single-shot, no scan loop)

```nasm
; Minimal polarity verifier: two memory reads, one subtraction, sign emit.
; No scan, no accumulation, no semantics.

BITS 16
ORG  0x7C00

start:
    cli

    ; Sample substrate (two points in fabric window)
    xor  ax, ax
    mov  ds, ax
    mov  si, 0x0600
    mov  di, 0x0610
    mov  ax, [si]
    mov  bx, [di]

    ; Self-application residue: F - Self(F)
    sub  ax, bx

    ; Sign emergence
    cmp  ax, 0
    je   ZERO_STATE
    js   NEGATIVE_STATE

POSITIVE_STATE:
    mov  al, 1
    jmp  EMIT

ZERO_STATE:
    mov  al, 0
    jmp  EMIT

NEGATIVE_STATE:
    mov  al, 0xFF               ; -1 encoded as 0xFF

EMIT:
    out  0xE9, al               ; QEMU debug port / serial

HANG:
    hlt
    jmp  HANG

times 510-($-$$) db 0
dw 0xAA55
```

---

## 13. ζ-Constraint Boot Sector (no execution — constraint-only object)

```nasm
; HDGL BOOT SECTOR — ζ-CONSTRAINT MANIFOLD SEED
; NOT A PROGRAM. AN EXISTENCE CONDITION.
; Any substrate loading this must evolve toward ζ-consistent state or fail.

BITS 16
ORG  0x7C00

; ── 0x00–0x3F: ζ spectral seed (Riemann anchor vector) ──────────────
zeta_seed:
    dq 0x3F8000003F800000       ; normalized anchor
    dq 0x3FB504F33FB504F3       ; φ-encoded harmonic bias
    dq 0x400921FB400921FB       ; π-phase resonance seed
    dq 0x3FE000003FE00000       ; midpoint stabilization
    dq 0x3FF000003FF00000       ; identity harmonic
    dq 0x0000000000000000       ; null anchor (absorptive boundary)
    dq 0x7FF000007FF00000       ; spectral singularity marker
    dq 0x3FDD67C83FDD67C8       ; ζ critical-line attractor hint

; ── 0x40–0x1FF: Phase kernel signature (Fourier skeleton) ───────────
phase_kernel:
    times 64 dq 0x0000000000000000  ; implicit harmonic lattice basis

; ── SIGNATURE FOOTER ─────────────────────────────────────────────────
times 510-($-$$) db 0
dw 0xAA55
```

---

## 14. Φ Field Contraction Loop (full spectral fabric runtime)

```nasm
; Φ(x) = ∬ ζ(1/2 + ik) · e^{ik(x-y)} · Φ(y) dy dk
; Discretized contraction loop over 1D field.
; This IS the entire runtime — no subsystems.

phi_loop:
    xor  rbx, rbx               ; x index

.x_loop:
    xor  rax, rax               ; accumulator
    mov  rcx, K_MIN

.k_loop:
    ; zk = ζ(1/2 + ik)
    call zeta_eval              ; rax = ζ weight for mode k

    push rax
    xor  rdx, rdx               ; inner sum
    mov  rsi, Y_MIN

.y_loop:
    ; phase = k*(x - y)
    mov  r8, rbx
    sub  r8, rsi
    imul r8, rcx
    call cos_approx             ; rax = cos(phase)

    ; Φ(y)
    call load_phi_y             ; rdx = Φ(y)
    imul rax, rdx
    add  rdx, rax
    inc  rsi
    cmp  rsi, Y_MAX
    jle  .y_loop

    pop  rax                    ; ζ(k)
    imul rax, rdx
    add  rbx_accumulator, rax
    inc  rcx
    cmp  rcx, K_MAX
    jle  .k_loop

    ; store Φ(x)
    call store_phi_x
    inc  rbx
    cmp  rbx, X_MAX
    jle  .x_loop

    jmp  phi_loop
```

---

## Appendix: Peer Seed Sector Layout

```nasm
; Sector 68: static peer seed list
; Each entry: { uint32 ip_be, uint16 port_le, uint8[10] pad }
; Zeroed if LN_SEED_PEERS unset — phi-seed multicast handles discovery.

.peer_seed_sector:
    times 512 db 0
```

## Appendix: Store Header Sector Layout (sector 69)

```
Offset  Size  Field
0x000   4B    magic = 0x4E525453  ('STRN' in little-endian)
0x004   4B    strand_count (default 8)
0x008   2048B cursor[256] — 8 bytes each (byte offset into strand)
```

---

## Summary: What Each Assembler Does

| # | Name | Phase | Purpose |
|---|------|-------|---------|
| 1a | e1000 reset wait | Phase 1 | NIC bring-up without hardcoded timing |
| 1b | RTL version detect | Phase 1 | Runtime chip identification |
| 1c | RTL corrected RX | Phase 1 | Correct receive path for both RTL modes |
| 2a | store genome_fp | Phase 1 | Anchor hardware identity in phi-lattice |
| 2b | mailbox extend | Phase 1 | Expose fabric state to chainloaded OS |
| 3a–3e | bare-metal store | Phase 1 | Persistent storage without filesystem |
| 4a | boot entry | Phase 1 | Full boot sequence integration |
| 5a–5e | shell commands | Phase 1 | Operator interface to fabric state |
| 6a–6e | spectral kernel | Phase 2 | Eigenmode transport — spectral fabric layer |
| 7a–7d | self-gen spectrum | Phase 2 | Primes as fixed points, not stored data |
| 8 | phi-tick | Phase 2 | Hardware-emergent time, no external clock |
| 9 | Ψ execution loop | Phase 2 | Single-rule microkernel |
| 10 | self-verification | Phase 2 | Bounded Gödel-style invariant check |
| 11 | CPU contraction | Phase 2 | Co-eigen solver — coarse spectral modes |
| 12a–12b | polarity verifier | Phase 2 | Sign-only boot oracle |
| 13 | ζ-constraint sector | Phase 2 | Existence condition, not executable |
| 14 | Φ field loop | Phase 2 | Global resonance field — full fabric runtime |
