foxygit / fake-08-xbox360 Log in
commit dcee23b49ed7862a761347e66915b70d328c5132
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Wed Apr 7 20:37:42 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Wed Apr 7 20:37:42 2021 -0600

    fix compatibility problems in vm

    implement some parts of stat (fps, time, semi useful fake cpu vals)
    fix crashes with negative length passed to memset/memcpy/reload
    handle calling update and draw if their definitions change at runtime
---
 source/picoluaapi.cpp |  89 +++++++++++++++++++++++++++++++++++++--
 source/vm.cpp         | 114 ++++++++++++++++++++++++++++++++++----------------
 source/vm.h           |  14 ++++++-
 3 files changed, 177 insertions(+), 40 deletions(-)

diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 407fc10..98ad30d 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -735,6 +735,88 @@ int time(lua_State *L) {
 }

 int stat(lua_State *L) {
+    int n = (int)lua_tonumber(L, 1);
+
+    switch(n){
+        //0 memory usage
+        case 0:
+            //TODO: get from z8lua
+            lua_pushnumber(L, 1);
+            return 1;
+        break;
+        //cpu usage
+        case 1:
+            // stubbed to return 50%
+            lua_pushnumber(L, 0.5);
+            return 1;
+        break;
+        //cpu usage (without system calls)
+        case 2:
+            // stubbed to return 50%
+            lua_pushnumber(L, 0.5);
+            return 1;
+        break;
+        //clipboard contents
+        case 4:
+            // no clipboard support currently
+            lua_pushstring(L, "");
+            return 1;
+        break;
+        //version
+        case 5:
+            // no clipboard support currently
+            lua_pushnumber(L, 22);
+            return 1;
+        break;
+        //argument
+        case 6:
+            // no args or loading other cards currently supported
+            lua_pushstring(L, "");
+            return 1;
+        break;
+        //frame rate
+        case 7:
+            lua_pushnumber(L, _vmForLuaApi->getFps());
+            return 1;
+        break;
+        //target framerate
+        case 8:
+            lua_pushnumber(L, _vmForLuaApi->getTargetFps());
+            return 1;
+        break;
+        //Current Year
+        case 90:
+            lua_pushnumber(L, _vmForLuaApi->getYear());
+            return 1;
+        break;
+        //Current month
+        case 91:
+            lua_pushnumber(L, _vmForLuaApi->getMonth());
+            return 1;
+        break;
+        //Current day
+        case 92:
+            lua_pushnumber(L, _vmForLuaApi->getDay());
+            return 1;
+        break;
+        //Current Hour
+        case 93:
+            lua_pushnumber(L, _vmForLuaApi->getHour());
+            return 1;
+        break;
+        //Current Minute
+        case 94:
+            lua_pushnumber(L, _vmForLuaApi->getMinute());
+            return 1;
+        break;
+        //Current second
+        case 95:
+            lua_pushnumber(L, _vmForLuaApi->getSecond());
+            return 1;
+        break;
+    }
+
+
     return noopreturns(L, "stat");
 }

@@ -880,9 +962,10 @@ int reload(lua_State *L) {

 //cart data
 int cartdata(lua_State *L) {
-    const char * str = lua_tolstring(L, 1, nullptr);
-
-    std::string result = _vmForLuaApi->vm_cartdata(str);
+    if (lua_gettop(L) > 0) {
+        std::string key = lua_tolstring(L, 1, nullptr);
+        _vmForLuaApi->vm_cartdata(key);
+    }

     return 0;
 }
diff --git a/source/vm.cpp b/source/vm.cpp
index cb636c3..4b009fe 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -9,6 +9,7 @@
 #include <sstream>
 #include <iostream>
 #include <iomanip>
+#include <ctime>

 #include "vm.h"
 #include "graphics.h"
@@ -41,8 +42,6 @@ Vm::Vm(
         _cleanupDeps(false),
         _targetFps(30),
         _picoFrameCount(0),
-        _hasUpdate(false),
-        _hasDraw(false),
         _cartChangeQueued(false),
         _nextCartKey(""),
         _cartLoadError(""),
@@ -288,25 +287,14 @@ bool Vm::loadCart(Cart* cart) {
     //check for update, mark correct target fps
     lua_getglobal(_luaState, "_update60");
     if (lua_isfunction(_luaState, -1)) {
-        _hasUpdate = true;
         _targetFps = 60;
     }
-    lua_pop(_luaState, 0);
-    if (!_hasUpdate){
-        lua_getglobal(_luaState, "_update");
-        if (lua_isfunction(_luaState, -1)) {
-            _hasUpdate = true;
-            _targetFps = 30;
-        }
-        lua_pop(_luaState, 0);
-    }
-
-    lua_getglobal(_luaState, "_draw");
-    if (lua_isfunction(_luaState, -1)) {
-        _hasDraw = true;
+    else {
+        _targetFps = 30;
     }
     lua_pop(_luaState, 0);

+
     //customize bios per host's requirements
     if (cart->Filename == "__FAKE08-BIOS.p8") {
         std::string customBiosLua = _host->customBiosLua();
@@ -446,29 +434,24 @@ void Vm::UpdateAndDraw() {
         lua_pop(_luaState, 0);
     }
     else{
-        if (_hasUpdate){
-            // Push the _update function on the top of the lua stack
-            if (_targetFps == 60) {
-                lua_getglobal(_luaState, "_update60");
-            } else {
-                lua_getglobal(_luaState, "_update");
-            }
+        // Push the _update function on the top of the lua stack
+        if (_targetFps == 60) {
+            lua_getglobal(_luaState, "_update60");
+        } else {
+            lua_getglobal(_luaState, "_update");
+        }

-            //we already checked that its a function, so we should be able to call it
+        if (lua_isfunction(_luaState, -1)) {
             lua_call(_luaState, 0, 0);
-
-            //pop the update fuction off the stack now that we're done with it
-            lua_pop(_luaState, 0);
         }
+        //pop the update fuction off the stack now that we're done with it
+        lua_pop(_luaState, 0);

-        if (_hasDraw) {
-            lua_getglobal(_luaState, "_draw");
-
+        lua_getglobal(_luaState, "_draw");
+        if (lua_isfunction(_luaState, -1)) {
             lua_call(_luaState, 0, 0);
-
-            //pop the update fuction off the stack now that we're done with it
-            lua_pop(_luaState, 0);
         }
+        lua_pop(_luaState, 0);
     }

 }
@@ -504,8 +487,6 @@ void Vm::CloseCart() {
     }

     Logger_Write("resetting state\n");
-    _hasUpdate = false;
-    _hasDraw = false;
     _targetFps = 30;
     _picoFrameCount = 0;
 }
@@ -721,10 +702,16 @@ void Vm::vm_dset(uint8_t n, fix32 value){
 }

 void Vm::vm_reload(int destaddr, int sourceaddr, int len, Cart* cart){
+    if (len <= 0) {
+        return;
+    }
     memcpy(&_memory->data[destaddr], &cart->CartRom.data[sourceaddr], len);
 }

 void Vm::vm_reload(int destaddr, int sourceaddr, int len, string filename){
+    if (len <= 0) {
+        return;
+    }
     if (destaddr < 0 || destaddr > (int)sizeof(PicoRam)) {
         //invalid dest address
         return;
@@ -762,6 +749,9 @@ void Vm::vm_reload(int destaddr, int sourceaddr, int len, string filename){
 }

 void Vm::vm_memset(int destaddr, uint8_t val, int len){
+    if (len <= 0) {
+        return;
+    }
     if (destaddr < 0 || destaddr + len > (int)sizeof(PicoRam)) {
         return;
     }
@@ -770,6 +760,9 @@ void Vm::vm_memset(int destaddr, uint8_t val, int len){

 }
 void Vm::vm_memcpy(int destaddr, int sourceaddr, int len){
+    if (len <= 0) {
+        return;
+    }
     if (sourceaddr < 0 || sourceaddr + len > (int)sizeof(PicoRam)) {
         return;
     }
@@ -871,3 +864,54 @@ void Vm::vm_extcmd(std::string cmd){
         QueueCartChange("__FAKE08-BIOS.p8");
     }
 }
+
+int Vm::getFps(){
+    //TODO: return actual fps (as fix32?)
+    return _targetFps;
+}
+
+int Vm::getTargetFps(){
+    return _targetFps;
+}
+
+int Vm::getYear(){
+    std::time_t t = std::time(0);
+    std::tm* now = std::localtime(&t);
+
+    return now->tm_year + 1900;
+}
+
+int Vm::getMonth(){
+    std::time_t t = std::time(0);
+    std::tm* now = std::localtime(&t);
+
+    return now->tm_mon + 1;
+}
+
+int Vm::getDay(){
+    std::time_t t = std::time(0);
+    std::tm* now = std::localtime(&t);
+
+    return now->tm_mday;
+}
+
+int Vm::getHour(){
+    std::time_t t = std::time(0);
+    std::tm* now = std::localtime(&t);
+
+    return now->tm_hour;
+}
+
+int Vm::getMinute(){
+    std::time_t t = std::time(0);
+    std::tm* now = std::localtime(&t);
+
+    return now->tm_min;
+}
+
+int Vm::getSecond(){
+    std::time_t t = std::time(0);
+    std::tm* now = std::localtime(&t);
+
+    return now->tm_sec;
+}
diff --git a/source/vm.h b/source/vm.h
index d677285..6ed53a5 100644
--- a/source/vm.h
+++ b/source/vm.h
@@ -35,8 +35,8 @@ class Vm {
     int _targetFps;

     int _picoFrameCount;
-    bool _hasUpdate;
-    bool _hasDraw;
+    //bool _hasUpdate;
+    //bool _hasDraw;

     bool _cartChangeQueued;
     bool _pauseMenu;
@@ -126,5 +126,15 @@ class Vm {
     void vm_flip();
     void vm_run();
     void vm_extcmd(string  cmd);
+
+    int getFps();
+    int getTargetFps();
+
+    int getYear();
+    int getMonth();
+    int getDay();
+    int getHour();
+    int getMinute();
+    int getSecond();
 };