foxygit / fake-08-xbox360 Log in
commit 8184866ba7aa6c8e89d23759885494ce12047fa0
Author:     mrfox <jens.se@icloud.com>
AuthorDate: Tue Nov 24 21:19:50 2026 +0100
Commit:     mrfox <jens.se@icloud.com>
CommitDate: Tue Nov 24 21:19:50 2026 +0100

    Xbox 360: perf overlay, debounce setting, dev settings section

    - Performance overlay (fps, slow frames, emu/blit/vblank-wait times,
      resolution) drawn in the left border; also logged to pico.log every 5 s.
      Toggled from a new settings -> dev -> performance overlay option, shown
      only when the platform sets Host::hasPerfOverlay. Default off.
    - "button debounce" setting (off / 1-3 frames) replaces the fixed 3-poll
      release debounce, which added ~50 ms to every release and made Celeste
      feel laggy. Default 1 frame.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 platform/xenon/source/XenonHost.cpp | 95 +++++++++++++++++++++++++++++++++++--
 source/FakoSettings.h               | 17 +++++++
 source/host.h                       |  4 ++
 source/hostCommonFunctions.cpp      | 21 ++++++++
 4 files changed, 133 insertions(+), 4 deletions(-)

diff --git a/platform/xenon/source/XenonHost.cpp b/platform/xenon/source/XenonHost.cpp
index f32b9e1..1626ecd 100644
--- a/platform/xenon/source/XenonHost.cpp
+++ b/platform/xenon/source/XenonHost.cpp
@@ -297,6 +297,79 @@ static void findStorage() {
     if (!first.empty()) deviceRoot = first;
 }

+// ---------------------------------------------------------------------------
+// Performance overlay (settings -> dev -> performance overlay). Drawn in the black border left of the
+// game image; numbers are refreshed once per second.
+// ---------------------------------------------------------------------------
+#include <console/font_8x16.h>
+
+static bool perfOverlay = false;   // mirrors the 'perfoverlay' setting
+static uint64_t perfLastDrawEnd = 0;
+static uint64_t perfSleepTb = 0;          // timer-mode sleep inside the frame
+static uint64_t perfLastVblank = 0;
+static uint64_t perfWindowStart = 0;
+static uint64_t perfLogStart = 0;
+struct PerfAcc { uint64_t sum, max; void add(uint64_t v){ sum += v; if (v > max) max = v; } };
+static PerfAcc accEmu = {0,0}, accBlit = {0,0}, accWait = {0,0};
+static int perfFrames = 0, perfSlow = 0;
+static char perfLines[5][24] = {"", "", "", "", ""};
+
+static inline uint32_t tbToUs(uint64_t tb) { return (uint32_t)(tb / (PPC_TIMEBASE_FREQ / 1000000)); }
+
+static void drawText(int px, int py, const char *str) {
+    for (; *str; str++, px += 8) {
+        const unsigned char *glyph = &fontdata_8x16[(unsigned char)*str * 16];
+        for (int gy = 0; gy < 16; gy++) {
+            unsigned char bits = glyph[gy];
+            for (int gx = 0; gx < 8; gx++) {
+                fb[tiledIndex(px + gx, py + gy)] = (bits & (0x80 >> gx)) ? 0xFFFFFF00 : 0;
+            }
+        }
+    }
+}
+
+static void drawPerfOverlay() {
+    const int widthChars = 20, lines = 5;
+    const int w = widthChars * 8;
+    if (destX < w + 4) return;                   // no room beside the image
+    const int x = (destX - w) / 2;               // centred in the left border
+    const int y = 8 + safeY;
+    char blank[widthChars + 1];
+    memset(blank, ' ', widthChars); blank[widthChars] = 0;
+    for (int i = 0; i < lines; i++) {
+        drawText(x, y + i * 16, blank);
+        if (perfOverlay) drawText(x, y + i * 16, perfLines[i]);
+    }
+    const size_t tileRowBytes = (size_t)fbPitchW * 32 * 4;
+    int b0 = y >> 5, b1 = (y + lines * 16) >> 5;
+    memdcbst((uint8_t *)fb + b0 * tileRowBytes, (b1 - b0 + 1) * tileRowBytes);
+}
+
+static void perfFrameDone(uint64_t emuTb, uint64_t waitTb, uint64_t blitTb, uint64_t now) {
+    accEmu.add(emuTb); accWait.add(waitTb); accBlit.add(blitTb);
+    perfFrames++;
+    // a frame is "slow" if more than 1.5 refresh periods passed since the last one
+    if (perfLastVblank && (now - perfLastVblank) > (PPC_TIMEBASE_FREQ / 60) * 3 / 2) perfSlow++;
+    perfLastVblank = now;
+
+    if (now - perfWindowStart >= PPC_TIMEBASE_FREQ) {
+        int n = perfFrames ? perfFrames : 1;
+        snprintf(perfLines[0], 24, "%s %dfps slow %d", vsyncMode ? "vsync" : "timer", perfFrames, perfSlow);
+        snprintf(perfLines[1], 24, "emu  %5.1f/%5.1f", tbToUs(accEmu.sum / n) / 1000.0, tbToUs(accEmu.max) / 1000.0);
+        snprintf(perfLines[2], 24, "blit %5.1f/%5.1f", tbToUs(accBlit.sum / n) / 1000.0, tbToUs(accBlit.max) / 1000.0);
+        snprintf(perfLines[3], 24, "wait %5.1f/%5.1f", tbToUs(accWait.sum / n) / 1000.0, tbToUs(accWait.max) / 1000.0);
+        snprintf(perfLines[4], 24, "%dx%d %dx%d", fbWidth, fbHeight, destW, destH);
+        if (now - perfLogStart >= 5 * PPC_TIMEBASE_FREQ) {
+            Logger_Write("perf: %s | %s | %s | %s | %s (avg/max ms)\n", perfLines[0], perfLines[1], perfLines[2], perfLines[3], perfLines[4]);
+            perfLogStart = now;
+        }
+        accEmu = {0,0}; accBlit = {0,0}; accWait = {0,0};
+        perfFrames = 0; perfSlow = 0;
+        perfWindowStart = now;
+    }
+    drawPerfOverlay();
+}
+
 // ---------------------------------------------------------------------------
 // Host
 // ---------------------------------------------------------------------------
@@ -322,6 +395,7 @@ Host::Host(int windowWidth, int windowHeight) {

     hasControllerSettings = true;
     saveSettingsOnChange = true;
+    hasPerfOverlay = true;

     _logFilePrefix = base + "/";
     _cartDirectory = deviceRoot + "p8carts";
@@ -418,10 +492,10 @@ InputState_t Host::scanInput() {
     // BACK + START quits back to XeLL
     if (c.back && c.start) quit = 1;

-    // Debounce releases: a button only counts as released after it has read
-    // "up" for ReleaseFrames consecutive polls. Presses register immediately.
-    // Stops one physical press from being seen as several (menu skipping).
-    const int ReleaseFrames = 3;
+    // Debounce releases (setting "button debounce"): a button only counts as
+    // released after it has read "up" for debounce+1 consecutive polls.
+    // Presses register immediately. 0 = off.
+    const int ReleaseFrames = ((debounce >= 0 && debounce <= 3) ? debounce : 1) + 1;
     static uint8_t upCount[8] = {0};
     uint8_t debounced = 0;
     for (int i = 0; i < 8; i++) {
@@ -457,6 +531,7 @@ void Host::waitForTargetFps() {
     if (elapsed < targetFrameTb) {
         uint64_t remainingUs = (targetFrameTb - elapsed) / (PPC_TIMEBASE_FREQ / 1000000);
         if (remainingUs > 0) udelay((int)remainingUs);
+        perfSleepTb += remainingUs * (PPC_TIMEBASE_FREQ / 1000000);
         lastFrameTb += targetFrameTb;
     } else {
         // Running behind: don't try to catch up with a burst of frames
@@ -471,6 +546,11 @@ void Host::drawFrame(uint8_t *picoFb, uint8_t *screenPaletteMap, uint8_t drawMod
     // crashes easy to diagnose. From here on the screen belongs to the game.
     if (stdout_hook) stdout_hook = nullptr;

+    perfOverlay = perfoverlay == 1;
+    uint64_t tbStart = mftb();
+    uint64_t emuTb = perfLastDrawEnd ? (tbStart - perfLastDrawEnd - perfSleepTb) : 0;
+    perfSleepTb = 0;
+
     if (needClear) {
         fbClear();
         needClear = false;
@@ -491,11 +571,14 @@ void Host::drawFrame(uint8_t *picoFb, uint8_t *screenPaletteMap, uint8_t drawMod

     // 2. In vsync mode start writing at the beginning of vblank. The blit is
     //    much faster than scanout, so we stay ahead of the beam: no tearing.
+    uint64_t tbWaitStart = mftb();
     if (vsyncMode && !waitVBlankStart()) {
         // vblank stopped toggling (mode change?) - fall back to the timer
         if (++vsyncMisses > 10) vsyncMode = false;
     }

+    uint64_t tbBlitStart = mftb();
+
     // 3. nearest-neighbour scale into the tiled framebuffer, flushing each
     //    32-line tile band from the CPU cache as soon as it is complete.
     const size_t tileRowBytes = (size_t)fbPitchW * 32 * 4;
@@ -512,6 +595,10 @@ void Host::drawFrame(uint8_t *picoFb, uint8_t *screenPaletteMap, uint8_t drawMod
         }
     }
     memdcbst((uint8_t *)fb + band * tileRowBytes, tileRowBytes);
+
+    uint64_t tbEnd = mftb();
+    perfFrameDone(emuTb + (tbWaitStart - tbStart), tbBlitStart - tbWaitStart, tbEnd - tbBlitStart, tbBlitStart);
+    perfLastDrawEnd = mftb();
 }

 bool Host::shouldFillAudioBuff() {
diff --git a/source/FakoSettings.h b/source/FakoSettings.h
index 0aa55fa..b064f0a 100644
--- a/source/FakoSettings.h
+++ b/source/FakoSettings.h
@@ -86,6 +86,7 @@ function _init()
 	if __getsetting('hascontroller') == 1 then
 		settings['btnlayout'] = __getsetting('btnlayout') + 1
 		settings['deadzone'] = __getsetting('deadzone') + 1
+		settings['debounce'] = __getsetting('debounce') + 1
 		add(list[1].ops, {
 			name = 'controller layout',
 			vn = 'btnlayout',
@@ -96,6 +97,22 @@ function _init()
 			vn = 'deadzone',
 			ch ={'low','medium','high'}
 		})
+		add(list[1].ops, {
+			name = 'button debounce',
+			vn = 'debounce',
+			ch ={'off','1 frame','2 frames','3 frames'}
+		})
+	end
+
+	if __getsetting('hasperfoverlay') == 1 then
+		settings['perfoverlay'] = __getsetting('perfoverlay') + 1
+		add(list, {name='dev', ops = {
+			{
+				name = 'performance overlay',
+				vn = 'perfoverlay',
+				ch ={'off','on'}
+			}
+		}})
 	end

 	if showpackinoptions then
diff --git a/source/host.h b/source/host.h
index d19abd6..825637d 100644
--- a/source/host.h
+++ b/source/host.h
@@ -83,9 +83,13 @@ class Host {
     bool hasControllerSettings = false;
     int btnlayout = 0;   // 0: confirm/jump on bottom face button, 1: swapped
     int deadzone = 1;    // 0: low, 1: medium, 2: high
+    int debounce = 1;    // extra polls a button must read 'up' before release counts (0-3)
     // write settings.ini on every change (for consoles that are usually just
     // switched off instead of exited cleanly)
     bool saveSettingsOnChange = false;
+    // developer settings (shown in a 'dev' section when hasPerfOverlay is set)
+    bool hasPerfOverlay = false;
+    int perfoverlay = 0; // 0: off, 1: on

     float scaleX = 1.0;
     float scaleY = 1.0;
diff --git a/source/hostCommonFunctions.cpp b/source/hostCommonFunctions.cpp
index ff19c61..ec18c3e 100644
--- a/source/hostCommonFunctions.cpp
+++ b/source/hostCommonFunctions.cpp
@@ -168,6 +168,10 @@ void Host::loadSettingsIni(){
 	//controller
 	btnlayout = (int)settingsIni.GetLongValue("settings", "btnlayout", 0);
 	deadzone = (int)settingsIni.GetLongValue("settings", "deadzone", 1);
+	debounce = (int)settingsIni.GetLongValue("settings", "debounce", 1);
+
+	//dev
+	perfoverlay = (int)settingsIni.GetLongValue("settings", "perfoverlay", 0);
 }

 void Host::saveSettingsIni(){
@@ -188,6 +192,11 @@ void Host::saveSettingsIni(){
     if (hasControllerSettings) {
         settingsIni.SetLongValue("settings", "btnlayout", btnlayout);
         settingsIni.SetLongValue("settings", "deadzone", deadzone);
+        settingsIni.SetLongValue("settings", "debounce", debounce);
+    }
+
+    if (hasPerfOverlay) {
+        settingsIni.SetLongValue("settings", "perfoverlay", perfoverlay);
     }

     std::string settingsIniStr = "";
@@ -281,6 +290,12 @@ int Host::getSetting(std::string sname) {
 		return btnlayout;
 	}else if(sname == "deadzone"){
 		return deadzone;
+	}else if(sname == "debounce"){
+		return debounce;
+	}else if(sname == "hasperfoverlay"){
+		return hasPerfOverlay ? 1 : 0;
+	}else if(sname == "perfoverlay"){
+		return perfoverlay;
 	}else if(sname == "p8_bgcolor"){

 		Logger_Write("Returning pico 8 bg color setting\n");
@@ -356,6 +371,12 @@ void Host::setSettingValue(std::string sname, int sval) {
 	}else if(sname == "deadzone"){
 		Logger_Write("setting deadzone\n");
 		deadzone = sval;
+	}else if(sname == "debounce"){
+		Logger_Write("setting debounce\n");
+		debounce = sval;
+	}else if(sname == "perfoverlay"){
+		Logger_Write("setting perfoverlay\n");
+		perfoverlay = sval;
 	}else if(sname == "packinloaded"){
 		Logger_Write("setting packinloaded\n");