; ============================================================================
; substrate.asm — the analog substrate entry point.
;
; This file stitches the existing, already-verified asm layers together.
; Nothing is invented here. All implementation lives in the included files.
;
;   Layer 0: symbolic-machine3/hdgl.asm        Z[φ] ring (verified: exit 161)
;   Layer 1: water_glyphs2/hdgl_float4096_arith.asm  Slot4096 (213-word asm)
;   Layer 3: Universal-Translator/substrate/phi_lattice.asm  phi-tick/consensus
;   Layer 4: flash/ladder_min.asm              Flash compression (k,N,r)
;
; Layers 2,5,6,7,8 are specified in .hdgl (hdgl_analog_substrate.hdgl) and
; have C implementations as a last resort — pending asm migration.
;
; Build (from workspace root):
;   nasm -f elf64 Universal-Translator/substrate/substrate.asm -o /tmp/substrate.o
;   ld   /tmp/substrate.o -o /tmp/substrate
;   /tmp/substrate   →  exit 161 = floor(φ·100) from integers alone
;
; Or use the per-layer verifiers:
;   bash symbolic-machine3/verify.sh              layer 0
;   nasm -f elf64 Universal-Translator/substrate/phi_lattice.asm -o ...  layer 3
; ============================================================================

BITS 64

; ── layer 0: the ring ────────────────────────────────────────────────────────
; Include the complete, verified Z[φ] substrate as-is.
; It contains _start and will exit 161 = floor(φ·100).
; When integrating with layers above, replace _start with sub_ring_init
; and jmp to the boot sequence below.
; ── (standalone mode: just include the ring and let it run) ──────────────────
%include "../../symbolic-machine3/hdgl.asm"

; ── layer 3: phi-lattice tick and consensus ───────────────────────────────────
; phi_lattice_tick and phi_lattice_consensus are in phi_lattice.asm.
; In the full substrate loop these are called from the face_settle path:
;   face_settle():
;     fold observed surface → lattice_tick
;     call phi_lattice_tick(lattice, &lattice_tick)
;     call phi_lattice_consensus(lattice) → settle_lock
; phi_lattice.asm is separately assembled and linked as an object.

; ── layer 4: flash compression ───────────────────────────────────────────────
; flash/ladder_min.asm: entropy seed → FIRE 47 steps → print (a, b, N).
; N is the conserved charge — the boot-unique norm that identifies the path.
; In the full substrate: capture (k,N,r) = flash_capture(a,b) for O(1) resume.
; flash/seeded_final.asm and flash/flashbottle-formal/ carry the deeper proof.

; ── substrate boot sequence (comment out %include above and uncomment this
;    section when integrating all layers into a single linked binary) ─────────
;
; global _start
; extern phi_lattice_tick       ; from phi_lattice.asm
; extern phi_lattice_consensus  ; from phi_lattice.asm
;
; section .bss
;   lattice       resd 128       ; 128 x 32-bit phi-lattice slots
;   lattice_tick  resq 1         ; driven by observed surface (Δ)
;
; section .text
; _start:
;   ; ── seed the lattice from the ring's own φ-hash ──────────────────────
;   ; slot[i] = phi_hash(0, i) & 0xFFFF | 0x1000  (GUZ floor respected)
;   mov  rdi, lattice
;   xor  rcx, rcx
; .seed:
;   mov  rax, 0x9E3779B97F4A7C15   ; 2^64/φ — derived, not stored as a constant
;   xor  rax, rcx
;   rol  rax, 17
;   shr  rax, 32
;   cmp  eax, 0x100
;   jae  .ok
;   mov  eax, 0x100                ; GUZ floor
; .ok:
;   mov  [rdi + rcx*4], eax
;   inc  rcx
;   cmp  rcx, 128
;   jne  .seed
;
;   ; ── run the substrate: tick → consensus → repeat ─────────────────────
; .loop:
;   lea  rdi, [rel lattice]
;   lea  rsi, [rel lattice_tick]
;   call phi_lattice_tick          ; Ω(n+1) = T(Ω(n))
;   lea  rdi, [rel lattice]
;   call phi_lattice_consensus     ; 1 = LOCK (settle_lock)
;   test eax, eax
;   jz   .loop                    ; spin until LOCK (wu-wei — do not force)
;
;   ; ── LOCK reached: substrate is settled ───────────────────────────────
;   ; from here: run the face operator, deliver input, observe surface,
;   ; hash genome, checkpoint the LOCK edge.
;   ; see: Universal-Translator/core/face_operator.c  (C as last resort,
;   ;       pending migration of face_settle/face_genome to asm)
;
;   mov  rax, 60      ; SYS_exit
;   mov  rdi, 161     ; floor(φ·100) — the ring's own exit code
;   syscall
