// ============================================================================
// HDGL SUBSTRATE CORE — header
// The substrate ticks; Omega = Fix(T) = phi is re-settled each tick; faces are
// projections stepped one quantum per tick with phi-fold-derived privacy.
// ============================================================================
#ifndef HDGL_SUBSTRATE_H
#define HDGL_SUBSTRATE_H
#include <lib/stdint.h>

namespace hdgl {

struct Substrate {
  uint32_t     tau;      // the substrate tick counter (advances every tick)
  long double  S;        // seed / living state (Delta-perturbed)
  long double  T;        // T(S) = 1 + 1/S
  long double  Omega;    // = Fix(T) = phi  (RE-SETTLED each tick, not stored)
  long double  Psi;      // = -1/Omega
  int          Lambda;   // settle depth (iterations to reach the fixed point)
  int          Nphi;     // trinary (-1)^tau  in {+1,-1}
  uint32_t     genome;   // settle genome = phi_fold(seed ^ session)
  uint32_t     session;  // per-boot session coordinate (from RDTSC/CPUID)
};

extern Substrate g_sub;

uint32_t phi_fold(uint32_t x);
void     substrate_init(uint32_t session_coord);
void     substrate_tick(void);

// ── FACE TABLE ──────────────────────────────────────────────────────────
// A face is a projection of the substrate, stepped one quantum per tick.
// Privacy token = phi_fold(genome ^ session ^ face_id): faces cannot read each
// other (derived, disjoint), and it differs every boot.
#define HDGL_MAX_FACES 8

typedef void (*face_step_fn)(void* self, uint32_t face_id, uint32_t priv_token);

struct Face {
  int          active;
  uint32_t     id;
  uint32_t     priv_token;    // phi_fold(genome ^ session ^ id)
  void*        self;
  face_step_fn step;          // advance ONE quantum (interleaved with all faces)
};

extern Face   g_faces[HDGL_MAX_FACES];
extern int    g_face_count;

uint32_t face_register(void* self, face_step_fn step);  // returns face_id
void     face_step_all(void);                           // one quantum each

} // namespace hdgl
#endif
