From b00fba1cd97d3646258d57458261c782fca8a087 Mon Sep 17 00:00:00 2001 From: Aren Elchinyan Date: Tue, 17 Sep 2024 22:22:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=B0=D1=80=D0=BA=D0=B0=D1=81=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=B7=D0=B0=D0=BF=D1=83=D1=81=D0=BA=D0=B0=20DOO?= =?UTF-8?q?M?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- configs/limine.cfg | 6 +- include/version.h | 2 +- modules/doom/build.sh | 8 ++- modules/doom/main.c | 131 +++++++++++++++++++++++++++++++++++++++++- 4 files changed, 138 insertions(+), 9 deletions(-) diff --git a/configs/limine.cfg b/configs/limine.cfg index ff6afbd..ade60d6 100644 --- a/configs/limine.cfg +++ b/configs/limine.cfg @@ -16,6 +16,9 @@ TERM_WALLPAPER=boot:///mod/boot.jpg KASLR=no KERNEL_PATH=boot:///kernel.elf + MODULE_PATH=boot:///mod/doom.ko + MODULE_CMDLINE=[MOD]doom.ko + MODULE_PATH=boot:///mod/pci_vendors.txt MODULE_CMDLINE=[PCI][DATA][VENDORS] @@ -34,8 +37,5 @@ TERM_WALLPAPER=boot:///mod/boot.jpg MODULE_PATH=boot:///mod/hello.ko MODULE_CMDLINE=[MOD]hello.ko - MODULE_PATH=boot:///mod/doom.ko - MODULE_CMDLINE=[MOD]doom.ko - MODULE_PATH=boot:///mod/ios.ko MODULE_CMDLINE=[MOD][IO]ios.ko \ No newline at end of file diff --git a/include/version.h b/include/version.h index 4b60dac..1dc30d4 100644 --- a/include/version.h +++ b/include/version.h @@ -1,3 +1,3 @@ #define VERSION_MAJOR 0 #define VERSION_MINOR 2 -#define VERSION_BUILD 257 +#define VERSION_BUILD 266 diff --git a/modules/doom/build.sh b/modules/doom/build.sh index f734441..4543831 100644 --- a/modules/doom/build.sh +++ b/modules/doom/build.sh @@ -9,8 +9,12 @@ if [ -d "../../sdk" ]; then CC="../../sdk/bin/x86_64-elf-gcc" fi -wget https://github.com/Daivuk/PureDOOM/raw/refs/heads/master/PureDOOM.h -wget https://github.com/Daivuk/PureDOOM/raw/refs/heads/master/doom1.wad +if [ ! -f "PureDOOM.h" ]; then + wget https://github.com/Daivuk/PureDOOM/raw/refs/heads/master/PureDOOM.h +fi +if [ ! -f "doom1.wad" ]; then + wget https://github.com/Daivuk/PureDOOM/raw/refs/heads/master/doom1.wad +fi $CC $ARCH_FLAGS -I../../modlib -finput-charset=UTF-8 -fexec-charset=cp1251 -c main.c -o doom.o $CC $ARCH_FLAGS -Wl,--entry=init,--build-id=none -T ../link.ld doom.o -L../../modlib/lib/ -lmod -o doom.ko diff --git a/modules/doom/main.c b/modules/doom/main.c index b37c8d3..7b96d03 100644 --- a/modules/doom/main.c +++ b/modules/doom/main.c @@ -1,16 +1,141 @@ +#define DOOM_IMPLEMENTATION #include "PureDOOM.h" #include static const char name[] = "[APP]DOOM"; static const char message[] = "DOOM :)"; -static int app_main( ) { - log_printf("[%s]\n", message); - return 2 + 2; +static void *doom_file_buffer = NULL; +static uint64_t doom_file_size = 0; +static uint64_t doom_file_seek = 0; +static uint32_t *doom_frame = NULL; +static int nsek = 100000000; + +static void d_printf(const char *str) { + log_printf("[DOOM] %s\n", str); +} + +static void *d_malloc(int size) { + void *addr = alloc(size); + // log_printf("[DOOM] %u - 0x%x\n", size, (uint64_t)addr); + return addr; +} + +static void d_free(void *addr) { + // log_printf("[DOOM] 0x%x\n", addr); + free(addr); +} + +static void d_gettime_fn(int *sec, int *usec) { + *sec = get_time( ).second; + //*usec = arch_get_tick( ) / 1000; + *usec = nsek; + nsek += 100000000; +} + +static void d_exit_fn(int code) { + log_printf("[DOOM] %d\n", code); + delete_thread( ); + for (;;) { asm volatile("hlt"); } +} + +static char *d_getenv_fn(const char *var) { + // log_printf("[DOOM] %s\n", var); + return "."; +} + +static void *d_open_fn(const char *filename, const char *mode) { + // log_printf("[DOOM] %s - %s\n", filename, mode); + + if (strcmp(filename, "@doom1.wad") == 0) { + return (void *)12; // Возвращаем произвольное значение для "handle" + } + + if (strcmp(filename, "./doom1.wad") == 0) { + return (void *)12; // Возвращаем произвольное значение для "handle" + } + return NULL; +} + +static int d_write_fn(void *handle, const void *buf, int count) { + // log_printf("[DOOM] %u\n", (unsigned int)handle); + if (handle == (void *)12) { + // Здесь можно записывать данные из buf в doom_file_buffer + // memcpy(doom_file_buffer, buf, count); + return count; + } + return 0; +} + +static void d_close_fn(void *handle) { + // log_printf("[DOOM] %u\n", (unsigned int)handle); + doom_file_seek = 0; +} + +static int d_read_fn(void *handle, void *buf, int count) { + // log_printf("[DOOM] %u\n", (unsigned int)handle); + if (handle == (void *)12) { + // Проверяем, чтобы не попытаться прочитать больше данных, чем доступно + if (doom_file_seek + count > doom_file_size) { count = (int)(doom_file_size - doom_file_seek); } + memcpy(buf, (uint8_t *)doom_file_buffer + doom_file_seek, count); + doom_file_seek += count; + return count; + } + return 0; +} + +static int d_seek_fn(void *handle, int offset, doom_seek_t origin) { + // log_printf("[DOOM] %u\n", (unsigned int)handle); + if (handle == (void *)12) { + switch (origin) { + case DOOM_SEEK_SET: doom_file_seek = offset; break; + case DOOM_SEEK_CUR: doom_file_seek += offset; break; + case DOOM_SEEK_END: doom_file_seek = doom_file_size + offset; break; + } + // Ограничиваем позицию указателя чтения/записи + if (doom_file_seek > doom_file_size) { doom_file_seek = doom_file_size; } + if (doom_file_seek < 0) { doom_file_seek = 0; } + return 0; + } + return -1; +} + +static int d_tell_fn(void *handle) { + // log_printf("[DOOM] %u\n", (unsigned int)handle); + if (handle == (void *)12) { return (int)doom_file_seek; } + return -1; +} + +static int d_eof_fn(void *handle) { + // log_printf("[DOOM] %u\n", (unsigned int)handle); + if (handle == (void *)12) { return doom_file_seek >= doom_file_size ? 1 : 0; } + return -1; +} + +static void app_main( ) { + framebuffer_t f = alloc_framebuffer( ); + log_printf("%x %ux%u %u\n", f.address, f.width, f.height, f.bpp); + char *argv[] = { "doom", "@doom1.wad" }; + doom_set_print(d_printf); + doom_set_malloc(d_malloc, d_free); + doom_set_file_io(d_open_fn, d_close_fn, d_read_fn, d_write_fn, d_seek_fn, d_tell_fn, d_eof_fn); + doom_set_gettime(d_gettime_fn); + doom_set_getenv(d_getenv_fn); + doom_set_exit(d_exit_fn); + doom_init(2, argv, 0); + doom_update( ); + doom_frame = (uint32_t *)doom_get_framebuffer(4 /* RGBA */); + for (;;) { + doom_update( ); + doom_frame = (uint32_t *)doom_get_framebuffer(4 /* RGBA */); + task_switch( ); + } } void __attribute__((section(".minit"))) init(env_t *env) { init_env(env); + framebuffer_t f = alloc_framebuffer( ); + log_printf("%x %ux%u %u\n", f.address, f.width, f.height, f.bpp); env->ret = &((module_info_t){ .name = (char *)&name, .message = (char *)&message, .type = 0,