Skip to content
Snippets Groups Projects
Commit 65951d06 authored by dg's avatar dg
Browse files

find cores!

git-svn-id: http://svn.net-core.org/repos/t-engine4@2991 51575b47-30f0-44d4-a5cc-537603b46e54
parent 887942d8
No related branches found
No related tags found
No related merge requests found
......@@ -25,18 +25,38 @@ else
end
-- Look for a core
function get_core(id)
function get_core(type, id)
local homepath = fs.getUserPath()..fs.getPathSeparator()..fs.getHomePath()..fs.getPathSeparator().."4.0"
fs.mount(homepath, "/", 1)
-- Look for possible cores - if id is -1 then check all the ones matching the given type and use the newest one
local usable = {}
for i, file in ipairs(fs.list("/engines/cores/")) do
if file:find("%.tec$") then
print("Possible engine core", file)
if id > 0 and file == type.."-"..id..".tec" then usable[#usable+1] = {file=file, id=id}
elseif id == -1 and file:find(type) then
local _, _, cid = file:find("%-([0-9]+)%.tec$")
cid = tonumber(cid)
if cid then
usable[#usable+1] = {file=file, id=cid}
end
end
end
end
local core = "/engines/cores/te4core-"..id..".tec"
-- Order the cores to find the newest
table.sort(usable, function(a, b) return b.id < a.id end)
for i, file in ipairs(usable) do print("Selected cores:", file.file) end
-- Check for sanity and tell the runner to use it
local core = "/engines/cores/"..usable[1].file
if fs.exists(core) then
local rcore = fs.getRealPath(core)
print("Using TE4CORE: ", core, rcore)
fs.umount(homepath)
return rcore
end
fs.umount(homepath)
return "NO CORE"
end
......
......@@ -42,6 +42,7 @@
#include "serial.h"
#include "profile.h"
#include "main.h"
#include "runner/core.h"
#define WIDTH 800
#define HEIGHT 600
......@@ -794,8 +795,10 @@ void boot_lua(int state, bool rebooting, int argc, char *argv[])
/**
* Core entry point.
*/
int _te4_export te4main(int argc, char *argv[])
void _te4_export te4main(int argc, char *argv[], core_boot_type *core_def)
{
core_def->corenum = 0;
// Get cpu cores
nb_cpus = get_number_cpus();
printf("[CPU] Detected %d CPUs\n", nb_cpus);
......@@ -821,7 +824,7 @@ int _te4_export te4main(int argc, char *argv[])
Uint32 flags=SDL_INIT_VIDEO | SDL_INIT_TIMER;
if (SDL_Init (flags) < 0) {
printf("cannot initialize SDL: %s\n", SDL_GetError ());
return -1;
return;
}
// Filter events, to catch the quit event
......@@ -836,7 +839,7 @@ int _te4_export te4main(int argc, char *argv[])
do_resize(WIDTH, HEIGHT, FALSE);
if (screen==NULL) {
printf("error opening screen: %s\n", SDL_GetError());
return -1;
return;
}
SDL_WM_SetCaption("T4Engine", NULL);
SDL_EnableUNICODE(TRUE);
......@@ -963,13 +966,13 @@ int _te4_export te4main(int argc, char *argv[])
free_profile_thread();
lua_close(L);
PHYSFS_deinit();
core_def->corenum = reboot_lua;
break;
}
}
}
SDL_Quit();
// We return the new core to run, if any
return reboot_lua;
}
/*
TE4 - T-Engine 4
Copyright (C) 2009, 2010 Nicolas Casalini
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Nicolas Casalini "DarkGod"
darkgod@te4.org
*/
#ifndef TE4CORE_H
#define TE4CORE_H
typedef struct {
int corenum;
char *coretype;
char *reboot_engine;
char *reboot_engine_version;
char *reboot_module;
char *reboot_name;
char *reboot_einfo;
int reboot_new;
} core_boot_type;
#endif
......@@ -32,11 +32,13 @@
#include "lauxlib.h"
#include "lualib.h"
#include "physfs.h"
#include "core.h"
#include "getself.h"
// Load the shared lib containing the core and calls te4main inside it, passing control to that core
int run_core(int corenum, int argc, char **argv)
void run_core(core_boot_type *core_def, int argc, char **argv)
{
int (*te4main)(int, char**);
int (*te4main)(int, char**, core_boot_type*);
/******************************************************************
** Find a core file
......@@ -76,9 +78,10 @@ int run_core(int corenum, int argc, char **argv)
// Get the core
lua_getglobal(L, "get_core");
lua_pushnumber(L, corenum);
lua_call(L, 1, 1);
char *core = lua_tostring(L, -1);
lua_pushstring(L, core_def->coretype);
lua_pushnumber(L, core_def->corenum);
lua_call(L, 2, 1);
char *core = (char*)lua_tostring(L, -1);
lua_close(L);
PHYSFS_deinit();
......@@ -95,13 +98,13 @@ int run_core(int corenum, int argc, char **argv)
HINSTANCE handle = LoadLibrary(core);
if (!handle) {
fprintf(stderr, "Error loading core %d (%s): %d\n", corenum, core, GetLastError());
fprintf(stderr, "Error loading core %d (%s): %d\n", core_def->corenum, core, GetLastError());
exit(EXIT_FAILURE);
}
te4main = GetProcAddress(handle, "te4main");
if (te4main == NULL) {
fprintf(stderr, "Error binding to core %d (%s): %d\n", corenum, core, GetLastError());
fprintf(stderr, "Error binding to core %d (%s): %d\n", core_def->corenum, core, GetLastError());
exit(EXIT_FAILURE);
}
......@@ -118,7 +121,7 @@ int run_core(int corenum, int argc, char **argv)
void *handle = dlopen(core, RTLD_LAZY);
if (!handle) {
fprintf(stderr, "Error loading core %d (%s): %s\n", corenum, core, dlerror());
fprintf(stderr, "Error loading core %d (%s): %s\n", core_def->corenum, core, dlerror());
exit(EXIT_FAILURE);
}
......@@ -134,19 +137,17 @@ int run_core(int corenum, int argc, char **argv)
*(void **) (&te4main) = dlsym(handle, "te4main");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "Error binding to core %d (%s): %s\n", corenum, core, error);
fprintf(stderr, "Error binding to core %d (%s): %s\n", core_def->corenum, core, error);
exit(EXIT_FAILURE);
}
// Run the core
corenum = te4main(argc, argv);
te4main(argc, argv, core_def);
dlclose(handle);
#endif
free(core);
return corenum;
}
// Let some platforms use a different entry point
......@@ -159,10 +160,19 @@ int run_core(int corenum, int argc, char **argv)
int main(int argc, char **argv)
{
int core = 12;
core_boot_type core_def;
core_def.corenum = -1; // Start with latest core
core_def.coretype = "te4core";
core_def.reboot_engine = NULL;
core_def.reboot_engine_version = NULL;
core_def.reboot_module = NULL;
core_def.reboot_name = NULL;
core_def.reboot_einfo = NULL;
core_def.reboot_new = 0;
// Run the requested cores until we want no more
while (core) core = run_core(core, argc, argv);
while (core_def.corenum) run_core(&core_def, argc, argv);
exit(EXIT_SUCCESS);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment