From 9d578edf4d098e8e2b9d70b9e0d3e642d58d52c5 Mon Sep 17 00:00:00 2001 From: condret Date: Tue, 19 Nov 2024 02:06:07 +0100 Subject: [PATCH] add lifecycle functions for GB --- Makefile | 5 +++- gb/esil.c | 6 +++++ gb/gb.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ include/gb.h | 8 ++++--- 4 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 gb/gb.c diff --git a/Makefile b/Makefile index d80313b..cbc8f47 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ LDFLAGS = $(shell pkg-config --libs sdl2 r_util r_io) CFLAGS = -Wall -I include/ $(shell pkg-config --cflags sdl2 r_util r_io r_arch r_esil r_anal r_reg) -all: sdl/pixbuf.o io/timers.o io/mbc1.o io/mbc2.o io/joypad.o io/dma.o io/ppu.o io/interrupts.o gb/esil.o +all: sdl/pixbuf.o io/timers.o io/mbc1.o io/mbc2.o io/joypad.o io/dma.o io/ppu.o io/interrupts.o gb/esil.o gb/gb.o sdl/pixbuf.o: gcc -c sdl/pixbuf.c -o sdl/pixbuf.o $(CFLAGS) @@ -30,5 +30,8 @@ io/interrupts.o: gb/esil.o: gcc -c gb/esil.c -o gb/esil.o $(CFLAGS) +gb/gb.o: + gcc -c gb/gb.c -o gb/gb.o $(CFLAGS) + clean: rm sdl/*.o && rm io/*.o && rm gb/*.o diff --git a/gb/esil.c b/gb/esil.c index 841bea9..26dd457 100644 --- a/gb/esil.c +++ b/gb/esil.c @@ -75,11 +75,17 @@ bool gb_esil_setup (GB *gb) { if (!gb) { return false; } +#if 0 gb->esil = r_esil_new (4096, 0, 1); if (!gb->esil) { return false; } +#else + gb->esil = gb->anal->esil; +#endif + gb->anal->arch->esil = gb->anal->esil; r_esil_setup (gb->esil, gb->anal, false, false, false); + r_arch_esilcb (gb->anal->arch, R_ARCH_ESIL_ACTION_INIT); gb->esil->user = gb; r_esil_set_op (gb->esil, "halt", gb_custom_op_halt, 0, 0, R_ESIL_OP_TYPE_CUSTOM); r_esil_set_op (gb->esil, "stop", gb_custom_op_stop, 0, 0, R_ESIL_OP_TYPE_CUSTOM); diff --git a/gb/gb.c b/gb/gb.c new file mode 100644 index 0000000..a30edc6 --- /dev/null +++ b/gb/gb.c @@ -0,0 +1,64 @@ +#include + +bool gb_init(GB *gb, SDL_Renderer *renderer) { + gb->io = r_io_new (); + if (!gb->io) { + goto fail_io; + } + gb->io->va = true; + gb->io->autofd = false; + gb->io->overlay = false; + gb->io->cachemode = false; + gb->io->p_cache = 0; + gb->anal = r_anal_new (); + if (!gb->anal) { + goto fail_anal; + } + r_io_bind (gb->io, &gb->anal->iob); + r_anal_use (gb->anal, "gb"); + if (!gb_dma_init (&gb->dma, gb->io)) { + goto fail_dma; + } + if (!(gb_timers_init (&gb->timers, gb->io) && + gb_joypad_init (&gb->joypad, gb->io) && + gb_interrupts_init (&gb->interrupts, gb->io, gb->anal->reg))) { + goto fail_gb; + } + gb->ppu = gb_ppu_open (gb->io, renderer); + if (!gb->ppu) { + goto fail_gb; + } + gb_esil_setup (gb); + return true; +fail_gb: + gb_dma_fini (&gb->dma, gb->io); +fail_dma: + r_anal_free (gb->anal); +fail_anal: + r_io_free (gb->io); +fail_io: + gb[0] = (const GB){0}; + return false; +} + +void gb_fini(GB *gb) { + if (!gb) { + return; + } + if (gb->anal) { + r_arch_esilcb (gb->anal->arch, R_ARCH_ESIL_ACTION_FINI); + r_anal_free (gb->anal); + } + if (gb->ppu && gb->io) { + gb_ppu_close (gb->ppu, gb->io); + } + if (!gb->io) { + return; + } + gb_interrupts_fini (&gb->interrupts, gb->io); + gb_joypad_fini (&gb->joypad, gb->io); + gb_timers_fini (&gb->timers, gb->io); + gb_dma_fini (&gb->dma, gb->io); + r_io_free (gb->io); + gb[0] = (const GB){0}; +} diff --git a/include/gb.h b/include/gb.h index 2208674..098a6df 100644 --- a/include/gb.h +++ b/include/gb.h @@ -233,7 +233,7 @@ enum { typedef struct gameboy_t { RIO *io; - RArch *arch; +// RArch *arch; RAnal *anal; REsil *esil; GBTimers timers; @@ -257,11 +257,13 @@ GBPPU *gb_ppu_open (RIO *io, SDL_Renderer *renderer); void gb_ppu_continue (GB *gb, ut32 cycles); void gb_ppu_close (GBPPU *ppu, RIO *io); +bool gb_esil_setup (GB *gb); +bool gb_init (GB *gb, SDL_Renderer *renderer); +void gb_fini (GB *gb); + extern RIOPlugin r_io_plugin_gb_timers; extern RIOPlugin r_io_plugin_gb_mbc1; extern RIOPlugin r_io_plugin_gb_mbc2; extern RIOPlugin r_io_plugin_gb_joypad; -bool gb_esil_setup (GB *gb); - #endif