diff --git a/.gitignore b/.gitignore index 0066a6e..3619521 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ *.zip *.gb milskoboy -*.sav \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index c008e77..0000000 --- a/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# milskoboy - -Basic Gameboy emulator written using PeanutGB and [Milsko](https://gitea.nishi.boats/pyrite-dev/milsko). Not meant to be used seriously, instead meant to be portable enough to be usable as a test across any platforms Milsko supports. \ No newline at end of file diff --git a/main.c b/main.c index 4ea7b38..ee870d9 100644 --- a/main.c +++ b/main.c @@ -2,15 +2,20 @@ #include "peanut_gb.h" +#include +#include +#include +#include +#include +#include +#include + #include struct priv_t { - MwU8 *rom; - MwU8 *cart_ram; - MwU8 fb[LCD_WIDTH * LCD_HEIGHT * 4]; - - MwU64 save_size; - char *save_file_name; + uint8_t *rom; + uint8_t *cart_ram; + uint8_t fb[LCD_WIDTH * LCD_HEIGHT * 4]; MwWidget *win; MwWidget *fb_widget; @@ -28,10 +33,10 @@ int mfb_open(const char *title, int width, int height) { MwLibraryInit(); priv.win = MwCreateWidget(MwWindowClass, title, NULL, MwDEFAULT, MwDEFAULT, - width * 2, height * 2); + width, height); priv.fb_widget = - MwCreateWidget(MwImageClass, "fb", priv.win, 0, 0, width * 2, height * 2); + MwCreateWidget(MwImageClass, "fb", priv.win, 0, 0, width, height); MwAddUserHandler(priv.win, MwNkeyHandler, key_pressed, NULL); MwAddUserHandler(priv.win, MwNkeyReleaseHandler, key_released, NULL); @@ -143,7 +148,7 @@ static void key_released(MwWidget handle, void *user, void *client) { /** * Returns a byte from the ROM file at the given address. */ -MwU8 gb_rom_read(struct gb_s *gb, const uint_fast32_t addr) { +uint8_t gb_rom_read(struct gb_s *gb, const uint_fast32_t addr) { const struct priv_t *const p = gb->direct.priv; return p->rom[addr]; } @@ -151,73 +156,27 @@ MwU8 gb_rom_read(struct gb_s *gb, const uint_fast32_t addr) { /** * Returns a byte from the cartridge RAM at the given address. */ -MwU8 gb_cart_ram_read(struct gb_s *gb, const uint_fast32_t addr) { +uint8_t gb_cart_ram_read(struct gb_s *gb, const uint_fast32_t addr) { const struct priv_t *const p = gb->direct.priv; return p->cart_ram[addr]; } -void read_cart_ram_file(const char *save_file_name, MwU8 **dest, - const MwU64 len) { - FILE *f; - - /* If save file not required. */ - if (len == 0) { - *dest = NULL; - return; - } - - /* Allocate enough memory to hold save file. */ - *dest = malloc(len); - - if ((f = fopen(save_file_name, "rb")) == NULL) { - printf("Unable to open save file (%s) for reading: %s\n", save_file_name, - strerror(errno)); - return; - } - - /* Read save file to allocated memory. */ - fread(*dest, sizeof(MwU8), len, f); - fclose(f); -} - -void write_cart_ram_file(const char *save_file_name, MwU8 **dest, - const MwU64 len) { - FILE *f; - - if (len == 0 || *dest == NULL) - return; - - if ((f = fopen(save_file_name, "wb")) == NULL) { - printf("Unable to open save file (%s) for writing: %s\n", save_file_name, - strerror(errno)); - return; - } - - /* Record save file. */ - fwrite(*dest, sizeof(MwU8), len, f); - fclose(f); - - return; -} - /** * Writes a given byte to the cartridge RAM at the given address. */ void gb_cart_ram_write(struct gb_s *gb, const uint_fast32_t addr, - const MwU8 val) { + const uint8_t val) { const struct priv_t *const p = gb->direct.priv; p->cart_ram[addr] = val; - - write_cart_ram_file(priv.save_file_name, &priv.cart_ram, priv.save_size); } /** * Returns a pointer to the allocated space containing the ROM. Must be freed. */ -MwU8 *read_rom_to_ram(const char *file_name) { +uint8_t *read_rom_to_ram(const char *file_name) { FILE *rom_file = fopen(file_name, "rb"); - MwU64 rom_size; - MwU8 *rom = NULL; + size_t rom_size; + uint8_t *rom = NULL; if (rom_file == NULL) return NULL; @@ -227,7 +186,7 @@ MwU8 *read_rom_to_ram(const char *file_name) { rewind(rom_file); rom = malloc(rom_size); - if (fread(rom, sizeof(MwU8), rom_size, rom_file) != rom_size) { + if (fread(rom, sizeof(uint8_t), rom_size, rom_file) != rom_size) { free(rom); fclose(rom_file); return NULL; @@ -259,7 +218,7 @@ void gb_error(struct gb_s *gb, const enum gb_error_e gb_err, /** * Draws scanline into framebuffer. */ -void lcd_draw_line(struct gb_s *gb, const MwU8 pixels[160], +void lcd_draw_line(struct gb_s *gb, const uint8_t pixels[160], const uint_fast8_t line) { struct priv_t *priv = gb->direct.priv; const uint16_t palette[3][4] = {{0xFF, 0xA5, 0x52, 0x00}, @@ -278,11 +237,9 @@ void lcd_draw_line(struct gb_s *gb, const MwU8 pixels[160], } int main(int argc, char **argv) { - int save_timer = 0; + /* Must be freed */ char *rom_file_name = NULL; enum gb_init_error_e ret; - char *str_replace; - const char extension[] = ".sav"; switch (argc) { case 2: @@ -308,25 +265,11 @@ int main(int argc, char **argv) { printf("Error: %d\n", ret); exit(EXIT_FAILURE); } - /* Load Save File. */ - gb_get_save_size_s(&gb, &priv.save_size); - priv.cart_ram = malloc(priv.save_size); - - /* rom save name */ - priv.save_file_name = malloc(strlen(rom_file_name) + strlen(extension) + 1); - strcpy(priv.save_file_name, rom_file_name); - if ((str_replace = strrchr(priv.save_file_name, '.')) == NULL || - str_replace == priv.save_file_name) - str_replace = priv.save_file_name + strlen(priv.save_file_name); - for (unsigned int i = 0; i <= strlen(extension); i++) - *(str_replace++) = extension[i]; - - /* Only attempt to load a save file if the ROM actually supports saves.*/ - if (priv.save_size > 0) - read_cart_ram_file(priv.save_file_name, &priv.cart_ram, priv.save_size); + priv.cart_ram = malloc(gb_get_save_size(&gb)); gb_init_lcd(&gb, &lcd_draw_line); + // gb.direct.interlace = true; if (!mfb_open("Peanut-minifb", LCD_WIDTH, LCD_HEIGHT)) return EXIT_FAILURE; @@ -338,7 +281,9 @@ int main(int argc, char **argv) { struct timeval timecheck; int state; - start = MwTimeGetTick(); + gettimeofday(&timecheck, NULL); + start = (long)timecheck.tv_sec * 1000000 + (long)timecheck.tv_usec; + /* Execute CPU cycles until the screen has to be redrawn. */ gb_run_frame(&gb); @@ -348,7 +293,8 @@ int main(int argc, char **argv) { if (state < 0) break; - end = MwTimeGetTick(); + gettimeofday(&timecheck, NULL); + end = (long)timecheck.tv_sec * 1000000 + (long)timecheck.tv_usec; delay = target_speed_us - (end - start); @@ -361,9 +307,6 @@ int main(int argc, char **argv) { continue; usleep(delay); - if (save_timer++ >= 60) { - } - save_timer++; } mfb_close(); diff --git a/peanut_gb.h b/peanut_gb.h index f83c67c..de5d519 100644 --- a/peanut_gb.h +++ b/peanut_gb.h @@ -41,7 +41,11 @@ #define __has_include(x) 0 #endif -#include +#include /* Required for bool types */ +#include /* Required for int types */ +#include /* Required for abort */ +#include /* Required for memset */ +#include /* Required for tm struct */ /** * If PEANUT_GB_IS_LITTLE_ENDIAN is positive, then Peanut-GB will be configured @@ -3360,7 +3364,7 @@ void gb_run_frame(struct gb_s *gb) { __gb_step_cpu(gb); } -int gb_get_save_size_s(struct gb_s *gb, MwU64 *ram_size) { +int gb_get_save_size_s(struct gb_s *gb, size_t *ram_size) { const uint_fast16_t ram_size_location = 0x0149; const uint_fast32_t ram_sizes[] = { /* 0, 2KiB, 8KiB, 32KiB, 128KiB, 64KiB */ @@ -3765,7 +3769,7 @@ void gb_init_serial(struct gb_s *gb, * set. * \returns 0 on success, or -1 if the RAM size is invalid or unknown. */ -int gb_get_save_size_s(struct gb_s *gb, MwU64 *ram_size); +int gb_get_save_size_s(struct gb_s *gb, size_t *ram_size); /** * Deprecated. Use gb_get_save_size_s() instead.