Skip to content

Headless runner: inspection commands, debug console output, flash cache profiling - #568

Merged
adriweb merged 3 commits into
CE-Programming:masterfrom
tommyb3ll:headless-tools
Dec 2, 2026
Merged

adriweb merged 3 commits into
CE-Programming:masterfrom
tommyb3ll:headless-tools

Conversation

@tommyb3ll

@tommyb3ll tommyb3ll commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

These are the emulator tools I used while speeding up FlowCE (a KhiCAS fork), rewritten for upstream. Three commits, each usable on its own:

  1. headless: peek, poke, keydown, keyup, regs, stats and lcd-dma. Read and write memory, hold a key while inspecting the calculator, see where a program is stuck (registers and IX frames), and read the cycle counters, flash cache misses included.
  2. core: a new opt-in define, DEBUG_CONSOLE_SUPPORT, prints the debug console without DEBUG_SUPPORT, so dbg_printf output can reach cemu-headless and the autotester (on stderr). It is off by default (a commented-out line in core/Makefile, like DEBUG_SUPPORT), so other consumers of the core are unaffected, as calc84maniac suggested.
  3. Flash cache profiling: per-line entry and miss counters (line-profile), an ordered trace of the lines entered for cache simulations (line-trace), and flash-cycles <n> to approximate pre-revision-M flash timing with a revision M ROM.

Everything is documented in headless_protocol.md. Tested on Linux with a TI-84 Plus CE ROM: each command on a running program; the line profile and the trace agree (same number of line entries); flash-cycles 10 makes a KhiCAS computation about 2.4 times slower; dbg_printf output appears on stderr when the core is built with -DDEBUG_CONSOLE_SUPPORT, and not without it.

For what it's worth, on KhiCAS cache misses were about 30% of the cycles, but a cache simulation of its trace showed that even a perfect code layout would remove only about a quarter of them.

Assisted by Claude code (plz don't discriminate just trying to help ;)

Comment thread core/flash.c Resolved
Comment thread tests/autotester/headless_cli.cpp Resolved
Comment thread tests/autotester/headless_cli.cpp Resolved
Comment thread core/mem.c Resolved
@adriweb

adriweb commented Oct 12, 2026 •

Copy link
Copy Markdown
Member

Meat proxying Codex's tests with this PR checked out:

Preserve the software-command setting when handling debug-console writes

The new console handler executes whenever either DEBUG_CONSOLE_SUPPORT or DEBUG_SUPPORT is defined. It intercepts writes to the debug-output addresses, prints the buffered message, and exits the switch with break before reaching the existing debug_get_flags() & DBG_SOFT_COMMANDS check.

Other findings:

  • Restarting a trace to the same path can corrupt it. It opens and truncates the new file before flushing the old stream. Reproduced: buffered entries [1,2,3], restart, then [4] produces [4,2,3].

  • Trace write failures silently report success. Both trace-writing paths ignore fwrite and fclose failures. A failed output stream still returns OK line-trace off, undermining the resulting cache analysis.

  • Fixed flash timing breaks the statistics. flash-cycles bypasses read accounting too. During a 100 ms run, total cycles increased by about 4.8 million while flash-reads and flash-delay stayed frozen. Reads and timing should remain accounted for independently of cache simulation.

@adriweb adriweb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(See PR comment)

@adriweb

adriweb commented Oct 20, 2026

Copy link
Copy Markdown
Member

If you're busy I suppose I can fix the remaining things in a new commit on top and merge that.

tommyb3ll and others added 3 commits December 2, 2026 18:09
For scripted tests and profiling of programs: read and write memory,
hold a key while inspecting the calculator, inspect registers and IX
frames, and read the cycle counters, including flash cache misses.

Reject unmapped poke ranges before any writes. Report flash accesses
with a counter that includes reads and writes.

Build and smoke-test the headless frontend on Linux.

Co-authored-by: Adrien Bertrand <bertrand.adrien@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Codex <codex@openai.com>
Add DEBUG_CONSOLE_SUPPORT to route calculator dbg_printf output through
the console callbacks even in builds without the debugger. The option
defaults to off. The headless runner and autotester write this output
to stderr.

Run the debugger handler first so DBG_SOFT_COMMANDS controls output in
debugger-only builds as well as debugger-open and console-clear commands.
An explicit DEBUG_CONSOLE_SUPPORT build can also emit console output
when software commands are off.

Co-authored-by: Adrien Bertrand <bertrand.adrien@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Codex <codex@openai.com>
Track entries and misses for each 32-byte serial flash cache line and
expose per-line counters and ordered traces through cemu-headless.
Finalize each trace before opening its replacement and report output
failures when writing or closing a trace or profile.

Allow a fixed cost per read to approximate pre-revision-M flash timing.
Account for accesses and delay in fixed mode, and require cached timing
for tracing and profiling.

In the original KhiCAS run, cache misses consumed roughly 30% of the
cycles. In simulation, a perfect layout removed about a quarter of
those misses.

Co-authored-by: Adrien Bertrand <bertrand.adrien@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Codex <codex@openai.com>
@adriweb
adriweb dismissed their stale review December 2, 2026 17:17

all good now

@adriweb

adriweb commented Dec 2, 2026

Copy link
Copy Markdown
Member

Squashed minor fixes/improvements into the commits, it's cleaner.

@adriweb
adriweb merged commit 5b829bf into CE-Programming:master Dec 2, 2026
@adriweb

adriweb commented Dec 2, 2026

Copy link
Copy Markdown
Member

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants