19 Commits
Author SHA1 Message Date
Thomas MartinandClaude Opus 5.5 ad38240bb8 Kernel: the next vblank a field after the last when the field's length changes
Counted from the start (vblanks x the field), a field-rate change after
many fields of another length put the next vblank far off: Shinobido at
60 (NTSC's fields) switching to PAL's for a movie waited 16 s for its
next vblank, the movie's sound polling for it. Now each vblank is a field
after the one before, and a change re-spaces the pending one. A rate
that never changes gets the same vblanks as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-30 15:21:23 +01:00
Thomas MartinandClaude Opus 5.5 fd67f9e899 libmpeg: callbacks on the caller's thread (MpegOptions::callbacksOnCaller)
Sony's libmpeg calls the stream callbacks inside sceMpegDemuxPss / sceMpegDemuxPssRing and Nodata inside
sceMpegGetPicture, on the caller's thread. The native library queued them for whichever thread ran next, which
deadlocks Steambot Chronicles' player (Sony's sample): its video callback waits on the IPU input ring's lock held by
the thread it was stacked on. With the option on (off by default), each callback is a handler bound to the calling
thread and the call is made again after it: the demux going on from where it was, GetPicture calling Nodata again
until a picture is there.

- Kernel::queue(handler, thread): a handler that runs only on that thread; handlerQueuedFor(thread) for the
  dispatcher's checks; the bindings saved in an optional section (older states load with none).
- Libraries::queueHandlerOn and restartCall: the free host stops the thread at the call's own address.
- MpegState::demuxResuming / demuxBytes, saved as an optional tail of the mpeg section.
- Tests: thread-bound handlers (handler_stacks), the demux and Nodata loop (mpeg_callbacks).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-29 13:39:05 +01:00
Thomas MartinandClaude Opus 5.5 0058e8234e Kernel: SetupThread's stack -1; libcdvd: sceGetstat
- SetupThread with stack -1 (the SDK crt0's default _stack, Shinobido's)
  asks for the end of RAM: the stack is the size bytes below 32 MB.
  It returned -1 + size, an odd stack pointer inside the program.
- sceGetstat(name, struct sce_stat *): a disc file's size as a read-only
  regular file (mode, size, its first sector in private[0]); -1 when the
  file is not on the disc. The image is opened first, as sceOpen does.
  Shinobido checks each file with it before reading it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-23 16:32:45 +01:00
Thomas MartinandClaude Opus 5.5 b73692ed37 Save states: the whole runtime saved and loaded at the dispatcher's safe point
A state is taken at the top of Dispatcher::run's loop, where no guest code is on
the host stack: memory, the dispatcher, the kernel (threads, frames, queues,
deadlines), every library's state, the native IOP host (memory, tasks, kernel
objects, RPC servers, mixer, libsd) and sections ports add (state::Sections).

Parked library lambdas become data: Resume records and native deadline kinds,
rebuilt from tables on load; ports can add their own kinds. FreeHost serves
requestSave / requestLoad from any thread, rolls back a failed load, and takes
FreeOptions (gameTag, loadState[Frame], saveState[Frame]). Windows get ten slots:
F5 save, F7 load, F6 / Shift+F6 select, and a Save states tab in F2.

989SND's engine and a playing movie are not covered: a save waits, saying why.
docs/save-states.md tells a port how to use and check it; tests/runtime/state_tests.cpp
covers the archive, files and a dispatcher round trip.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-18 18:22:15 +01:00
ArzarothandClaude Opus 5.5 42833a68c4 [ffop/iop-host-imports] feat(kernel): a port can have the sync-V callback at each vblank's start
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-12 03:12:44 +01:00
Thomas MartinandClaude Opus 5.5 c4de93f1d3 Owned handler stacks: a SetupThread of size 0 declares the stack below its top
Twisted Metal: Black's crt0 calls SetupThread(0x8ff800, 0): the address is
the stack pointer and the stack grows below it, undeclared. The owned
handler stacks (PR #75) need bounds, so every native callback threw
"native handler requires a declared thread stack". The kernel now takes
kUndeclaredStackBytes (1 MB) below such a top as the thread's stack. The
call's result is unchanged; a top too low keeps the declaration as given.

The handler-stack test covers the declaration, a callback below the live
SP, and one below a parked SP while the guest uses SP as data (TMB's
hierTraverseAsm, parked by the port).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-10 13:28:49 +01:00
Thomas Martin 666aecb71a Use owned thread stacks for native EE callbacks 2026-11-08 13:23:27 +01:00
ArzarothandClaude Opus 5.5 582a8d7249 [sc3/libs] feat(kernel): deadlines that run native code, 64-bit handler arguments, the delay wait
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-06 21:40:43 +01:00
ArzarothandClaude Opus 5.5 a634661960 [sc3/kernel-syscalls] feat(kernel): SetAlarm and ReleaseAlarm on the native deadlines
Alarms are deadlines in EE cycles, one key per alarm id; the time is counted in
horizontal blanks of the field rate the game set. No timer or compare register
is behind them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-06 20:58:56 +01:00
ArzarothandClaude Opus 5.5 b21a787d50 [sc3/kernel-syscalls] feat(kernel): GsGetIMR/GsPutIMR, EndOfHeap, SetGsCrt, OSD configuration, Suspend/ResumeThread answered natively
The kernel calls SC3's libkernel and libgraph wrappers reach by number that
faulted so far. The IMR is a stored mask with no device behind it; EndOfHeap
comes from SetupHeap and SetupThread; the OSD words are the dispatcher's own.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-06 20:58:56 +01:00
boubou666andThomas Martin 4f6555d948 Schedule native callback deadlines in kernel event loop (#10)
Co-authored-by: Thomas Martin <[email protected]>
2026-11-06 15:01:52 +01:00
boubou666andThomas Martin 32b13ce75e Implement native SDK thread sleep and wakeup services (#9)
Co-authored-by: Thomas Martin <[email protected]>
2026-11-06 14:51:05 +01:00
Thomas Martin 11cb6df323 Add reusable native image, compiled-library and port services 2026-11-06 02:47:13 +01:00
Thomas MartinandClaude Opus 5.5 24292a623e Kernel: a mask of the non-empty ready queues, for best() and sliceContender()
firstReady() scanned the 128 priority queues and sliceContender() up to the running thread's priority at every
pick, every transfer and every updateLimit (each out-call): Kernel::best was 2.6% of the game thread in a 1P match.
The mask is kept after every change to a queue; the choices are the same.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 23:15:23 +01:00
Thomas MartinandClaude Opus 5.5 a8bef92557 Native kernel and libraries from match4p and the stages (phase 4 M5): TerminateThread and DeleteThread of an unknown thread -407 (KE_UNKNOWN_THID); RemoveDmacHandler / RemoveIntcHandler 0 also for a handler not there; DeleteSema makes its operation for an unknown id too; sceSifFreeIopHeap -1 for a block not allocated (the movie player torn down twice, match4p #11334755-#11334788); sceMcGetInfo of a multitap slot: no card, type, free and format 0, result -4 (#228107); printf's %p as the reference's host printed it, 16 hex digits (#16615141, the stages' "toc starts at")
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 19:46:59 +01:00
Thomas MartinandClaude Opus 5.5 60a8d50e1f Phase 4 M0: a port's overrides run natively (bozo/port.h), the free host, the renderer's game side and the packs moved
TMB_Recomp's runtime builds on BozoEngine (its branch bozo/m0, cmake -DTMB_BOZO=ON): its overrides run natively, each
call checked against the reference's log (tmb-check), and with no log at all (tmb-headless).

- Port API (runtime/include/bozo/port.h, bozo_port): Registry::registerFunction, BOZO_PORT, the startup check that
  every registered override is an external of the generated code (port::undeclared), and what overrides use: the
  kernel (changePriority, rotateReadyQueue, transfer), the EE clock (now, accountCycles), vblankCount, the hardware
  registers (ioRead32 / ioWrite32: a port's own access), frameEnd, callLibrary, scratchpad, libraries, deterministic,
  realTime, setStorePatch. A port::Host is behind them.
- Overrides that run guest code (kernel.md section 3b): checkpoints charge, no event is taken in place; an event due,
  a call that blocks, a jump through a register, an inner call's return and a recursive call's return make guest code
  leave the override there, the stop deferred to its end (Dispatcher::beginOutcall / leave / takeDeferredStop); the
  override's own code runs on, as the reference's did; no exception goes through it. Every rule settled on a record.
- Generated code: bozo::checkpoint and bozo::innerReturn return true when guest code leaves, the check after each
  call tests ctx->leaving, a recursive call's return inside an override calls bozo_leave; ctx->outcall,
  ctx->leaving; the externals table (<prefix>_external_table); "store 0xADDR" externals (the store sets ctx->pc first).
- bozo-externals (tools/externals): the --externals list from the ELF's symbols BozoEngine answers, a port's stub names
  and its override list; --check compares the stubs with a call log's.
- bozo-run as a library (bozo_run, run_main.h): a port's checker links it with its generated code and overrides,
  which run natively (--overrides native|log); LogHost checks each call (context words, memory runs, traced calls, the
  operations and frame end inside, where it left) and pins where the reference's override left and whether a timed
  call inside it waited. The renderer's draw lists fill the record's draws (reported).
- The free host (runtime/host/free_host.cpp, bozo_host): every out-call native, libraries bound by the ELF's symbols,
  free timing, a watchdog when no frame ends.
- Moved from the team's fork code (phase4-inventory.md 2.1, MIT headers): the renderer's game side
  (bozo/render/renderer.h, renderer.cpp: headless, recording the published lists and their hash), Texture,
  the texture and mesh packs, mesh_draw, the movie picture hook, the FramePacer (unpaced off real time) and
  determinism.h (settable by the host).
- Libraries, from what only ran inside overrides before: sceGsExecLoadImage (0, no upload), T8H/T4HL/T4HH image tags as
  32-bit texels, the GS CSR's FIELD, sceSifSetDma raising SIF0's DMAC interrupt, CPCOND0 counting completions from
  the last FlushCache, a port's own register accesses, RotateThreadReadyQueue returning 0, libmpeg calling the movie
  picture hook, PadState::multitap.
- BOZO_BUILD_TOOLS (zlib-free configure for a port), the tests only when top level.

Results: TMB boot --pinned with every override native, 3000 of 3000 frames and 4,650,558 of 4,650,558 records (ram,
scratchpad, calls, threads); match1p: see docs/phase4-m0-notes.md. bozo-run without a port still matches.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 11:02:09 +01:00
Thomas MartinandClaude Opus 5.5 8e12b20047 Pinned mode: the vblank's events at the log's points (phase 2 exit check)
bozo-run --pinned takes each asynchronous event at its first record's
point in the log (out-calls before, checkpoints since the last one, pc)
instead of when the EE clock reaches it; the kernel's decisions stay
native and every record is still checked in lockstep. EE time and
checkpoint counts are reported, not checked; eeCycle is reported only.
Inside inner calls the logged pc is the reference's lagging context pc
(a checkpoint passed lately); an event whose count is reached one
checkpoint early is taken at the next one, at the logged pc (match1p
#50893616, #50900546). Free mode keeps the clock-driven cycle model.

boot pinned: 3000/3000 frames, all records. match1p pinned: 16000/16000
frames, all 74,615,923 records. Free mode unchanged (boot 3000/3000,
match1p 5786).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 16:29:50 +01:00
Thomas MartinandClaude Opus 5.5 ac640ebe3d match1p: frames 0-5590 equal to match1p.ref (boot still all 3000)
The fixes, each from the match1p record that showed it:
- time slice: it starts when the dispatcher picks a thread out of the ready queue, not when a handler stacked on the
  running thread returns (#14760095: thread 17 preempted 65,536 cycles after it was picked, an interrupt between);
- a recursive call to the function's own start is an ordinary call (#17531044, _M_erase resumed mid-recursion: its
  lost levels return through the dispatcher);
- a jump table the reference resolves needs an sltiu range check besides a base built in place (#17536212: the
  table at 0x17f304 has none and goes through the dispatcher);
- falling through into the next function is not charged (sysZeroQBuf into sysZeroLabelA, a label the reference does
  not end a function at);
- bozo-run reports the guest sp with a divergence.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 14:19:26 +01:00
Thomas MartinandClaude Opus 5.5 c53d7f231c Phase 2: the EE kernel, the dispatcher and bozo-run (TMB boot: frames 0-1614 equal to boot.ref)
runtime/ee: BozoEngine's own EE kernel (threads, ready queues, semaphores, INTC and DMAC handlers and their
queue, the vblank, the kernel calls TMB makes) and dispatcher (runs guest code on the kernel's threads and stacked
handlers, the reference's cycle model, stops by unwinding and resumes through the resume entries, time slices).
runtime/harness: the PS2XREF1 record (writer, reader, the calls and threads hashes). bozo/hash.h: XXH64, FNV-1a.

tools/run: bozo-run, TMB's generated code on that kernel with every other out-call answered from a call log and
every decision checked record by record against it; bozo-calllog-tool lists a log's external addresses for the
generator. Recompiler: jumps through a register go through bozo_jump (the reference's dispatcher), except the jump
tables it resolves itself (built in place); direct tail jumps are not charged; tail calls to externals go through
bozo_tail_call (the reference's record of calls only has jal and jalr).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 13:03:35 +01:00