From 8dd5462525ea084436b4bb3bf6380f819dd9e6d3 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 6 Aug 2016 13:57:38 +0300 Subject: [PATCH] Correct DMA timing --- Core/gb.h | 2 +- Core/memory.c | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Core/gb.h b/Core/gb.h index aa8a62d..2f46ef0 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -243,7 +243,7 @@ typedef struct GB_gameboy_s { uint8_t dma_steps_left; uint8_t dma_current_dest; uint16_t dma_current_src; - uint16_t dma_cycles; + int16_t dma_cycles; ); /* MBC */ diff --git a/Core/memory.c b/Core/memory.c index 1038f38..a014cb9 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -432,8 +432,8 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) return; case GB_IO_DMA: - if (value <= 0xF1) { /* According to Pan Docs */ - gb->dma_cycles = 0; + if (value <= 0xE0) { + gb->dma_cycles = -7; gb->dma_current_dest = 0; gb->dma_current_src = value << 8; gb->dma_steps_left = 0xa0; @@ -574,7 +574,7 @@ void GB_dma_run(GB_gameboy_t *gb) { /* + 1 as a compensation over the fact that DMA is never started in the first internal cycle of an opcode, and SameBoy isn't sub-cycle accurate (yet?) . */ - while (gb->dma_cycles >= 4 + 1 && gb->dma_steps_left) { + while (gb->dma_cycles >= 4 && gb->dma_steps_left) { /* Todo: measure this value */ gb->dma_cycles -= 4; gb->dma_steps_left--; @@ -589,7 +589,7 @@ void GB_hdma_run(GB_gameboy_t *gb) if (!gb->hdma_on) return; /* + 1 as a compensation over the fact that HDMA is never started in the first internal cycle of an opcode, and SameBoy isn't sub-cycle accurate (yet?) . */ - while (gb->hdma_cycles >= 8 + 1) { + while (gb->hdma_cycles >= 8) { gb->hdma_cycles -= 8; // The CGB boot rom uses the dest in "absolute" space, while some games use it relative to VRAM. // This "normalizes" the dest to the CGB address space.