From 20a3f908334a4978fcbbc1dd04c52078944f38ed Mon Sep 17 00:00:00 2001 From: Mahyar Koshkouei Date: Mon, 12 Feb 2024 16:13:46 +0000 Subject: [PATCH] main: use SDL functions only Removes mmap() and other non-portable instructions. Only SDL2 functions are used now. Tested working on Windows 10 in a MinGW64 development environment. This may fix issue #1, whereby a crash happens if rom.sav is not found. Now, if rom.sav is not found, 32kB is allocated, and then later saved on exit. Not tested on macOS. Signed-off-by: Mahyar Koshkouei --- deobfuscated.cc | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/deobfuscated.cc b/deobfuscated.cc index 3328b26..4836081 100644 --- a/deobfuscated.cc +++ b/deobfuscated.cc @@ -1,8 +1,4 @@ -#include -#include -#include -#include -#include +#include "SDL.h" #define OPCREL(_) opcrel = (opcode - _) / 8 @@ -136,14 +132,10 @@ uint8_t get_color(int tile, int y_offset, int x_offset) { return (tile_data[1] >> x_offset) % 2 * 2 + (*tile_data >> x_offset) % 2; } -int main() { - rom1 = (rom0 = (uint8_t *)mmap(0, 1 << 20, PROT_READ, MAP_SHARED, - open("rom.gb", O_RDONLY), 0)) + - 32768; - tmp = open("rom.sav", O_CREAT|O_RDWR, 0666); - ftruncate(tmp, 32768); - extrambank = extram = - (uint8_t *)mmap(0, 32768, PROT_READ | PROT_WRITE, MAP_SHARED, tmp, 0); +int main(int, char**) { + rom1 = (rom0 = (uint8_t *)SDL_LoadFile("rom.gb", NULL)) + 32768; + if(!rom0) return -1; + extrambank = extram = (uint8_t *)SDL_realloc(SDL_LoadFile("rom.sav", NULL), 32768); LCDC = 145; DIV = 44032; SDL_Init(SDL_INIT_VIDEO); @@ -422,8 +414,12 @@ int main() { SDL_RenderPresent(renderer); SDL_Event event; while (SDL_PollEvent(&event)) - if (event.type == SDL_QUIT) + if (event.type == SDL_QUIT) { + SDL_RWops *sav = SDL_RWFromFile("rom.sav", "wb"); + SDL_RWwrite(sav, extram, 1, 32768); + SDL_RWclose(sav); return 0; + } } LY = (LY + 1) % 154;