From 21875865c4430a1a32160315090947fdd0f458b8 Mon Sep 17 00:00:00 2001 From: condret Date: Mon, 28 Oct 2024 21:03:15 +0100 Subject: [PATCH] Start working on ppu --- include/gb.h | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- io/dma.c | 18 +++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/include/gb.h b/include/gb.h index 031dc50..709dd3b 100644 --- a/include/gb.h +++ b/include/gb.h @@ -7,6 +7,7 @@ enum { GB_TIMERS_TIMA, GB_TIMERS_TMA, GB_TIMERS_TAC, + GB_TIMERS_N_REGS, }; typedef struct gb_timers_t { @@ -47,6 +48,7 @@ typedef struct gb_dma_t { ut64 seek; //17 bit seek and some flags int dma_fd; //fd representing the dma register int dma_bus_fd; //fd representing the memory bus while dma occupies it + int oam_fd; //fd representing oam ut32 dma_bank_id; ut32 default_bank_id; // ut16 bus_occupancy_size; @@ -60,11 +62,58 @@ typedef struct gb_dma_t { #define GB_DMA_LAUNCH 0x20000 #define GB_DMA_RUNNING 0x40000 #define GB_DMA_ACTIVE 0x60000 +#define GB_DMA_CGB_MODE 0x80000 -GBDMA *gb_dma_open(RIO *io, bool cgb); +GBDMA *gb_dma_open(RIO *io); +//void gb_dma_enable_cgb(GBDMA *dma); void gb_dma_update(GBDMA *dma, RIO *io, ut32 cycles, bool pre_exec); void gb_dma_close(GBDMA *dma, RIO *io); +enum { + GB_PPU_LCDC = 0, //0xff40 + GB_PPU_STAT, //0xff41 + GB_PPU_SCY, //0xff42 + GB_PPU_SCX, //0xff43 + GB_PPU_LY, //0xff44 + GB_PPU_LYC, //0xff45 + GB_PPU_BGP, //0xff47 + GB_PPU_OBP0, //0xff48 + GB_PPU_OBP1, //0xff49 + GB_PPU_WY, //0xff4a + GB_PPU_WX, //0xff4b + GB_PPU_N_REGS, +}; + +typedef struct gb_dmg_ppu_t { + ut64 seek; + ut8 buf[GB_PPU_N_REGS]; + int reg_fd; + int vram_fd; +} GBPPU; + +#define GB_PPU_MODE0 0 +#define GB_PPU_MODE1 0x10000 +#define GB_PPU_MODE2 0x20000 +#define GB_PPU_MODE3 0x30000 +#define GB_PPU_MODE_MASK 0x30000 +#define GB_PPU_DOUBLE_SPEED 0x40000 + +typedef struct gameboy_t { + RIO *io; + RArch *arch; + GBTimers *timers; + GBJoypad *joypad; + GBDMA *dma; + GBPPU *ppu; + GBPixBuf *pixbuf; + int cartrigde_fd; + +} GB; + +GBPPU *gb_ppu_open (RIO *io) +void gb_ppu_update (GB *gb, ut32 cycles); +void gb_ppu_close (GBPPU *ppu); + extern RIOPlugin r_io_plugin_gb_timers; extern RIOPlugin r_io_plugin_gb_mbc1; extern RIOPlugin r_io_plugin_gb_mbc2; diff --git a/io/dma.c b/io/dma.c index 5dd8040..2d18a7c 100644 --- a/io/dma.c +++ b/io/dma.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -171,7 +172,7 @@ RIOPlugin r_io_plugin_gb_dma = { .write = __write, }; -GBDMA *gb_dma_open (RIO *io, bool cgb) { +GBDMA *gb_dma_open (RIO *io) { GBDMA *dma = R_NEW0 (GBDMA); if (!dma) { return NULL; @@ -190,10 +191,18 @@ GBDMA *gb_dma_open (RIO *io, bool cgb) { dma->dma_bank_id = bank->id; char uri[64]; memset (uri, 0x00, sizeof (char) * 64); + strcpy (uri, "malloc://0xa0"); + dma->oam_fd = r_io_fd_open (io, uri, R_PERM_RWX, 0); + if (oam_fd < 0) { + r_io_bank_free (bank); + free (dma); + return NULL; + } sprintf (uri, "gb_dma://%p", dma); RIODesc *desc = r_io_desc_open_plugin (io, &r_io_plugin_gb_dma, uri, R_PERM_RWX, 0); if (!desc) { + r_io_fd_close (io, dma->oam_fd); r_io_bank_free (bank); free (dma); return NULL; @@ -202,6 +211,7 @@ GBDMA *gb_dma_open (RIO *io, bool cgb) { sprintf (uri, "gb_dma_bus://%p", dma); desc = r_io_desc_open_plugin (io, &r_io_plugin_gb_dma_bus, uri, R_PERM_RWX, 0); if (!desc) { + r_io_fd_close (io, dma->oam_fd); r_io_fd_close (io, dma->dma_fd); r_io_bank_free (bank); free (dma); @@ -236,8 +246,13 @@ void gb_dma_update (GBDMA *dma, RIO *io, ut32 cycles, bool pre_exec) { dma->frontrun = ((st32)cycles) * (-1); } if (cycles) { +#if 1 + r_io_fd_write_at (io, dma->oam_fd, (ut64)dma->dst, + &dma->buf[dma->dst], cycles); +#else r_io_bank_write_at (io, dma->default_bank_id, 0xfe00 | dma->dst, &dma->buf[dma->dst], cycles); +#endif dma->dst += cycles; } if (pre_exec) { @@ -257,5 +272,6 @@ void gb_dma_close (GBDMA *dma, RIO *io) { r_io_bank_del (io, dma->dma_bank_id); r_io_fd_close (io, dma->dma_bus_fd); r_io_fd_close (io, dma->dma_fd); + r_io_fd_close (io, dma->oam_fd); free (dma); }