; ============================================================================
; phi_lattice.asm — the REAL analog substrate tick, in bare x86-64 assembly.
;
; This is NOT new machinery: it is extracted, logic-for-logic unchanged, from
; hdgl_router64-0.4/src/hdgl_router64.asm's `.phi_tick`/`.phi_consensus`
; (itself boot-verified: "Router64> ps" reports real LOCK/UNLOCK on real and
; QEMU boot). Only change: parameterized via System V AMD64 registers
; (rdi=lattice pointer, rsi=tick pointer) instead of the original's hardcoded
; kernel boot addresses (0x101020 lattice / 0x101010 tick), so this module is
; linkable and testable standalone, per this project's own religion:
; "push implementation as close to bare metal (.asm) as possible."
;
; Ω(n+1) = T(Ω(n)): prismatic recursion `slot = slot*3 + tick`, saturating at
; GOI=0xFFFF0000 (ceiling) and flooring at GUZ=0x00000100. Consensus (LOCK) is
; reached when the maximum per-slot deviation from the mean drops below
; mean/2 — the exact criterion this project's own golden-dome fine_tuned/
; FUDGE10 material also converges on for phase-lock detection.
;
; No SHA. No AES. No XOR. No libc. No stack frame beyond register saves.
; ============================================================================

BITS 64
section .text

; void phi_lattice_tick(uint32_t* lattice /*rdi*/, uint64_t* tick /*rsi*/);
; 128 x 32-bit slots at [rdi]; the caller's own tick counter at [rsi] drives
; the recursion (fold your guest's observed Δ into *tick before calling this
; -- the substrate never drives the guest, only observes it).
global phi_lattice_tick
phi_lattice_tick:
    push rbx
    inc  qword [rsi]
    mov  eax, 0xFFFF0000        ; GOI ceiling
    mov  rcx, 128
.pt_l:
    mov  ebx, [rdi]
    cmp  ebx, eax
    jae  .pt_goi
    imul ebx, ebx, 3
    add  ebx, dword [rsi]       ; + tick (low 32 bits) -- Ω(n+1)=T(Ω(n))
    cmp  ebx, 0x100              ; GUZ floor
    jae  .pt_store
    mov  ebx, 0x100
.pt_store:
    mov  [rdi], ebx
    jmp  .pt_next
.pt_goi:
    mov  dword [rdi], 0xFFFF0000
.pt_next:
    add  rdi, 4
    dec  rcx
    jnz  .pt_l
    pop  rbx
    ret

; int phi_lattice_consensus(uint32_t* lattice /*rdi*/);
; returns 1 in eax if LOCK (max deviation from mean < mean/2), else 0.
global phi_lattice_consensus
phi_lattice_consensus:
    push rbx
    push r12
    mov  r12, rdi                ; keep the base pointer (callee-saved)
    xor  eax, eax
    mov  rcx, 128
.pc_sum:
    add  eax, dword [rdi]
    add  rdi, 4
    dec  rcx
    jnz  .pc_sum
    shr  eax, 7                  ; eax = mean
    mov  rdi, r12
    mov  rcx, 128
    xor  ebx, ebx                ; ebx = max deviation
.pc_var:
    mov  edx, [rdi]
    sub  edx, eax
    jns  .pc_pos
    neg  edx
.pc_pos:
    cmp  edx, ebx
    jle  .pc_next
    mov  ebx, edx
.pc_next:
    add  rdi, 4
    dec  rcx
    jnz  .pc_var
    shr  eax, 1                  ; eax = mean/2
    xor  edx, edx
    cmp  ebx, eax
    setl dl                      ; dl = 1 if max_dev < mean/2
    movzx eax, dl
    pop  r12
    pop  rbx
    ret
