diff options
author | polwex <polwex@sortug.com> | 2025-10-06 10:30:19 +0700 |
---|---|---|
committer | polwex <polwex@sortug.com> | 2025-10-06 10:30:19 +0700 |
commit | 4be1d7f999ffb3eb1c12c54e863b141af21b3fbf (patch) | |
tree | 6e33b141cd98985799e02a253dddcf201fec6b74 /ocaml/lib/boot.ml | |
parent | c3545b7ba9e8448226417fab6edaa2d039c9babe (diff) |
some progress but man
Diffstat (limited to 'ocaml/lib/boot.ml')
-rw-r--r-- | ocaml/lib/boot.ml | 130 |
1 files changed, 128 insertions, 2 deletions
diff --git a/ocaml/lib/boot.ml b/ocaml/lib/boot.ml index e56c114..92e4907 100644 --- a/ocaml/lib/boot.ml +++ b/ocaml/lib/boot.ml @@ -38,12 +38,18 @@ let load_pill ~fs pill_path = let file_path = Eio.Path.(fs / pill_path) in let pill_bytes = Eio.Path.load file_path |> Bytes.of_string in - Printf.printf "[Boot] Pill file: %d bytes\n%!" (Bytes.length pill_bytes); + Printf.printf "[Boot] Pill file: %d bytes (%.1f MB)\n%!" + (Bytes.length pill_bytes) + (float_of_int (Bytes.length pill_bytes) /. 1024.0 /. 1024.0); + + Printf.printf "[Boot] Cuing pill (this may take a while)...\n%!"; + let start = Unix.gettimeofday () in (* Cue the pill to get kernel noun *) let kernel_noun = Serial.cue pill_bytes in - Printf.printf "[Boot] Pill cued successfully\n%!"; + let elapsed = Unix.gettimeofday () -. start in + Printf.printf "[Boot] Pill cued successfully in %.2f seconds\n%!" elapsed; (* For now, treat the entire pill as the kernel * In a real implementation, we'd parse the structure: @@ -114,3 +120,123 @@ let boot_fake state = Printf.printf "[Boot] Creating fake minimal kernel...\n%!"; let pill = fake_pill () in boot_from_pill state pill + +(* u3v_life: Execute lifecycle formula on ivory core + * + * From C Vere vortex.c:26: + * u3_noun lyf = u3nt(2, u3nc(0, 3), u3nc(0, 2)); // [2 [0 3] [0 2]] + * u3_noun gat = u3n_nock_on(eve, lyf); + * u3_noun cor = u3k(u3x_at(7, gat)); + * + * The lifecycle formula [2 [0 3] [0 2]] means: + * - Opcode 2: nock on computed subject + * - [0 3] gets the formula at slot 3 + * - [0 2] gets the sample at slot 2 + * This calls the lifecycle arm, then we extract slot 7 (context) + *) +let life eve = + try + (* Build lifecycle formula: [2 [0 3] [0 2]] *) + let lyf = Noun.cell (Noun.atom 2) + (Noun.cell + (Noun.cell (Noun.atom 0) (Noun.atom 3)) + (Noun.cell (Noun.atom 0) (Noun.atom 2))) in + + Printf.printf "[Boot] Running lifecycle formula [2 [0 3] [0 2]]...\n%!"; + + (* Debug: check what's in slot 2 and slot 3 *) + (try + let slot2 = Noun.slot (Z.of_int 2) eve in + let slot3 = Noun.slot (Z.of_int 3) eve in + Printf.printf "[Boot] Slot 2: %s\n%!" + (if Noun.is_cell slot2 then "cell" else "atom"); + Printf.printf "[Boot] Slot 3: %s\n%!" + (if Noun.is_cell slot3 then "cell" else "atom"); + with _ -> ()); + + (* Run lifecycle formula on ivory core *) + let gat = + try + Nock.nock_on eve lyf + with e -> + Printf.printf "[Boot] ✗ Nock failed during lifecycle: %s\n%!" + (Printexc.to_string e); + raise e + in + + Printf.printf "[Boot] ✓ Lifecycle formula completed\n%!"; + Printf.printf "[Boot] Result is: %s\n%!" + (if Noun.is_cell gat then "cell" else "atom"); + + (* Extract slot 7 (the context) from resulting gate *) + let cor = + try + Noun.slot (Z.of_int 7) gat + with e -> + Printf.printf "[Boot] ✗ Failed to extract slot 7: %s\n%!" + (Printexc.to_string e); + Printf.printf "[Boot] (Result type: %s)\n%!" + (if Noun.is_cell gat then "cell" else "atom"); + raise e + in + + Printf.printf "[Boot] ✓ Extracted slot 7 from result\n%!"; + cor + + with e -> + Printf.printf "[Boot] ✗ u3v_life failed: %s\n%!" (Printexc.to_string e); + raise e + +(* Boot from ivory pill - the lightweight boot sequence + * + * Ivory pills have structure: ["ivory" core] + * The core contains a lifecycle formula that must be executed + *) +let boot_ivory ~fs state pill_path = + Printf.printf "[Boot] Booting from ivory pill...\n%!"; + + match load_pill ~fs pill_path with + | Error err -> + let msg = match err with + | FileNotFound s -> "File not found: " ^ s + | InvalidPill s -> "Invalid pill: " ^ s + | BootFailed s -> "Boot failed: " ^ s + in + Printf.printf "[Boot] Error: %s\n%!" msg; + Error msg + + | Ok pill -> + (* Check if pill has ivory tag *) + if not (Noun.is_cell pill.kernel) then + Error "Ivory pill must be a cell" + else begin + let hed = Noun.head pill.kernel in + let tal = Noun.tail pill.kernel in + + (* Check for "ivory" tag *) + (* "ivory" as cord (little-endian): 0x79726f7669 = 521610950249 *) + let ivory_tag = Z.of_string "521610950249" in + + match hed with + | Noun.Atom z when Z.equal z ivory_tag -> + Printf.printf "[Boot] ✓ Found ivory tag\n%!"; + Printf.printf "[Boot] Running lifecycle formula...\n%!"; + + (try + let start = Unix.gettimeofday () in + let core = life tal in + let elapsed = Unix.gettimeofday () -. start in + + Printf.printf "[Boot] ✓ Lifecycle completed in %.4fs\n%!" elapsed; + Printf.printf "[Boot] Setting Arvo core...\n%!"; + + State.boot state core; + Printf.printf "[Boot] ✓ Ivory pill booted!\n%!"; + Ok () + with e -> + Error ("Lifecycle failed: " ^ Printexc.to_string e)) + + | _ -> + Printf.printf "[Boot] Warning: No ivory tag found, trying regular boot...\n%!"; + boot_from_pill state pill + end |