From 773cd197a323d2f9b701addd63e30d54e43c74f5 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Thu, 20 Apr 2023 11:37:34 +0200 Subject: Add optional DMA usage on flipbuf This will copy an entire row of tiles instead of per tile copy, but for applications that clear the screen on each frame it will be more efficient. --- src/common.h | 4 ++++ src/ppu.c | 54 ++++++++++++++++++++++++++++-------------------------- src/ppu.h | 6 ------ 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/common.h b/src/common.h index e62c78c..1fbfa4c 100644 --- a/src/common.h +++ b/src/common.h @@ -419,12 +419,16 @@ dma_transfer_fill(void *dst, volatile u32 src, u32 count, int channel, u32 optio inline void dma_copy(void *dst, const void *src, u32 size, int channel) { dma_transfer_copy(dst, src, size / 4, channel, DMA_CHUNK_32 | DMA_ENABLE); + // Stall for 2 cycles in case we call this function more than once. + asm("nop"); asm("nop"); } // Fill the dst location with the word set at src. inline void dma_fill(void *dst, vu32 src, u32 size, int channel) { dma_transfer_fill(dst, src, size / 4, channel, DMA_CHUNK_32 | DMA_ENABLE); + // Stall for 2 cycles in case we call this function more than once. + asm("nop"); asm("nop"); } // diff --git a/src/ppu.c b/src/ppu.c index d68ab8f..bc6f503 100644 --- a/src/ppu.c +++ b/src/ppu.c @@ -16,6 +16,7 @@ WITH REGARD TO THIS SOFTWARE. */ #define NEW_PPU 1 +#define FLIPBUF_DMA 1 #define FG_FRONT ((u32*)(MEM_VRAM)) #define BG_FRONT ((u32*)(MEM_VRAM + KB(20))) @@ -722,32 +723,33 @@ putfontchar(u32 *layer, u16 tile_x, u16 tile_y, u8 ch, u8 color) { IWRAM_CODE void flipbuf() { - // Copy the fg and bg layers fully with the DMA. - dma_copy((u32*)FG_FRONT, (u32*)FG_BACK, KB(40), 3); - // dma_sync_copy((u32*)FG_FRONT, (u32*)FG_BACK, KB(40)); - // - // TODO: dma the dirty lines instead of per tile copying. - // dma_copy((u32*)BG_FRONT, (u32*)BG_BACK, KB(20), 3); - // dma_sync_copy((u32*)FG_FRONT, (u32*)FG_BACK, KB(20)); - // dma_sync_copy((u32*)BG_FRONT, (u32*)BG_BACK, KB(20)); - // Tile *mem_fg = FG_FRONT; - // Tile *mem_bg = BG_FRONT; - // for (size_t j = 0; j < 20; ++j) { - // if (dirty_tiles[j] == 0) { - // continue; - // } - - // // size_t k = 1; - // // for (size_t i = 0; i < 30; ++i, k <<= 1) { - // // if (dirty_tiles[j] & k) { - // // Tile *tile_fg = FG_BACK; - // // Tile *tile_bg = BG_BACK; - // // mem_fg[i + j * 32] = tile_fg[i + j * 32]; - // // mem_bg[i + j * 32] = tile_bg[i + j * 32]; - // // } - // // } - // dirty_tiles[j] = 0; - // } + u32 *fg_back = FG_BACK; + u32 *bg_back = BG_BACK; + u32 *bg_front = BG_FRONT; + u32 *fg_front = FG_FRONT; + for (size_t j = 0; j < 20; ++j) { + if (dirty_tiles[j] == 0) { + continue; + } +#if FLIPBUF_DMA == 1 + u32 offset = j * 32 * 8; + dma_copy(fg_front + offset, fg_back + offset, 32 * 8 * 4, 3); + dma_copy(bg_front + offset, bg_back + offset, 32 * 8 * 4, 3); +#else + size_t k = 1; + for (size_t i = 0; i < 30; ++i, k <<= 1) { + if (dirty_tiles[j] & k) { + Tile *mem_fg = FG_FRONT; + Tile *mem_bg = BG_FRONT; + Tile *tile_fg = FG_BACK; + Tile *tile_bg = BG_BACK; + mem_fg[i + j * 32] = tile_fg[i + j * 32]; + mem_bg[i + j * 32] = tile_bg[i + j * 32]; + } + } +#endif + dirty_tiles[j] = 0; + } } typedef struct KeyboardChar { diff --git a/src/ppu.h b/src/ppu.h index f12e1a7..bd9120c 100644 --- a/src/ppu.h +++ b/src/ppu.h @@ -17,12 +17,6 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE. */ -typedef struct Ppu { - u32 *bg, *fg; - u16 hor, ver, width, height; -} Ppu; - -int initppu(Ppu *p, u8 hor, u8 ver); void putcolors(u8 *addr); void ppu_pixel(u32 *layer, u16 x, u16 y, u8 color); void ppu_1bpp(u32 *layer, u16 x, u16 y, u8 *sprite, u8 color, u8 flipx, u8 flipy); -- cgit v1.2.1