From da574b6a107b63fb0de41a642243c2037c6b980e Mon Sep 17 00:00:00 2001 From: Forest Belton Date: Wed, 29 Sep 2021 03:48:51 -0400 Subject: [PATCH] Add VRAM engine for copying tile data --- src/main.c | 2 ++ src/title.c | 3 +++ src/vram.c | 19 +++++++++++++++++++ src/vram.h | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 src/vram.c create mode 100644 src/vram.h diff --git a/src/main.c b/src/main.c index c8dc35e..335524e 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include "game.h" #include "sdk/hardware.h" #include "sdk/oam.h" +#include "vram.h" game_state_t game_state; @@ -12,6 +13,7 @@ void main() { // Setup the OAM for sprite drawing oam_init(); + vram_init(); // Set up background & sprites rBGP = 0b11100100; diff --git a/src/title.c b/src/title.c index 1b7778f..be6e638 100644 --- a/src/title.c +++ b/src/title.c @@ -6,6 +6,7 @@ #include "sdk/joypad.h" #include "sdk/oam.h" #include "sdk/video.h" +#include "vram.h" ASSET(bg_tiles, "bg/title.2bpp"); ASSET(bg_map, "bg/title.map"); @@ -39,5 +40,7 @@ void title(void) { // Wait for VBLANK. Clear IF since global interrupts are disabled. HALT(); rIF = 0; + + vram_update(); } } diff --git a/src/vram.c b/src/vram.c new file mode 100644 index 0000000..5e93fb0 --- /dev/null +++ b/src/vram.c @@ -0,0 +1,19 @@ +#include "vram.h" + +#include + +#include "sdk/video.h" + +mem_xfer_t mem_xfer_queue[MAX_XFERS]; +uint8_t mem_xfer_queue_size; + +void vram_init(void) { mem_xfer_queue_size = 0; } + +void vram_update(void) { + mem_xfer_t *xfer = &mem_xfer_queue[0]; + for (uint8_t i = 0; i < mem_xfer_queue_size; i++) { + vram_memcpy((uint16_t)xfer->dest, (void *)xfer->source, xfer->size); + xfer++; + } + mem_xfer_queue_size = 0; +} diff --git a/src/vram.h b/src/vram.h new file mode 100644 index 0000000..59fa840 --- /dev/null +++ b/src/vram.h @@ -0,0 +1,35 @@ +#ifndef IS_VRAM_H_ +#define IS_VRAM_H_ + +#include + +#define MAX_XFERS 5 + +typedef struct { + uint8_t *dest; + const uint8_t *source; + uint8_t size; +} mem_xfer_t; + +extern mem_xfer_t mem_xfer_queue[MAX_XFERS]; +extern uint8_t mem_xfer_queue_size; + +/** + * @brief Initialize VRAM engine + */ +void vram_init(void); + +/** + * @brief Process VRAM memory transfers + */ +void vram_update(void); + +#define vram_enqueue_mem_xfer(_dest, _source, _size) \ + do { \ + mem_xfer_t *node = &mem_xfer_queue[mem_xfer_queue_size++]; \ + node->dest = (_dest); \ + node->source = (_source); \ + node->size = (_size); \ + } while (0) + +#endif