From f3125a2b0bf1c95a6fa587317c205f3d0197cdba Mon Sep 17 00:00:00 2001 From: Tobias Gunkel Date: Sun, 24 Aug 2025 09:08:58 +0200 Subject: [PATCH] add rom boot read --- .gitignore | 5 ++++- src/gb.cpp | 29 +++++++++++++++++++++++++---- src/gb.h | 2 +- src/main.cpp | 6 +----- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index d512e27..fe0f632 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,7 @@ build uf2 .pio .vscode -*_bin.h \ No newline at end of file +*_bin.h +*.bin +*.gb +*.gbc diff --git a/src/gb.cpp b/src/gb.cpp index 6aadd35..9ed3f44 100644 --- a/src/gb.cpp +++ b/src/gb.cpp @@ -63,8 +63,29 @@ static void gb_error(struct gb_s* gb, const enum gb_error_e gb_err, const uint16 error(String("Error ") + gb_err + " occurred: " + gb_err_str[gb_err] + " at 0x" + String(addr, 16)); } -gb_init_error_e initGbContext() { - memcpy(rom_bank0, rom, sizeof(rom_bank0)); - return gb_init(&gb, &gb_rom_read, &gb_cart_ram_read, - &gb_cart_ram_write, &gb_error, NULL); +#ifdef USE_BOOT_ROM +#include "boot_cgb_bin.h" +#include "boot_gb_bin.h" +static uint8_t gb_bootrom_read(struct gb_s* gb, const uint_fast16_t addr) { + if (gb->cgb.cgbMode) { + return CGB_BOOT_ROM[addr]; + } else { + return GB_BOOT_ROM[addr]; + } +} +#endif + +void initGbContext() { + memcpy(rom_bank0, rom, sizeof(rom_bank0)); + + auto ret = gb_init(&gb, &gb_rom_read, &gb_cart_ram_read, &gb_cart_ram_write, &gb_error, NULL); + if (ret != GB_INIT_NO_ERROR) { + error(String("Error initializing emulator: ") + ret); + } + +#ifdef USE_BOOT_ROM + // gb_bootrom_read has to be set after gb_init(), as it sets it to NULL + gb_set_bootrom(&gb, &gb_bootrom_read); + gb_reset(&gb); +#endif } diff --git a/src/gb.h b/src/gb.h index b968942..6be0601 100644 --- a/src/gb.h +++ b/src/gb.h @@ -13,4 +13,4 @@ extern const uint8_t* rom; #define RAM_SIZE 32768 extern uint8_t ram[RAM_SIZE]; -gb_init_error_e initGbContext(); +void initGbContext(); diff --git a/src/main.cpp b/src/main.cpp index 51ec2ac..e826e1e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -64,11 +64,7 @@ void startEmulator() { #endif Serial.println("Init GB context ..."); - auto ret = initGbContext(); - if (ret != GB_INIT_NO_ERROR) { - Serial.printf("Error: %d\n", ret); - reset(); - } + initGbContext(); /* Automatically assign a colour palette to the game */ char rom_title[16];