From 7cd4a38c600d29f42f03cbac407b1caa09a69be9 Mon Sep 17 00:00:00 2001 From: Alex Duchesne Date: Fri, 28 Jun 2024 14:47:15 -0400 Subject: [PATCH] test --- build_sdl2.bat | 63 ++++++++++++++++++++++ build_sdl2.sh | 66 +++++++++++++++++++++++ components/retro-go/rg_display.c | 90 +++++++++++++++++++++++++++++--- components/retro-go/rg_storage.c | 34 ++++++++++++ components/retro-go/rg_system.c | 24 ++++----- retro-go.code-workspace | 10 ++-- 6 files changed, 263 insertions(+), 24 deletions(-) create mode 100644 build_sdl2.bat create mode 100644 build_sdl2.sh diff --git a/build_sdl2.bat b/build_sdl2.bat new file mode 100644 index 000000000..622ddb7ab --- /dev/null +++ b/build_sdl2.bat @@ -0,0 +1,63 @@ +@echo off + +del launcher.exe retro-core.exe + + +tcc -Wall -DRG_TARGET_SDL2 -DRETRO_GO -DCJSON_HIDE_SYMBOLS -D_HAVE_STDINT_H=1 ^ + -Icomponents/retro-go ^ + -Icomponents/retro-go/libs/cJSON ^ + -Icomponents/retro-go/libs/lodepng ^ + -Ilauncher/main ^ + components/retro-go/*.c ^ + components/retro-go/fonts/*.c ^ + components/retro-go/libs/cJSON/*.c ^ + components/retro-go/libs/lodepng/*.c ^ + launcher/main/*.c ^ + -lSDL2 ^ + -o launcher.exe || goto :fail + + + +tcc -Wall -DRG_TARGET_SDL2 -DRETRO_GO -DCJSON_HIDE_SYMBOLS -D_HAVE_STDINT_H=1 ^ + -Icomponents/retro-go ^ + -Icomponents/retro-go/libs/cJSON ^ + -Icomponents/retro-go/libs/lodepng ^ + -Iretro-core/components/gnuboy ^ + -Iretro-core/components/gw-emulator/src ^ + -Iretro-core/components/gw-emulator/src/cpus ^ + -Iretro-core/components/gw-emulator/src/gw_sys ^ + -Iretro-core/components/nofrendo ^ + -Iretro-core/components/pce-go ^ + -Iretro-core/components/smsplus ^ + -Iretro-core/main ^ + components/retro-go/*.c ^ + components/retro-go/fonts/*.c ^ + components/retro-go/libs/cJSON/*.c ^ + components/retro-go/libs/lodepng/*.c ^ + retro-core/components/gnuboy/*.c ^ + retro-core/components/gw-emulator/src/*.c ^ + retro-core/components/gw-emulator/src/cpus/*.c ^ + retro-core/components/gw-emulator/src/gw_sys/*.c ^ + retro-core/components/nofrendo/mappers/*.c ^ + retro-core/components/nofrendo/nes/*.c ^ + retro-core/components/nofrendo/*.c ^ + retro-core/components/pce-go/*.c ^ + retro-core/components/smsplus/*.c ^ + retro-core/components/smsplus/cpu/*.c ^ + retro-core/components/smsplus/sound/*.c ^ + retro-core/main/*.c ^ + -lSDL2 ^ + -o retro-core.exe || goto :fail + + +:loop +launcher.exe && exit +retro-core.exe && exit +goto loop + + +exit + + +:fail +echo Compilation failed diff --git a/build_sdl2.sh b/build_sdl2.sh new file mode 100644 index 000000000..f53478d63 --- /dev/null +++ b/build_sdl2.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +rm -f launcher.exe retro-core.exe + +gcc -Wall -DRG_TARGET_SDL2 -DRETRO_GO -DCJSON_HIDE_SYMBOLS -DSDL_MAIN_HANDLED=1 \ + -Icomponents/retro-go \ + -Icomponents/retro-go/libs/cJSON \ + -Icomponents/retro-go/libs/lodepng \ + -Ilauncher/main \ + -Wno-address-of-packed-member \ + $(sdl2-config --cflags) \ + -Dapp_main=SDL_Main \ + components/retro-go/*.c \ + components/retro-go/fonts/*.c \ + components/retro-go/libs/cJSON/*.c \ + components/retro-go/libs/lodepng/*.c \ + launcher/main/*.c \ + $(sdl2-config --libs) \ + -lstdc++ \ + -o launcher.exe -O2 -pg -no-pie + + +gcc -Wall -DRG_TARGET_SDL2 -DRETRO_GO -DCJSON_HIDE_SYMBOLS -DSDL_MAIN_HANDLED=1 \ + -Icomponents/retro-go \ + -Icomponents/retro-go/libs/cJSON \ + -Icomponents/retro-go/libs/lodepng \ + -Iretro-core/components/gnuboy \ + -Iretro-core/components/gw-emulator/src \ + -Iretro-core/components/gw-emulator/src/cpus \ + -Iretro-core/components/gw-emulator/src/gw_sys \ + -Iretro-core/components/handy \ + -Iretro-core/components/nofrendo \ + -Iretro-core/components/pce-go \ + -Iretro-core/components/snes9x \ + -Iretro-core/components/smsplus \ + -Iretro-core/main \ + -Wno-address-of-packed-member \ + $(sdl2-config --cflags) \ + -Dapp_main=SDL_Main \ + components/retro-go/*.c \ + components/retro-go/fonts/*.c \ + components/retro-go/libs/cJSON/*.c \ + components/retro-go/libs/lodepng/*.c \ + retro-core/components/gnuboy/*.c \ + retro-core/components/gw-emulator/src/*.c \ + retro-core/components/gw-emulator/src/cpus/*.c \ + retro-core/components/gw-emulator/src/gw_sys/*.c \ + retro-core/components/handy/*.cpp \ + retro-core/components/nofrendo/mappers/*.c \ + retro-core/components/nofrendo/nes/*.c \ + retro-core/components/nofrendo/*.c \ + retro-core/components/pce-go/*.c \ + retro-core/components/snes9x/*.c \ + retro-core/components/smsplus/*.c \ + retro-core/components/smsplus/cpu/*.c \ + retro-core/components/smsplus/sound/*.c \ + retro-core/main/*.c \ + retro-core/main/*.cpp \ + $(sdl2-config --libs) \ + -lstdc++ \ + -o retro-core.exe -O2 -pg -no-pie + +./launcher.exe && ./retro-core.exe + +# && gprof.exe retro-core.exe gmon.out > profile.txt +# gdb -iex 'set pagination off' -ex run ./retro-core.exe diff --git a/components/retro-go/rg_display.c b/components/retro-go/rg_display.c index fcf388e2e..a340c08f2 100644 --- a/components/retro-go/rg_display.c +++ b/components/retro-go/rg_display.c @@ -286,12 +286,65 @@ static void lcd_deinit(void) // gpio_reset_pin(RG_GPIO_LCD_DC); } #else -#define lcd_init() #define lcd_deinit() -#define lcd_get_buffer() (void *)0 #define lcd_set_backlight(l) -#define lcd_send_data(a, b) -#define lcd_set_window(a, b, c, d) + +static SDL_Window *window; +static SDL_Surface *surface, *canvas; +static int win_left, win_top, win_width, win_height, cursor; +static uint16_t lcd_buffer[LCD_BUFFER_LENGTH]; + +static void lcd_set_window(int left, int top, int width, int height) +{ + int right = left + width - 1; + int bottom = top + height - 1; + if (left < 0 || top < 0 || right >= RG_SCREEN_WIDTH || bottom >= RG_SCREEN_HEIGHT) + RG_LOGW("Bad lcd window (x0=%d, y0=%d, x1=%d, y1=%d)\n", left, top, right, bottom); + win_left = left; + win_top = top; + win_width = width; + win_height = height; + cursor = 0; +} + +static inline void lcd_send_data(const uint16_t *buffer, size_t length) +{ + int bpp = canvas->format->BytesPerPixel; + int pitch = canvas->pitch; + void *pixels = canvas->pixels; + for (size_t i = 0; i < length; ++i) { + int real_top = win_top + (cursor / win_width); + int real_left = win_left + (cursor % win_width); + if (real_top >= RG_SCREEN_HEIGHT || real_left >= RG_SCREEN_WIDTH) + return; + uint16_t *dst = (void*)pixels + (real_top * pitch) + (real_left * bpp); + uint16_t pixel = buffer[i]; + *dst = ((pixel & 0xFF) << 8) | ((pixel & 0xFF00) >> 8);; + cursor++; + } +} + +static inline uint16_t *lcd_get_buffer(void) +{ + return lcd_buffer; +} + +static void lcd_sync(void) +{ + SDL_Event user_event; + user_event.type = SDL_USEREVENT; + user_event.user.code = 2; + user_event.user.data1 = NULL; + user_event.user.data2 = NULL; + SDL_PushEvent(&user_event); +} + +static void lcd_init(void) +{ + window = SDL_CreateWindow("Retro-Go", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, RG_SCREEN_WIDTH, RG_SCREEN_HEIGHT, 0); + surface = SDL_GetWindowSurface(window); + canvas = SDL_CreateRGBSurfaceWithFormat(0, display.screen.width, display.screen.height, 16, SDL_PIXELFORMAT_RGB565); +} #endif static inline unsigned blend_pixels(unsigned a, unsigned b) @@ -456,7 +509,7 @@ static inline void write_update(const rg_surface_t *update) else { // Return buffer - xQueueSend(spi_buffers, &line_buffer, portMAX_DELAY); + // xQueueSend(spi_buffers, &line_buffer, portMAX_DELAY); } lines_remaining -= lines_to_copy; @@ -563,8 +616,24 @@ static void display_task(void *arg) { const rg_surface_t *update; +#ifdef RG_TARGET_SDL2 + SDL_BlitSurface(canvas, NULL, surface, NULL); + SDL_UpdateWindowSurface(window); + + SDL_Event e; + while (SDL_PollEvent(&e) > 0) + { + if (e.type == SDL_QUIT) + rg_system_shutdown(); + if (e.type == SDL_USEREVENT) + break; + } + update = e.user.data1; + if (!update) + continue; +#else rg_queue_peek(display_task_queue, &update, -1); - // rg_queue_receive(display_task_queue, &update, -1); +#endif // Received a shutdown request! if (update == (void *)-1) @@ -711,7 +780,16 @@ void rg_display_submit(const rg_surface_t *update, uint32_t flags) display.changed = true; } +#ifdef RG_TARGET_SDL2 + SDL_Event user_event; + user_event.type = SDL_USEREVENT; + user_event.user.code = 2; + user_event.user.data1 = update; + user_event.user.data2 = NULL; + SDL_PushEvent(&user_event); +#else rg_queue_send(display_task_queue, &update, 1000); +#endif counters.blockTime += rg_system_timer() - time_start; counters.totalFrames++; diff --git a/components/retro-go/rg_storage.c b/components/retro-go/rg_storage.c index f5e4fd24e..c1cb0feb5 100644 --- a/components/retro-go/rg_storage.c +++ b/components/retro-go/rg_storage.c @@ -24,6 +24,40 @@ #include #define access _access #define mkdir(A, B) mkdir(A) +typedef struct { + char path[RG_PATH_MAX]; + HANDLE hFind; + WIN32_FIND_DATA data; + struct dirent {char d_name[RG_PATH_MAX];} ent; + size_t consumed; +} DIR; +DIR *opendir(const char *path) +{ + DIR *dir = calloc(1, sizeof(DIR)); + snprintf(dir->path, RG_PATH_MAX, "%s/*", path); + dir->hFind = FindFirstFile(dir->path, &dir->data); + dir->consumed = 0; + if (dir->hFind == INVALID_HANDLE_VALUE) { + free(dir); + return NULL; + } + strcpy(dir->ent.d_name, dir->data.cFileName); + return dir; +} +struct dirent *readdir(DIR *dir) +{ + if (!dir->consumed++) + return &dir->ent; + if (!FindNextFile(dir->hFind, &dir->data)) + return NULL; + strcpy(dir->ent.d_name, dir->data.cFileName); + return &dir->ent; +} +void closedir(DIR *dir) +{ + FindClose(dir->hFind); + free(dir); +} #else #include #include diff --git a/components/retro-go/rg_system.c b/components/retro-go/rg_system.c index 836bd928f..0baa5a492 100644 --- a/components/retro-go/rg_system.c +++ b/components/retro-go/rg_system.c @@ -287,6 +287,7 @@ static void platform_init(void) // freopen("stdout.txt", "w", stdout); // freopen("stderr.txt", "w", stderr); SDL_SetMainReady(); + SDL_SetHint(SDL_HINT_WINDOWS_DPI_AWARENESS, "system"); if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0) RG_PANIC("SDL Init failed!"); #endif @@ -532,51 +533,48 @@ void rg_task_yield(void) rg_queue_t *rg_queue_create(size_t length, size_t itemSize) { - return (rg_queue_t *)xQueueCreate(length, itemSize); + return NULL; } void rg_queue_free(rg_queue_t *queue) { - if (queue) - vQueueDelete((QueueHandle_t)queue); + // if (queue) + // vQueueDelete((QueueHandle_t)queue); } bool rg_queue_send(rg_queue_t *queue, const void *item, int timeoutMS) { - int ticks = timeoutMS < 0 ? portMAX_DELAY : pdMS_TO_TICKS(timeoutMS); - return xQueueSend((QueueHandle_t)queue, item, ticks) == pdTRUE; + return true; } bool rg_queue_receive(rg_queue_t *queue, void *out, int timeoutMS) { - int ticks = timeoutMS < 0 ? portMAX_DELAY : pdMS_TO_TICKS(timeoutMS); - return xQueueReceive((QueueHandle_t)queue, out, ticks) == pdTRUE; + return true; } bool rg_queue_receive_from_isr(rg_queue_t *queue, void *out, void *xTaskWokenByReceive) { - return xQueueReceiveFromISR((QueueHandle_t)queue, out, xTaskWokenByReceive) == pdTRUE; + return true; } bool rg_queue_peek(rg_queue_t *queue, void *out, int timeoutMS) { - int ticks = timeoutMS < 0 ? portMAX_DELAY : pdMS_TO_TICKS(timeoutMS); - return xQueuePeek((QueueHandle_t)queue, out, ticks) == pdTRUE; + return true; } bool rg_queue_is_empty(rg_queue_t *queue) { - return uxQueueMessagesWaiting((QueueHandle_t)queue) == 0; + return true; } bool rg_queue_is_full(rg_queue_t *queue) { - return uxQueueSpacesAvailable((QueueHandle_t)queue) == 0; + return false; } size_t rg_queue_messages_waiting(rg_queue_t *queue) { - return uxQueueMessagesWaiting((QueueHandle_t)queue); + return 0; } void rg_system_load_time(void) diff --git a/retro-go.code-workspace b/retro-go.code-workspace index 88b9c7ced..d9dca5814 100644 --- a/retro-go.code-workspace +++ b/retro-go.code-workspace @@ -39,22 +39,22 @@ "RG_ENABLE_NETPLAY=", "RG_ENABLE_NETWORKING=", "RTC_NOINIT_ATTR=", - "ESP_PLATFORM=" + "RG_TARGET_SDL2=" ], "C_Cpp.default.includePath": [ "${workspaceFolder}/components/**", "${workspaceFolder}/../components/**", - "${config:retro-go.sdk-path}/components/**", - "${config:retro-go.tools-path}/include/**" + "C:/msys64/mingw64/include/**" ], "C_Cpp.default.forcedInclude": [ "${workspaceFolder}/build/config/sdkconfig.h", "${workspaceFolder}/build/sdkconfig.h", ], - "C_Cpp.default.compilerPath": "${config:retro-go.tools-path}/bin/xtensa-esp32-elf-g++.exe", + "C_Cpp.default.compilerPath": "C:/msys64/mingw64/bin/gcc.exe", "C_Cpp.default.cStandard": "c11", "C_Cpp.default.cppStandard": "c++11", - "C_Cpp.default.intelliSenseMode": "gcc-x86", + "C_Cpp.default.intelliSenseMode": "gcc-x64", +// "C_Cpp.debugShortcut": false, "cSpell.words": [ "apll", "coleco",