foxygit / fake-08-xbox360 Log in
commit 481b033af6edec3fad805e3c293b79ecb5687ff2
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Thu Jan 1 20:34:28 2026 -0700
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Thu Jan 1 20:34:28 2026 -0700

    Add ability to change cart directory
---
 platform/3ds/source/3dsHost.cpp             |  23 +++++
 platform/SDL2Common/source/sdl2basehost.cpp |  23 +++++
 platform/bittboy/source/BittBoyHost.cpp     |  23 +++++
 platform/funkey-s/source/FunkeyHost.cpp     |  23 +++++
 platform/gcw0/source/ODHost.cpp             |  23 +++++
 platform/libretro/libretrohost.cpp          |   5 +
 platform/miyoomini/source/MiyooMiniHost.cpp |  23 +++++
 platform/switch/source/SwitchHost.cpp       |  23 +++++
 platform/vita/source/VitaHost.cpp           |  30 ++++++
 platform/wiiu/source/WiiUHost.cpp           |  23 +++++
 platform/windows/source/WindowsHost.cpp     |  23 +++++
 source/DefaultCart.h                        | 142 +++++++++++++++++++++++-----
 source/host.h                               |   2 +
 source/hostCommonFunctions.cpp              |   4 +
 source/p8GlobalLuaFunctions.h               |   3 +-
 source/picoluaapi.cpp                       |  36 +++++++
 source/picoluaapi.h                         |   3 +
 source/vm.cpp                               |  54 +++++++++++
 source/vm.h                                 |   3 +
 test/stubhost.cpp                           |   9 ++
 20 files changed, 472 insertions(+), 26 deletions(-)

diff --git a/platform/3ds/source/3dsHost.cpp b/platform/3ds/source/3dsHost.cpp
index 9194baa..68c6e36 100644
--- a/platform/3ds/source/3dsHost.cpp
+++ b/platform/3ds/source/3dsHost.cpp
@@ -749,3 +749,26 @@ std::string Host::customBiosLua() {
 std::string Host::getCartDirectory() {
     return _cartDirectory;
 }
+
+std::vector<std::string> Host::listdirs() {
+    std::vector<std::string> dirs;
+
+    DIR *dir;
+    struct dirent *ent;
+    if ((dir = opendir (_cartDirectory.c_str())) != NULL) {
+        while ((ent = readdir (dir)) != NULL) {
+            if (ent->d_name[0] == '.') {
+                continue;
+            }
+            std::string fullPath = _cartDirectory + "/" + ent->d_name;
+            DIR* testDir = opendir(fullPath.c_str());
+            if (testDir != NULL) {
+                closedir(testDir);
+                dirs.push_back(ent->d_name);
+            }
+        }
+        closedir (dir);
+    }
+
+    return dirs;
+}
diff --git a/platform/SDL2Common/source/sdl2basehost.cpp b/platform/SDL2Common/source/sdl2basehost.cpp
index 6fa57b2..bf356e2 100644
--- a/platform/SDL2Common/source/sdl2basehost.cpp
+++ b/platform/SDL2Common/source/sdl2basehost.cpp
@@ -449,3 +449,26 @@ std::string Host::customBiosLua() {
 std::string Host::getCartDirectory() {
     return _cartDirectory;
 }
+
+std::vector<std::string> Host::listdirs() {
+    std::vector<std::string> dirs;
+
+    DIR *dir;
+    struct dirent *ent;
+    if ((dir = opendir (_cartDirectory.c_str())) != NULL) {
+        while ((ent = readdir (dir)) != NULL) {
+            if (ent->d_name[0] == '.') {
+                continue;
+            }
+            std::string fullPath = _cartDirectory + "/" + ent->d_name;
+            DIR* testDir = opendir(fullPath.c_str());
+            if (testDir != NULL) {
+                closedir(testDir);
+                dirs.push_back(ent->d_name);
+            }
+        }
+        closedir (dir);
+    }
+
+    return dirs;
+}
diff --git a/platform/bittboy/source/BittBoyHost.cpp b/platform/bittboy/source/BittBoyHost.cpp
index 7dc654c..35e47ed 100644
--- a/platform/bittboy/source/BittBoyHost.cpp
+++ b/platform/bittboy/source/BittBoyHost.cpp
@@ -630,3 +630,26 @@ std::string Host::customBiosLua() {
 std::string Host::getCartDirectory() {
     return _cartDirectory;
 }
+
+std::vector<std::string> Host::listdirs() {
+    std::vector<std::string> dirs;
+
+    DIR *dir;
+    struct dirent *ent;
+    if ((dir = opendir (_cartDirectory.c_str())) != NULL) {
+        while ((ent = readdir (dir)) != NULL) {
+            if (ent->d_name[0] == '.') {
+                continue;
+            }
+            std::string fullPath = _cartDirectory + "/" + ent->d_name;
+            DIR* testDir = opendir(fullPath.c_str());
+            if (testDir != NULL) {
+                closedir(testDir);
+                dirs.push_back(ent->d_name);
+            }
+        }
+        closedir (dir);
+    }
+
+    return dirs;
+}
diff --git a/platform/funkey-s/source/FunkeyHost.cpp b/platform/funkey-s/source/FunkeyHost.cpp
index ee465fa..adb9511 100644
--- a/platform/funkey-s/source/FunkeyHost.cpp
+++ b/platform/funkey-s/source/FunkeyHost.cpp
@@ -336,3 +336,26 @@ std::string Host::customBiosLua() {
 std::string Host::getCartDirectory() {
     return _cartDirectory;
 }
+
+std::vector<std::string> Host::listdirs() {
+    std::vector<std::string> dirs;
+
+    DIR *dir;
+    struct dirent *ent;
+    if ((dir = opendir (_cartDirectory.c_str())) != NULL) {
+        while ((ent = readdir (dir)) != NULL) {
+            if (ent->d_name[0] == '.') {
+                continue;
+            }
+            std::string fullPath = _cartDirectory + "/" + ent->d_name;
+            DIR* testDir = opendir(fullPath.c_str());
+            if (testDir != NULL) {
+                closedir(testDir);
+                dirs.push_back(ent->d_name);
+            }
+        }
+        closedir (dir);
+    }
+
+    return dirs;
+}
diff --git a/platform/gcw0/source/ODHost.cpp b/platform/gcw0/source/ODHost.cpp
index 4a4d1e7..b0f3554 100644
--- a/platform/gcw0/source/ODHost.cpp
+++ b/platform/gcw0/source/ODHost.cpp
@@ -676,3 +676,26 @@ std::string Host::customBiosLua() {
 std::string Host::getCartDirectory() {
     return _cartDirectory;
 }
+
+std::vector<std::string> Host::listdirs() {
+    std::vector<std::string> dirs;
+
+    DIR *dir;
+    struct dirent *ent;
+    if ((dir = opendir (_cartDirectory.c_str())) != NULL) {
+        while ((ent = readdir (dir)) != NULL) {
+            if (ent->d_name[0] == '.') {
+                continue;
+            }
+            std::string fullPath = _cartDirectory + "/" + ent->d_name;
+            DIR* testDir = opendir(fullPath.c_str());
+            if (testDir != NULL) {
+                closedir(testDir);
+                dirs.push_back(ent->d_name);
+            }
+        }
+        closedir (dir);
+    }
+
+    return dirs;
+}
diff --git a/platform/libretro/libretrohost.cpp b/platform/libretro/libretrohost.cpp
index 06a325a..64c4a07 100644
--- a/platform/libretro/libretrohost.cpp
+++ b/platform/libretro/libretrohost.cpp
@@ -120,6 +120,11 @@ std::string Host::getCartDirectory() {
     return _currentCartDirectory;
 }

+std::vector<std::string> Host::listdirs() {
+    std::vector<std::string> dirs;
+    return dirs;
+}
+
 void Host::overrideLogFilePrefix(const char* newPrefix) {
     _logFilePrefix = newPrefix;

diff --git a/platform/miyoomini/source/MiyooMiniHost.cpp b/platform/miyoomini/source/MiyooMiniHost.cpp
index 9f6d337..dcd8ce8 100644
--- a/platform/miyoomini/source/MiyooMiniHost.cpp
+++ b/platform/miyoomini/source/MiyooMiniHost.cpp
@@ -642,3 +642,26 @@ std::string Host::customBiosLua() {
 std::string Host::getCartDirectory() {
     return _cartDirectory;
 }
+
+std::vector<std::string> Host::listdirs() {
+    std::vector<std::string> dirs;
+
+    DIR *dir;
+    struct dirent *ent;
+    if ((dir = opendir (_cartDirectory.c_str())) != NULL) {
+        while ((ent = readdir (dir)) != NULL) {
+            if (ent->d_name[0] == '.') {
+                continue;
+            }
+            std::string fullPath = _cartDirectory + "/" + ent->d_name;
+            DIR* testDir = opendir(fullPath.c_str());
+            if (testDir != NULL) {
+                closedir(testDir);
+                dirs.push_back(ent->d_name);
+            }
+        }
+        closedir (dir);
+    }
+
+    return dirs;
+}
diff --git a/platform/switch/source/SwitchHost.cpp b/platform/switch/source/SwitchHost.cpp
index c104d2b..6ec1da9 100644
--- a/platform/switch/source/SwitchHost.cpp
+++ b/platform/switch/source/SwitchHost.cpp
@@ -176,3 +176,26 @@ vector<string> Host::listcarts(){
     return carts;
 }

+std::vector<std::string> Host::listdirs() {
+    std::vector<std::string> dirs;
+
+    DIR *dir;
+    struct dirent *ent;
+    if ((dir = opendir (_cartDirectory.c_str())) != NULL) {
+        while ((ent = readdir (dir)) != NULL) {
+            if (ent->d_name[0] == '.') {
+                continue;
+            }
+            std::string fullPath = _cartDirectory + "/" + ent->d_name;
+            DIR* testDir = opendir(fullPath.c_str());
+            if (testDir != NULL) {
+                closedir(testDir);
+                dirs.push_back(ent->d_name);
+            }
+        }
+        closedir (dir);
+    }
+
+    return dirs;
+}
+
diff --git a/platform/vita/source/VitaHost.cpp b/platform/vita/source/VitaHost.cpp
index 913d3f8..152a4c1 100644
--- a/platform/vita/source/VitaHost.cpp
+++ b/platform/vita/source/VitaHost.cpp
@@ -302,4 +302,34 @@ vector<string> Host::listcarts(){
     return carts;
 }

+std::vector<std::string> Host::listdirs() {
+    std::vector<std::string> dirs;
+
+    SceUID dir = 0;
+    int ret = 0;
+
+    if (R_SUCCEEDED(dir = sceIoDopen(_cartDirectory.c_str()))) {
+
+        do {
+            SceIoDirent dirent;
+
+            if (R_FAILED(ret = sceIoDread(dir, &dirent))) {
+                continue;
+            }
+
+            if (dirent.d_name[0] == '.') {
+                continue;
+            }
+
+            if (SCE_S_ISDIR(dirent.d_stat.st_mode)) {
+                dirs.push_back(dirent.d_name);
+            }
+        } while (ret > 0);
+
+        sceIoDclose(dir);
+    }
+
+    return dirs;
+}
+

diff --git a/platform/wiiu/source/WiiUHost.cpp b/platform/wiiu/source/WiiUHost.cpp
index 842ede5..0fbaa31 100644
--- a/platform/wiiu/source/WiiUHost.cpp
+++ b/platform/wiiu/source/WiiUHost.cpp
@@ -585,3 +585,26 @@ std::string Host::getCartDirectory() {
     return _cartDirectory;
 }

+std::vector<std::string> Host::listdirs() {
+    std::vector<std::string> dirs;
+
+    DIR *dir;
+    struct dirent *ent;
+    if ((dir = opendir (_cartDirectory.c_str())) != NULL) {
+        while ((ent = readdir (dir)) != NULL) {
+            if (ent->d_name[0] == '.') {
+                continue;
+            }
+            std::string fullPath = _cartDirectory + "/" + ent->d_name;
+            DIR* testDir = opendir(fullPath.c_str());
+            if (testDir != NULL) {
+                closedir(testDir);
+                dirs.push_back(ent->d_name);
+            }
+        }
+        closedir (dir);
+    }
+
+    return dirs;
+}
+
diff --git a/platform/windows/source/WindowsHost.cpp b/platform/windows/source/WindowsHost.cpp
index 003491a..a9fa0c8 100644
--- a/platform/windows/source/WindowsHost.cpp
+++ b/platform/windows/source/WindowsHost.cpp
@@ -234,3 +234,26 @@ vector<string> Host::listcarts(){
     return carts;
 }

+std::vector<std::string> Host::listdirs() {
+    std::vector<std::string> dirs;
+
+    DIR *dir;
+    struct dirent *ent;
+    if ((dir = opendir (_cartDirectory.c_str())) != NULL) {
+        while ((ent = readdir (dir)) != NULL) {
+            if (ent->d_name[0] == '.') {
+                continue;
+            }
+            std::string fullPath = _cartDirectory + "/" + ent->d_name;
+            DIR* testDir = opendir(fullPath.c_str());
+            if (testDir != NULL) {
+                closedir(testDir);
+                dirs.push_back(ent->d_name);
+            }
+        }
+        closedir (dir);
+    }
+
+    return dirs;
+}
+
diff --git a/source/DefaultCart.h b/source/DefaultCart.h
index 712ea9c..e9388a0 100644
--- a/source/DefaultCart.h
+++ b/source/DefaultCart.h
@@ -45,6 +45,9 @@ function classic_init()

 	carts={}
 	numcarts = 0
+	dirs={}
+	numdirs = 0
+	currentdirname = ""

 	if __getsetting then
 			pal(1, __getsetting('p8_bgcolor'))
@@ -55,38 +58,88 @@ function classic_init()
 	end


-	cidx=0
+	cidx=0  -- cart index (left/right)
+	didx=0  -- dir index (up/down): 0=pwd, 1=cd .., 2+=dirs
+	mode=0  -- 0=cart mode, 1=dir mode
 	carttoload = ""
 	t=0
 	linebuffer=""

 	bgcolor=1
 	runcmd=false
+	cdcmd=false

 	if __listcarts then
 		carts = __listcarts()
 		numcarts = #carts
 	end
+
+	if __listdirs then
+		dirs = __listdirs()
+		numdirs = #dirs
+	end
+
+	-- get current directory name (just the last component)
+	if __pwd then
+		local fullpath = __pwd()
+		local lastslash = findlastchar(fullpath, "/")
+		if lastslash then
+			currentdirname = sub(fullpath, lastslash + 1)
+		else
+			currentdirname = fullpath
+		end
+	end

 	if __getbioserror then
 		linebuffer = __getbioserror()
 	end
 end

+function classic_refreshlists()
+	if __listcarts then
+		carts = __listcarts()
+		numcarts = #carts
+	end
+	if __listdirs then
+		dirs = __listdirs()
+		numdirs = #dirs
+	end
+	-- refresh current directory name
+	if __pwd then
+		local fullpath = __pwd()
+		local lastslash = findlastchar(fullpath, "/")
+		if lastslash then
+			currentdirname = sub(fullpath, lastslash + 1)
+		else
+			currentdirname = fullpath
+		end
+	end
+	cidx = 0
+	didx = 0
+end
+
 function classic_update()
 	t+=1
+
+	-- left/right: navigate carts
 	if btnp(1) then
+		mode = 0
 		cidx = min((cidx + 1), numcarts)
 	end
 	if btnp(0) then
+		mode = 0
 		cidx = max((cidx - 1), 1)
 	end

+	-- up/down: navigate directories
 	if btnp(2) then
-		cidx = max((cidx - 10), 1)
+		mode = 1
+		didx = max((didx - 1), 0)
 	end
 	if btnp(3) then
-		cidx = min((cidx + 10), numcarts)
+		mode = 1
+		didx = min((didx + 1), numdirs + 1)
+		-- +1 because: 0=pwd, 1=cd .., 2+ = actual dirs
 	end

 	if btnp(4) then
@@ -94,30 +147,75 @@ function classic_update()
 	end
 	if btnp(5) then
 		linebuffer = ""
-		runcmd = true
+		if mode == 0 then
+			runcmd = true
+		else
+			cdcmd = true
+		end
 	end

-	if cidx > 0 and cidx <= numcarts then
-		carttoload = carts[cidx]
-		local lastslashidx = findlastchar(carttoload, "/")
-		local dispstr = carttoload
-		if lastslashidx ~= nil and lastslashidx > 0 then
-			dispstr = sub(dispstr, lastslashidx + 1)
+	-- update linebuffer based on mode
+	if mode == 0 then
+		-- cart mode
+		if cidx > 0 and cidx <= numcarts then
+			carttoload = carts[cidx]
+			local lastslashidx = findlastchar(carttoload, "/")
+			local dispstr = carttoload
+			if lastslashidx ~= nil and lastslashidx > 0 then
+				dispstr = sub(dispstr, lastslashidx + 1)
+			end
+			linebuffer = "load " .. dispstr
+		else
+			carttoload = ""
+			if numcarts < 1 then
+				linebuffer = "(no carts in directory)"
+			end
 		end
-		linebuffer = "load " .. dispstr
 	else
-		carttoload = ""
+		-- dir mode
+		if didx == 0 then
+			if __pwd then
+				linebuffer = "pwd: " .. __pwd()
+			else
+				linebuffer = "pwd"
+			end
+		elseif didx == 1 then
+			linebuffer = "cd .."
+		else
+			local dirIdx = didx - 1
+			if dirIdx <= numdirs then
+				linebuffer = "cd " .. dirs[dirIdx]
+			end
+		end
 	end

 	if runcmd then
 		runcmd = false
-		--make call to global
-		--load cart here
-
 		if load and carttoload then
 			load(carttoload)
 		end
 	end
+
+	if cdcmd then
+		cdcmd = false
+		if didx == 0 then
+			-- pwd: just show, do nothing
+		elseif didx == 1 then
+			-- cd ..
+			if __cd then
+				__cd("..")
+				classic_refreshlists()
+			end
+		else
+			local dirIdx = didx - 1
+			if dirIdx <= numdirs and __cd then
+				__cd(dirs[dirIdx])
+				classic_refreshlists()
+			end
+		end
+		mode = 0
+		linebuffer = ""
+	end

 end

@@ -131,16 +229,10 @@ function classic_draw()
 	print("a homebrew pico-8 emulator")
 	print("currently in alpha (" .. versionstr .. ")")
 	print("")
-	print("place p8 carts in " .. cartpath)
-	if numcarts < 1 then
-		print("--no carts found--")
-	else
-		print("⬅️➡️ to navigate carts 1 by 1")
-		print("⬆️⬇️ to navigate carts 10 by 10")
-		print("🅾️ (" .. selectbtn .. ") to load selected cart")
-		print(pausebtn .. " to close current cart")
-	end
-	print(sizebtn)
+	print("dir: " .. currentdirname)
+	print("⬅️➡️ load carts (" .. numcarts .. " found)")
+	print("⬆️⬇️ cd dirs (" .. numdirs .. " found)")
+	print("🅾️ (" .. selectbtn .. ") to execute")
 	print(exitbtn .. " to exit")
 	print("")
 	-- 18 pixels from cursor() call, then
diff --git a/source/host.h b/source/host.h
index b7c2948..6aceffb 100644
--- a/source/host.h
+++ b/source/host.h
@@ -142,6 +142,8 @@ class Host {
     void writeBufferToFile(std::string cartDataKey, char* buffer, size_t length);

     std::string getCartDirectory();
+    void setCartDirectory(std::string cartDirectory);
+    std::vector<std::string> listdirs();

     //settings
     int getSetting(std::string sname);
diff --git a/source/hostCommonFunctions.cpp b/source/hostCommonFunctions.cpp
index 2a0aa50..96b255f 100644
--- a/source/hostCommonFunctions.cpp
+++ b/source/hostCommonFunctions.cpp
@@ -297,6 +297,10 @@ int Host::getSetting(std::string sname) {

 }

+void Host::setCartDirectory(std::string cartDirectory) {
+    _cartDirectory = cartDirectory;
+}
+
 void Host::setSetting(std::string sname, int sval) {
 	if(sname == "kbmode"){ //why cant you use strings in switch statements in c++ :(
 		Logger_Write("setting KB mode\n");
diff --git a/source/p8GlobalLuaFunctions.h b/source/p8GlobalLuaFunctions.h
index 3361891..cee1f29 100644
--- a/source/p8GlobalLuaFunctions.h
+++ b/source/p8GlobalLuaFunctions.h
@@ -443,7 +443,8 @@ local picofuncnames = {
     cstore = 1, _set_fps = 1,
     -- fake-08 internal
     __ispaused = 1, __resetcart = 1, __loaddefaultcart = 1, __loadsettingscart = 1,
-    __listcarts = 1, __getbioserror = 1, __getsetting = 1, __setsetting = 1, __loadlabel = 1
+    __listcarts = 1, __listdirs = 1, __cd = 1, __pwd = 1,
+    __getbioserror = 1, __getsetting = 1, __setsetting = 1, __loadlabel = 1
  }
  return picofuncnames[funcname] == 1
 end
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index c824211..ce72125 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -1362,6 +1362,42 @@ int listcarts(lua_State *L) {
     return 1;
 }

+int listdirs(lua_State *L) {
+    vector<string> dirs = _vmForLuaApi->GetDirList();
+
+    lua_createtable(L, dirs.size(), 0);
+    int newTable = lua_gettop(L);
+    int index = 1;
+
+    for(size_t i = 0; i < dirs.size(); i++){
+        lua_pushstring(L, dirs[i].c_str());
+        lua_rawseti(L, newTable, index);
+
+        ++index;
+    }
+
+    return 1;
+}
+
+int cd(lua_State *L) {
+    const char* dir = "";
+    if (lua_gettop(L) > 0 && lua_isstring(L, 1)){
+        dir = lua_tolstring(L, 1, nullptr);
+    }
+
+    bool result = _vmForLuaApi->ChangeDirectory(dir);
+    lua_pushboolean(L, result);
+
+    return 1;
+}
+
+int pwd(lua_State *L) {
+    string dir = _vmForLuaApi->GetCurrentDirectory();
+    lua_pushstring(L, dir.c_str());
+
+    return 1;
+}
+


 int getbioserror(lua_State *L) {
diff --git a/source/picoluaapi.h b/source/picoluaapi.h
index aa70b4c..d012fe9 100644
--- a/source/picoluaapi.h
+++ b/source/picoluaapi.h
@@ -95,6 +95,9 @@ int printh(lua_State *L);

 //file system/vm functions
 int listcarts(lua_State *L);
+int listdirs(lua_State *L);
+int cd(lua_State *L);
+int pwd(lua_State *L);

 int getbioserror(lua_State *L);
 int loaddefaultcart(lua_State *L);
diff --git a/source/vm.cpp b/source/vm.cpp
index 2603208..833e7ea 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -22,6 +22,7 @@
 #include "p8GlobalLuaFunctions.h"
 #include "hostVmShared.h"
 #include "emojiconversion.h"
+#include "filehelpers.h"

 #include "NoLabel.h"

@@ -47,6 +48,9 @@ bool _initializeLuaState(lua_State* luaState) {
     //system
     //must be registered before loading globals for pause menu to work
     lua_register(luaState, "__listcarts", listcarts);
+    lua_register(luaState, "__listdirs", listdirs);
+    lua_register(luaState, "__cd", cd);
+    lua_register(luaState, "__pwd", pwd);
     lua_register(luaState, "__getbioserror", getbioserror);
     lua_register(luaState, "__loaddefaultcart", loaddefaultcart);
     lua_register(luaState, "__loadsettingscart", loadsettingscart);
@@ -444,6 +448,17 @@ void Vm::LoadCart(std::string filename, bool loadBiosOnFail){
     Logger_Write("Loading cart %s\n", filename.c_str());
     CloseCart();

+    // If the filename is an absolute path, update the cart directory
+    if (isAbsolutePath(filename)) {
+        std::string dir = getDirectory(filename);
+        if (dir.length() > 0) {
+            Logger_Write("Setting cart directory to %s\n", dir.c_str());
+            _host->setCartDirectory(dir);
+            // Also update the cart list for the default cart
+            SetCartList(_host->listcarts());
+        }
+    }
+
     Logger_Write("Calling Cart Constructor\n");
     auto cartDir = _host->getCartDirectory();
     Cart *cart = new Cart(filename, cartDir);
@@ -794,6 +809,45 @@ vector<string> Vm::GetCartList(){
     return _cartList;
 }

+vector<string> Vm::GetDirList(){
+    return _host->listdirs();
+}
+
+bool Vm::ChangeDirectory(string dir){
+    std::string currentDir = _host->getCartDirectory();
+    std::string newDir;
+
+    if (dir == "..") {
+        // Go up one directory
+        size_t lastSlash = currentDir.find_last_of("/\\");
+        if (lastSlash != std::string::npos && lastSlash > 0) {
+            newDir = currentDir.substr(0, lastSlash);
+        } else {
+            return false; // Already at root
+        }
+    } else if (dir.length() > 0 && (dir[0] == '/' || dir.find(':') != std::string::npos)) {
+        // Absolute path
+        newDir = dir;
+    } else {
+        // Relative path
+        newDir = currentDir + "/" + dir;
+    }
+
+    // Verify the directory exists by trying to list its contents
+    _host->setCartDirectory(newDir);
+    vector<string> testList = _host->listcarts();
+
+    // Update cart list even if empty (the directory might only contain subdirs)
+    SetCartList(testList);
+
+    Logger_Write("Changed directory to: %s\n", newDir.c_str());
+    return true;
+}
+
+string Vm::GetCurrentDirectory(){
+    return _host->getCartDirectory();
+}
+
 string Vm::GetBiosError() {
     return _cartLoadError;
 }
diff --git a/source/vm.h b/source/vm.h
index 6aaaee3..7732cf2 100644
--- a/source/vm.h
+++ b/source/vm.h
@@ -98,6 +98,9 @@ class Vm {

     void SetCartList(vector<string> cartList);
     vector<string> GetCartList();
+    vector<string> GetDirList();
+    bool ChangeDirectory(string dir);
+    string GetCurrentDirectory();
     string GetBiosError();

     bool ExecuteLua(string luaString, string callbackFunction);
diff --git a/test/stubhost.cpp b/test/stubhost.cpp
index 7a593a9..c70feeb 100644
--- a/test/stubhost.cpp
+++ b/test/stubhost.cpp
@@ -135,6 +135,15 @@ std::string Host::getCartDirectory() {
     return "carts";
 }

+void Host::setCartDirectory(std::string cartDirectory) {
+    _cartDirectory = cartDirectory;
+}
+
+std::vector<std::string> Host::listdirs() {
+    std::vector<std::string> dirs;
+    return dirs;
+}
+

 void Host::setUpPaletteColors(){
     _paletteColors[0] = COLOR_00;