add lifecycle functions for GB

This commit is contained in:
condret 2024-11-19 02:06:07 +01:00
parent 326006255a
commit 9d578edf4d
4 changed files with 79 additions and 4 deletions

View File

@ -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

View File

@ -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);

64
gb/gb.c Normal file
View File

@ -0,0 +1,64 @@
#include <gb.h>
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};
}

View File

@ -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