From 37ae2a28a9cadf20129ba38ce471a567db78ae22 Mon Sep 17 00:00:00 2001 From: Egor00f Date: Fri, 25 Apr 2025 23:25:01 +0500 Subject: [PATCH] add core version and library version --- .vscode/c_cpp_properties.json | 9 +- .vscode/settings.json | 4 +- Makefile | 17 ++- src/syscalls.c | 6 +- src/version/coreversion.c | 13 +++ src/version/coreversion.h | 8 ++ src/version/library_version.h | 17 +++ src/version/version_type.c | 202 ++++++++++++++++++++++++++++++++++ src/version/version_type.h | 28 +++++ 9 files changed, 297 insertions(+), 7 deletions(-) create mode 100644 src/version/coreversion.c create mode 100644 src/version/coreversion.h create mode 100644 src/version/library_version.h create mode 100644 src/version/version_type.c create mode 100644 src/version/version_type.h diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 936f463..a6c77da 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -6,7 +6,11 @@ "${workspaceFolder}/lua/src", "${workspaceFolder}/../kolibrios/contrib/sdk/sources/newlib/libc/include" ], - "defines": [] + "defines": [ + "SYSCALLS_VERSION_A", + "SYSCALLS_VERSION_B", + "SYSCALLS_VERSION_C" + ] }, { "name": "Debug", @@ -15,6 +19,9 @@ "${workspaceFolder}/../kolibrios/contrib/sdk/sources/newlib/libc/include" ], "defines": [ + "SYSCALLS_VERSION_A", + "SYSCALLS_VERSION_B", + "SYSCALLS_VERSION_C", "NDEBUG" ] } diff --git a/.vscode/settings.json b/.vscode/settings.json index 805b389..844e796 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,7 +7,9 @@ "socket.h": "c", "graphic.h": "c", "syscalls.h": "c", - "registers.h": "c" + "registers.h": "c", + "version_type.h": "c", + "library_version.h": "c" }, "cSpell.words": [ "syscalls", diff --git a/Makefile b/Makefile index 81b8f3e..a842386 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,8 @@ LUA_V = 54 +SYSCALLS_VER_A = 2 +SYSCALLS_VER_B = 0 +SYSCALLS_VER_C = 0 +SYSCALLS_VER = $(SYSCALLS_VER_C).$(SYSCALLS_VER_B).$(SYSCALLS_VER_A) CC = kos32-gcc LD = kos32-ld @@ -21,7 +25,7 @@ KOLIBRIOS_REPO = $(abspath ../kolibrios) SDK_DIR = $(KOLIBRIOS_REPO)/contrib/sdk NewLib_DIR = $(SDK_DIR)/sources/newlib -SYSCFLAGS = -fno-ident -fomit-frame-pointer -U__WIN32__ -U_Win32 -U_WIN32 -U__MINGW32__ -UWIN32 -I$(NewLib_DIR)/libc/include -I$(abspath .)/lua/src +SYSCFLAGS = -fno-ident -fomit-frame-pointer -U__WIN32__ -U_Win32 -U_WIN32 -U__MINGW32__ -UWIN32 -I$(NewLib_DIR)/libc/include -I$(abspath .)/lua/src -DSYSCALLS_VERSION_A=$(SYSCALLS_VER_A) -DSYSCALLS_VERSION_B=$(SYSCALLS_VER_B) -DSYSCALLS_VERSION_C=$(SYSCALLS_VER_C) SYSLDFLAGS = --image-base 0 -Tapp-dynamic.lds SYSLIBS = -nostdlib -L $(SDK_DIR)/lib -L$(TOOLCHAIN_PATH)/lib -L$(TOOLCHAIN_PATH)/mingw32/lib -lgcc -lc.dll -ldll MYCFLAGS = @@ -32,8 +36,9 @@ MYOBJS = Socket_O = src/sockets/socket.o src/sockets/socket_lua.o src/sockets/sockaddr.o Debug_O = src/debug/debug.o src/debug/registers.o +Version_O = src/version/coreversion.o src/version/version_type.o -ALL_O = src/syscalls.o src/ARP_entry.o src/systemColors.o src/graphic.o $(Socket_O) $(Debug_O) +ALL_O = src/syscalls.o src/ARP_entry.o src/systemColors.o src/graphic.o $(Socket_O) $(Debug_O) $(Version_O) syscalls.dll: $(ALL_O) $(CC) -shared -T dll.lds --entry _DllStartup -o $@ $(ALL_O) $(LIBS) @@ -45,7 +50,7 @@ clean: ## Sources -src/syscalls.o: src/syscalls.c src/syscalls.h src/systemColors.h src/ARP_entry.h src/scancodes.h src/sockets/socket_lua.h src/graphic.h +src/syscalls.o: src/syscalls.c src/syscalls.h src/systemColors.h src/ARP_entry.h src/scancodes.h src/sockets/socket_lua.h src/graphic.h src/version/library_version.h src/ARP_entry.o: src/ARP_entry.c src/ARP_entry.h src/debug.h src/systemColors.o: src/systemColors.c src/systemColors.h src/debug.h src/sockets/socket.o: src/sockets/socket.c src/sockets/socket.h @@ -53,7 +58,9 @@ src/sockets/socket_lua.o: src/sockets/socket_lua.c src/sockets/socket_lua.h src/sockets/sockaddr.o: src/sockets/sockaddr.c src/sockets/sockaddr.h src/graphic.o: src/graphic.c src/graphic.h src/debug/debug.o: src/debug/debug.c src/debug/debug.h src/debug/registers.h -src/debug/registers.o: src/debug/registers.c src/debug/registers.h src/syscalls.h +src/debug/registers.o: src/debug/registers.c src/debug/registers.h src/syscalls.h src/debug.h +src/version/coreversion.o: src/version/coreversion.c src/version/coreversion.h +src/version/version_type.o: src/version/version_type.c src/version/version_type.h src/debug.h ## headers @@ -62,3 +69,5 @@ src/sockets/socket_lua.h: src/syscalls.h src/sockets/socket.h src/sockets/sockaddr.h: src/sockets/socket.h src/syscalls.h src/debug/debug.h: src/syscalls.h src/debug/registers.h: src/syscalls.h +src/version/coreversion.h: src/version/version_type.h +src/version/version_type.h: src/syscalls.h diff --git a/src/syscalls.c b/src/syscalls.c index 9c7fc0b..ceb555b 100644 --- a/src/syscalls.c +++ b/src/syscalls.c @@ -15,7 +15,8 @@ #include "sockets/socket_lua.h" #include "debug/debug.h" #include "graphic.h" - +#include "version/coreversion.h" +#include "version/library_version.h" /* Режим ввода с клавиатуры @@ -1775,6 +1776,8 @@ LUALIB_API int luaopen_syscalls(lua_State* L) { luaL_newlib(L, syscallsLib); + syscalls_push_library_version(L); + syscalls_push_events(L); syscalls_push_slotStates(L); syscalls_register_scancodes(L); @@ -1787,6 +1790,7 @@ LUALIB_API int luaopen_syscalls(lua_State* L) syscalls_register_ARPEntry(L); syscalls_register_SystemColors(L); + syscalls_register_Version(L); _ksys_set_event_mask(7); // set default event mask diff --git a/src/version/coreversion.c b/src/version/coreversion.c new file mode 100644 index 0000000..a50d712 --- /dev/null +++ b/src/version/coreversion.c @@ -0,0 +1,13 @@ +#include "coreversion.h" + +int syscalls_GetCoreVersion(lua_State* L) +{ + struct core_version* ver = syscalls_pushVersion(L); + + asm_inline( + "int $0x40" + ::"a"(18), "b"(13), "c"(ver) + ); + + return 1; +} \ No newline at end of file diff --git a/src/version/coreversion.h b/src/version/coreversion.h new file mode 100644 index 0000000..cfa3782 --- /dev/null +++ b/src/version/coreversion.h @@ -0,0 +1,8 @@ +#ifndef __CORE_VERSION_H__ +#define __CORE_VERSION_H__ + +#include "version_type.h" + +int syscalls_GetCoreVersion(lua_State *L); + +#endif // __CORE_VERSION_H__ diff --git a/src/version/library_version.h b/src/version/library_version.h new file mode 100644 index 0000000..f2f5067 --- /dev/null +++ b/src/version/library_version.h @@ -0,0 +1,17 @@ +#ifndef __LIBRARY_VERSION_H__ +#define __LIBRARY_VERSION_H__ + +#include "../syscalls.h" + +inline void syscalls_push_library_version(lua_State* L) +{ + lua_createtable(L, 0, 3); + + LUA_PUSH_INTEGER_FIELD(L, SYSCALLS_VERSION_A, "a"); + LUA_PUSH_INTEGER_FIELD(L, SYSCALLS_VERSION_B, "b"); + LUA_PUSH_INTEGER_FIELD(L, SYSCALLS_VERSION_C, "c"); + + lua_setfield(L, -2, "version"); +} + +#endif // __LIBRARY_VERSION_H__ \ No newline at end of file diff --git a/src/version/version_type.c b/src/version/version_type.c new file mode 100644 index 0000000..ff8394a --- /dev/null +++ b/src/version/version_type.c @@ -0,0 +1,202 @@ +#include "version_type.h" +#include +#include "../debug.h" + +static int syscalls_indexVersion(lua_State* L) +{ + struct core_version* r = luaL_checkudata(L, 1, syscalls_Version_metatable_name); + const char* index = luaL_checkstring(L, 2); + + if (strcmp(index, "a") == 0) + { + lua_pushinteger(L, r->a); + } + else if (strcmp(index, "b") == 0) + { + lua_pushinteger(L, r->b); + } + else if (strcmp(index, "c") == 0) + { + lua_pushinteger(L, r->c); + } + else if (strcmp(index, "d") == 0) + { + lua_pushinteger(L, r->d); + } + else if (strcmp(index, "rev") == 0) + { + lua_pushinteger(L, r->revision); + } + else + { + lua_pushnil(L); + } + + return 1; +} + +static int syscalls_newindexVersion(lua_State* L) +{ + struct core_version* r = luaL_checkudata(L, 1, syscalls_Version_metatable_name); + const char* index = luaL_checkstring(L, 2); + uint32_t val = luaL_checkinteger(L, 3); + + if (strcmp(index, "a") == 0) + { + r->a = val; + } + else if (strcmp(index, "b") == 0) + { + r->b = val; + } + else if (strcmp(index, "c") == 0) + { + r->c = val; + } + else if (strcmp(index, "d") == 0) + { + r->d = val; + } + else if (strcmp(index, "rev") == 0) + { + r->revision = val; + } + else + { + luaL_error(L, "wrong index: %s", index); + } + + return 0; +} + +static int syscalls_eqVersion(lua_State* L) +{ + lua_pushboolean( + L, + memcmp( + luaL_checkudata(L, 1, syscalls_Version_metatable_name), + luaL_checkudata(L, 2, syscalls_Version_metatable_name), + sizeof(struct core_version) + ) + ); + + return 1; +} + +static int syscalls_ltVersion(lua_State* L) +{ + struct core_version* a = luaL_checkudata(L, 1, syscalls_Version_metatable_name); + struct core_version* b = luaL_checkudata(L, 2, syscalls_Version_metatable_name); + + bool r; + + if (a->a < b->a) + r = true; + else if (a->b < b->b) + r = true; + else if (a->c < b->c) + r = true; + else if (a->d < b->d) + r = true; + else if (a->revision < b->revision) + r = true; + else + r = false; + + lua_pushboolean(L, r); + + return 1; +} + +static int syscalls_leVersion(lua_State* L) +{ + struct core_version* a = luaL_checkudata(L, 1, syscalls_Version_metatable_name); + struct core_version* b = luaL_checkudata(L, 2, syscalls_Version_metatable_name); + + bool r; + + if (a->a <= b->a) + r = true; + else if (a->b <= b->b) + r = true; + else if (a->c <= b->c) + r = true; + else if (a->d <= b->d) + r = true; + else if (a->revision <= b->revision) + r = true; + else + r = false; + + lua_pushboolean(L, r); + return 1; +} + +static int syscalls_tostringVersion(lua_State* L) +{ + // AAA.BBB.CCC.DDD.RRRRRRRRRRRRR + // +1 на конце на всякий случай + char buff[4 + 4 + 4 + 4 + 13 + 1]; + struct core_version* a = luaL_checkudata(L, 1, syscalls_Version_metatable_name); + + sprintf(buff, "%d.%d.%d.%d.%d", a->a, a->b, a->c, a->d, a->revision); + + lua_pushstring(L, buff); + + return 1; +} + +static const luaL_Reg syscalls_Version_m[] = { + {"__index", syscalls_indexVersion}, + {"__newindex", syscalls_newindexVersion}, + {"__eq", syscalls_eqVersion}, + {"__lt", syscalls_ltVersion}, + {"__le", syscalls_ltVersion}, + {"__tostring", syscalls_tostringVersion}, + {NULL, NULL} +}; + +struct core_version* syscalls_pushVersion(lua_State* L) +{ + DEBUG_LINE("push Version entry"); + + struct core_version* entry = lua_newuserdata(L, sizeof(struct core_version)); + + luaL_setmetatable(L, syscalls_Version_metatable_name); + + return entry; +} + +static int syscalls_newVersion(lua_State* L) +{ + struct core_version* r = syscalls_pushVersion(L); + memset(r, 0, sizeof(struct core_version)); + + r->a = luaL_checkinteger(L, 1); + r->b = luaL_checkinteger(L, 2); + r->c = luaL_checkinteger(L, 3); + r->d = luaL_checkinteger(L, 4); + r->revision = luaL_checkinteger(L, 5); + + return 1; +} + +static const luaL_Reg syscalls_Version_lib[] = { + {"new", syscalls_newVersion}, + {NULL, NULL} +}; + +void syscalls_register_Version(lua_State* L) +{ + DEBUG_LINE("register Version entry"); + + luaL_newlib(L, syscalls_Version_lib); + + lua_setfield(L, -2, syscalls_Version_name); + + + luaL_newmetatable(L, syscalls_Version_metatable_name); + luaL_setfuncs(L, syscalls_Version_m, 0); + + lua_pop(L, 1); +} diff --git a/src/version/version_type.h b/src/version/version_type.h new file mode 100644 index 0000000..9fe66d7 --- /dev/null +++ b/src/version/version_type.h @@ -0,0 +1,28 @@ +#ifndef __VERSION_TYPE_H__ +#define __VERSION_TYPE_H__ + +#include +#include "../syscalls.h" + +// sub library name +#define syscalls_Version_name "CoreVersion" +#define syscalls_Version_metatable_name "Version table" +#define syscalls_Version_metatable_name "Version metatable" + +struct core_version +{ + uint8_t a; + uint8_t b; + uint8_t c; + uint8_t d; + uint8_t zero; + uint32_t revision; + + uint32_t unused; +}; + +struct core_version* syscalls_pushVersion(lua_State* L); + +void syscalls_register_Version(lua_State* L); + +#endif // __VERSION_TYPE_H__