foxygit / fake-08-xbox360 Log in
commit 9c13c5bd7601ea1769aae1c10515f828dbaa1cad
Author:     mrfox <jens.se@icloud.com>
AuthorDate: Wed Nov 25 18:18:42 2026 +0100
Commit:     mrfox <jens.se@icloud.com>
CommitDate: Wed Nov 25 18:18:42 2026 +0100

    BIOS: file browser start menu

    A list of the folder (../, folders, then carts; 16 rows, scroll bar,
    selection in inverse colours) replaces the one-cart-at-a-time classic menu:
    up/down move, left/right page, openkey (X by default) runs a cart or opens
    a folder, the other button goes up a folder. The Xbox hosts set openkey so
    that A opens and B goes back in either button layout.

    Test: the stub host can list carts and folders; a new test renders the
    browser (FAKE08_BIOS_PNG=<file> writes the screen).

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 platform/xenon/source/XenonHost.cpp |   7 +-
 platform/xex/source/XexHost.cpp     |   7 +-
 source/DefaultCart.h                | 194 +++++++++++++++++++++++++++++++++++-
 test/biosbrowsertests.cpp           |  56 +++++++++++
 test/stubhost.cpp                   |  14 ++-
 test/stubhost.h                     |   7 ++
 6 files changed, 271 insertions(+), 14 deletions(-)

diff --git a/platform/xenon/source/XenonHost.cpp b/platform/xenon/source/XenonHost.cpp
index b157b46..420c8ef 100644
--- a/platform/xenon/source/XenonHost.cpp
+++ b/platform/xenon/source/XenonHost.cpp
@@ -690,9 +690,12 @@ const char *Host::logFilePrefix() {
 }

 std::string Host::customBiosLua() {
-    // The BIOS runs a cart with X: that is B in layout 0 and A in layout 1.
     return "cartpath = \"" + _cartDirectory + "/\"\n"
-           "selectbtn = \"" + std::string(btnlayout == 1 ? "a" : "b") + "\"\n"
+           // file browser: A opens and B goes up a folder in either layout
+           // (layout 0: A is O, layout 1: A is X)
+           "selectbtn = \"a\"\n"
+           "backbtn = \"b\"\n"
+           "openkey = " + std::string(btnlayout == 1 ? "5" : "4") + "\n"
            "pausebtn = \"start\"\n"
            "exitbtn = \"back + start\"\n"
            "sizebtn = \"back to cycle screen sizes\"";
diff --git a/platform/xex/source/XexHost.cpp b/platform/xex/source/XexHost.cpp
index 5622bf6..e70a304 100644
--- a/platform/xex/source/XexHost.cpp
+++ b/platform/xex/source/XexHost.cpp
@@ -750,9 +750,12 @@ const char *Host::logFilePrefix() {
 }

 std::string Host::customBiosLua() {
-    // The BIOS runs a cart with X: that is B in layout 0 and A in layout 1.
     return "cartpath = \"" + _cartDirectory + "/\"\n"
-           "selectbtn = \"" + std::string(btnlayout == 1 ? "a" : "b") + "\"\n"
+           // file browser: A opens and B goes up a folder in either layout
+           // (layout 0: A is O, layout 1: A is X)
+           "selectbtn = \"a\"\n"
+           "backbtn = \"b\"\n"
+           "openkey = " + std::string(btnlayout == 1 ? "5" : "4") + "\n"
            "pausebtn = \"start\"\n"
            "exitbtn = \"xbox button > home\"\n"
            "sizebtn = \"back to cycle screen sizes\"";
diff --git a/source/DefaultCart.h b/source/DefaultCart.h
index e9388a0..2333c14 100644
--- a/source/DefaultCart.h
+++ b/source/DefaultCart.h
@@ -19,6 +19,8 @@ __lua__
 --start customizable per platform
 cartpath = "sdmc:/p8carts/"
 selectbtn = "a"
+backbtn = "b"
+openkey = 5
 pausebtn = "start"
 versionstr = ")#" VER_STR  R"#("
 exitbtn = "l + r"
@@ -751,10 +753,192 @@ function list_draw()
 end


+-->8
+--browser: a file list (folders, then carts)
+
+browser_rows = 16
+
+function browser_basename(p)
+	local i = findlastchar(p, "/")
+	if i then
+		return sub(p, i + 1)
+	end
+	return p
+end
+
+function browser_sort(a)
+	for i = 2, #a do
+		local v = a[i]
+		local j = i - 1
+		while j >= 1 and a[j] > v do
+			a[j + 1] = a[j]
+			j -= 1
+		end
+		a[j + 1] = v
+	end
+end
+
+function browser_init()
+	if __getsetting then
+		pal(1, __getsetting('p8_bgcolor'))
+		pal(7, __getsetting('p8_textcolor'))
+	end
+	bioserror = ""
+	if __getbioserror then
+		bioserror = __getbioserror()
+	end
+	browser_refresh()
+end
+
+function browser_refresh()
+	entries = {}
+	currentdir = ""
+	if __pwd then
+		currentdir = __pwd()
+	end
+	add(entries, {kind = "up", label = "../"})
+
+	local dirs = {}
+	if __listdirs then
+		for v in all(__listdirs()) do
+			add(dirs, v)
+		end
+	end
+	browser_sort(dirs)
+	for v in all(dirs) do
+		add(entries, {kind = "dir", label = v .. "/", name = v})
+	end
+
+	local carts = {}
+	if __listcarts then
+		carts = __listcarts()
+	end
+	numcarts = #carts
+	for v in all(carts) do
+		add(entries, {kind = "cart", label = browser_basename(v), name = v})
+	end
+
+	-- start on the first cart, else on the first folder
+	sel = 1
+	if numcarts > 0 then
+		sel = #entries - numcarts + 1
+	elseif #entries > 1 then
+		sel = 2
+	end
+	top = 1
+	browser_scroll()
+end
+
+function browser_scroll()
+	if sel < top then
+		top = sel
+	end
+	if sel > top + browser_rows - 1 then
+		top = sel - browser_rows + 1
+	end
+end
+
+function browser_cd(name)
+	if __cd then
+		__cd(name)
+		browser_refresh()
+	end
+end
+
+function browser_update()
+	local n = #entries
+	if btnp(2) then
+		if sel > 1 then sel -= 1 else sel = n end
+	end
+	if btnp(3) then
+		if sel < n then sel += 1 else sel = 1 end
+	end
+	if btnp(0) then
+		sel = max(1, sel - browser_rows)
+	end
+	if btnp(1) then
+		sel = min(n, sel + browser_rows)
+	end
+	browser_scroll()
+
+	-- openkey (❎ by default) opens, the other of 🅾️/❎ goes up a folder
+	if btnp(openkey) then
+		bioserror = ""
+		local e = entries[sel]
+		if e.kind == "cart" then
+			if load then
+				load(e.name)
+			end
+		elseif e.kind == "dir" then
+			browser_cd(e.name)
+		else
+			browser_cd("..")
+		end
+	elseif btnp(9 - openkey) then
+		bioserror = ""
+		browser_cd("..")
+	end
+end
+
+function browser_draw()
+	cls()
+	-- rectfill, not cls(1): the background follows pal(1, bgcolor)
+	rectfill(0, 0, 127, 127, 1)
+	spr(128, 1, 1, 6, 1)
+	print(versionstr, 128 - #versionstr * 4, 3, 6)
+
+	local d = currentdir
+	if #d > 31 then
+		d = sub(d, #d - 30)
+	end
+	print(d, 1, 11, 6)
+	line(0, 17, 127, 17, 6)
+
+	for r = 0, browser_rows - 1 do
+		local i = top + r
+		local e = entries[i]
+		if e then
+			local y = 19 + r * 6
+			local c = 7
+			if e.kind != "cart" then
+				c = 6
+			end
+			if i == sel then
+				rectfill(0, y - 1, 124, y + 5, 7)
+				c = 1
+			end
+			print(sub(e.label, 1, 30), 2, y, c)
+		end
+	end
+	if numcarts == 0 and #entries < browser_rows then
+		print("(no carts in this folder)", 2, 19 + #entries * 6 + 2, 6)
+	end
+
+	-- scroll bar
+	local n = #entries
+	if n > browser_rows then
+		local h = max(4, flr(browser_rows * 96 / n))
+		local y = 18 + flr((top - 1) * (96 - h) / (n - browser_rows))
+		rectfill(126, y, 127, y + h, 6)
+	end
+
+	line(0, 115, 127, 115, 6)
+	if bioserror != "" then
+		print(sub(bioserror, 1, 32), 0, 117, 8)
+	else
+		local o, b = "❎", "🅾️"
+		if openkey == 4 then
+			o, b = "🅾️", "❎"
+		end
+		print(o .. "(" .. selectbtn .. ") open " .. b .. "(" .. backbtn .. ") back ⬅️➡️ page", 0, 117, 6)
+	end
+	print(exitbtn .. ": exit", 0, 123, 6)
+end
+
 -->8
 --runner

-inits = {classic_init,fancy_init,list_init}
+inits = {classic_init,fancy_init,list_init,browser_init}
 function _init()
 	--disabling menu styles - they are currently broken
 	--if __getsetting then
@@ -764,17 +948,17 @@ function _init()
 	--end
 	--theme = theme % #inits --don't load a nonexistant menu style

-	theme = 0
-
+	theme = 3   -- the file browser
+
 	inits[theme+1]()
 end

-updates = {classic_update,fancy_update,list_update}
+updates = {classic_update,fancy_update,list_update,browser_update}
 function _update60()
 	updates[theme+1]()
 end

-draws = {classic_draw,fancy_draw,list_draw}
+draws = {classic_draw,fancy_draw,list_draw,browser_draw}
 function _draw()
 	draws[theme+1]()
 end
diff --git a/test/biosbrowsertests.cpp b/test/biosbrowsertests.cpp
new file mode 100644
index 0000000..9d269e8
--- /dev/null
+++ b/test/biosbrowsertests.cpp
@@ -0,0 +1,56 @@
+#include <string>
+#include <vector>
+#include <cstdlib>
+
+#include "doctest.h"
+#include "../libs/lodepng/lodepng.h"
+
+#include "../source/vm.h"
+#include "../source/host.h"
+#include "../source/hostVmShared.h"
+#include "../source/nibblehelpers.h"
+#include "stubhost.h"
+
+// Renders the BIOS cart's file browser. With FAKE08_BIOS_PNG set, the screen
+// is also written to that file (for looking at the layout).
+TEST_CASE("bios file browser") {
+    stubDirList = {"zeta", "alpha"};
+    stubCartList = {"carts/celeste.p8.png", "carts/a_rather_long_cart_name_that_needs_cutting.p8", "carts/b.p8"};
+    for (int i = 0; i < 20; i++) stubCartList.push_back("carts/cart" + std::to_string(i) + ".p8");
+
+    StubHost* host = new StubHost();
+    Vm* vm = new Vm(host);
+    vm->SetCartList(stubCartList);   // as main() does
+    vm->LoadBiosCart();
+    vm->vm_run();
+    for (int i = 0; i < 5; i++) {
+        host->stubInput(0, 0);
+        vm->Step();
+    }
+
+    CHECK(vm->GetBiosError() == "");
+
+    // the selection bar (text colour, 7) starts on the first cart: row 3
+    // (.., alpha, zeta, then carts), y = 19 + 3 * 6 - 1
+    uint8_t* fb = vm->GetPicoInteralFb();
+    CHECK(getPixelNibble(64, 19 + 3 * 6 - 1, fb) == 7);
+
+    if (const char* out = getenv("FAKE08_BIOS_PNG")) {
+        uint8_t* map = vm->GetScreenPaletteMap();
+        host->setUpPaletteColors();
+        Color* colors = host->GetPaletteColors();
+        std::vector<unsigned char> image(128 * 128 * 4);
+        for (int y = 0; y < 128; y++)
+            for (int x = 0; x < 128; x++) {
+                Color c = colors[map[getPixelNibble(x, y, fb)] & 0x8f];
+                size_t i = (y * 128 + x) * 4;
+                image[i] = c.Red; image[i + 1] = c.Green; image[i + 2] = c.Blue; image[i + 3] = 255;
+            }
+        lodepng::encode(out, image, 128, 128);
+    }
+
+    stubDirList.clear();
+    stubCartList.clear();
+    delete vm;
+    delete host;
+}
diff --git a/test/stubhost.cpp b/test/stubhost.cpp
index c70feeb..4ed14fd 100644
--- a/test/stubhost.cpp
+++ b/test/stubhost.cpp
@@ -94,10 +94,12 @@ bool Host::shouldRunMainLoop(){
     return true;
 }

-vector<string> Host::listcarts(){
-    vector<string> carts;
+// Lists the stub host hands out (empty unless a test sets them)
+std::vector<std::string> stubCartList;
+std::vector<std::string> stubDirList;

-    return carts;
+vector<string> Host::listcarts(){
+    return stubCartList;
 }

 std::string Host::customBiosLua() {
@@ -124,6 +126,9 @@ size_t Host::getFileContents(std::string fileName, char* buffer) {
 }

 int Host::getSetting(std::string sname) {
+    // the BIOS menu's default colours
+    if (sname == "p8_bgcolor") return 1;
+    if (sname == "p8_textcolor") return 7;
     return 0;
 }

@@ -140,8 +145,7 @@ void Host::setCartDirectory(std::string cartDirectory) {
 }

 std::vector<std::string> Host::listdirs() {
-    std::vector<std::string> dirs;
-    return dirs;
+    return stubDirList;
 }


diff --git a/test/stubhost.h b/test/stubhost.h
index f461718..0eae5fe 100644
--- a/test/stubhost.h
+++ b/test/stubhost.h
@@ -2,6 +2,13 @@

 #include "../source/host.h"

+#include <string>
+#include <vector>
+
+// Lists that Host::listcarts / listdirs return (empty by default)
+extern std::vector<std::string> stubCartList;
+extern std::vector<std::string> stubDirList;
+
 class StubHost : public Host {
     public:
     StubHost();